Merge branch 'pipe-video' of git://anongit.freedesktop.org/~deathsimple/xvmc-r600...
[mesa.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_api.h"
39 #include "egl_g3d_image.h"
40
41 /* move this to native display? */
42 #include "state_tracker/drm_driver.h"
43
44 /**
45 * Reference and return the front left buffer of the native pixmap.
46 */
47 static struct pipe_resource *
48 egl_g3d_reference_native_pixmap(_EGLDisplay *dpy, EGLNativePixmapType pix)
49 {
50 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
51 struct egl_g3d_config *gconf;
52 struct native_surface *nsurf;
53 struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS];
54 enum native_attachment natt;
55
56 gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, pix));
57 if (!gconf)
58 return NULL;
59
60 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
61 pix, gconf->native);
62 if (!nsurf)
63 return NULL;
64
65 natt = NATIVE_ATTACHMENT_FRONT_LEFT;
66 if (!nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL))
67 textures[natt] = NULL;
68
69 nsurf->destroy(nsurf);
70
71 return textures[natt];
72 }
73
74 #ifdef EGL_MESA_drm_image
75
76 static struct pipe_resource *
77 egl_g3d_create_drm_buffer(_EGLDisplay *dpy, _EGLImage *img,
78 const EGLint *attribs)
79 {
80 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
81 struct pipe_screen *screen = gdpy->native->screen;
82 struct pipe_resource templ;
83 _EGLImageAttribs attrs;
84 EGLint format, valid_use;
85
86 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
87 return NULL;
88
89 if (attrs.Width <= 0 || attrs.Height <= 0) {
90 _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)",
91 attrs.Width, attrs.Height);
92 return NULL;
93 }
94
95 switch (attrs.DRMBufferFormatMESA) {
96 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
97 format = PIPE_FORMAT_B8G8R8A8_UNORM;
98 break;
99 default:
100 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
101 attrs.DRMBufferFormatMESA);
102 return NULL;
103 break;
104 }
105
106 valid_use = EGL_DRM_BUFFER_USE_SCANOUT_MESA |
107 EGL_DRM_BUFFER_USE_SHARE_MESA;
108 if (attrs.DRMBufferUseMESA & ~valid_use) {
109 _eglLog(_EGL_DEBUG, "bad image use bit 0x%04x",
110 attrs.DRMBufferUseMESA);
111 return NULL;
112 }
113
114 memset(&templ, 0, sizeof(templ));
115 templ.target = PIPE_TEXTURE_2D;
116 templ.format = format;
117 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
118 templ.width0 = attrs.Width;
119 templ.height0 = attrs.Height;
120 templ.depth0 = 1;
121
122 /*
123 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
124 * size check
125 */
126 if ((attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) &&
127 attrs.Width >= 640 && attrs.Height >= 480)
128 templ.bind |= PIPE_BIND_SCANOUT;
129 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
130 templ.bind |= PIPE_BIND_SHARED;
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 struct pipe_screen *screen = gdpy->native->screen;
141 struct pipe_resource templ;
142 struct winsys_handle wsh;
143 _EGLImageAttribs attrs;
144 EGLint format;
145
146 /* winsys_handle is in theory platform-specific */
147 if (dpy->Platform != _EGL_PLATFORM_DRM)
148 return NULL;
149
150 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
151 return NULL;
152
153 if (attrs.Width <= 0 || attrs.Height <= 0 ||
154 attrs.DRMBufferStrideMESA <= 0) {
155 _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
156 attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA);
157 return NULL;
158 }
159
160 switch (attrs.DRMBufferFormatMESA) {
161 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
162 format = PIPE_FORMAT_B8G8R8A8_UNORM;
163 break;
164 default:
165 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
166 attrs.DRMBufferFormatMESA);
167 return NULL;
168 break;
169 }
170
171 memset(&templ, 0, sizeof(templ));
172 templ.target = PIPE_TEXTURE_2D;
173 templ.format = format;
174 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
175 templ.width0 = attrs.Width;
176 templ.height0 = attrs.Height;
177 templ.depth0 = 1;
178
179 memset(&wsh, 0, sizeof(wsh));
180 wsh.handle = (unsigned) name;
181 wsh.stride = attrs.DRMBufferStrideMESA;
182
183 return screen->resource_from_handle(screen, &templ, &wsh);
184 }
185
186 #endif /* EGL_MESA_drm_image */
187
188 _EGLImage *
189 egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
190 EGLenum target, EGLClientBuffer buffer,
191 const EGLint *attribs)
192 {
193 struct pipe_resource *ptex;
194 struct egl_g3d_image *gimg;
195 unsigned face = 0, level = 0, zslice = 0;
196
197 gimg = CALLOC_STRUCT(egl_g3d_image);
198 if (!gimg) {
199 _eglError(EGL_BAD_ALLOC, "eglCreateEGLImageKHR");
200 return NULL;
201 }
202
203 if (!_eglInitImage(&gimg->base, dpy)) {
204 FREE(gimg);
205 return NULL;
206 }
207
208 switch (target) {
209 case EGL_NATIVE_PIXMAP_KHR:
210 ptex = egl_g3d_reference_native_pixmap(dpy,
211 (EGLNativePixmapType) buffer);
212 break;
213 #ifdef EGL_MESA_drm_image
214 case EGL_DRM_BUFFER_MESA:
215 ptex = egl_g3d_reference_drm_buffer(dpy,
216 (EGLint) buffer, &gimg->base, attribs);
217 break;
218 #endif
219 default:
220 ptex = NULL;
221 break;
222 }
223
224 if (!ptex) {
225 FREE(gimg);
226 return NULL;
227 }
228
229 if (level > ptex->last_level) {
230 _eglError(EGL_BAD_MATCH, "eglCreateEGLImageKHR");
231 pipe_resource_reference(&gimg->texture, NULL);
232 FREE(gimg);
233 return NULL;
234 }
235 if (zslice > ptex->depth0) {
236 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
237 pipe_resource_reference(&gimg->texture, NULL);
238 FREE(gimg);
239 return NULL;
240 }
241
242 /* transfer the ownership to the image */
243 gimg->texture = ptex;
244 gimg->face = face;
245 gimg->level = level;
246 gimg->zslice = zslice;
247
248 return &gimg->base;
249 }
250
251 EGLBoolean
252 egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img)
253 {
254 struct egl_g3d_image *gimg = egl_g3d_image(img);
255
256 pipe_resource_reference(&gimg->texture, NULL);
257 FREE(gimg);
258
259 return EGL_TRUE;
260 }
261
262 _EGLImage *
263 egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
264 const EGLint *attribs)
265 {
266 struct egl_g3d_image *gimg;
267 struct pipe_resource *ptex;
268
269 gimg = CALLOC_STRUCT(egl_g3d_image);
270 if (!gimg) {
271 _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR");
272 return NULL;
273 }
274
275 if (!_eglInitImage(&gimg->base, dpy)) {
276 FREE(gimg);
277 return NULL;
278 }
279
280 #ifdef EGL_MESA_drm_image
281 ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs);
282 #else
283 ptex = NULL;
284 #endif
285 if (!ptex) {
286 FREE(gimg);
287 return NULL;
288 }
289
290 /* transfer the ownership to the image */
291 gimg->texture = ptex;
292 gimg->face = 0;
293 gimg->level = 0;
294 gimg->zslice = 0;
295
296 return &gimg->base;
297 }
298
299 EGLBoolean
300 egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
301 EGLint *name, EGLint *handle, EGLint *stride)
302 {
303 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
304 struct egl_g3d_image *gimg = egl_g3d_image(img);
305 struct pipe_screen *screen = gdpy->native->screen;
306 struct winsys_handle wsh;
307
308 /* winsys_handle is in theory platform-specific */
309 if (dpy->Platform != _EGL_PLATFORM_DRM)
310 return EGL_FALSE;
311
312 /* get shared handle */
313 if (name) {
314 memset(&handle, 0, sizeof(handle));
315 wsh.type = DRM_API_HANDLE_TYPE_SHARED;
316 if (!screen->resource_get_handle(screen, gimg->texture, &wsh)) {
317 return EGL_FALSE;
318 }
319
320 *name = wsh.handle;
321 }
322
323 /* get KMS handle */
324 if (handle || stride) {
325 memset(&wsh, 0, sizeof(wsh));
326 wsh.type = DRM_API_HANDLE_TYPE_KMS;
327 if (!screen->resource_get_handle(screen, gimg->texture, &wsh))
328 return EGL_FALSE;
329
330 if (handle)
331 *handle = wsh.handle;
332 if (stride)
333 *stride = wsh.stride;
334 }
335
336 return EGL_TRUE;
337 }