1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.. All rights reserved.
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 "hdilayer_context_systest.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace MockSys {
HdiLayerContext(IRect dstRect,IRect srcRect,uint32_t zOrder)21 HdiLayerContext::HdiLayerContext(IRect dstRect, IRect srcRect, uint32_t zOrder)
22 : srcRect_(srcRect), dstRect_(dstRect), zOrder_(zOrder)
23 {
24 cSurface_ = Surface::CreateSurfaceAsConsumer();
25 cSurface_->SetDefaultWidthAndHeight(srcRect_.w, srcRect_.h);
26 cSurface_->SetDefaultUsage(BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA);
27 sptr<IBufferProducer> producer = cSurface_->GetProducer();
28 pSurface_ = Surface::CreateSurfaceAsProducer(producer);
29 hdiLayer_ = HdiLayerInfo::CreateHdiLayerInfo();
30 }
31
~HdiLayerContext()32 HdiLayerContext::~HdiLayerContext()
33 {
34 cSurface_ = nullptr;
35 pSurface_ = nullptr;
36 hdiLayer_ = nullptr;
37 }
38
GetHdiLayer()39 std::shared_ptr<HdiLayerInfo> HdiLayerContext::GetHdiLayer()
40 {
41 return hdiLayer_;
42 }
43
DrawBufferColor()44 GSError HdiLayerContext::DrawBufferColor()
45 {
46 sptr<SurfaceBuffer> buffer;
47 int32_t releaseFence = -1;
48 BufferRequestConfig config = {
49 .width = srcRect_.w,
50 .height = srcRect_.h,
51 .strideAlignment = 0x8,
52 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
53 .usage = pSurface_->GetDefaultUsage(),
54 };
55
56 GSError ret = pSurface_->RequestBuffer(buffer, releaseFence, config);
57 if (ret != 0) {
58 return ret;
59 }
60 sptr<SyncFence> tempFence = new SyncFence(releaseFence);
61 tempFence->Wait(10); // 10 ms
62
63 if (buffer == nullptr) {
64 return SURFACE_ERROR_NULLPTR;
65 }
66
67 auto addr = static_cast<uint8_t *>(buffer->GetVirAddr());
68 DrawColor(addr, (uint32_t)buffer->GetWidth(), (uint32_t)buffer->GetHeight());
69
70 BufferFlushConfig flushConfig = {
71 .damage = {
72 .w = srcRect_.w,
73 .h = srcRect_.h,
74 },
75 };
76
77 ret = pSurface_->FlushBuffer(buffer, -1, flushConfig);
78 return ret;
79 }
80
DrawColor(void * image,uint32_t width,uint32_t height)81 void HdiLayerContext::DrawColor(void *image, uint32_t width, uint32_t height)
82 {
83 static uint32_t value = 0xff00ffff;
84 uint32_t *pixel = static_cast<uint32_t *>(image);
85 for (uint32_t x = 0; x < width; x++) {
86 for (uint32_t y = 0; y < height; y++) {
87 *pixel++ = value;
88 }
89 }
90 }
91
FillHdiLayer()92 GSError HdiLayerContext::FillHdiLayer()
93 {
94 sptr<SurfaceBuffer> buffer = nullptr;
95 int32_t acquireFence = -1;
96 int64_t timestamp;
97 Rect damage;
98 GSError ret = cSurface_->AcquireBuffer(buffer, acquireFence, timestamp, damage);
99 sptr<SyncFence> acquireSyncFence = new SyncFence(acquireFence);
100 if (ret != SURFACE_ERROR_OK) {
101 return ret;
102 }
103 GraphicLayerAlpha alpha = { .enPixelAlpha = true };
104 hdiLayer_->SetSurface(cSurface_);
105 hdiLayer_->SetBuffer(buffer, acquireSyncFence);
106 hdiLayer_->SetZorder(static_cast<int32_t>(zOrder_));
107 hdiLayer_->SetAlpha(alpha);
108 hdiLayer_->SetLayerSize(dstRect_);
109 hdiLayer_->SetCropRect(srcRect_);
110 hdiLayer_->SetDirtyRegion(srcRect_);
111 hdiLayer_->SetVisibleRegion(1, srcRect_);
112 hdiLayer_->SetBlendType(GraphicBlendType::GRAPHIC_BLEND_SRCOVER);
113 hdiLayer_->SetCompositionType(GraphicCompositionType::GRAPHIC_COMPOSITION_DEVICE);
114 hdiLayer_->SetTransform(GraphicTransformType::GRAPHIC_ROTATE_NONE);
115 hdiLayer_->SetPreMulti(false);
116 return ret;
117 }
118
119 } // namespace MockSys
120 } // namespace Rosen
121 } // namespace OHOS