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 #ifdef OHOS_PLATFORM 168 void ReleaseSurfaceBufferById(uint32_t bufferId, sptr<SyncFence> fence); 169 #endif 170 171 #ifdef OHOS_PLATFORM 172 void ReleaseSurfaceBufferForRT(const Rosen::FinishCallbackRet& ret); 173 #endif 174 175 void RegisterBufferCallback() override; 176 177 void SetIsUniRender(bool isUniRender); 178 179 void PostRenderOnlyTaskToUI(); 180 181 void OnWindowStateChange(bool isShow) override; 182 183 private: 184 void MarkDirtyIfNeeded(); 185 186 #ifdef OHOS_PLATFORM 187 void InsertSurfaceNode(const std::shared_ptr<SurfaceBufferNode>& surfaceNode); 188 #endif 189 190 std::mutex surfaceNodeMutex_; 191 bool isNeedSyncGeometryProperties_ = false; 192 RectF keyBoardAvoidRect_; 193 OffsetF orgin_ { 0, 0 }; 194 std::string patternType_; 195 std::string bufferUsage_; 196 int32_t queueSize_ = SURFACE_QUEUE_SIZE; 197 Axis axis_ = Axis::NONE; 198 float webOffset_ = 0.0; 199 #ifdef OHOS_PLATFORM 200 OHOS::sptr<OHOS::Surface> producerSurface_ = nullptr; 201 OHOS::sptr<IConsumerSurface> consumerSurface_ = nullptr; 202 OHOS::sptr<IBufferConsumerListener> drawBufferListener_ = nullptr; 203 struct NativeWindow* nativeWindow_ = nullptr; 204 sptr<OHOS::SurfaceDelegate> surfaceDelegate_; 205 std::queue<std::shared_ptr<SurfaceBufferNode>> availableBuffers_; 206 std::list<std::shared_ptr<SurfaceBufferNode>> buffersToDraw_; 207 std::list<std::shared_ptr<SurfaceBufferNode>> buffersToRelease_; 208 std::shared_ptr<Rosen::SurfaceBufferCallback> bufferCallback_; 209 int32_t failTimes_ = 0; 210 #endif 211 WeakPtr<NG::RenderContext> renderContext_ = nullptr; 212 RefPtr<ExtSurfaceCallbackInterface> extSurfaceCallbackInterface_ = nullptr; 213 bool isTexture_ = false; 214 int32_t instanceId_; 215 216 std::atomic_bool isShow_ = true; 217 std::atomic_bool isUniRender_ = true; 218 std::atomic_int32_t sendCount_ = -1; 219 220 ACE_DISALLOW_COPY_AND_MOVE(RosenRenderSurface); 221 }; 222 223 #ifdef OHOS_PLATFORM 224 class DrawBufferListener : public IBufferConsumerListener { 225 public: DrawBufferListener(const WeakPtr<NG::RosenRenderSurface> & renderSurface)226 explicit DrawBufferListener(const WeakPtr<NG::RosenRenderSurface>& renderSurface) : renderSurface_(renderSurface) {} 227 ~DrawBufferListener() override = default; 228 void OnBufferAvailable() override; 229 230 private: 231 WeakPtr<NG::RosenRenderSurface> renderSurface_; 232 }; 233 234 class XComponentSurfaceBufferCallback : public Rosen::SurfaceBufferCallback { 235 public: XComponentSurfaceBufferCallback(const WeakPtr<NG::RosenRenderSurface> & renderSurface)236 explicit XComponentSurfaceBufferCallback(const WeakPtr<NG::RosenRenderSurface>& renderSurface) 237 : renderSurface_(renderSurface) 238 {} 239 virtual ~XComponentSurfaceBufferCallback() = default; 240 void OnFinish(const Rosen::FinishCallbackRet& ret) override; OnAfterAcquireBuffer(const Rosen::AfterAcquireBufferRet & ret)241 void OnAfterAcquireBuffer(const Rosen::AfterAcquireBufferRet& ret) override {} 242 243 private: 244 WeakPtr<NG::RosenRenderSurface> renderSurface_; 245 }; 246 #endif 247 248 #ifdef OHOS_PLATFORM 249 class ExtSurfaceCallback : public OHOS::SurfaceDelegate::ISurfaceCallback { 250 public: ExtSurfaceCallback(const WeakPtr<ExtSurfaceCallbackInterface> & interface)251 explicit ExtSurfaceCallback(const WeakPtr<ExtSurfaceCallbackInterface>& interface) : weakInterface_(interface) {} 252 253 ~ExtSurfaceCallback() override = default; 254 255 void OnSurfaceCreated(const sptr<Surface>& surface) override; 256 257 void OnSurfaceChanged(const sptr<Surface>& surface, int32_t width, int32_t height) override; 258 259 void OnSurfaceDestroyed() override; 260 261 private: 262 WeakPtr<ExtSurfaceCallbackInterface> weakInterface_; 263 }; 264 #endif 265 } // namespace OHOS::Ace::NG 266 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_ROSEN_RENDER_SURFACE_H 267