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