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 "layer_context.h"
17
18 #include <securec.h>
19 #include "hdi_log.h"
20
21 using namespace OHOS;
22 using namespace OHOS::Rosen;
23
24 namespace {
25 #define LOGI(fmt, ...) ::OHOS::HiviewDFX::HiLog::Info( \
26 ::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "HelloComposer"}, \
27 "%{public}s: " fmt, __func__, ##__VA_ARGS__)
28 #define LOGE(fmt, ...) ::OHOS::HiviewDFX::HiLog::Error( \
29 ::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "HelloComposer"}, \
30 "%{public}s: " fmt, __func__, ##__VA_ARGS__)
31 }
32
LayerContext(IRect dst,IRect src,uint32_t zorder,LayerType layerType)33 LayerContext::LayerContext(IRect dst, IRect src, uint32_t zorder, LayerType layerType)
34 : dst_(dst), src_(src), zorder_(zorder), layerType_(layerType)
35 {
36 cSurface_ = Surface::CreateSurfaceAsConsumer();
37 cSurface_->SetDefaultWidthAndHeight(src.w, src.h);
38 cSurface_->SetDefaultUsage(HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA);
39
40 OHOS::sptr<IBufferProducer> producer = cSurface_->GetProducer();
41 pSurface_ = Surface::CreateSurfaceAsProducer(producer);
42 cSurface_->RegisterConsumerListener(this);
43
44 hdiLayer_ = HdiLayerInfo::CreateHdiLayerInfo();
45 LOGI("%{public}s: create surface w:%{public}d, h:%{public}d", __func__, src.w, src.h);
46 }
47
~LayerContext()48 LayerContext::~LayerContext()
49 {
50 cSurface_ = nullptr;
51 pSurface_ = nullptr;
52 prevBuffer_ = nullptr;
53 hdiLayer_ = nullptr;
54 }
55
OnBufferAvailable()56 void LayerContext::OnBufferAvailable()
57 {
58 }
59
GetHdiLayer()60 const std::shared_ptr<HdiLayerInfo> LayerContext::GetHdiLayer()
61 {
62 return hdiLayer_;
63 }
64
DrawBufferColor()65 SurfaceError LayerContext::DrawBufferColor()
66 {
67 OHOS::sptr<SurfaceBuffer> buffer;
68 int32_t releaseFence = -1;
69 BufferRequestConfig config = {
70 .width = src_.w,
71 .height = src_.h,
72 .strideAlignment = 0x8,
73 .format = PIXEL_FMT_RGBA_8888,
74 .usage = pSurface_->GetDefaultUsage(),
75 };
76
77 SurfaceError ret = pSurface_->RequestBuffer(buffer, releaseFence, config);
78 if (ret != 0) {
79 LOGE("RequestBuffer failed: %{public}s", SurfaceErrorStr(ret).c_str());
80 return ret;
81 }
82
83 sptr<SyncFence> tempFence = new SyncFence(releaseFence);
84 tempFence->Wait(100); // 100 ms
85
86 if (buffer == nullptr) {
87 LOGE("%s: buffer is nullptr", __func__);
88 return SURFACE_ERROR_NULLPTR;
89 }
90
91 auto addr = static_cast<uint8_t *>(buffer->GetVirAddr());
92 LOGI("buffer w:%{public}d h:%{public}d stride:%{public}d", buffer->GetWidth(),
93 buffer->GetHeight(), buffer->GetBufferHandle()->stride);
94 DrawColor(addr, buffer->GetWidth(), buffer->GetHeight());
95
96 BufferFlushConfig flushConfig = {
97 .damage = {
98 .w = src_.w,
99 .h = src_.h,
100 },
101 };
102
103 ret = pSurface_->FlushBuffer(buffer, -1, flushConfig);
104 if (ret != SURFACE_ERROR_OK) {
105 LOGE("FlushBuffer failed");
106 }
107
108 return ret;
109 }
110
FillHDILayer()111 SurfaceError LayerContext::FillHDILayer()
112 {
113 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
114 int32_t acquireFence = -1;
115 int64_t timestamp;
116 Rect damage;
117 SurfaceError ret = cSurface_->AcquireBuffer(buffer, acquireFence, timestamp, damage);
118 UniqueFd acquireFenceFd(acquireFence);
119 if (ret != SURFACE_ERROR_OK) {
120 LOGE("Acquire buffer failed");
121 return ret;
122 }
123
124 LayerAlpha alpha = { .enPixelAlpha = true };
125
126 hdiLayer_->SetSurface(cSurface_);
127 auto acquireSyncFence = new SyncFence(acquireFenceFd.Release());
128 hdiLayer_->SetBuffer(buffer, acquireSyncFence, prevBuffer_, prevFence_);
129 hdiLayer_->SetZorder(static_cast<int32_t>(zorder_));
130 hdiLayer_->SetAlpha(alpha);
131 hdiLayer_->SetCompositionType(CompositionType::COMPOSITION_DEVICE);
132 hdiLayer_->SetVisibleRegion(1, src_);
133 hdiLayer_->SetDirtyRegion(src_);
134 hdiLayer_->SetLayerSize(dst_);
135 hdiLayer_->SetBlendType(BlendType::BLEND_SRCOVER);
136 hdiLayer_->SetCropRect(src_);
137 hdiLayer_->SetPreMulti(false);
138
139 prevBuffer_ = buffer;
140 prevFence_ = acquireSyncFence;
141
142 return ret;
143 }
144
DrawColor(void * image,int width,int height)145 void LayerContext::DrawColor(void *image, int width, int height)
146 {
147 if (layerType_ >= LayerType::LAYER_EXTRA) {
148 DrawExtraColor(image, static_cast<uint32_t>(width), static_cast<uint32_t>(height));
149 } else {
150 DrawBaseColor(image, static_cast<uint32_t>(width), static_cast<uint32_t>(height));
151 }
152 }
153
DrawExtraColor(void * image,uint32_t width,uint32_t height)154 void LayerContext::DrawExtraColor(void *image, uint32_t width, uint32_t height)
155 {
156 frameCounter_ = frameCounter_ % 60; // 60 is cycle size
157 if (frameCounter_ == 0) {
158 colorIndex_ = colorIndex_ % colors_.size();
159 color_ = colors_[colorIndex_];
160 colorIndex_++;
161 }
162
163 uint32_t *pixel = static_cast<uint32_t *>(image);
164 for (uint32_t x = 0; x < width; x++) {
165 for (uint32_t y = 0; y < height; y++) {
166 *pixel++ = color_;
167 }
168 }
169
170 frameCounter_++;
171 }
172
DrawBaseColor(void * image,uint32_t width,uint32_t height)173 void LayerContext::DrawBaseColor(void *image, uint32_t width, uint32_t height)
174 {
175 static uint32_t value = 0x00;
176 if (layerType_ == LayerType::LAYER_STATUS) {
177 value = 0xfff0000f;
178 } else if (layerType_ == LayerType::LAYER_LAUNCHER) {
179 value = 0xffffffff;
180 } else {
181 value = 0xff00ffff;
182 }
183
184 uint32_t *pixel = static_cast<uint32_t *>(image);
185 for (uint32_t x = 0; x < width; x++) {
186 for (uint32_t y = 0; y < height; y++) {
187 *pixel++ = value;
188 }
189 }
190 }
191