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