• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_LOGI("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_LOGI("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(const LayerInfo & info,const uint32_t id,const uint32_t displayId)105 HdiTestLayer::HdiTestLayer(const LayerInfo &info, const uint32_t id, const 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_LOGI();
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_LOGI("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_LOGI("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     DISPLAY_TEST_LOGI();
159     int ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerSize(mDisplayID, mId, &mDisplayRect);
160     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display rect failed"));
161 
162     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerCrop(mDisplayID, mId, &mCropRect);
163     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display crop failed"));
164 
165     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerZorder(mDisplayID, mId, mZorder);
166     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set display zorder failed"));
167 
168     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerCompositionType(mDisplayID, mId, mCompType);
169     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
170         DISPLAY_TEST_LOGE("set display composition type failed"));
171 
172     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetTransformMode(mDisplayID, mId, mTransform);
173     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set transform mode failed"));
174 
175     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerAlpha(mDisplayID, mId, &mAlpha);
176     HdiGrallocBuffer *buffer = AcquireBackBuffer();
177     DISPLAY_TEST_CHK_RETURN((buffer == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("can not get back buffer"));
178 
179     BufferHandle *handle = buffer->Get();
180     DISPLAY_TEST_CHK_RETURN((handle == nullptr), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("BufferHandle is null"));
181     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerBuffer(mDisplayID, mId, handle, -1);
182     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set buffer handle failed"));
183 
184     ret = HdiTestDevice::GetInstance().GetLayerFuncs().SetLayerBlendType(mDisplayID, mId, mBlendType);
185     DISPLAY_TEST_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_TEST_LOGE("set blend type failed"));
186     return DISPLAY_SUCCESS;
187 }
188 
SetZorder(uint32_t zorder)189 void HdiTestLayer::SetZorder(uint32_t zorder)
190 {
191     DISPLAY_TEST_LOGI("the zorder is %d", zorder);
192     mZorder = zorder;
193 }
194 
SetCompType(CompositionType type)195 void HdiTestLayer::SetCompType(CompositionType type)
196 {
197     DISPLAY_TEST_LOGI("layer id %d ,the type is : %d", mId, type);
198     mCompType = type;
199 }
200 
SetTransform(TransformType transform)201 void HdiTestLayer::SetTransform(TransformType transform)
202 {
203     mTransform = transform;
204 }
205 
SetAlpha(LayerAlpha alpha)206 void HdiTestLayer::SetAlpha(LayerAlpha alpha)
207 {
208     DISPLAY_TEST_LOGI();
209     mAlpha = alpha;
210 }
211 
SetBlendType(BlendType type)212 void HdiTestLayer::SetBlendType(BlendType type)
213 {
214     DISPLAY_TEST_LOGI("type %d", type);
215     mBlendType = type;
216 }
217 
SetReleaseFence(int fd)218 void HdiTestLayer::SetReleaseFence(int fd)
219 {
220     DISPLAY_TEST_LOGI("layer id %d , fd %d", mId, fd);
221 }
222 
~HdiTestLayer()223 HdiTestLayer::~HdiTestLayer() {}
224 } // OHOS
225 } // HDI
226 } // DISPLAY
227 } // TEST
228