1d6aed566Sopenharmony_ci/*
2d6aed566Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3d6aed566Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d6aed566Sopenharmony_ci * you may not use this file except in compliance with the License.
5d6aed566Sopenharmony_ci * You may obtain a copy of the License at
6d6aed566Sopenharmony_ci *
7d6aed566Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d6aed566Sopenharmony_ci *
9d6aed566Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d6aed566Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d6aed566Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d6aed566Sopenharmony_ci * See the License for the specific language governing permissions and
13d6aed566Sopenharmony_ci * limitations under the License.
14d6aed566Sopenharmony_ci */
15d6aed566Sopenharmony_ci
16d6aed566Sopenharmony_ci#include "fbdev.h"
17d6aed566Sopenharmony_ci
18d6aed566Sopenharmony_ci#include "display_layer.h"
19d6aed566Sopenharmony_ci#include "display_type.h"
20d6aed566Sopenharmony_ci#include "gfx_utils/color.h"
21d6aed566Sopenharmony_ci#include "gfx_utils/graphic_log.h"
22d6aed566Sopenharmony_ci#include "graphic_config.h"
23d6aed566Sopenharmony_ci
24d6aed566Sopenharmony_cinamespace OHOS {
25d6aed566Sopenharmony_cistruct DisplayDesc {
26d6aed566Sopenharmony_ci    LayerFuncs* layerFuncs;
27d6aed566Sopenharmony_ci    uint32_t devId;
28d6aed566Sopenharmony_ci    uint32_t layerId;
29d6aed566Sopenharmony_ci    LayerBuffer buffer;
30d6aed566Sopenharmony_ci    LayerRotateType rotateType;
31d6aed566Sopenharmony_ci};
32d6aed566Sopenharmony_ci
33d6aed566Sopenharmony_cistatic LayerInfo g_layerInfo = {};
34d6aed566Sopenharmony_cistatic DisplayDesc g_display = {};
35d6aed566Sopenharmony_ciconstexpr const uint8_t DISPLAY_DEV_ID = 0;
36d6aed566Sopenharmony_ci#ifdef LAYER_PF_ARGB1555
37d6aed566Sopenharmony_ciconstexpr const uint8_t LAYER_BPP = 16;
38d6aed566Sopenharmony_ciconstexpr const PixelFormat HDI_LAYER_PIXEL_FORMAT = PIXEL_FMT_RGBA_5551;
39d6aed566Sopenharmony_ciconstexpr const ImagePixelFormat LAYER_PIXEL_FORMAT = IMAGE_PIXEL_FORMAT_ARGB1555;
40d6aed566Sopenharmony_ci#elif defined LAYER_PF_ARGB8888
41d6aed566Sopenharmony_ciconstexpr const uint8_t LAYER_BPP = 32;
42d6aed566Sopenharmony_ciconstexpr const PixelFormat HDI_LAYER_PIXEL_FORMAT = PIXEL_FMT_RGBA_8888;
43d6aed566Sopenharmony_ciconstexpr const ImagePixelFormat LAYER_PIXEL_FORMAT = IMAGE_PIXEL_FORMAT_ARGB8888;
44d6aed566Sopenharmony_ci#endif
45d6aed566Sopenharmony_ciconstexpr const uint8_t BITS_PER_BYTE = 8;
46d6aed566Sopenharmony_cistatic LiteSurfaceData g_devSurfaceData = {};
47d6aed566Sopenharmony_ci
48d6aed566Sopenharmony_civoid FbdevFlush(void)
49d6aed566Sopenharmony_ci{
50d6aed566Sopenharmony_ci    if (g_display.layerFuncs->Flush != nullptr) {
51d6aed566Sopenharmony_ci        int32_t ret =
52d6aed566Sopenharmony_ci            g_display.layerFuncs->Flush(g_display.devId, g_display.layerId, &g_display.buffer);
53d6aed566Sopenharmony_ci        if (ret != DISPLAY_SUCCESS) {
54d6aed566Sopenharmony_ci            GRAPHIC_LOGE("flush fail");
55d6aed566Sopenharmony_ci            return;
56d6aed566Sopenharmony_ci        }
57d6aed566Sopenharmony_ci    }
58d6aed566Sopenharmony_ci}
59d6aed566Sopenharmony_ci
60d6aed566Sopenharmony_ciLayerRotateType GetLayerRotateType(void)
61d6aed566Sopenharmony_ci{
62d6aed566Sopenharmony_ci    return g_display.rotateType;
63d6aed566Sopenharmony_ci}
64d6aed566Sopenharmony_ci
65d6aed566Sopenharmony_ciLiteSurfaceData* GetDevSurfaceData(void)
66d6aed566Sopenharmony_ci{
67d6aed566Sopenharmony_ci    return &g_devSurfaceData;
68d6aed566Sopenharmony_ci}
69d6aed566Sopenharmony_ci
70d6aed566Sopenharmony_cistatic void DisplayInit(void)
71d6aed566Sopenharmony_ci{
72d6aed566Sopenharmony_ci    int32_t ret = LayerInitialize(&g_display.layerFuncs);
73d6aed566Sopenharmony_ci    if (ret != DISPLAY_SUCCESS || g_display.layerFuncs == nullptr) {
74d6aed566Sopenharmony_ci        GRAPHIC_LOGE("layer initialize failed");
75d6aed566Sopenharmony_ci        return;
76d6aed566Sopenharmony_ci    }
77d6aed566Sopenharmony_ci    if (g_display.layerFuncs->InitDisplay != nullptr) {
78d6aed566Sopenharmony_ci        ret = g_display.layerFuncs->InitDisplay(DISPLAY_DEV_ID);
79d6aed566Sopenharmony_ci        if (ret != DISPLAY_SUCCESS) {
80d6aed566Sopenharmony_ci            GRAPHIC_LOGE("InitDisplay fail");
81d6aed566Sopenharmony_ci            return;
82d6aed566Sopenharmony_ci        }
83d6aed566Sopenharmony_ci    }
84d6aed566Sopenharmony_ci}
85d6aed566Sopenharmony_ci
86d6aed566Sopenharmony_cistatic void OpenLayer(void)
87d6aed566Sopenharmony_ci{
88d6aed566Sopenharmony_ci    if (g_display.layerFuncs->GetDisplayInfo == nullptr) {
89d6aed566Sopenharmony_ci        return;
90d6aed566Sopenharmony_ci    }
91d6aed566Sopenharmony_ci    g_display.devId = DISPLAY_DEV_ID;
92d6aed566Sopenharmony_ci    DisplayInfo displayInfo = {};
93d6aed566Sopenharmony_ci    int32_t ret = g_display.layerFuncs->GetDisplayInfo(g_display.devId, &displayInfo);
94d6aed566Sopenharmony_ci    if (ret != DISPLAY_SUCCESS) {
95d6aed566Sopenharmony_ci        GRAPHIC_LOGE("GetDisplayInfo fail");
96d6aed566Sopenharmony_ci        return;
97d6aed566Sopenharmony_ci    }
98d6aed566Sopenharmony_ci    g_display.rotateType = static_cast<LayerRotateType>(displayInfo.rotAngle);
99d6aed566Sopenharmony_ci    g_layerInfo.width = displayInfo.width;
100d6aed566Sopenharmony_ci    g_layerInfo.height = displayInfo.height;
101d6aed566Sopenharmony_ci    g_layerInfo.bpp = LAYER_BPP;
102d6aed566Sopenharmony_ci    g_layerInfo.pixFormat = HDI_LAYER_PIXEL_FORMAT;
103d6aed566Sopenharmony_ci    g_layerInfo.type = LAYER_TYPE_GRAPHIC;
104d6aed566Sopenharmony_ci    if (g_display.layerFuncs->CreateLayer != nullptr) {
105d6aed566Sopenharmony_ci        ret = g_display.layerFuncs->CreateLayer(g_display.devId, &g_layerInfo, &g_display.layerId);
106d6aed566Sopenharmony_ci        if (ret != DISPLAY_SUCCESS) {
107d6aed566Sopenharmony_ci            GRAPHIC_LOGE("CreateLayer fail");
108d6aed566Sopenharmony_ci            return;
109d6aed566Sopenharmony_ci        }
110d6aed566Sopenharmony_ci    }
111d6aed566Sopenharmony_ci}
112d6aed566Sopenharmony_ci
113d6aed566Sopenharmony_cistatic void SetLayerVisible(bool visibled)
114d6aed566Sopenharmony_ci{
115d6aed566Sopenharmony_ci    if (g_display.layerFuncs->SetLayerVisible != nullptr) {
116d6aed566Sopenharmony_ci        int32_t ret = g_display.layerFuncs->SetLayerVisible(g_display.devId, g_display.layerId, visibled);
117d6aed566Sopenharmony_ci        if (ret != DISPLAY_SUCCESS) {
118d6aed566Sopenharmony_ci            GRAPHIC_LOGE("setLayerVisible fail");
119d6aed566Sopenharmony_ci            return;
120d6aed566Sopenharmony_ci        }
121d6aed566Sopenharmony_ci    }
122d6aed566Sopenharmony_ci}
123d6aed566Sopenharmony_ci
124d6aed566Sopenharmony_cistatic void SetLayerDirtyRegion(void)
125d6aed566Sopenharmony_ci{
126d6aed566Sopenharmony_ci    IRect rect = {0, 0, g_layerInfo.width, g_layerInfo.height};
127d6aed566Sopenharmony_ci    if (g_display.layerFuncs->SetLayerDirtyRegion != nullptr) {
128d6aed566Sopenharmony_ci        int32_t ret = g_display.layerFuncs->SetLayerDirtyRegion(g_display.devId, g_display.layerId, &rect);
129d6aed566Sopenharmony_ci        if (ret != DISPLAY_SUCCESS) {
130d6aed566Sopenharmony_ci            GRAPHIC_LOGE("setLayerDirtyRegion fail");
131d6aed566Sopenharmony_ci            return;
132d6aed566Sopenharmony_ci        }
133d6aed566Sopenharmony_ci    }
134d6aed566Sopenharmony_ci}
135d6aed566Sopenharmony_ci
136d6aed566Sopenharmony_cistatic void AllocDisplayBuffer(void)
137d6aed566Sopenharmony_ci{
138d6aed566Sopenharmony_ci    if (g_display.layerFuncs->GetLayerBuffer != nullptr) {
139d6aed566Sopenharmony_ci        int32_t ret =
140d6aed566Sopenharmony_ci            g_display.layerFuncs->GetLayerBuffer(g_display.devId, g_display.layerId, &g_display.buffer);
141d6aed566Sopenharmony_ci        if (ret != DISPLAY_SUCCESS) {
142d6aed566Sopenharmony_ci            GRAPHIC_LOGE("getLayerBuffer fail");
143d6aed566Sopenharmony_ci            return;
144d6aed566Sopenharmony_ci        }
145d6aed566Sopenharmony_ci    }
146d6aed566Sopenharmony_ci}
147d6aed566Sopenharmony_ci
148d6aed566Sopenharmony_civoid FbdevInit(void)
149d6aed566Sopenharmony_ci{
150d6aed566Sopenharmony_ci    DisplayInit();
151d6aed566Sopenharmony_ci    OpenLayer();
152d6aed566Sopenharmony_ci    SetLayerVisible(true);
153d6aed566Sopenharmony_ci    SetLayerDirtyRegion();
154d6aed566Sopenharmony_ci    AllocDisplayBuffer();
155d6aed566Sopenharmony_ci    uintptr_t phyAddr = g_display.buffer.data.phyAddr;
156d6aed566Sopenharmony_ci    g_devSurfaceData.phyAddr = reinterpret_cast<uint8_t*>(phyAddr);
157d6aed566Sopenharmony_ci    g_devSurfaceData.pixelFormat = LAYER_PIXEL_FORMAT;
158d6aed566Sopenharmony_ci    g_devSurfaceData.width = g_layerInfo.width;
159d6aed566Sopenharmony_ci    g_devSurfaceData.height = g_layerInfo.height;
160d6aed566Sopenharmony_ci    g_devSurfaceData.stride = g_display.buffer.pitch;
161d6aed566Sopenharmony_ci    g_devSurfaceData.virAddr = static_cast<uint8_t*>(g_display.buffer.data.virAddr);
162d6aed566Sopenharmony_ci    g_devSurfaceData.bytePerPixel = g_layerInfo.bpp / BITS_PER_BYTE;
163d6aed566Sopenharmony_ci}
164d6aed566Sopenharmony_ci
165d6aed566Sopenharmony_civoid FbdevClose(void)
166d6aed566Sopenharmony_ci{
167d6aed566Sopenharmony_ci    if (g_display.layerFuncs->CloseLayer == nullptr) {
168d6aed566Sopenharmony_ci        return;
169d6aed566Sopenharmony_ci    }
170d6aed566Sopenharmony_ci    if (g_display.layerFuncs->DeinitDisplay == nullptr) {
171d6aed566Sopenharmony_ci        return;
172d6aed566Sopenharmony_ci    }
173d6aed566Sopenharmony_ci    int32_t ret = g_display.layerFuncs->CloseLayer(g_display.devId, g_display.layerId);
174d6aed566Sopenharmony_ci    if (ret != DISPLAY_SUCCESS) {
175d6aed566Sopenharmony_ci        GRAPHIC_LOGE("CloseLayer fail");
176d6aed566Sopenharmony_ci        return;
177d6aed566Sopenharmony_ci    }
178d6aed566Sopenharmony_ci    ret = g_display.layerFuncs->DeinitDisplay(g_display.devId);
179d6aed566Sopenharmony_ci    if (ret != DISPLAY_SUCCESS) {
180d6aed566Sopenharmony_ci        GRAPHIC_LOGE("DeinitDisplay fail");
181d6aed566Sopenharmony_ci        return;
182d6aed566Sopenharmony_ci    }
183d6aed566Sopenharmony_ci    ret = LayerUninitialize(g_display.layerFuncs);
184d6aed566Sopenharmony_ci    if (ret != DISPLAY_SUCCESS) {
185d6aed566Sopenharmony_ci        GRAPHIC_LOGE("LayerUninitialize fail");
186d6aed566Sopenharmony_ci    }
187d6aed566Sopenharmony_ci}
188d6aed566Sopenharmony_ci} // namespace OHOS
189