• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_SURFACE_HANDLER_H
16 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_SURFACE_HANDLER_H
17 
18 #include <atomic>
19 #include <mutex>
20 
21 #include "common/rs_common_def.h"
22 #include "common/rs_macros.h"
23 #ifndef ROSEN_CROSS_PLATFORM
24 #include <iconsumer_surface.h>
25 #include <surface.h>
26 #include "sync_fence.h"
27 #endif
28 
29 namespace OHOS {
30 namespace Rosen {
31 using OnDeleteBufferFunc = std::function<void(int32_t)>;
32 class RSB_EXPORT RSSurfaceHandler {
33 public:
34     // indicates which node this handler belongs to.
RSSurfaceHandler(NodeId id)35     explicit RSSurfaceHandler(NodeId id) : id_(id) {}
36     virtual ~RSSurfaceHandler() noexcept = default;
37 
38     struct SurfaceBufferEntry {
39 #ifndef ROSEN_CROSS_PLATFORM
~SurfaceBufferEntrySurfaceBufferEntry40         ~SurfaceBufferEntry() noexcept
41         {
42             if (buffer != nullptr && bufferDeleteCb_ != nullptr) {
43                 bufferDeleteCb_(buffer->GetSeqNum());
44             }
45         }
46 
RegisterDeleteBufferListenerSurfaceBufferEntry47         void RegisterDeleteBufferListener(OnDeleteBufferFunc bufferDeleteCb)
48         {
49             if (bufferDeleteCb_ == nullptr) {
50                 bufferDeleteCb_ = bufferDeleteCb;
51             }
52         }
53 #endif
ResetSurfaceBufferEntry54         void Reset()
55         {
56 #ifndef ROSEN_CROSS_PLATFORM
57             if (buffer == nullptr) {
58                 return;
59             }
60             if (bufferDeleteCb_) {
61                 bufferDeleteCb_(buffer->GetSeqNum());
62             }
63             buffer = nullptr;
64             acquireFence = SyncFence::INVALID_FENCE;
65             releaseFence = SyncFence::INVALID_FENCE;
66             damageRect = Rect {0, 0, 0, 0};
67 #endif
68             timestamp = 0;
69         }
70 #ifndef ROSEN_CROSS_PLATFORM
71         sptr<SurfaceBuffer> buffer;
72         sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
73         sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
74         Rect damageRect = {0, 0, 0, 0};
75         OnDeleteBufferFunc bufferDeleteCb_ = nullptr;
76 #endif
77         int64_t timestamp = 0;
78     };
79 
80     void IncreaseAvailableBuffer();
81     void ReduceAvailableBuffer();
82 
GetNodeId()83     NodeId GetNodeId() const
84     {
85         return id_;
86     }
87 
SetDefaultWidthAndHeight(int32_t width,int32_t height)88     void SetDefaultWidthAndHeight(int32_t width, int32_t height)
89     {
90 #ifndef ROSEN_CROSS_PLATFORM
91         if (consumer_ != nullptr) {
92             consumer_->SetDefaultWidthAndHeight(width, height);
93         }
94 #endif
95     }
96 
97 #ifndef ROSEN_CROSS_PLATFORM
98     void SetConsumer(const sptr<IConsumerSurface>& consumer);
99 
GetConsumer()100     const sptr<IConsumerSurface>& GetConsumer() const
101     {
102         return consumer_;
103     }
104 
SetBuffer(const sptr<SurfaceBuffer> & buffer,const sptr<SyncFence> & acquireFence,const Rect & damage,const int64_t timestamp)105     void SetBuffer(
106         const sptr<SurfaceBuffer>& buffer,
107         const sptr<SyncFence>& acquireFence,
108         const Rect& damage,
109         const int64_t timestamp)
110     {
111         std::lock_guard<std::mutex> lock(bufMutex_);
112         preBuffer_.Reset();
113         preBuffer_ = buffer_;
114         buffer_.buffer = buffer;
115         buffer_.acquireFence = acquireFence;
116         buffer_.damageRect = damage;
117         buffer_.timestamp = timestamp;
118     }
119 
GetBuffer()120     const sptr<SurfaceBuffer>& GetBuffer() const
121     {
122         std::lock_guard<std::mutex> lock(bufMutex_);
123         return buffer_.buffer;
124     }
125 
GetAcquireFence()126     const sptr<SyncFence>& GetAcquireFence() const
127     {
128         std::lock_guard<std::mutex> lock(bufMutex_);
129         return buffer_.acquireFence;
130     }
131 
GetDamageRegion()132     const Rect& GetDamageRegion() const
133     {
134         std::lock_guard<std::mutex> lock(bufMutex_);
135         return buffer_.damageRect;
136     }
137 
SetCurrentReleaseFence(sptr<SyncFence> fence)138     void SetCurrentReleaseFence(sptr<SyncFence> fence)
139     {
140         buffer_.releaseFence = fence;
141     }
142 
SetReleaseFence(sptr<SyncFence> fence)143     void SetReleaseFence(sptr<SyncFence> fence)
144     {
145         // The fence which get from hdi is preBuffer's releaseFence now.
146         preBuffer_.releaseFence = std::move(fence);
147     }
148 #endif
149 
GetPreBuffer()150     SurfaceBufferEntry& GetPreBuffer()
151     {
152         return preBuffer_;
153     }
154 
GetAvailableBufferCount()155     int32_t GetAvailableBufferCount() const
156     {
157         return bufferAvailableCount_;
158     }
159 
GetTimestamp()160     int64_t GetTimestamp() const
161     {
162         std::lock_guard<std::mutex> lock(bufMutex_);
163         return buffer_.timestamp;
164     }
165 
CleanCache()166     void CleanCache()
167     {
168         std::lock_guard<std::mutex> lock(bufMutex_);
169         buffer_.Reset();
170         preBuffer_.Reset();
171     }
172 
ResetBufferAvailableCount()173     void ResetBufferAvailableCount()
174     {
175         bufferAvailableCount_ = 0;
176     }
177 
178     void SetGlobalZOrder(float globalZOrder);
179     float GetGlobalZOrder() const;
180 
HasConsumer()181     bool HasConsumer() const
182     {
183 #ifndef ROSEN_CROSS_PLATFORM
184         return consumer_ != nullptr;
185 #else
186         return false;
187 #endif
188     }
IsCurrentFrameBufferConsumed()189     inline bool IsCurrentFrameBufferConsumed()
190     {
191         return isCurrentFrameBufferConsumed_;
192     }
ResetCurrentFrameBufferConsumed()193     inline void ResetCurrentFrameBufferConsumed()
194     {
195         isCurrentFrameBufferConsumed_ = false;
196     }
SetCurrentFrameBufferConsumed()197     inline void SetCurrentFrameBufferConsumed()
198     {
199         isCurrentFrameBufferConsumed_ = true;
200     }
201 
202 #ifndef ROSEN_CROSS_PLATFORM
RegisterDeleteBufferListener(OnDeleteBufferFunc bufferDeleteCb)203     void RegisterDeleteBufferListener(OnDeleteBufferFunc bufferDeleteCb)
204     {
205         std::lock_guard<std::mutex> lock(bufMutex_);
206         if (bufferDeleteCb != nullptr) {
207             buffer_.RegisterDeleteBufferListener(bufferDeleteCb);
208             preBuffer_.RegisterDeleteBufferListener(bufferDeleteCb);
209         }
210     }
211 #endif
212 
213 protected:
214 #ifndef ROSEN_CROSS_PLATFORM
215     sptr<IConsumerSurface> consumer_;
216 #endif
217     bool isCurrentFrameBufferConsumed_ = false;
218 
219 private:
220     NodeId id_ = 0;
221     mutable std::mutex bufMutex_;
222     SurfaceBufferEntry buffer_; // GUARDED BY bufMutex_
223     SurfaceBufferEntry preBuffer_; // GUARDED BY bufMutex_
224     float globalZOrder_ = 0.0f;
225     std::atomic<int> bufferAvailableCount_ = 0;
226 };
227 }
228 }
229 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_SURFACE_HANDLER_H
230