2 * buffer manager for libtbm-vigs
4 * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
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>
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:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
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
39 #include <tbm_bufmgr.h>
40 #include <tbm_bufmgr_backend.h>
41 #include <tbm_surface.h>
43 #include "tbm_emulator_log.h"
47 static uint32_t tbm_bufmgr_emulator_color_format_list[] =
56 static tbm_bo_handle get_tbm_bo_handle(struct vigs_drm_gem *gem,
59 tbm_bo_handle bo_handle;
62 memset(&bo_handle, 0, sizeof(bo_handle));
65 case TBM_DEVICE_DEFAULT:
67 bo_handle.u32 = gem->handle;
70 ret = vigs_drm_gem_map(gem, 1);
73 bo_handle.ptr = gem->vaddr;
75 TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_map failed: %s",
81 TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_3D not supported");
84 TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_MM not supported");
87 TBM_EMULATOR_LOG_ERROR("%d not supported", device);
94 static void tbm_bufmgr_emulator_deinit(void *priv)
96 struct vigs_drm_device *drm_dev = priv;
98 TBM_EMULATOR_LOG_DEBUG("enter");
100 vigs_drm_device_destroy(drm_dev);
103 static int tbm_bufmgr_emulator_bo_size(tbm_bo bo)
105 struct vigs_drm_surface *sfc;
107 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
109 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
111 return sfc->gem.size;
114 static void *tbm_bufmgr_emulator_bo_alloc(tbm_bo bo, int size, int flags)
116 struct vigs_drm_device *drm_dev;
117 struct vigs_drm_surface *sfc;
118 uint32_t width = 2048, height;
121 TBM_EMULATOR_LOG_DEBUG("size = %d, flags = 0x%X", size, flags);
123 drm_dev = (struct vigs_drm_device*)tbm_backend_get_bufmgr_priv(bo);
125 height = ((uint32_t)size + (width * 4) - 1) / (width * 4);
127 ret = vigs_drm_surface_create(drm_dev,
130 vigs_drm_surface_bgra8888, 0,
134 TBM_EMULATOR_LOG_ERROR("vigs_drm_suface_create failed: %s",
142 static void tbm_bufmgr_emulator_bo_free(tbm_bo bo)
144 struct vigs_drm_surface *sfc;
146 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
148 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
150 vigs_drm_gem_unref(&sfc->gem);
153 static void *tbm_bufmgr_emulator_bo_import(tbm_bo bo, unsigned int key)
155 struct vigs_drm_device *drm_dev;
157 struct vigs_drm_surface *sfc;
159 TBM_EMULATOR_LOG_DEBUG("bo = %p, key = %u", bo, key);
161 drm_dev = (struct vigs_drm_device*)tbm_backend_get_bufmgr_priv(bo);
163 ret = vigs_drm_surface_open(drm_dev, key, &sfc);
166 TBM_EMULATOR_LOG_ERROR("vigs_drm_surface_open failed for key %u: %s",
172 TBM_EMULATOR_LOG_DEBUG("handle = %u", sfc->gem.handle);
177 static unsigned int tbm_bufmgr_emulator_bo_export(tbm_bo bo)
179 struct vigs_drm_surface *sfc;
182 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
184 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
186 ret = vigs_drm_gem_get_name(&sfc->gem);
189 TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
194 return sfc->gem.name;
197 static tbm_bo_handle tbm_bufmgr_emulator_bo_get_handle(tbm_bo bo, int device)
199 struct vigs_drm_surface *sfc;
201 TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d", bo, device);
203 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
205 return get_tbm_bo_handle(&sfc->gem, device);
208 static tbm_bo_handle tbm_bufmgr_emulator_bo_map(tbm_bo bo, int device, int opt)
210 struct vigs_drm_surface *sfc;
211 tbm_bo_handle handle;
214 TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d, opt = %d", bo, device, opt);
216 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
218 handle = get_tbm_bo_handle(&sfc->gem, device);
224 if ((opt & TBM_OPTION_READ) != 0) {
225 saf |= VIGS_DRM_SAF_READ;
228 if ((opt & TBM_OPTION_WRITE) != 0) {
229 saf |= VIGS_DRM_SAF_WRITE;
232 vigs_drm_surface_start_access(sfc, saf);
237 static int tbm_bufmgr_emulator_bo_unmap(tbm_bo bo)
239 struct vigs_drm_surface *sfc;
241 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
243 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
245 vigs_drm_surface_end_access(sfc, 1);
250 static int tbm_bufmgr_emulator_bo_cache_flush(tbm_bo bo, int flags)
252 TBM_EMULATOR_LOG_DEBUG("bo = %p, flags = %d", bo, flags);
256 static int tbm_bufmgr_emulator_bo_lock(tbm_bo bo)
258 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
262 static int tbm_bufmgr_emulator_bo_unlock(tbm_bo bo)
264 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
268 static int tbm_bufmgr_emulator_bo_get_global_key(tbm_bo bo)
270 struct vigs_drm_surface *sfc;
273 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
275 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
277 ret = vigs_drm_gem_get_name(&sfc->gem);
280 TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
285 return sfc->gem.name;
288 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)
295 case TBM_FORMAT_XRGB8888:
296 case TBM_FORMAT_ARGB8888:
297 *size = width * height * 4;
301 case TBM_FORMAT_NV21:
302 if (plane_idx == 0) {
303 *size = width * height;
306 } else if (plane_idx == 1) {
307 *size = width * (height >> 1);
308 *offset = width * height;
314 case TBM_FORMAT_NV61:
315 if (plane_idx == 0) {
316 *size = width * height;
319 } else if (plane_idx == 1) {
320 *size = width * height;
321 *offset = width * height;
327 case TBM_FORMAT_YUV420:
328 if (plane_idx == 0) {
329 *size = width * height;
332 } else if (plane_idx == 1) {
333 *size = (width * height) >> 2;
334 *offset = width * height;
335 *pitch = width >> 1 ;
336 } else if (plane_idx == 2) {
337 *size = (width * height) >> 2;
338 *offset = (width * height) + (width * height >> 2);
349 static int tbm_bufmgr_emulator_surface_get_size(tbm_surface_h surface, int width, int height, tbm_format format)
354 case TBM_FORMAT_XRGB8888:
355 case TBM_FORMAT_ARGB8888:
358 /* NV21 : Y/CrCb 4:2:0 */
359 /* YUV420 : YUV 4:2:0 */
360 case TBM_FORMAT_NV21:
361 case TBM_FORMAT_YUV420:
364 /* NV61 : Y/CrCb 4:2:2 */
365 case TBM_FORMAT_NV61:
371 return (width * height * bpp) >> 3;
374 static int tbm_bufmgr_emulator_surface_supported_format(uint32_t **formats, uint32_t *num)
376 uint32_t *color_formats;
378 color_formats = (uint32_t*)calloc(1, sizeof(tbm_bufmgr_emulator_color_format_list));
380 if (!color_formats) {
384 memcpy(color_formats,
385 tbm_bufmgr_emulator_color_format_list,
386 sizeof(tbm_bufmgr_emulator_color_format_list));
388 *formats = color_formats;
389 *num = sizeof(tbm_bufmgr_emulator_color_format_list)/sizeof(tbm_bufmgr_emulator_color_format_list[0]);
395 MODULEINITPPROTO(tbm_bufmgr_emulator_init);
397 static TBMModuleVersionInfo EmulatorVersRec =
404 TBMModuleData tbmModuleData = { &EmulatorVersRec, tbm_bufmgr_emulator_init };
406 int tbm_bufmgr_emulator_init(tbm_bufmgr bufmgr, int fd)
409 struct vigs_drm_device *drm_dev = NULL;
410 tbm_bufmgr_backend backend = NULL;
412 TBM_EMULATOR_LOG_DEBUG("enter");
418 ret = vigs_drm_device_create(fd, &drm_dev);
421 TBM_EMULATOR_LOG_ERROR("vigs_drm_device_create failed: %s", strerror(-ret));
425 backend = tbm_backend_alloc();
428 TBM_EMULATOR_LOG_ERROR("tbm_backend_alloc failed");
432 backend->flags = TBM_CACHE_CTRL_BACKEND|TBM_LOCK_CTRL_BACKEND;
433 backend->priv = (void*)drm_dev;
434 backend->bufmgr_deinit = tbm_bufmgr_emulator_deinit;
435 backend->bo_size = tbm_bufmgr_emulator_bo_size;
436 backend->bo_alloc = tbm_bufmgr_emulator_bo_alloc;
437 backend->bo_free = tbm_bufmgr_emulator_bo_free;
438 backend->bo_import = tbm_bufmgr_emulator_bo_import;
439 backend->bo_export = tbm_bufmgr_emulator_bo_export;
440 backend->bo_get_handle = tbm_bufmgr_emulator_bo_get_handle;
441 backend->bo_map = tbm_bufmgr_emulator_bo_map;
442 backend->bo_unmap = tbm_bufmgr_emulator_bo_unmap;
443 backend->bo_cache_flush = tbm_bufmgr_emulator_bo_cache_flush;
444 backend->bo_get_global_key = tbm_bufmgr_emulator_bo_get_global_key;
445 backend->bo_lock = tbm_bufmgr_emulator_bo_lock;
446 backend->bo_unlock = tbm_bufmgr_emulator_bo_unlock;
447 backend->surface_get_plane_data = tbm_bufmgr_emulator_surface_get_plane_data;
448 backend->surface_get_size = tbm_bufmgr_emulator_surface_get_size;
449 backend->surface_supported_format = tbm_bufmgr_emulator_surface_supported_format;
451 if (!tbm_backend_init(bufmgr, backend)) {
452 TBM_EMULATOR_LOG_ERROR("tbm_backend_init failed");
456 TBM_EMULATOR_LOG_INFO("initialized");
462 tbm_backend_free(backend);
466 vigs_drm_device_destroy(drm_dev);