1 /*
2  * Copyright (c) 2020-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 "hals/gfx_engines.h"
17 
18 #include "gfx_utils/graphic_log.h"
19 
20 namespace OHOS {
21 struct {
22     ImagePixelFormat srcColorFmt;
23     PixelFormat dstColorFmt;
24 } g_fmtMapTable[] = {
25     {IMAGE_PIXEL_FORMAT_RGB565, PIXEL_FMT_RGB_565},
26     {IMAGE_PIXEL_FORMAT_ARGB1555, PIXEL_FMT_RGBA_5551},
27     {IMAGE_PIXEL_FORMAT_RGB888, PIXEL_FMT_RGB_888},
28     {IMAGE_PIXEL_FORMAT_ARGB8888, PIXEL_FMT_RGBA_8888},
29 };
30 
ConvertFormat(const ImagePixelFormat & srcColorFmt,PixelFormat & dstColorFmt)31 static bool ConvertFormat(const ImagePixelFormat& srcColorFmt, PixelFormat& dstColorFmt)
32 {
33     int32_t i;
34     int32_t len = sizeof(g_fmtMapTable) / sizeof(g_fmtMapTable[0]);
35     for (i = 0; i < len; i++) {
36         if (g_fmtMapTable[i].srcColorFmt == srcColorFmt) {
37             dstColorFmt = g_fmtMapTable[i].dstColorFmt;
38             return true;
39         }
40     }
41     return false;
42 }
43 
Convert2ISurface(const LiteSurfaceData & liteSurfaceData,ISurface & iSurface)44 static bool Convert2ISurface(const LiteSurfaceData& liteSurfaceData, ISurface& iSurface)
45 {
46     if (!ConvertFormat(liteSurfaceData.pixelFormat, iSurface.enColorFmt)) {
47         GRAPHIC_LOGE("unsupport color format!");
48         return false;
49     }
50 
51     uintptr_t phyAddr = reinterpret_cast<uintptr_t>(liteSurfaceData.phyAddr);
52     iSurface.phyAddr = phyAddr;
53     iSurface.height = liteSurfaceData.height;
54     iSurface.width = liteSurfaceData.width;
55     iSurface.stride = liteSurfaceData.stride;
56     iSurface.clutPhyAddr = 0;
57     iSurface.bYCbCrClut = false;
58     iSurface.bAlphaMax255 = true;
59     iSurface.bAlphaExt1555 = false;
60     iSurface.alpha0 = 0;
61     iSurface.alpha1 = OPA_OPAQUE;
62     return true;
63 }
64 
Convert2IRect(const Rect & rect,IRect & iRect)65 static void Convert2IRect(const Rect& rect, IRect& iRect)
66 {
67     iRect.x = rect.GetLeft();
68     iRect.y = rect.GetTop();
69     iRect.w = rect.GetWidth();
70     iRect.h = rect.GetHeight();
71 }
72 
GetInstance()73 GfxEngines* GfxEngines::GetInstance()
74 {
75     static GfxEngines instance;
76     return &instance;
77 }
78 
InitDriver()79 bool GfxEngines::InitDriver()
80 {
81     if (GfxInitialize(&gfxFuncs_) == DISPLAY_SUCCESS) {
82         if (gfxFuncs_ == nullptr) {
83             GRAPHIC_LOGE("gfxFuncs_ is null!");
84             return false;
85         }
86         if (gfxFuncs_->InitGfx == nullptr) {
87             GRAPHIC_LOGE("InitGfx is null!");
88             return false;
89         }
90         if (gfxFuncs_->InitGfx() != DISPLAY_SUCCESS) {
91             GRAPHIC_LOGE("InitGfx failed!");
92             return false;
93         }
94         return true;
95     }
96     return false;
97 }
98 
CloseDriver()99 void GfxEngines::CloseDriver()
100 {
101     if (gfxFuncs_ == nullptr) {
102         GRAPHIC_LOGE("gfxFuncs_ is null!");
103         return;
104     }
105     if (gfxFuncs_->DeinitGfx != nullptr) {
106         if (gfxFuncs_->DeinitGfx() != DISPLAY_SUCCESS) {
107             GRAPHIC_LOGE("DeinitGfx failed!");
108         }
109     }
110     GfxUninitialize(gfxFuncs_);
111     gfxFuncs_ = nullptr;
112 }
113 
GfxFillArea(const LiteSurfaceData & dstSurfaceData,const Rect & fillArea,const ColorType & color,const OpacityType & opa)114 bool GfxEngines::GfxFillArea(const LiteSurfaceData& dstSurfaceData,
115                              const Rect& fillArea,
116                              const ColorType& color,
117                              const OpacityType& opa)
118 {
119     if (gfxFuncs_ == nullptr || dstSurfaceData.phyAddr == nullptr) {
120         return false;
121     }
122     ISurface surface = {};
123     if (!Convert2ISurface(dstSurfaceData, surface)) {
124         return false;
125     }
126     IRect rect = {};
127     Convert2IRect(fillArea, rect);
128     GfxOpt opt = {};
129     opt.enGlobalAlpha = true;
130     opt.globalAlpha = opa;
131     if (gfxFuncs_->FillRect(&surface, &rect, color.full, &opt) == DISPLAY_FAILURE) {
132         GRAPHIC_LOGE("fill rect failed!");
133         return false;
134     }
135     return true;
136 }
137 
GfxBlit(const LiteSurfaceData & srcSurfaceData,const Rect & srcRect,const LiteSurfaceData & dstSurfaceData,int16_t x,int16_t y)138 bool GfxEngines::GfxBlit(const LiteSurfaceData& srcSurfaceData,
139                          const Rect& srcRect,
140                          const LiteSurfaceData& dstSurfaceData,
141                          int16_t x,
142                          int16_t y)
143 {
144     if (gfxFuncs_ == nullptr || srcSurfaceData.phyAddr == nullptr || dstSurfaceData.phyAddr == nullptr) {
145         return false;
146     }
147     ISurface srcSurface = {};
148     ISurface dstSurface = {};
149     if (!Convert2ISurface(srcSurfaceData, srcSurface)) {
150         return false;
151     }
152     if (!Convert2ISurface(dstSurfaceData, dstSurface)) {
153         return false;
154     }
155     IRect srcIRect = {};
156     Convert2IRect(srcRect, srcIRect);
157     IRect dstIRect = {x, y, srcRect.GetWidth(), srcRect.GetHeight()};
158     if (gfxFuncs_->Blit(&srcSurface, &srcIRect, &dstSurface, &dstIRect, NULL) == DISPLAY_FAILURE) {
159         GRAPHIC_LOGE("blit failed!");
160         return false;
161     }
162     return true;
163 }
164 } // namespace OHOS
165