WebViewTextureSample.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // Copyright aXiuShen. All Rights Reserved.
  2. #pragma once
  3. #include "CoreTypes.h"
  4. #include "CoreGlobals.h"
  5. #include "Containers/Queue.h"
  6. #include "Math/IntPoint.h"
  7. #include "Math/Range.h"
  8. #include "MediaObjectPool.h"
  9. #include "RHI.h"
  10. #include "RHIUtilities.h"
  11. #include "Templates/SharedPointer.h"
  12. /**
  13. * Texture sample generated by the WebBrowser.
  14. */
  15. class FWebViewTextureSample
  16. : public IMediaPoolable
  17. {
  18. public:
  19. /** Default constructor. */
  20. FWebViewTextureSample()
  21. : Buffer(nullptr)
  22. , BufferSize(0)
  23. , Dim(FIntPoint::ZeroValue)
  24. , ScaleRotation(FLinearColor(1.0f, 0.0f, 0.0f, 1.0f))
  25. , Offset(FLinearColor(0.0f, 0.0f, 0.0f, 0.0f))
  26. { }
  27. /** Virtual destructor. */
  28. virtual ~FWebViewTextureSample()
  29. {
  30. if (BufferSize > 0)
  31. {
  32. FMemory::Free(Buffer);
  33. }
  34. }
  35. public:
  36. /**
  37. * Get a writable pointer to the sample buffer.
  38. *
  39. * @return Sample buffer.
  40. */
  41. void* GetMutableBuffer()
  42. {
  43. return Buffer;
  44. }
  45. /**
  46. * Initialize the sample.
  47. *
  48. * @param InDim The sample buffer's width and height (in pixels).
  49. * @param InDuration The duration for which the sample is valid.
  50. * @return true on success, false otherwise.
  51. */
  52. bool Initialize(const FIntPoint& InDim)
  53. {
  54. if (InDim.GetMin() <= 0)
  55. {
  56. return false;
  57. }
  58. Dim = InDim;
  59. return true;
  60. }
  61. /**
  62. * Initialize the sample with a memory buffer.
  63. *
  64. * @param InBuffer The buffer containing the sample data.
  65. * @param Copy Whether the buffer should be copied (true) or referenced (false).
  66. * @see InitializeTexture
  67. */
  68. void InitializeBuffer(void* InBuffer, bool Copy)
  69. {
  70. if (Copy)
  71. {
  72. SIZE_T RequiredBufferSize = Dim.X * Dim.Y * sizeof(int32);
  73. if (BufferSize < RequiredBufferSize)
  74. {
  75. if (BufferSize == 0)
  76. {
  77. Buffer = FMemory::Malloc(RequiredBufferSize);
  78. }
  79. else
  80. {
  81. Buffer = FMemory::Realloc(Buffer, RequiredBufferSize);
  82. }
  83. BufferSize = RequiredBufferSize;
  84. }
  85. FMemory::Memcpy(Buffer, InBuffer, RequiredBufferSize);
  86. }
  87. else
  88. {
  89. if (BufferSize > 0)
  90. {
  91. FMemory::Free(Buffer);
  92. BufferSize = 0;
  93. }
  94. Buffer = InBuffer;
  95. }
  96. }
  97. /**
  98. * Initialize the sample for copy externally.
  99. *
  100. * @return The size of the buffer
  101. * @see InitializeTexture
  102. */
  103. int32 InitializeBufferForCopy()
  104. {
  105. SIZE_T RequiredBufferSize = Dim.X * Dim.Y * sizeof(int32);
  106. if (BufferSize < RequiredBufferSize)
  107. {
  108. if (BufferSize == 0)
  109. {
  110. Buffer = FMemory::Malloc(RequiredBufferSize);
  111. }
  112. else
  113. {
  114. Buffer = FMemory::Realloc(Buffer, RequiredBufferSize);
  115. }
  116. BufferSize = RequiredBufferSize;
  117. }
  118. return BufferSize;
  119. }
  120. /**
  121. * Initialize the sample with a texture resource.
  122. *
  123. * @return The texture resource object that will hold the sample data.
  124. * @note This method must be called on the render thread.
  125. * @see InitializeBuffer
  126. */
  127. FRHITexture2D* InitializeTexture()
  128. {
  129. check(IsInRenderingThread());
  130. if (Texture.IsValid() && (Texture->GetSizeXY() == Dim))
  131. {
  132. return Texture;
  133. }
  134. #if WEBVIEW_ENGINE_VERSION>=50100
  135. const FRHITextureCreateDesc Desc =
  136. FRHITextureCreateDesc::Create2D(TEXT("FWebViewTextureSample"))
  137. .SetExtent(Dim)
  138. .SetFormat(PF_B8G8R8A8)
  139. .SetFlags(ETextureCreateFlags::Dynamic | ETextureCreateFlags::SRGB | ETextureCreateFlags::RenderTargetable | ETextureCreateFlags::ShaderResource)
  140. .SetInitialState(ERHIAccess::SRVMask);
  141. Texture = RHICreateTexture(Desc);
  142. #else
  143. const ETextureCreateFlags CreateFlags = TexCreate_Dynamic | TexCreate_SRGB;
  144. TRefCountPtr<FRHITexture2D> DummyTexture2DRHI;
  145. #if WEBVIEW_ENGINE_VERSION>=50000
  146. FRHIResourceCreateInfo CreateInfo(TEXT("FWebViewTextureSample"));
  147. #else
  148. FRHIResourceCreateInfo CreateInfo;
  149. #endif
  150. RHICreateTargetableShaderResource2D(
  151. Dim.X,
  152. Dim.Y,
  153. PF_B8G8R8A8,
  154. 1,
  155. CreateFlags,
  156. TexCreate_RenderTargetable,
  157. false,
  158. CreateInfo,
  159. Texture,
  160. DummyTexture2DRHI
  161. );
  162. #endif
  163. return Texture;
  164. }
  165. /**
  166. * Set the sample Scale, Rotation, Offset.
  167. *
  168. * @param InScaleRotation The sample scale and rotation transform (2x2).
  169. * @param InOffset The sample offset.
  170. */
  171. void SetScaleRotationOffset(FVector4& InScaleRotation, FVector4& InOffset)
  172. {
  173. ScaleRotation = FLinearColor(InScaleRotation.X, InScaleRotation.Y, InScaleRotation.Z, InScaleRotation.W);
  174. Offset = FLinearColor(InOffset.X, InOffset.Y, InOffset.Z, InOffset.W);
  175. }
  176. public:
  177. //~ IMediaTextureSample interface
  178. virtual const void* GetBuffer()
  179. {
  180. return Buffer;
  181. }
  182. virtual FIntPoint GetDim() const
  183. {
  184. return Dim;
  185. }
  186. virtual uint32 GetStride() const
  187. {
  188. return Dim.X * sizeof(int32);
  189. }
  190. #if WITH_ENGINE
  191. virtual FRHITexture* GetTexture() const
  192. {
  193. return Texture.GetReference();
  194. }
  195. #endif //WITH_ENGINE
  196. virtual FLinearColor GetScaleRotation() const
  197. {
  198. return ScaleRotation;
  199. }
  200. virtual FLinearColor GetOffset() const
  201. {
  202. return Offset;
  203. }
  204. private:
  205. /** The sample's data buffer. */
  206. void* Buffer;
  207. /** Current allocation size of Buffer. */
  208. SIZE_T BufferSize;
  209. /** Width and height of the texture sample. */
  210. FIntPoint Dim;
  211. /** ScaleRotation for the sample. */
  212. FLinearColor ScaleRotation;
  213. /** Offset for the sample. */
  214. FLinearColor Offset;
  215. #if WITH_ENGINE
  216. /** Texture resource. */
  217. TRefCountPtr<FRHITexture2D> Texture;
  218. #endif //WITH_ENGINE
  219. };
  220. class FWebViewTextureSampleQueue
  221. {
  222. public:
  223. /** Default constructor. */
  224. FWebViewTextureSampleQueue()
  225. : NumSamples(0)
  226. , PendingFlushes(0)
  227. { }
  228. /** Virtual destructor. */
  229. virtual ~FWebViewTextureSampleQueue() { }
  230. public:
  231. /**
  232. * Get the number of samples in the queue.
  233. *
  234. * @return Number of samples.
  235. * @see Enqueue, Dequeue, Peek
  236. */
  237. int32 Num() const
  238. {
  239. return NumSamples;
  240. }
  241. public:
  242. //~ TMediaSampleSource interface (to be called only from consumer thread)
  243. virtual bool Dequeue(TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe>& OutSample)
  244. {
  245. DoPendingFlushes();
  246. TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe> Sample;
  247. if (!Samples.Peek(Sample))
  248. {
  249. return false; // empty queue
  250. }
  251. if (!Sample.IsValid())
  252. {
  253. return false; // pending flush
  254. }
  255. Samples.Pop();
  256. FPlatformAtomics::InterlockedDecrement(&NumSamples);
  257. check(NumSamples >= 0);
  258. OutSample = Sample;
  259. return true;
  260. }
  261. virtual bool Peek(TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe>& OutSample)
  262. {
  263. DoPendingFlushes();
  264. TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe> Sample;
  265. if (!Samples.Peek(Sample))
  266. {
  267. return false; // empty queue
  268. }
  269. if (!Sample.IsValid())
  270. {
  271. return false; // pending flush
  272. }
  273. OutSample = Sample;
  274. return true;
  275. }
  276. virtual bool Pop()
  277. {
  278. TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe> Sample;
  279. if (!Samples.Peek(Sample))
  280. {
  281. return false; // empty queue
  282. }
  283. if (!Sample.IsValid())
  284. {
  285. return false; // pending flush
  286. }
  287. Samples.Pop();
  288. FPlatformAtomics::InterlockedDecrement(&NumSamples);
  289. check(NumSamples >= 0);
  290. return true;
  291. }
  292. public:
  293. //~ TMediaSampleSink interface (to be called only from producer thread)
  294. virtual bool Enqueue(const TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe>& Sample)
  295. {
  296. if (Sample.IsValid())
  297. {
  298. FPlatformAtomics::InterlockedIncrement(&NumSamples);
  299. }
  300. if (!Samples.Enqueue(Sample))
  301. {
  302. if (Sample.IsValid())
  303. {
  304. FPlatformAtomics::InterlockedDecrement(&NumSamples);
  305. }
  306. return false;
  307. }
  308. return true;
  309. }
  310. virtual void RequestFlush()
  311. {
  312. Samples.Enqueue(nullptr); // insert flush marker
  313. FPlatformAtomics::InterlockedIncrement(&PendingFlushes);
  314. }
  315. protected:
  316. /** Perform any pending flushes. */
  317. void DoPendingFlushes()
  318. {
  319. TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe> Sample;
  320. while ((PendingFlushes > 0) && Samples.Dequeue(Sample))
  321. {
  322. if (Sample.IsValid())
  323. {
  324. FPlatformAtomics::InterlockedDecrement(&NumSamples);
  325. check(NumSamples >= 0);
  326. }
  327. else
  328. {
  329. FPlatformAtomics::InterlockedDecrement(&PendingFlushes);
  330. }
  331. }
  332. }
  333. private:
  334. /** Number of samples in the queue. */
  335. int32 NumSamples;
  336. /** Number of pending flushes. */
  337. int32 PendingFlushes;
  338. /** Audio sample queue. */
  339. TQueue<TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe>, EQueueMode::Mpsc> Samples;
  340. };
  341. /** Implements a pool for WebBrowser's texture sample objects. */
  342. class FFWebViewTextureSamplePool : public TMediaObjectPool<FWebViewTextureSample> { };