GpuMemoryBuffer.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <shared_mutex>
  3. #include <common_video/include/video_frame_buffer.h>
  4. #include <rtc_base/ref_counted_object.h>
  5. #include "GraphicsDevice/GraphicsDevice.h"
  6. #include "IUnityRenderingExtensions.h"
  7. #include "PlatformBase.h"
  8. #include "Size.h"
  9. #if CUDA_PLATFORM
  10. #include <cuda.h>
  11. #endif
  12. namespace unity
  13. {
  14. namespace webrtc
  15. {
  16. using namespace ::webrtc;
  17. struct GpuMemoryBufferHandle
  18. {
  19. GpuMemoryBufferHandle();
  20. GpuMemoryBufferHandle(GpuMemoryBufferHandle&& other);
  21. GpuMemoryBufferHandle& operator=(GpuMemoryBufferHandle&& other);
  22. virtual ~GpuMemoryBufferHandle();
  23. };
  24. class ITexture2D;
  25. class GpuMemoryBufferInterface : public rtc::RefCountInterface
  26. {
  27. public:
  28. virtual Size GetSize() const = 0;
  29. virtual UnityRenderingExtTextureFormat GetFormat() const = 0;
  30. virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0;
  31. virtual const GpuMemoryBufferHandle* handle() const = 0;
  32. protected:
  33. ~GpuMemoryBufferInterface() override = default;
  34. };
  35. class GpuMemoryBufferFromUnity : public GpuMemoryBufferInterface
  36. {
  37. public:
  38. GpuMemoryBufferFromUnity(
  39. IGraphicsDevice* device, NativeTexPtr ptr, const Size& size, UnityRenderingExtTextureFormat format);
  40. GpuMemoryBufferFromUnity(const GpuMemoryBufferFromUnity&) = delete;
  41. GpuMemoryBufferFromUnity& operator=(const GpuMemoryBufferFromUnity&) = delete;
  42. void ResetSync();
  43. void CopyBuffer(NativeTexPtr ptr);
  44. UnityRenderingExtTextureFormat GetFormat() const override;
  45. Size GetSize() const override;
  46. rtc::scoped_refptr<I420BufferInterface> ToI420() override;
  47. const GpuMemoryBufferHandle* handle() const override;
  48. protected:
  49. ~GpuMemoryBufferFromUnity() override;
  50. private:
  51. IGraphicsDevice* device_;
  52. UnityRenderingExtTextureFormat format_;
  53. Size size_;
  54. std::unique_ptr<ITexture2D> texture_;
  55. std::unique_ptr<ITexture2D> textureCpuRead_;
  56. std::unique_ptr<GpuMemoryBufferHandle> handle_;
  57. };
  58. }
  59. }