st/egl: Set pipe_resource::array_size to 1.
[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 templ.array_size = 1;
177
178 memset(&wsh, 0, sizeof(wsh));
179 wsh.handle = (unsigned) name;
180 wsh.stride =
181 attrs.DRMBufferStrideMESA * util_format_get_blocksize(templ.format);
182
183 return gdpy->native->buffer->import_buffer(gdpy->native, &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 level = 0, layer = 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 (layer >= (u_minify(ptex->depth0, level) + ptex->array_size - 1)) {
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->level = level;
245 gimg->layer = layer;
246
247 return &gimg->base;
248 }
249
250 EGLBoolean
251 egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img)
252 {
253 struct egl_g3d_image *gimg = egl_g3d_image(img);
254
255 pipe_resource_reference(&gimg->texture, NULL);
256 FREE(gimg);
257
258 return EGL_TRUE;
259 }
260
261 _EGLImage *
262 egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
263 const EGLint *attribs)
264 {
265 struct egl_g3d_image *gimg;
266 struct pipe_resource *ptex;
267
268 gimg = CALLOC_STRUCT(egl_g3d_image);
269 if (!gimg) {
270 _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR");
271 return NULL;
272 }
273
274 if (!_eglInitImage(&gimg->base, dpy)) {
275 FREE(gimg);
276 return NULL;
277 }
278
279 #ifdef EGL_MESA_drm_image
280 ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs);
281 #else
282 ptex = NULL;
283 #endif
284 if (!ptex) {
285 FREE(gimg);
286 return NULL;
287 }
288
289 /* transfer the ownership to the image */
290 gimg->texture = ptex;
291 gimg->level = 0;
292 gimg->layer = 0;
293
294 return &gimg->base;
295 }
296
297 EGLBoolean
298 egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
299 EGLint *name, EGLint *handle, EGLint *stride)
300 {
301 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
302 struct egl_g3d_image *gimg = egl_g3d_image(img);
303 struct winsys_handle wsh;
304
305 if (dpy->Platform != _EGL_PLATFORM_DRM)
306 return EGL_FALSE;
307
308 /* get shared handle */
309 if (name) {
310 memset(&handle, 0, sizeof(handle));
311 wsh.type = DRM_API_HANDLE_TYPE_SHARED;
312 if (!gdpy->native->buffer->export_buffer(gdpy->native,
313 gimg->texture, &wsh))
314 return EGL_FALSE;
315
316 *name = wsh.handle;
317 }
318
319 /* get KMS handle */
320 if (handle || stride) {
321 memset(&wsh, 0, sizeof(wsh));
322 wsh.type = DRM_API_HANDLE_TYPE_KMS;
323 if (!gdpy->native->buffer->export_buffer(gdpy->native,
324 gimg->texture, &wsh))
325 return EGL_FALSE;
326
327 if (handle)
328 *handle = wsh.handle;
329 if (stride)
330 *stride = wsh.stride;
331 }
332
333 return EGL_TRUE;
334 }