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 <cinttypes>
17 #include <dlfcn.h>
18 #include <cerrno>
19 #include "display_gfx.h"
20 #include "hdi_gfx_composition.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace DISPLAY {
Init(void)25 int32_t HdiGfxComposition::Init(void)
26 {
27 DISPLAY_DEBUGLOG();
28 int32_t ret = GfxModuleInit();
29 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS) || (mGfxFuncs == nullptr), DISPLAY_FAILURE,
30 DISPLAY_LOGE("GfxModuleInit failed"));
31 ret = mGfxFuncs->InitGfx();
32 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("gfx init failed"));
33 return DISPLAY_SUCCESS;
34 }
35
GfxModuleInit(void)36 int32_t HdiGfxComposition::GfxModuleInit(void)
37 {
38 DISPLAY_DEBUGLOG();
39 mGfxModule = dlopen("libdisplay_gfx.z.so", RTLD_NOW | RTLD_NOLOAD);
40 if (mGfxModule != nullptr) {
41 DISPLAY_LOGI("Module '%{public}s' already loaded", "libdisplay_gfx.z.so");
42 } else {
43 DISPLAY_LOGI("Loading module '%{public}s'", "libdisplay_gfx.z.so");
44 mGfxModule = dlopen("libdisplay_gfx.z.so", RTLD_NOW);
45 if (mGfxModule == nullptr) {
46 DISPLAY_LOGE("Failed to load module: %{public}s", dlerror());
47 return DISPLAY_FAILURE;
48 }
49 }
50
51 using InitFunc = int32_t (*)(GfxFuncs **funcs);
52 InitFunc func = reinterpret_cast<InitFunc>(dlsym(mGfxModule, "GfxInitialize"));
53 if (func == nullptr) {
54 DISPLAY_LOGE("Failed to lookup %{public}s function: %s", "GfxInitialize", dlerror());
55 dlclose(mGfxModule);
56 return DISPLAY_FAILURE;
57 }
58 return func(&mGfxFuncs);
59 }
60
GfxModuleDeinit(void)61 int32_t HdiGfxComposition::GfxModuleDeinit(void)
62 {
63 DISPLAY_DEBUGLOG();
64 int32_t ret = DISPLAY_SUCCESS;
65 if (mGfxModule == nullptr) {
66 using DeinitFunc = int32_t (*)(GfxFuncs *funcs);
67 DeinitFunc func = reinterpret_cast<DeinitFunc>(dlsym(mGfxModule, "GfxUninitialize"));
68 if (func == nullptr) {
69 DISPLAY_LOGE("Failed to lookup %{public}s function: %s", "GfxUninitialize", dlerror());
70 } else {
71 ret = func(mGfxFuncs);
72 }
73 dlclose(mGfxModule);
74 }
75 return ret;
76 }
77
CanHandle(HdiLayer & hdiLayer)78 bool HdiGfxComposition::CanHandle(HdiLayer &hdiLayer)
79 {
80 DISPLAY_DEBUGLOG();
81 (void)hdiLayer;
82 return true;
83 }
84
SetLayers(std::vector<HdiLayer * > & layers,HdiLayer & clientLayer)85 int32_t HdiGfxComposition::SetLayers(std::vector<HdiLayer *> &layers, HdiLayer &clientLayer)
86 {
87 DISPLAY_DEBUGLOG("layers size %{public}zd", layers.size());
88 mClientLayer = &clientLayer;
89 mCompLayers.clear();
90 for (auto &layer : layers) {
91 if (CanHandle(*layer)) {
92 if ((layer->GetCompositionType() != COMPOSITION_VIDEO) &&
93 (layer->GetCompositionType() != COMPOSITION_CURSOR)) {
94 layer->SetDeviceSelect(COMPOSITION_DEVICE);
95 } else {
96 layer->SetDeviceSelect(layer->GetCompositionType());
97 }
98 mCompLayers.push_back(layer);
99 }
100 }
101 DISPLAY_DEBUGLOG("composer layers size %{public}zd", mCompLayers.size());
102 return DISPLAY_SUCCESS;
103 }
104
InitGfxSurface(ISurface & iSurface,HdiLayerBuffer & buffer)105 void HdiGfxComposition::InitGfxSurface(ISurface &iSurface, HdiLayerBuffer &buffer)
106 {
107 iSurface.width = buffer.GetWight();
108 iSurface.height = buffer.GetHeight();
109 iSurface.phyAddr = buffer.GetFb(); // buffer.GetPhysicalAddr();
110 iSurface.enColorFmt = (PixelFormat)buffer.GetFormat();
111 iSurface.stride = buffer.GetStride();
112 iSurface.bAlphaExt1555 = true;
113 iSurface.bAlphaMax255 = true;
114 iSurface.alpha0 = 0XFF;
115 iSurface.alpha1 = 0XFF;
116 DISPLAY_DEBUGLOG("iSurface fd %{public}d w:%{public}d h:%{public}d addr:0x%{public}" \
117 PRIx64 " fmt:%{public}d stride:%{public}d",
118 buffer.GetFb(), iSurface.width, iSurface.height,
119 buffer.GetPhysicalAddr(), iSurface.enColorFmt, iSurface.stride);
120 }
121
122 // now not handle the alpha of layer
BlitLayer(HdiLayer & src,HdiLayer & dst)123 int32_t HdiGfxComposition::BlitLayer(HdiLayer &src, HdiLayer &dst)
124 {
125 ISurface srcSurface = { 0 };
126 ISurface dstSurface = { 0 };
127 GfxOpt opt = { 0 };
128 src.WaitAcquireFence();
129 DISPLAY_DEBUGLOG();
130 HdiLayerBuffer *srcBuffer = src.GetCurrentBuffer();
131 DISPLAY_CHK_RETURN((srcBuffer == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("the srcbuffer is null"));
132 DISPLAY_DEBUGLOG("init the src iSurface");
133 InitGfxSurface(srcSurface, *srcBuffer);
134
135 HdiLayerBuffer *dstBuffer = dst.GetCurrentBuffer();
136 DISPLAY_CHK_RETURN((dstBuffer == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not get client layer buffer"));
137 DISPLAY_DEBUGLOG("init the dst iSurface");
138 InitGfxSurface(dstSurface, *dstBuffer);
139
140 opt.blendType = src.GetLayerBlenType();
141 DISPLAY_DEBUGLOG("blendType %{public}d", opt.blendType);
142 opt.enPixelAlpha = true;
143 opt.enableScale = true;
144
145 if (src.GetAlpha().enGlobalAlpha) { // is alpha is 0xff we not set it
146 opt.enGlobalAlpha = true;
147 srcSurface.alpha0 = src.GetAlpha().gAlpha;
148 DISPLAY_DEBUGLOG("src alpha %{public}x", src.GetAlpha().gAlpha);
149 }
150 opt.rotateType = src.GetTransFormType();
151 DISPLAY_DEBUGLOG(" the roate type is %{public}d", opt.rotateType);
152 IRect crop = src.GetLayerCrop();
153 IRect displayRect = src.GetLayerDisplayRect();
154 DISPLAY_DEBUGLOG("crop x: %{public}d y : %{public}d w : %{public}d h: %{public}d", crop.x, crop.y, crop.w, crop.h);
155 DISPLAY_DEBUGLOG("displayRect x: %{public}d y : %{public}d w : %{public}d h : %{public}d",
156 displayRect.x, displayRect.y, displayRect.w, displayRect.h);
157 DISPLAY_CHK_RETURN(mGfxFuncs == nullptr, DISPLAY_FAILURE, DISPLAY_LOGE("Blit: mGfxFuncs is null"));
158 return mGfxFuncs->Blit(&srcSurface, &crop, &dstSurface, &displayRect, &opt);
159 }
160
ClearRect(HdiLayer & src,HdiLayer & dst)161 int32_t HdiGfxComposition::ClearRect(HdiLayer &src, HdiLayer &dst)
162 {
163 ISurface dstSurface = { 0 };
164 GfxOpt opt = { 0 };
165 DISPLAY_DEBUGLOG();
166 HdiLayerBuffer *dstBuffer = dst.GetCurrentBuffer();
167 DISPLAY_CHK_RETURN((dstBuffer == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not get client layer buffer"));
168 InitGfxSurface(dstSurface, *dstBuffer);
169 IRect rect = src.GetLayerDisplayRect();
170 DISPLAY_CHK_RETURN(mGfxFuncs == nullptr, DISPLAY_FAILURE, DISPLAY_LOGE("Rect: mGfxFuncs is null"));
171 return mGfxFuncs->FillRect(&dstSurface, &rect, 0, &opt);
172 }
173
Apply(bool modeSet)174 int32_t HdiGfxComposition::Apply(bool modeSet)
175 {
176 int32_t ret;
177 DISPLAY_DEBUGLOG("composer layers size %{public}zd", mCompLayers.size());
178 for (uint32_t i = 0; i < mCompLayers.size(); i++) {
179 HdiLayer *layer = mCompLayers[i];
180 CompositionType compType = layer->GetCompositionType();
181 switch (compType) {
182 case COMPOSITION_VIDEO:
183 ret = ClearRect(*layer, *mClientLayer);
184 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
185 DISPLAY_LOGE("clear layer %{public}d failed", i));
186 break;
187 case COMPOSITION_DEVICE:
188 ret = BlitLayer(*layer, *mClientLayer);
189 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
190 DISPLAY_LOGE("blit layer %{public}d failed ", i));
191 break;
192 default:
193 DISPLAY_LOGE("the gfx composition can not surpport the type %{public}d", compType);
194 break;
195 }
196 }
197 return DISPLAY_SUCCESS;
198 }
199 } // namespace OHOS
200 } // namespace HDI
201 } // namespace DISPLAY
202