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
, _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_screen
*screen
= gdpy
->native
->screen
;
141 struct pipe_resource templ
;
142 struct winsys_handle wsh
;
143 _EGLImageAttribs attrs
;
146 /* winsys_handle is in theory platform-specific */
147 if (dpy
->Platform
!= _EGL_PLATFORM_DRM
)
150 if (_eglParseImageAttribList(&attrs
, dpy
, attribs
) != EGL_SUCCESS
)
153 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
154 attrs
.DRMBufferStrideMESA
<= 0) {
155 _eglLog(_EGL_DEBUG
, "bad width, height, or stride (%dx%dx%d)",
156 attrs
.Width
, attrs
.Height
, attrs
.DRMBufferStrideMESA
);
160 switch (attrs
.DRMBufferFormatMESA
) {
161 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
162 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
165 _eglLog(_EGL_DEBUG
, "bad image format value 0x%04x",
166 attrs
.DRMBufferFormatMESA
);
171 memset(&templ
, 0, sizeof(templ
));
172 templ
.target
= PIPE_TEXTURE_2D
;
173 templ
.format
= format
;
174 templ
.bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
175 templ
.width0
= attrs
.Width
;
176 templ
.height0
= attrs
.Height
;
179 memset(&wsh
, 0, sizeof(wsh
));
180 wsh
.handle
= (unsigned) name
;
181 wsh
.stride
= attrs
.DRMBufferStrideMESA
;
183 return screen
->resource_from_handle(screen
, &templ
, &wsh
);
186 #endif /* EGL_MESA_drm_image */
189 egl_g3d_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
190 EGLenum target
, EGLClientBuffer buffer
,
191 const EGLint
*attribs
)
193 struct pipe_resource
*ptex
;
194 struct egl_g3d_image
*gimg
;
195 unsigned face
= 0, level
= 0, zslice
= 0;
197 gimg
= CALLOC_STRUCT(egl_g3d_image
);
199 _eglError(EGL_BAD_ALLOC
, "eglCreateEGLImageKHR");
203 if (!_eglInitImage(&gimg
->base
, dpy
)) {
209 case EGL_NATIVE_PIXMAP_KHR
:
210 ptex
= egl_g3d_reference_native_pixmap(dpy
,
211 (EGLNativePixmapType
) buffer
);
213 #ifdef EGL_MESA_drm_image
214 case EGL_DRM_BUFFER_MESA
:
215 ptex
= egl_g3d_reference_drm_buffer(dpy
,
216 (EGLint
) buffer
, &gimg
->base
, attribs
);
229 if (level
> ptex
->last_level
) {
230 _eglError(EGL_BAD_MATCH
, "eglCreateEGLImageKHR");
231 pipe_resource_reference(&gimg
->texture
, NULL
);
235 if (zslice
> ptex
->depth0
) {
236 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
237 pipe_resource_reference(&gimg
->texture
, NULL
);
242 /* transfer the ownership to the image */
243 gimg
->texture
= ptex
;
246 gimg
->zslice
= zslice
;
252 egl_g3d_destroy_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
)
254 struct egl_g3d_image
*gimg
= egl_g3d_image(img
);
256 pipe_resource_reference(&gimg
->texture
, NULL
);
263 egl_g3d_create_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
264 const EGLint
*attribs
)
266 struct egl_g3d_image
*gimg
;
267 struct pipe_resource
*ptex
;
269 gimg
= CALLOC_STRUCT(egl_g3d_image
);
271 _eglError(EGL_BAD_ALLOC
, "eglCreateDRMImageKHR");
275 if (!_eglInitImage(&gimg
->base
, dpy
)) {
280 #ifdef EGL_MESA_drm_image
281 ptex
= egl_g3d_create_drm_buffer(dpy
, &gimg
->base
, attribs
);
290 /* transfer the ownership to the image */
291 gimg
->texture
= ptex
;
300 egl_g3d_export_drm_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLImage
*img
,
301 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
303 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
304 struct egl_g3d_image
*gimg
= egl_g3d_image(img
);
305 struct pipe_screen
*screen
= gdpy
->native
->screen
;
306 struct winsys_handle wsh
;
308 /* winsys_handle is in theory platform-specific */
309 if (dpy
->Platform
!= _EGL_PLATFORM_DRM
)
312 /* get shared handle */
314 memset(&handle
, 0, sizeof(handle
));
315 wsh
.type
= DRM_API_HANDLE_TYPE_SHARED
;
316 if (!screen
->resource_get_handle(screen
, gimg
->texture
, &wsh
)) {
324 if (handle
|| stride
) {
325 memset(&wsh
, 0, sizeof(wsh
));
326 wsh
.type
= DRM_API_HANDLE_TYPE_KMS
;
327 if (!screen
->resource_get_handle(screen
, gimg
->texture
, &wsh
))
331 *handle
= wsh
.handle
;
333 *stride
= wsh
.stride
;