1 /* 2 * Mesa 3-D graphics library 3 * Version: 7.8 4 * 5 * Copyright (C) 2010 LunarG Inc. 6 * 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: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 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. 24 * 25 * Authors: 26 * Chia-I Wu <olv (at) lunarg.com> 27 */ 28 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" 34 #include "egllog.h" 35 36 #include "native.h" 37 #include "egl_g3d.h" 38 #include "egl_g3d_image.h" 39 40 /** 41 * Reference and return the front left buffer of the native pixmap. 42 */ 43 static struct pipe_resource * 44 egl_g3d_reference_native_pixmap(_EGLDisplay *dpy, EGLNativePixmapType pix) 45 { 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; 50 51 nsurf = gdpy->native->create_pixmap_surface(gdpy->native, pix, NULL); 52 if (!nsurf) 53 return NULL; 54 55 natt = NATIVE_ATTACHMENT_FRONT_LEFT; 56 if (!nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL)) 57 textures[natt] = NULL; 58 59 nsurf->destroy(nsurf); 60 61 return textures[natt]; 62 } 63 64 #ifdef EGL_MESA_drm_image 65 66 static struct pipe_resource * 67 egl_g3d_create_drm_buffer(_EGLDisplay *dpy, _EGLImage *img, 68 const EGLint *attribs) 69 { 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; 75 76 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS) 77 return NULL; 78 79 if (attrs.Width <= 0 || attrs.Height <= 0) { 80 _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)", 81 attrs.Width, attrs.Height); 82 return NULL; 83 } 84 85 switch (attrs.DRMBufferFormatMESA) { 86 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: 87 format = PIPE_FORMAT_B8G8R8A8_UNORM; 88 break; 89 default: 90 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x", 91 attrs.DRMBufferFormatMESA); 92 return NULL; 93 break; 94 } 95 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); 102 return NULL; 103 } 104 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; 111 templ.depth0 = 1; 112 templ.array_size = 1; 113 114 /* 115 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the 116 * size check 117 */ 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) 125 return NULL; 126 templ.bind |= PIPE_BIND_CURSOR; 127 } 128 129 return screen->resource_create(screen, &templ); 130 } 131 132 static struct pipe_resource * 133 egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name, 134 _EGLImage *img, const EGLint *attribs) 135 { 136 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 137 _EGLImageAttribs attrs; 138 EGLint format; 139 struct native_buffer nbuf; 140 141 if (!dpy->Extensions.MESA_drm_image) 142 return NULL; 143 144 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS) 145 return NULL; 146 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); 151 return NULL; 152 } 153 154 switch (attrs.DRMBufferFormatMESA) { 155 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: 156 format = PIPE_FORMAT_B8G8R8A8_UNORM; 157 break; 158 default: 159 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x", 160 attrs.DRMBufferFormatMESA); 161 return NULL; 162 break; 163 } 164 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; 174 175 nbuf.u.drm.name = name; 176 nbuf.u.drm.stride = 177 attrs.DRMBufferStrideMESA * util_format_get_blocksize(format); 178 179 return gdpy->native->buffer->import_buffer(gdpy->native, &nbuf); 180 } 181 182 #endif /* EGL_MESA_drm_image */ 183 184 #ifdef EGL_WL_bind_wayland_display 185 186 static struct pipe_resource * 187 egl_g3d_reference_wl_buffer(_EGLDisplay *dpy, struct wl_buffer *buffer, 188 _EGLImage *img, const EGLint *attribs) 189 { 190 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 191 struct pipe_resource *resource = NULL, *tmp = NULL; 192 193 if (!gdpy->native->wayland_bufmgr) 194 return NULL; 195 196 tmp = gdpy->native->wayland_bufmgr->buffer_get_resource(gdpy->native, buffer); 197 198 pipe_resource_reference(&resource, tmp); 199 200 return resource; 201 } 202 203 #endif /* EGL_WL_bind_wayland_display */ 204 205 #ifdef EGL_ANDROID_image_native_buffer 206 207 static struct pipe_resource * 208 egl_g3d_reference_android_native_buffer(_EGLDisplay *dpy, 209 struct ANativeWindowBuffer *buf) 210 { 211 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 212 struct native_buffer nbuf; 213 214 memset(&nbuf, 0, sizeof(nbuf)); 215 nbuf.type = NATIVE_BUFFER_ANDROID; 216 nbuf.u.android = buf; 217 218 return gdpy->native->buffer->import_buffer(gdpy->native, &nbuf); 219 } 220 221 #endif /* EGL_ANDROID_image_native_buffer */ 222 223 _EGLImage * 224 egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx, 225 EGLenum target, EGLClientBuffer buffer, 226 const EGLint *attribs) 227 { 228 struct pipe_resource *ptex; 229 struct egl_g3d_image *gimg; 230 unsigned level = 0, layer = 0; 231 232 gimg = CALLOC_STRUCT(egl_g3d_image); 233 if (!gimg) { 234 _eglError(EGL_BAD_ALLOC, "eglCreateEGLImageKHR"); 235 return NULL; 236 } 237 238 if (!_eglInitImage(&gimg->base, dpy)) { 239 FREE(gimg); 240 return NULL; 241 } 242 243 switch (target) { 244 case EGL_NATIVE_PIXMAP_KHR: 245 ptex = egl_g3d_reference_native_pixmap(dpy, 246 (EGLNativePixmapType) buffer); 247 break; 248 #ifdef EGL_MESA_drm_image 249 case EGL_DRM_BUFFER_MESA: 250 ptex = egl_g3d_reference_drm_buffer(dpy, 251 (EGLint) pointer_to_intptr(buffer), &gimg->base, attribs); 252 break; 253 #endif 254 #ifdef EGL_WL_bind_wayland_display 255 case EGL_WAYLAND_BUFFER_WL: 256 ptex = egl_g3d_reference_wl_buffer(dpy, 257 (struct wl_buffer *) buffer, &gimg->base, attribs); 258 break; 259 #endif 260 #ifdef EGL_ANDROID_image_native_buffer 261 case EGL_NATIVE_BUFFER_ANDROID: 262 ptex = egl_g3d_reference_android_native_buffer(dpy, 263 (struct ANativeWindowBuffer *) buffer); 264 break; 265 #endif 266 default: 267 ptex = NULL; 268 break; 269 } 270 271 if (!ptex) { 272 FREE(gimg); 273 return NULL; 274 } 275 276 if (level > ptex->last_level) { 277 _eglError(EGL_BAD_MATCH, "eglCreateEGLImageKHR"); 278 pipe_resource_reference(&gimg->texture, NULL); 279 FREE(gimg); 280 return NULL; 281 } 282 if (layer >= (u_minify(ptex->depth0, level) + ptex->array_size - 1)) { 283 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); 284 pipe_resource_reference(&gimg->texture, NULL); 285 FREE(gimg); 286 return NULL; 287 } 288 289 /* transfer the ownership to the image */ 290 gimg->texture = ptex; 291 gimg->level = level; 292 gimg->layer = layer; 293 294 return &gimg->base; 295 } 296 297 EGLBoolean 298 egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img) 299 { 300 struct egl_g3d_image *gimg = egl_g3d_image(img); 301 302 pipe_resource_reference(&gimg->texture, NULL); 303 FREE(gimg); 304 305 return EGL_TRUE; 306 } 307 308 _EGLImage * 309 egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, 310 const EGLint *attribs) 311 { 312 struct egl_g3d_image *gimg; 313 struct pipe_resource *ptex; 314 315 gimg = CALLOC_STRUCT(egl_g3d_image); 316 if (!gimg) { 317 _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR"); 318 return NULL; 319 } 320 321 if (!_eglInitImage(&gimg->base, dpy)) { 322 FREE(gimg); 323 return NULL; 324 } 325 326 #ifdef EGL_MESA_drm_image 327 ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs); 328 #else 329 ptex = NULL; 330 #endif 331 if (!ptex) { 332 FREE(gimg); 333 return NULL; 334 } 335 336 /* transfer the ownership to the image */ 337 gimg->texture = ptex; 338 gimg->level = 0; 339 gimg->layer = 0; 340 341 return &gimg->base; 342 } 343 344 EGLBoolean 345 egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img, 346 EGLint *name, EGLint *handle, EGLint *stride) 347 { 348 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 349 struct egl_g3d_image *gimg = egl_g3d_image(img); 350 struct native_buffer nbuf; 351 352 if (!dpy->Extensions.MESA_drm_image) 353 return EGL_FALSE; 354 355 memset(&nbuf, 0, sizeof(nbuf)); 356 nbuf.type = NATIVE_BUFFER_DRM; 357 if (name) 358 nbuf.u.drm.templ.bind |= PIPE_BIND_SHARED; 359 360 if (!gdpy->native->buffer->export_buffer(gdpy->native, 361 gimg->texture, &nbuf)) 362 return EGL_FALSE; 363 364 if (name) 365 *name = nbuf.u.drm.name; 366 if (handle) 367 *handle = nbuf.u.drm.handle; 368 if (stride) 369 *stride = nbuf.u.drm.stride; 370 371 return EGL_TRUE; 372 } 373