38627a52ec221b245855eee58850c43d70f8390e
[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 /* for struct winsys_handle */
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_resource templ;
141 struct winsys_handle wsh;
142 _EGLImageAttribs attrs;
143 EGLint format;
144
145 if (dpy->Platform != _EGL_PLATFORM_DRM)
146 return NULL;
147
148 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
149 return NULL;
150
151 if (attrs.Width <= 0 || attrs.Height <= 0 ||
152 attrs.DRMBufferStrideMESA <= 0) {
153 _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
154 attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA);
155 return NULL;
156 }
157
158 switch (attrs.DRMBufferFormatMESA) {
159 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
160 format = PIPE_FORMAT_B8G8R8A8_UNORM;
161 break;
162 default:
163 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
164 attrs.DRMBufferFormatMESA);
165 return NULL;
166 break;
167 }
168
169 memset(&templ, 0, sizeof(templ));
170 templ.target = PIPE_TEXTURE_2D;
171 templ.format = format;
172 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
173 templ.width0 = attrs.Width;
174 templ.height0 = attrs.Height;
175 templ.depth0 = 1;
176
177 memset(&wsh, 0, sizeof(wsh));
178 wsh.handle = (unsigned) name;
179 wsh.stride =
180 attrs.DRMBufferStrideMESA * util_format_get_blocksize(templ.format);
181
182 return gdpy->native->buffer->import_buffer(gdpy->native, &templ, &wsh);
183 }
184
185 #endif /* EGL_MESA_drm_image */
186
187 _EGLImage *
188 egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
189 EGLenum target, EGLClientBuffer buffer,
190 const EGLint *attribs)
191 {
192 struct pipe_resource *ptex;
193 struct egl_g3d_image *gimg;
194 unsigned level = 0, layer = 0;
195
196 gimg = CALLOC_STRUCT(egl_g3d_image);
197 if (!gimg) {
198 _eglError(EGL_BAD_ALLOC, "eglCreateEGLImageKHR");
199 return NULL;
200 }
201
202 if (!_eglInitImage(&gimg->base, dpy)) {
203 FREE(gimg);
204 return NULL;
205 }
206
207 switch (target) {
208 case EGL_NATIVE_PIXMAP_KHR:
209 ptex = egl_g3d_reference_native_pixmap(dpy,
210 (EGLNativePixmapType) buffer);
211 break;
212 #ifdef EGL_MESA_drm_image
213 case EGL_DRM_BUFFER_MESA:
214 ptex = egl_g3d_reference_drm_buffer(dpy,
215 (EGLint) buffer, &gimg->base, attribs);
216 break;
217 #endif
218 default:
219 ptex = NULL;
220 break;
221 }
222
223 if (!ptex) {
224 FREE(gimg);
225 return NULL;
226 }
227
228 if (level > ptex->last_level) {
229 _eglError(EGL_BAD_MATCH, "eglCreateEGLImageKHR");
230 pipe_resource_reference(&gimg->texture, NULL);
231 FREE(gimg);
232 return NULL;
233 }
234 if (layer >= (u_minify(ptex->depth0, level) + ptex->array_size - 1)) {
235 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
236 pipe_resource_reference(&gimg->texture, NULL);
237 FREE(gimg);
238 return NULL;
239 }
240
241 /* transfer the ownership to the image */
242 gimg->texture = ptex;
243 gimg->level = level;
244 gimg->layer = layer;
245
246 return &gimg->base;
247 }
248
249 EGLBoolean
250 egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img)
251 {
252 struct egl_g3d_image *gimg = egl_g3d_image(img);
253
254 pipe_resource_reference(&gimg->texture, NULL);
255 FREE(gimg);
256
257 return EGL_TRUE;
258 }
259
260 _EGLImage *
261 egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
262 const EGLint *attribs)
263 {
264 struct egl_g3d_image *gimg;
265 struct pipe_resource *ptex;
266
267 gimg = CALLOC_STRUCT(egl_g3d_image);
268 if (!gimg) {
269 _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR");
270 return NULL;
271 }
272
273 if (!_eglInitImage(&gimg->base, dpy)) {
274 FREE(gimg);
275 return NULL;
276 }
277
278 #ifdef EGL_MESA_drm_image
279 ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs);
280 #else
281 ptex = NULL;
282 #endif
283 if (!ptex) {
284 FREE(gimg);
285 return NULL;
286 }
287
288 /* transfer the ownership to the image */
289 gimg->texture = ptex;
290 gimg->level = 0;
291 gimg->layer = 0;
292
293 return &gimg->base;
294 }
295
296 EGLBoolean
297 egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
298 EGLint *name, EGLint *handle, EGLint *stride)
299 {
300 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
301 struct egl_g3d_image *gimg = egl_g3d_image(img);
302 struct winsys_handle wsh;
303
304 if (dpy->Platform != _EGL_PLATFORM_DRM)
305 return EGL_FALSE;
306
307 /* get shared handle */
308 if (name) {
309 memset(&handle, 0, sizeof(handle));
310 wsh.type = DRM_API_HANDLE_TYPE_SHARED;
311 if (!gdpy->native->buffer->export_buffer(gdpy->native,
312 gimg->texture, &wsh))
313 return EGL_FALSE;
314
315 *name = wsh.handle;
316 }
317
318 /* get KMS handle */
319 if (handle || stride) {
320 memset(&wsh, 0, sizeof(wsh));
321 wsh.type = DRM_API_HANDLE_TYPE_KMS;
322 if (!gdpy->native->buffer->export_buffer(gdpy->native,
323 gimg->texture, &wsh))
324 return EGL_FALSE;
325
326 if (handle)
327 *handle = wsh.handle;
328 if (stride)
329 *stride = wsh.stride;
330 }
331
332 return EGL_TRUE;
333 }