• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #ifndef HDI_LAYER_H
17 #define HDI_LAYER_H
18 #include <unordered_set>
19 #include <vector>
20 #include <memory>
21 #include <queue>
22 #include "buffer_handle.h"
23 #include "display_log.h"
24 #include "v1_0/display_composer_type.h"
25 #include "hdi_device_common.h"
26 #include "hdi_fd.h"
27 
28 namespace OHOS {
29 namespace HDI {
30 namespace DISPLAY {
31 using namespace OHOS::HDI::Display::Composer::V1_0;
32 const uint32_t FENCE_TIMEOUT = 3000;
33 struct HdiLayerBuffer {
34 public:
35     explicit HdiLayerBuffer(const BufferHandle &hdl);
36     virtual ~HdiLayerBuffer();
37     HdiLayerBuffer &operator = (const BufferHandle &right);
GetMemHandleHdiLayerBuffer38     uint64_t GetMemHandle() const
39     {
40         if (mPhyAddr == 0) {
41             return static_cast<uint64_t>(mFd);
42         } else {
43             return mPhyAddr;
44         }
45     }
GetHeightHdiLayerBuffer46     int32_t GetHeight() const
47     {
48         return mHeight;
49     }
GetWidthHdiLayerBuffer50     int32_t GetWidth() const
51     {
52         return mWidth;
53     }
GetStrideHdiLayerBuffer54     int32_t GetStride() const
55     {
56         return mStride;
57     }
GetFormatHdiLayerBuffer58     int32_t GetFormat() const
59     {
60         return mFormat;
61     }
GetFbHdiLayerBuffer62     int GetFb() const
63     {
64         return mFd;
65     }
66     BufferHandle mHandle;
67 
68 private:
69     uint64_t mPhyAddr = 0;
70     int32_t mHeight = 0;
71     int32_t mWidth = 0;
72     int32_t mStride = 0;
73     int32_t mFormat = 0;
74     int mFd = -1;
75 };
76 
77 class HdiLayer {
78 public:
HdiLayer(LayerType type)79     explicit HdiLayer(LayerType type) : mType(type) {}
80     int32_t Init();
GetId()81     uint32_t GetId() const
82     {
83         return mId;
84     }
GetZorder()85     uint32_t GetZorder() const
86     {
87         return mZorder;
88     }
GetLayerDisplayRect()89     const IRect &GetLayerDisplayRect() const
90     {
91         return mDisplayRect;
92     }
GetLayerCrop()93     const IRect &GetLayerCrop() const
94     {
95         return mCrop;
96     }
GetLayerPreMulti()97     bool GetLayerPreMulti() const
98     {
99         return mPreMul;
100     }
GetAlpha()101     const LayerAlpha &GetAlpha() const
102     {
103         return mAlpha;
104     }
GetType()105     LayerType GetType() const
106     {
107         return mType;
108     }
GetTransFormType()109     TransformType GetTransFormType() const
110     {
111         return mTransformType;
112     }
GetLayerBlenType()113     BlendType GetLayerBlenType() const
114     {
115         return mBlendType;
116     }
GetCompositionType()117     CompositionType GetCompositionType() const
118     {
119         return mCompositionType;
120     }
SetDeviceSelect(CompositionType type)121     void SetDeviceSelect(CompositionType type)
122     {
123         DISPLAY_LOGD("%{public}d", type);
124         mDeviceSelect = type;
125     }
GetDeviceSelect()126     CompositionType GetDeviceSelect() const
127     {
128         return mDeviceSelect;
129     }
130 
GetAcquireFenceFd()131     int GetAcquireFenceFd()
132     {
133         return mAcquireFence.GetFd();
134     }
135 
GetReleaseFenceFd()136     int GetReleaseFenceFd()
137     {
138         int fd;
139         if (releaseFences_.empty()) {
140             fd = -1;
141         } else {
142             fd = releaseFences_.front();
143             releaseFences_.pop();
144         }
145         return fd;
146     }
147 
IsVisible()148     bool IsVisible()
149     {
150         return mVisible;
151     }
152 
SetReleaseFence(int fd)153     void SetReleaseFence(int fd)
154     {
155         releaseFences_.push(fd);
156     };
157     void ClearColor(uint32_t color);
158 
159     void SetPixel(const BufferHandle &handle, int x, int y, uint32_t color);
160 
161     void WaitAcquireFence();
162     virtual int32_t SetLayerRegion(IRect *rect);
163     virtual int32_t SetLayerCrop(IRect *rect);
164     virtual void SetLayerZorder(uint32_t zorder);
165     virtual int32_t SetLayerPreMulti(bool preMul);
166     virtual int32_t SetLayerAlpha(LayerAlpha *alpha);
167     virtual int32_t SetLayerTransformMode(TransformType type);
168     virtual int32_t SetLayerDirtyRegion(IRect *region);
169     virtual int32_t SetLayerVisibleRegion(uint32_t num, IRect *rect);
170     virtual int32_t SetLayerBuffer(const BufferHandle *buffer, int32_t fence);
171     virtual int32_t SetLayerCompositionType(CompositionType type);
172     virtual int32_t SetLayerBlendType(BlendType type);
173     virtual int32_t SetLayerVisible(bool visible);
GetCurrentBuffer()174     virtual HdiLayerBuffer *GetCurrentBuffer()
175     {
176         return mHdiBuffer.get();
177     }
~HdiLayer()178     virtual ~HdiLayer()
179     {
180         mIdSets.erase(mId);
181     }
182 
183 private:
184     static uint32_t GetIdleId();
185     static uint32_t mIdleId;
186     static std::unordered_set<uint32_t> mIdSets;
187     bool mVisible = true;
188     uint32_t mId = 0;
189     HdiFd mAcquireFence;
190     std::queue<int> releaseFences_;
191     LayerType mType;
192 
193     IRect mDisplayRect;
194     IRect mCrop;
195     uint32_t mZorder = -1;
196     bool mPreMul = false;
197     LayerAlpha mAlpha;
198     int32_t mFenceTimeOut = FENCE_TIMEOUT;
199     TransformType mTransformType;
200     CompositionType mCompositionType = COMPOSITION_CLIENT;
201     CompositionType mDeviceSelect = COMPOSITION_CLIENT;
202     BlendType mBlendType;
203     std::unique_ptr<HdiLayerBuffer> mHdiBuffer;
204 };
205 
206 struct SortLayersByZ {
operatorSortLayersByZ207     bool operator () (const HdiLayer *lhs, const HdiLayer *rhs) const
208     {
209         if (lhs == nullptr || rhs == nullptr) {
210             return (lhs == nullptr) && (rhs == nullptr);
211         }
212         return lhs->GetZorder() < rhs->GetZorder();
213     }
214 };
215 } // namespace OHOS
216 } // namespace HDI
217 } // namespace DISPLAY
218 
219 #endif // HDI_LAYER_H