1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
5 * Copyright 2010 George Sapountzis <gsapountzis@gmail.com>
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 #include "util/u_format.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/u_box.h"
33 #include "pipe/p_context.h"
34 #include "pipe-loader/pipe_loader.h"
35 #include "state_tracker/drisw_api.h"
36 #include "state_tracker/st_context.h"
38 #include "dri_screen.h"
39 #include "dri_context.h"
40 #include "dri_drawable.h"
41 #include "dri_helpers.h"
42 #include "dri_query_renderer.h"
44 DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present
, "SWRAST_NO_PRESENT", FALSE
);
47 get_drawable_info(__DRIdrawable
*dPriv
, int *x
, int *y
, int *w
, int *h
)
49 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
50 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
52 loader
->getDrawableInfo(dPriv
,
54 dPriv
->loaderPrivate
);
58 put_image(__DRIdrawable
*dPriv
, void *data
, unsigned width
, unsigned height
)
60 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
61 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
63 loader
->putImage(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
65 data
, dPriv
->loaderPrivate
);
69 put_image2(__DRIdrawable
*dPriv
, void *data
, int x
, int y
,
70 unsigned width
, unsigned height
, unsigned stride
)
72 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
73 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
75 loader
->putImage2(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
76 x
, y
, width
, height
, stride
,
77 data
, dPriv
->loaderPrivate
);
81 put_image_shm(__DRIdrawable
*dPriv
, int shmid
, char *shmaddr
,
82 unsigned offset
, unsigned offset_x
, int x
, int y
,
83 unsigned width
, unsigned height
, unsigned stride
)
85 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
86 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
88 /* if we have the newer interface, don't have to add the offset_x here. */
89 if (loader
->base
.version
> 4 && loader
->putImageShm2
)
90 loader
->putImageShm2(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
91 x
, y
, width
, height
, stride
,
92 shmid
, shmaddr
, offset
, dPriv
->loaderPrivate
);
94 loader
->putImageShm(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
95 x
, y
, width
, height
, stride
,
96 shmid
, shmaddr
, offset
+ offset_x
, dPriv
->loaderPrivate
);
100 get_image(__DRIdrawable
*dPriv
, int x
, int y
, int width
, int height
, void *data
)
102 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
103 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
105 loader
->getImage(dPriv
,
107 data
, dPriv
->loaderPrivate
);
111 get_image2(__DRIdrawable
*dPriv
, int x
, int y
, int width
, int height
, int stride
, void *data
)
113 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
114 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
116 /* getImage2 support is only in version 3 or newer */
117 if (loader
->base
.version
< 3)
120 loader
->getImage2(dPriv
,
121 x
, y
, width
, height
, stride
,
122 data
, dPriv
->loaderPrivate
);
126 get_image_shm(__DRIdrawable
*dPriv
, int x
, int y
, int width
, int height
,
127 struct pipe_resource
*res
)
129 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
130 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
131 struct winsys_handle whandle
;
133 whandle
.type
= WINSYS_HANDLE_TYPE_SHMID
;
135 if (loader
->base
.version
< 4 || !loader
->getImageShm
)
138 if (!res
->screen
->resource_get_handle(res
->screen
, NULL
, res
, &whandle
, PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
))
141 loader
->getImageShm(dPriv
, x
, y
, width
, height
, whandle
.handle
, dPriv
->loaderPrivate
);
146 drisw_update_drawable_info(struct dri_drawable
*drawable
)
148 __DRIdrawable
*dPriv
= drawable
->dPriv
;
151 get_drawable_info(dPriv
, &x
, &y
, &dPriv
->w
, &dPriv
->h
);
155 drisw_get_image(struct dri_drawable
*drawable
,
156 int x
, int y
, unsigned width
, unsigned height
, unsigned stride
,
159 __DRIdrawable
*dPriv
= drawable
->dPriv
;
160 int draw_x
, draw_y
, draw_w
, draw_h
;
162 get_drawable_info(dPriv
, &draw_x
, &draw_y
, &draw_w
, &draw_h
);
163 get_image2(dPriv
, x
, y
, draw_w
, draw_h
, stride
, data
);
167 drisw_put_image(struct dri_drawable
*drawable
,
168 void *data
, unsigned width
, unsigned height
)
170 __DRIdrawable
*dPriv
= drawable
->dPriv
;
172 put_image(dPriv
, data
, width
, height
);
176 drisw_put_image2(struct dri_drawable
*drawable
,
177 void *data
, int x
, int y
, unsigned width
, unsigned height
,
180 __DRIdrawable
*dPriv
= drawable
->dPriv
;
182 put_image2(dPriv
, data
, x
, y
, width
, height
, stride
);
186 drisw_put_image_shm(struct dri_drawable
*drawable
,
187 int shmid
, char *shmaddr
, unsigned offset
,
189 int x
, int y
, unsigned width
, unsigned height
,
192 __DRIdrawable
*dPriv
= drawable
->dPriv
;
194 put_image_shm(dPriv
, shmid
, shmaddr
, offset
, offset_x
, x
, y
, width
, height
, stride
);
198 drisw_present_texture(__DRIdrawable
*dPriv
,
199 struct pipe_resource
*ptex
, struct pipe_box
*sub_box
)
201 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
202 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
204 if (screen
->swrast_no_present
)
207 screen
->base
.screen
->flush_frontbuffer(screen
->base
.screen
, ptex
, 0, 0, drawable
, sub_box
);
211 drisw_invalidate_drawable(__DRIdrawable
*dPriv
)
213 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
215 drawable
->texture_stamp
= dPriv
->lastStamp
- 1;
217 p_atomic_inc(&drawable
->base
.stamp
);
221 drisw_copy_to_front(__DRIdrawable
* dPriv
,
222 struct pipe_resource
*ptex
)
224 drisw_present_texture(dPriv
, ptex
, NULL
);
226 drisw_invalidate_drawable(dPriv
);
230 * Backend functions for st_framebuffer interface and swap_buffers.
234 drisw_swap_buffers(__DRIdrawable
*dPriv
)
236 struct dri_context
*ctx
= dri_get_current(dPriv
->driScreenPriv
);
237 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
238 struct pipe_resource
*ptex
;
243 ptex
= drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
247 pp_run(ctx
->pp
, ptex
, ptex
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
249 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
251 drisw_copy_to_front(dPriv
, ptex
);
256 drisw_copy_sub_buffer(__DRIdrawable
*dPriv
, int x
, int y
,
259 struct dri_context
*ctx
= dri_get_current(dPriv
->driScreenPriv
);
260 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
261 struct pipe_resource
*ptex
;
266 ptex
= drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
269 if (ctx
->pp
&& drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
270 pp_run(ctx
->pp
, ptex
, ptex
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
272 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
274 u_box_2d(x
, dPriv
->h
- y
- h
, w
, h
, &box
);
275 drisw_present_texture(dPriv
, ptex
, &box
);
280 drisw_flush_frontbuffer(struct dri_context
*ctx
,
281 struct dri_drawable
*drawable
,
282 enum st_attachment_type statt
)
284 struct pipe_resource
*ptex
;
289 ptex
= drawable
->textures
[statt
];
292 drisw_copy_to_front(ctx
->dPriv
, ptex
);
297 * Allocate framebuffer attachments.
299 * During fixed-size operation, the function keeps allocating new attachments
300 * as they are requested. Unused attachments are not removed, not until the
301 * framebuffer is resized or destroyed.
304 drisw_allocate_textures(struct dri_context
*stctx
,
305 struct dri_drawable
*drawable
,
306 const enum st_attachment_type
*statts
,
309 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
310 const __DRIswrastLoaderExtension
*loader
= drawable
->dPriv
->driScreenPriv
->swrast_loader
;
311 struct pipe_resource templ
;
312 unsigned width
, height
;
316 width
= drawable
->dPriv
->w
;
317 height
= drawable
->dPriv
->h
;
319 resized
= (drawable
->old_w
!= width
||
320 drawable
->old_h
!= height
);
322 /* remove outdated textures */
324 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
325 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
328 memset(&templ
, 0, sizeof(templ
));
329 templ
.target
= screen
->target
;
330 templ
.width0
= width
;
331 templ
.height0
= height
;
333 templ
.array_size
= 1;
334 templ
.last_level
= 0;
336 for (i
= 0; i
< count
; i
++) {
337 enum pipe_format format
;
340 /* the texture already exists or not requested */
341 if (drawable
->textures
[statts
[i
]])
344 dri_drawable_get_format(drawable
, statts
[i
], &format
, &bind
);
346 /* if we don't do any present, no need for display targets */
347 if (statts
[i
] != ST_ATTACHMENT_DEPTH_STENCIL
&& !screen
->swrast_no_present
)
348 bind
|= PIPE_BIND_DISPLAY_TARGET
;
350 if (format
== PIPE_FORMAT_NONE
)
353 templ
.format
= format
;
356 if (statts
[i
] == ST_ATTACHMENT_FRONT_LEFT
&&
357 screen
->base
.screen
->resource_create_front
&&
358 loader
->base
.version
>= 3) {
359 drawable
->textures
[statts
[i
]] =
360 screen
->base
.screen
->resource_create_front(screen
->base
.screen
, &templ
, (const void *)drawable
);
362 drawable
->textures
[statts
[i
]] =
363 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
366 drawable
->old_w
= width
;
367 drawable
->old_h
= height
;
371 drisw_update_tex_buffer(struct dri_drawable
*drawable
,
372 struct dri_context
*ctx
,
373 struct pipe_resource
*res
)
375 __DRIdrawable
*dPriv
= drawable
->dPriv
;
377 struct st_context
*st_ctx
= (struct st_context
*)ctx
->st
;
378 struct pipe_context
*pipe
= st_ctx
->pipe
;
379 struct pipe_transfer
*transfer
;
382 int ximage_stride
, line
;
383 int cpp
= util_format_get_blocksize(res
->format
);
385 get_drawable_info(dPriv
, &x
, &y
, &w
, &h
);
387 map
= pipe_transfer_map(pipe
, res
,
388 0, 0, // level, layer,
390 x
, y
, w
, h
, &transfer
);
392 /* Copy the Drawable content to the mapped texture buffer */
393 if (!get_image_shm(dPriv
, x
, y
, w
, h
, res
))
394 get_image(dPriv
, x
, y
, w
, h
, map
);
396 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
397 get_image() has a pitch rounded up to 4 bytes. */
398 ximage_stride
= ((w
* cpp
) + 3) & -4;
399 for (line
= h
-1; line
; --line
) {
400 memmove(&map
[line
* transfer
->stride
],
401 &map
[line
* ximage_stride
],
405 pipe_transfer_unmap(pipe
, transfer
);
408 static __DRIimageExtension driSWImageExtension
= {
409 .base
= { __DRI_IMAGE
, 6 },
411 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
412 .createImageFromTexture
= dri2_create_from_texture
,
413 .destroyImage
= dri2_destroy_image
,
417 * Backend function for init_screen.
420 static const __DRIextension
*drisw_screen_extensions
[] = {
421 &driTexBufferExtension
.base
,
422 &dri2RendererQueryExtension
.base
,
423 &dri2ConfigQueryExtension
.base
,
424 &dri2FenceExtension
.base
,
425 &dri2NoErrorExtension
.base
,
426 &driSWImageExtension
.base
,
427 &dri2FlushControlExtension
.base
,
431 static const struct drisw_loader_funcs drisw_lf
= {
432 .get_image
= drisw_get_image
,
433 .put_image
= drisw_put_image
,
434 .put_image2
= drisw_put_image2
437 static const struct drisw_loader_funcs drisw_shm_lf
= {
438 .get_image
= drisw_get_image
,
439 .put_image
= drisw_put_image
,
440 .put_image2
= drisw_put_image2
,
441 .put_image_shm
= drisw_put_image_shm
444 static const __DRIconfig
**
445 drisw_init_screen(__DRIscreen
* sPriv
)
447 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
448 const __DRIconfig
**configs
;
449 struct dri_screen
*screen
;
450 struct pipe_screen
*pscreen
= NULL
;
451 const struct drisw_loader_funcs
*lf
= &drisw_lf
;
453 screen
= CALLOC_STRUCT(dri_screen
);
457 screen
->sPriv
= sPriv
;
460 screen
->swrast_no_present
= debug_get_option_swrast_no_present();
462 sPriv
->driverPrivate
= (void *)screen
;
463 sPriv
->extensions
= drisw_screen_extensions
;
464 if (loader
->base
.version
>= 4) {
465 if (loader
->putImageShm
)
469 if (pipe_loader_sw_probe_dri(&screen
->dev
, lf
)) {
470 dri_init_options(screen
);
472 pscreen
= pipe_loader_create_screen(screen
->dev
);
478 configs
= dri_init_screen_helper(screen
, pscreen
);
482 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
486 dri_destroy_screen_helper(screen
);
488 pipe_loader_release(&screen
->dev
, 1);
494 drisw_create_buffer(__DRIscreen
* sPriv
,
495 __DRIdrawable
* dPriv
,
496 const struct gl_config
* visual
, boolean isPixmap
)
498 struct dri_drawable
*drawable
= NULL
;
500 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
503 drawable
= dPriv
->driverPrivate
;
505 drawable
->allocate_textures
= drisw_allocate_textures
;
506 drawable
->update_drawable_info
= drisw_update_drawable_info
;
507 drawable
->flush_frontbuffer
= drisw_flush_frontbuffer
;
508 drawable
->update_tex_buffer
= drisw_update_tex_buffer
;
514 * DRI driver virtual function table.
516 * DRI versions differ in their implementation of init_screen and swap_buffers.
518 const struct __DriverAPIRec galliumsw_driver_api
= {
519 .InitScreen
= drisw_init_screen
,
520 .DestroyScreen
= dri_destroy_screen
,
521 .CreateContext
= dri_create_context
,
522 .DestroyContext
= dri_destroy_context
,
523 .CreateBuffer
= drisw_create_buffer
,
524 .DestroyBuffer
= dri_destroy_buffer
,
525 .SwapBuffers
= drisw_swap_buffers
,
526 .MakeCurrent
= dri_make_current
,
527 .UnbindContext
= dri_unbind_context
,
528 .CopySubBuffer
= drisw_copy_sub_buffer
,
531 /* This is the table of extensions that the loader will dlsym() for. */
532 const __DRIextension
*galliumsw_driver_extensions
[] = {
533 &driCoreExtension
.base
,
534 &driSWRastExtension
.base
,
535 &driCopySubBufferExtension
.base
,
536 &gallium_config_options
.base
,
540 /* vim: set sw=3 ts=8 sts=3 expandtab: */