1 /* 2 * Copyright (c) 2022-2024 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 #include <queue> 20 21 #ifdef OHOS_PLATFORM 22 #include "ibuffer_consumer_listener.h" 23 #include "iconsumer_surface.h" 24 #include "surface.h" 25 #include "surface/window.h" 26 #include "surface_delegate.h" 27 #include "transaction/rs_render_service_client.h" 28 #endif 29 30 #include "base/memory/referenced.h" 31 #include "base/utils/noncopyable.h" 32 #include "core/components_ng/render/drawing.h" 33 #include "core/components_ng/render/ext_surface_callback_interface.h" 34 #include "core/components_ng/render/render_surface.h" 35 36 namespace OHOS::Ace::NG { 37 constexpr int32_t SURFACE_QUEUE_SIZE = 5; 38 #ifdef OHOS_PLATFORM 39 struct SurfaceBufferNode; 40 #endif 41 42 class RosenRenderSurface : public RenderSurface { 43 DECLARE_ACE_TYPE(RosenRenderSurface, NG::RenderSurface) 44 public: 45 RosenRenderSurface() = default; 46 ~RosenRenderSurface() override; 47 48 void InitSurface() override; 49 50 void UpdateSurfaceConfig() override; 51 52 void* GetNativeWindow() override; 53 54 void SetRenderContext(const RefPtr<RenderContext>& renderContext) override; 55 56 void ConfigSurface(uint32_t surfaceWidth, uint32_t surfaceHeight) override; 57 58 bool IsSurfaceValid() const override; 59 60 void CreateNativeWindow() override; 61 62 void AdjustNativeWindowSize(uint32_t width, uint32_t height) override; 63 64 std::string GetUniqueId() const override; 65 66 uint64_t GetUniqueIdNum() const; 67 SetIsTexture(bool isTexture)68 void SetIsTexture(bool isTexture) override 69 { 70 isTexture_ = isTexture; 71 } 72 SetInstanceId(int32_t instanceId)73 void SetInstanceId(int32_t instanceId) override 74 { 75 instanceId_ = instanceId; 76 } 77 78 void SetSurfaceDefaultSize(int32_t width, int32_t height) override; 79 80 void ConsumeXComponentBuffer(); 81 82 void ConsumeWebBuffer(); 83 SetWebMessage(OffsetF offset)84 void SetWebMessage(OffsetF offset) override 85 { 86 orgin_ = offset; 87 } 88 SetPatternType(const std::string & type)89 void SetPatternType(const std::string& type) override 90 { 91 patternType_ = type; 92 } 93 GetPatternType()94 std::string GetPatternType() const 95 { 96 return patternType_; 97 } 98 SetBufferUsage(const std::string & usage)99 void SetBufferUsage(const std::string& usage) override 100 { 101 bufferUsage_ = usage; 102 } 103 GetBufferUsage()104 std::string GetBufferUsage() const 105 { 106 return bufferUsage_; 107 } 108 SetWebSlideAxis(Axis axis)109 void SetWebSlideAxis(Axis axis) override 110 { 111 axis_ = axis; 112 } 113 SetWebOffset(float webOffset)114 void SetWebOffset(float webOffset) override 115 { 116 webOffset_ = webOffset; 117 } 118 119 void SetSurfaceQueueSize(int32_t queueSize = SURFACE_QUEUE_SIZE) override 120 { 121 queueSize_ = queueSize; 122 } 123 124 void DrawBuffer(int32_t width = -1, int32_t height = -1); 125 126 void DrawBufferForXComponent(RSCanvas& canvas, float width, float height, float offsetX, float offsetY) override; 127 128 #ifdef OHOS_PLATFORM GetSurface()129 OHOS::sptr<OHOS::Surface> GetSurface() const 130 { 131 return producerSurface_; 132 } 133 134 bool CompareBufferSize(int32_t width, int32_t height, std::shared_ptr<SurfaceBufferNode> surfaceNode); 135 SetIsNeedSyncGeometryProperties(bool isNeedSyncGeometryProperties)136 void SetIsNeedSyncGeometryProperties(bool isNeedSyncGeometryProperties) override 137 { 138 isNeedSyncGeometryProperties_ = isNeedSyncGeometryProperties; 139 } 140 SetKeyBoardAvoidRect(RectF keyBoardAvoidRect)141 void SetKeyBoardAvoidRect(RectF keyBoardAvoidRect) override 142 { 143 keyBoardAvoidRect_ = keyBoardAvoidRect; 144 } 145 146 #endif 147 148 void ReleaseSurfaceBuffers() override; 149 150 void UpdateSurfaceSizeInUserData(uint32_t width, uint32_t height) override; 151 152 void SetExtSurfaceBounds(int32_t left, int32_t top, int32_t width, int32_t height) override; 153 154 void SetExtSurfaceCallback(const RefPtr<ExtSurfaceCallbackInterface>& extSurfaceCallback) override; 155 156 void SetTransformHint(uint32_t rotation) override; 157 158 void RegisterSurface() const override; 159 160 void UnregisterSurface() const override; 161 162 void Connect() const override; 163 164 void Disconnect() const override; 165 166 void DumpInfo() override; 167 168 void ReleaseSurfaceBufferById(uint32_t bufferId); 169 170 #ifdef OHOS_PLATFORM 171 void ReleaseSurfaceBufferForRT(const Rosen::FinishCallbackRet& ret); 172 #endif 173 174 void RegisterBufferCallback() override; 175 176 void SetIsUniRender(bool isUniRender); 177 178 void PostRenderOnlyTaskToUI(); 179 180 void OnWindowStateChange(bool isShow) override; 181 182 private: 183 void MarkDirtyIfNeeded(); 184 185 #ifdef OHOS_PLATFORM 186 void InsertSurfaceNode(const std::shared_ptr<SurfaceBufferNode>& surfaceNode); 187 #endif 188 189 std::mutex surfaceNodeMutex_; 190 bool isNeedSyncGeometryProperties_ = false; 191 RectF keyBoardAvoidRect_; 192 OffsetF orgin_ { 0, 0 }; 193 std::string patternType_; 194 std::string bufferUsage_; 195 int32_t queueSize_ = SURFACE_QUEUE_SIZE; 196 Axis axis_ = Axis::NONE; 197 float webOffset_ = 0.0; 198 #ifdef OHOS_PLATFORM 199 OHOS::sptr<OHOS::Surface> producerSurface_ = nullptr; 200 OHOS::sptr<IConsumerSurface> consumerSurface_ = nullptr; 201 OHOS::sptr<IBufferConsumerListener> drawBufferListener_ = nullptr; 202 struct NativeWindow* nativeWindow_ = nullptr; 203 sptr<OHOS::SurfaceDelegate> surfaceDelegate_; 204 std::queue<std::shared_ptr<SurfaceBufferNode>> availableBuffers_; 205 std::list<std::shared_ptr<SurfaceBufferNode>> buffersToDraw_; 206 std::list<std::shared_ptr<SurfaceBufferNode>> buffersToRelease_; 207 std::shared_ptr<Rosen::SurfaceBufferCallback> bufferCallback_; 208 int32_t failTimes_ = 0; 209 #endif 210 WeakPtr<NG::RenderContext> renderContext_ = nullptr; 211 RefPtr<ExtSurfaceCallbackInterface> extSurfaceCallbackInterface_ = nullptr; 212 bool isTexture_ = false; 213 int32_t instanceId_; 214 215 std::atomic_bool isShow_ = true; 216 std::atomic_bool isUniRender_ = true; 217 std::atomic_int32_t sendCount_ = -1; 218 219 ACE_DISALLOW_COPY_AND_MOVE(RosenRenderSurface); 220 }; 221 222 #ifdef OHOS_PLATFORM 223 class DrawBufferListener : public IBufferConsumerListener { 224 public: DrawBufferListener(const WeakPtr<NG::RosenRenderSurface> & renderSurface)225 explicit DrawBufferListener(const WeakPtr<NG::RosenRenderSurface>& renderSurface) : renderSurface_(renderSurface) {} 226 ~DrawBufferListener() override = default; 227 void OnBufferAvailable() override; 228 229 private: 230 WeakPtr<NG::RosenRenderSurface> renderSurface_; 231 }; 232 233 class XComponentSurfaceBufferCallback : public Rosen::SurfaceBufferCallback { 234 public: XComponentSurfaceBufferCallback(const WeakPtr<NG::RosenRenderSurface> & renderSurface)235 explicit XComponentSurfaceBufferCallback(const WeakPtr<NG::RosenRenderSurface>& renderSurface) 236 : renderSurface_(renderSurface) 237 {} 238 virtual ~XComponentSurfaceBufferCallback() = default; 239 void OnFinish(const Rosen::FinishCallbackRet& ret) override; OnAfterAcquireBuffer(const Rosen::AfterAcquireBufferRet & ret)240 void OnAfterAcquireBuffer(const Rosen::AfterAcquireBufferRet& ret) override {} 241 242 private: 243 WeakPtr<NG::RosenRenderSurface> renderSurface_; 244 }; 245 #endif 246 247 #ifdef OHOS_PLATFORM 248 class ExtSurfaceCallback : public OHOS::SurfaceDelegate::ISurfaceCallback { 249 public: ExtSurfaceCallback(const WeakPtr<ExtSurfaceCallbackInterface> & interface)250 explicit ExtSurfaceCallback(const WeakPtr<ExtSurfaceCallbackInterface>& interface) : weakInterface_(interface) {} 251 252 ~ExtSurfaceCallback() override = default; 253 254 void OnSurfaceCreated(const sptr<Surface>& surface) override; 255 256 void OnSurfaceChanged(const sptr<Surface>& surface, int32_t width, int32_t height) override; 257 258 void OnSurfaceDestroyed() override; 259 260 private: 261 WeakPtr<ExtSurfaceCallbackInterface> weakInterface_; 262 }; 263 #endif 264 } // namespace OHOS::Ace::NG 265 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_ROSEN_RENDER_SURFACE_H 266