1 /**********************************************************
2 * Copyright 2009-2011 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 *********************************************************
26 * Zack Rusin <zackr-at-vmware-dot-com>
27 * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 #include "xa_context.h"
31 #include "cso_cache/cso_context.h"
32 #include "util/u_inlines.h"
33 #include "util/u_rect.h"
34 #include "util/u_surface.h"
35 #include "pipe/p_context.h"
39 xa_context_default(struct xa_tracker
*xa
)
41 return xa
->default_ctx
;
45 xa_context_create(struct xa_tracker
*xa
)
47 struct xa_context
*ctx
= calloc(1, sizeof(*ctx
));
50 ctx
->pipe
= xa
->screen
->context_create(xa
->screen
, NULL
);
51 ctx
->cso
= cso_create_context(ctx
->pipe
);
52 ctx
->shaders
= xa_shaders_create(ctx
);
53 renderer_init_state(ctx
);
59 xa_context_destroy(struct xa_context
*r
)
61 struct pipe_resource
**vsbuf
= &r
->vs_const_buffer
;
62 struct pipe_resource
**fsbuf
= &r
->fs_const_buffer
;
65 pipe_resource_reference(vsbuf
, NULL
);
68 pipe_resource_reference(fsbuf
, NULL
);
71 xa_shaders_destroy(r
->shaders
);
76 cso_release_all(r
->cso
);
77 cso_destroy_context(r
->cso
);
83 xa_surface_dma(struct xa_context
*ctx
,
84 struct xa_surface
*srf
,
87 int to_surface
, struct xa_box
*boxes
, unsigned int num_boxes
)
89 struct pipe_transfer
*transfer
;
92 enum pipe_transfer_usage transfer_direction
;
93 struct pipe_context
*pipe
= ctx
->pipe
;
95 transfer_direction
= (to_surface
? PIPE_TRANSFER_WRITE
:
98 for (i
= 0; i
< num_boxes
; ++i
, ++boxes
) {
99 w
= boxes
->x2
- boxes
->x1
;
100 h
= boxes
->y2
- boxes
->y1
;
102 transfer
= pipe_get_transfer(pipe
, srf
->tex
, 0, 0,
103 transfer_direction
, boxes
->x1
, boxes
->y1
,
106 return -XA_ERR_NORES
;
108 map
= pipe_transfer_map(ctx
->pipe
, transfer
);
113 util_copy_rect(map
, srf
->tex
->format
, transfer
->stride
,
114 0, 0, w
, h
, data
, pitch
, boxes
->x1
, boxes
->y1
);
116 util_copy_rect(data
, srf
->tex
->format
, pitch
,
117 boxes
->x1
, boxes
->y1
, w
, h
, map
, transfer
->stride
, 0,
120 pipe
->transfer_unmap(pipe
, transfer
);
121 pipe
->transfer_destroy(pipe
, transfer
);
123 pipe
->flush(pipe
, &ctx
->last_fence
);
127 pipe
->transfer_destroy(pipe
, transfer
);
128 return -XA_ERR_NORES
;
132 xa_surface_map(struct xa_context
*ctx
,
133 struct xa_surface
*srf
, unsigned int usage
)
136 unsigned int transfer_direction
= 0;
137 struct pipe_context
*pipe
= ctx
->pipe
;
142 if (usage
& XA_MAP_READ
)
143 transfer_direction
= PIPE_TRANSFER_READ
;
144 if (usage
& XA_MAP_WRITE
)
145 transfer_direction
= PIPE_TRANSFER_WRITE
;
147 if (!transfer_direction
)
150 srf
->transfer
= pipe_get_transfer(pipe
, srf
->tex
, 0, 0,
151 transfer_direction
, 0, 0,
152 srf
->tex
->width0
, srf
->tex
->height0
);
156 map
= pipe_transfer_map(pipe
, srf
->transfer
);
158 pipe
->transfer_destroy(pipe
, srf
->transfer
);
160 srf
->mapping_pipe
= pipe
;
165 xa_surface_unmap(struct xa_surface
*srf
)
168 struct pipe_context
*pipe
= srf
->mapping_pipe
;
170 pipe
->transfer_unmap(pipe
, srf
->transfer
);
171 pipe
->transfer_destroy(pipe
, srf
->transfer
);
172 srf
->transfer
= NULL
;
177 xa_surface_psurf_create(struct xa_context
*ctx
, struct xa_surface
*dst
)
179 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
180 struct pipe_surface srf_templ
;
183 return -XA_ERR_INVAL
;
185 if (!screen
->is_format_supported(screen
, dst
->tex
->format
,
187 PIPE_BIND_RENDER_TARGET
))
188 return -XA_ERR_INVAL
;
190 u_surface_default_template(&srf_templ
, dst
->tex
,
191 PIPE_BIND_RENDER_TARGET
);
192 dst
->srf
= ctx
->pipe
->create_surface(ctx
->pipe
, dst
->tex
, &srf_templ
);
194 return -XA_ERR_NORES
;
200 xa_surface_psurf_destroy(struct xa_surface
*dst
)
202 pipe_surface_reference(&dst
->srf
, NULL
);
206 xa_copy_prepare(struct xa_context
*ctx
,
207 struct xa_surface
*dst
, struct xa_surface
*src
)
209 if (src
== dst
|| dst
->srf
!= NULL
)
210 return -XA_ERR_INVAL
;
212 if (src
->tex
->format
!= dst
->tex
->format
) {
213 int ret
= xa_surface_psurf_create(ctx
, dst
);
214 if (ret
!= XA_ERR_NONE
)
216 renderer_copy_prepare(ctx
, dst
->srf
, src
->tex
);
217 ctx
->simple_copy
= 0;
219 ctx
->simple_copy
= 1;
228 xa_copy(struct xa_context
*ctx
,
229 int dx
, int dy
, int sx
, int sy
, int width
, int height
)
231 struct pipe_box src_box
;
233 if (ctx
->simple_copy
) {
234 u_box_2d(sx
, sy
, width
, height
, &src_box
);
235 ctx
->pipe
->resource_copy_region(ctx
->pipe
,
236 ctx
->dst
->tex
, 0, dx
, dy
, 0,
240 renderer_copy(ctx
, dx
, dy
, sx
, sy
, width
, height
,
241 (float) width
, (float) height
);
245 xa_copy_done(struct xa_context
*ctx
)
247 if (!ctx
->simple_copy
) {
248 renderer_draw_flush(ctx
);
249 ctx
->pipe
->flush(ctx
->pipe
, &ctx
->last_fence
);
250 xa_surface_psurf_destroy(ctx
->dst
);
252 ctx
->pipe
->flush(ctx
->pipe
, &ctx
->last_fence
);
256 bind_solid_blend_state(struct xa_context
*ctx
)
258 struct pipe_blend_state blend
;
260 memset(&blend
, 0, sizeof(struct pipe_blend_state
));
261 blend
.rt
[0].blend_enable
= 0;
262 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
264 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
265 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
266 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
267 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
269 cso_set_blend(ctx
->cso
, &blend
);
273 xa_solid_prepare(struct xa_context
*ctx
, struct xa_surface
*dst
,
276 unsigned vs_traits
, fs_traits
;
277 struct xa_shader shader
;
281 xa_pixel_to_float4(fg
, ctx
->solid_color
);
282 ctx
->has_solid_color
= 1;
284 ret
= xa_surface_psurf_create(ctx
, dst
);
285 if (ret
!= XA_ERR_NONE
)
289 width
= dst
->srf
->width
;
290 height
= dst
->srf
->height
;
293 debug_printf("Color Pixel=(%d, %d, %d, %d), RGBA=(%f, %f, %f, %f)\n",
294 (fg
>> 24) & 0xff, (fg
>> 16) & 0xff,
295 (fg
>> 8) & 0xff, (fg
>> 0) & 0xff,
296 exa
->solid_color
[0], exa
->solid_color
[1],
297 exa
->solid_color
[2], exa
->solid_color
[3]);
300 vs_traits
= VS_SOLID_FILL
;
301 fs_traits
= FS_SOLID_FILL
;
303 renderer_bind_destination(ctx
, dst
->srf
, width
, height
);
304 bind_solid_blend_state(ctx
);
305 cso_set_samplers(ctx
->cso
, 0, NULL
);
306 cso_set_fragment_sampler_views(ctx
->cso
, 0, NULL
);
308 shader
= xa_shaders_get(ctx
->shaders
, vs_traits
, fs_traits
);
309 cso_set_vertex_shader_handle(ctx
->cso
, shader
.vs
);
310 cso_set_fragment_shader_handle(ctx
->cso
, shader
.fs
);
312 renderer_begin_solid(ctx
);
314 xa_surface_psurf_destroy(dst
);
319 xa_solid(struct xa_context
*ctx
, int x
, int y
, int width
, int height
)
321 renderer_solid(ctx
, x
, y
, x
+ width
, y
+ height
, ctx
->solid_color
);
325 xa_solid_done(struct xa_context
*ctx
)
327 renderer_draw_flush(ctx
);
328 ctx
->pipe
->flush(ctx
->pipe
, &ctx
->last_fence
);
331 ctx
->has_solid_color
= FALSE
;
332 ctx
->num_bound_samplers
= 0;
336 xa_fence_get(struct xa_context
*ctx
)
338 struct xa_fence
*fence
= malloc(sizeof(*fence
));
339 struct pipe_screen
*screen
= ctx
->xa
->screen
;
346 if (ctx
->last_fence
== NULL
)
347 fence
->pipe_fence
= NULL
;
349 screen
->fence_reference(screen
, &fence
->pipe_fence
, ctx
->last_fence
);
355 xa_fence_wait(struct xa_fence
*fence
, uint64_t timeout
)
360 if (fence
->pipe_fence
) {
361 struct pipe_screen
*screen
= fence
->xa
->screen
;
364 timed_out
= !screen
->fence_finish(screen
, fence
->pipe_fence
, timeout
);
368 screen
->fence_reference(screen
, &fence
->pipe_fence
, NULL
);
374 xa_fence_destroy(struct xa_fence
*fence
)
379 if (fence
->pipe_fence
) {
380 struct pipe_screen
*screen
= fence
->xa
->screen
;
382 screen
->fence_reference(screen
, &fence
->pipe_fence
, NULL
);