Support TBM_FORMAT_RGB888
[platform/adaptation/emulator/libtbm-vigs.git] / src / tbm_bufmgr_emulator.c
1 /*
2  * buffer manager for libtbm-vigs
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact :
7  * Stanislav Vorobiov <s.vorobiov@samsung.com>
8  * Jinhyung Jo <jinhyung.jo@samsung.com>
9  * Sangho Park <sangho1206.park@samsung.com>
10  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28  * THE SOFTWARE.
29  *
30  * Contributors:
31  * - S-Core Co., Ltd
32  *
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <tbm_bufmgr.h>
40 #include <tbm_bufmgr_backend.h>
41 #include <tbm_surface.h>
42 #include "vigs.h"
43 #include "tbm_emulator_log.h"
44 #include <string.h>
45 #include <stdlib.h>
46
47 static uint32_t tbm_bufmgr_emulator_color_format_list[] =
48 {
49     TBM_FORMAT_RGB888,
50     TBM_FORMAT_ARGB8888,
51     TBM_FORMAT_XRGB8888,
52     TBM_FORMAT_NV21,
53     TBM_FORMAT_NV61,
54     TBM_FORMAT_YUV420,
55 };
56
57 static tbm_bo_handle get_tbm_bo_handle(struct vigs_drm_gem *gem,
58                                        int device)
59 {
60     tbm_bo_handle bo_handle;
61     int ret;
62
63     memset(&bo_handle, 0, sizeof(bo_handle));
64
65     switch (device) {
66     case TBM_DEVICE_DEFAULT:
67     case TBM_DEVICE_2D:
68         bo_handle.u32 = gem->handle;
69         break;
70     case TBM_DEVICE_CPU:
71         ret = vigs_drm_gem_map(gem, 1);
72
73         if (ret == 0) {
74             bo_handle.ptr = gem->vaddr;
75         } else {
76             TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_map failed: %s",
77                                    strerror(-ret));
78         }
79
80         break;
81     case TBM_DEVICE_3D:
82         TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_3D not supported");
83         break;
84     case TBM_DEVICE_MM:
85         TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_MM not supported");
86         break;
87     default:
88         TBM_EMULATOR_LOG_ERROR("%d not supported", device);
89         break;
90     }
91
92     return bo_handle;
93 }
94
95 static void tbm_bufmgr_emulator_deinit(void *priv)
96 {
97     struct vigs_drm_device *drm_dev = priv;
98
99     TBM_EMULATOR_LOG_DEBUG("enter");
100
101     vigs_drm_device_destroy(drm_dev);
102 }
103
104 static int tbm_bufmgr_emulator_bo_size(tbm_bo bo)
105 {
106     struct vigs_drm_surface *sfc;
107
108     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
109
110     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
111
112     return sfc->gem.size;
113 }
114
115 static void *tbm_bufmgr_emulator_bo_alloc(tbm_bo bo, int size, int flags)
116 {
117     struct vigs_drm_device *drm_dev;
118     struct vigs_drm_surface *sfc;
119     uint32_t width = 2048, height;
120     int ret;
121
122     TBM_EMULATOR_LOG_DEBUG("size = %d, flags = 0x%X", size, flags);
123
124     drm_dev = (struct vigs_drm_device*)tbm_backend_get_bufmgr_priv(bo);
125
126     height = ((uint32_t)size + (width * 4) - 1) / (width * 4);
127
128     ret = vigs_drm_surface_create(drm_dev,
129                                   width, height,
130                                   width * 4,
131                                   vigs_drm_surface_bgra8888, 0,
132                                   &sfc);
133
134     if (ret != 0) {
135         TBM_EMULATOR_LOG_ERROR("vigs_drm_suface_create failed: %s",
136                                strerror(-ret));
137         return NULL;
138     }
139
140     return sfc;
141 }
142
143 static void tbm_bufmgr_emulator_bo_free(tbm_bo bo)
144 {
145     struct vigs_drm_surface *sfc;
146
147     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
148
149     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
150
151     vigs_drm_gem_unref(&sfc->gem);
152 }
153
154 static void *tbm_bufmgr_emulator_bo_import(tbm_bo bo, unsigned int key)
155 {
156     struct vigs_drm_device *drm_dev;
157     int ret;
158     struct vigs_drm_surface *sfc;
159
160     TBM_EMULATOR_LOG_DEBUG("bo = %p, key = %u", bo, key);
161
162     drm_dev = (struct vigs_drm_device*)tbm_backend_get_bufmgr_priv(bo);
163
164     ret = vigs_drm_surface_open(drm_dev, key, &sfc);
165
166     if (ret != 0) {
167         TBM_EMULATOR_LOG_ERROR("vigs_drm_surface_open failed for key %u: %s",
168                                key,
169                                strerror(-ret));
170         return NULL;
171     }
172
173     TBM_EMULATOR_LOG_DEBUG("handle = %u", sfc->gem.handle);
174
175     return sfc;
176 }
177
178 static void *tbm_bufmgr_emulator_bo_import_fd(tbm_bo bo, tbm_fd key)
179 {
180     struct vigs_drm_device *drm_dev;
181     struct vigs_drm_surface *sfc;
182     int ret;
183
184     TBM_EMULATOR_LOG_DEBUG("bo = %p, key = %d", bo, key);
185
186     drm_dev = (struct vigs_drm_device *)tbm_backend_get_bufmgr_priv(bo);
187
188     ret = vigs_drm_prime_import_fd(drm_dev, key, &sfc);
189
190     if (ret != 0) {
191         TBM_EMULATOR_LOG_ERROR("vigs_drm_prime_import_fd failed for key %d: %s",
192                                key,
193                                strerror(-ret));
194         return NULL;
195     }
196
197     TBM_EMULATOR_LOG_DEBUG("handle = %u", sfc->gem.handle);
198
199     return sfc;
200 }
201
202
203 static unsigned int tbm_bufmgr_emulator_bo_export(tbm_bo bo)
204 {
205     struct vigs_drm_surface *sfc;
206     int ret;
207
208     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
209
210     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
211
212     ret = vigs_drm_gem_get_name(&sfc->gem);
213
214     if (ret != 0) {
215         TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
216                                strerror(-ret));
217         return 0;
218     }
219
220     return sfc->gem.name;
221 }
222
223 tbm_fd tbm_bufmgr_emulator_bo_export_fd(tbm_bo bo)
224 {
225     struct vigs_drm_device *drm_dev;
226     struct vigs_drm_surface *sfc;
227     int ret, fd = 0;
228
229     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
230
231     drm_dev = (struct vigs_drm_device *)tbm_backend_get_bufmgr_priv(bo);
232     sfc = (struct vigs_drm_surface *)tbm_backend_get_bo_priv(bo);;
233
234     ret = vigs_drm_prime_export_fd(drm_dev, sfc, &fd);
235
236     if (ret != 0) {
237         TBM_EMULATOR_LOG_ERROR("vigs_drm_prime_export_fd failed: %s",
238                                strerror(-ret));
239         return 0;
240     }
241
242     return fd;
243 }
244
245 static tbm_bo_handle tbm_bufmgr_emulator_bo_get_handle(tbm_bo bo, int device)
246 {
247     struct vigs_drm_surface *sfc;
248
249     TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d", bo, device);
250
251     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
252
253     return get_tbm_bo_handle(&sfc->gem, device);
254 }
255
256 static tbm_bo_handle tbm_bufmgr_emulator_bo_map(tbm_bo bo, int device, int opt)
257 {
258     struct vigs_drm_surface *sfc;
259     tbm_bo_handle handle;
260     uint32_t saf = 0;
261
262     TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d, opt = %d", bo, device, opt);
263
264     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
265
266     handle = get_tbm_bo_handle(&sfc->gem, device);
267
268     if (!handle.ptr) {
269         return handle;
270     }
271
272     if ((opt & TBM_OPTION_READ) != 0) {
273         saf |= VIGS_DRM_SAF_READ;
274     }
275
276     if ((opt & TBM_OPTION_WRITE) != 0) {
277         saf |= VIGS_DRM_SAF_WRITE;
278     }
279
280     vigs_drm_surface_start_access(sfc, saf);
281
282     return handle;
283 }
284
285 static int tbm_bufmgr_emulator_bo_unmap(tbm_bo bo)
286 {
287     struct vigs_drm_surface *sfc;
288
289     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
290
291     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
292
293     vigs_drm_surface_end_access(sfc, 1);
294
295     return 1;
296 }
297
298 static int tbm_bufmgr_emulator_bo_cache_flush(tbm_bo bo, int flags)
299 {
300     TBM_EMULATOR_LOG_DEBUG("bo = %p, flags = %d", bo, flags);
301     return 1;
302 }
303
304 static int tbm_bufmgr_emulator_bo_lock(tbm_bo bo)
305 {
306     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
307     return 1;
308 }
309
310 static int tbm_bufmgr_emulator_bo_unlock(tbm_bo bo)
311 {
312     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
313     return 1;
314 }
315
316 static int tbm_bufmgr_emulator_bo_get_global_key(tbm_bo bo)
317 {
318     struct vigs_drm_surface *sfc;
319     int ret;
320
321     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
322
323     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
324
325     ret = vigs_drm_gem_get_name(&sfc->gem);
326
327     if (ret != 0) {
328         TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
329                                strerror(-ret));
330         return 0;
331     }
332
333     return sfc->gem.name;
334 }
335
336 static int tbm_bufmgr_emulator_surface_get_plane_data(tbm_surface_h surface, int width, int height, tbm_format format, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch, int *bo_idx)
337 {
338     *size = 0;
339     *offset = 0;
340     *pitch = 0;
341     *bo_idx = 0;
342
343     switch(format) {
344     case TBM_FORMAT_RGB888:
345         *size = width * height * 3;
346         *offset = 0;
347         *pitch = width * 3;
348         *bo_idx = 0;
349         return 1;
350     case TBM_FORMAT_XRGB8888:
351     case TBM_FORMAT_ARGB8888:
352         *size = width * height * 4;
353         *offset = 0;
354         *pitch = width * 4;
355         *bo_idx = 0;
356         return 1;
357     case TBM_FORMAT_NV21:
358         if (plane_idx == 0) {
359             *size = width * height;
360             *offset = 0;
361             *pitch = width;
362             *bo_idx = 0;
363         } else if (plane_idx == 1) {
364             *size = width * (height >> 1);
365             *offset = width * height;
366             *pitch = width;
367             *bo_idx = 0;
368         } else {
369             return 0;
370         }
371         return 1;
372     case TBM_FORMAT_NV61:
373         if (plane_idx == 0) {
374             *size = width * height;
375             *offset = 0;
376             *pitch = width;
377             *bo_idx = 0;
378         } else if (plane_idx == 1) {
379             *size = width * height;
380             *offset = width * height;
381             *pitch = width;
382             *bo_idx = 0;
383         } else {
384             return 0;
385         }
386         return 1;
387     case TBM_FORMAT_YUV420:
388         if (plane_idx == 0) {
389             *size = width * height;
390             *offset = 0;
391             *pitch = width;
392             *bo_idx = 0;
393         } else if (plane_idx == 1) {
394             *size = (width * height) >> 2;
395             *offset = width * height;
396             *pitch = width >> 1 ;
397             *bo_idx = 0;
398         } else if (plane_idx == 2) {
399             *size = (width * height) >> 2;
400             *offset = (width * height) + (width  * height >> 2);
401             *pitch = width >> 1;
402             *bo_idx = 0;
403         } else {
404             return 0;
405         }
406         return 1;
407     default:
408         return 0;
409     }
410 }
411
412 static int tbm_bufmgr_emulator_surface_get_size(tbm_surface_h surface, int width, int height, tbm_format format)
413 {
414     int bpp;
415
416     switch(format) {
417     case TBM_FORMAT_RGB888:
418         bpp = 24;
419         break;
420     case TBM_FORMAT_XRGB8888:
421     case TBM_FORMAT_ARGB8888:
422         bpp = 32;
423         break;
424     /* NV21 : Y/CrCb 4:2:0 */
425     /* YUV420 : YUV 4:2:0 */
426     case TBM_FORMAT_NV21:
427     case TBM_FORMAT_YUV420:
428         bpp = 12;
429         break;
430     /* NV61 : Y/CrCb 4:2:2 */
431     case TBM_FORMAT_NV61:
432         bpp = 16;
433         break;
434     default:
435         return 0;
436     }
437     return (width * height * bpp) >> 3;
438 }
439
440 static int tbm_bufmgr_emulator_surface_get_num_bos (tbm_format format)
441 {
442     int num = 0;
443
444     switch(format) {
445     case TBM_FORMAT_RGB888:
446     case TBM_FORMAT_XRGB8888:
447     case TBM_FORMAT_ARGB8888:
448     /* NV21 : Y/CrCb 4:2:0 */
449     /* YUV420 : YUV 4:2:0 */
450     case TBM_FORMAT_NV21:
451     case TBM_FORMAT_YUV420:
452     /* NV61 : Y/CrCb 4:2:2 */
453     case TBM_FORMAT_NV61:
454         num = 1;
455         break;
456     default:
457         return 0;
458     }
459     return num;
460 }
461
462 static int tbm_bufmgr_emulator_surface_supported_format(uint32_t **formats, uint32_t *num)
463 {
464     uint32_t *color_formats;
465
466     color_formats = (uint32_t*)calloc(1, sizeof(tbm_bufmgr_emulator_color_format_list));
467
468     if (!color_formats) {
469         return 0;
470     }
471
472     memcpy(color_formats,
473            tbm_bufmgr_emulator_color_format_list,
474            sizeof(tbm_bufmgr_emulator_color_format_list));
475
476     *formats = color_formats;
477     *num = sizeof(tbm_bufmgr_emulator_color_format_list)/sizeof(tbm_bufmgr_emulator_color_format_list[0]);
478
479     return 1;
480 }
481
482
483 MODULEINITPPROTO(tbm_bufmgr_emulator_init);
484
485 static TBMModuleVersionInfo EmulatorVersRec =
486 {
487     "emulator",
488     "Samsung",
489     TBM_ABI_VERSION,
490 };
491
492 TBMModuleData tbmModuleData = { &EmulatorVersRec, tbm_bufmgr_emulator_init };
493
494 int tbm_bufmgr_emulator_init(tbm_bufmgr bufmgr, int fd)
495 {
496     int ret = 0;
497     struct vigs_drm_device *drm_dev = NULL;
498     tbm_bufmgr_backend backend = NULL;
499
500     TBM_EMULATOR_LOG_DEBUG("enter");
501
502     if (!bufmgr) {
503         return 0;
504     }
505
506     ret = vigs_drm_device_create(fd, &drm_dev);
507
508     if (ret != 0) {
509         TBM_EMULATOR_LOG_ERROR("vigs_drm_device_create failed: %s", strerror(-ret));
510         goto fail;
511     }
512
513     backend = tbm_backend_alloc();
514
515     if (!backend) {
516         TBM_EMULATOR_LOG_ERROR("tbm_backend_alloc failed");
517         goto fail;
518     }
519
520     backend->flags = TBM_CACHE_CTRL_BACKEND|TBM_LOCK_CTRL_BACKEND;
521     backend->priv = (void*)drm_dev;
522     backend->bufmgr_deinit = tbm_bufmgr_emulator_deinit;
523     backend->bo_size = tbm_bufmgr_emulator_bo_size;
524     backend->bo_alloc = tbm_bufmgr_emulator_bo_alloc;
525     backend->bo_free = tbm_bufmgr_emulator_bo_free;
526     backend->bo_import = tbm_bufmgr_emulator_bo_import;
527     backend->bo_import_fd = tbm_bufmgr_emulator_bo_import_fd;
528     backend->bo_export = tbm_bufmgr_emulator_bo_export;
529     backend->bo_export_fd = tbm_bufmgr_emulator_bo_export_fd;
530     backend->bo_get_handle = tbm_bufmgr_emulator_bo_get_handle;
531     backend->bo_map = tbm_bufmgr_emulator_bo_map;
532     backend->bo_unmap = tbm_bufmgr_emulator_bo_unmap;
533     backend->bo_cache_flush = tbm_bufmgr_emulator_bo_cache_flush;
534     backend->bo_get_global_key = tbm_bufmgr_emulator_bo_get_global_key;
535     backend->bo_lock = tbm_bufmgr_emulator_bo_lock;
536     backend->bo_unlock = tbm_bufmgr_emulator_bo_unlock;
537     backend->surface_get_plane_data = tbm_bufmgr_emulator_surface_get_plane_data;
538     backend->surface_get_size = tbm_bufmgr_emulator_surface_get_size;
539     backend->surface_supported_format = tbm_bufmgr_emulator_surface_supported_format;
540     backend->surface_get_num_bos = tbm_bufmgr_emulator_surface_get_num_bos;
541
542     if (!tbm_backend_init(bufmgr, backend)) {
543         TBM_EMULATOR_LOG_ERROR("tbm_backend_init failed");
544         goto fail;
545     }
546
547     TBM_EMULATOR_LOG_INFO("initialized");
548
549     return 1;
550
551 fail:
552     if (backend) {
553         tbm_backend_free(backend);
554     }
555
556     if (drm_dev) {
557         vigs_drm_device_destroy(drm_dev);
558     }
559
560     return 0;
561 }