• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hdi_test_layer.h"
17 #include "hdi_test_device.h"
18 namespace OHOS {
19 namespace HDI {
20 namespace DISPLAY {
21 namespace TEST {
HdiGrallocBuffer(uint32_t w,uint32_t h,PixelFormat fmt)22 HdiGrallocBuffer::HdiGrallocBuffer(uint32_t w, uint32_t h, PixelFormat fmt)
23 {
24     GrallocFuncs &gralloc = HdiTestDevice::GetInstance().GetGrallocFuncs();
25     AllocInfo info = { 0 };
26     info.width = w;
27     info.height = h;
28     info.usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE;
29     info.format = fmt;
30 
31     int ret = gralloc.AllocMem(&info, &mBufferHandle);
32     if (ret != DISPLAY_SUCCESS) {
33         DISPLAY_TEST_LOGE("can not alloc memory");
34     }
35     void *vaddr = gralloc.Mmap(mBufferHandle);
36     if (vaddr == nullptr) {
37         DISPLAY_TEST_LOGE("mmap failed");
38     }
39 }
40 
~HdiGrallocBuffer()41 HdiGrallocBuffer::~HdiGrallocBuffer()
42 {
43     if (mBufferHandle != nullptr) {
44         GrallocFuncs &gralloc = HdiTestDevice::GetInstance().GetGrallocFuncs();
45         if (mBufferHandle->virAddr != nullptr) {
46             int ret = gralloc.Unmap(mBufferHandle);
47             if (ret != DISPLAY_SUCCESS) {
48                 DISPLAY_TEST_LOGE("can not ummap buffer handle");
49             }
50         }
51         gralloc.FreeMem(mBufferHandle);
52         mBufferHandle = nullptr;
53     }
54     if (mReleaseFence != -1) {
55         close(mReleaseFence);
56     }
57 }
58 
SetReleaseFence(int fd)59 void HdiGrallocBuffer::SetReleaseFence(int fd)
60 {
61     DISPLAY_TEST_LOGD("the fd is %d", fd);
62     if (mReleaseFence != -1) {
63         close(mReleaseFence);
64         mReleaseFence = -1;
65     }
66     mReleaseFence = fd;
67 }
68 
SetAcquirceFence(int fd)69 void HdiGrallocBuffer::SetAcquirceFence(int fd)
70 {
71     DISPLAY_TEST_LOGD("the fd is %d", fd);
72     mAcquireFence = fd;
73 }
74 
AcquireBackBuffer()75 HdiGrallocBuffer *HdiTestLayer::AcquireBackBuffer()
76 {
77     if (!mBackBuffers.empty()) {
78         if (mCurrentBuffer != nullptr) {
79             mFrontBuffers.emplace(std::move(mCurrentBuffer));
80         }
81         mCurrentBuffer = std::move(mBackBuffers.front());
82         mBackBuffers.pop();
83     }
84     return mCurrentBuffer.get();
85 }
86 
GetFrontBuffer()87 HdiGrallocBuffer *HdiTestLayer::GetFrontBuffer()
88 {
89     HdiGrallocBuffer *buffer = nullptr;
90     if (!mFrontBuffers.empty()) {
91         buffer = mFrontBuffers.front().get();
92     }
93     return buffer;
94 }
95 
GetBackBuffer()96 HdiGrallocBuffer *HdiTestLayer::GetBackBuffer()
97 {
98     HdiGrallocBuffer *buffer = nullptr;
99     if (!mBackBuffers.empty()) {
100         buffer = mBackBuffers.front().get();
101     }
102     return buffer;
103 }
104 
HdiTestLayer(LayerInfo & info,uint32_t id,uint32_t displayId)105 HdiTestLayer::HdiTestLayer(LayerInfo &info, uint32_t id, uint32_t displayId)
106     : mId(id), mDisplayID(displayId), mLayerInfo(info)
107 {}
108 
Init()109 int32_t HdiTestLayer::Init()
110 {
111     // init the font queue
112     DISPLAY_TEST_LOGD();
113     const int maxBufferCount = 3;
114     for (int i = 0; i < maxBufferCount; i++) {
115         std::unique_ptr<HdiGrallocBuffer> buffer =
116             std::make_unique<HdiGrallocBuffer>(mLayerInfo.width, mLayerInfo.height, mLayerInfo.pixFormat);
117         DISPLAY_TEST_CHK_RETURN((buffer->Get() == nullptr), DISPLAY_FAILURE,
118             DISPLAY_TEST_LOGE("buffer handle is null"));
119         mFrontBuffers.emplace(std::move(buffer));
120     }
121     mDisplayRect.w = mLayerInfo.width;
122     mDisplayRect.h = mLayerInfo.height;
123     mCropRect = mDisplayRect;
124     return DISPLAY_SUCCESS;
125 }
126 
127 
SwapFrontToBackQ()128 int32_t HdiTestLayer::SwapFrontToBackQ()
129 {
130     DISPLAY_TEST_CHK_RETURN((mFrontBuffers.empty()), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("the font buffer is empty"));
131     mBackBuffers.emplace(std::move(mFrontBuffers.front()));
132     mFrontBuffers.pop();
133     return DISPLAY_SUCCESS;
134 }
135 
SwapBackToFrontQ()136 int32_t HdiTestLayer::SwapBackToFrontQ()
137 {
138     DISPLAY_TEST_CHK_RETURN((mBackBuffers.empty()), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("the font buffer is empty"));
139     mFrontBuffers.emplace(std::move(mBackBuffers.front()));
140     mBackBuffers.pop();
141     return DISPLAY_SUCCESS;
142 }
143 
SetLayerSize(IRect & rect)144 void HdiTestLayer::SetLayerSize(IRect &rect)
145 {
146     DISPLAY_TEST_LOGD("x : %d y : %d w : %d h : %d", rect.x, rect.y, rect.w, rect.h);
147     mDisplayRect = rect;
148 }
149 
SetLayerCrop(IRect & rect)150 void HdiTestLayer::SetLayerCrop(IRect &rect)
151 {
152     DISPLAY_TEST_LOGD("x : %d y : %d w : %d h : %d", rect.x, rect.y, rect.w, rect.h);
153     mCropRect = rect;
154 }
155 
PreparePresent()156 int32_t HdiTestLayer::PreparePresent()
157 {
158     int ret;
159     DISPLAY_TEST_LOGD();
160     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerSize(mDisplayID, mId, &mDisplayRect);
161     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display rect failed"));
162 
163     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerCrop(mDisplayID, mId, &mCropRect);
164     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display crop failed"));
165 
166     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerZorder(mDisplayID, mId, mZorder);
167     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display zorder failed"));
168 
169     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerCompositionType(mDisplayID, mId, mCompType);
170     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
171         DISPLAY_TEST_LOGE("set display composition type failed"));
172 
173     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetTransformMode(mDisplayID, mId, mTransform);
174     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set transform mode failed"));
175 
176     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerAlpha(mDisplayID, mId, &mAlpha);
177     HdiGrallocBuffer *buffer = AcquireBackBuffer();
178     DISPLAY_TEST_CHK_RETURN((buffer == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("can not get back buffer"));
179 
180     BufferHandle *handle = buffer->Get();
181     DISPLAY_TEST_CHK_RETURN((handle == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("BufferHandle is null"));
182     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerBuffer(mDisplayID, mId, handle, -1);
183     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set buffer handle failed"));
184 
185     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerBlendType(mDisplayID, mId, mBlendType);
186     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set blend type failed"));
187     return DISPLAY_SUCCESS;
188 }
189 
SetZorder(uint32_t zorder)190 void HdiTestLayer::SetZorder(uint32_t zorder)
191 {
192     DISPLAY_TEST_LOGD("the zorder is %d", zorder);
193     mZorder = zorder;
194 }
195 
SetCompType(CompositionType type)196 void HdiTestLayer::SetCompType(CompositionType type)
197 {
198     DISPLAY_TEST_LOGD("layer id %d ,the type is : %d", mId, type);
199     mCompType = type;
200 }
201 
SetTransform(TransformType transform)202 void HdiTestLayer::SetTransform(TransformType transform)
203 {
204     mTransform = transform;
205 }
206 
SetAlpha(LayerAlpha alpha)207 void HdiTestLayer::SetAlpha(LayerAlpha alpha)
208 {
209     DISPLAY_TEST_LOGD();
210     mAlpha = alpha;
211 }
212 
SetBlendType(BlendType type)213 void HdiTestLayer::SetBlendType(BlendType type)
214 {
215     DISPLAY_TEST_LOGD("type %d", type);
216     mBlendType = type;
217 }
218 
SetReleaseFence(int fd)219 void HdiTestLayer::SetReleaseFence(int fd)
220 {
221     DISPLAY_TEST_LOGD("layer id %d , fd %d", mId, fd);
222 }
223 
~HdiTestLayer()224 HdiTestLayer::~HdiTestLayer() {}
225 } // OHOS
226 } // HDI
227 } // DISPLAY
228 } // TEST
229