1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_ROSEN_RENDER_SURFACE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_ROSEN_RENDER_SURFACE_H 18 19 #ifdef OHOS_PLATFORM 20 #include "ibuffer_consumer_listener.h" 21 #include "iconsumer_surface.h" 22 #include "surface.h" 23 #include "surface_delegate.h" 24 #include "window.h" 25 #endif 26 27 #include "base/memory/referenced.h" 28 #include "base/utils/noncopyable.h" 29 #include "core/components_ng/render/drawing.h" 30 #include "core/components_ng/render/ext_surface_callback_interface.h" 31 #include "core/components_ng/render/render_surface.h" 32 33 namespace OHOS::Ace::NG { 34 constexpr int32_t SURFACE_QUEUE_SIZE = 5; 35 #ifdef OHOS_PLATFORM 36 struct SurfaceBufferNode; 37 #endif 38 39 class RosenRenderSurface : public RenderSurface { 40 DECLARE_ACE_TYPE(RosenRenderSurface, NG::RenderSurface) 41 public: 42 RosenRenderSurface() = default; 43 ~RosenRenderSurface() override; 44 45 void InitSurface() override; 46 47 void UpdateXComponentConfig() override; 48 49 void* GetNativeWindow() override; 50 51 void SetRenderContext(const RefPtr<RenderContext>& renderContext) override; 52 53 void ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight) override; 54 55 bool IsSurfaceValid() const override; 56 57 void CreateNativeWindow() override; 58 59 void AdjustNativeWindowSize(uint32_t width, uint32_t height) override; 60 61 std::string GetUniqueId() const override; 62 SetIsTexture(bool isTexture)63 void SetIsTexture(bool isTexture) override 64 { 65 isTexture_ = isTexture; 66 } 67 SetInstanceId(int32_t instanceId)68 void SetInstanceId(int32_t instanceId) override 69 { 70 instanceId_ = instanceId; 71 } 72 73 void SetSurfaceDefaultSize(int32_t width, int32_t height) override; 74 75 void ConsumeXComponentBuffer(); 76 77 void ConsumeWebBuffer(); 78 SetWebMessage(OffsetF offset)79 void SetWebMessage(OffsetF offset) override 80 { 81 orgin_ = offset; 82 } 83 SetPatternType(const std::string & type)84 void SetPatternType(const std::string& type) override 85 { 86 patternType_ = type; 87 } 88 GetPatternType()89 std::string GetPatternType() const 90 { 91 return patternType_; 92 } 93 94 void SetSurfaceQueueSize(int32_t queueSize = SURFACE_QUEUE_SIZE) override 95 { 96 queueSize_ = queueSize; 97 } 98 99 void DrawBuffer(); 100 101 void DrawBufferForXComponent(RSCanvas& canvas, float width, float height) override; 102 103 #ifdef OHOS_PLATFORM GetSurface()104 OHOS::sptr<OHOS::Surface> GetSurface() const 105 { 106 return producerSurface_; 107 } 108 #endif 109 110 void SetExtSurfaceBounds(int32_t left, int32_t top, int32_t width, int32_t height) override; 111 112 void SetExtSurfaceCallback(const RefPtr<ExtSurfaceCallbackInterface>& extSurfaceCallback) override; 113 114 private: 115 void PostTaskToUI(const std::function<void()>&& task) const; 116 void RegisterSurface() const; 117 118 std::mutex surfaceNodeMutex_; 119 OffsetF orgin_ { 0, 0 }; 120 std::string patternType_; 121 int32_t queueSize_ = SURFACE_QUEUE_SIZE; 122 #ifdef OHOS_PLATFORM 123 OHOS::sptr<OHOS::Surface> producerSurface_ = nullptr; 124 OHOS::sptr<IConsumerSurface> consumerSurface_ = nullptr; 125 OHOS::sptr<IBufferConsumerListener> drawBufferListener_ = nullptr; 126 struct NativeWindow* nativeWindow_ = nullptr; 127 sptr<OHOS::SurfaceDelegate> surfaceDelegate_; 128 std::queue<std::shared_ptr<SurfaceBufferNode>> availableBuffers_; 129 #endif 130 WeakPtr<NG::RenderContext> renderContext_ = nullptr; 131 RefPtr<ExtSurfaceCallbackInterface> extSurfaceCallbackInterface_ = nullptr; 132 bool isTexture_ = false; 133 int32_t instanceId_; 134 135 ACE_DISALLOW_COPY_AND_MOVE(RosenRenderSurface); 136 }; 137 138 #ifdef OHOS_PLATFORM 139 class DrawBufferListener : public IBufferConsumerListener { 140 public: DrawBufferListener(const WeakPtr<NG::RosenRenderSurface> & renderSurface)141 explicit DrawBufferListener(const WeakPtr<NG::RosenRenderSurface>& renderSurface) : renderSurface_(renderSurface) {} 142 ~DrawBufferListener() override = default; 143 void OnBufferAvailable() override; 144 145 private: 146 WeakPtr<NG::RosenRenderSurface> renderSurface_; 147 }; 148 #endif 149 150 #ifdef OHOS_PLATFORM 151 class ExtSurfaceCallback : public OHOS::SurfaceDelegate::ISurfaceCallback { 152 public: ExtSurfaceCallback(const WeakPtr<ExtSurfaceCallbackInterface> & interface)153 explicit ExtSurfaceCallback(const WeakPtr<ExtSurfaceCallbackInterface>& interface) : weakInterface_(interface) {} 154 155 ~ExtSurfaceCallback() override = default; 156 157 void OnSurfaceCreated(const sptr<Surface>& surface) override; 158 159 void OnSurfaceChanged(const sptr<Surface>& surface, int32_t width, int32_t height) override; 160 161 void OnSurfaceDestroyed() override; 162 163 private: 164 WeakPtr<ExtSurfaceCallbackInterface> weakInterface_; 165 }; 166 #endif 167 } // namespace OHOS::Ace::NG 168 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_ROSEN_RENDER_SURFACE_H 169