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_api.h"
39 #include "egl_g3d_image.h"
41 /* for struct winsys_handle */
42 #include "state_tracker/drm_driver.h"
45 * Reference and return the front left buffer of the native pixmap.
47 static struct pipe_resource
*
48 egl_g3d_reference_native_pixmap(_EGLDisplay
*dpy
, EGLNativePixmapType pix
)
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
;
56 gconf
= egl_g3d_config(egl_g3d_find_pixmap_config(dpy
, pix
));
60 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
65 natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
66 if (!nsurf
->validate(nsurf
, 1 << natt
, NULL
, textures
, NULL
, NULL
))
67 textures
[natt
] = NULL
;
69 nsurf
->destroy(nsurf
);
71 return textures
[natt
];
74 #ifdef EGL_MESA_drm_image
76 static struct pipe_resource
*
77 egl_g3d_create_drm_buffer(_EGLDisplay
*dpy
, _EGLImage
*img
,
78 const EGLint
*attribs
)
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
;
86 if (_eglParseImageAttribList(&attrs
, dpy
, attribs
) != EGL_SUCCESS
)
89 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
90 _eglLog(_EGL_DEBUG
, "bad width or height (%dx%d)",
91 attrs
.Width
, attrs
.Height
);
95 switch (attrs
.DRMBufferFormatMESA
) {
96 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
97 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
100 _eglLog(_EGL_DEBUG
, "bad image format value 0x%04x",
101 attrs
.DRMBufferFormatMESA
);
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
);
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
;
123 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
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
;
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 struct pipe_resource templ
;
141 struct winsys_handle wsh
;
142 _EGLImageAttribs attrs
;
145 if (dpy
->Platform
!= _EGL_PLATFORM_DRM
)
148 if (_eglParseImageAttribList(&attrs
, dpy
, attribs
) != EGL_SUCCESS
)
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
);
158 switch (attrs
.DRMBufferFormatMESA
) {
159 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
160 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
163 _eglLog(_EGL_DEBUG
, "bad image format value 0x%04x",
164 attrs
.DRMBufferFormatMESA
);
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
;
177 memset(&wsh
, 0, sizeof(wsh
));
178 wsh
.handle
= (unsigned) name
;
180 attrs
.DRMBufferStrideMESA
* util_format_get_blocksize(templ
.format
);
182 return gdpy
->native
->buffer
->import_buffer(gdpy
->native
, &templ
, &wsh
);
185 #endif /* EGL_MESA_drm_image */
188 egl_g3d_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
189 EGLenum target
, EGLClientBuffer buffer
,
190 const EGLint
*attribs
)
192 struct pipe_resource
*ptex
;
193 struct egl_g3d_image
*gimg
;
194 unsigned level
= 0, layer
= 0;
196 gimg
= CALLOC_STRUCT(egl_g3d_image
);
198 _eglError(EGL_BAD_ALLOC
, "eglCreateEGLImageKHR");
202 if (!_eglInitImage(&gimg
->base
, dpy
)) {
208 case EGL_NATIVE_PIXMAP_KHR
:
209 ptex
= egl_g3d_reference_native_pixmap(dpy
,
210 (EGLNativePixmapType
) buffer
);
212 #ifdef EGL_MESA_drm_image
213 case EGL_DRM_BUFFER_MESA
:
214 ptex
= egl_g3d_reference_drm_buffer(dpy
,
215 (EGLint
) buffer
, &gimg
->base
, attribs
);
228 if (level
> ptex
->last_level
) {
229 _eglError(EGL_BAD_MATCH
, "eglCreateEGLImageKHR");
230 pipe_resource_reference(&gimg
->texture
, NULL
);
234 if (layer
>= (u_minify(ptex
->depth0
, level
) + ptex
->array_size
- 1)) {
235 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
236 pipe_resource_reference(&gimg
->texture
, NULL
);
241 /* transfer the ownership to the image */
242 gimg
->texture
= ptex
;
250 egl_g3d_destroy_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
)
252 struct egl_g3d_image
*gimg
= egl_g3d_image(img
);
254 pipe_resource_reference(&gimg
->texture
, NULL
);
261 egl_g3d_create_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
262 const EGLint
*attribs
)
264 struct egl_g3d_image
*gimg
;
265 struct pipe_resource
*ptex
;
267 gimg
= CALLOC_STRUCT(egl_g3d_image
);
269 _eglError(EGL_BAD_ALLOC
, "eglCreateDRMImageKHR");
273 if (!_eglInitImage(&gimg
->base
, dpy
)) {
278 #ifdef EGL_MESA_drm_image
279 ptex
= egl_g3d_create_drm_buffer(dpy
, &gimg
->base
, attribs
);
288 /* transfer the ownership to the image */
289 gimg
->texture
= ptex
;
297 egl_g3d_export_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
,
298 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
300 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
301 struct egl_g3d_image
*gimg
= egl_g3d_image(img
);
302 struct winsys_handle wsh
;
304 if (dpy
->Platform
!= _EGL_PLATFORM_DRM
)
307 /* get shared handle */
309 memset(&handle
, 0, sizeof(handle
));
310 wsh
.type
= DRM_API_HANDLE_TYPE_SHARED
;
311 if (!gdpy
->native
->buffer
->export_buffer(gdpy
->native
,
312 gimg
->texture
, &wsh
))
319 if (handle
|| stride
) {
320 memset(&wsh
, 0, sizeof(wsh
));
321 wsh
.type
= DRM_API_HANDLE_TYPE_KMS
;
322 if (!gdpy
->native
->buffer
->export_buffer(gdpy
->native
,
323 gimg
->texture
, &wsh
))
327 *handle
= wsh
.handle
;
329 *stride
= wsh
.stride
;