1 /*
2 * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3 * This file contains confidential and proprietary information of
4 * OSWare Technology Co., Ltd
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21 #include "hdf_log.h"
22 #include "display_type.h"
23 #include "g2d.h"
24 #include "display_common.h"
25 #include "display_gfx.h"
26
27 #ifndef errno_t
28 typedef int errno_t;
29 #endif
30
31 void *handle = NULL;
32 #define ALIGN_UP(x, a) ((((x) + ((a)-1)) / (a)) * (a))
33 extern errno_t memset_s(void *dest, size_t destMax, int c, size_t count);
34
Imx8mmInitGfx(void)35 int32_t Imx8mmInitGfx(void)
36 {
37 g2d_open(&handle);
38 return DISPLAY_SUCCESS;
39 }
40
Imx8mmDeinitGfx(void)41 int32_t Imx8mmDeinitGfx(void)
42 {
43 g2d_close(handle);
44 return DISPLAY_SUCCESS;
45 }
46
colorSpaceModeChange(PixelFormat color)47 enum g2d_format colorSpaceModeChange(PixelFormat color)
48 {
49 enum g2d_format nxpFormat;
50 switch (color) {
51 case PIXEL_FMT_RGB_565: /**< RGB565 format */
52 nxpFormat = G2D_RGB565;
53 break;
54 case PIXEL_FMT_RGBX_8888: /**< RGBX8888 format */
55 nxpFormat = G2D_RGBX8888;
56 break;
57 case PIXEL_FMT_RGBA_8888: /**< RGBA8888 format */
58 nxpFormat = G2D_RGBA8888;
59 break;
60 case PIXEL_FMT_RGB_888: /**< RGB888 format */
61 nxpFormat = G2D_RGB888;
62 break;
63 case PIXEL_FMT_BGR_565: /**< BGR565 format */
64 nxpFormat = G2D_BGR565;
65 break;
66 case PIXEL_FMT_BGRX_8888: /**< BGRX8888 format */
67 nxpFormat = G2D_BGRX8888;
68 break;
69 case PIXEL_FMT_BGRA_8888: /**< BGRA8888 format */
70 nxpFormat = G2D_BGRA8888;
71 break;
72 default:
73 nxpFormat = G2D_FORMAT_UNKNOWN;
74 break;
75 }
76
77 return nxpFormat;
78 }
79
blendTypeChange(BlendType blendType)80 int32_t blendTypeChange(BlendType blendType)
81 {
82 int32_t nxpBlendType;
83 switch (blendType) {
84 case BLEND_SRC: /**< SRC blending */
85 nxpBlendType = G2D_SRC_ALPHA;
86 break;
87 case BLEND_DST: /**< DST blending */
88 nxpBlendType = G2D_DST_ALPHA;
89 break;
90 case BLEND_SRCOVER: /**< SRC_OVER blending */
91 nxpBlendType = G2D_ONE_MINUS_SRC_ALPHA;
92 break;
93 case BLEND_DSTOVER: /**< DST_OVER blending */
94 nxpBlendType = G2D_ONE_MINUS_DST_ALPHA;
95 break;
96 default:
97 nxpBlendType = -1;
98 break;
99 }
100
101 return nxpBlendType;
102 }
103
TransformTypeChange(TransformType type)104 int32_t TransformTypeChange(TransformType type)
105 {
106 int32_t nxpRotateType;
107 switch (type) {
108 case ROTATE_90: /**< Rotation by 90 degrees */
109 nxpRotateType = G2D_ROTATION_90;
110 break;
111 case ROTATE_180: /**< Rotation by 180 degrees */
112 nxpRotateType = G2D_ROTATION_180;
113 break;
114 case ROTATE_270: /**< Rotation by 270 degrees */
115 nxpRotateType = G2D_ROTATION_270;
116 break;
117 default:
118 nxpRotateType = G2D_ROTATION_0; /**< No rotation */
119 break;
120 }
121
122 return nxpRotateType;
123 }
124
Imx8mmFillRect(ISurface * surface,IRect * rect,uint32_t color,GfxOpt * opt)125 int32_t Imx8mmFillRect(ISurface *surface, IRect *rect, uint32_t color, GfxOpt *opt)
126 {
127 int err = 0;
128 CHECK_NULLPOINTER_RETURN_VALUE(surface, DISPLAY_NULL_PTR);
129 CHECK_NULLPOINTER_RETURN_VALUE(rect, DISPLAY_NULL_PTR);
130 CHECK_NULLPOINTER_RETURN_VALUE(opt, DISPLAY_NULL_PTR);
131
132 struct g2d_surface dst_surfase;
133
134 err = memset_s(&dst_surfase, sizeof(dst_surfase), 0x00, sizeof(dst_surfase));
135 if (err != 0) {
136 HDF_LOGE("%s: memset_s failed", __func__);
137 }
138
139 /*---------------dst_surfase-------------------*/
140 dst_surfase.format = colorSpaceModeChange(surface->enColorFmt);
141 dst_surfase.planes[0] = surface->phyAddr;
142 dst_surfase.left = rect->x;
143 dst_surfase.top = rect->y;
144 dst_surfase.right = (rect->x + rect->w);
145 dst_surfase.bottom = (rect->y + rect->h);
146 dst_surfase.stride = surface->stride;
147 dst_surfase.width = surface->width;
148 dst_surfase.height = surface->height;
149
150 if (opt->blendType) {
151 dst_surfase.blendfunc = blendTypeChange(opt->blendType);
152 }
153
154 if (opt->enGlobalAlpha) {
155 dst_surfase.global_alpha = opt->globalAlpha;
156 }
157 dst_surfase.clrcolor = color;
158 dst_surfase.rot = TransformTypeChange(opt->rotateType);
159
160 g2d_clear(handle, &dst_surfase);
161
162 return DISPLAY_SUCCESS;
163 }
164
Imx8mmBlit(ISurface * srcSurface,IRect * srcRect,ISurface * dstSurface,IRect * dstRect,GfxOpt * opt)165 int32_t Imx8mmBlit(ISurface *srcSurface, IRect *srcRect, ISurface *dstSurface, IRect *dstRect, GfxOpt *opt)
166 {
167 int err = 0;
168 struct g2d_surface src_surfase;
169 struct g2d_surface dst_surfase;
170
171 err = memset_s(&src_surfase, sizeof(src_surfase), 0x00, sizeof(src_surfase));
172 if (err != 0) {
173 HDF_LOGE("%s: memset_s failed", __func__);
174 }
175
176 err = memset_s(&dst_surfase, sizeof(src_surfase), 0x00, sizeof(dst_surfase));
177 if (err != 0) {
178 HDF_LOGE("%s: memset_s failed", __func__);
179 }
180
181 CHECK_NULLPOINTER_RETURN_VALUE(srcSurface, DISPLAY_NULL_PTR);
182 CHECK_NULLPOINTER_RETURN_VALUE(srcRect, DISPLAY_NULL_PTR);
183 CHECK_NULLPOINTER_RETURN_VALUE(dstSurface, DISPLAY_NULL_PTR);
184 CHECK_NULLPOINTER_RETURN_VALUE(dstRect, DISPLAY_NULL_PTR);
185 CHECK_NULLPOINTER_RETURN_VALUE(opt, DISPLAY_NULL_PTR);
186
187 /*---------------src_surfase-------------------*/
188 src_surfase.format = colorSpaceModeChange(srcSurface->enColorFmt);
189 src_surfase.planes[0] = srcSurface->phyAddr;
190 src_surfase.left = srcRect->x;
191 src_surfase.top = srcRect->y;
192 src_surfase.right = (srcRect->x + srcRect->w);
193 src_surfase.bottom = (srcRect->y + srcRect->h);
194 src_surfase.stride = ALIGN_UP(srcSurface->width, 16);
195 src_surfase.width = srcSurface->width;
196 src_surfase.height = srcSurface->height;
197 src_surfase.blendfunc = G2D_ONE;
198
199 if (opt->enGlobalAlpha) {
200 src_surfase.global_alpha = opt->globalAlpha;
201 }
202 src_surfase.rot = TransformTypeChange(opt->rotateType);
203
204 /*---------------dst_surfase-------------------*/
205 dst_surfase.format = colorSpaceModeChange(dstSurface->enColorFmt);
206 dst_surfase.planes[0] = dstSurface->phyAddr;
207 dst_surfase.left = dstRect->x;
208 dst_surfase.top = dstRect->y;
209 dst_surfase.right = (dstRect->x + dstRect->w);
210 dst_surfase.bottom = (dstRect->y + dstRect->h);
211 dst_surfase.stride = ALIGN_UP(dstSurface->width, 16);
212 dst_surfase.width = dstSurface->width;
213 dst_surfase.height = dstSurface->height;
214 if (opt->blendType) {
215 dst_surfase.blendfunc = blendTypeChange(opt->blendType);
216 }
217
218 g2d_enable(handle, 0);
219 g2d_blit(handle, &src_surfase, &dst_surfase);
220 g2d_finish(handle);
221 g2d_disable(handle, 0);
222
223 return DISPLAY_SUCCESS;
224 }
225
Imx8mmSync(int32_t timeOut)226 int32_t Imx8mmSync(int32_t timeOut)
227 {
228 (void)timeOut;
229
230 return DISPLAY_SUCCESS;
231 }
232
GfxInitialize(GfxFuncs ** funcs)233 int32_t GfxInitialize(GfxFuncs **funcs)
234 {
235 if (funcs == NULL) {
236 HDF_LOGE("%s: funcs is null", __func__);
237 return DISPLAY_NULL_PTR;
238 }
239
240 GfxFuncs *gfxFuncs = (GfxFuncs *)malloc(sizeof(GfxFuncs));
241 if (gfxFuncs == NULL) {
242 HDF_LOGE("%s: funcs is null", __func__);
243 return DISPLAY_NULL_PTR;
244 }
245
246 gfxFuncs->InitGfx = Imx8mmInitGfx;
247 gfxFuncs->DeinitGfx = Imx8mmDeinitGfx;
248 gfxFuncs->FillRect = Imx8mmFillRect;
249 gfxFuncs->Blit = Imx8mmBlit;
250 gfxFuncs->Sync = Imx8mmSync;
251 *funcs = gfxFuncs;
252 HDF_LOGI("%s: gfx initialize success", __func__);
253 return DISPLAY_SUCCESS;
254 }
255
GfxUninitialize(GfxFuncs * funcs)256 int32_t GfxUninitialize(GfxFuncs *funcs)
257 {
258 if (funcs == NULL) {
259 HDF_LOGE("%s: funcs is null", __func__);
260 return DISPLAY_NULL_PTR;
261 }
262 free(funcs);
263 HDF_LOGI("%s: gfx uninitialize success", __func__);
264 return DISPLAY_SUCCESS;
265 }