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