1 /**************************************************************************
5 Copyright 2013 Samsung Electronics co., Ltd. All Rights Reserved.
8 Stanislav Vorobiov <s.vorobiov@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 **************************************************************************/
36 #include <tbm_bufmgr.h>
37 #include <tbm_bufmgr_backend.h>
39 #include "tbm_emulator_log.h"
43 static tbm_bo_handle get_tbm_bo_handle(struct vigs_drm_gem *gem,
46 tbm_bo_handle bo_handle;
49 memset(&bo_handle, 0, sizeof(bo_handle));
52 case TBM_DEVICE_DEFAULT:
54 bo_handle.u32 = gem->handle;
57 ret = vigs_drm_gem_map(gem, 1);
60 bo_handle.ptr = gem->vaddr;
62 TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_map failed: %s",
68 TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_3D not supported");
71 TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_MM not supported");
74 TBM_EMULATOR_LOG_ERROR("%d not supported", device);
81 static void tbm_bufmgr_emulator_deinit(void *priv)
83 struct vigs_drm_device *drm_dev = priv;
85 TBM_EMULATOR_LOG_DEBUG("enter");
87 vigs_drm_device_destroy(drm_dev);
90 static int tbm_bufmgr_emulator_bo_size(tbm_bo bo)
92 struct vigs_drm_surface *sfc;
94 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
96 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
101 static void *tbm_bufmgr_emulator_bo_alloc(tbm_bo bo, int size, int flags)
103 TBM_EMULATOR_LOG_ERROR("not supported");
107 static void tbm_bufmgr_emulator_bo_free(tbm_bo bo)
109 struct vigs_drm_surface *sfc;
111 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
113 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
115 vigs_drm_gem_unref(&sfc->gem);
118 static void *tbm_bufmgr_emulator_bo_import(tbm_bo bo, unsigned int key)
120 struct vigs_drm_device *drm_dev;
122 struct vigs_drm_surface *sfc;
124 TBM_EMULATOR_LOG_DEBUG("bo = %p, key = %u", bo, key);
126 drm_dev = (struct vigs_drm_device*)tbm_backend_get_bufmgr_priv(bo);
128 ret = vigs_drm_surface_open(drm_dev, key, &sfc);
131 TBM_EMULATOR_LOG_ERROR("vigs_drm_surface_open failed for key %u: %s",
137 TBM_EMULATOR_LOG_DEBUG("handle = %u", sfc->gem.handle);
142 static unsigned int tbm_bufmgr_emulator_bo_export(tbm_bo bo)
144 struct vigs_drm_surface *sfc;
147 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
149 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
151 ret = vigs_drm_gem_get_name(&sfc->gem);
154 TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
159 return sfc->gem.name;
162 static tbm_bo_handle tbm_bufmgr_emulator_bo_get_handle(tbm_bo bo, int device)
164 struct vigs_drm_surface *sfc;
166 TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d", bo, device);
168 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
170 return get_tbm_bo_handle(&sfc->gem, device);
173 static tbm_bo_handle tbm_bufmgr_emulator_bo_map(tbm_bo bo, int device, int opt)
175 struct vigs_drm_surface *sfc;
176 tbm_bo_handle handle;
179 TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d, opt = %d", bo, device, opt);
181 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
183 handle = get_tbm_bo_handle(&sfc->gem, device);
189 if ((opt & TBM_OPTION_READ) != 0) {
190 saf |= VIGS_DRM_SAF_READ;
193 if ((opt & TBM_OPTION_WRITE) != 0) {
194 saf |= VIGS_DRM_SAF_WRITE;
197 vigs_drm_surface_start_access(sfc, saf);
202 static int tbm_bufmgr_emulator_bo_unmap(tbm_bo bo)
204 struct vigs_drm_surface *sfc;
206 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
208 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
210 vigs_drm_surface_end_access(sfc, 1);
215 static int tbm_bufmgr_emulator_bo_cache_flush(tbm_bo bo, int flags)
217 TBM_EMULATOR_LOG_DEBUG("bo = %p, flags = %d", bo, flags);
221 static int tbm_bufmgr_emulator_bo_lock(tbm_bo bo)
223 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
227 static int tbm_bufmgr_emulator_bo_unlock(tbm_bo bo)
229 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
233 static int tbm_bufmgr_emulator_bo_get_global_key(tbm_bo bo)
235 struct vigs_drm_surface *sfc;
238 TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
240 sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
242 ret = vigs_drm_gem_get_name(&sfc->gem);
245 TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
250 return sfc->gem.name;
253 MODULEINITPPROTO(tbm_bufmgr_emulator_init);
255 static TBMModuleVersionInfo EmulatorVersRec =
262 TBMModuleData tbmModuleData = { &EmulatorVersRec, tbm_bufmgr_emulator_init };
264 int tbm_bufmgr_emulator_init(tbm_bufmgr bufmgr, int fd)
267 struct vigs_drm_device *drm_dev = NULL;
268 tbm_bufmgr_backend backend = NULL;
270 TBM_EMULATOR_LOG_DEBUG("enter");
276 ret = vigs_drm_device_create(fd, &drm_dev);
279 TBM_EMULATOR_LOG_ERROR("vigs_drm_device_create failed: %s", strerror(-ret));
283 backend = tbm_backend_alloc();
286 TBM_EMULATOR_LOG_ERROR("tbm_backend_alloc failed");
290 backend->flags = TBM_CACHE_CTRL_BACKEND|TBM_LOCK_CTRL_BACKEND;
291 backend->priv = (void*)drm_dev;
292 backend->bufmgr_deinit = tbm_bufmgr_emulator_deinit;
293 backend->bo_size = tbm_bufmgr_emulator_bo_size;
294 backend->bo_alloc = tbm_bufmgr_emulator_bo_alloc;
295 backend->bo_free = tbm_bufmgr_emulator_bo_free;
296 backend->bo_import = tbm_bufmgr_emulator_bo_import;
297 backend->bo_export = tbm_bufmgr_emulator_bo_export;
298 backend->bo_get_handle = tbm_bufmgr_emulator_bo_get_handle;
299 backend->bo_map = tbm_bufmgr_emulator_bo_map;
300 backend->bo_unmap = tbm_bufmgr_emulator_bo_unmap;
301 backend->bo_cache_flush = tbm_bufmgr_emulator_bo_cache_flush;
302 backend->bo_get_global_key = tbm_bufmgr_emulator_bo_get_global_key;
303 backend->bo_lock = tbm_bufmgr_emulator_bo_lock;
304 backend->bo_unlock = tbm_bufmgr_emulator_bo_unlock;
306 if (!tbm_backend_init(bufmgr, backend)) {
307 TBM_EMULATOR_LOG_ERROR("tbm_backend_init failed");
311 TBM_EMULATOR_LOG_INFO("initialized");
317 tbm_backend_free(backend);
321 vigs_drm_device_destroy(drm_dev);