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 if (attrs
.DRMBufferUseMESA
& ~valid_use
) {
102 _eglLog(_EGL_DEBUG
, "bad image use bit 0x%04x",
103 attrs
.DRMBufferUseMESA
);
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
;
116 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
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
;
125 return screen
->resource_create(screen
, &templ
);
128 static struct pipe_resource
*
129 egl_g3d_reference_drm_buffer(_EGLDisplay
*dpy
, EGLint name
,
130 _EGLImage
*img
, const EGLint
*attribs
)
132 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
133 struct pipe_resource templ
;
134 struct winsys_handle wsh
;
135 _EGLImageAttribs attrs
;
138 if (!dpy
->Extensions
.MESA_drm_image
)
141 if (_eglParseImageAttribList(&attrs
, dpy
, attribs
) != EGL_SUCCESS
)
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
);
151 switch (attrs
.DRMBufferFormatMESA
) {
152 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
153 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
156 _eglLog(_EGL_DEBUG
, "bad image format value 0x%04x",
157 attrs
.DRMBufferFormatMESA
);
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
;
169 templ
.array_size
= 1;
171 memset(&wsh
, 0, sizeof(wsh
));
172 wsh
.handle
= (unsigned) name
;
174 attrs
.DRMBufferStrideMESA
* util_format_get_blocksize(templ
.format
);
176 return gdpy
->native
->buffer
->import_buffer(gdpy
->native
, &templ
, &wsh
);
179 #endif /* EGL_MESA_drm_image */
182 egl_g3d_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
183 EGLenum target
, EGLClientBuffer buffer
,
184 const EGLint
*attribs
)
186 struct pipe_resource
*ptex
;
187 struct egl_g3d_image
*gimg
;
188 unsigned level
= 0, layer
= 0;
190 gimg
= CALLOC_STRUCT(egl_g3d_image
);
192 _eglError(EGL_BAD_ALLOC
, "eglCreateEGLImageKHR");
196 if (!_eglInitImage(&gimg
->base
, dpy
)) {
202 case EGL_NATIVE_PIXMAP_KHR
:
203 ptex
= egl_g3d_reference_native_pixmap(dpy
,
204 (EGLNativePixmapType
) buffer
);
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
);
222 if (level
> ptex
->last_level
) {
223 _eglError(EGL_BAD_MATCH
, "eglCreateEGLImageKHR");
224 pipe_resource_reference(&gimg
->texture
, NULL
);
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
);
235 /* transfer the ownership to the image */
236 gimg
->texture
= ptex
;
244 egl_g3d_destroy_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
)
246 struct egl_g3d_image
*gimg
= egl_g3d_image(img
);
248 pipe_resource_reference(&gimg
->texture
, NULL
);
255 egl_g3d_create_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
256 const EGLint
*attribs
)
258 struct egl_g3d_image
*gimg
;
259 struct pipe_resource
*ptex
;
261 gimg
= CALLOC_STRUCT(egl_g3d_image
);
263 _eglError(EGL_BAD_ALLOC
, "eglCreateDRMImageKHR");
267 if (!_eglInitImage(&gimg
->base
, dpy
)) {
272 #ifdef EGL_MESA_drm_image
273 ptex
= egl_g3d_create_drm_buffer(dpy
, &gimg
->base
, attribs
);
282 /* transfer the ownership to the image */
283 gimg
->texture
= ptex
;
291 egl_g3d_export_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
,
292 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
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
;
298 if (!dpy
->Extensions
.MESA_drm_image
)
301 /* get shared handle */
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
))
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
))
321 *handle
= wsh
.handle
;
323 *stride
= wsh
.stride
;