put the implementation to create tbm_surface with bos
[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
37 static int _tbm_surface_internal_get_num_planes (tbm_format format)
38 {
39     int num_planes = 0;
40
41     switch (format)
42     {
43         case TBM_FORMAT_C8:
44         case TBM_FORMAT_RGB332:
45         case TBM_FORMAT_BGR233:
46         case TBM_FORMAT_XRGB4444:
47         case TBM_FORMAT_XBGR4444:
48         case TBM_FORMAT_RGBX4444:
49         case TBM_FORMAT_BGRX4444:
50         case TBM_FORMAT_ARGB4444:
51         case TBM_FORMAT_ABGR4444:
52         case TBM_FORMAT_RGBA4444:
53         case TBM_FORMAT_BGRA4444:
54         case TBM_FORMAT_XRGB1555:
55         case TBM_FORMAT_XBGR1555:
56         case TBM_FORMAT_RGBX5551:
57         case TBM_FORMAT_BGRX5551:
58         case TBM_FORMAT_ARGB1555:
59         case TBM_FORMAT_ABGR1555:
60         case TBM_FORMAT_RGBA5551:
61         case TBM_FORMAT_BGRA5551:
62         case TBM_FORMAT_RGB565:
63         case TBM_FORMAT_BGR565:
64         case TBM_FORMAT_RGB888:
65         case TBM_FORMAT_BGR888:
66         case TBM_FORMAT_XRGB8888:
67         case TBM_FORMAT_XBGR8888:
68         case TBM_FORMAT_RGBX8888:
69         case TBM_FORMAT_BGRX8888:
70         case TBM_FORMAT_ARGB8888:
71         case TBM_FORMAT_ABGR8888:
72         case TBM_FORMAT_RGBA8888:
73         case TBM_FORMAT_BGRA8888:
74         case TBM_FORMAT_XRGB2101010:
75         case TBM_FORMAT_XBGR2101010:
76         case TBM_FORMAT_RGBX1010102:
77         case TBM_FORMAT_BGRX1010102:
78         case TBM_FORMAT_ARGB2101010:
79         case TBM_FORMAT_ABGR2101010:
80         case TBM_FORMAT_RGBA1010102:
81         case TBM_FORMAT_BGRA1010102:
82         case TBM_FORMAT_YUYV:
83         case TBM_FORMAT_YVYU:
84         case TBM_FORMAT_UYVY:
85         case TBM_FORMAT_VYUY:
86         case TBM_FORMAT_AYUV:
87             num_planes = 1;
88             break;
89         case TBM_FORMAT_NV12:
90         case TBM_FORMAT_NV21:
91         case TBM_FORMAT_NV16:
92         case TBM_FORMAT_NV61:
93             num_planes = 2;
94             break;
95         case TBM_FORMAT_YUV410:
96         case TBM_FORMAT_YVU410:
97         case TBM_FORMAT_YUV411:
98         case TBM_FORMAT_YVU411:
99         case TBM_FORMAT_YUV420:
100         case TBM_FORMAT_YVU420:
101         case TBM_FORMAT_YUV422:
102         case TBM_FORMAT_YVU422:
103         case TBM_FORMAT_YUV444:
104         case TBM_FORMAT_YVU444:
105             num_planes = 3;
106             break;
107
108         default :
109             break;
110     }
111
112     return num_planes;
113 }
114
115
116 static int _tbm_surface_internal_get_bpp (tbm_format format)
117 {
118     int bpp = 0;
119
120     switch (format)
121     {
122         case TBM_FORMAT_C8:
123         case TBM_FORMAT_RGB332:
124         case TBM_FORMAT_BGR233:
125             bpp = 8;
126             break;
127         case TBM_FORMAT_XRGB4444:
128         case TBM_FORMAT_XBGR4444:
129         case TBM_FORMAT_RGBX4444:
130         case TBM_FORMAT_BGRX4444:
131         case TBM_FORMAT_ARGB4444:
132         case TBM_FORMAT_ABGR4444:
133         case TBM_FORMAT_RGBA4444:
134         case TBM_FORMAT_BGRA4444:
135         case TBM_FORMAT_XRGB1555:
136         case TBM_FORMAT_XBGR1555:
137         case TBM_FORMAT_RGBX5551:
138         case TBM_FORMAT_BGRX5551:
139         case TBM_FORMAT_ARGB1555:
140         case TBM_FORMAT_ABGR1555:
141         case TBM_FORMAT_RGBA5551:
142         case TBM_FORMAT_BGRA5551:
143         case TBM_FORMAT_RGB565:
144         case TBM_FORMAT_BGR565:
145             bpp = 16;
146             break;
147         case TBM_FORMAT_RGB888:
148         case TBM_FORMAT_BGR888:
149             bpp = 24;
150             break;
151         case TBM_FORMAT_XRGB8888:
152         case TBM_FORMAT_XBGR8888:
153         case TBM_FORMAT_RGBX8888:
154         case TBM_FORMAT_BGRX8888:
155         case TBM_FORMAT_ARGB8888:
156         case TBM_FORMAT_ABGR8888:
157         case TBM_FORMAT_RGBA8888:
158         case TBM_FORMAT_BGRA8888:
159         case TBM_FORMAT_XRGB2101010:
160         case TBM_FORMAT_XBGR2101010:
161         case TBM_FORMAT_RGBX1010102:
162         case TBM_FORMAT_BGRX1010102:
163         case TBM_FORMAT_ARGB2101010:
164         case TBM_FORMAT_ABGR2101010:
165         case TBM_FORMAT_RGBA1010102:
166         case TBM_FORMAT_BGRA1010102:
167         case TBM_FORMAT_YUYV:
168         case TBM_FORMAT_YVYU:
169         case TBM_FORMAT_UYVY:
170         case TBM_FORMAT_VYUY:
171         case TBM_FORMAT_AYUV:
172             bpp = 32;
173             break;
174         case TBM_FORMAT_NV12:
175         case TBM_FORMAT_NV21:
176             bpp = 12;
177             break;
178         case TBM_FORMAT_NV16:
179         case TBM_FORMAT_NV61:
180             bpp = 16;
181             break;
182         case TBM_FORMAT_YUV410:
183         case TBM_FORMAT_YVU410:
184             bpp = 9;
185             break;
186         case TBM_FORMAT_YUV411:
187         case TBM_FORMAT_YVU411:
188         case TBM_FORMAT_YUV420:
189         case TBM_FORMAT_YVU420:
190             bpp = 12;
191             break;
192         case TBM_FORMAT_YUV422:
193         case TBM_FORMAT_YVU422:
194             bpp = 16;
195             break;
196         case TBM_FORMAT_YUV444:
197         case TBM_FORMAT_YVU444:
198             bpp = 24;
199             break;
200         default :
201             break;
202     }
203
204     return bpp;
205 }
206
207
208 int
209 tbm_surface_internal_query_supported_formats (tbm_bufmgr bufmgr, uint32_t **formats, uint32_t *num)
210 {
211     TBM_RETURN_VAL_IF_FAIL (bufmgr, 0);
212
213     struct _tbm_bufmgr *mgr = (struct _tbm_bufmgr*)bufmgr;
214     int ret = 0;
215
216     pthread_mutex_lock (&mgr->lock);
217
218     ret = mgr->backend->surface_supported_format (formats, num);
219
220     pthread_mutex_unlock (&mgr->lock);
221
222     return ret;
223 }
224
225 tbm_surface_h
226 tbm_surface_internal_create_with_flags (tbm_bufmgr bufmgr, int width, int height, int format, int flags)
227 {
228     TBM_RETURN_VAL_IF_FAIL (bufmgr, NULL);
229     TBM_RETURN_VAL_IF_FAIL (width > 0, NULL);
230     TBM_RETURN_VAL_IF_FAIL (height > 0, NULL);
231
232     struct _tbm_bufmgr *mgr = (struct _tbm_bufmgr*)bufmgr;
233
234     TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(mgr), NULL);
235
236     struct _tbm_surface *surf = NULL;
237     uint32_t size = 0;
238     uint32_t offset = 0;
239     uint32_t stride = 0;
240     int i;
241
242     surf = calloc (1, sizeof(struct _tbm_surface));
243     if (!surf)
244         return NULL;
245
246     surf->bufmgr = bufmgr;
247     surf->info.width = width;
248     surf->info.height = height;
249     surf->info.format = format;
250     surf->info.bpp = _tbm_surface_internal_get_bpp (format);
251     surf->info.size = tbm_surface_internal_get_size (surf);
252     surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
253
254     /* get size, stride and offset */
255     for (i = 0; i < surf->info.num_planes; i++)
256     {
257         tbm_surface_internal_get_plane_data (surf, i, &size, &offset, &stride);
258         surf->info.planes[i].size = size;
259         surf->info.planes[i].offset = offset;
260         surf->info.planes[i].stride = stride;
261     }
262
263     surf->flags = flags;
264
265     /* create only one bo */
266     surf->num_bos = 1;
267     surf->bos[0] = tbm_bo_alloc (mgr, surf->info.size, flags);
268     if (!surf->bos[0])
269     {
270         free (surf);
271         surf = NULL;
272     }
273
274 //    LIST_ADD (&surf->item_link, &mgr->surf_list);
275
276     return surf;
277 }
278
279 tbm_surface_h
280 tbm_surface_internal_create_with_bos (tbm_bufmgr bufmgr, int width, int height, int format, tbm_bo *bos, int num)
281 {
282     TBM_RETURN_VAL_IF_FAIL (bufmgr, NULL);
283     TBM_RETURN_VAL_IF_FAIL (width > 0, NULL);
284     TBM_RETURN_VAL_IF_FAIL (height > 0, NULL);
285     TBM_RETURN_VAL_IF_FAIL (bos, NULL);
286
287     struct _tbm_bufmgr *mgr = (struct _tbm_bufmgr*)bufmgr;
288
289     TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(mgr), NULL);
290
291     struct _tbm_surface *surf = NULL;
292     uint32_t size = 0;
293     uint32_t offset = 0;
294     uint32_t stride = 0;
295     int i;
296
297     surf = calloc (1, sizeof(struct _tbm_surface));
298     if (!surf)
299         return NULL;
300
301     surf->bufmgr = bufmgr;
302     surf->info.width = width;
303     surf->info.height = height;
304     surf->info.format = format;
305     surf->info.bpp = _tbm_surface_internal_get_bpp (format);
306     surf->info.size = tbm_surface_internal_get_size (surf);
307     surf->info.num_planes = _tbm_surface_internal_get_num_planes(format);
308
309     /* get size, stride and offset */
310     for (i = 0; i < surf->info.num_planes; i++)
311     {
312         tbm_surface_internal_get_plane_data (surf, i, &size, &offset, &stride);
313         surf->info.planes[i].size = size;
314         surf->info.planes[i].offset = offset;
315         surf->info.planes[i].stride = stride;
316     }
317
318     surf->flags = TBM_BO_DEFAULT;
319
320     /* create only one bo */
321     surf->num_bos = num;
322     for (i = 0; i < num; i++)
323     {
324         bos[i] = tbm_bo_alloc (mgr, surf->info.size, TBM_BO_DEFAULT);
325         if (!bos[i])
326             goto bail1;
327         surf->bos[i] = bos[i];
328     }
329
330 //    LIST_ADD (&surf->item_link, &mgr->surf_list);
331
332     return surf;
333 bail1:
334     for (i = 0; i < num; i++)
335     {
336         if (surf->bos[i])
337         {
338             free (surf->bos[i]);
339             surf->bos[i] = NULL;
340         }
341     }
342
343     free (surf);
344     surf = NULL;
345     return NULL;
346 }
347
348
349 int
350 tbm_surface_internal_get_num_bos (tbm_surface_h surface)
351 {
352     TBM_RETURN_VAL_IF_FAIL (surface, 0);
353
354     struct _tbm_surface *surf = (struct _tbm_surface *) surface;
355
356     return surf->num_bos;
357 }
358
359 tbm_bo
360 tbm_surface_internal_get_bo (tbm_surface_h surface, int bo_idx)
361 {
362     TBM_RETURN_VAL_IF_FAIL (surface, NULL);
363     TBM_RETURN_VAL_IF_FAIL (bo_idx > -1, NULL);
364
365     struct _tbm_surface *surf = (struct _tbm_surface *) surface;
366
367     return surf->bos[bo_idx];
368 }
369
370 int
371 tbm_surface_internal_get_size (tbm_surface_h surface)
372 {
373     TBM_RETURN_VAL_IF_FAIL (surface, 0);
374
375     struct _tbm_surface *surf = (struct _tbm_surface *) surface;
376     struct _tbm_bufmgr *mgr = surf->bufmgr;
377     int size = 0;
378
379     TBM_RETURN_VAL_IF_FAIL (surf->info.width > 0, 0);
380     TBM_RETURN_VAL_IF_FAIL (surf->info.height > 0, 0);
381     TBM_RETURN_VAL_IF_FAIL (surf->info.format > 0, 0);
382
383     pthread_mutex_lock (&mgr->lock);
384
385     size = mgr->backend->surface_get_size (surf, surf->info.width, surf->info.height, surf->info.format);
386
387     pthread_mutex_unlock (&mgr->lock);
388
389     return size;
390 }
391
392 int
393 tbm_surface_internal_get_plane_data (tbm_surface_h surface, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch)
394 {
395     TBM_RETURN_VAL_IF_FAIL (surface, 0);
396     TBM_RETURN_VAL_IF_FAIL (plane_idx > -1, 0);
397
398     struct _tbm_surface *surf = (struct _tbm_surface *) surface;
399     struct _tbm_bufmgr *mgr = surf->bufmgr;
400     int ret = 0;
401
402     TBM_RETURN_VAL_IF_FAIL (surf->info.width > 0, 0);
403     TBM_RETURN_VAL_IF_FAIL (surf->info.height > 0, 0);
404     TBM_RETURN_VAL_IF_FAIL (surf->info.format > 0, 0);
405
406     pthread_mutex_lock (&mgr->lock);
407
408     ret = mgr->backend->surface_get_plane_data (surf, surf->info.width, surf->info.height, surf->info.format, plane_idx, size, offset, pitch);
409     if (!ret)
410         return 0;
411
412     pthread_mutex_unlock (&mgr->lock);
413
414     return 1;
415 }
416
417
418