2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * Chia-I Wu <olv@lunarg.com>
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"
38 #include "egl_g3d_image.h"
40 /* for struct winsys_handle */
41 #include "state_tracker/drm_driver.h"
44 * Reference and return the front left buffer of the native pixmap.
46 static struct pipe_resource *
47 egl_g3d_reference_native_pixmap(_EGLDisplay *dpy, EGLNativePixmapType pix)
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;
54 nsurf = gdpy->native->create_pixmap_surface(gdpy->native, pix, NULL);
58 natt = NATIVE_ATTACHMENT_FRONT_LEFT;
59 if (!nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL))
60 textures[natt] = NULL;
62 nsurf->destroy(nsurf);
64 return textures[natt];
67 #ifdef EGL_MESA_drm_image
69 static struct pipe_resource *
70 egl_g3d_create_drm_buffer(_EGLDisplay *dpy, _EGLImage *img,
71 const EGLint *attribs)
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;
79 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
82 if (attrs.Width <= 0 || attrs.Height <= 0) {
83 _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)",
84 attrs.Width, attrs.Height);
88 switch (attrs.DRMBufferFormatMESA) {
89 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
90 format = PIPE_FORMAT_B8G8R8A8_UNORM;
93 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
94 attrs.DRMBufferFormatMESA);
99 valid_use = EGL_DRM_BUFFER_USE_SCANOUT_MESA |
100 EGL_DRM_BUFFER_USE_SHARE_MESA |
101 EGL_DRM_BUFFER_USE_CURSOR_MESA;
102 if (attrs.DRMBufferUseMESA & ~valid_use) {
103 _eglLog(_EGL_DEBUG, "bad image use bit 0x%04x",
104 attrs.DRMBufferUseMESA);
108 memset(&templ, 0, sizeof(templ));
109 templ.target = PIPE_TEXTURE_2D;
110 templ.format = format;
111 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
112 templ.width0 = attrs.Width;
113 templ.height0 = attrs.Height;
115 templ.array_size = 1;
118 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
121 if ((attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) &&
122 attrs.Width >= 640 && attrs.Height >= 480)
123 templ.bind |= PIPE_BIND_SCANOUT;
124 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
125 templ.bind |= PIPE_BIND_SHARED;
126 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA) {
127 if (attrs.Width != 64 || attrs.Height != 64)
129 templ.bind |= PIPE_BIND_CURSOR;
132 return screen->resource_create(screen, &templ);
135 static struct pipe_resource *
136 egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name,
137 _EGLImage *img, const EGLint *attribs)
139 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
140 _EGLImageAttribs attrs;
142 struct native_buffer nbuf;
144 if (!dpy->Extensions.MESA_drm_image)
147 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
150 if (attrs.Width <= 0 || attrs.Height <= 0 ||
151 attrs.DRMBufferStrideMESA <= 0) {
152 _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
153 attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA);
157 switch (attrs.DRMBufferFormatMESA) {
158 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
159 format = PIPE_FORMAT_B8G8R8A8_UNORM;
162 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
163 attrs.DRMBufferFormatMESA);
168 memset(&nbuf, 0, sizeof(nbuf));
169 nbuf.type = NATIVE_BUFFER_DRM;
170 nbuf.u.drm.templ.target = PIPE_TEXTURE_2D;
171 nbuf.u.drm.templ.format = format;
172 nbuf.u.drm.templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
173 nbuf.u.drm.templ.width0 = attrs.Width;
174 nbuf.u.drm.templ.height0 = attrs.Height;
175 nbuf.u.drm.templ.depth0 = 1;
176 nbuf.u.drm.templ.array_size = 1;
178 nbuf.u.drm.name = name;
180 attrs.DRMBufferStrideMESA * util_format_get_blocksize(format);
182 return gdpy->native->buffer->import_buffer(gdpy->native, &nbuf);
185 #endif /* EGL_MESA_drm_image */
187 #ifdef EGL_WL_bind_wayland_display
189 static struct pipe_resource *
190 egl_g3d_reference_wl_buffer(_EGLDisplay *dpy, struct wl_buffer *buffer,
191 _EGLImage *img, const EGLint *attribs)
193 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
194 struct pipe_resource *resource = NULL, *tmp = NULL;
196 if (!gdpy->native->wayland_bufmgr)
199 tmp = gdpy->native->wayland_bufmgr->buffer_get_resource(gdpy->native, buffer);
201 pipe_resource_reference(&resource, tmp);
206 #endif /* EGL_WL_bind_wayland_display */
209 egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
210 EGLenum target, EGLClientBuffer buffer,
211 const EGLint *attribs)
213 struct pipe_resource *ptex;
214 struct egl_g3d_image *gimg;
215 unsigned level = 0, layer = 0;
217 gimg = CALLOC_STRUCT(egl_g3d_image);
219 _eglError(EGL_BAD_ALLOC, "eglCreateEGLImageKHR");
223 if (!_eglInitImage(&gimg->base, dpy)) {
229 case EGL_NATIVE_PIXMAP_KHR:
230 ptex = egl_g3d_reference_native_pixmap(dpy,
231 (EGLNativePixmapType) buffer);
233 #ifdef EGL_MESA_drm_image
234 case EGL_DRM_BUFFER_MESA:
235 ptex = egl_g3d_reference_drm_buffer(dpy,
236 (EGLint) buffer, &gimg->base, attribs);
239 #ifdef EGL_WL_bind_wayland_display
240 case EGL_WAYLAND_BUFFER_WL:
241 ptex = egl_g3d_reference_wl_buffer(dpy,
242 (struct wl_buffer *) buffer, &gimg->base, attribs);
255 if (level > ptex->last_level) {
256 _eglError(EGL_BAD_MATCH, "eglCreateEGLImageKHR");
257 pipe_resource_reference(&gimg->texture, NULL);
261 if (layer >= (u_minify(ptex->depth0, level) + ptex->array_size - 1)) {
262 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
263 pipe_resource_reference(&gimg->texture, NULL);
268 /* transfer the ownership to the image */
269 gimg->texture = ptex;
277 egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img)
279 struct egl_g3d_image *gimg = egl_g3d_image(img);
281 pipe_resource_reference(&gimg->texture, NULL);
288 egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy,
289 const EGLint *attribs)
291 struct egl_g3d_image *gimg;
292 struct pipe_resource *ptex;
294 gimg = CALLOC_STRUCT(egl_g3d_image);
296 _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR");
300 if (!_eglInitImage(&gimg->base, dpy)) {
305 #ifdef EGL_MESA_drm_image
306 ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs);
315 /* transfer the ownership to the image */
316 gimg->texture = ptex;
324 egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img,
325 EGLint *name, EGLint *handle, EGLint *stride)
327 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
328 struct egl_g3d_image *gimg = egl_g3d_image(img);
329 struct native_buffer nbuf;
331 if (!dpy->Extensions.MESA_drm_image)
334 memset(&nbuf, 0, sizeof(nbuf));
335 nbuf.type = NATIVE_BUFFER_DRM;
337 nbuf.u.drm.templ.bind |= PIPE_BIND_SHARED;
339 if (!gdpy->native->buffer->export_buffer(gdpy->native,
340 gimg->texture, &nbuf))
344 *name = nbuf.u.drm.name;
346 *handle = nbuf.u.drm.handle;
348 *stride = nbuf.u.drm.stride;