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