drisw.c revision 98aa2a8f725e44aec8bd998fe436a134e94f13bb
1/************************************************************************** 2 * 3 * Copyright 2009, VMware, Inc. 4 * All Rights Reserved. 5 * Copyright 2010 George Sapountzis <gsapountzis@gmail.com> 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * 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 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 29/* TODO: 30 * 31 * xshm / EGLImage: 32 * 33 * Allow the loaders to use the XSHM extension. It probably requires callbacks 34 * for createImage/destroyImage similar to DRI2 getBuffers. 35 */ 36 37#include "util/u_format.h" 38#include "util/u_memory.h" 39#include "util/u_inlines.h" 40#include "pipe/p_context.h" 41#include "state_tracker/drisw_api.h" 42#include "state_tracker/st_context.h" 43 44#include "dri_screen.h" 45#include "dri_context.h" 46#include "dri_drawable.h" 47 48DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present, "SWRAST_NO_PRESENT", FALSE); 49static boolean swrast_no_present = FALSE; 50 51static INLINE void 52get_drawable_info(__DRIdrawable *dPriv, int *x, int *y, int *w, int *h) 53{ 54 __DRIscreen *sPriv = dPriv->driScreenPriv; 55 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader; 56 57 loader->getDrawableInfo(dPriv, 58 x, y, w, h, 59 dPriv->loaderPrivate); 60} 61 62static INLINE void 63put_image(__DRIdrawable *dPriv, void *data, unsigned width, unsigned height) 64{ 65 __DRIscreen *sPriv = dPriv->driScreenPriv; 66 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader; 67 68 loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP, 69 0, 0, width, height, 70 data, dPriv->loaderPrivate); 71} 72 73static void 74drisw_update_drawable_info(struct dri_drawable *drawable) 75{ 76 __DRIdrawable *dPriv = drawable->dPriv; 77 int x, y; 78 79 get_drawable_info(dPriv, &x, &y, &dPriv->w, &dPriv->h); 80} 81 82static void 83drisw_put_image(struct dri_drawable *drawable, 84 void *data, unsigned width, unsigned height) 85{ 86 __DRIdrawable *dPriv = drawable->dPriv; 87 88 put_image(dPriv, data, width, height); 89} 90 91static INLINE void 92drisw_present_texture(__DRIdrawable *dPriv, 93 struct pipe_resource *ptex) 94{ 95 struct dri_drawable *drawable = dri_drawable(dPriv); 96 struct dri_screen *screen = dri_screen(drawable->sPriv); 97 98 if (swrast_no_present) 99 return; 100 101 screen->base.screen->flush_frontbuffer(screen->base.screen, ptex, 0, 0, drawable); 102} 103 104static INLINE void 105drisw_invalidate_drawable(__DRIdrawable *dPriv) 106{ 107 struct dri_drawable *drawable = dri_drawable(dPriv); 108 109 drawable->texture_stamp = dPriv->lastStamp - 1; 110 111 p_atomic_inc(&drawable->base.stamp); 112} 113 114static INLINE void 115drisw_copy_to_front(__DRIdrawable * dPriv, 116 struct pipe_resource *ptex) 117{ 118 drisw_present_texture(dPriv, ptex); 119 120 drisw_invalidate_drawable(dPriv); 121} 122 123/* 124 * Backend functions for st_framebuffer interface and swap_buffers. 125 */ 126 127static void 128drisw_swap_buffers(__DRIdrawable *dPriv) 129{ 130 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv); 131 struct dri_drawable *drawable = dri_drawable(dPriv); 132 struct pipe_resource *ptex; 133 134 if (!ctx) 135 return; 136 137 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT]; 138 139 if (ptex) { 140 if (ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]) 141 pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]); 142 143 ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL); 144 145 drisw_copy_to_front(dPriv, ptex); 146 } 147} 148 149static void 150drisw_flush_frontbuffer(struct dri_drawable *drawable, 151 enum st_attachment_type statt) 152{ 153 struct dri_context *ctx = dri_get_current(drawable->sPriv); 154 struct pipe_resource *ptex; 155 156 if (!ctx) 157 return; 158 159 ptex = drawable->textures[statt]; 160 161 if (ptex) { 162 drisw_copy_to_front(ctx->dPriv, ptex); 163 } 164} 165 166/** 167 * Allocate framebuffer attachments. 168 * 169 * During fixed-size operation, the function keeps allocating new attachments 170 * as they are requested. Unused attachments are not removed, not until the 171 * framebuffer is resized or destroyed. 172 */ 173static void 174drisw_allocate_textures(struct dri_drawable *drawable, 175 const enum st_attachment_type *statts, 176 unsigned count) 177{ 178 struct dri_screen *screen = dri_screen(drawable->sPriv); 179 struct pipe_resource templ; 180 unsigned width, height; 181 boolean resized; 182 unsigned i; 183 184 width = drawable->dPriv->w; 185 height = drawable->dPriv->h; 186 187 resized = (drawable->old_w != width || 188 drawable->old_h != height); 189 190 /* remove outdated textures */ 191 if (resized) { 192 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 193 pipe_resource_reference(&drawable->textures[i], NULL); 194 } 195 196 memset(&templ, 0, sizeof(templ)); 197 templ.target = screen->target; 198 templ.width0 = width; 199 templ.height0 = height; 200 templ.depth0 = 1; 201 templ.array_size = 1; 202 templ.last_level = 0; 203 204 for (i = 0; i < count; i++) { 205 enum pipe_format format; 206 unsigned bind; 207 208 /* the texture already exists or not requested */ 209 if (drawable->textures[statts[i]]) 210 continue; 211 212 dri_drawable_get_format(drawable, statts[i], &format, &bind); 213 214 /* if we don't do any present, no need for display targets */ 215 if (statts[i] != ST_ATTACHMENT_DEPTH_STENCIL && !swrast_no_present) 216 bind |= PIPE_BIND_DISPLAY_TARGET; 217 218 if (format == PIPE_FORMAT_NONE) 219 continue; 220 221 templ.format = format; 222 templ.bind = bind; 223 224 drawable->textures[statts[i]] = 225 screen->base.screen->resource_create(screen->base.screen, &templ); 226 } 227 228 drawable->old_w = width; 229 drawable->old_h = height; 230} 231 232static void 233drisw_update_tex_buffer(struct dri_drawable *drawable, 234 struct dri_context *ctx, 235 struct pipe_resource *res) 236{ 237 struct pipe_context *pipe = ((struct st_context *) ctx)->st->pipe; 238 __DRIdrawable *dPriv = drawable->dPriv; 239 __DRIscreen *sPriv = dPriv->driScreenPriv; 240 int x, y, w, h; 241 struct pipe_transfer *transfer; 242 char *map; 243 int ximage_stride, line; 244 245 get_drawable_info(dPriv, &x, &y, &w, &h); 246 247 transfer = pipe_get_transfer(pipe, res, 248 0, 0, // level, layer, 249 PIPE_TRANSFER_WRITE, 250 x, y, w, h); 251 map = pipe_transfer_map(pipe, transfer); 252 253 /* Copy the Drawable content to the mapped texture buffer */ 254 sPriv->swrast_loader->getImage(dPriv, x, y, w, h, map, dPriv->loaderPrivate); 255 256 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels. 257 We assume 32 bit pixels. */ 258 ximage_stride = w * 4; 259 for (line = h-1; line; --line) { 260 memmove(&map[line * transfer->stride], 261 &map[line * ximage_stride], 262 ximage_stride); 263 } 264 265 pipe_transfer_unmap(pipe, transfer); 266 pipe_transfer_destroy(pipe, transfer); 267} 268 269/* 270 * Backend function for init_screen. 271 */ 272 273static const __DRIextension *drisw_screen_extensions[] = { 274 &driTexBufferExtension.base, 275 NULL 276}; 277 278static struct drisw_loader_funcs drisw_lf = { 279 .put_image = drisw_put_image 280}; 281 282static const __DRIconfig ** 283drisw_init_screen(__DRIscreen * sPriv) 284{ 285 const __DRIconfig **configs; 286 struct dri_screen *screen; 287 struct pipe_screen *pscreen; 288 289 screen = CALLOC_STRUCT(dri_screen); 290 if (!screen) 291 return NULL; 292 293 screen->sPriv = sPriv; 294 screen->fd = -1; 295 296 swrast_no_present = debug_get_option_swrast_no_present(); 297 298 sPriv->private = (void *)screen; 299 sPriv->extensions = drisw_screen_extensions; 300 301 pscreen = drisw_create_screen(&drisw_lf); 302 /* dri_init_screen_helper checks pscreen for us */ 303 304 configs = dri_init_screen_helper(screen, pscreen, 32); 305 if (!configs) 306 goto fail; 307 308 return configs; 309fail: 310 dri_destroy_screen_helper(screen); 311 FREE(screen); 312 return NULL; 313} 314 315static boolean 316drisw_create_buffer(__DRIscreen * sPriv, 317 __DRIdrawable * dPriv, 318 const struct gl_config * visual, boolean isPixmap) 319{ 320 struct dri_drawable *drawable = NULL; 321 322 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap)) 323 return FALSE; 324 325 drawable = dPriv->driverPrivate; 326 327 drawable->allocate_textures = drisw_allocate_textures; 328 drawable->update_drawable_info = drisw_update_drawable_info; 329 drawable->flush_frontbuffer = drisw_flush_frontbuffer; 330 drawable->update_tex_buffer = drisw_update_tex_buffer; 331 332 return TRUE; 333} 334 335/** 336 * DRI driver virtual function table. 337 * 338 * DRI versions differ in their implementation of init_screen and swap_buffers. 339 */ 340const struct __DriverAPIRec driDriverAPI = { 341 .InitScreen = drisw_init_screen, 342 .DestroyScreen = dri_destroy_screen, 343 .CreateContext = dri_create_context, 344 .DestroyContext = dri_destroy_context, 345 .CreateBuffer = drisw_create_buffer, 346 .DestroyBuffer = dri_destroy_buffer, 347 .MakeCurrent = dri_make_current, 348 .UnbindContext = dri_unbind_context, 349 350 .SwapBuffers = drisw_swap_buffers, 351}; 352 353/* This is the table of extensions that the loader will dlsym() for. */ 354PUBLIC const __DRIextension *__driDriverExtensions[] = { 355 &driCoreExtension.base, 356 &driSWRastExtension.base, 357 NULL 358}; 359 360/* vim: set sw=3 ts=8 sts=3 expandtab: */ 361