• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 #include "fbdev.h"
16 #include "display_layer.h"
17 #include "display_type.h"
18 #include "gfx_utils/color.h"
19 #include "gfx_utils/graphic_log.h"
20 #include "graphic_config.h"
21 
22 namespace OHOS {
23 struct DisplayDesc {
24     LayerFuncs* layerFuncs;
25     uint32_t devId;
26     uint32_t layerId;
27     LayerBuffer buffer;
28     LayerRotateType rotateType;
29 };
30 
31 static LayerInfo g_layerInfo = {};
32 static DisplayDesc g_display = {};
33 constexpr const uint8_t DISPALY_DEV_ID = 0;
34 #ifdef LAYER_PF_ARGB1555
35 constexpr const uint8_t LAYER_BPP = 16;
36 constexpr const PixelFormat HDI_LAYER_PIXEL_FORMAT = PIXEL_FMT_RGBA_5551;
37 constexpr const ImagePixelFormat LAYER_PIXEL_FORMAT = IMAGE_PIXEL_FORMAT_ARGB1555;
38 #elif defined LAYER_PF_ARGB8888
39 constexpr const uint8_t LAYER_BPP = 32;
40 constexpr const PixelFormat HDI_LAYER_PIXEL_FORMAT = PIXEL_FMT_RGBA_8888;
41 constexpr const ImagePixelFormat LAYER_PIXEL_FORMAT = IMAGE_PIXEL_FORMAT_ARGB8888;
42 #endif
43 constexpr const uint8_t BITS_PER_BYTE = 8;
44 static LiteSurfaceData g_devSurfaceData = {};
45 
GetLayerRotateType()46 LayerRotateType GetLayerRotateType()
47 {
48     return g_display.rotateType;
49 }
50 
AllocDisplayBuffer(void)51 static void AllocDisplayBuffer(void)
52 {
53     if (g_display.layerFuncs->GetLayerBuffer != nullptr) {
54         int32_t ret = g_display.layerFuncs->GetLayerBuffer(g_display.devId, g_display.layerId, &g_display.buffer);
55         if (ret != DISPLAY_SUCCESS) {
56             GRAPHIC_LOGE("getLayerBuffer fail");
57             return;
58         }
59     }
60 }
61 
GetDevSurfaceData()62 LiteSurfaceData* GetDevSurfaceData()
63 {
64     AllocDisplayBuffer();
65     g_devSurfaceData.phyAddr = reinterpret_cast<uint8_t*>(g_display.buffer.data.phyAddr);
66     g_devSurfaceData.virAddr = static_cast<uint8_t*>(g_display.buffer.data.virAddr);
67     g_devSurfaceData.pixelFormat = LAYER_PIXEL_FORMAT;
68     g_devSurfaceData.width = g_layerInfo.width;
69     g_devSurfaceData.height = g_layerInfo.height;
70     g_devSurfaceData.stride = g_display.buffer.pitch;
71     g_devSurfaceData.bytePerPixel = g_layerInfo.bpp / BITS_PER_BYTE;
72     return &g_devSurfaceData;
73 }
74 
DisplayInit(void)75 static void DisplayInit(void)
76 {
77     int32_t ret = LayerInitialize(&g_display.layerFuncs);
78     if (ret != DISPLAY_SUCCESS || g_display.layerFuncs == nullptr) {
79         GRAPHIC_LOGE("layer initialize failed");
80         return;
81     }
82     if (g_display.layerFuncs->InitDisplay != nullptr) {
83         ret = g_display.layerFuncs->InitDisplay(DISPALY_DEV_ID);
84         if (ret != DISPLAY_SUCCESS) {
85             GRAPHIC_LOGE("InitDisplay fail");
86             return;
87         }
88     }
89 }
90 
OpenLayer(void)91 static void OpenLayer(void)
92 {
93     if (g_display.layerFuncs->GetDisplayInfo == nullptr) {
94         return;
95     }
96     g_display.devId = DISPALY_DEV_ID;
97     DisplayInfo displayInfo = {};
98     int32_t ret = g_display.layerFuncs->GetDisplayInfo(g_display.devId, &displayInfo);
99     if (ret != DISPLAY_SUCCESS) {
100         GRAPHIC_LOGE("GetDisplayInfo fail");
101         return;
102     }
103     g_display.rotateType = static_cast<LayerRotateType>(displayInfo.rotAngle);
104     g_layerInfo.width = displayInfo.width;
105     g_layerInfo.height = displayInfo.height;
106     g_layerInfo.bpp = LAYER_BPP;
107     g_layerInfo.pixFormat = HDI_LAYER_PIXEL_FORMAT;
108     g_layerInfo.type = LAYER_TYPE_GRAPHIC;
109     if (g_display.layerFuncs->CreateLayer != nullptr) {
110         ret = g_display.layerFuncs->CreateLayer(g_display.devId, &g_layerInfo, &g_display.layerId);
111         if (ret != DISPLAY_SUCCESS) {
112             GRAPHIC_LOGE("CreateLayer fail");
113             return;
114         }
115     }
116 }
117 
FbdevFlush()118 void FbdevFlush()
119 {
120     if (g_display.layerFuncs->Flush != nullptr) {
121         int32_t ret = g_display.layerFuncs->Flush(g_display.devId, g_display.layerId, &g_display.buffer);
122         if (ret != DISPLAY_SUCCESS) {
123             GRAPHIC_LOGE("flush fail");
124             return;
125         }
126     }
127 }
128 
FbdevInit()129 void FbdevInit()
130 {
131     DisplayInit();
132     OpenLayer();
133     GetDevSurfaceData();
134 }
135 
FbdevClose()136 void FbdevClose()
137 {
138     if (g_display.layerFuncs->CloseLayer == nullptr) {
139         return;
140     }
141     if (g_display.layerFuncs->DeinitDisplay == nullptr) {
142         return;
143     }
144     int32_t ret = g_display.layerFuncs->CloseLayer(g_display.devId, g_display.layerId);
145     if (ret != DISPLAY_SUCCESS) {
146         GRAPHIC_LOGE("CloseLayer fail");
147         return;
148     }
149     ret = g_display.layerFuncs->DeinitDisplay(g_display.devId);
150     if (ret != DISPLAY_SUCCESS) {
151         GRAPHIC_LOGE("DeinitDisplay fail");
152         return;
153     }
154     ret = LayerUninitialize(g_display.layerFuncs);
155     if (ret != DISPLAY_SUCCESS) {
156         GRAPHIC_LOGE("LayerUninitialize fail");
157     }
158 }
159 } // namespace OHOS
160