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"
37 #include "tbm_user_data.h"
39 static tbm_bufmgr g_surface_bufmgr = NULL;
40 static pthread_mutex_t tbm_surface_lock;
42 char *_tbm_surface_internal_format_to_str(tbm_format format)
46 return "TBM_FORMAT_C8";
47 case TBM_FORMAT_RGB332:
48 return "TBM_FORMAT_RGB332";
49 case TBM_FORMAT_BGR233:
50 return "TBM_FORMAT_BGR233";
51 case TBM_FORMAT_XRGB4444:
52 return "TBM_FORMAT_XRGB4444";
53 case TBM_FORMAT_XBGR4444:
54 return "TBM_FORMAT_XBGR4444";
55 case TBM_FORMAT_RGBX4444:
56 return "TBM_FORMAT_RGBX4444";
57 case TBM_FORMAT_BGRX4444:
58 return "TBM_FORMAT_BGRX4444";
59 case TBM_FORMAT_ARGB4444:
60 return "TBM_FORMAT_ARGB4444";
61 case TBM_FORMAT_ABGR4444:
62 return "TBM_FORMAT_ABGR4444";
63 case TBM_FORMAT_RGBA4444:
64 return "TBM_FORMAT_RGBA4444";
65 case TBM_FORMAT_BGRA4444:
66 return "TBM_FORMAT_BGRA4444";
67 case TBM_FORMAT_XRGB1555:
68 return "TBM_FORMAT_XRGB1555";
69 case TBM_FORMAT_XBGR1555:
70 return "TBM_FORMAT_XBGR1555";
71 case TBM_FORMAT_RGBX5551:
72 return "TBM_FORMAT_RGBX5551";
73 case TBM_FORMAT_BGRX5551:
74 return "TBM_FORMAT_BGRX5551";
75 case TBM_FORMAT_ARGB1555:
76 return "TBM_FORMAT_ARGB1555";
77 case TBM_FORMAT_ABGR1555:
78 return "TBM_FORMAT_ABGR1555";
79 case TBM_FORMAT_RGBA5551:
80 return "TBM_FORMAT_RGBA5551";
81 case TBM_FORMAT_BGRA5551:
82 return "TBM_FORMAT_BGRA5551";
83 case TBM_FORMAT_RGB565:
84 return "TBM_FORMAT_RGB565";
85 case TBM_FORMAT_BGR565:
86 return "TBM_FORMAT_BGR565";
87 case TBM_FORMAT_RGB888:
88 return "TBM_FORMAT_RGB888";
89 case TBM_FORMAT_BGR888:
90 return "TBM_FORMAT_BGR888";
91 case TBM_FORMAT_XRGB8888:
92 return "TBM_FORMAT_XRGB8888";
93 case TBM_FORMAT_XBGR8888:
94 return "TBM_FORMAT_XBGR8888";
95 case TBM_FORMAT_RGBX8888:
96 return "TBM_FORMAT_RGBX8888";
97 case TBM_FORMAT_BGRX8888:
98 return "TBM_FORMAT_BGRX8888";
99 case TBM_FORMAT_ARGB8888:
100 return "TBM_FORMAT_ARGB8888";
101 case TBM_FORMAT_ABGR8888:
102 return "TBM_FORMAT_ABGR8888";
103 case TBM_FORMAT_RGBA8888:
104 return "TBM_FORMAT_RGBA8888";
105 case TBM_FORMAT_BGRA8888:
106 return "TBM_FORMAT_BGRA8888";
107 case TBM_FORMAT_XRGB2101010:
108 return "TBM_FORMAT_XRGB2101010";
109 case TBM_FORMAT_XBGR2101010:
110 return "TBM_FORMAT_XBGR2101010";
111 case TBM_FORMAT_RGBX1010102:
112 return "TBM_FORMAT_RGBX1010102";
113 case TBM_FORMAT_BGRX1010102:
114 return "TBM_FORMAT_BGRX1010102";
115 case TBM_FORMAT_ARGB2101010:
116 return "TBM_FORMAT_ARGB2101010";
117 case TBM_FORMAT_ABGR2101010:
118 return "TBM_FORMAT_ABGR2101010";
119 case TBM_FORMAT_RGBA1010102:
120 return "TBM_FORMAT_RGBA1010102";
121 case TBM_FORMAT_BGRA1010102:
122 return "TBM_FORMAT_BGRA1010102";
123 case TBM_FORMAT_YUYV:
124 return "TBM_FORMAT_YUYV";
125 case TBM_FORMAT_YVYU:
126 return "TBM_FORMAT_YVYU";
127 case TBM_FORMAT_UYVY:
128 return "TBM_FORMAT_UYVY";
129 case TBM_FORMAT_VYUY:
130 return "TBM_FORMAT_VYUY";
131 case TBM_FORMAT_AYUV:
132 return "TBM_FORMAT_AYUV";
133 case TBM_FORMAT_NV12:
134 return "TBM_FORMAT_NV12";
135 case TBM_FORMAT_NV21:
136 return "TBM_FORMAT_NV21";
137 case TBM_FORMAT_NV16:
138 return "TBM_FORMAT_NV16";
139 case TBM_FORMAT_NV61:
140 return "TBM_FORMAT_NV61";
141 case TBM_FORMAT_YUV410:
142 return "TBM_FORMAT_YUV410";
143 case TBM_FORMAT_YVU410:
144 return "TBM_FORMAT_YVU410";
145 case TBM_FORMAT_YUV411:
146 return "TBM_FORMAT_YUV411";
147 case TBM_FORMAT_YVU411:
148 return "TBM_FORMAT_YVU411";
149 case TBM_FORMAT_YUV420:
150 return "TBM_FORMAT_YUV420";
151 case TBM_FORMAT_YVU420:
152 return "TBM_FORMAT_YVU420";
153 case TBM_FORMAT_YUV422:
154 return "TBM_FORMAT_YUV422";
155 case TBM_FORMAT_YVU422:
156 return "TBM_FORMAT_YVU422";
157 case TBM_FORMAT_YUV444:
158 return "TBM_FORMAT_YUV444";
159 case TBM_FORMAT_YVU444:
160 return "TBM_FORMAT_YVU444";
161 case TBM_FORMAT_NV12MT:
162 return "TBM_FORMAT_NV12MT";
168 static bool _tbm_surface_mutex_init(void)
170 static bool tbm_surface_mutex_init = false;
172 if (tbm_surface_mutex_init)
175 if (pthread_mutex_init(&tbm_surface_lock, NULL)) {
176 TBM_LOG("[libtbm] fail: tbm_surface mutex init\n");
180 tbm_surface_mutex_init = true;
185 void _tbm_surface_mutex_lock(void)
187 if (!_tbm_surface_mutex_init())
190 pthread_mutex_lock(&tbm_surface_lock);
193 void _tbm_surface_mutex_unlock(void)
195 pthread_mutex_unlock(&tbm_surface_lock);
198 static void _init_surface_bufmgr()
200 g_surface_bufmgr = tbm_bufmgr_init(-1);
203 static void _deinit_surface_bufmgr()
205 if (!g_surface_bufmgr)
208 tbm_bufmgr_deinit(g_surface_bufmgr);
209 g_surface_bufmgr = NULL;
212 static int _tbm_surface_internal_query_size(tbm_surface_h surface)
214 TBM_RETURN_VAL_IF_FAIL(surface, 0);
216 struct _tbm_surface *surf = (struct _tbm_surface *)surface;
217 struct _tbm_bufmgr *mgr = surf->bufmgr;
220 TBM_RETURN_VAL_IF_FAIL(mgr != NULL, 0);
221 TBM_RETURN_VAL_IF_FAIL(surf->info.width > 0, 0);
222 TBM_RETURN_VAL_IF_FAIL(surf->info.height > 0, 0);
223 TBM_RETURN_VAL_IF_FAIL(surf->info.format > 0, 0);
225 if (!mgr->backend->surface_get_size)
228 size = mgr->backend->surface_get_size(surf, surf->info.width, surf->info.height, surf->info.format);
233 static int _tbm_surface_internal_query_plane_data(tbm_surface_h surface, int plane_idx, uint32_t * size, uint32_t * offset, uint32_t * pitch, int *bo_idx)
235 TBM_RETURN_VAL_IF_FAIL(surface, 0);
236 TBM_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
238 struct _tbm_surface *surf = (struct _tbm_surface *)surface;
239 struct _tbm_bufmgr *mgr = surf->bufmgr;
242 TBM_RETURN_VAL_IF_FAIL(mgr != NULL, 0);
243 TBM_RETURN_VAL_IF_FAIL(surf->info.width > 0, 0);
244 TBM_RETURN_VAL_IF_FAIL(surf->info.height > 0, 0);
245 TBM_RETURN_VAL_IF_FAIL(surf->info.format > 0, 0);
247 if (!mgr->backend->surface_get_plane_data)
250 ret = mgr->backend->surface_get_plane_data(surf, surf->info.width, surf->info.height, surf->info.format, plane_idx, size, offset, pitch, bo_idx);
257 static int _tbm_surface_internal_query_num_bos(tbm_format format)
259 TBM_RETURN_VAL_IF_FAIL(format > 0, 0);
260 struct _tbm_bufmgr *mgr;
263 mgr = g_surface_bufmgr;
265 if (!mgr->backend->surface_get_num_bos)
268 ret = mgr->backend->surface_get_num_bos(format);
275 static void _tbm_surface_internal_destroy(tbm_surface_h surface)
278 tbm_bufmgr bufmgr = surface->bufmgr;
279 tbm_user_data *old_data = NULL, *tmp = NULL;
281 for (i = 0; i < surface->num_bos; i++) {
282 surface->bos[i]->surface = NULL;
284 tbm_bo_unref(surface->bos[i]);
285 surface->bos[i] = NULL;
288 /* destory the user_data_list */
289 if (!LIST_IS_EMPTY(&surface->user_data_list)) {
290 LIST_FOR_EACH_ENTRY_SAFE(old_data, tmp, &surface->user_data_list, item_link) {
291 TBM_LOG("[tbm_surface:%d] free user_data \n",
293 user_data_delete(old_data);
297 LIST_DEL(&surface->item_link);
302 if (LIST_IS_EMPTY(&bufmgr->surf_list)) {
303 LIST_DELINIT(&bufmgr->surf_list);
304 _deinit_surface_bufmgr();
308 int tbm_surface_internal_query_supported_formats(uint32_t ** formats, uint32_t * num)
310 struct _tbm_bufmgr *mgr;
313 _tbm_surface_mutex_lock();
315 if (!g_surface_bufmgr) {
316 _init_surface_bufmgr();
317 LIST_INITHEAD(&g_surface_bufmgr->surf_list);
320 mgr = g_surface_bufmgr;
322 if (!mgr->backend->surface_supported_format) {
323 _tbm_surface_mutex_unlock();
327 ret = mgr->backend->surface_supported_format(formats, num);
329 _tbm_surface_mutex_unlock();
334 int tbm_surface_internal_get_num_planes(tbm_format format)
340 case TBM_FORMAT_RGB332:
341 case TBM_FORMAT_BGR233:
342 case TBM_FORMAT_XRGB4444:
343 case TBM_FORMAT_XBGR4444:
344 case TBM_FORMAT_RGBX4444:
345 case TBM_FORMAT_BGRX4444:
346 case TBM_FORMAT_ARGB4444:
347 case TBM_FORMAT_ABGR4444:
348 case TBM_FORMAT_RGBA4444:
349 case TBM_FORMAT_BGRA4444:
350 case TBM_FORMAT_XRGB1555:
351 case TBM_FORMAT_XBGR1555:
352 case TBM_FORMAT_RGBX5551:
353 case TBM_FORMAT_BGRX5551:
354 case TBM_FORMAT_ARGB1555:
355 case TBM_FORMAT_ABGR1555:
356 case TBM_FORMAT_RGBA5551:
357 case TBM_FORMAT_BGRA5551:
358 case TBM_FORMAT_RGB565:
359 case TBM_FORMAT_BGR565:
360 case TBM_FORMAT_RGB888:
361 case TBM_FORMAT_BGR888:
362 case TBM_FORMAT_XRGB8888:
363 case TBM_FORMAT_XBGR8888:
364 case TBM_FORMAT_RGBX8888:
365 case TBM_FORMAT_BGRX8888:
366 case TBM_FORMAT_ARGB8888:
367 case TBM_FORMAT_ABGR8888:
368 case TBM_FORMAT_RGBA8888:
369 case TBM_FORMAT_BGRA8888:
370 case TBM_FORMAT_XRGB2101010:
371 case TBM_FORMAT_XBGR2101010:
372 case TBM_FORMAT_RGBX1010102:
373 case TBM_FORMAT_BGRX1010102:
374 case TBM_FORMAT_ARGB2101010:
375 case TBM_FORMAT_ABGR2101010:
376 case TBM_FORMAT_RGBA1010102:
377 case TBM_FORMAT_BGRA1010102:
378 case TBM_FORMAT_YUYV:
379 case TBM_FORMAT_YVYU:
380 case TBM_FORMAT_UYVY:
381 case TBM_FORMAT_VYUY:
382 case TBM_FORMAT_AYUV:
385 case TBM_FORMAT_NV12:
386 case TBM_FORMAT_NV21:
387 case TBM_FORMAT_NV16:
388 case TBM_FORMAT_NV61:
391 case TBM_FORMAT_YUV410:
392 case TBM_FORMAT_YVU410:
393 case TBM_FORMAT_YUV411:
394 case TBM_FORMAT_YVU411:
395 case TBM_FORMAT_YUV420:
396 case TBM_FORMAT_YVU420:
397 case TBM_FORMAT_YUV422:
398 case TBM_FORMAT_YVU422:
399 case TBM_FORMAT_YUV444:
400 case TBM_FORMAT_YVU444:
411 int tbm_surface_internal_get_bpp(tbm_format format)
417 case TBM_FORMAT_RGB332:
418 case TBM_FORMAT_BGR233:
421 case TBM_FORMAT_XRGB4444:
422 case TBM_FORMAT_XBGR4444:
423 case TBM_FORMAT_RGBX4444:
424 case TBM_FORMAT_BGRX4444:
425 case TBM_FORMAT_ARGB4444:
426 case TBM_FORMAT_ABGR4444:
427 case TBM_FORMAT_RGBA4444:
428 case TBM_FORMAT_BGRA4444:
429 case TBM_FORMAT_XRGB1555:
430 case TBM_FORMAT_XBGR1555:
431 case TBM_FORMAT_RGBX5551:
432 case TBM_FORMAT_BGRX5551:
433 case TBM_FORMAT_ARGB1555:
434 case TBM_FORMAT_ABGR1555:
435 case TBM_FORMAT_RGBA5551:
436 case TBM_FORMAT_BGRA5551:
437 case TBM_FORMAT_RGB565:
438 case TBM_FORMAT_BGR565:
441 case TBM_FORMAT_RGB888:
442 case TBM_FORMAT_BGR888:
445 case TBM_FORMAT_XRGB8888:
446 case TBM_FORMAT_XBGR8888:
447 case TBM_FORMAT_RGBX8888:
448 case TBM_FORMAT_BGRX8888:
449 case TBM_FORMAT_ARGB8888:
450 case TBM_FORMAT_ABGR8888:
451 case TBM_FORMAT_RGBA8888:
452 case TBM_FORMAT_BGRA8888:
453 case TBM_FORMAT_XRGB2101010:
454 case TBM_FORMAT_XBGR2101010:
455 case TBM_FORMAT_RGBX1010102:
456 case TBM_FORMAT_BGRX1010102:
457 case TBM_FORMAT_ARGB2101010:
458 case TBM_FORMAT_ABGR2101010:
459 case TBM_FORMAT_RGBA1010102:
460 case TBM_FORMAT_BGRA1010102:
461 case TBM_FORMAT_YUYV:
462 case TBM_FORMAT_YVYU:
463 case TBM_FORMAT_UYVY:
464 case TBM_FORMAT_VYUY:
465 case TBM_FORMAT_AYUV:
468 case TBM_FORMAT_NV12:
469 case TBM_FORMAT_NV21:
472 case TBM_FORMAT_NV16:
473 case TBM_FORMAT_NV61:
476 case TBM_FORMAT_YUV410:
477 case TBM_FORMAT_YVU410:
480 case TBM_FORMAT_YUV411:
481 case TBM_FORMAT_YVU411:
482 case TBM_FORMAT_YUV420:
483 case TBM_FORMAT_YVU420:
486 case TBM_FORMAT_YUV422:
487 case TBM_FORMAT_YVU422:
490 case TBM_FORMAT_YUV444:
491 case TBM_FORMAT_YVU444:
501 tbm_surface_h tbm_surface_internal_create_with_flags(int width, int height, int format, int flags)
503 TBM_RETURN_VAL_IF_FAIL(width > 0, NULL);
504 TBM_RETURN_VAL_IF_FAIL(height > 0, NULL);
506 struct _tbm_bufmgr *mgr;
507 struct _tbm_surface *surf = NULL;
511 uint32_t bo_size = 0;
515 _tbm_surface_mutex_lock();
517 if (!g_surface_bufmgr) {
518 _init_surface_bufmgr();
519 LIST_INITHEAD(&g_surface_bufmgr->surf_list);
522 mgr = g_surface_bufmgr;
523 if (!TBM_BUFMGR_IS_VALID(mgr)) {
524 _tbm_surface_mutex_unlock();
527 surf = calloc(1, sizeof(struct _tbm_surface));
529 _tbm_surface_mutex_unlock();
534 surf->info.width = width;
535 surf->info.height = height;
536 surf->info.format = format;
537 surf->info.bpp = tbm_surface_internal_get_bpp(format);
538 surf->info.size = _tbm_surface_internal_query_size(surf);
539 surf->info.num_planes = tbm_surface_internal_get_num_planes(format);
540 surf->num_bos = _tbm_surface_internal_query_num_bos(format);
543 /* get size, stride and offset bo_idx */
544 for (i = 0; i < surf->info.num_planes; i++) {
545 _tbm_surface_internal_query_plane_data(surf, i, &size, &offset, &stride, &bo_idx);
546 surf->info.planes[i].size = size;
547 surf->info.planes[i].offset = offset;
548 surf->info.planes[i].stride = stride;
549 surf->planes_bo_idx[i] = bo_idx;
554 for (i = 0; i < surf->num_bos; i++) {
556 for (j = 0; j < surf->info.num_planes; j++) {
557 if (surf->planes_bo_idx[j] == i)
558 bo_size += surf->info.planes[j].size;
560 surf->bos[i] = tbm_bo_alloc(mgr, bo_size, flags);
562 for (j = 0; j < i; j++) {
564 tbm_bo_unref(surf->bos[j]);
570 if (LIST_IS_EMPTY(&mgr->surf_list)) {
571 LIST_DELINIT(&mgr->surf_list);
572 _deinit_surface_bufmgr();
575 _tbm_surface_mutex_unlock();
578 _tbm_bo_set_surface(surf->bos[i], surf);
582 LIST_ADD(&surf->item_link, &mgr->surf_list);
584 _tbm_surface_mutex_unlock();
589 tbm_surface_h tbm_surface_internal_create_with_bos(tbm_surface_info_s * info, tbm_bo * bos, int num)
591 TBM_RETURN_VAL_IF_FAIL(bos, NULL);
592 TBM_RETURN_VAL_IF_FAIL(info, NULL);
593 TBM_RETURN_VAL_IF_FAIL(num == 1 || info->num_planes == num, NULL);
595 struct _tbm_bufmgr *mgr;
596 struct _tbm_surface *surf = NULL;
599 _tbm_surface_mutex_lock();
601 if (!g_surface_bufmgr) {
602 _init_surface_bufmgr();
603 LIST_INITHEAD(&g_surface_bufmgr->surf_list);
606 mgr = g_surface_bufmgr;
607 if (!TBM_BUFMGR_IS_VALID(mgr)) {
608 _tbm_surface_mutex_unlock();
612 surf = calloc(1, sizeof(struct _tbm_surface));
614 _tbm_surface_mutex_unlock();
619 surf->info.width = info->width;
620 surf->info.height = info->height;
621 surf->info.format = info->format;
622 surf->info.bpp = info->bpp;
623 surf->info.num_planes = info->num_planes;
626 /* get size, stride and offset */
627 for (i = 0; i < info->num_planes; i++) {
628 surf->info.planes[i].offset = info->planes[i].offset;
629 surf->info.planes[i].stride = info->planes[i].stride;
631 if (info->planes[i].size > 0)
632 surf->info.planes[i].size = info->planes[i].size;
634 surf->info.planes[i].size += surf->info.planes[i].stride * info->height;
637 surf->planes_bo_idx[i] = 0;
639 surf->planes_bo_idx[i] = i;
642 if (info->size > 0) {
643 surf->info.size = info->size;
646 for (i = 0; i < info->num_planes; i++)
647 surf->info.size += surf->info.planes[i].size;
650 surf->flags = TBM_BO_DEFAULT;
652 /* create only one bo */
654 for (i = 0; i < num; i++) {
658 surf->bos[i] = tbm_bo_ref(bos[i]);
659 _tbm_bo_set_surface(bos[i], surf);
662 LIST_ADD(&surf->item_link, &mgr->surf_list);
664 _tbm_surface_mutex_unlock();
668 for (i = 0; i < num; i++) {
670 tbm_bo_unref(surf->bos[i]);
678 if (LIST_IS_EMPTY(&g_surface_bufmgr->surf_list)) {
679 LIST_DELINIT(&g_surface_bufmgr->surf_list);
680 _deinit_surface_bufmgr();
683 _tbm_surface_mutex_unlock();
688 void tbm_surface_internal_destroy(tbm_surface_h surface)
693 _tbm_surface_mutex_lock();
697 if (surface->refcnt > 0) {
698 _tbm_surface_mutex_unlock();
702 if (surface->refcnt == 0)
703 _tbm_surface_internal_destroy(surface);
705 _tbm_surface_mutex_unlock();
708 void tbm_surface_internal_ref(tbm_surface_h surface)
710 TBM_RETURN_IF_FAIL(surface);
712 _tbm_surface_mutex_lock();
716 _tbm_surface_mutex_unlock();
719 void tbm_surface_internal_unref(tbm_surface_h surface)
721 TBM_RETURN_IF_FAIL(surface);
723 _tbm_surface_mutex_lock();
727 if (surface->refcnt > 0) {
728 _tbm_surface_mutex_unlock();
732 if (surface->refcnt == 0)
733 _tbm_surface_internal_destroy(surface);
735 _tbm_surface_mutex_unlock();
738 int tbm_surface_internal_get_num_bos(tbm_surface_h surface)
740 TBM_RETURN_VAL_IF_FAIL(surface, 0);
742 struct _tbm_surface *surf;
745 _tbm_surface_mutex_lock();
747 surf = (struct _tbm_surface *)surface;
750 _tbm_surface_mutex_unlock();
755 tbm_bo tbm_surface_internal_get_bo(tbm_surface_h surface, int bo_idx)
757 TBM_RETURN_VAL_IF_FAIL(surface, NULL);
758 TBM_RETURN_VAL_IF_FAIL(bo_idx > -1, NULL);
760 struct _tbm_surface *surf;
763 _tbm_surface_mutex_lock();
765 surf = (struct _tbm_surface *)surface;
766 bo = surf->bos[bo_idx];
768 _tbm_surface_mutex_unlock();
773 int tbm_surface_internal_get_size(tbm_surface_h surface)
775 TBM_RETURN_VAL_IF_FAIL(surface, 0);
777 struct _tbm_surface *surf;
780 _tbm_surface_mutex_lock();
782 surf = (struct _tbm_surface *)surface;
783 size = surf->info.size;
785 _tbm_surface_mutex_unlock();
790 int tbm_surface_internal_get_plane_data(tbm_surface_h surface, int plane_idx, uint32_t * size, uint32_t * offset, uint32_t * pitch)
792 TBM_RETURN_VAL_IF_FAIL(surface, 0);
793 TBM_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
795 struct _tbm_surface *surf;
797 _tbm_surface_mutex_lock();
799 surf = (struct _tbm_surface *)surface;
801 if (plane_idx >= surf->info.num_planes) {
802 _tbm_surface_mutex_unlock();
807 *size = surf->info.planes[plane_idx].size;
810 *offset = surf->info.planes[plane_idx].offset;
813 *pitch = surf->info.planes[plane_idx].stride;
815 _tbm_surface_mutex_unlock();
820 int tbm_surface_internal_get_info(tbm_surface_h surface, int opt, tbm_surface_info_s * info, int map)
822 struct _tbm_surface *surf;
823 tbm_bo_handle bo_handles[4];
826 _tbm_surface_mutex_lock();
828 memset(bo_handles, 0, sizeof(tbm_bo_handle) * 4);
830 surf = (struct _tbm_surface *)surface;
832 info->width = surf->info.width;
833 info->height = surf->info.height;
834 info->format = surf->info.format;
835 info->bpp = surf->info.bpp;
836 info->size = surf->info.size;
837 info->num_planes = surf->info.num_planes;
840 for (i = 0; i < surf->num_bos; i++) {
841 bo_handles[i] = tbm_bo_map(surf->bos[i], TBM_DEVICE_CPU, opt);
842 if (bo_handles[i].ptr == NULL) {
843 for (j = 0; j < i; j++)
844 tbm_bo_unmap(surf->bos[j]);
846 _tbm_surface_mutex_unlock();
851 for (i = 0; i < surf->num_bos; i++) {
852 bo_handles[i] = tbm_bo_get_handle(surf->bos[i], TBM_DEVICE_CPU);
853 if (bo_handles[i].ptr == NULL) {
854 _tbm_surface_mutex_unlock();
860 for (i = 0; i < surf->info.num_planes; i++) {
861 info->planes[i].size = surf->info.planes[i].size;
862 info->planes[i].offset = surf->info.planes[i].offset;
863 info->planes[i].stride = surf->info.planes[i].stride;
864 info->planes[i].ptr = bo_handles[surf->planes_bo_idx[i]].ptr + surf->info.planes[i].offset;
867 _tbm_surface_mutex_unlock();
872 void tbm_surface_internal_unmap(tbm_surface_h surface)
874 struct _tbm_surface *surf;
877 _tbm_surface_mutex_lock();
879 surf = (struct _tbm_surface *)surface;
881 for (i = 0; i < surf->num_bos; i++)
882 tbm_bo_unmap(surf->bos[i]);
884 _tbm_surface_mutex_unlock();
887 unsigned int tbm_surface_internal_get_width(tbm_surface_h surface)
889 struct _tbm_surface *surf;
892 _tbm_surface_mutex_lock();
894 surf = (struct _tbm_surface *)surface;
895 width = surf->info.width;
897 _tbm_surface_mutex_unlock();
902 unsigned int tbm_surface_internal_get_height(tbm_surface_h surface)
904 struct _tbm_surface *surf;
907 _tbm_surface_mutex_lock();
909 surf = (struct _tbm_surface *)surface;
910 height = surf->info.height;
912 _tbm_surface_mutex_unlock();
918 tbm_format tbm_surface_internal_get_format(tbm_surface_h surface)
920 struct _tbm_surface *surf;
923 _tbm_surface_mutex_lock();
925 surf = (struct _tbm_surface *)surface;
926 format = surf->info.format;
928 _tbm_surface_mutex_unlock();
933 int tbm_surface_internal_get_plane_bo_idx(tbm_surface_h surface, int plane_idx)
935 TBM_RETURN_VAL_IF_FAIL(surface, 0);
936 TBM_RETURN_VAL_IF_FAIL(plane_idx > -1, 0);
937 struct _tbm_surface *surf;
940 _tbm_surface_mutex_lock();
942 surf = (struct _tbm_surface *)surface;
943 bo_idx = surf->planes_bo_idx[plane_idx];
945 _tbm_surface_mutex_unlock();
950 unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface)
952 TBM_RETURN_VAL_IF_FAIL(surface, 0);
954 return surface->debug_pid;
957 void tbm_surface_internal_set_debug_pid(tbm_surface_h surface, unsigned int pid)
959 TBM_RETURN_IF_FAIL(surface);
961 surface->debug_pid = pid;
964 int tbm_surface_internal_add_user_data(tbm_surface_h surface, unsigned long key, tbm_data_free data_free_func)
966 TBM_RETURN_VAL_IF_FAIL(surface, 0);
970 /* check if the data according to the key exist if so, return false. */
971 data = user_data_lookup(&surface->user_data_list, key);
973 TBM_LOG("[libtbm:%d] "
974 "waring: %s:%d user data already exist. key:%ld\n",
975 getpid(), __FUNCTION__, __LINE__, key);
979 data = user_data_create(key, data_free_func);
983 LIST_ADD(&data->item_link, &surface->user_data_list);
988 int tbm_surface_internal_set_user_data(tbm_surface_h surface, unsigned long key, void *data)
990 TBM_RETURN_VAL_IF_FAIL(surface, 0);
992 tbm_user_data *old_data;
994 if (LIST_IS_EMPTY(&surface->user_data_list))
997 old_data = user_data_lookup(&surface->user_data_list, key);
1001 if (old_data->data && old_data->free_func)
1002 old_data->free_func(old_data->data);
1004 old_data->data = data;
1009 int tbm_surface_internal_get_user_data(tbm_surface_h surface, unsigned long key, void **data)
1011 TBM_RETURN_VAL_IF_FAIL(surface, 0);
1013 tbm_user_data *old_data;
1015 if (!data || LIST_IS_EMPTY(&surface->user_data_list))
1018 old_data = user_data_lookup(&surface->user_data_list, key);
1024 *data = old_data->data;
1029 int tbm_surface_internal_delete_user_data(tbm_surface_h surface, unsigned long key)
1031 TBM_RETURN_VAL_IF_FAIL(surface, 0);
1033 tbm_user_data *old_data = (void *)0;
1035 if (LIST_IS_EMPTY(&surface->user_data_list))
1038 old_data = user_data_lookup(&surface->user_data_list, key);
1042 user_data_delete(old_data);