tizen 2.4 release
[sdk/emulator-yagl.git] / EGL / yagl_native_display.c
1 /*
2  * YaGL
3  *
4  * Copyright (c) 2012 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  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  * THE SOFTWARE.
28  *
29  * Contributors:
30  * - S-Core Co., Ltd
31  *
32  */
33
34 #include "yagl_native_display.h"
35 #include "yagl_log.h"
36 #include "yagl_backend.h"
37 #ifdef YAGL_PLATFORM_WAYLAND
38 #include "wayland-drm.h"
39 #endif
40 #include "vigs.h"
41 #include "EGL/eglext.h"
42 #include "EGL/eglmesaext.h"
43 #include <string.h>
44 #include <stdlib.h>
45
46 void yagl_native_display_init(struct yagl_native_display *dpy,
47                               struct yagl_native_platform *platform,
48                               yagl_os_display os_dpy,
49                               struct vigs_drm_device *drm_dev,
50                               const char *drm_dev_name)
51 {
52     dpy->platform = platform;
53     dpy->os_dpy = os_dpy;
54     dpy->drm_dev = drm_dev;
55     if (drm_dev) {
56         dpy->drm_dev_name = strdup(drm_dev_name);
57     } else {
58         dpy->drm_dev_name = NULL;
59     }
60 #ifdef YAGL_PLATFORM_WAYLAND
61     dpy->WL_bind_wayland_display_supported = (drm_dev ? 1 : 0);
62 #else
63     dpy->WL_bind_wayland_display_supported = 0;
64 #endif
65 }
66
67 void yagl_native_display_cleanup(struct yagl_native_display *dpy)
68 {
69     dpy->drm_dev = NULL;
70     free(dpy->drm_dev_name);
71     dpy->drm_dev_name = NULL;
72 }
73
74 #ifdef YAGL_PLATFORM_WAYLAND
75 static int yagl_native_display_wl_authenticate(void *user_data,
76                                                uint32_t id)
77 {
78     struct yagl_native_display *dpy = user_data;
79
80     return dpy->authenticate(dpy, id);
81 }
82
83 static struct vigs_drm_surface
84     *yagl_native_display_wl_acquire_buffer(void *user_data, uint32_t name)
85 {
86     struct yagl_native_display *dpy = user_data;
87     struct vigs_drm_surface *drm_sfc;
88     int ret;
89
90     YAGL_LOG_FUNC_SET(yagl_native_display_wl_acquire_buffer);
91
92     ret = vigs_drm_surface_open(dpy->drm_dev, name, &drm_sfc);
93
94     if (ret != 0) {
95         YAGL_LOG_ERROR("vigs_drm_surface_open failed for name %u: %s",
96                        name,
97                        strerror(-ret));
98         return NULL;
99     }
100
101     return drm_sfc;
102 }
103
104 static struct wayland_drm_callbacks wl_drm_callbacks =
105 {
106     .authenticate = yagl_native_display_wl_authenticate,
107     .acquire_buffer = yagl_native_display_wl_acquire_buffer,
108 };
109
110 int yagl_native_display_bind_wl_display(struct yagl_native_display *dpy,
111                                         struct wl_display *wl_dpy)
112 {
113     if (dpy->wl_server_drm) {
114         return 0;
115     }
116
117     dpy->wl_server_drm = wayland_drm_create(wl_dpy,
118                                             dpy->drm_dev_name,
119                                             &wl_drm_callbacks,
120                                             dpy);
121
122     return dpy->wl_server_drm ? 1 : 0;
123 }
124
125 int yagl_native_display_unbind_wl_display(struct yagl_native_display *dpy)
126 {
127     if (!dpy->wl_server_drm) {
128         return 0;
129     }
130
131     wayland_drm_destroy(dpy->wl_server_drm);
132     dpy->wl_server_drm = NULL;
133
134     return 1;
135 }
136
137 int yagl_native_display_query_wl_buffer(struct yagl_native_display *dpy,
138                                         struct wl_resource *buffer,
139                                         EGLint attribute,
140                                         EGLint *value)
141 {
142     struct wl_drm_buffer *drm_buffer = wayland_drm_get_buffer(buffer);
143     struct vigs_drm_surface *drm_sfc;
144
145     if (!drm_buffer) {
146         return 0;
147     }
148
149     drm_sfc = wayland_drm_buffer_get_sfc(drm_buffer);
150
151     switch (attribute) {
152     case EGL_TEXTURE_FORMAT:
153         switch (drm_sfc->format) {
154         case vigs_drm_surface_bgrx8888:
155             *value = EGL_TEXTURE_RGB;
156             break;
157         case vigs_drm_surface_bgra8888:
158             *value = EGL_TEXTURE_RGBA;
159             break;
160         default:
161             return 0;
162         }
163         break;
164     case EGL_WIDTH:
165         *value = drm_sfc->width;
166         break;
167     case EGL_HEIGHT:
168         *value = drm_sfc->height;
169         break;
170     case EGL_WAYLAND_Y_INVERTED_WL:
171         *value = yagl_get_backend()->y_inverted;
172         break;
173     default:
174         return 0;
175     }
176
177     return 1;
178 }
179 #endif