Share master fd with tdm backend and open fd if it needed
[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 <tbm_drm_helper.h>
43 #include "vigs.h"
44 #include "tbm_emulator_log.h"
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <xf86drm.h>
50
51 #define VIGS_DRM_NAME "vigs"
52
53 static uint32_t tbm_bufmgr_emulator_color_format_list[] =
54 {
55     TBM_FORMAT_RGB888,
56     TBM_FORMAT_ARGB8888,
57     TBM_FORMAT_XRGB8888,
58     TBM_FORMAT_NV21,
59     TBM_FORMAT_NV61,
60     TBM_FORMAT_YUV420,
61 };
62
63 static int _tbm_vigs_open_drm(void)
64 {
65     int fd = -1;
66
67     fd = drmOpen(VIGS_DRM_NAME, NULL);
68     if (fd < 0) {
69         TBM_EMULATOR_LOG_ERROR ("open vigs drm device failed");
70         return -1;
71     }
72
73     return fd;
74 }
75
76 static tbm_bo_handle get_tbm_bo_handle(struct vigs_drm_gem *gem,
77                                        int device)
78 {
79     tbm_bo_handle bo_handle;
80     int ret;
81
82     memset(&bo_handle, 0, sizeof(bo_handle));
83
84     switch (device) {
85     case TBM_DEVICE_DEFAULT:
86     case TBM_DEVICE_2D:
87         bo_handle.u32 = gem->handle;
88         break;
89     case TBM_DEVICE_CPU:
90         ret = vigs_drm_gem_map(gem, 1);
91
92         if (ret == 0) {
93             bo_handle.ptr = gem->vaddr;
94         } else {
95             TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_map failed: %s",
96                                    strerror(-ret));
97         }
98
99         break;
100     case TBM_DEVICE_3D:
101         TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_3D not supported");
102         break;
103     case TBM_DEVICE_MM:
104         TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_MM not supported");
105         break;
106     default:
107         TBM_EMULATOR_LOG_ERROR("%d not supported", device);
108         break;
109     }
110
111     return bo_handle;
112 }
113
114 static void tbm_bufmgr_emulator_deinit(void *priv)
115 {
116     struct vigs_drm_device *drm_dev = priv;
117
118     TBM_EMULATOR_LOG_DEBUG("enter");
119
120     if (tbm_backend_is_display_server()) {
121         tbm_drm_helper_wl_auth_server_deinit();
122         tbm_drm_helper_unset_tbm_master_fd();
123     }
124
125     close(drm_dev->fd);
126
127     vigs_drm_device_destroy(drm_dev);
128 }
129
130 static int tbm_bufmgr_emulator_bo_size(tbm_bo bo)
131 {
132     struct vigs_drm_surface *sfc;
133
134     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
135
136     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
137
138     return sfc->gem.size;
139 }
140
141 static void *tbm_bufmgr_emulator_bo_alloc(tbm_bo bo, int size, int flags)
142 {
143     struct vigs_drm_device *drm_dev;
144     struct vigs_drm_surface *sfc;
145     uint32_t width = 2048, height;
146     int ret;
147
148     TBM_EMULATOR_LOG_DEBUG("size = %d, flags = 0x%X", size, flags);
149
150     drm_dev = (struct vigs_drm_device*)tbm_backend_get_bufmgr_priv(bo);
151
152     height = ((uint32_t)size + (width * 4) - 1) / (width * 4);
153
154     ret = vigs_drm_surface_create(drm_dev,
155                                   width, height,
156                                   width * 4,
157                                   vigs_drm_surface_bgra8888, 0,
158                                   &sfc);
159
160     if (ret != 0) {
161         TBM_EMULATOR_LOG_ERROR("vigs_drm_suface_create failed: %s",
162                                strerror(-ret));
163         return NULL;
164     }
165
166     return sfc;
167 }
168
169 static void tbm_bufmgr_emulator_bo_free(tbm_bo bo)
170 {
171     struct vigs_drm_surface *sfc;
172
173     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
174
175     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
176
177     vigs_drm_gem_unref(&sfc->gem);
178 }
179
180 static void *tbm_bufmgr_emulator_bo_import(tbm_bo bo, unsigned int key)
181 {
182     struct vigs_drm_device *drm_dev;
183     int ret;
184     struct vigs_drm_surface *sfc;
185
186     TBM_EMULATOR_LOG_DEBUG("bo = %p, key = %u", bo, key);
187
188     drm_dev = (struct vigs_drm_device*)tbm_backend_get_bufmgr_priv(bo);
189
190     ret = vigs_drm_surface_open(drm_dev, key, &sfc);
191
192     if (ret != 0) {
193         TBM_EMULATOR_LOG_ERROR("vigs_drm_surface_open failed for key %u: %s",
194                                key,
195                                strerror(-ret));
196         return NULL;
197     }
198
199     TBM_EMULATOR_LOG_DEBUG("handle = %u", sfc->gem.handle);
200
201     return sfc;
202 }
203
204 static void *tbm_bufmgr_emulator_bo_import_fd(tbm_bo bo, tbm_fd key)
205 {
206     struct vigs_drm_device *drm_dev;
207     struct vigs_drm_surface *sfc;
208     int ret;
209
210     TBM_EMULATOR_LOG_DEBUG("bo = %p, key = %d", bo, key);
211
212     drm_dev = (struct vigs_drm_device *)tbm_backend_get_bufmgr_priv(bo);
213
214     ret = vigs_drm_prime_import_fd(drm_dev, key, &sfc);
215
216     if (ret != 0) {
217         TBM_EMULATOR_LOG_ERROR("vigs_drm_prime_import_fd failed for key %d: %s",
218                                key,
219                                strerror(-ret));
220         return NULL;
221     }
222
223     TBM_EMULATOR_LOG_DEBUG("handle = %u", sfc->gem.handle);
224
225     return sfc;
226 }
227
228
229 static unsigned int tbm_bufmgr_emulator_bo_export(tbm_bo bo)
230 {
231     struct vigs_drm_surface *sfc;
232     int ret;
233
234     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
235
236     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
237
238     ret = vigs_drm_gem_get_name(&sfc->gem);
239
240     if (ret != 0) {
241         TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
242                                strerror(-ret));
243         return 0;
244     }
245
246     return sfc->gem.name;
247 }
248
249 tbm_fd tbm_bufmgr_emulator_bo_export_fd(tbm_bo bo)
250 {
251     struct vigs_drm_device *drm_dev;
252     struct vigs_drm_surface *sfc;
253     int ret, fd = 0;
254
255     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
256
257     drm_dev = (struct vigs_drm_device *)tbm_backend_get_bufmgr_priv(bo);
258     sfc = (struct vigs_drm_surface *)tbm_backend_get_bo_priv(bo);;
259
260     ret = vigs_drm_prime_export_fd(drm_dev, sfc, &fd);
261
262     if (ret != 0) {
263         TBM_EMULATOR_LOG_ERROR("vigs_drm_prime_export_fd failed: %s",
264                                strerror(-ret));
265         return 0;
266     }
267
268     return fd;
269 }
270
271 static tbm_bo_handle tbm_bufmgr_emulator_bo_get_handle(tbm_bo bo, int device)
272 {
273     struct vigs_drm_surface *sfc;
274
275     TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d", bo, device);
276
277     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
278
279     return get_tbm_bo_handle(&sfc->gem, device);
280 }
281
282 static tbm_bo_handle tbm_bufmgr_emulator_bo_map(tbm_bo bo, int device, int opt)
283 {
284     struct vigs_drm_surface *sfc;
285     tbm_bo_handle handle;
286     uint32_t saf = 0;
287
288     TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d, opt = %d", bo, device, opt);
289
290     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
291
292     handle = get_tbm_bo_handle(&sfc->gem, device);
293
294     if (!handle.ptr) {
295         return handle;
296     }
297
298     if ((opt & TBM_OPTION_READ) != 0) {
299         saf |= VIGS_DRM_SAF_READ;
300     }
301
302     if ((opt & TBM_OPTION_WRITE) != 0) {
303         saf |= VIGS_DRM_SAF_WRITE;
304     }
305
306     vigs_drm_surface_start_access(sfc, saf);
307
308     return handle;
309 }
310
311 static int tbm_bufmgr_emulator_bo_unmap(tbm_bo bo)
312 {
313     struct vigs_drm_surface *sfc;
314
315     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
316
317     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
318
319     vigs_drm_surface_end_access(sfc, 1);
320
321     return 1;
322 }
323
324 static int tbm_bufmgr_emulator_bo_lock(tbm_bo bo, int device, int opt)
325 {
326     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
327     return 1;
328 }
329
330 static int tbm_bufmgr_emulator_bo_unlock(tbm_bo bo)
331 {
332     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
333     return 1;
334 }
335
336 static int tbm_bufmgr_emulator_surface_get_plane_data(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_supported_format(uint32_t **formats, uint32_t *num)
413 {
414     uint32_t *color_formats;
415
416     color_formats = (uint32_t*)calloc(1, sizeof(tbm_bufmgr_emulator_color_format_list));
417
418     if (!color_formats) {
419         return 0;
420     }
421
422     memcpy(color_formats,
423            tbm_bufmgr_emulator_color_format_list,
424            sizeof(tbm_bufmgr_emulator_color_format_list));
425
426     *formats = color_formats;
427     *num = sizeof(tbm_bufmgr_emulator_color_format_list)/sizeof(tbm_bufmgr_emulator_color_format_list[0]);
428
429     return 1;
430 }
431
432 static int
433 tbm_bufmgr_emulator_bind_native_display (tbm_bufmgr bufmgr, void *native_display)
434 {
435     struct vigs_drm_device *drm_dev;
436     char *device_name;
437     int ret = 0;
438
439     drm_dev = (struct vigs_drm_device *)tbm_backend_get_priv_from_bufmgr(bufmgr);
440
441     device_name = drmGetDeviceNameFromFd(drm_dev->fd);
442
443     if (!device_name) {
444         TBM_EMULATOR_LOG_ERROR("drmGetDeviceNameFromFd failed");
445         return 0;
446     }
447
448     ret = tbm_drm_helper_wl_auth_server_init(native_display, drm_dev->fd, device_name, 0);
449
450     if (!ret) {
451         TBM_EMULATOR_LOG_ERROR("tbm_drm_helper_wl_auth_server_init failed");
452         free(device_name);
453         return 0;
454     }
455
456     free(device_name);
457
458     return 1;
459 }
460
461 MODULEINITPPROTO(tbm_bufmgr_emulator_init);
462
463 static TBMModuleVersionInfo EmulatorVersRec =
464 {
465     "emulator",
466     "Samsung",
467     TBM_ABI_VERSION,
468 };
469
470 TBMModuleData tbmModuleData = { &EmulatorVersRec, tbm_bufmgr_emulator_init };
471
472 int tbm_bufmgr_emulator_init(tbm_bufmgr bufmgr, int fd)
473 {
474     int ret = 0;
475     struct vigs_drm_device *drm_dev = NULL;
476     tbm_bufmgr_backend backend = NULL;
477     int drm_fd = -1;
478
479     TBM_EMULATOR_LOG_DEBUG("enter");
480
481     if (!bufmgr) {
482         return 0;
483     }
484
485     if (tbm_backend_is_display_server()) {
486         drm_fd = tbm_drm_helper_get_master_fd();
487
488         if (drm_fd < 0) {
489             drm_fd = _tbm_vigs_open_drm();
490         }
491
492         if (drm_fd < 0) {
493             TBM_EMULATOR_LOG_ERROR ("vigs drm device failed");
494             goto fail;
495         }
496
497         tbm_drm_helper_set_tbm_master_fd(drm_fd);
498
499     } else {
500         if (!tbm_drm_helper_get_auth_info(&drm_fd, NULL, NULL)) {
501             TBM_EMULATOR_LOG_ERROR ("tbm_drm_helper_get_auth_info failed");
502             goto fail;
503         }
504     }
505
506     ret = vigs_drm_device_create(drm_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->priv = (void*)drm_dev;
521     backend->bufmgr_deinit = tbm_bufmgr_emulator_deinit;
522     backend->bo_size = tbm_bufmgr_emulator_bo_size;
523     backend->bo_alloc = tbm_bufmgr_emulator_bo_alloc;
524     backend->bo_free = tbm_bufmgr_emulator_bo_free;
525     backend->bo_import = tbm_bufmgr_emulator_bo_import;
526     backend->bo_import_fd = tbm_bufmgr_emulator_bo_import_fd;
527     backend->bo_export = tbm_bufmgr_emulator_bo_export;
528     backend->bo_export_fd = tbm_bufmgr_emulator_bo_export_fd;
529     backend->bo_get_handle = tbm_bufmgr_emulator_bo_get_handle;
530     backend->bo_map = tbm_bufmgr_emulator_bo_map;
531     backend->bo_unmap = tbm_bufmgr_emulator_bo_unmap;
532     backend->bo_lock = tbm_bufmgr_emulator_bo_lock;
533     backend->bo_unlock = tbm_bufmgr_emulator_bo_unlock;
534     backend->surface_get_plane_data = tbm_bufmgr_emulator_surface_get_plane_data;
535     backend->surface_supported_format = tbm_bufmgr_emulator_surface_supported_format;
536     backend->bufmgr_bind_native_display = tbm_bufmgr_emulator_bind_native_display;
537
538     if (!tbm_backend_init(bufmgr, backend)) {
539         TBM_EMULATOR_LOG_ERROR("tbm_backend_init failed");
540         goto fail;
541     }
542
543     TBM_EMULATOR_LOG_INFO("initialized");
544
545     return 1;
546
547 fail:
548     if (backend) {
549         tbm_backend_free(backend);
550     }
551
552     if (drm_dev) {
553         vigs_drm_device_destroy(drm_dev);
554     }
555
556     if (drm_fd >= 0) {
557         if (tbm_backend_is_display_server()) {
558             tbm_drm_helper_unset_tbm_master_fd();
559         }
560
561         close(drm_fd);
562     }
563
564     return 0;
565 }