1 /**************************************************************************
5 Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
33 #include "tbm_bufmgr.h"
34 #include "tbm_bufmgr_int.h"
35 #include "tbm_surface_internal.h"
38 static tbm_bufmgr g_surface_bufmgr = NULL;
39 struct list_head g_surface_list; /* list of surfaces belonging to bufmgr */
41 static int _tbm_surface_internal_get_num_planes (tbm_format format)
48 case TBM_FORMAT_RGB332:
49 case TBM_FORMAT_BGR233:
50 case TBM_FORMAT_XRGB4444:
51 case TBM_FORMAT_XBGR4444:
52 case TBM_FORMAT_RGBX4444:
53 case TBM_FORMAT_BGRX4444:
54 case TBM_FORMAT_ARGB4444:
55 case TBM_FORMAT_ABGR4444:
56 case TBM_FORMAT_RGBA4444:
57 case TBM_FORMAT_BGRA4444:
58 case TBM_FORMAT_XRGB1555:
59 case TBM_FORMAT_XBGR1555:
60 case TBM_FORMAT_RGBX5551:
61 case TBM_FORMAT_BGRX5551:
62 case TBM_FORMAT_ARGB1555:
63 case TBM_FORMAT_ABGR1555:
64 case TBM_FORMAT_RGBA5551:
65 case TBM_FORMAT_BGRA5551:
66 case TBM_FORMAT_RGB565:
67 case TBM_FORMAT_BGR565:
68 case TBM_FORMAT_RGB888:
69 case TBM_FORMAT_BGR888:
70 case TBM_FORMAT_XRGB8888:
71 case TBM_FORMAT_XBGR8888:
72 case TBM_FORMAT_RGBX8888:
73 case TBM_FORMAT_BGRX8888:
74 case TBM_FORMAT_ARGB8888:
75 case TBM_FORMAT_ABGR8888:
76 case TBM_FORMAT_RGBA8888:
77 case TBM_FORMAT_BGRA8888:
78 case TBM_FORMAT_XRGB2101010:
79 case TBM_FORMAT_XBGR2101010:
80 case TBM_FORMAT_RGBX1010102:
81 case TBM_FORMAT_BGRX1010102:
82 case TBM_FORMAT_ARGB2101010:
83 case TBM_FORMAT_ABGR2101010:
84 case TBM_FORMAT_RGBA1010102:
85 case TBM_FORMAT_BGRA1010102:
99 case TBM_FORMAT_YUV410:
100 case TBM_FORMAT_YVU410:
101 case TBM_FORMAT_YUV411:
102 case TBM_FORMAT_YVU411:
103 case TBM_FORMAT_YUV420:
104 case TBM_FORMAT_YVU420:
105 case TBM_FORMAT_YUV422:
106 case TBM_FORMAT_YVU422:
107 case TBM_FORMAT_YUV444:
108 case TBM_FORMAT_YVU444:
120 static int _tbm_surface_internal_get_bpp (tbm_format format)
127 case TBM_FORMAT_RGB332:
128 case TBM_FORMAT_BGR233:
131 case TBM_FORMAT_XRGB4444:
132 case TBM_FORMAT_XBGR4444:
133 case TBM_FORMAT_RGBX4444:
134 case TBM_FORMAT_BGRX4444:
135 case TBM_FORMAT_ARGB4444:
136 case TBM_FORMAT_ABGR4444:
137 case TBM_FORMAT_RGBA4444:
138 case TBM_FORMAT_BGRA4444:
139 case TBM_FORMAT_XRGB1555:
140 case TBM_FORMAT_XBGR1555:
141 case TBM_FORMAT_RGBX5551:
142 case TBM_FORMAT_BGRX5551:
143 case TBM_FORMAT_ARGB1555:
144 case TBM_FORMAT_ABGR1555:
145 case TBM_FORMAT_RGBA5551:
146 case TBM_FORMAT_BGRA5551:
147 case TBM_FORMAT_RGB565:
148 case TBM_FORMAT_BGR565:
151 case TBM_FORMAT_RGB888:
152 case TBM_FORMAT_BGR888:
155 case TBM_FORMAT_XRGB8888:
156 case TBM_FORMAT_XBGR8888:
157 case TBM_FORMAT_RGBX8888:
158 case TBM_FORMAT_BGRX8888:
159 case TBM_FORMAT_ARGB8888:
160 case TBM_FORMAT_ABGR8888:
161 case TBM_FORMAT_RGBA8888:
162 case TBM_FORMAT_BGRA8888:
163 case TBM_FORMAT_XRGB2101010:
164 case TBM_FORMAT_XBGR2101010:
165 case TBM_FORMAT_RGBX1010102:
166 case TBM_FORMAT_BGRX1010102:
167 case TBM_FORMAT_ARGB2101010:
168 case TBM_FORMAT_ABGR2101010:
169 case TBM_FORMAT_RGBA1010102:
170 case TBM_FORMAT_BGRA1010102:
171 case TBM_FORMAT_YUYV:
172 case TBM_FORMAT_YVYU:
173 case TBM_FORMAT_UYVY:
174 case TBM_FORMAT_VYUY:
175 case TBM_FORMAT_AYUV:
178 case TBM_FORMAT_NV12:
179 case TBM_FORMAT_NV21:
182 case TBM_FORMAT_NV16:
183 case TBM_FORMAT_NV61:
186 case TBM_FORMAT_YUV410:
187 case TBM_FORMAT_YVU410:
190 case TBM_FORMAT_YUV411:
191 case TBM_FORMAT_YVU411:
192 case TBM_FORMAT_YUV420:
193 case TBM_FORMAT_YVU420:
196 case TBM_FORMAT_YUV422:
197 case TBM_FORMAT_YVU422:
200 case TBM_FORMAT_YUV444:
201 case TBM_FORMAT_YVU444:
212 _init_surface_bufmgr()
214 g_surface_bufmgr = tbm_bufmgr_init (-1);
218 _deinit_surface_bufmgr()
220 if (!g_surface_bufmgr)
223 tbm_bufmgr_deinit (g_surface_bufmgr);
224 g_surface_bufmgr = NULL;
229 tbm_surface_internal_query_supported_formats (uint32_t **formats, uint32_t *num)
231 if (!g_surface_bufmgr)
233 _init_surface_bufmgr();
234 LIST_INITHEAD (&g_surface_list);
237 struct _tbm_bufmgr *mgr = g_surface_bufmgr;
240 pthread_mutex_lock (&mgr->lock);
242 ret = mgr->backend->surface_supported_format (formats, num);
244 pthread_mutex_unlock (&mgr->lock);
250 tbm_surface_internal_create_with_flags (int width, int height, int format, int flags)
252 TBM_RETURN_VAL_IF_FAIL (width > 0, NULL);
253 TBM_RETURN_VAL_IF_FAIL (height > 0, NULL);
255 if (!g_surface_bufmgr)
257 _init_surface_bufmgr();
258 LIST_INITHEAD (&g_surface_list);
261 struct _tbm_bufmgr *mgr = g_surface_bufmgr;
264 TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(mgr), NULL);
266 struct _tbm_surface *surf = NULL;
272 surf = calloc (1, sizeof(struct _tbm_surface));
277 surf->info.width = width;
278 surf->info.height = height;
279 surf->info.format = format;
280 surf->info.bpp = _tbm_surface_internal_get_bpp (format);
281 surf->info.size = tbm_surface_internal_get_size (surf);
282 surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
284 /* get size, stride and offset */
285 for (i = 0; i < surf->info.num_planes; i++)
287 tbm_surface_internal_get_plane_data (surf, i, &size, &offset, &stride);
288 surf->info.planes[i].size = size;
289 surf->info.planes[i].offset = offset;
290 surf->info.planes[i].stride = stride;
295 /* create only one bo */
297 surf->bos[0] = tbm_bo_alloc (mgr, surf->info.size, flags);
303 if(LIST_IS_EMPTY (&g_surface_list))
305 _deinit_surface_bufmgr ();
306 LIST_DELINIT (&g_surface_list);
310 LIST_ADD (&surf->item_link, &g_surface_list);
316 tbm_surface_internal_create_with_bos (int width, int height, int format, tbm_bo *bos, int num)
318 TBM_RETURN_VAL_IF_FAIL (width > 0, NULL);
319 TBM_RETURN_VAL_IF_FAIL (height > 0, NULL);
320 TBM_RETURN_VAL_IF_FAIL (bos, NULL);
322 if (!g_surface_bufmgr)
324 _init_surface_bufmgr();
325 LIST_INITHEAD (&g_surface_list);
329 struct _tbm_bufmgr *mgr = g_surface_bufmgr;
331 TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(mgr), NULL);
333 struct _tbm_surface *surf = NULL;
339 surf = calloc (1, sizeof(struct _tbm_surface));
344 surf->info.width = width;
345 surf->info.height = height;
346 surf->info.format = format;
347 surf->info.bpp = _tbm_surface_internal_get_bpp (format);
348 surf->info.size = tbm_surface_internal_get_size (surf);
349 surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
351 /* get size, stride and offset */
352 for (i = 0; i < surf->info.num_planes; i++)
354 tbm_surface_internal_get_plane_data (surf, i, &size, &offset, &stride);
355 surf->info.planes[i].size = size;
356 surf->info.planes[i].offset = offset;
357 surf->info.planes[i].stride = stride;
360 surf->flags = TBM_BO_DEFAULT;
362 /* create only one bo */
364 for (i = 0; i < num; i++)
369 surf->bos[i] = tbm_bo_ref(bos[i]);
372 LIST_ADD (&surf->item_link, &g_surface_list);
376 for (i = 0; i < num; i++)
380 tbm_bo_unref (surf->bos[i]);
388 if(LIST_IS_EMPTY (&g_surface_list))
390 _deinit_surface_bufmgr ();
391 LIST_DELINIT (&g_surface_list);
399 tbm_surface_internal_destroy (tbm_surface_h surface)
406 surface = (struct _tbm_surface *)surface;
408 for (i = 0; i < surface->num_bos; i++)
410 tbm_bo_unref (surface->bos[i]);
411 surface->bos[i] = NULL;
414 LIST_DEL (&surface->item_link);
419 if(LIST_IS_EMPTY (&g_surface_list))
421 _deinit_surface_bufmgr ();
422 LIST_DELINIT (&g_surface_list);
428 tbm_surface_internal_get_num_bos (tbm_surface_h surface)
430 TBM_RETURN_VAL_IF_FAIL (surface, 0);
432 struct _tbm_surface *surf = (struct _tbm_surface *) surface;
434 return surf->num_bos;
438 tbm_surface_internal_get_bo (tbm_surface_h surface, int bo_idx)
440 TBM_RETURN_VAL_IF_FAIL (surface, NULL);
441 TBM_RETURN_VAL_IF_FAIL (bo_idx > -1, NULL);
443 struct _tbm_surface *surf = (struct _tbm_surface *) surface;
445 return surf->bos[bo_idx];
449 tbm_surface_internal_get_size (tbm_surface_h surface)
451 TBM_RETURN_VAL_IF_FAIL (surface, 0);
453 struct _tbm_surface *surf = (struct _tbm_surface *) surface;
454 struct _tbm_bufmgr *mgr = surf->bufmgr;
457 TBM_RETURN_VAL_IF_FAIL (mgr != NULL, 0);
458 TBM_RETURN_VAL_IF_FAIL (surf->info.width > 0, 0);
459 TBM_RETURN_VAL_IF_FAIL (surf->info.height > 0, 0);
460 TBM_RETURN_VAL_IF_FAIL (surf->info.format > 0, 0);
462 pthread_mutex_lock (&mgr->lock);
464 size = mgr->backend->surface_get_size (surf, surf->info.width, surf->info.height, surf->info.format);
466 pthread_mutex_unlock (&mgr->lock);
472 tbm_surface_internal_get_plane_data (tbm_surface_h surface, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch)
474 TBM_RETURN_VAL_IF_FAIL (surface, 0);
475 TBM_RETURN_VAL_IF_FAIL (plane_idx > -1, 0);
477 struct _tbm_surface *surf = (struct _tbm_surface *) surface;
478 struct _tbm_bufmgr *mgr = surf->bufmgr;
481 TBM_RETURN_VAL_IF_FAIL (mgr != NULL, 0);
482 TBM_RETURN_VAL_IF_FAIL (surf->info.width > 0, 0);
483 TBM_RETURN_VAL_IF_FAIL (surf->info.height > 0, 0);
484 TBM_RETURN_VAL_IF_FAIL (surf->info.format > 0, 0);
486 pthread_mutex_lock (&mgr->lock);
488 ret = mgr->backend->surface_get_plane_data (surf, surf->info.width, surf->info.height, surf->info.format, plane_idx, size, offset, pitch);
492 pthread_mutex_unlock (&mgr->lock);