WebViewTextureResource.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright aXiuShen. All Rights Reserved.
  2. #pragma once
  3. #include "CoreTypes.h"
  4. #include "Containers/Queue.h"
  5. #include "Logging/LogMacros.h"
  6. #include "Math/Color.h"
  7. #include "Misc/Guid.h"
  8. #include "Misc/Timespan.h"
  9. #include "Templates/RefCounting.h"
  10. #include "Templates/SharedPointer.h"
  11. #include "TextureResource.h"
  12. #include "UnrealClient.h"
  13. #include "WebViewTextureSample.h"
  14. class UWebViewTexture;
  15. //WEBBROWSERTEXTURE_API DECLARE_LOG_CATEGORY_EXTERN(LogWebViewTexture, Log, All);
  16. /**
  17. * Texture resource type for webbrowser textures.
  18. */
  19. class FWebViewTextureResource
  20. : public FRenderTarget
  21. , public FTextureResource
  22. {
  23. public:
  24. /**
  25. * Creates and initializes a new instance.
  26. *
  27. * @param InOwner The Movie texture object to create a resource for (must not be nullptr).
  28. * @param InOwnerDim The initial width and height of the texture.
  29. * @param InOwnerSize The initial memory allocated to the texture.
  30. */
  31. FWebViewTextureResource(UWebViewTexture& InOwner, FIntPoint& InOwnerDim, SIZE_T& InOwnerSize);
  32. /** Virtual destructor. */
  33. virtual ~FWebViewTextureResource() { }
  34. public:
  35. /** Parameters for the Render method. */
  36. struct FRenderParams
  37. {
  38. /** The clear color to use when clearing the texture. */
  39. FLinearColor ClearColor;
  40. /** Guid associated with the texture. */
  41. FGuid PlayerGuid;
  42. /** The sample source to render. */
  43. TWeakPtr<FWebViewTextureSampleQueue, ESPMode::ThreadSafe> SampleSource;
  44. };
  45. /**
  46. * Render the texture resource.
  47. *
  48. * This method is called on the render thread by the WebViewTexture that owns this
  49. * texture resource to clear or redraw the resource using the given parameters.
  50. *
  51. * @param Params Render parameters.
  52. */
  53. void Render(const FRenderParams& Params);
  54. public:
  55. //~ FRenderTarget interface
  56. virtual FIntPoint GetSizeXY() const override;
  57. public:
  58. //~ FTextureResource interface
  59. virtual FString GetFriendlyName() const override;
  60. virtual uint32 GetSizeX() const override;
  61. virtual uint32 GetSizeY() const override;
  62. #if WEBVIEW_ENGINE_VERSION>=50300
  63. virtual void InitRHI(FRHICommandListBase& RHICmdList) override;
  64. #else
  65. virtual void InitDynamicRHI() override;
  66. #endif
  67. virtual void ReleaseRHI() override;
  68. protected:
  69. /**
  70. * Clear the texture using the given clear color.
  71. *
  72. * @param ClearColor The clear color to use.
  73. */
  74. void ClearTexture(const FLinearColor& ClearColor);
  75. /**
  76. * Render the given texture sample by using it as or copying it to the render target.
  77. *
  78. * @param Sample The texture sample to copy.
  79. * @param ClearColor The clear color to use for the output texture.
  80. * @see ConvertSample
  81. */
  82. void CopySample(const TSharedPtr<FWebViewTextureSample, ESPMode::ThreadSafe>& Sample, const FLinearColor& ClearColor);
  83. /** Calculates the current resource size and notifies the owner texture. */
  84. void UpdateResourceSize();
  85. /**
  86. * Set the owner's texture reference to the given texture.
  87. *
  88. * @param NewTexture The texture to set.
  89. */
  90. void UpdateTextureReference(FRHITexture2D* NewTexture);
  91. private:
  92. /** Whether the texture has been cleared. */
  93. bool Cleared;
  94. /** Tracks the current clear color. */
  95. FLinearColor CurrentClearColor;
  96. /** Input render target if the texture samples don't provide one (for conversions). */
  97. TRefCountPtr<FRHITexture2D> InputTarget;
  98. /** Output render target if the texture samples don't provide one. */
  99. TRefCountPtr<FRHITexture2D> OutputTarget;
  100. /** The webbrowser texture that owns this resource. */
  101. UWebViewTexture& Owner;
  102. /** Reference to the owner's texture dimensions field. */
  103. FIntPoint& OwnerDim;
  104. /** Reference to the owner's texture size field. */
  105. SIZE_T& OwnerSize;
  106. };