• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "hdi_test_layer.h"
17 #include <unistd.h>
18 #include "hdi_test_device.h"
19 #include "v1_0/include/idisplay_buffer.h"
20 #include "v1_0/display_buffer_type.h"
21 #include "v1_0/display_composer_type.h"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Display {
26 namespace TEST {
27 using namespace OHOS::HDI::Display::Buffer::V1_0;
28 
HdiGrallocBuffer(uint32_t seqNo,uint32_t w,uint32_t h,PixelFormat fmt)29 HdiGrallocBuffer::HdiGrallocBuffer(uint32_t seqNo, uint32_t w, uint32_t h, PixelFormat fmt)
30 {
31     std::shared_ptr<IDisplayBuffer> gralloc = HdiTestDevice::GetInstance().GetGrallocInterface();
32     AllocInfo info = { 0 };
33     info.width = w;
34     info.height = h;
35     info.usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE;
36     info.format = fmt;
37 
38     BufferHandle* buffer = nullptr;
39     int ret = gralloc->AllocMem(info, buffer);
40     if (ret != DISPLAY_SUCCESS) {
41         DISPLAY_TEST_LOGE("can not alloc memory");
42     }
43     if (buffer != nullptr) {
44         void* vaddr = gralloc->Mmap(*buffer);
45         if (vaddr == nullptr) {
46             DISPLAY_TEST_LOGE("mmap failed");
47         }
48         buffer_ = buffer;
49         seqNo_ = seqNo;
50     }
51 }
52 
~HdiGrallocBuffer()53 HdiGrallocBuffer::~HdiGrallocBuffer()
54 {
55     if (buffer_ != nullptr) {
56         std::shared_ptr<IDisplayBuffer> gralloc = HdiTestDevice::GetInstance().GetGrallocInterface();
57         if (buffer_->virAddr != nullptr) {
58             int ret = gralloc->Unmap(*buffer_);
59             if (ret != DISPLAY_SUCCESS) {
60                 DISPLAY_TEST_LOGE("can not ummap buffer handle");
61             }
62         }
63         gralloc->FreeMem(*buffer_);
64         buffer_ = nullptr;
65     }
66     if (mReleaseFence != -1) {
67         close(mReleaseFence);
68     }
69 }
70 
SetReleaseFence(int fd)71 void HdiGrallocBuffer::SetReleaseFence(int fd)
72 {
73     DISPLAY_TEST_LOGD("the fd is %{public}d", fd);
74     if (mReleaseFence != -1) {
75         close(mReleaseFence);
76         mReleaseFence = -1;
77     }
78 }
79 
SetAcquirceFence(int fd)80 void HdiGrallocBuffer::SetAcquirceFence(int fd)
81 {
82     DISPLAY_TEST_LOGD("the fd is %{public}d", fd);
83     mAcquireFence = fd;
84 }
85 
SetGraphicBuffer(std::function<int32_t (const BufferHandle *,uint32_t)> realFunc)86 int32_t HdiGrallocBuffer::SetGraphicBuffer(std::function<int32_t (const BufferHandle*, uint32_t)> realFunc)
87 {
88     DISPLAY_TEST_CHK_RETURN(buffer_ == nullptr, DISPLAY_FAILURE,
89         DISPLAY_TEST_LOGE("buffer handle is null"));
90     DISPLAY_TEST_CHK_RETURN(seqNo_ == UINT32_MAX, DISPLAY_FAILURE,
91         DISPLAY_TEST_LOGE("seqNo is invalid"));
92 
93     int32_t ret = DISPLAY_SUCCESS;
94     if (cacheValid_ == false) {
95         ret = realFunc(buffer_, seqNo_);
96         cacheValid_ = (ret == DISPLAY_SUCCESS) ? true : false;
97     } else {
98         ret = realFunc(nullptr, seqNo_);
99     }
100     return ret;
101 }
102 
AcquireBackBuffer()103 HdiGrallocBuffer* HdiTestLayer::AcquireBackBuffer()
104 {
105     if (!backBuffers_.empty()) {
106         if (currentBuffer_ != nullptr) {
107             frontBuffers_.emplace(std::move(currentBuffer_));
108         }
109         currentBuffer_ = std::move(backBuffers_.front());
110         backBuffers_.pop();
111     }
112     return currentBuffer_.get();
113 }
114 
GetFrontBuffer() const115 HdiGrallocBuffer* HdiTestLayer::GetFrontBuffer() const
116 {
117     HdiGrallocBuffer* buffer = nullptr;
118     if (!frontBuffers_.empty()) {
119         buffer = frontBuffers_.front().get();
120     }
121     return buffer;
122 }
123 
GetBackBuffer() const124 HdiGrallocBuffer* HdiTestLayer::GetBackBuffer() const
125 {
126     HdiGrallocBuffer* buffer = nullptr;
127     if (!backBuffers_.empty()) {
128         buffer = backBuffers_.front().get();
129     }
130     return buffer;
131 }
132 
HdiTestLayer(LayerInfo & info,uint32_t id,uint32_t displayId)133 HdiTestLayer::HdiTestLayer(LayerInfo& info, uint32_t id, uint32_t displayId)
134     : id_(id), displayID_(displayId), layerBufferCount_(MAX_BUFFER_COUNT), layerInfo_(info)
135 {}
136 
GenerateSeq()137 static uint32_t GenerateSeq()
138 {
139     static uint32_t originSeq = 0;
140     return originSeq++;
141 }
142 
Init(uint32_t bufferCount)143 int32_t HdiTestLayer::Init(uint32_t bufferCount)
144 {
145     // init the font queue
146     DISPLAY_TEST_LOGD();
147     layerBufferCount_ = bufferCount;
148     for (int i = 0; i < layerBufferCount_; i++) {
149         std::unique_ptr<HdiGrallocBuffer> buffer =
150             std::make_unique<HdiGrallocBuffer>(GenerateSeq(),
151             layerInfo_.width, layerInfo_.height, layerInfo_.pixFormat);
152         DISPLAY_TEST_CHK_RETURN((buffer->Get() == nullptr), DISPLAY_FAILURE,
153             DISPLAY_TEST_LOGE("buffer handle is null"));
154         frontBuffers_.emplace(std::move(buffer));
155     }
156     displayRect_.w = layerInfo_.width;
157     displayRect_.h = layerInfo_.height;
158     cropRect_ = displayRect_;
159     return DISPLAY_SUCCESS;
160 }
161 
162 
SwapFrontToBackQ()163 int32_t HdiTestLayer::SwapFrontToBackQ()
164 {
165     DISPLAY_TEST_CHK_RETURN((frontBuffers_.empty()), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("the font buffer is empty"));
166     backBuffers_.emplace(std::move(frontBuffers_.front()));
167     frontBuffers_.pop();
168     return DISPLAY_SUCCESS;
169 }
170 
SwapBackToFrontQ()171 int32_t HdiTestLayer::SwapBackToFrontQ()
172 {
173     DISPLAY_TEST_CHK_RETURN((backBuffers_.empty()), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("the font buffer is empty"));
174     frontBuffers_.emplace(std::move(backBuffers_.front()));
175     backBuffers_.pop();
176     return DISPLAY_SUCCESS;
177 }
178 
SetLayerPosition(const IRect & rect)179 void HdiTestLayer::SetLayerPosition(const IRect& rect)
180 {
181     DISPLAY_TEST_LOGD("x : %{public}d y : %{public}d w : %{public}d h : %{public}d", rect.x, rect.y, rect.w, rect.h);
182     displayRect_ = rect;
183 }
184 
SetLayerCrop(const IRect & rect)185 void HdiTestLayer::SetLayerCrop(const IRect& rect)
186 {
187     DISPLAY_TEST_LOGD("x : %{public}d y : %{public}d w : %{public}d h : %{public}d", rect.x, rect.y, rect.w, rect.h);
188     cropRect_ = rect;
189 }
190 
PreparePresent()191 int32_t HdiTestLayer::PreparePresent()
192 {
193     int ret;
194     DISPLAY_TEST_LOGD();
195     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerRegion(displayID_, id_, displayRect_);
196     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display rect failed"));
197 
198     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerCrop(displayID_, id_, cropRect_);
199     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display crop failed"));
200 
201     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerZorder(displayID_, id_, zorder_);
202     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display zorder failed"));
203 
204     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerCompositionType(displayID_, id_, compType_);
205     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
206         DISPLAY_TEST_LOGE("set display composition type failed"));
207 
208     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerTransformMode(displayID_, id_, transform_);
209     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set transform mode failed"));
210 
211     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerAlpha(displayID_, id_, alpha_);
212     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set alpha failed"));
213 
214     HdiGrallocBuffer* buffer = AcquireBackBuffer();
215     DISPLAY_TEST_CHK_RETURN((buffer == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("can not get back buffer"));
216 
217     BufferHandle* handle = buffer->Get();
218     DISPLAY_TEST_CHK_RETURN((handle == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("BufferHandle is null"));
219 
220     IRect tmp {0, 0, handle->width, handle->height};
221     std::vector<IRect> vRects;
222     vRects.push_back(tmp);
223     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerVisibleRegion(displayID_, id_, vRects);
224     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
225         DISPLAY_TEST_LOGE("SetLayerVisibleRegion failed"));
226 
227     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerDirtyRegion(displayID_, id_, vRects);
228     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
229         DISPLAY_TEST_LOGE("SetLayerDirtyRegion failed"));
230 
231     ret = buffer->SetGraphicBuffer([&](const BufferHandle* ptrBuffer, uint32_t seqNo) -> int32_t {
232         std::vector<uint32_t> deletingList;
233         int32_t result = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerBuffer(
234             displayID_, id_, ptrBuffer, seqNo, -1, deletingList);
235         DISPLAY_TEST_CHK_RETURN((result != DISPLAY_SUCCESS), DISPLAY_FAILURE,
236             DISPLAY_TEST_LOGE("set buffer handle failed"));
237         return DISPLAY_SUCCESS;
238     });
239     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), ret, DISPLAY_TEST_LOGE("set buffer handle failed"));
240 
241     ret = HdiTestDevice::GetInstance().GetDeviceInterface()->SetLayerBlendType(displayID_, id_, blendType_);
242     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set blend type failed"));
243     return DISPLAY_SUCCESS;
244 }
245 
SetZorder(uint32_t zorder)246 void HdiTestLayer::SetZorder(uint32_t zorder)
247 {
248     DISPLAY_TEST_LOGD("the zorder is %{public}u", zorder);
249     zorder_ = zorder;
250 }
251 
SetCompType(CompositionType type)252 void HdiTestLayer::SetCompType(CompositionType type)
253 {
254     DISPLAY_TEST_LOGD("layer id %{public}u ,the type is : %{public}d", id_, type);
255     compType_ = type;
256 }
257 
SetTransform(TransformType transform)258 void HdiTestLayer::SetTransform(TransformType transform)
259 {
260     transform_ = transform;
261 }
262 
SetAlpha(LayerAlpha alpha)263 void HdiTestLayer::SetAlpha(LayerAlpha alpha)
264 {
265     DISPLAY_TEST_LOGD();
266     alpha_ = alpha;
267 }
268 
SetBlendType(BlendType type)269 void HdiTestLayer::SetBlendType(BlendType type)
270 {
271     DISPLAY_TEST_LOGD("type %{public}d", type);
272     blendType_ = type;
273 }
274 
SetReleaseFence(int fd)275 void HdiTestLayer::SetReleaseFence(int fd)
276 {
277     DISPLAY_TEST_LOGD("layer id %{public}u , fd %{public}d", id_, fd);
278     if (currentBuffer_ != nullptr) {
279         currentBuffer_->SetReleaseFence(fd);
280     }
281 }
282 
GetLayerBuffercount() const283 uint32_t HdiTestLayer::GetLayerBuffercount() const
284 {
285     return layerBufferCount_;
286 }
287 
~HdiTestLayer()288 HdiTestLayer::~HdiTestLayer() {}
289 } // OHOS
290 } // HDI
291 } // Display
292 } // TEST
293