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"
38 XA_EXPORT
struct xa_context
*
39 xa_context_default(struct xa_tracker
*xa
)
41 return xa
->default_ctx
;
44 XA_EXPORT
struct xa_context
*
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
);
75 xa_ctx_sampler_views_destroy(r
);
78 cso_release_all(r
->cso
);
79 cso_destroy_context(r
->cso
);
83 r
->pipe
->destroy(r
->pipe
);
87 xa_surface_dma(struct xa_context
*ctx
,
88 struct xa_surface
*srf
,
91 int to_surface
, struct xa_box
*boxes
, unsigned int num_boxes
)
93 struct pipe_transfer
*transfer
;
96 enum pipe_transfer_usage transfer_direction
;
97 struct pipe_context
*pipe
= ctx
->pipe
;
99 transfer_direction
= (to_surface
? PIPE_TRANSFER_WRITE
:
102 for (i
= 0; i
< num_boxes
; ++i
, ++boxes
) {
103 w
= boxes
->x2
- boxes
->x1
;
104 h
= boxes
->y2
- boxes
->y1
;
106 map
= pipe_transfer_map(pipe
, srf
->tex
, 0, 0,
107 transfer_direction
, boxes
->x1
, boxes
->y1
,
110 return -XA_ERR_NORES
;
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
);
122 pipe
->flush(pipe
, &ctx
->last_fence
, 0);
128 xa_surface_map(struct xa_context
*ctx
,
129 struct xa_surface
*srf
, unsigned int usage
)
132 unsigned int transfer_direction
= 0;
133 struct pipe_context
*pipe
= ctx
->pipe
;
136 * A surface may only have a single map.
141 if (usage
& XA_MAP_READ
)
142 transfer_direction
= PIPE_TRANSFER_READ
;
143 if (usage
& XA_MAP_WRITE
)
144 transfer_direction
= PIPE_TRANSFER_WRITE
;
146 if (!transfer_direction
)
149 map
= pipe_transfer_map(pipe
, srf
->tex
, 0, 0,
150 transfer_direction
, 0, 0,
151 srf
->tex
->width0
, srf
->tex
->height0
,
156 srf
->mapping_pipe
= pipe
;
161 xa_surface_unmap(struct xa_surface
*srf
)
164 struct pipe_context
*pipe
= srf
->mapping_pipe
;
166 pipe
->transfer_unmap(pipe
, srf
->transfer
);
167 srf
->transfer
= NULL
;
172 xa_ctx_srf_create(struct xa_context
*ctx
, struct xa_surface
*dst
)
174 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
175 struct pipe_surface srf_templ
;
178 return -XA_ERR_INVAL
;
180 if (!screen
->is_format_supported(screen
, dst
->tex
->format
,
182 PIPE_BIND_RENDER_TARGET
))
183 return -XA_ERR_INVAL
;
185 u_surface_default_template(&srf_templ
, dst
->tex
);
186 ctx
->srf
= ctx
->pipe
->create_surface(ctx
->pipe
, dst
->tex
, &srf_templ
);
188 return -XA_ERR_NORES
;
194 xa_ctx_srf_destroy(struct xa_context
*ctx
)
196 pipe_surface_reference(&ctx
->srf
, NULL
);
200 xa_copy_prepare(struct xa_context
*ctx
,
201 struct xa_surface
*dst
, struct xa_surface
*src
)
203 if (src
== dst
|| ctx
->srf
!= NULL
)
204 return -XA_ERR_INVAL
;
206 if (src
->tex
->format
!= dst
->tex
->format
) {
207 int ret
= xa_ctx_srf_create(ctx
, dst
);
208 if (ret
!= XA_ERR_NONE
)
210 renderer_copy_prepare(ctx
, ctx
->srf
, src
->tex
,
211 src
->fdesc
.xa_format
,
212 dst
->fdesc
.xa_format
);
213 ctx
->simple_copy
= 0;
215 ctx
->simple_copy
= 1;
219 xa_ctx_srf_destroy(ctx
);
225 xa_copy(struct xa_context
*ctx
,
226 int dx
, int dy
, int sx
, int sy
, int width
, int height
)
228 struct pipe_box src_box
;
230 if (ctx
->simple_copy
) {
231 u_box_2d(sx
, sy
, width
, height
, &src_box
);
232 ctx
->pipe
->resource_copy_region(ctx
->pipe
,
233 ctx
->dst
->tex
, 0, dx
, dy
, 0,
237 renderer_copy(ctx
, dx
, dy
, sx
, sy
, width
, height
,
238 (float) ctx
->src
->tex
->width0
,
239 (float) ctx
->src
->tex
->height0
);
243 xa_copy_done(struct xa_context
*ctx
)
245 if (!ctx
->simple_copy
) {
246 renderer_draw_flush(ctx
);
247 ctx
->pipe
->flush(ctx
->pipe
, &ctx
->last_fence
, 0);
249 ctx
->pipe
->flush(ctx
->pipe
, &ctx
->last_fence
, 0);
253 bind_solid_blend_state(struct xa_context
*ctx
)
255 struct pipe_blend_state blend
;
257 memset(&blend
, 0, sizeof(struct pipe_blend_state
));
258 blend
.rt
[0].blend_enable
= 0;
259 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
261 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
262 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
263 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
264 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
266 cso_set_blend(ctx
->cso
, &blend
);
270 xa_solid_prepare(struct xa_context
*ctx
, struct xa_surface
*dst
,
273 unsigned vs_traits
, fs_traits
;
274 struct xa_shader shader
;
278 ret
= xa_ctx_srf_create(ctx
, dst
);
279 if (ret
!= XA_ERR_NONE
)
282 if (ctx
->srf
->format
== PIPE_FORMAT_L8_UNORM
)
283 xa_pixel_to_float4_a8(fg
, ctx
->solid_color
);
285 xa_pixel_to_float4(fg
, ctx
->solid_color
);
286 ctx
->has_solid_color
= 1;
289 width
= ctx
->srf
->width
;
290 height
= ctx
->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
, ctx
->srf
, width
, height
);
304 bind_solid_blend_state(ctx
);
305 cso_set_samplers(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 0, NULL
);
306 cso_set_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 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_ctx_srf_destroy(ctx
);
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
, 0);
331 ctx
->has_solid_color
= FALSE
;
332 ctx
->num_bound_samplers
= 0;
335 XA_EXPORT
struct xa_fence
*
336 xa_fence_get(struct xa_context
*ctx
)
338 struct xa_fence
*fence
= calloc(1, 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
);
389 xa_ctx_sampler_views_destroy(struct xa_context
*ctx
)
393 for (i
= 0; i
< ctx
->num_bound_samplers
; ++i
)
394 pipe_sampler_view_reference(&ctx
->bound_sampler_views
[i
], NULL
);
395 ctx
->num_bound_samplers
= 0;