1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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
16 #include "display_gralloc_gbm.h"
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <inttypes.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <pthread.h>
25 #include <xf86drm.h>
26 #include <securec.h>
27 #include <sys/mman.h>
28 #include <linux/dma-buf.h>
29 #include "drm_fourcc.h"
30 #include "seed_gbm.h"
31 #include "hdf_dlist.h"
32 #include "display_gralloc_private.h"
33 #include "display_common.h"
34
35 #define ERRHANDLE(fd, bo, priBuffer) \
36 do { \
37 close(fd); \
38 hdi_gbm_bo_destroy(bo); \
39 if ((priBuffer) != NULL) { \
40 free((priBuffer)); \
41 } \
42 GRALLOC_UNLOCK(); \
43 return DISPLAY_FAILURE; \
44 } while (0)
45
46 const char *g_drmFileNode = "/dev/dri/card1";
47 static GrallocManager *g_grallocManager = NULL;
48 static pthread_mutex_t g_lock;
49 static uint64_t global_id = 0;
50
51 typedef struct {
52 uint32_t drmFormat;
53 PixelFormat pixFormat;
54 } PixelFormatConvertTbl;
55
56 typedef struct {
57 uint32_t value;
58 const char *str;
59 } ValueStrMap;
60
GetGrallocManager(void)61 static GrallocManager *GetGrallocManager(void)
62 {
63 if (g_grallocManager == NULL) {
64 g_grallocManager = (GrallocManager *)malloc(sizeof(GrallocManager));
65 errno_t eok = memset_s(g_grallocManager, sizeof(GrallocManager), 0, sizeof(GrallocManager));
66 if (eok != EOK) {
67 DISPLAY_LOGE("memset_s failed");
68 }
69 if (g_grallocManager == NULL) {
70 DISPLAY_LOGE("gralloc manager malloc failed");
71 }
72 }
73 return g_grallocManager;
74 }
75
GetPixelFmtStr(PixelFormat format)76 const char *GetPixelFmtStr(PixelFormat format)
77 {
78 static const ValueStrMap pixelStrMaps[] = {
79 {PIXEL_FMT_CLUT8, "PIXEL_FMT_CLUT8"}, {PIXEL_FMT_CLUT1, "PIXEL_FMT_CLUT1"},
80 {PIXEL_FMT_CLUT4, "PIXEL_FMT_CLUT4"}, {PIXEL_FMT_RGB_565, "PIXEL_FMT_RGB_565"},
81 {PIXEL_FMT_RGBA_5658, "IXEL_FMT_RGBA_5658"}, {PIXEL_FMT_RGBX_4444, "PIXEL_FMT_RGBX_4444"},
82 {PIXEL_FMT_RGBA_4444, "PIXEL_FMT_RGBA_4444"}, {PIXEL_FMT_RGB_444, "PIXEL_FMT_RGB_444"},
83 {PIXEL_FMT_RGBX_5551, "PIXEL_FMT_RGBX_5551"}, {PIXEL_FMT_RGBA_5551, "PIXEL_FMT_RGBA_5551"},
84 {PIXEL_FMT_RGB_555, "PIXEL_FMT_RGB_555"}, {PIXEL_FMT_RGBX_8888, "PIXEL_FMT_RGBX_8888"},
85 {PIXEL_FMT_RGBA_8888, "PIXEL_FMT_RGBA_8888"}, {PIXEL_FMT_RGB_888, "PIXEL_FMT_RGB_888"},
86 {PIXEL_FMT_BGR_565, "PIXEL_FMT_BGR_565"}, {PIXEL_FMT_BGRX_4444, "PIXEL_FMT_BGRX_4444"},
87 {PIXEL_FMT_BGRA_4444, "PIXEL_FMT_BGRA_4444"}, {PIXEL_FMT_BGRX_5551, "PIXEL_FMT_BGRX_5551"},
88 {PIXEL_FMT_BGRA_5551, "PIXEL_FMT_BGRA_5551"}, {PIXEL_FMT_BGRX_8888, "PIXEL_FMT_BGRX_8888"},
89 {PIXEL_FMT_BGRA_8888, "PIXEL_FMT_BGRA_8888"}, {PIXEL_FMT_YUV_422_I, "PIXEL_FMT_YUV_422_I"},
90 {PIXEL_FMT_YUV_422_I, "PIXEL_FMT_YUV_422_I"}, {PIXEL_FMT_YCBCR_422_SP, "PIXEL_FMT_YCBCR_422_SP"},
91 {PIXEL_FMT_YCRCB_422_SP, "PIXEL_FMT_YCRCB_422_SP"}, {PIXEL_FMT_YCBCR_420_SP, "PIXEL_FMT_YCBCR_420_SP"},
92 {PIXEL_FMT_YCRCB_420_SP, "PIXEL_FMT_YCRCB_420_SP"}, {PIXEL_FMT_YCBCR_422_P, "PIXEL_FMT_YCBCR_422_P"},
93 {PIXEL_FMT_YCRCB_422_P, "PIXEL_FMT_YCRCB_422_P"}, {PIXEL_FMT_YCBCR_420_P, "PIXEL_FMT_YCBCR_420_P"},
94 {PIXEL_FMT_YCRCB_420_P, "PIXEL_FMT_YCRCB_420_P"}, {PIXEL_FMT_YUYV_422_PKG, "PIXEL_FMT_YUYV_422_PKG"},
95 {PIXEL_FMT_UYVY_422_PKG, "PIXEL_FMT_UYVY_422_PKG"}, {PIXEL_FMT_YVYU_422_PKG, "PIXEL_FMT_YVYU_422_PKG"},
96 {PIXEL_FMT_VYUY_422_PKG, "PIXEL_FMT_VYUY_422_PKG"}, {PIXEL_FMT_BUTT, "PIXEL_FMT_BUTT"},
97 };
98 static const char *unknown = "unknown format";
99 for (uint32_t i = 0; i < sizeof(pixelStrMaps) / sizeof(pixelStrMaps[0]); i++) {
100 if (pixelStrMaps[i].value == format) {
101 return pixelStrMaps[i].str;
102 }
103 }
104 DISPLAY_LOGE("GetPixelFmtStr unknown format %{public}d", format);
105 return unknown;
106 }
107
GetDrmFmtStr(uint32_t format)108 const char *GetDrmFmtStr(uint32_t format)
109 {
110 static const ValueStrMap formatStrMaps[] = {
111 {DRM_FORMAT_C8, "DRM_FORMAT_C8" }, {DRM_FORMAT_R8, "DRM_FORMAT_R8" },
112 {DRM_FORMAT_R16, "DRM_FORMAT_R16"}, {DRM_FORMAT_RG88, "DRM_FORMAT_RG88"},
113 {DRM_FORMAT_GR88, "DRM_FORMAT_GR88"}, {DRM_FORMAT_RG1616, "DRM_FORMAT_RG1616"},
114 {DRM_FORMAT_GR1616, "DRM_FORMAT_GR1616"}, {DRM_FORMAT_RGB332, "DRM_FORMAT_RGB332"},
115 {DRM_FORMAT_BGR233, "DRM_FORMAT_BGR233"}, {DRM_FORMAT_XRGB4444, "DRM_FORMAT_XRGB4444"},
116 {DRM_FORMAT_XBGR4444, "DRM_FORMAT_XBGR4444"}, {DRM_FORMAT_RGBX4444, "DRM_FORMAT_RGBX4444"},
117 {DRM_FORMAT_BGRX4444, "DRM_FORMAT_BGRX4444"}, {DRM_FORMAT_ARGB4444, "DRM_FORMAT_ARGB4444"},
118 {DRM_FORMAT_ABGR4444, "DRM_FORMAT_ABGR4444"}, {DRM_FORMAT_RGBA4444, "DRM_FORMAT_RGBA4444"},
119 {DRM_FORMAT_BGRA4444, "DRM_FORMAT_BGRA4444"}, {DRM_FORMAT_XRGB1555, "DRM_FORMAT_XRGB1555"},
120 {DRM_FORMAT_XBGR1555, "DRM_FORMAT_XBGR1555"}, {DRM_FORMAT_RGBX5551, "DRM_FORMAT_RGBX5551"},
121 {DRM_FORMAT_BGRX5551, "DRM_FORMAT_BGRX5551"}, {DRM_FORMAT_ARGB1555, "DRM_FORMAT_ARGB1555"},
122 {DRM_FORMAT_ABGR1555, "DRM_FORMAT_ABGR1555"}, {DRM_FORMAT_RGBA5551, "DRM_FORMAT_RGBA5551"},
123 {DRM_FORMAT_BGRA5551, "DRM_FORMAT_BGRA5551"}, {DRM_FORMAT_RGB565, "DRM_FORMAT_RGB565"},
124 {DRM_FORMAT_BGR565, "DRM_FORMAT_BGR565"}, {DRM_FORMAT_RGB888, "DRM_FORMAT_RGB888"},
125 {DRM_FORMAT_BGR888, "DRM_FORMAT_BGR888"}, {DRM_FORMAT_XRGB8888, "DRM_FORMAT_XRGB8888"},
126 {DRM_FORMAT_XBGR8888, "DRM_FORMAT_XBGR8888"}, {DRM_FORMAT_RGBX8888, "DRM_FORMAT_RGBX8888"},
127 {DRM_FORMAT_BGRX8888, "DRM_FORMAT_BGRX8888"}, {DRM_FORMAT_ARGB8888, "DRM_FORMAT_ARGB8888"},
128 {DRM_FORMAT_ABGR8888, "DRM_FORMAT_ABGR8888"}, {DRM_FORMAT_RGBA8888, "DRM_FORMAT_RGBA8888"},
129 {DRM_FORMAT_BGRA8888, "DRM_FORMAT_BGRA8888"}, {DRM_FORMAT_XRGB2101010, "DRM_FORMAT_XRGB2101010"},
130 {DRM_FORMAT_BGRX1010102, "DRM_FORMAT_BGRX1010102"}, {DRM_FORMAT_ARGB2101010, "DRM_FORMAT_ARGB2101010"},
131 {DRM_FORMAT_ABGR2101010, "DRM_FORMAT_ABGR2101010"}, {DRM_FORMAT_RGBA1010102, "DRM_FORMAT_RGBA1010102"},
132 {DRM_FORMAT_YVYU, "DRM_FORMAT_YVYU"}, {DRM_FORMAT_UYVY, "DRM_FORMAT_UYVY"},
133 {DRM_FORMAT_VYUY, "DRM_FORMAT_VYUY"}, {DRM_FORMAT_AYUV, "DRM_FORMAT_AYUV"},
134 {DRM_FORMAT_NV12, "DRM_FORMAT_NV12"}, {DRM_FORMAT_NV21, "DRM_FORMAT_NV21"},
135 {DRM_FORMAT_NV16, "DRM_FORMAT_NV16"}, {DRM_FORMAT_NV61, "DRM_FORMAT_NV61"},
136 {DRM_FORMAT_NV24, "DRM_FORMAT_NV24"}, {DRM_FORMAT_NV42, "DRM_FORMAT_NV42"},
137 {DRM_FORMAT_YUV410, "DRM_FORMAT_YUV410"}, {DRM_FORMAT_YVU410, "DRM_FORMAT_YVU410"},
138 {DRM_FORMAT_YUV411, "DRM_FORMAT_YUV411"}, {DRM_FORMAT_YVU411, "DRM_FORMAT_YVU411"},
139 {DRM_FORMAT_YUV420, "DRM_FORMAT_YUV420"}, {DRM_FORMAT_YVU420, "DRM_FORMAT_YVU420"},
140 {DRM_FORMAT_YUV422, "DRM_FORMAT_YUV422"}, {DRM_FORMAT_YVU422, "DRM_FORMAT_YVU422"},
141 {DRM_FORMAT_YUV444, "DRM_FORMAT_YUV444"}, {DRM_FORMAT_YVU444, "DRM_FORMAT_YVU444"},
142 };
143
144 static const char *unknown = "unknown drm format";
145 for (uint32_t i = 0; i < sizeof(formatStrMaps) / sizeof(formatStrMaps[0]); i++) {
146 if (formatStrMaps[i].value == format) {
147 return formatStrMaps[i].str;
148 }
149 }
150 DISPLAY_LOGE("GetDrmFmtStr unknown format %{public}d", format);
151 return unknown;
152 }
153
ConvertFormatToDrm(PixelFormat fmtIn)154 static uint32_t ConvertFormatToDrm(PixelFormat fmtIn)
155 {
156 static const PixelFormatConvertTbl convertTable[] = {
157 {DRM_FORMAT_RGBX8888, PIXEL_FMT_RGBX_8888}, {DRM_FORMAT_RGBA8888, PIXEL_FMT_RGBA_8888},
158 {DRM_FORMAT_RGB888, PIXEL_FMT_RGB_888}, {DRM_FORMAT_RGB565, PIXEL_FMT_BGR_565},
159 {DRM_FORMAT_BGRX4444, PIXEL_FMT_BGRX_4444}, {DRM_FORMAT_BGRA4444, PIXEL_FMT_BGRA_4444},
160 {DRM_FORMAT_RGBA4444, PIXEL_FMT_RGBA_4444}, {DRM_FORMAT_RGBX4444, PIXEL_FMT_RGBX_4444},
161 {DRM_FORMAT_BGRX5551, PIXEL_FMT_BGRX_5551}, {DRM_FORMAT_BGRA5551, PIXEL_FMT_BGRA_5551},
162 {DRM_FORMAT_BGRX8888, PIXEL_FMT_BGRX_8888}, {DRM_FORMAT_BGRA8888, PIXEL_FMT_BGRA_8888},
163 {DRM_FORMAT_NV12, PIXEL_FMT_YCBCR_420_SP}, {DRM_FORMAT_NV21, PIXEL_FMT_YCRCB_420_SP},
164 {DRM_FORMAT_YUV420, PIXEL_FMT_YCBCR_420_P}, {DRM_FORMAT_YVU420, PIXEL_FMT_YCRCB_420_P},
165 {DRM_FORMAT_NV16, PIXEL_FMT_YCBCR_422_SP}, {DRM_FORMAT_NV61, PIXEL_FMT_YCRCB_422_SP},
166 {DRM_FORMAT_YUV422, PIXEL_FMT_YCBCR_422_P}, {DRM_FORMAT_YVU422, PIXEL_FMT_YCRCB_422_P},
167 };
168 uint32_t fmtOut = 0;
169 for (uint32_t i = 0; i < sizeof(convertTable) / sizeof(convertTable[0]); i++) {
170 if (convertTable[i].pixFormat == fmtIn) {
171 fmtOut = convertTable[i].drmFormat;
172 }
173 }
174 DISPLAY_LOGD("fmtIn %{public}d : %{public}s, outFmt %{public}d : %{public}s", fmtIn, GetPixelFmtStr(fmtIn), fmtOut,
175 GetDrmFmtStr(fmtOut));
176 return fmtOut;
177 }
178
ConvertUsageToGbm(uint64_t inUsage)179 static uint64_t ConvertUsageToGbm(uint64_t inUsage)
180 {
181 uint64_t outUsage = GBM_BO_USE_TEXTURING;
182 if (inUsage & HBM_USE_CPU_READ) {
183 outUsage |= GBM_BO_USE_SW_READ_OFTEN;
184 }
185 if (inUsage & HBM_USE_CPU_WRITE) {
186 outUsage |= GBM_BO_USE_SW_WRITE_OFTEN;
187 }
188 DISPLAY_LOGD("outUsage 0x%{public}" PRIx64 "", outUsage);
189 return outUsage;
190 }
191
InitGbmDevice(const char * drmFile,GrallocManager * grallocManager)192 static int32_t InitGbmDevice(const char *drmFile, GrallocManager *grallocManager)
193 {
194 DISPLAY_LOGD();
195 if (grallocManager->gbmDevice == NULL) {
196 char path[PATH_MAX] = {0};
197 if (realpath(drmFile, path) == NULL) {
198 DISPLAY_LOGE(" drm File : %{public}s is not a realpath, errno: %{public}s", drmFile, strerror(errno));
199 return DISPLAY_PARAM_ERR;
200 }
201 int drmFd = open(path, O_RDWR);
202 if (drmFd < 0) {
203 DISPLAY_LOGE("drm file:%{public}s open failed %{public}s", drmFile, strerror(errno));
204 return DISPLAY_FD_ERR;
205 }
206 drmDropMaster(drmFd);
207 struct gbm_device *gbmDevice = hdi_gbm_create_device(drmFd);
208 grallocManager->drmFd = drmFd;
209 if (gbmDevice == NULL) {
210 close(drmFd);
211 grallocManager->drmFd = -1;
212 DISPLAY_LOGE("gbm device create failed");
213 return DISPLAY_FAILURE;
214 }
215 grallocManager->gbmDevice = gbmDevice;
216 grallocManager->drmFd = drmFd;
217 DListHeadInit(&grallocManager->gbmBoHead);
218 }
219 return DISPLAY_SUCCESS;
220 }
221
DeInitGbmDevice(GrallocManager * grallocManager)222 static void DeInitGbmDevice(GrallocManager *grallocManager)
223 {
224 DISPLAY_LOGD();
225 hdi_gbm_device_destroy(grallocManager->gbmDevice);
226 if (grallocManager->drmFd > 0) {
227 close(grallocManager->drmFd);
228 grallocManager->drmFd = -1;
229 }
230 grallocManager->gbmDevice = NULL;
231 }
232
DmaBufferSync(const BufferHandle * handle,bool start)233 static int32_t DmaBufferSync(const BufferHandle *handle, bool start)
234 {
235 DISPLAY_LOGD();
236 struct dma_buf_sync syncPrm;
237 errno_t eok = memset_s(&syncPrm, sizeof(syncPrm), 0, sizeof(syncPrm));
238 DISPLAY_CHK_RETURN((eok != EOK), DISPLAY_PARAM_ERR, DISPLAY_LOGE("dma buffer sync memset_s failed"));
239
240 if (handle->usage & HBM_USE_CPU_WRITE) {
241 syncPrm.flags |= DMA_BUF_SYNC_WRITE;
242 }
243
244 if (handle->usage & HBM_USE_CPU_READ) {
245 syncPrm.flags |= DMA_BUF_SYNC_READ;
246 }
247
248 if (start) {
249 syncPrm.flags |= DMA_BUF_SYNC_START;
250 } else {
251 syncPrm.flags |= DMA_BUF_SYNC_END;
252 }
253 int retry = 6;
254 int ret;
255 do {
256 ret = ioctl(handle->fd, DMA_BUF_IOCTL_SYNC, &syncPrm);
257 } while ((retry--) && (ret != -EAGAIN) && (ret != -EINTR));
258
259 if (ret < 0) {
260 DISPLAY_LOGE("DISPLAY_SUCCESS sync failed");
261 return DISPLAY_SUCCESS;
262 }
263 return DISPLAY_SUCCESS;
264 }
265
InitBufferHandle(struct gbm_bo * bo,int fd,const AllocInfo * info,PriBufferHandle * buffer)266 static void InitBufferHandle(struct gbm_bo *bo, int fd, const AllocInfo *info, PriBufferHandle *buffer)
267 {
268 BufferHandle *bufferHandle = &(buffer->hdl);
269 bufferHandle->fd = fd;
270 bufferHandle->reserveFds = GRALLOC_PRIV_NUM_FDS;
271 bufferHandle->reserveInts = GRALLOC_PRIV_NUM_INTS;
272 bufferHandle->stride = hdi_gbm_bo_get_stride(bo);
273 bufferHandle->width = info->width;
274 bufferHandle->height = info->height;
275 bufferHandle->usage = info->usage;
276 bufferHandle->format = info->format;
277 bufferHandle->virAddr = NULL;
278 bufferHandle->size = hdi_gbm_bo_get_stride(bo) * hdi_gbm_bo_get_height(bo);
279 switch (info->format) {
280 case PIXEL_FMT_YCBCR_420_SP:
281 case PIXEL_FMT_YCRCB_420_SP:
282 buffer->plan_num = 2; // plan_num is 2
283 buffer->yuv_info = AW_YUV_NO_INFO;
284 buffer->plane_info[0].alloc_height = hdi_gbm_bo_get_height(bo);
285 buffer->plane_info[0].alloc_width = hdi_gbm_bo_get_width(bo);
286 buffer->plane_info[0].byte_stride = hdi_gbm_bo_get_width(bo);
287 buffer->plane_info[0].offset = 0;
288 buffer->plane_info[1].alloc_height = buffer->plane_info[0].alloc_height/2; // alloc_height/2
289 buffer->plane_info[1].alloc_width = buffer->plane_info[0].alloc_width/2; // alloc_height/2
290 buffer->plane_info[1].byte_stride = buffer->plane_info[0].byte_stride;
291 buffer->plane_info[1].offset = buffer->plane_info[0].byte_stride * buffer->plane_info[0].alloc_height;
292 break;
293
294 case PIXEL_FMT_YCBCR_422_SP:
295 case PIXEL_FMT_YCRCB_422_SP:
296 break;
297
298 case PIXEL_FMT_YCBCR_420_P:
299 case PIXEL_FMT_YCRCB_420_P:
300 buffer->plan_num = 3; // plan_num is 3
301 buffer->yuv_info = AW_YUV_NO_INFO;
302 buffer->plane_info[0].alloc_height = hdi_gbm_bo_get_height(bo);
303 buffer->plane_info[0].alloc_width = hdi_gbm_bo_get_width(bo);
304 buffer->plane_info[0].byte_stride = hdi_gbm_bo_get_width(bo);
305 buffer->plane_info[0].offset = 0;
306 buffer->plane_info[1].alloc_height = buffer->plane_info[0].alloc_height/2; // alloc_height/2
307 buffer->plane_info[1].alloc_width = buffer->plane_info[0].alloc_width/2; // alloc_width/2
308 buffer->plane_info[1].byte_stride = buffer->plane_info[0].byte_stride/2; // byte_stride/2
309 buffer->plane_info[1].offset = buffer->plane_info[0].byte_stride * buffer->plane_info[0].alloc_height;
310 buffer->plane_info[2].alloc_height = buffer->plane_info[0].alloc_height/2; // alloc_height/2
311 buffer->plane_info[2].alloc_width = buffer->plane_info[0].alloc_width/2; // alloc_width/2
312 buffer->plane_info[2].byte_stride = buffer->plane_info[0].byte_stride/2; // byte_stride/2
313 buffer->plane_info[2].offset = buffer->plane_info[1].offset
314 + buffer->plane_info[1].byte_stride * buffer->plane_info[1].alloc_height; // plane_info[1].alloc_height
315 break;
316
317 case PIXEL_FMT_YCBCR_422_P:
318 case PIXEL_FMT_YCRCB_422_P:
319 break;
320
321 default:
322 buffer->plan_num = 1;
323 buffer->yuv_info = AW_YUV_NO_INFO;
324 buffer->plane_info[0].alloc_height = hdi_gbm_bo_get_height(bo);
325 buffer->plane_info[0].alloc_width = hdi_gbm_bo_get_width(bo);
326 buffer->plane_info[0].byte_stride = hdi_gbm_bo_get_stride(bo);
327 buffer->plane_info[0].offset = 0;
328 }
329 }
330
GbmAllocMem(const AllocInfo * info,BufferHandle ** buffer)331 int32_t GbmAllocMem(const AllocInfo *info, BufferHandle **buffer)
332 {
333 DISPLAY_CHK_RETURN((info == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("info is null"));
334 DISPLAY_CHK_RETURN((buffer == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("buffer is null"));
335 PriBufferHandle *priBuffer = NULL;
336 uint32_t drmFmt = ConvertFormatToDrm(info->format);
337 DISPLAY_CHK_RETURN((drmFmt == INVALID_PIXEL_FMT), DISPLAY_NOT_SUPPORT,
338 DISPLAY_LOGE("format %{public}d can not support", info->format));
339
340 GRALLOC_LOCK();
341 GrallocManager *grallocManager = GetGrallocManager();
342 DISPLAY_CHK_RETURN((grallocManager == NULL), DISPLAY_PARAM_ERR, DISPLAY_LOGE("gralloc manager failed");
343 GRALLOC_UNLOCK());
344 struct gbm_bo *bo =
345 hdi_gbm_bo_create(grallocManager->gbmDevice, info->width, info->height, drmFmt, ConvertUsageToGbm(info->usage));
346 DISPLAY_CHK_RETURN((bo == NULL), DISPLAY_NOMEM, DISPLAY_LOGE("gbm create bo failed");
347 GRALLOC_UNLOCK());
348
349 int fd = hdi_gbm_bo_get_fd(bo);
350 DISPLAY_CHK_RETURN((fd < 0), DISPLAY_FD_ERR, DISPLAY_LOGE("gbm can not get fd");
351 hdi_gbm_bo_destroy(bo);
352 GRALLOC_UNLOCK());
353
354 priBuffer = (PriBufferHandle *)malloc(sizeof(PriBufferHandle));
355 DISPLAY_CHK_RETURN((priBuffer == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("bufferhandle malloc failed");
356 ERRHANDLE(fd, bo, priBuffer));
357 errno_t eok = memset_s(priBuffer, sizeof(PriBufferHandle), 0, sizeof(PriBufferHandle));
358 DISPLAY_CHK_RETURN((eok != EOK), DISPLAY_PARAM_ERR, DISPLAY_LOGE("memset_s failed");
359 ERRHANDLE(fd, bo, priBuffer));
360
361 InitBufferHandle(bo, fd, info, priBuffer);
362 priBuffer->id = global_id++;
363 DISPLAY_LOGD(
364 "alloc mem width %{public}d, heigt %{public}d, drmformat %{public}d, format %{public}d buffer id:%{public}llu",
365 info->width, info->height, drmFmt, info->format, priBuffer->id);
366
367 priBuffer->hdl.phyAddr = 0;
368 *buffer = &priBuffer->hdl;
369 hdi_gbm_bo_destroy(bo);
370 GRALLOC_UNLOCK();
371 return DISPLAY_SUCCESS;
372 }
373
CloseBufferHandle(BufferHandle * handle)374 static void CloseBufferHandle(BufferHandle *handle)
375 {
376 DISPLAY_CHK_RETURN_NOT_VALUE((handle == NULL), DISPLAY_LOGE("buffer is null"));
377 if (handle->fd >= 0) {
378 close(handle->fd);
379 handle->fd = -1;
380 }
381 const uint32_t reserveFds = handle->reserveFds;
382 for (uint32_t i = 0; i < reserveFds; i++) {
383 if (handle->reserve[i] >= 0) {
384 close(handle->reserve[i]);
385 handle->reserve[i] = -1;
386 }
387 }
388 }
389
GbmFreeMem(BufferHandle * buffer)390 void GbmFreeMem(BufferHandle *buffer)
391 {
392 DISPLAY_LOGD();
393 DISPLAY_CHK_RETURN_NOT_VALUE((buffer == NULL), DISPLAY_LOGE("buffer is null"));
394 if ((buffer->virAddr != NULL) && (GbmUnmap(buffer) != DISPLAY_SUCCESS)) {
395 DISPLAY_LOGE("freeMem unmap buffer failed");
396 }
397 CloseBufferHandle(buffer);
398 free(buffer);
399 }
400
GbmMmap(BufferHandle * buffer)401 void *GbmMmap(BufferHandle *buffer)
402 {
403 void *virAddr = NULL;
404 DISPLAY_LOGD();
405 if (buffer == NULL) {
406 DISPLAY_LOGE("gbmmap the buffer handle is NULL");
407 return NULL;
408 }
409 if (buffer->virAddr != NULL) {
410 DISPLAY_LOGD("the buffer has virtual addr");
411 return buffer->virAddr;
412 }
413 virAddr = mmap(NULL, buffer->size, PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0);
414 if (virAddr == MAP_FAILED) {
415 DISPLAY_LOGE("mmap failed errno %{public}s, fd : %{public}d", strerror(errno), buffer->fd);
416 }
417 buffer->virAddr = virAddr;
418 return virAddr;
419 }
420
GbmUnmap(BufferHandle * buffer)421 int32_t GbmUnmap(BufferHandle *buffer)
422 {
423 DISPLAY_LOGD();
424 if (buffer == NULL) {
425 DISPLAY_LOGE("gbmumap the buffer handle is null");
426 return DISPLAY_NULL_PTR;
427 }
428
429 if (buffer->virAddr == NULL) {
430 DISPLAY_LOGE("virAddr is NULL , has not map the buffer");
431 return DISPLAY_PARAM_ERR;
432 }
433 int ret = munmap(buffer->virAddr, buffer->size);
434 if (ret != 0) {
435 DISPLAY_LOGE("munmap failed err: %{public}s", strerror(errno));
436 return DISPLAY_FAILURE;
437 }
438 buffer->virAddr = NULL;
439 return DISPLAY_SUCCESS;
440 }
441
GbmInvalidateCache(BufferHandle * buffer)442 int32_t GbmInvalidateCache(BufferHandle *buffer)
443 {
444 DISPLAY_LOGD();
445 return DmaBufferSync(buffer, true);
446 }
447
GbmFlushCache(BufferHandle * buffer)448 int32_t GbmFlushCache(BufferHandle *buffer)
449 {
450 DISPLAY_LOGD();
451 return DmaBufferSync(buffer, false);
452 }
453
GbmGrallocUninitialize(void)454 int32_t GbmGrallocUninitialize(void)
455 {
456 DISPLAY_LOGD();
457 GRALLOC_LOCK();
458 GrallocManager *grallocManager = GetGrallocManager();
459 DISPLAY_CHK_RETURN((grallocManager == NULL), DISPLAY_PARAM_ERR, DISPLAY_LOGE("gralloc manager failed");
460 GRALLOC_UNLOCK());
461 grallocManager->referCount--;
462 if (grallocManager->referCount < 0) {
463 DeInitGbmDevice(grallocManager);
464 free(g_grallocManager);
465 g_grallocManager = NULL;
466 }
467 GRALLOC_UNLOCK();
468 return DISPLAY_SUCCESS;
469 }
470
GbmGrallocInitialize(void)471 int32_t GbmGrallocInitialize(void)
472 {
473 DISPLAY_LOGD();
474 GRALLOC_LOCK();
475 GrallocManager *grallocManager = GetGrallocManager();
476 DISPLAY_CHK_RETURN((grallocManager == NULL), DISPLAY_PARAM_ERR, DISPLAY_LOGE("gralloc manager failed");
477 GRALLOC_UNLOCK());
478 int ret = InitGbmDevice(g_drmFileNode, grallocManager);
479 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), ret, DISPLAY_LOGE("gralloc manager failed");
480 GRALLOC_UNLOCK());
481 grallocManager->referCount++;
482 int32_t pid = getpid();
483 global_id = ((uint64_t)(pid))<<32; // (pid))<<32
484 GRALLOC_UNLOCK();
485 return DISPLAY_SUCCESS;
486 }
487