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 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
40 #include "util/u_box.h"
41 #include "pipe/p_context.h"
42 #include "pipe-loader/pipe_loader.h"
43 #include "state_tracker/drisw_api.h"
44 #include "state_tracker/st_context.h"
46 #include "dri_screen.h"
47 #include "dri_context.h"
48 #include "dri_drawable.h"
49 #include "dri_query_renderer.h"
51 DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present
, "SWRAST_NO_PRESENT", FALSE
);
52 static boolean swrast_no_present
= FALSE
;
55 get_drawable_info(__DRIdrawable
*dPriv
, int *x
, int *y
, int *w
, int *h
)
57 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
58 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
60 loader
->getDrawableInfo(dPriv
,
62 dPriv
->loaderPrivate
);
66 put_image(__DRIdrawable
*dPriv
, void *data
, unsigned width
, unsigned height
)
68 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
69 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
71 loader
->putImage(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
73 data
, dPriv
->loaderPrivate
);
77 put_image2(__DRIdrawable
*dPriv
, void *data
, int x
, int y
,
78 unsigned width
, unsigned height
, unsigned stride
)
80 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
81 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
83 loader
->putImage2(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
84 x
, y
, width
, height
, stride
,
85 data
, dPriv
->loaderPrivate
);
89 get_image(__DRIdrawable
*dPriv
, int x
, int y
, int width
, int height
, void *data
)
91 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
92 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
94 loader
->getImage(dPriv
,
96 data
, dPriv
->loaderPrivate
);
100 get_image2(__DRIdrawable
*dPriv
, int x
, int y
, int width
, int height
, int stride
, void *data
)
102 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
103 const __DRIswrastLoaderExtension
*loader
= sPriv
->swrast_loader
;
105 /* getImage2 support is only in version 3 or newer */
106 if (loader
->base
.version
< 3)
109 loader
->getImage2(dPriv
,
110 x
, y
, width
, height
, stride
,
111 data
, dPriv
->loaderPrivate
);
115 drisw_update_drawable_info(struct dri_drawable
*drawable
)
117 __DRIdrawable
*dPriv
= drawable
->dPriv
;
120 get_drawable_info(dPriv
, &x
, &y
, &dPriv
->w
, &dPriv
->h
);
124 drisw_get_image(struct dri_drawable
*drawable
,
125 int x
, int y
, unsigned width
, unsigned height
, unsigned stride
,
128 __DRIdrawable
*dPriv
= drawable
->dPriv
;
129 int draw_x
, draw_y
, draw_w
, draw_h
;
131 get_drawable_info(dPriv
, &draw_x
, &draw_y
, &draw_w
, &draw_h
);
132 get_image2(dPriv
, x
, y
, draw_w
, draw_h
, stride
, data
);
136 drisw_put_image(struct dri_drawable
*drawable
,
137 void *data
, unsigned width
, unsigned height
)
139 __DRIdrawable
*dPriv
= drawable
->dPriv
;
141 put_image(dPriv
, data
, width
, height
);
145 drisw_put_image2(struct dri_drawable
*drawable
,
146 void *data
, int x
, int y
, unsigned width
, unsigned height
,
149 __DRIdrawable
*dPriv
= drawable
->dPriv
;
151 put_image2(dPriv
, data
, x
, y
, width
, height
, stride
);
155 drisw_present_texture(__DRIdrawable
*dPriv
,
156 struct pipe_resource
*ptex
, struct pipe_box
*sub_box
)
158 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
159 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
161 if (swrast_no_present
)
164 screen
->base
.screen
->flush_frontbuffer(screen
->base
.screen
, ptex
, 0, 0, drawable
, sub_box
);
168 drisw_invalidate_drawable(__DRIdrawable
*dPriv
)
170 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
172 drawable
->texture_stamp
= dPriv
->lastStamp
- 1;
174 p_atomic_inc(&drawable
->base
.stamp
);
178 drisw_copy_to_front(__DRIdrawable
* dPriv
,
179 struct pipe_resource
*ptex
)
181 drisw_present_texture(dPriv
, ptex
, NULL
);
183 drisw_invalidate_drawable(dPriv
);
187 * Backend functions for st_framebuffer interface and swap_buffers.
191 drisw_swap_buffers(__DRIdrawable
*dPriv
)
193 struct dri_context
*ctx
= dri_get_current(dPriv
->driScreenPriv
);
194 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
195 struct pipe_resource
*ptex
;
200 ptex
= drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
204 pp_run(ctx
->pp
, ptex
, ptex
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
206 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
208 drisw_copy_to_front(dPriv
, ptex
);
213 drisw_copy_sub_buffer(__DRIdrawable
*dPriv
, int x
, int y
,
216 struct dri_context
*ctx
= dri_get_current(dPriv
->driScreenPriv
);
217 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
218 struct pipe_resource
*ptex
;
223 ptex
= drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
226 if (ctx
->pp
&& drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
227 pp_run(ctx
->pp
, ptex
, ptex
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
229 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
231 u_box_2d(x
, dPriv
->h
- y
- h
, w
, h
, &box
);
232 drisw_present_texture(dPriv
, ptex
, &box
);
237 drisw_flush_frontbuffer(struct dri_context
*ctx
,
238 struct dri_drawable
*drawable
,
239 enum st_attachment_type statt
)
241 struct pipe_resource
*ptex
;
246 ptex
= drawable
->textures
[statt
];
249 drisw_copy_to_front(ctx
->dPriv
, ptex
);
254 * Allocate framebuffer attachments.
256 * During fixed-size operation, the function keeps allocating new attachments
257 * as they are requested. Unused attachments are not removed, not until the
258 * framebuffer is resized or destroyed.
261 drisw_allocate_textures(struct dri_context
*stctx
,
262 struct dri_drawable
*drawable
,
263 const enum st_attachment_type
*statts
,
266 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
267 const __DRIswrastLoaderExtension
*loader
= drawable
->dPriv
->driScreenPriv
->swrast_loader
;
268 struct pipe_resource templ
;
269 unsigned width
, height
;
273 width
= drawable
->dPriv
->w
;
274 height
= drawable
->dPriv
->h
;
276 resized
= (drawable
->old_w
!= width
||
277 drawable
->old_h
!= height
);
279 /* remove outdated textures */
281 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
282 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
285 memset(&templ
, 0, sizeof(templ
));
286 templ
.target
= screen
->target
;
287 templ
.width0
= width
;
288 templ
.height0
= height
;
290 templ
.array_size
= 1;
291 templ
.last_level
= 0;
293 for (i
= 0; i
< count
; i
++) {
294 enum pipe_format format
;
297 /* the texture already exists or not requested */
298 if (drawable
->textures
[statts
[i
]])
301 dri_drawable_get_format(drawable
, statts
[i
], &format
, &bind
);
303 /* if we don't do any present, no need for display targets */
304 if (statts
[i
] != ST_ATTACHMENT_DEPTH_STENCIL
&& !swrast_no_present
)
305 bind
|= PIPE_BIND_DISPLAY_TARGET
;
307 if (format
== PIPE_FORMAT_NONE
)
310 templ
.format
= format
;
313 if (statts
[i
] == ST_ATTACHMENT_FRONT_LEFT
&&
314 screen
->base
.screen
->resource_create_front
&&
315 loader
->base
.version
>= 3) {
316 drawable
->textures
[statts
[i
]] =
317 screen
->base
.screen
->resource_create_front(screen
->base
.screen
, &templ
, (const void *)drawable
);
319 drawable
->textures
[statts
[i
]] =
320 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
323 drawable
->old_w
= width
;
324 drawable
->old_h
= height
;
328 drisw_update_tex_buffer(struct dri_drawable
*drawable
,
329 struct dri_context
*ctx
,
330 struct pipe_resource
*res
)
332 __DRIdrawable
*dPriv
= drawable
->dPriv
;
334 struct st_context
*st_ctx
= (struct st_context
*)ctx
->st
;
335 struct pipe_context
*pipe
= st_ctx
->pipe
;
336 struct pipe_transfer
*transfer
;
339 int ximage_stride
, line
;
340 int cpp
= util_format_get_blocksize(res
->format
);
342 get_drawable_info(dPriv
, &x
, &y
, &w
, &h
);
344 map
= pipe_transfer_map(pipe
, res
,
345 0, 0, // level, layer,
347 x
, y
, w
, h
, &transfer
);
349 /* Copy the Drawable content to the mapped texture buffer */
350 get_image(dPriv
, x
, y
, w
, h
, map
);
352 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
353 get_image() has a pitch rounded up to 4 bytes. */
354 ximage_stride
= ((w
* cpp
) + 3) & -4;
355 for (line
= h
-1; line
; --line
) {
356 memmove(&map
[line
* transfer
->stride
],
357 &map
[line
* ximage_stride
],
361 pipe_transfer_unmap(pipe
, transfer
);
365 * Backend function for init_screen.
368 static const __DRIextension
*drisw_screen_extensions
[] = {
369 &driTexBufferExtension
.base
,
370 &dri2RendererQueryExtension
.base
,
371 &dri2ConfigQueryExtension
.base
,
375 static struct drisw_loader_funcs drisw_lf
= {
376 .get_image
= drisw_get_image
,
377 .put_image
= drisw_put_image
,
378 .put_image2
= drisw_put_image2
381 static const __DRIconfig
**
382 drisw_init_screen(__DRIscreen
* sPriv
)
384 const __DRIconfig
**configs
;
385 struct dri_screen
*screen
;
386 struct pipe_screen
*pscreen
= NULL
;
388 screen
= CALLOC_STRUCT(dri_screen
);
392 screen
->sPriv
= sPriv
;
395 swrast_no_present
= debug_get_option_swrast_no_present();
397 sPriv
->driverPrivate
= (void *)screen
;
398 sPriv
->extensions
= drisw_screen_extensions
;
400 if (pipe_loader_sw_probe_dri(&screen
->dev
, &drisw_lf
))
401 pscreen
= pipe_loader_create_screen(screen
->dev
);
406 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
412 dri_destroy_screen_helper(screen
);
414 pipe_loader_release(&screen
->dev
, 1);
420 drisw_create_buffer(__DRIscreen
* sPriv
,
421 __DRIdrawable
* dPriv
,
422 const struct gl_config
* visual
, boolean isPixmap
)
424 struct dri_drawable
*drawable
= NULL
;
426 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
429 drawable
= dPriv
->driverPrivate
;
431 drawable
->allocate_textures
= drisw_allocate_textures
;
432 drawable
->update_drawable_info
= drisw_update_drawable_info
;
433 drawable
->flush_frontbuffer
= drisw_flush_frontbuffer
;
434 drawable
->update_tex_buffer
= drisw_update_tex_buffer
;
440 * DRI driver virtual function table.
442 * DRI versions differ in their implementation of init_screen and swap_buffers.
444 const struct __DriverAPIRec galliumsw_driver_api
= {
445 .InitScreen
= drisw_init_screen
,
446 .DestroyScreen
= dri_destroy_screen
,
447 .CreateContext
= dri_create_context
,
448 .DestroyContext
= dri_destroy_context
,
449 .CreateBuffer
= drisw_create_buffer
,
450 .DestroyBuffer
= dri_destroy_buffer
,
451 .SwapBuffers
= drisw_swap_buffers
,
452 .MakeCurrent
= dri_make_current
,
453 .UnbindContext
= dri_unbind_context
,
454 .CopySubBuffer
= drisw_copy_sub_buffer
,
457 /* This is the table of extensions that the loader will dlsym() for. */
458 const __DRIextension
*galliumsw_driver_extensions
[] = {
459 &driCoreExtension
.base
,
460 &driSWRastExtension
.base
,
461 &driCopySubBufferExtension
.base
,
462 &gallium_config_options
.base
,
466 /* vim: set sw=3 ts=8 sts=3 expandtab: */