dri2.c revision a523b622036117e8bc1be0a1496d4368562e9702
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.9 4 * 5 * Copyright 2009, VMware, Inc. 6 * All Rights Reserved. 7 * Copyright (C) 2010 LunarG Inc. 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a 10 * copy of this software and associated documentation files (the "Software"), 11 * to deal in the Software without restriction, including without limitation 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 * and/or sell copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included 17 * in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 * Authors: 27 * Keith Whitwell <keithw@vmware.com> 28 * Jakob Bornecrantz <wallbraker@gmail.com> 29 * Chia-I Wu <olv@lunarg.com> 30 */ 31 32#include "util/u_memory.h" 33#include "util/u_inlines.h" 34#include "util/u_format.h" 35#include "util/u_debug.h" 36#include "state_tracker/drm_api.h" 37 38#include "dri_screen.h" 39#include "dri_context.h" 40#include "dri_drawable.h" 41#include "dri_st_api.h" 42#include "dri2.h" 43 44/** 45 * DRI2 flush extension. 46 */ 47static void 48dri2_flush_drawable(__DRIdrawable *draw) 49{ 50} 51 52static void 53dri2_invalidate_drawable(__DRIdrawable *dPriv) 54{ 55 struct dri_drawable *drawable = dri_drawable(dPriv); 56 struct dri_context *ctx = dri_context(dPriv->driContextPriv); 57 58 dri2InvalidateDrawable(dPriv); 59 drawable->dPriv->lastStamp = *drawable->dPriv->pStamp; 60 61 if (ctx) 62 ctx->st->notify_invalid_framebuffer(ctx->st, drawable->stfb); 63} 64 65static const __DRI2flushExtension dri2FlushExtension = { 66 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION }, 67 dri2_flush_drawable, 68 dri2_invalidate_drawable, 69}; 70 71/** 72 * These are used for GLX_EXT_texture_from_pixmap 73 */ 74static void 75dri2_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target, 76 GLint format, __DRIdrawable *dPriv) 77{ 78 struct dri_context *ctx = dri_context(pDRICtx); 79 struct dri_drawable *drawable = dri_drawable(dPriv); 80 struct pipe_resource *pt; 81 82 dri_st_framebuffer_validate_att(drawable->stfb, ST_ATTACHMENT_FRONT_LEFT); 83 84 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT]; 85 86 if (pt) { 87 enum pipe_format internal_format = pt->format; 88 89 if (format == __DRI_TEXTURE_FORMAT_RGB) { 90 /* only need to cover the formats recognized by dri_fill_st_visual */ 91 switch (internal_format) { 92 case PIPE_FORMAT_B8G8R8A8_UNORM: 93 internal_format = PIPE_FORMAT_B8G8R8X8_UNORM; 94 break; 95 case PIPE_FORMAT_A8R8G8B8_UNORM: 96 internal_format = PIPE_FORMAT_X8R8G8B8_UNORM; 97 break; 98 default: 99 break; 100 } 101 } 102 103 ctx->st->teximage(ctx->st, 104 (target == GL_TEXTURE_2D) ? ST_TEXTURE_2D : ST_TEXTURE_RECT, 105 0, internal_format, pt, FALSE); 106 } 107} 108 109static void 110dri2_set_tex_buffer(__DRIcontext *pDRICtx, GLint target, 111 __DRIdrawable *dPriv) 112{ 113 dri2_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv); 114} 115 116static const __DRItexBufferExtension dri2TexBufferExtension = { 117 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION }, 118 dri2_set_tex_buffer, 119 dri2_set_tex_buffer2, 120}; 121 122/** 123 * Get the format and binding of an attachment. 124 */ 125static INLINE void 126dri2_drawable_get_format(struct dri_drawable *drawable, 127 enum st_attachment_type statt, 128 enum pipe_format *format, 129 unsigned *bind) 130{ 131 switch (statt) { 132 case ST_ATTACHMENT_FRONT_LEFT: 133 case ST_ATTACHMENT_BACK_LEFT: 134 case ST_ATTACHMENT_FRONT_RIGHT: 135 case ST_ATTACHMENT_BACK_RIGHT: 136 *format = drawable->stvis.color_format; 137 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW; 138 break; 139 case ST_ATTACHMENT_DEPTH_STENCIL: 140 *format = drawable->stvis.depth_stencil_format; 141 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */ 142 break; 143 default: 144 *format = PIPE_FORMAT_NONE; 145 *bind = 0; 146 break; 147 } 148} 149 150 151/** 152 * Retrieve __DRIbuffer from the DRI loader. 153 */ 154static __DRIbuffer * 155dri2_drawable_get_buffers(struct dri_drawable *drawable, 156 const enum st_attachment_type *statts, 157 unsigned *count) 158{ 159 __DRIdrawable *dri_drawable = drawable->dPriv; 160 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader; 161 boolean with_format; 162 __DRIbuffer *buffers; 163 int num_buffers; 164 unsigned attachments[10]; 165 unsigned num_attachments, i; 166 167 assert(loader); 168 with_format = dri_with_format(drawable->sPriv); 169 170 num_attachments = 0; 171 172 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */ 173 if (!with_format) 174 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT; 175 176 for (i = 0; i < *count; i++) { 177 enum pipe_format format; 178 unsigned bind; 179 int att, bpp; 180 181 dri2_drawable_get_format(drawable, statts[i], &format, &bind); 182 if (format == PIPE_FORMAT_NONE) 183 continue; 184 185 switch (statts[i]) { 186 case ST_ATTACHMENT_FRONT_LEFT: 187 /* already added */ 188 if (!with_format) 189 continue; 190 att = __DRI_BUFFER_FRONT_LEFT; 191 break; 192 case ST_ATTACHMENT_BACK_LEFT: 193 att = __DRI_BUFFER_BACK_LEFT; 194 break; 195 case ST_ATTACHMENT_FRONT_RIGHT: 196 att = __DRI_BUFFER_FRONT_RIGHT; 197 break; 198 case ST_ATTACHMENT_BACK_RIGHT: 199 att = __DRI_BUFFER_BACK_RIGHT; 200 break; 201 case ST_ATTACHMENT_DEPTH_STENCIL: 202 att = __DRI_BUFFER_DEPTH_STENCIL; 203 break; 204 default: 205 att = -1; 206 break; 207 } 208 209 bpp = util_format_get_blocksizebits(format); 210 211 if (att >= 0) { 212 attachments[num_attachments++] = att; 213 if (with_format) { 214 attachments[num_attachments++] = bpp; 215 } 216 } 217 } 218 219 if (with_format) { 220 num_attachments /= 2; 221 buffers = loader->getBuffersWithFormat(dri_drawable, 222 &dri_drawable->w, &dri_drawable->h, 223 attachments, num_attachments, 224 &num_buffers, dri_drawable->loaderPrivate); 225 } 226 else { 227 buffers = loader->getBuffers(dri_drawable, 228 &dri_drawable->w, &dri_drawable->h, 229 attachments, num_attachments, 230 &num_buffers, dri_drawable->loaderPrivate); 231 } 232 233 if (buffers) { 234 /* set one cliprect to cover the whole dri_drawable */ 235 dri_drawable->x = 0; 236 dri_drawable->y = 0; 237 dri_drawable->backX = 0; 238 dri_drawable->backY = 0; 239 dri_drawable->numClipRects = 1; 240 dri_drawable->pClipRects[0].x1 = 0; 241 dri_drawable->pClipRects[0].y1 = 0; 242 dri_drawable->pClipRects[0].x2 = dri_drawable->w; 243 dri_drawable->pClipRects[0].y2 = dri_drawable->h; 244 dri_drawable->numBackClipRects = 1; 245 dri_drawable->pBackClipRects[0].x1 = 0; 246 dri_drawable->pBackClipRects[0].y1 = 0; 247 dri_drawable->pBackClipRects[0].x2 = dri_drawable->w; 248 dri_drawable->pBackClipRects[0].y2 = dri_drawable->h; 249 250 *count = num_buffers; 251 } 252 253 return buffers; 254} 255 256/** 257 * Process __DRIbuffer and convert them into pipe_resources. 258 */ 259static void 260dri2_drawable_process_buffers(struct dri_drawable *drawable, 261 __DRIbuffer *buffers, unsigned count) 262{ 263 struct dri_screen *screen = dri_screen(drawable->sPriv); 264 __DRIdrawable *dri_drawable = drawable->dPriv; 265 struct pipe_resource templ; 266 struct winsys_handle whandle; 267 boolean have_depth = FALSE; 268 unsigned i, bind; 269 270 if (drawable->old_num == count && 271 drawable->old_w == dri_drawable->w && 272 drawable->old_h == dri_drawable->h && 273 memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0) 274 return; 275 276 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 277 pipe_resource_reference(&drawable->textures[i], NULL); 278 279 memset(&templ, 0, sizeof(templ)); 280 templ.target = PIPE_TEXTURE_2D; 281 templ.last_level = 0; 282 templ.width0 = dri_drawable->w; 283 templ.height0 = dri_drawable->h; 284 templ.depth0 = 1; 285 286 memset(&whandle, 0, sizeof(whandle)); 287 288 for (i = 0; i < count; i++) { 289 __DRIbuffer *buf = &buffers[i]; 290 enum st_attachment_type statt; 291 enum pipe_format format; 292 293 switch (buf->attachment) { 294 case __DRI_BUFFER_FRONT_LEFT: 295 if (!screen->auto_fake_front) { 296 statt = ST_ATTACHMENT_INVALID; 297 break; 298 } 299 /* fallthrough */ 300 case __DRI_BUFFER_FAKE_FRONT_LEFT: 301 statt = ST_ATTACHMENT_FRONT_LEFT; 302 break; 303 case __DRI_BUFFER_BACK_LEFT: 304 statt = ST_ATTACHMENT_BACK_LEFT; 305 break; 306 case __DRI_BUFFER_DEPTH: 307 case __DRI_BUFFER_DEPTH_STENCIL: 308 case __DRI_BUFFER_STENCIL: 309 /* use only the first depth/stencil buffer */ 310 if (!have_depth) { 311 have_depth = TRUE; 312 statt = ST_ATTACHMENT_DEPTH_STENCIL; 313 } 314 else { 315 statt = ST_ATTACHMENT_INVALID; 316 } 317 break; 318 default: 319 statt = ST_ATTACHMENT_INVALID; 320 break; 321 } 322 323 dri2_drawable_get_format(drawable, statt, &format, &bind); 324 if (statt == ST_ATTACHMENT_INVALID || format == PIPE_FORMAT_NONE) 325 continue; 326 327 templ.format = format; 328 templ.bind = bind; 329 whandle.handle = buf->name; 330 whandle.stride = buf->pitch; 331 332 drawable->textures[statt] = 333 screen->pipe_screen->resource_from_handle(screen->pipe_screen, 334 &templ, &whandle); 335 } 336 337 drawable->old_num = count; 338 drawable->old_w = dri_drawable->w; 339 drawable->old_h = dri_drawable->h; 340 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count); 341} 342 343/* 344 * Backend functions for st_framebuffer interface. 345 */ 346 347void 348dri2_allocate_textures(struct dri_drawable *drawable, 349 const enum st_attachment_type *statts, 350 unsigned count) 351{ 352 __DRIbuffer *buffers; 353 unsigned num_buffers = count; 354 355 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers); 356 dri2_drawable_process_buffers(drawable, buffers, num_buffers); 357} 358 359void 360dri2_flush_frontbuffer(struct dri_drawable *drawable, 361 enum st_attachment_type statt) 362{ 363 __DRIdrawable *dri_drawable = drawable->dPriv; 364 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader; 365 366 if (loader->flushFrontBuffer == NULL) 367 return; 368 369 if (statt == ST_ATTACHMENT_FRONT_LEFT) { 370 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate); 371 } 372} 373 374__DRIimage * 375dri2_lookup_egl_image(struct dri_context *ctx, void *handle) 376{ 377 __DRIimageLookupExtension *loader = ctx->sPriv->dri2.image; 378 __DRIimage *img; 379 380 if (!loader->lookupEGLImage) 381 return NULL; 382 383 img = loader->lookupEGLImage(ctx->cPriv, handle, ctx->cPriv->loaderPrivate); 384 385 return img; 386} 387 388static __DRIimage * 389dri2_create_image_from_name(__DRIcontext *context, 390 int width, int height, int format, 391 int name, int pitch, void *loaderPrivate) 392{ 393 struct dri_screen *screen = dri_screen(context->driScreenPriv); 394 __DRIimage *img; 395 struct pipe_resource templ; 396 struct winsys_handle whandle; 397 unsigned tex_usage; 398 enum pipe_format pf; 399 400 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW; 401 402 switch (format) { 403 case __DRI_IMAGE_FORMAT_RGB565: 404 pf = PIPE_FORMAT_B5G6R5_UNORM; 405 break; 406 case __DRI_IMAGE_FORMAT_XRGB8888: 407 pf = PIPE_FORMAT_B8G8R8X8_UNORM; 408 break; 409 case __DRI_IMAGE_FORMAT_ARGB8888: 410 pf = PIPE_FORMAT_B8G8R8A8_UNORM; 411 break; 412 default: 413 pf = PIPE_FORMAT_NONE; 414 break; 415 } 416 if (pf == PIPE_FORMAT_NONE) 417 return NULL; 418 419 img = CALLOC_STRUCT(__DRIimageRec); 420 if (!img) 421 return NULL; 422 423 memset(&templ, 0, sizeof(templ)); 424 templ.bind = tex_usage; 425 templ.format = pf; 426 templ.target = PIPE_TEXTURE_2D; 427 templ.last_level = 0; 428 templ.width0 = width; 429 templ.height0 = height; 430 templ.depth0 = 1; 431 432 memset(&whandle, 0, sizeof(whandle)); 433 whandle.handle = name; 434 whandle.stride = pitch * util_format_get_blocksize(pf); 435 436 img->texture = screen->pipe_screen->resource_from_handle(screen->pipe_screen, 437 &templ, &whandle); 438 if (!img->texture) { 439 FREE(img); 440 return NULL; 441 } 442 443 img->face = 0; 444 img->level = 0; 445 img->zslice = 0; 446 img->loader_private = loaderPrivate; 447 448 return img; 449} 450 451static __DRIimage * 452dri2_create_image_from_renderbuffer(__DRIcontext *context, 453 int renderbuffer, void *loaderPrivate) 454{ 455 struct dri_context *ctx = dri_context(context->driverPrivate); 456 457 if (!ctx->st->get_resource_for_egl_image) 458 return NULL; 459 460 /* TODO */ 461 return NULL; 462} 463 464static void 465dri2_destroy_image(__DRIimage *img) 466{ 467 pipe_resource_reference(&img->texture, NULL); 468 FREE(img); 469} 470 471static struct __DRIimageExtensionRec dri2ImageExtension = { 472 { __DRI_IMAGE, __DRI_IMAGE_VERSION }, 473 dri2_create_image_from_name, 474 dri2_create_image_from_renderbuffer, 475 dri2_destroy_image, 476}; 477 478/* 479 * Backend function init_screen. 480 */ 481 482static const __DRIextension *dri_screen_extensions[] = { 483 &driReadDrawableExtension, 484 &driCopySubBufferExtension.base, 485 &driSwapControlExtension.base, 486 &driFrameTrackingExtension.base, 487 &driMediaStreamCounterExtension.base, 488 &dri2TexBufferExtension.base, 489 &dri2FlushExtension.base, 490 &dri2ImageExtension.base, 491 NULL 492}; 493 494/** 495 * This is the driver specific part of the createNewScreen entry point. 496 * 497 * Returns the __GLcontextModes supported by this driver. 498 */ 499const __DRIconfig ** 500dri2_init_screen(__DRIscreen * sPriv) 501{ 502 const __DRIconfig **configs; 503 struct dri_screen *screen; 504 struct pipe_screen *pscreen; 505 506 screen = CALLOC_STRUCT(dri_screen); 507 if (!screen) 508 return NULL; 509 510 screen->api = drm_api_create(); 511 screen->sPriv = sPriv; 512 screen->fd = sPriv->fd; 513 514 sPriv->private = (void *)screen; 515 sPriv->extensions = dri_screen_extensions; 516 517 pscreen = screen->api->create_screen(screen->api, screen->fd, NULL); 518 /* dri_init_screen_helper checks pscreen for us */ 519 520 configs = dri_init_screen_helper(screen, pscreen, 32); 521 if (!configs) 522 goto fail; 523 524 screen->auto_fake_front = dri_with_format(sPriv); 525 526 return configs; 527fail: 528 dri_destroy_screen_helper(screen); 529 FREE(screen); 530 return NULL; 531} 532 533/* This is the table of extensions that the loader will dlsym() for. */ 534PUBLIC const __DRIextension *__driDriverExtensions[] = { 535 &driCoreExtension.base, 536 &driLegacyExtension.base, 537 &driDRI2Extension.base, 538 NULL 539}; 540 541/* vim: set sw=3 ts=8 sts=3 expandtab: */ 542