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