Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / state_trackers / egl / common / egl_g3d_image.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  7.8
4  *
5  * Copyright (C) 2010 LunarG Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Chia-I Wu <olv@lunarg.com>
27  */
28
29 #include "pipe/p_screen.h"
30 #include "util/u_memory.h"
31 #include "util/u_rect.h"
32 #include "util/u_inlines.h"
33 #include "eglcurrent.h"
34 #include "egllog.h"
35
36 #include "native.h"
37 #include "egl_g3d.h"
38 #include "egl_g3d_image.h"
39
40 /* for struct winsys_handle */
41 #include "state_tracker/drm_driver.h"
42
43 /**
44  * Reference and return the front left buffer of the native pixmap.
45  */
46 static struct pipe_resource *
47 egl_g3d_reference_native_pixmap(_EGLDisplay *dpy, EGLNativePixmapType pix)
48 {
49    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
50    struct native_surface *nsurf;
51    struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS];
52    enum native_attachment natt;
53
54    nsurf = gdpy->native->create_pixmap_surface(gdpy->native, pix, NULL);
55    if (!nsurf)
56       return NULL;
57
58    natt = NATIVE_ATTACHMENT_FRONT_LEFT;
59    if (!nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL))
60       textures[natt] = NULL;
61
62    nsurf->destroy(nsurf);
63
64    return textures[natt];
65 }
66
67 #ifdef EGL_MESA_drm_image
68
69 static struct pipe_resource *
70 egl_g3d_create_drm_buffer(_EGLDisplay *dpy, _EGLImage *img,
71                           const EGLint *attribs)
72 {
73    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
74    struct pipe_screen *screen = gdpy->native->screen;
75    struct pipe_resource templ;
76    _EGLImageAttribs attrs;
77    EGLint format, valid_use;
78
79    if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
80       return NULL;
81
82    if (attrs.Width <= 0 || attrs.Height <= 0) {
83       _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)",
84             attrs.Width, attrs.Height);
85       return NULL;
86    }
87
88    switch (attrs.DRMBufferFormatMESA) {
89    case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
90       format = PIPE_FORMAT_B8G8R8A8_UNORM;
91       break;
92    default:
93       _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
94             attrs.DRMBufferFormatMESA);
95       return NULL;
96       break;
97    }
98
99    valid_use = EGL_DRM_BUFFER_USE_SCANOUT_MESA |
100                EGL_DRM_BUFFER_USE_SHARE_MESA |
101                EGL_DRM_BUFFER_USE_CURSOR_MESA;
102    if (attrs.DRMBufferUseMESA & ~valid_use) {
103       _eglLog(_EGL_DEBUG, "bad image use bit 0x%04x",
104             attrs.DRMBufferUseMESA);
105       return NULL;
106    }
107
108    memset(&templ, 0, sizeof(templ));
109    templ.target = PIPE_TEXTURE_2D;
110    templ.format = format;
111    templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
112    templ.width0 = attrs.Width;
113    templ.height0 = attrs.Height;
114    templ.depth0 = 1;
115    templ.array_size = 1;
116
117    /*
118     * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
119     * size check
120     */
121    if ((attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) &&
122        attrs.Width >= 640 && attrs.Height >= 480)
123       templ.bind |= PIPE_BIND_SCANOUT;
124    if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
125       templ.bind |= PIPE_BIND_SHARED;
126    if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA) {
127       if (attrs.Width != 64 || attrs.Height != 64)
128          return NULL;
129       templ.bind |= PIPE_BIND_CURSOR;
130    }
131
132    return screen->resource_create(screen, &templ);
133 }
134
135 static struct pipe_resource *
136 egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name,
137                              _EGLImage *img, const EGLint *attribs)
138 {
139    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
140    _EGLImageAttribs attrs;
141    EGLint format;
142    struct native_buffer nbuf;
143
144    if (!dpy->Extensions.MESA_drm_image)
145       return NULL;
146
147    if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
148       return NULL;
149
150    if (attrs.Width <= 0 || attrs.Height <= 0 ||
151        attrs.DRMBufferStrideMESA <= 0) {
152       _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
153             attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA);
154       return NULL;
155    }
156
157    switch (attrs.DRMBufferFormatMESA) {
158    case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
159       format = PIPE_FORMAT_B8G8R8A8_UNORM;
160       break;
161    default:
162       _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
163             attrs.DRMBufferFormatMESA);
164       return NULL;
165       break;
166    }
167
168    memset(&nbuf, 0, sizeof(nbuf));
169    nbuf.type = NATIVE_BUFFER_DRM;
170    nbuf.u.drm.templ.target = PIPE_TEXTURE_2D;
171    nbuf.u.drm.templ.format = format;
172    nbuf.u.drm.templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
173    nbuf.u.drm.templ.width0 = attrs.Width;
174    nbuf.u.drm.templ.height0 = attrs.Height;
175    nbuf.u.drm.templ.depth0 = 1;
176    nbuf.u.drm.templ.array_size = 1;
177
178    nbuf.u.drm.name = name;
179    nbuf.u.drm.stride =
180       attrs.DRMBufferStrideMESA * util_format_get_blocksize(format);
181
182    return gdpy->native->buffer->import_buffer(gdpy->native, &nbuf);
183 }
184
185 #endif /* EGL_MESA_drm_image */
186
187 #ifdef EGL_WL_bind_wayland_display
188
189 static struct pipe_resource *
190 egl_g3d_reference_wl_buffer(_EGLDisplay *dpy, struct wl_buffer *buffer,
191                             _EGLImage *img, const EGLint *attribs)
192 {
193    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
194    struct pipe_resource *resource = NULL, *tmp = NULL;
195
196    if (!gdpy->native->wayland_bufmgr)
197       return NULL;
198
199    tmp = gdpy->native->wayland_bufmgr->buffer_get_resource(gdpy->native, buffer);
200
201    pipe_resource_reference(&resource, tmp);
202
203    return resource;
204 }
205
206 #endif /* EGL_WL_bind_wayland_display */
207
208 _EGLImage *
209 egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
210                      EGLenum target, EGLClientBuffer buffer,
211                      const EGLint *attribs)
212 {
213    struct pipe_resource *ptex;
214    struct egl_g3d_image *gimg;
215    unsigned level = 0, layer = 0;
216
217    gimg = CALLOC_STRUCT(egl_g3d_image);
218    if (!gimg) {
219       _eglError(EGL_BAD_ALLOC, "eglCreateEGLImageKHR");
220       return NULL;
221    }
222
223    if (!_eglInitImage(&gimg->base, dpy)) {
224       FREE(gimg);
225       return NULL;
226    }
227
228    switch (target) {
229    case EGL_NATIVE_PIXMAP_KHR:
230       ptex = egl_g3d_reference_native_pixmap(dpy,
231             (EGLNativePixmapType) buffer);
232       break;
233 #ifdef EGL_MESA_drm_image
234    case EGL_DRM_BUFFER_MESA:
235       ptex = egl_g3d_reference_drm_buffer(dpy,
236             (EGLint) buffer, &gimg->base, attribs);
237       break;
238 #endif
239 #ifdef EGL_WL_bind_wayland_display
240    case EGL_WAYLAND_BUFFER_WL:
241       ptex = egl_g3d_reference_wl_buffer(dpy,
242             (struct wl_buffer *) buffer, &gimg->base, attribs);
243       break;
244 #endif
245    default:
246       ptex = NULL;
247       break;
248    }
249
250    if (!ptex) {
251       FREE(gimg);
252       return NULL;
253    }
254
255    if (level > ptex->last_level) {
256       _eglError(EGL_BAD_MATCH, "eglCreateEGLImageKHR");
257       pipe_resource_reference(&gimg->texture, NULL);
258       FREE(gimg);
259       return NULL;
260    }
261    if (layer >= (u_minify(ptex->depth0, level) + ptex->array_size - 1)) {
262       _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
263       pipe_resource_reference(&gimg->texture, NULL);
264       FREE(gimg);
265       return NULL;
266    }
267
268    /* transfer the ownership to the image */
269    gimg->texture = ptex;
270    gimg->level = level;
271    gimg->layer = layer;
272
273    return &gimg->base;
274 }
275
276 EGLBoolean
277 egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img)
278 {
279    struct egl_g3d_image *gimg = egl_g3d_image(img);
280
281    pipe_resource_reference(&gimg->texture, NULL);
282    FREE(gimg);
283
284    return EGL_TRUE;
285 }
286
287 _EGLImage *
288 egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
289                          const EGLint *attribs)
290 {
291    struct egl_g3d_image *gimg;
292    struct pipe_resource *ptex;
293
294    gimg = CALLOC_STRUCT(egl_g3d_image);
295    if (!gimg) {
296       _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR");
297       return NULL;
298    }
299
300    if (!_eglInitImage(&gimg->base, dpy)) {
301       FREE(gimg);
302       return NULL;
303    }
304
305 #ifdef EGL_MESA_drm_image
306    ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs);
307 #else
308    ptex = NULL;
309 #endif
310    if (!ptex) {
311       FREE(gimg);
312       return NULL;
313    }
314
315    /* transfer the ownership to the image */
316    gimg->texture = ptex;
317    gimg->level = 0;
318    gimg->layer = 0;
319
320    return &gimg->base;
321 }
322
323 EGLBoolean
324 egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
325                          EGLint *name, EGLint *handle, EGLint *stride)
326 {
327    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
328    struct egl_g3d_image *gimg = egl_g3d_image(img);
329    struct native_buffer nbuf;
330
331    if (!dpy->Extensions.MESA_drm_image)
332       return EGL_FALSE;
333
334    memset(&nbuf, 0, sizeof(nbuf));
335    nbuf.type = NATIVE_BUFFER_DRM;
336    if (name)
337       nbuf.u.drm.templ.bind |= PIPE_BIND_SHARED;
338
339    if (!gdpy->native->buffer->export_buffer(gdpy->native,
340                                             gimg->texture, &nbuf))
341       return EGL_FALSE;
342
343    if (name)
344       *name = nbuf.u.drm.name;
345    if (handle)
346       *handle = nbuf.u.drm.handle;
347    if (stride)
348       *stride = nbuf.u.drm.stride;
349
350    return EGL_TRUE;
351 }