egl_g3d_image.c revision 2d62e39c622124566779e504e7ed26eee96785fb
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@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/* for struct winsys_handle */ 41#include "state_tracker/drm_driver.h" 42 43/** 44 * Reference and return the front left buffer of the native pixmap. 45 */ 46static struct pipe_resource * 47egl_g3d_reference_native_pixmap(_EGLDisplay *dpy, EGLNativePixmapType pix) 48{ 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; 53 54 nsurf = gdpy->native->create_pixmap_surface(gdpy->native, pix, NULL); 55 if (!nsurf) 56 return NULL; 57 58 natt = NATIVE_ATTACHMENT_FRONT_LEFT; 59 if (!nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL)) 60 textures[natt] = NULL; 61 62 nsurf->destroy(nsurf); 63 64 return textures[natt]; 65} 66 67#ifdef EGL_MESA_drm_image 68 69static struct pipe_resource * 70egl_g3d_create_drm_buffer(_EGLDisplay *dpy, _EGLImage *img, 71 const EGLint *attribs) 72{ 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; 78 79 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS) 80 return NULL; 81 82 if (attrs.Width <= 0 || attrs.Height <= 0) { 83 _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)", 84 attrs.Width, attrs.Height); 85 return NULL; 86 } 87 88 switch (attrs.DRMBufferFormatMESA) { 89 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: 90 format = PIPE_FORMAT_B8G8R8A8_UNORM; 91 break; 92 default: 93 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x", 94 attrs.DRMBufferFormatMESA); 95 return NULL; 96 break; 97 } 98 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); 104 return NULL; 105 } 106 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; 113 templ.depth0 = 1; 114 templ.array_size = 1; 115 116 /* 117 * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the 118 * size check 119 */ 120 if ((attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) && 121 attrs.Width >= 640 && attrs.Height >= 480) 122 templ.bind |= PIPE_BIND_SCANOUT; 123 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA) 124 templ.bind |= PIPE_BIND_SHARED; 125 126 return screen->resource_create(screen, &templ); 127} 128 129static struct pipe_resource * 130egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name, 131 _EGLImage *img, const EGLint *attribs) 132{ 133 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 134 struct pipe_resource templ; 135 struct winsys_handle wsh; 136 _EGLImageAttribs attrs; 137 EGLint format; 138 139 if (!dpy->Extensions.MESA_drm_image) 140 return NULL; 141 142 if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS) 143 return NULL; 144 145 if (attrs.Width <= 0 || attrs.Height <= 0 || 146 attrs.DRMBufferStrideMESA <= 0) { 147 _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)", 148 attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA); 149 return NULL; 150 } 151 152 switch (attrs.DRMBufferFormatMESA) { 153 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA: 154 format = PIPE_FORMAT_B8G8R8A8_UNORM; 155 break; 156 default: 157 _eglLog(_EGL_DEBUG, "bad image format value 0x%04x", 158 attrs.DRMBufferFormatMESA); 159 return NULL; 160 break; 161 } 162 163 memset(&templ, 0, sizeof(templ)); 164 templ.target = PIPE_TEXTURE_2D; 165 templ.format = format; 166 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW; 167 templ.width0 = attrs.Width; 168 templ.height0 = attrs.Height; 169 templ.depth0 = 1; 170 templ.array_size = 1; 171 172 memset(&wsh, 0, sizeof(wsh)); 173 wsh.handle = (unsigned) name; 174 wsh.stride = 175 attrs.DRMBufferStrideMESA * util_format_get_blocksize(templ.format); 176 177 return gdpy->native->buffer->import_buffer(gdpy->native, &templ, &wsh); 178} 179 180#endif /* EGL_MESA_drm_image */ 181 182_EGLImage * 183egl_g3d_create_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx, 184 EGLenum target, EGLClientBuffer buffer, 185 const EGLint *attribs) 186{ 187 struct pipe_resource *ptex; 188 struct egl_g3d_image *gimg; 189 unsigned level = 0, layer = 0; 190 191 gimg = CALLOC_STRUCT(egl_g3d_image); 192 if (!gimg) { 193 _eglError(EGL_BAD_ALLOC, "eglCreateEGLImageKHR"); 194 return NULL; 195 } 196 197 if (!_eglInitImage(&gimg->base, dpy)) { 198 FREE(gimg); 199 return NULL; 200 } 201 202 switch (target) { 203 case EGL_NATIVE_PIXMAP_KHR: 204 ptex = egl_g3d_reference_native_pixmap(dpy, 205 (EGLNativePixmapType) buffer); 206 break; 207#ifdef EGL_MESA_drm_image 208 case EGL_DRM_BUFFER_MESA: 209 ptex = egl_g3d_reference_drm_buffer(dpy, 210 (EGLint) buffer, &gimg->base, attribs); 211 break; 212#endif 213 default: 214 ptex = NULL; 215 break; 216 } 217 218 if (!ptex) { 219 FREE(gimg); 220 return NULL; 221 } 222 223 if (level > ptex->last_level) { 224 _eglError(EGL_BAD_MATCH, "eglCreateEGLImageKHR"); 225 pipe_resource_reference(&gimg->texture, NULL); 226 FREE(gimg); 227 return NULL; 228 } 229 if (layer >= (u_minify(ptex->depth0, level) + ptex->array_size - 1)) { 230 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR"); 231 pipe_resource_reference(&gimg->texture, NULL); 232 FREE(gimg); 233 return NULL; 234 } 235 236 /* transfer the ownership to the image */ 237 gimg->texture = ptex; 238 gimg->level = level; 239 gimg->layer = layer; 240 241 return &gimg->base; 242} 243 244EGLBoolean 245egl_g3d_destroy_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img) 246{ 247 struct egl_g3d_image *gimg = egl_g3d_image(img); 248 249 pipe_resource_reference(&gimg->texture, NULL); 250 FREE(gimg); 251 252 return EGL_TRUE; 253} 254 255_EGLImage * 256egl_g3d_create_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, 257 const EGLint *attribs) 258{ 259 struct egl_g3d_image *gimg; 260 struct pipe_resource *ptex; 261 262 gimg = CALLOC_STRUCT(egl_g3d_image); 263 if (!gimg) { 264 _eglError(EGL_BAD_ALLOC, "eglCreateDRMImageKHR"); 265 return NULL; 266 } 267 268 if (!_eglInitImage(&gimg->base, dpy)) { 269 FREE(gimg); 270 return NULL; 271 } 272 273#ifdef EGL_MESA_drm_image 274 ptex = egl_g3d_create_drm_buffer(dpy, &gimg->base, attribs); 275#else 276 ptex = NULL; 277#endif 278 if (!ptex) { 279 FREE(gimg); 280 return NULL; 281 } 282 283 /* transfer the ownership to the image */ 284 gimg->texture = ptex; 285 gimg->level = 0; 286 gimg->layer = 0; 287 288 return &gimg->base; 289} 290 291EGLBoolean 292egl_g3d_export_drm_image(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img, 293 EGLint *name, EGLint *handle, EGLint *stride) 294{ 295 struct egl_g3d_display *gdpy = egl_g3d_display(dpy); 296 struct egl_g3d_image *gimg = egl_g3d_image(img); 297 struct winsys_handle wsh; 298 299 if (!dpy->Extensions.MESA_drm_image) 300 return EGL_FALSE; 301 302 /* get shared handle */ 303 if (name) { 304 memset(&handle, 0, sizeof(handle)); 305 wsh.type = DRM_API_HANDLE_TYPE_SHARED; 306 if (!gdpy->native->buffer->export_buffer(gdpy->native, 307 gimg->texture, &wsh)) 308 return EGL_FALSE; 309 310 *name = wsh.handle; 311 } 312 313 /* get KMS handle */ 314 if (handle || stride) { 315 memset(&wsh, 0, sizeof(wsh)); 316 wsh.type = DRM_API_HANDLE_TYPE_KMS; 317 if (!gdpy->native->buffer->export_buffer(gdpy->native, 318 gimg->texture, &wsh)) 319 return EGL_FALSE; 320 321 if (handle) 322 *handle = wsh.handle; 323 if (stride) 324 *stride = wsh.stride; 325 } 326 327 return EGL_TRUE; 328} 329