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