1 /* 2 * Copyright (c) 2021 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 #ifndef RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_SURFACE_RENDER_NODE_H 16 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_SURFACE_RENDER_NODE_H 17 18 #include <functional> 19 #include <memory> 20 #include <surface.h> 21 22 #include "display_type.h" 23 #include "ipc_callbacks/buffer_available_callback.h" 24 #include "pipeline/rs_render_node.h" 25 #include "refbase.h" 26 #include "sync_fence.h" 27 28 class SkCanvas; 29 namespace OHOS { 30 namespace Rosen { 31 class RSCommand; 32 class RSSurfaceRenderNode : public RSRenderNode { 33 public: 34 using WeakPtr = std::weak_ptr<RSSurfaceRenderNode>; 35 using SharedPtr = std::shared_ptr<RSSurfaceRenderNode>; 36 static inline constexpr RSRenderNodeType Type = RSRenderNodeType::SURFACE_NODE; 37 38 explicit RSSurfaceRenderNode(NodeId id, std::weak_ptr<RSContext> context = {}); 39 explicit RSSurfaceRenderNode(const RSSurfaceRenderNodeConfig& config, std::weak_ptr<RSContext> context = {}); 40 virtual ~RSSurfaceRenderNode(); 41 42 void SetConsumer(const sptr<Surface>& consumer); 43 void SetBuffer(const sptr<SurfaceBuffer>& buffer); 44 void SetFence(sptr<SyncFence> fence); 45 void SetDamageRegion(const Rect& damage); 46 void IncreaseAvailableBuffer(); 47 int32_t ReduceAvailableBuffer(); 48 GetBuffer()49 sptr<SurfaceBuffer>& GetBuffer() 50 { 51 return buffer_; 52 } 53 GetFence()54 sptr<SyncFence> GetFence() const 55 { 56 return fence_; 57 } 58 GetPreBuffer()59 sptr<SurfaceBuffer>& GetPreBuffer() 60 { 61 return preBuffer_; 62 } 63 GetPreFence()64 sptr<SyncFence> GetPreFence() const 65 { 66 return preFence_; 67 } 68 GetDamageRegion()69 const Rect& GetDamageRegion() const 70 { 71 return damageRect_; 72 } 73 GetConsumer()74 const sptr<Surface>& GetConsumer() const 75 { 76 return consumer_; 77 } 78 GetAvailableBufferCount()79 int32_t GetAvailableBufferCount() const 80 { 81 return bufferAvailableCount_; 82 } 83 GetName()84 std::string GetName() const 85 { 86 return name_; 87 } 88 89 void Prepare(const std::shared_ptr<RSNodeVisitor>& visitor) override; 90 void Process(const std::shared_ptr<RSNodeVisitor>& visitor) override; 91 GetType()92 RSRenderNodeType GetType() const override 93 { 94 return RSRenderNodeType::SURFACE_NODE; 95 } 96 97 void SetMatrix(const SkMatrix& transform, bool sendMsg = true); 98 const SkMatrix& GetMatrix() const; 99 100 void SetAlpha(float alpha, bool sendMsg = true); 101 float GetAlpha() const; 102 103 void SetClipRegion(Vector4f clipRegion, bool sendMsg = true); 104 105 void SetSecurityLayer(bool isSecurityLayer); 106 bool GetSecurityLayer() const; 107 GetClipRegion()108 const Vector4f& GetClipRegion() const 109 { 110 return clipRect_; 111 } 112 SetDstRect(const RectI & dstRect)113 void SetDstRect(const RectI& dstRect) 114 { 115 dstRect_ = dstRect; 116 } 117 GetDstRect()118 const RectI& GetDstRect() const 119 { 120 return dstRect_; 121 } 122 123 // Only use in Render Service 124 void SetGlobalZOrder(float globalZOrder); 125 float GetGlobalZOrder() const; 126 127 void SetParentId(NodeId parentId, bool sendMsg = true); 128 NodeId GetParentId() const; 129 130 void UpdateSurfaceDefaultSize(float width, float height); 131 132 static void SendPropertyCommand(std::unique_ptr<RSCommand>& command); 133 134 BlendType GetBlendType(); 135 void SetBlendType(BlendType blendType); 136 137 // Only SurfaceNode in RS calls "RegisterBufferAvailableListener" to save callback method sent by RT 138 void RegisterBufferAvailableListener(sptr<RSIBufferAvailableCallback> callback); 139 140 // Only SurfaceNode in RT calls "ConnectToNodeInRenderService" to send callback method to RS 141 void ConnectToNodeInRenderService(); 142 143 void NotifyBufferAvailable(bool isBufferAvailable); 144 bool IsBufferAvailable() const; 145 146 // UI Thread would not be notified when SurfaceNode created by Video/Camera in RenderService has available buffer. 147 // And RenderThread does not call mainFunc_ if nothing in UI thread is changed 148 // which would cause callback for "clip" on parent SurfaceNode cannot be triggered 149 // for "clip" is executed in RenderThreadVisitor::ProcessSurfaceRenderNode. 150 // To fix this bug, we set callback which would call RSRenderThread::RequestNextVSync() to forcely "refresh" 151 // RenderThread when SurfaceNode in RenderService has available buffer and execute RSIBufferAvailableCallback. 152 void SetCallbackForRenderThreadRefresh(std::function<void(void)> callback); 153 bool NeedSetCallbackForRenderThreadRefresh(); 154 155 private: 156 friend class RSRenderTransition; 157 sptr<Surface> consumer_; 158 159 std::mutex mutex_; 160 std::atomic<int> bufferAvailableCount_ = 0; 161 SkMatrix matrix_; 162 float alpha_ = 1.0f; 163 float globalZOrder_ = 0.0f; 164 bool isSecurityLayer_ = false; 165 NodeId parentId_ = 0; 166 sptr<SurfaceBuffer> buffer_; 167 sptr<SurfaceBuffer> preBuffer_; 168 sptr<SyncFence> fence_; 169 sptr<SyncFence> preFence_; 170 Rect damageRect_ = {0, 0, 0, 0}; 171 RectI dstRect_; 172 Vector4f clipRect_; 173 std::string name_; 174 BlendType blendType_ = BlendType::BLEND_SRCOVER; 175 std::atomic<bool> isBufferAvailable_ = false; 176 sptr<RSIBufferAvailableCallback> callback_; 177 std::function<void(void)> callbackForRenderThreadRefresh_ = nullptr; 178 }; 179 } // namespace Rosen 180 } // namespace OHOS 181 182 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_SURFACE_RENDER_NODE_H 183