81ce7ab45c7a135ac69120e9bc1ad3c5917b37f5
[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_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 if (attrs.DRMBufferUseMESA & ~valid_use) {
102 _eglLog(_EGL_DEBUG, "bad image use bit 0x%04x",
103 attrs.DRMBufferUseMESA);
104 return NULL;
105 }
106
107 memset(&templ, 0, sizeof(templ));
108 templ.target = PIPE_TEXTURE_2D;
109 templ.format = format;
110 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
111 templ.width0 = attrs.Width;
112 templ.height0 = attrs.Height;
113 templ.depth0 = 1;
114
115 /*
116 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
117 * size check
118 */
119 if ((attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) &&
120 attrs.Width >= 640 && attrs.Height >= 480)
121 templ.bind |= PIPE_BIND_SCANOUT;
122 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
123 templ.bind |= PIPE_BIND_SHARED;
124
125 return screen->resource_create(screen, &templ);
126 }
127
128 static struct pipe_resource *
129 egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name,
130 _EGLImage *img, const EGLint *attribs)
131 {
132 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
133 struct pipe_resource templ;
134 struct winsys_handle wsh;
135 _EGLImageAttribs attrs;
136 EGLint format;
137
138 if (!dpy->Extensions.MESA_drm_image)
139 return NULL;
140
141 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
142 return NULL;
143
144 if (attrs.Width <= 0 || attrs.Height <= 0 ||
145 attrs.DRMBufferStrideMESA <= 0) {
146 _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
147 attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA);
148 return NULL;
149 }
150
151 switch (attrs.DRMBufferFormatMESA) {
152 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
153 format = PIPE_FORMAT_B8G8R8A8_UNORM;
154 break;
155 default:
156 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
157 attrs.DRMBufferFormatMESA);
158 return NULL;
159 break;
160 }
161
162 memset(&templ, 0, sizeof(templ));
163 templ.target = PIPE_TEXTURE_2D;
164 templ.format = format;
165 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
166 templ.width0 = attrs.Width;
167 templ.height0 = attrs.Height;
168 templ.depth0 = 1;
169 templ.array_size = 1;
170
171 memset(&wsh, 0, sizeof(wsh));
172 wsh.handle = (unsigned) name;
173 wsh.stride =
174 attrs.DRMBufferStrideMESA * util_format_get_blocksize(templ.format);
175
176 return gdpy->native->buffer->import_buffer(gdpy->native, &templ, &wsh);
177 }
178
179 #endif /* EGL_MESA_drm_image */
180
181 _EGLImage *
182 egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
183 EGLenum target, EGLClientBuffer buffer,
184 const EGLint *attribs)
185 {
186 struct pipe_resource *ptex;
187 struct egl_g3d_image *gimg;
188 unsigned level = 0, layer = 0;
189
190 gimg = CALLOC_STRUCT(egl_g3d_image);
191 if (!gimg) {
192 _eglError(EGL_BAD_ALLOC, "eglCreateEGLImageKHR");
193 return NULL;
194 }
195
196 if (!_eglInitImage(&gimg->base, dpy)) {
197 FREE(gimg);
198 return NULL;
199 }
200
201 switch (target) {
202 case EGL_NATIVE_PIXMAP_KHR:
203 ptex = egl_g3d_reference_native_pixmap(dpy,
204 (EGLNativePixmapType) buffer);
205 break;
206 #ifdef EGL_MESA_drm_image
207 case EGL_DRM_BUFFER_MESA:
208 ptex = egl_g3d_reference_drm_buffer(dpy,
209 (EGLint) buffer, &gimg->base, attribs);
210 break;
211 #endif
212 default:
213 ptex = NULL;
214 break;
215 }
216
217 if (!ptex) {
218 FREE(gimg);
219 return NULL;
220 }
221
222 if (level > ptex->last_level) {
223 _eglError(EGL_BAD_MATCH, "eglCreateEGLImageKHR");
224 pipe_resource_reference(&gimg->texture, NULL);
225 FREE(gimg);
226 return NULL;
227 }
228 if (layer >= (u_minify(ptex->depth0, level) + ptex->array_size - 1)) {
229 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
230 pipe_resource_reference(&gimg->texture, NULL);
231 FREE(gimg);
232 return NULL;
233 }
234
235 /* transfer the ownership to the image */
236 gimg->texture = ptex;
237 gimg->level = level;
238 gimg->layer = layer;
239
240 return &gimg->base;
241 }
242
243 EGLBoolean
244 egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img)
245 {
246 struct egl_g3d_image *gimg = egl_g3d_image(img);
247
248 pipe_resource_reference(&gimg->texture, NULL);
249 FREE(gimg);
250
251 return EGL_TRUE;
252 }
253
254 _EGLImage *
255 egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
256 const EGLint *attribs)
257 {
258 struct egl_g3d_image *gimg;
259 struct pipe_resource *ptex;
260
261 gimg = CALLOC_STRUCT(egl_g3d_image);
262 if (!gimg) {
263 _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR");
264 return NULL;
265 }
266
267 if (!_eglInitImage(&gimg->base, dpy)) {
268 FREE(gimg);
269 return NULL;
270 }
271
272 #ifdef EGL_MESA_drm_image
273 ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs);
274 #else
275 ptex = NULL;
276 #endif
277 if (!ptex) {
278 FREE(gimg);
279 return NULL;
280 }
281
282 /* transfer the ownership to the image */
283 gimg->texture = ptex;
284 gimg->level = 0;
285 gimg->layer = 0;
286
287 return &gimg->base;
288 }
289
290 EGLBoolean
291 egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
292 EGLint *name, EGLint *handle, EGLint *stride)
293 {
294 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
295 struct egl_g3d_image *gimg = egl_g3d_image(img);
296 struct winsys_handle wsh;
297
298 if (!dpy->Extensions.MESA_drm_image)
299 return EGL_FALSE;
300
301 /* get shared handle */
302 if (name) {
303 memset(&handle, 0, sizeof(handle));
304 wsh.type = DRM_API_HANDLE_TYPE_SHARED;
305 if (!gdpy->native->buffer->export_buffer(gdpy->native,
306 gimg->texture, &wsh))
307 return EGL_FALSE;
308
309 *name = wsh.handle;
310 }
311
312 /* get KMS handle */
313 if (handle || stride) {
314 memset(&wsh, 0, sizeof(wsh));
315 wsh.type = DRM_API_HANDLE_TYPE_KMS;
316 if (!gdpy->native->buffer->export_buffer(gdpy->native,
317 gimg->texture, &wsh))
318 return EGL_FALSE;
319
320 if (handle)
321 *handle = wsh.handle;
322 if (stride)
323 *stride = wsh.stride;
324 }
325
326 return EGL_TRUE;
327 }