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 /* move this to native display? */
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
, const EGLint
*attribs
)
79 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
80 struct pipe_screen
*screen
= gdpy
->native
->screen
;
81 struct pipe_resource templ
;
82 EGLint width
= 0, height
= 0, format
= 0, use
= 0;
84 EGLint i
, err
= EGL_SUCCESS
;
86 for (i
= 0; attribs
[i
] != EGL_NONE
; i
++) {
87 EGLint attr
= attribs
[i
++];
88 EGLint val
= attribs
[i
];
97 case EGL_DRM_BUFFER_FORMAT_MESA
:
100 case EGL_DRM_BUFFER_USE_MESA
:
104 err
= EGL_BAD_ATTRIBUTE
;
108 if (err
!= EGL_SUCCESS
) {
109 _eglLog(_EGL_DEBUG
, "bad image attribute 0x%04x", attr
);
114 if (width
<= 0 || height
<= 0) {
115 _eglLog(_EGL_DEBUG
, "bad width or height (%dx%d)", width
, height
);
120 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
121 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
124 _eglLog(_EGL_DEBUG
, "bad image format value 0x%04x", format
);
129 valid_use
= EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
130 EGL_DRM_BUFFER_USE_SHARE_MESA
;
131 if (use
& ~valid_use
) {
132 _eglLog(_EGL_DEBUG
, "bad image use bit 0x%04x", use
);
136 memset(&templ
, 0, sizeof(templ
));
137 templ
.target
= PIPE_TEXTURE_2D
;
138 templ
.format
= format
;
139 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
140 templ
.width0
= width
;
141 templ
.height0
= height
;
145 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
148 if ((use
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
) &&
149 width
>= 640 && height
>= 480)
150 templ
.bind
|= PIPE_BIND_SCANOUT
;
151 if (use
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
152 templ
.bind
|= PIPE_BIND_SHARED
;
154 return screen
->resource_create(screen
, &templ
);
157 static struct pipe_resource
*
158 egl_g3d_reference_drm_buffer(_EGLDisplay
*dpy
, EGLint name
,
159 const EGLint
*attribs
)
161 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
162 struct pipe_screen
*screen
= gdpy
->native
->screen
;
163 struct pipe_resource templ
;
164 struct winsys_handle wsh
;
165 EGLint width
= 0, height
= 0, format
= 0, stride
= 0;
166 EGLint i
, err
= EGL_SUCCESS
;
168 /* winsys_handle is in theory platform-specific */
169 if (dpy
->Platform
!= _EGL_PLATFORM_DRM
)
172 for (i
= 0; attribs
[i
] != EGL_NONE
; i
++) {
173 EGLint attr
= attribs
[i
++];
174 EGLint val
= attribs
[i
];
183 case EGL_DRM_BUFFER_FORMAT_MESA
:
186 case EGL_DRM_BUFFER_STRIDE_MESA
:
190 err
= EGL_BAD_ATTRIBUTE
;
194 if (err
!= EGL_SUCCESS
) {
195 _eglLog(_EGL_DEBUG
, "bad image attribute 0x%04x", attr
);
200 if (width
<= 0 || height
<= 0 || stride
<= 0) {
201 _eglLog(_EGL_DEBUG
, "bad width, height, or stride (%dx%dx%d)",
202 width
, height
, stride
);
207 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
208 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
211 _eglLog(_EGL_DEBUG
, "bad image format value 0x%04x", format
);
216 memset(&templ
, 0, sizeof(templ
));
217 templ
.target
= PIPE_TEXTURE_2D
;
218 templ
.format
= format
;
219 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
220 templ
.width0
= width
;
221 templ
.height0
= height
;
224 memset(&wsh
, 0, sizeof(wsh
));
225 wsh
.handle
= (unsigned) name
;
228 return screen
->resource_from_handle(screen
, &templ
, &wsh
);
231 #endif /* EGL_MESA_drm_image */
234 egl_g3d_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
235 EGLenum target
, EGLClientBuffer buffer
,
236 const EGLint
*attribs
)
238 struct pipe_resource
*ptex
;
239 struct egl_g3d_image
*gimg
;
240 unsigned face
= 0, level
= 0, zslice
= 0;
242 gimg
= CALLOC_STRUCT(egl_g3d_image
);
244 _eglError(EGL_BAD_ALLOC
, "eglCreateEGLImageKHR");
248 if (!_eglInitImage(&gimg
->base
, dpy
, attribs
)) {
254 case EGL_NATIVE_PIXMAP_KHR
:
255 ptex
= egl_g3d_reference_native_pixmap(dpy
,
256 (EGLNativePixmapType
) buffer
);
258 #ifdef EGL_MESA_drm_image
259 case EGL_DRM_BUFFER_MESA
:
260 ptex
= egl_g3d_reference_drm_buffer(dpy
, (EGLint
) buffer
, attribs
);
273 if (level
> ptex
->last_level
) {
274 _eglError(EGL_BAD_MATCH
, "eglCreateEGLImageKHR");
275 pipe_resource_reference(&gimg
->texture
, NULL
);
279 if (zslice
> ptex
->depth0
) {
280 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
281 pipe_resource_reference(&gimg
->texture
, NULL
);
286 /* transfer the ownership to the image */
287 gimg
->texture
= ptex
;
290 gimg
->zslice
= zslice
;
296 egl_g3d_destroy_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
)
298 struct egl_g3d_image
*gimg
= egl_g3d_image(img
);
300 pipe_resource_reference(&gimg
->texture
, NULL
);
307 egl_g3d_create_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
308 const EGLint
*attribs
)
310 struct egl_g3d_image
*gimg
;
311 struct pipe_resource
*ptex
;
313 gimg
= CALLOC_STRUCT(egl_g3d_image
);
315 _eglError(EGL_BAD_ALLOC
, "eglCreateDRMImageKHR");
319 if (!_eglInitImage(&gimg
->base
, dpy
, attribs
)) {
324 #ifdef EGL_MESA_drm_image
325 ptex
= egl_g3d_create_drm_buffer(dpy
, attribs
);
334 /* transfer the ownership to the image */
335 gimg
->texture
= ptex
;
344 egl_g3d_export_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
,
345 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
347 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
348 struct egl_g3d_image
*gimg
= egl_g3d_image(img
);
349 struct pipe_screen
*screen
= gdpy
->native
->screen
;
350 struct winsys_handle wsh
;
352 /* winsys_handle is in theory platform-specific */
353 if (dpy
->Platform
!= _EGL_PLATFORM_DRM
)
356 /* get shared handle */
358 memset(&handle
, 0, sizeof(handle
));
359 wsh
.type
= DRM_API_HANDLE_TYPE_SHARED
;
360 if (!screen
->resource_get_handle(screen
, gimg
->texture
, &wsh
)) {
368 if (handle
|| stride
) {
369 memset(&wsh
, 0, sizeof(wsh
));
370 wsh
.type
= DRM_API_HANDLE_TYPE_KMS
;
371 if (!screen
->resource_get_handle(screen
, gimg
->texture
, &wsh
))
375 *handle
= wsh
.handle
;
377 *stride
= wsh
.stride
;