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 **************************************************************************/
33 * Allow the loaders to use the XSHM extension. It probably requires callbacks
34 * for createImage/destroyImage similar to DRI2 getBuffers.
37 /* XXX: Temporary hack, until we get rid of drisw_create_screen() */
40 #include "util/u_format.h"
41 #include "util/u_memory.h"
42 #include "util/u_inlines.h"
43 #include "util/u_box.h"
44 #include "pipe/p_context.h"
45 #include "state_tracker/drisw_api.h"
46 #include "state_tracker/st_context.h"
48 #include "dri_screen.h"
49 #include "dri_context.h"
50 #include "dri_drawable.h"
51 #include "dri_query_renderer.h"
53 DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present
, "SWRAST_NO_PRESENT", FALSE
);
54 static boolean swrast_no_present
= FALSE
;
57 get_drawable_info(__DRIdrawable
*dPriv
, int *x
, int *y
, int *w
, int *h
)
59 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
60 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
62 loader
->getDrawableInfo(dPriv
,
64 dPriv
->loaderPrivate
);
68 put_image(__DRIdrawable
*dPriv
, void *data
, unsigned width
, unsigned height
)
70 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
71 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
73 loader
->putImage(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
75 data
, dPriv
->loaderPrivate
);
79 put_image2(__DRIdrawable
*dPriv
, void *data
, int x
, int y
,
80 unsigned width
, unsigned height
, unsigned stride
)
82 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
83 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
85 loader
->putImage2(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
86 x
, y
, width
, height
, stride
,
87 data
, dPriv
->loaderPrivate
);
91 get_image(__DRIdrawable
*dPriv
, int x
, int y
, int width
, int height
, void *data
)
93 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
94 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
96 loader
->getImage(dPriv
,
98 data
, dPriv
->loaderPrivate
);
102 get_image2(__DRIdrawable
*dPriv
, int x
, int y
, int width
, int height
, int stride
, void *data
)
104 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
105 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
107 /* getImage2 support is only in version 3 or newer */
108 if (loader
->base
.version
< 3)
111 loader
->getImage2(dPriv
,
112 x
, y
, width
, height
, stride
,
113 data
, dPriv
->loaderPrivate
);
117 drisw_update_drawable_info(struct dri_drawable
*drawable
)
119 __DRIdrawable
*dPriv
= drawable
->dPriv
;
122 get_drawable_info(dPriv
, &x
, &y
, &dPriv
->w
, &dPriv
->h
);
126 drisw_get_image(struct dri_drawable
*drawable
,
127 int x
, int y
, unsigned width
, unsigned height
, unsigned stride
,
130 __DRIdrawable
*dPriv
= drawable
->dPriv
;
131 int draw_x
, draw_y
, draw_w
, draw_h
;
133 get_drawable_info(dPriv
, &draw_x
, &draw_y
, &draw_w
, &draw_h
);
134 get_image2(dPriv
, x
, y
, draw_w
, draw_h
, stride
, data
);
138 drisw_put_image(struct dri_drawable
*drawable
,
139 void *data
, unsigned width
, unsigned height
)
141 __DRIdrawable
*dPriv
= drawable
->dPriv
;
143 put_image(dPriv
, data
, width
, height
);
147 drisw_put_image2(struct dri_drawable
*drawable
,
148 void *data
, int x
, int y
, unsigned width
, unsigned height
,
151 __DRIdrawable
*dPriv
= drawable
->dPriv
;
153 put_image2(dPriv
, data
, x
, y
, width
, height
, stride
);
157 drisw_present_texture(__DRIdrawable
*dPriv
,
158 struct pipe_resource
*ptex
, struct pipe_box
*sub_box
)
160 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
161 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
163 if (swrast_no_present
)
166 screen
->base
.screen
->flush_frontbuffer(screen
->base
.screen
, ptex
, 0, 0, drawable
, sub_box
);
170 drisw_invalidate_drawable(__DRIdrawable
*dPriv
)
172 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
174 drawable
->texture_stamp
= dPriv
->lastStamp
- 1;
176 p_atomic_inc(&drawable
->base
.stamp
);
180 drisw_copy_to_front(__DRIdrawable
* dPriv
,
181 struct pipe_resource
*ptex
)
183 drisw_present_texture(dPriv
, ptex
, NULL
);
185 drisw_invalidate_drawable(dPriv
);
189 * Backend functions for st_framebuffer interface and swap_buffers.
193 drisw_swap_buffers(__DRIdrawable
*dPriv
)
195 struct dri_context
*ctx
= dri_get_current(dPriv
->driScreenPriv
);
196 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
197 struct pipe_resource
*ptex
;
202 ptex
= drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
206 pp_run(ctx
->pp
, ptex
, ptex
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
208 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
210 drisw_copy_to_front(dPriv
, ptex
);
215 drisw_copy_sub_buffer(__DRIdrawable
*dPriv
, int x
, int y
,
218 struct dri_context
*ctx
= dri_get_current(dPriv
->driScreenPriv
);
219 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
220 struct pipe_resource
*ptex
;
225 ptex
= drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
228 if (ctx
->pp
&& drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
229 pp_run(ctx
->pp
, ptex
, ptex
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
231 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
233 u_box_2d(x
, dPriv
->h
- y
- h
, w
, h
, &box
);
234 drisw_present_texture(dPriv
, ptex
, &box
);
239 drisw_flush_frontbuffer(struct dri_context
*ctx
,
240 struct dri_drawable
*drawable
,
241 enum st_attachment_type statt
)
243 struct pipe_resource
*ptex
;
248 ptex
= drawable
->textures
[statt
];
251 drisw_copy_to_front(ctx
->dPriv
, ptex
);
256 * Allocate framebuffer attachments.
258 * During fixed-size operation, the function keeps allocating new attachments
259 * as they are requested. Unused attachments are not removed, not until the
260 * framebuffer is resized or destroyed.
263 drisw_allocate_textures(struct dri_context
*stctx
,
264 struct dri_drawable
*drawable
,
265 const enum st_attachment_type
*statts
,
268 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
269 const __DRIswrastLoaderExtension
*loader
= drawable
->dPriv
->driScreenPriv
->swrast_loader
;
270 struct pipe_resource templ
;
271 unsigned width
, height
;
275 width
= drawable
->dPriv
->w
;
276 height
= drawable
->dPriv
->h
;
278 resized
= (drawable
->old_w
!= width
||
279 drawable
->old_h
!= height
);
281 /* remove outdated textures */
283 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
284 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
287 memset(&templ
, 0, sizeof(templ
));
288 templ
.target
= screen
->target
;
289 templ
.width0
= width
;
290 templ
.height0
= height
;
292 templ
.array_size
= 1;
293 templ
.last_level
= 0;
295 for (i
= 0; i
< count
; i
++) {
296 enum pipe_format format
;
299 /* the texture already exists or not requested */
300 if (drawable
->textures
[statts
[i
]])
303 dri_drawable_get_format(drawable
, statts
[i
], &format
, &bind
);
305 /* if we don't do any present, no need for display targets */
306 if (statts
[i
] != ST_ATTACHMENT_DEPTH_STENCIL
&& !swrast_no_present
)
307 bind
|= PIPE_BIND_DISPLAY_TARGET
;
309 if (format
== PIPE_FORMAT_NONE
)
312 templ
.format
= format
;
315 if (statts
[i
] == ST_ATTACHMENT_FRONT_LEFT
&&
316 screen
->base
.screen
->resource_create_front
&&
317 loader
->base
.version
>= 3) {
318 drawable
->textures
[statts
[i
]] =
319 screen
->base
.screen
->resource_create_front(screen
->base
.screen
, &templ
, (const void *)drawable
);
321 drawable
->textures
[statts
[i
]] =
322 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
325 drawable
->old_w
= width
;
326 drawable
->old_h
= height
;
330 drisw_update_tex_buffer(struct dri_drawable
*drawable
,
331 struct dri_context
*ctx
,
332 struct pipe_resource
*res
)
334 __DRIdrawable
*dPriv
= drawable
->dPriv
;
336 struct st_context
*st_ctx
= (struct st_context
*)ctx
->st
;
337 struct pipe_context
*pipe
= st_ctx
->pipe
;
338 struct pipe_transfer
*transfer
;
341 int ximage_stride
, line
;
342 int cpp
= util_format_get_blocksize(res
->format
);
344 get_drawable_info(dPriv
, &x
, &y
, &w
, &h
);
346 map
= pipe_transfer_map(pipe
, res
,
347 0, 0, // level, layer,
349 x
, y
, w
, h
, &transfer
);
351 /* Copy the Drawable content to the mapped texture buffer */
352 get_image(dPriv
, x
, y
, w
, h
, map
);
354 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
355 get_image() has a pitch rounded up to 4 bytes. */
356 ximage_stride
= ((w
* cpp
) + 3) & -4;
357 for (line
= h
-1; line
; --line
) {
358 memmove(&map
[line
* transfer
->stride
],
359 &map
[line
* ximage_stride
],
363 pipe_transfer_unmap(pipe
, transfer
);
367 * Backend function for init_screen.
370 static const __DRIextension
*drisw_screen_extensions
[] = {
371 &driTexBufferExtension
.base
,
372 &dri2RendererQueryExtension
.base
,
373 &dri2ConfigQueryExtension
.base
,
377 static struct drisw_loader_funcs drisw_lf
= {
378 .get_image
= drisw_get_image
,
379 .put_image
= drisw_put_image
,
380 .put_image2
= drisw_put_image2
383 static const __DRIconfig
**
384 drisw_init_screen(__DRIscreen
* sPriv
)
386 const __DRIconfig
**configs
;
387 struct dri_screen
*screen
;
388 struct pipe_screen
*pscreen
;
390 screen
= CALLOC_STRUCT(dri_screen
);
394 screen
->sPriv
= sPriv
;
397 swrast_no_present
= debug_get_option_swrast_no_present();
399 sPriv
->driverPrivate
= (void *)screen
;
400 sPriv
->extensions
= drisw_screen_extensions
;
402 pscreen
= drisw_create_screen(&drisw_lf
);
403 /* dri_init_screen_helper checks pscreen for us */
405 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
411 dri_destroy_screen_helper(screen
);
417 drisw_create_buffer(__DRIscreen
* sPriv
,
418 __DRIdrawable
* dPriv
,
419 const struct gl_config
* visual
, boolean isPixmap
)
421 struct dri_drawable
*drawable
= NULL
;
423 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
426 drawable
= dPriv
->driverPrivate
;
428 drawable
->allocate_textures
= drisw_allocate_textures
;
429 drawable
->update_drawable_info
= drisw_update_drawable_info
;
430 drawable
->flush_frontbuffer
= drisw_flush_frontbuffer
;
431 drawable
->update_tex_buffer
= drisw_update_tex_buffer
;
437 * DRI driver virtual function table.
439 * DRI versions differ in their implementation of init_screen and swap_buffers.
441 const struct __DriverAPIRec galliumsw_driver_api
= {
442 .InitScreen
= drisw_init_screen
,
443 .DestroyScreen
= dri_destroy_screen
,
444 .CreateContext
= dri_create_context
,
445 .DestroyContext
= dri_destroy_context
,
446 .CreateBuffer
= drisw_create_buffer
,
447 .DestroyBuffer
= dri_destroy_buffer
,
448 .SwapBuffers
= drisw_swap_buffers
,
449 .MakeCurrent
= dri_make_current
,
450 .UnbindContext
= dri_unbind_context
,
451 .CopySubBuffer
= drisw_copy_sub_buffer
,
454 /* This is the table of extensions that the loader will dlsym() for. */
455 const __DRIextension
*galliumsw_driver_extensions
[] = {
456 &driCoreExtension
.base
,
457 &driSWRastExtension
.base
,
458 &driCopySubBufferExtension
.base
,
459 &gallium_config_options
.base
,
463 /* vim: set sw=3 ts=8 sts=3 expandtab: */