• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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}" PRIx64
117                                  " fmt:%{public}d stride:%{public}d",
118                                  buffer.GetFb(), iSurface.width, iSurface.height, buffer.GetPhysicalAddr(),
119                                  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,
137                                    DISPLAY_LOGE("can not get client layer buffer"));
138                 DISPLAY_DEBUGLOG("init the dst iSurface");
139                 InitGfxSurface(dstSurface, *dstBuffer);
140 
141                 opt.blendType = src.GetLayerBlenType();
142                 DISPLAY_DEBUGLOG("blendType %{public}d", opt.blendType);
143                 opt.enPixelAlpha = true;
144                 opt.enableScale = true;
145 
146                 if (src.GetAlpha().enGlobalAlpha) { // is alpha is 0xff we not set it
147                     opt.enGlobalAlpha = true;
148                     srcSurface.alpha0 = src.GetAlpha().gAlpha;
149                     DISPLAY_DEBUGLOG("src alpha %{public}x", src.GetAlpha().gAlpha);
150                 }
151                 opt.rotateType = src.GetTransFormType();
152                 DISPLAY_DEBUGLOG(" the roate type is %{public}d", opt.rotateType);
153                 IRect crop = src.GetLayerCrop();
154                 IRect displayRect = src.GetLayerDisplayRect();
155                 DISPLAY_DEBUGLOG("crop x: %{public}d y : %{public}d w : %{public}d h: %{public}d", crop.x, crop.y,
156                                  crop.w, crop.h);
157                 DISPLAY_DEBUGLOG("displayRect x: %{public}d y : %{public}d w : %{public}d h : %{public}d",
158                                  displayRect.x, displayRect.y, displayRect.w, displayRect.h);
159                 DISPLAY_CHK_RETURN(mGfxFuncs == nullptr, DISPLAY_FAILURE, DISPLAY_LOGE("Blit: mGfxFuncs is null"));
160                 return mGfxFuncs->Blit(&srcSurface, &crop, &dstSurface, &displayRect, &opt);
161             }
162 
ClearRect(HdiLayer & src,HdiLayer & dst)163             int32_t HdiGfxComposition::ClearRect(HdiLayer &src, HdiLayer &dst)
164             {
165                 ISurface dstSurface = {0};
166                 GfxOpt opt = {0};
167                 DISPLAY_DEBUGLOG();
168                 HdiLayerBuffer *dstBuffer = dst.GetCurrentBuffer();
169                 DISPLAY_CHK_RETURN((dstBuffer == nullptr), DISPLAY_FAILURE,
170                                    DISPLAY_LOGE("can not get client layer buffer"));
171                 InitGfxSurface(dstSurface, *dstBuffer);
172                 IRect rect = src.GetLayerDisplayRect();
173                 DISPLAY_CHK_RETURN(mGfxFuncs == nullptr, DISPLAY_FAILURE, DISPLAY_LOGE("Rect: mGfxFuncs is null"));
174                 return mGfxFuncs->FillRect(&dstSurface, &rect, 0, &opt);
175             }
176 
Apply(bool modeSet)177             int32_t HdiGfxComposition::Apply(bool modeSet)
178             {
179                 int32_t ret;
180                 DISPLAY_DEBUGLOG("composer layers size %{public}zd", mCompLayers.size());
181                 for (uint32_t i = 0; i < mCompLayers.size(); i++) {
182                     HdiLayer *layer = mCompLayers[i];
183                     CompositionType compType = layer->GetCompositionType();
184                     switch (compType) {
185                         case COMPOSITION_VIDEO:
186                             ret = ClearRect(*layer, *mClientLayer);
187                             DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
188                                                DISPLAY_LOGE("clear layer %{public}d failed", i));
189                             break;
190                         case COMPOSITION_DEVICE:
191                             ret = BlitLayer(*layer, *mClientLayer);
192                             DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
193                                                DISPLAY_LOGE("blit layer %{public}d failed ", i));
194                             break;
195                         default:
196                             DISPLAY_LOGE("the gfx composition can not surpport the type %{public}d", compType);
197                             break;
198                     }
199                 }
200                 return DISPLAY_SUCCESS;
201             }
202         } // namespace OHOS
203     }     // namespace HDI
204 } // namespace DISPLAY
205