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 <string.h>
16 #include "display_layer.h"
17 #include "disp_hal.h"
18 #include "display_type.h"
19 #include "hdf_log.h"
20
21 #define DEV_ID 0
22 #define LAYER_ID 0
23 #define DISP_WIDTH 720
24 #define DISP_HEIGHT 480
25 #define BITS_PER_PIXEL 32
26 #define BITS_TO_BYTE 8
27
28 struct LayerPrivate {
29 uint32_t width;
30 uint32_t height;
31 int32_t pitch;
32 PixelFormat pixFmt;
33 void *fbAddr;
34 uint32_t fbSize;
35 };
36
GetLayerInstance(void)37 static struct LayerPrivate *GetLayerInstance(void)
38 {
39 // default layer config, rewritten by OpenLayer()
40 static struct LayerPrivate layerPriv = {
41 .width = DISP_WIDTH,
42 .height = DISP_HEIGHT,
43 .pixFmt = PIXEL_FMT_RGBA_8888,
44 };
45 return &layerPriv;
46 }
47
InitDisplay(uint32_t devId)48 static int32_t InitDisplay(uint32_t devId)
49 {
50 if (devId != DEV_ID) {
51 HDF_LOGE("devId invalid");
52 return DISPLAY_FAILURE;
53 }
54 if (DispInit(devId) != DISPLAY_SUCCESS) {
55 HDF_LOGE("DispInit failed");
56 return DISPLAY_FAILURE;
57 }
58 return DISPLAY_SUCCESS;
59 }
60
DeinitDisplay(uint32_t devId)61 static int32_t DeinitDisplay(uint32_t devId)
62 {
63 if (devId != DEV_ID) {
64 HDF_LOGE("devId invalid");
65 return DISPLAY_FAILURE;
66 }
67 DispOff(devId);
68 return DISPLAY_SUCCESS;
69 }
70
OpenLayer(uint32_t devId,const LayerInfo * layerInfo,uint32_t * layerId)71 static int32_t OpenLayer(uint32_t devId, const LayerInfo *layerInfo, uint32_t *layerId)
72 {
73 if (layerInfo == NULL || layerId == NULL) {
74 HDF_LOGE("pointer is null");
75 return DISPLAY_NULL_PTR;
76 }
77 if (devId != DEV_ID) {
78 HDF_LOGE("devId invalid");
79 return DISPLAY_FAILURE;
80 }
81 struct LayerPrivate *priv = GetLayerInstance();
82 priv->width = layerInfo->width;
83 priv->height = layerInfo->height;
84 priv->pixFmt = layerInfo->pixFormat;
85 priv->pitch = layerInfo->width * layerInfo->bpp / BITS_TO_BYTE;
86 priv->fbSize = ((priv->pitch * priv->height) + 0xfff) & (~0xfff);
87
88 if (DispOn(devId) != DISPLAY_SUCCESS) {
89 HDF_LOGE("DispOn failed");
90 return DISPLAY_FAILURE;
91 }
92 *layerId = LAYER_ID;
93 return DISPLAY_SUCCESS;
94 }
95
CloseLayer(uint32_t devId,uint32_t layerId)96 static int32_t CloseLayer(uint32_t devId, uint32_t layerId)
97 {
98 if (devId != DEV_ID) {
99 HDF_LOGE("devId invalid");
100 return DISPLAY_FAILURE;
101 }
102 if (layerId != LAYER_ID) {
103 HDF_LOGE("layerId invalid");
104 return DISPLAY_FAILURE;
105 }
106 return DISPLAY_SUCCESS;
107 }
108
GetDisplayInfo(uint32_t devId,DisplayInfo * dispInfo)109 static int32_t GetDisplayInfo(uint32_t devId, DisplayInfo *dispInfo)
110 {
111 if (dispInfo == NULL) {
112 HDF_LOGE("dispInfo is null");
113 return DISPLAY_NULL_PTR;
114 }
115 if (devId != DEV_ID) {
116 HDF_LOGE("devId invalid");
117 return DISPLAY_FAILURE;
118 }
119 struct DispInfo info = {0};
120 if (DispGetInfo(devId, &info) != DISPLAY_SUCCESS) {
121 HDF_LOGE("DispGetInfo failed");
122 return DISPLAY_FAILURE;
123 }
124 dispInfo->width = info.width;
125 dispInfo->height = info.height;
126 dispInfo->rotAngle = ROTATE_NONE;
127 return DISPLAY_SUCCESS;
128 }
129
Flush(uint32_t devId,uint32_t layerId,LayerBuffer * buffer)130 static int32_t Flush(uint32_t devId, uint32_t layerId, LayerBuffer *buffer)
131 {
132 if (devId != DEV_ID) {
133 HDF_LOGE("devId invalid");
134 return DISPLAY_FAILURE;
135 }
136 if (layerId != LAYER_ID) {
137 HDF_LOGE("layerId invalid");
138 return DISPLAY_FAILURE;
139 }
140 if (buffer == NULL) {
141 HDF_LOGE("buffer is null");
142 return DISPLAY_FAILURE;
143 }
144 DispFlush();
145 return DISPLAY_SUCCESS;
146 }
147
GetLayerBuffer(uint32_t devId,uint32_t layerId,LayerBuffer * buffer)148 static int32_t GetLayerBuffer(uint32_t devId, uint32_t layerId, LayerBuffer *buffer)
149 {
150 if (buffer == NULL) {
151 HDF_LOGE("buffer is null");
152 return DISPLAY_NULL_PTR;
153 }
154 if (devId != DEV_ID) {
155 HDF_LOGE("devId invalid");
156 return DISPLAY_FAILURE;
157 }
158 if (layerId != LAYER_ID) {
159 HDF_LOGE("layerId invalid");
160 return DISPLAY_FAILURE;
161 }
162 struct LayerPrivate *priv = GetLayerInstance();
163 buffer->fenceId = 0;
164 buffer->width = priv->width;
165 buffer->height = priv->height;
166 buffer->pixFormat = priv->pixFmt;
167 buffer->pitch = priv->pitch;
168 // fbAddr may change
169 priv->fbAddr = (void *)DispMmap(priv->fbSize);
170 if (priv->fbAddr == NULL) {
171 HDF_LOGE("mmap fb address failed, size 0x%x", priv->fbSize);
172 priv->pitch = 0;
173 priv->fbSize = 0;
174 return DISPLAY_FAILURE;
175 }
176 buffer->data.phyAddr = (uint32_t)priv->fbAddr;
177 buffer->data.virAddr = priv->fbAddr;
178 if (buffer->data.virAddr == NULL) {
179 HDF_LOGE("virAddr null");
180 return DISPLAY_FAILURE;
181 }
182 return DISPLAY_SUCCESS;
183 }
184
LayerInitialize(LayerFuncs ** funcs)185 int32_t LayerInitialize(LayerFuncs **funcs)
186 {
187 if (funcs == NULL) {
188 HDF_LOGE("funcs is null");
189 return DISPLAY_NULL_PTR;
190 }
191 LayerFuncs *lFuncs = (LayerFuncs *)malloc(sizeof(LayerFuncs));
192 if (lFuncs == NULL) {
193 HDF_LOGE("lFuncs is null");
194 return DISPLAY_NULL_PTR;
195 }
196 (void)memset(lFuncs, 0, sizeof(LayerFuncs));
197 lFuncs->InitDisplay = InitDisplay;
198 lFuncs->DeinitDisplay = DeinitDisplay;
199 lFuncs->GetDisplayInfo = GetDisplayInfo;
200 lFuncs->CreateLayer = OpenLayer;
201 lFuncs->CloseLayer = CloseLayer;
202 lFuncs->Flush = Flush;
203 lFuncs->GetLayerBuffer = GetLayerBuffer;
204 *funcs = lFuncs;
205 return DISPLAY_SUCCESS;
206 }
207
LayerUninitialize(LayerFuncs * funcs)208 int32_t LayerUninitialize(LayerFuncs *funcs)
209 {
210 if (funcs == NULL) {
211 HDF_LOGE("funcs is null");
212 return DISPLAY_NULL_PTR;
213 }
214 free(funcs);
215 return DISPLAY_SUCCESS;
216 }
217