1bafb9395Sopenharmony_ci/*
2bafb9395Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3bafb9395Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bafb9395Sopenharmony_ci * you may not use this file except in compliance with the License.
5bafb9395Sopenharmony_ci * You may obtain a copy of the License at
6bafb9395Sopenharmony_ci *
7bafb9395Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bafb9395Sopenharmony_ci *
9bafb9395Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bafb9395Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bafb9395Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bafb9395Sopenharmony_ci * See the License for the specific language governing permissions and
13bafb9395Sopenharmony_ci * limitations under the License.
14bafb9395Sopenharmony_ci */
15bafb9395Sopenharmony_ci
16bafb9395Sopenharmony_ci#include "hals/gfx_engines.h"
17bafb9395Sopenharmony_ci
18bafb9395Sopenharmony_ci#include "gfx_utils/graphic_log.h"
19bafb9395Sopenharmony_ci
20bafb9395Sopenharmony_cinamespace OHOS {
21bafb9395Sopenharmony_cistruct {
22bafb9395Sopenharmony_ci    ImagePixelFormat srcColorFmt;
23bafb9395Sopenharmony_ci    PixelFormat dstColorFmt;
24bafb9395Sopenharmony_ci} g_fmtMapTable[] = {
25bafb9395Sopenharmony_ci    {IMAGE_PIXEL_FORMAT_RGB565, PIXEL_FMT_RGB_565},
26bafb9395Sopenharmony_ci    {IMAGE_PIXEL_FORMAT_ARGB1555, PIXEL_FMT_RGBA_5551},
27bafb9395Sopenharmony_ci    {IMAGE_PIXEL_FORMAT_RGB888, PIXEL_FMT_RGB_888},
28bafb9395Sopenharmony_ci    {IMAGE_PIXEL_FORMAT_ARGB8888, PIXEL_FMT_RGBA_8888},
29bafb9395Sopenharmony_ci};
30bafb9395Sopenharmony_ci
31bafb9395Sopenharmony_cistatic bool ConvertFormat(const ImagePixelFormat& srcColorFmt, PixelFormat& dstColorFmt)
32bafb9395Sopenharmony_ci{
33bafb9395Sopenharmony_ci    int32_t i;
34bafb9395Sopenharmony_ci    int32_t len = sizeof(g_fmtMapTable) / sizeof(g_fmtMapTable[0]);
35bafb9395Sopenharmony_ci    for (i = 0; i < len; i++) {
36bafb9395Sopenharmony_ci        if (g_fmtMapTable[i].srcColorFmt == srcColorFmt) {
37bafb9395Sopenharmony_ci            dstColorFmt = g_fmtMapTable[i].dstColorFmt;
38bafb9395Sopenharmony_ci            return true;
39bafb9395Sopenharmony_ci        }
40bafb9395Sopenharmony_ci    }
41bafb9395Sopenharmony_ci    return false;
42bafb9395Sopenharmony_ci}
43bafb9395Sopenharmony_ci
44bafb9395Sopenharmony_cistatic bool Convert2ISurface(const LiteSurfaceData& liteSurfaceData, ISurface& iSurface)
45bafb9395Sopenharmony_ci{
46bafb9395Sopenharmony_ci    if (!ConvertFormat(liteSurfaceData.pixelFormat, iSurface.enColorFmt)) {
47bafb9395Sopenharmony_ci        GRAPHIC_LOGE("unsupport color format!");
48bafb9395Sopenharmony_ci        return false;
49bafb9395Sopenharmony_ci    }
50bafb9395Sopenharmony_ci
51bafb9395Sopenharmony_ci    uintptr_t phyAddr = reinterpret_cast<uintptr_t>(liteSurfaceData.phyAddr);
52bafb9395Sopenharmony_ci    iSurface.phyAddr = phyAddr;
53bafb9395Sopenharmony_ci    iSurface.height = liteSurfaceData.height;
54bafb9395Sopenharmony_ci    iSurface.width = liteSurfaceData.width;
55bafb9395Sopenharmony_ci    iSurface.stride = liteSurfaceData.stride;
56bafb9395Sopenharmony_ci    iSurface.clutPhyAddr = 0;
57bafb9395Sopenharmony_ci    iSurface.bYCbCrClut = false;
58bafb9395Sopenharmony_ci    iSurface.bAlphaMax255 = true;
59bafb9395Sopenharmony_ci    iSurface.bAlphaExt1555 = false;
60bafb9395Sopenharmony_ci    iSurface.alpha0 = 0;
61bafb9395Sopenharmony_ci    iSurface.alpha1 = OPA_OPAQUE;
62bafb9395Sopenharmony_ci    return true;
63bafb9395Sopenharmony_ci}
64bafb9395Sopenharmony_ci
65bafb9395Sopenharmony_cistatic void Convert2IRect(const Rect& rect, IRect& iRect)
66bafb9395Sopenharmony_ci{
67bafb9395Sopenharmony_ci    iRect.x = rect.GetLeft();
68bafb9395Sopenharmony_ci    iRect.y = rect.GetTop();
69bafb9395Sopenharmony_ci    iRect.w = rect.GetWidth();
70bafb9395Sopenharmony_ci    iRect.h = rect.GetHeight();
71bafb9395Sopenharmony_ci}
72bafb9395Sopenharmony_ci
73bafb9395Sopenharmony_ciGfxEngines* GfxEngines::GetInstance()
74bafb9395Sopenharmony_ci{
75bafb9395Sopenharmony_ci    static GfxEngines instance;
76bafb9395Sopenharmony_ci    return &instance;
77bafb9395Sopenharmony_ci}
78bafb9395Sopenharmony_ci
79bafb9395Sopenharmony_cibool GfxEngines::InitDriver()
80bafb9395Sopenharmony_ci{
81bafb9395Sopenharmony_ci    if (GfxInitialize(&gfxFuncs_) == DISPLAY_SUCCESS) {
82bafb9395Sopenharmony_ci        if (gfxFuncs_ == nullptr) {
83bafb9395Sopenharmony_ci            GRAPHIC_LOGE("gfxFuncs_ is null!");
84bafb9395Sopenharmony_ci            return false;
85bafb9395Sopenharmony_ci        }
86bafb9395Sopenharmony_ci        if (gfxFuncs_->InitGfx == nullptr) {
87bafb9395Sopenharmony_ci            GRAPHIC_LOGE("InitGfx is null!");
88bafb9395Sopenharmony_ci            return false;
89bafb9395Sopenharmony_ci        }
90bafb9395Sopenharmony_ci        if (gfxFuncs_->InitGfx() != DISPLAY_SUCCESS) {
91bafb9395Sopenharmony_ci            GRAPHIC_LOGE("InitGfx failed!");
92bafb9395Sopenharmony_ci            return false;
93bafb9395Sopenharmony_ci        }
94bafb9395Sopenharmony_ci        return true;
95bafb9395Sopenharmony_ci    }
96bafb9395Sopenharmony_ci    return false;
97bafb9395Sopenharmony_ci}
98bafb9395Sopenharmony_ci
99bafb9395Sopenharmony_civoid GfxEngines::CloseDriver()
100bafb9395Sopenharmony_ci{
101bafb9395Sopenharmony_ci    if (gfxFuncs_ == nullptr) {
102bafb9395Sopenharmony_ci        GRAPHIC_LOGE("gfxFuncs_ is null!");
103bafb9395Sopenharmony_ci        return;
104bafb9395Sopenharmony_ci    }
105bafb9395Sopenharmony_ci    if (gfxFuncs_->DeinitGfx != nullptr) {
106bafb9395Sopenharmony_ci        if (gfxFuncs_->DeinitGfx() != DISPLAY_SUCCESS) {
107bafb9395Sopenharmony_ci            GRAPHIC_LOGE("DeinitGfx failed!");
108bafb9395Sopenharmony_ci        }
109bafb9395Sopenharmony_ci    }
110bafb9395Sopenharmony_ci    GfxUninitialize(gfxFuncs_);
111bafb9395Sopenharmony_ci    gfxFuncs_ = nullptr;
112bafb9395Sopenharmony_ci}
113bafb9395Sopenharmony_ci
114bafb9395Sopenharmony_cibool GfxEngines::GfxFillArea(const LiteSurfaceData& dstSurfaceData,
115bafb9395Sopenharmony_ci                             const Rect& fillArea,
116bafb9395Sopenharmony_ci                             const ColorType& color,
117bafb9395Sopenharmony_ci                             const OpacityType& opa)
118bafb9395Sopenharmony_ci{
119bafb9395Sopenharmony_ci    if (gfxFuncs_ == nullptr || dstSurfaceData.phyAddr == nullptr) {
120bafb9395Sopenharmony_ci        return false;
121bafb9395Sopenharmony_ci    }
122bafb9395Sopenharmony_ci    ISurface surface = {};
123bafb9395Sopenharmony_ci    if (!Convert2ISurface(dstSurfaceData, surface)) {
124bafb9395Sopenharmony_ci        return false;
125bafb9395Sopenharmony_ci    }
126bafb9395Sopenharmony_ci    IRect rect = {};
127bafb9395Sopenharmony_ci    Convert2IRect(fillArea, rect);
128bafb9395Sopenharmony_ci    GfxOpt opt = {};
129bafb9395Sopenharmony_ci    opt.enGlobalAlpha = true;
130bafb9395Sopenharmony_ci    opt.globalAlpha = opa;
131bafb9395Sopenharmony_ci    if (gfxFuncs_->FillRect(&surface, &rect, color.full, &opt) == DISPLAY_FAILURE) {
132bafb9395Sopenharmony_ci        GRAPHIC_LOGE("fill rect failed!");
133bafb9395Sopenharmony_ci        return false;
134bafb9395Sopenharmony_ci    }
135bafb9395Sopenharmony_ci    return true;
136bafb9395Sopenharmony_ci}
137bafb9395Sopenharmony_ci
138bafb9395Sopenharmony_cibool GfxEngines::GfxBlit(const LiteSurfaceData& srcSurfaceData,
139bafb9395Sopenharmony_ci                         const Rect& srcRect,
140bafb9395Sopenharmony_ci                         const LiteSurfaceData& dstSurfaceData,
141bafb9395Sopenharmony_ci                         int16_t x,
142bafb9395Sopenharmony_ci                         int16_t y)
143bafb9395Sopenharmony_ci{
144bafb9395Sopenharmony_ci    if (gfxFuncs_ == nullptr || srcSurfaceData.phyAddr == nullptr || dstSurfaceData.phyAddr == nullptr) {
145bafb9395Sopenharmony_ci        return false;
146bafb9395Sopenharmony_ci    }
147bafb9395Sopenharmony_ci    ISurface srcSurface = {};
148bafb9395Sopenharmony_ci    ISurface dstSurface = {};
149bafb9395Sopenharmony_ci    if (!Convert2ISurface(srcSurfaceData, srcSurface)) {
150bafb9395Sopenharmony_ci        return false;
151bafb9395Sopenharmony_ci    }
152bafb9395Sopenharmony_ci    if (!Convert2ISurface(dstSurfaceData, dstSurface)) {
153bafb9395Sopenharmony_ci        return false;
154bafb9395Sopenharmony_ci    }
155bafb9395Sopenharmony_ci    IRect srcIRect = {};
156bafb9395Sopenharmony_ci    Convert2IRect(srcRect, srcIRect);
157bafb9395Sopenharmony_ci    IRect dstIRect = {x, y, srcRect.GetWidth(), srcRect.GetHeight()};
158bafb9395Sopenharmony_ci    if (gfxFuncs_->Blit(&srcSurface, &srcIRect, &dstSurface, &dstIRect, NULL) == DISPLAY_FAILURE) {
159bafb9395Sopenharmony_ci        GRAPHIC_LOGE("blit failed!");
160bafb9395Sopenharmony_ci        return false;
161bafb9395Sopenharmony_ci    }
162bafb9395Sopenharmony_ci    return true;
163bafb9395Sopenharmony_ci}
164bafb9395Sopenharmony_ci} // namespace OHOS
165