remove bufmgr parameter at the tbm_surface_internal APIs
[platform/core/uifw/libtbm.git] / src / tbm_surface_internal.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
6
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>
9
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:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
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.
29
30 **************************************************************************/
31
32 #include "config.h"
33 #include "tbm_bufmgr.h"
34 #include "tbm_bufmgr_int.h"
35 #include "tbm_surface_internal.h"
36 #include "list.h"
37
38 static tbm_bufmgr g_surface_bufmgr = NULL;
39 struct list_head g_surface_list; /* list of surfaces belonging to bufmgr */
40
41 static int _tbm_surface_internal_get_num_planes (tbm_format format)
42 {
43     int num_planes = 0;
44
45     switch (format)
46     {
47         case TBM_FORMAT_C8:
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:
86         case TBM_FORMAT_YUYV:
87         case TBM_FORMAT_YVYU:
88         case TBM_FORMAT_UYVY:
89         case TBM_FORMAT_VYUY:
90         case TBM_FORMAT_AYUV:
91             num_planes = 1;
92             break;
93         case TBM_FORMAT_NV12:
94         case TBM_FORMAT_NV21:
95         case TBM_FORMAT_NV16:
96         case TBM_FORMAT_NV61:
97             num_planes = 2;
98             break;
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:
109             num_planes = 3;
110             break;
111
112         default :
113             break;
114     }
115
116     return num_planes;
117 }
118
119
120 static int _tbm_surface_internal_get_bpp (tbm_format format)
121 {
122     int bpp = 0;
123
124     switch (format)
125     {
126         case TBM_FORMAT_C8:
127         case TBM_FORMAT_RGB332:
128         case TBM_FORMAT_BGR233:
129             bpp = 8;
130             break;
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:
149             bpp = 16;
150             break;
151         case TBM_FORMAT_RGB888:
152         case TBM_FORMAT_BGR888:
153             bpp = 24;
154             break;
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:
176             bpp = 32;
177             break;
178         case TBM_FORMAT_NV12:
179         case TBM_FORMAT_NV21:
180             bpp = 12;
181             break;
182         case TBM_FORMAT_NV16:
183         case TBM_FORMAT_NV61:
184             bpp = 16;
185             break;
186         case TBM_FORMAT_YUV410:
187         case TBM_FORMAT_YVU410:
188             bpp = 9;
189             break;
190         case TBM_FORMAT_YUV411:
191         case TBM_FORMAT_YVU411:
192         case TBM_FORMAT_YUV420:
193         case TBM_FORMAT_YVU420:
194             bpp = 12;
195             break;
196         case TBM_FORMAT_YUV422:
197         case TBM_FORMAT_YVU422:
198             bpp = 16;
199             break;
200         case TBM_FORMAT_YUV444:
201         case TBM_FORMAT_YVU444:
202             bpp = 24;
203             break;
204         default :
205             break;
206     }
207
208     return bpp;
209 }
210
211 static void
212 _init_surface_bufmgr()
213 {
214     g_surface_bufmgr = tbm_bufmgr_init (-1);
215 }
216
217 static void
218 _deinit_surface_bufmgr()
219 {
220     if (!g_surface_bufmgr)
221         return;
222
223     tbm_bufmgr_deinit (g_surface_bufmgr);
224     g_surface_bufmgr = NULL;
225 }
226
227
228 int
229 tbm_surface_internal_query_supported_formats (uint32_t **formats, uint32_t *num)
230 {
231     if (!g_surface_bufmgr)
232     {
233         _init_surface_bufmgr();
234         LIST_INITHEAD (&g_surface_list);
235     }
236
237     struct _tbm_bufmgr *mgr = g_surface_bufmgr;
238     int ret = 0;
239
240     pthread_mutex_lock (&mgr->lock);
241
242     ret = mgr->backend->surface_supported_format (formats, num);
243
244     pthread_mutex_unlock (&mgr->lock);
245
246     return ret;
247 }
248
249 tbm_surface_h
250 tbm_surface_internal_create_with_flags (int width, int height, int format, int flags)
251 {
252     TBM_RETURN_VAL_IF_FAIL (width > 0, NULL);
253     TBM_RETURN_VAL_IF_FAIL (height > 0, NULL);
254
255     if (!g_surface_bufmgr)
256     {
257         _init_surface_bufmgr();
258         LIST_INITHEAD (&g_surface_list);
259     }
260
261     struct _tbm_bufmgr *mgr = g_surface_bufmgr;
262
263
264     TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(mgr), NULL);
265
266     struct _tbm_surface *surf = NULL;
267     uint32_t size = 0;
268     uint32_t offset = 0;
269     uint32_t stride = 0;
270     int i;
271
272     surf = calloc (1, sizeof(struct _tbm_surface));
273     if (!surf)
274         return NULL;
275
276     surf->bufmgr = mgr;
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);
283
284     /* get size, stride and offset */
285     for (i = 0; i < surf->info.num_planes; i++)
286     {
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;
291     }
292
293     surf->flags = flags;
294
295     /* create only one bo */
296     surf->num_bos = 1;
297     surf->bos[0] = tbm_bo_alloc (mgr, surf->info.size, flags);
298     if (!surf->bos[0])
299     {
300         free (surf);
301         surf = NULL;
302
303         if(LIST_IS_EMPTY (&g_surface_list))
304         {
305             _deinit_surface_bufmgr ();
306             LIST_DELINIT (&g_surface_list);
307         }
308     }
309
310     LIST_ADD (&surf->item_link, &g_surface_list);
311
312     return surf;
313 }
314
315 tbm_surface_h
316 tbm_surface_internal_create_with_bos (int width, int height, int format, tbm_bo *bos, int num)
317 {
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);
321
322     if (!g_surface_bufmgr)
323     {
324         _init_surface_bufmgr();
325         LIST_INITHEAD (&g_surface_list);
326     }
327
328
329     struct _tbm_bufmgr *mgr = g_surface_bufmgr;
330
331     TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(mgr), NULL);
332
333     struct _tbm_surface *surf = NULL;
334     uint32_t size = 0;
335     uint32_t offset = 0;
336     uint32_t stride = 0;
337     int i;
338
339     surf = calloc (1, sizeof(struct _tbm_surface));
340     if (!surf)
341         return NULL;
342
343     surf->bufmgr = mgr;
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);
350
351     /* get size, stride and offset */
352     for (i = 0; i < surf->info.num_planes; i++)
353     {
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;
358     }
359
360     surf->flags = TBM_BO_DEFAULT;
361
362     /* create only one bo */
363     surf->num_bos = num;
364     for (i = 0; i < num; i++)
365     {
366         if (bos[i] == NULL)
367             goto bail1;
368
369         surf->bos[i] = tbm_bo_ref(bos[i]);
370     }
371
372     LIST_ADD (&surf->item_link, &g_surface_list);
373
374     return surf;
375 bail1:
376     for (i = 0; i < num; i++)
377     {
378         if (surf->bos[i])
379         {
380             tbm_bo_unref (surf->bos[i]);
381             surf->bos[i] = NULL;
382         }
383     }
384
385     free (surf);
386     surf = NULL;
387
388     if(LIST_IS_EMPTY (&g_surface_list))
389     {
390         _deinit_surface_bufmgr ();
391         LIST_DELINIT (&g_surface_list);
392     }
393
394     return NULL;
395 }
396
397
398 void
399 tbm_surface_internal_destroy (tbm_surface_h surface)
400 {
401     int i;
402
403     if (!surface)
404         return;
405
406     surface = (struct _tbm_surface *)surface;
407
408     for (i = 0; i < surface->num_bos; i++)
409     {
410         tbm_bo_unref (surface->bos[i]);
411         surface->bos[i] = NULL;
412     }
413
414     LIST_DEL (&surface->item_link);
415
416     free (surface);
417     surface = NULL;
418
419     if(LIST_IS_EMPTY (&g_surface_list))
420     {
421         _deinit_surface_bufmgr ();
422         LIST_DELINIT (&g_surface_list);
423     }
424 }
425
426
427 int
428 tbm_surface_internal_get_num_bos (tbm_surface_h surface)
429 {
430     TBM_RETURN_VAL_IF_FAIL (surface, 0);
431
432     struct _tbm_surface *surf = (struct _tbm_surface *) surface;
433
434     return surf->num_bos;
435 }
436
437 tbm_bo
438 tbm_surface_internal_get_bo (tbm_surface_h surface, int bo_idx)
439 {
440     TBM_RETURN_VAL_IF_FAIL (surface, NULL);
441     TBM_RETURN_VAL_IF_FAIL (bo_idx > -1, NULL);
442
443     struct _tbm_surface *surf = (struct _tbm_surface *) surface;
444
445     return surf->bos[bo_idx];
446 }
447
448 int
449 tbm_surface_internal_get_size (tbm_surface_h surface)
450 {
451     TBM_RETURN_VAL_IF_FAIL (surface, 0);
452
453     struct _tbm_surface *surf = (struct _tbm_surface *) surface;
454     struct _tbm_bufmgr *mgr = surf->bufmgr;
455     int size = 0;
456
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);
461
462     pthread_mutex_lock (&mgr->lock);
463
464     size = mgr->backend->surface_get_size (surf, surf->info.width, surf->info.height, surf->info.format);
465
466     pthread_mutex_unlock (&mgr->lock);
467
468     return size;
469 }
470
471 int
472 tbm_surface_internal_get_plane_data (tbm_surface_h surface, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch)
473 {
474     TBM_RETURN_VAL_IF_FAIL (surface, 0);
475     TBM_RETURN_VAL_IF_FAIL (plane_idx > -1, 0);
476
477     struct _tbm_surface *surf = (struct _tbm_surface *) surface;
478     struct _tbm_bufmgr *mgr = surf->bufmgr;
479     int ret = 0;
480
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);
485
486     pthread_mutex_lock (&mgr->lock);
487
488     ret = mgr->backend->surface_get_plane_data (surf, surf->info.width, surf->info.height, surf->info.format, plane_idx, size, offset, pitch);
489     if (!ret)
490         return 0;
491
492     pthread_mutex_unlock (&mgr->lock);
493
494     return 1;
495 }
496
497
498