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 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_common.h" 24 #include "hdi_device_common.h" 25 #include "hdi_fd.h" 26 27 namespace OHOS { 28 namespace HDI { 29 namespace DISPLAY { 30 const uint32_t INVALIDE_LAYER_ID = 0xffffffff; 31 struct HdiLayerBuffer { 32 public: 33 explicit HdiLayerBuffer(const BufferHandle &hdl); 34 virtual ~HdiLayerBuffer(); 35 HdiLayerBuffer &operator = (const BufferHandle &right); GetMemHandleHdiLayerBuffer36 uint64_t GetMemHandle() const 37 { 38 if (mPhyAddr == 0) { 39 return static_cast<uint64_t>(mFd); 40 } else { 41 return mPhyAddr; 42 } 43 } GetHeightHdiLayerBuffer44 int32_t GetHeight() const 45 { 46 return mHeight; 47 } GetWidthHdiLayerBuffer48 int32_t GetWidth() const 49 { 50 return mWidth; 51 } GetStrideHdiLayerBuffer52 int32_t GetStride() const 53 { 54 return mStride; 55 } GetFormatHdiLayerBuffer56 int32_t GetFormat() const 57 { 58 return mFormat; 59 } GetFbHdiLayerBuffer60 int GetFb() const 61 { 62 return mFd; 63 } 64 BufferHandle mHandle; 65 66 private: 67 HdiLayerBuffer(const HdiLayerBuffer &rhs); 68 HdiLayerBuffer& operator=(const HdiLayerBuffer &rhs); 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() const 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 DISPLAY_LOGD("fd is %d releaseFences_ size %zu", fd, releaseFences_.size()); 146 return fd; 147 } 148 IsVisible()149 bool IsVisible() const 150 { 151 return mVisible; 152 } 153 SetReleaseFence(int fd)154 void SetReleaseFence(int fd) 155 { 156 releaseFences_.push(fd); 157 }; 158 void ClearColor(uint32_t color); 159 160 void SetPixel(const BufferHandle &handle, int x, int y, uint32_t color); 161 162 virtual int32_t SetLayerSize(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 SetTransformMode(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 } 178 virtual ~HdiLayer(); 179 180 private: 181 static uint32_t GetIdleId(); 182 static uint32_t mIdleId; 183 static std::unordered_set<uint32_t> mIdSets; 184 bool mVisible = true; 185 uint32_t mId = 0; 186 HdiFd mAcquireFence; 187 std::queue<int> releaseFences_; 188 LayerType mType; 189 190 IRect mDisplayRect; 191 IRect mCrop; 192 uint32_t mZorder = -1; 193 bool mPreMul = false; 194 LayerAlpha mAlpha; 195 TransformType mTransformType; 196 CompositionType mCompositionType = COMPOSITION_CLIENT; 197 CompositionType mDeviceSelect = COMPOSITION_CLIENT; 198 BlendType mBlendType; 199 std::unique_ptr<HdiLayerBuffer> mHdiBuffer; 200 }; 201 202 struct SortLayersByZ { operatorSortLayersByZ203 bool operator () (const HdiLayer *lhs, const HdiLayer *rhs) const 204 { 205 if (lhs == nullptr || rhs == nullptr) { 206 return (lhs == nullptr) && (rhs == nullptr); 207 } 208 return lhs->GetZorder() < rhs->GetZorder(); 209 } 210 }; 211 } // namespace OHOS 212 } // namespace HDI 213 } // namespace DISPLAY 214 215 #endif // HDI_LAYER_H 216