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"
41 * Reference and return the front left buffer of the native pixmap.
43 static struct pipe_resource
*
44 egl_g3d_reference_native_pixmap(_EGLDisplay
*dpy
, EGLNativePixmapType pix
)
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
;
51 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
, pix
, NULL
);
55 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
56 if (!nsurf
->validate(nsurf
, 1 << natt
, NULL
, textures
, NULL
, NULL
))
57 textures
[natt
] = NULL
;
59 nsurf
->destroy(nsurf
);
61 return textures
[natt
];
64 #ifdef EGL_MESA_drm_image
66 static struct pipe_resource
*
67 egl_g3d_create_drm_buffer(_EGLDisplay
*dpy
, _EGLImage
*img
,
68 const EGLint
*attribs
)
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
;
76 if (_eglParseImageAttribList(&attrs
, dpy
, attribs
) != EGL_SUCCESS
)
79 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
80 _eglLog(_EGL_DEBUG
, "bad width or height (%dx%d)",
81 attrs
.Width
, attrs
.Height
);
85 switch (attrs
.DRMBufferFormatMESA
) {
86 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
87 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
90 _eglLog(_EGL_DEBUG
, "bad image format value 0x%04x",
91 attrs
.DRMBufferFormatMESA
);
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
);
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
;
112 templ
.array_size
= 1;
115 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
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)
126 templ
.bind
|= PIPE_BIND_CURSOR
;
129 return screen
->resource_create(screen
, &templ
);
132 static struct pipe_resource
*
133 egl_g3d_reference_drm_buffer(_EGLDisplay
*dpy
, EGLint name
,
134 _EGLImage
*img
, const EGLint
*attribs
)
136 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
137 _EGLImageAttribs attrs
;
139 struct native_buffer nbuf
;
141 if (!dpy
->Extensions
.MESA_drm_image
)
144 if (_eglParseImageAttribList(&attrs
, dpy
, attribs
) != EGL_SUCCESS
)
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
);
154 switch (attrs
.DRMBufferFormatMESA
) {
155 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
156 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
159 _eglLog(_EGL_DEBUG
, "bad image format value 0x%04x",
160 attrs
.DRMBufferFormatMESA
);
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;
175 nbuf
.u
.drm
.name
= name
;
177 attrs
.DRMBufferStrideMESA
* util_format_get_blocksize(format
);
179 return gdpy
->native
->buffer
->import_buffer(gdpy
->native
, &nbuf
);
182 #endif /* EGL_MESA_drm_image */
184 #ifdef EGL_WL_bind_wayland_display
186 static struct pipe_resource
*
187 egl_g3d_reference_wl_buffer(_EGLDisplay
*dpy
, struct wl_buffer
*buffer
,
188 _EGLImage
*img
, const EGLint
*attribs
)
190 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
191 struct pipe_resource
*resource
= NULL
, *tmp
= NULL
;
193 if (!gdpy
->native
->wayland_bufmgr
)
196 tmp
= gdpy
->native
->wayland_bufmgr
->buffer_get_resource(gdpy
->native
, buffer
);
198 pipe_resource_reference(&resource
, tmp
);
203 #endif /* EGL_WL_bind_wayland_display */
206 egl_g3d_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
207 EGLenum target
, EGLClientBuffer buffer
,
208 const EGLint
*attribs
)
210 struct pipe_resource
*ptex
;
211 struct egl_g3d_image
*gimg
;
212 unsigned level
= 0, layer
= 0;
214 gimg
= CALLOC_STRUCT(egl_g3d_image
);
216 _eglError(EGL_BAD_ALLOC
, "eglCreateEGLImageKHR");
220 if (!_eglInitImage(&gimg
->base
, dpy
)) {
226 case EGL_NATIVE_PIXMAP_KHR
:
227 ptex
= egl_g3d_reference_native_pixmap(dpy
,
228 (EGLNativePixmapType
) buffer
);
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
);
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
);
252 if (level
> ptex
->last_level
) {
253 _eglError(EGL_BAD_MATCH
, "eglCreateEGLImageKHR");
254 pipe_resource_reference(&gimg
->texture
, NULL
);
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
);
265 /* transfer the ownership to the image */
266 gimg
->texture
= ptex
;
274 egl_g3d_destroy_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
)
276 struct egl_g3d_image
*gimg
= egl_g3d_image(img
);
278 pipe_resource_reference(&gimg
->texture
, NULL
);
285 egl_g3d_create_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
286 const EGLint
*attribs
)
288 struct egl_g3d_image
*gimg
;
289 struct pipe_resource
*ptex
;
291 gimg
= CALLOC_STRUCT(egl_g3d_image
);
293 _eglError(EGL_BAD_ALLOC
, "eglCreateDRMImageKHR");
297 if (!_eglInitImage(&gimg
->base
, dpy
)) {
302 #ifdef EGL_MESA_drm_image
303 ptex
= egl_g3d_create_drm_buffer(dpy
, &gimg
->base
, attribs
);
312 /* transfer the ownership to the image */
313 gimg
->texture
= ptex
;
321 egl_g3d_export_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
,
322 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
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
;
328 if (!dpy
->Extensions
.MESA_drm_image
)
331 memset(&nbuf
, 0, sizeof(nbuf
));
332 nbuf
.type
= NATIVE_BUFFER_DRM
;
334 nbuf
.u
.drm
.templ
.bind
|= PIPE_BIND_SHARED
;
336 if (!gdpy
->native
->buffer
->export_buffer(gdpy
->native
,
337 gimg
->texture
, &nbuf
))
341 *name
= nbuf
.u
.drm
.name
;
343 *handle
= nbuf
.u
.drm
.handle
;
345 *stride
= nbuf
.u
.drm
.stride
;