Merge branch 'gallium-polygon-stipple'
[mesa.git] / src / gallium / state_trackers / xa / xa_context.c
1 /**********************************************************
2 * Copyright 2009-2011 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 *********************************************************
25 * Authors:
26 * Zack Rusin <zackr-at-vmware-dot-com>
27 * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
28 */
29 #include "xa_context.h"
30 #include "xa_priv.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"
36
37
38 struct xa_context *
39 xa_context_default(struct xa_tracker *xa)
40 {
41 return xa->default_ctx;
42 }
43
44 struct xa_context *
45 xa_context_create(struct xa_tracker *xa)
46 {
47 struct xa_context *ctx = calloc(1, sizeof(*ctx));
48
49 ctx->xa = xa;
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);
54
55 return ctx;
56 }
57
58 void
59 xa_context_destroy(struct xa_context *r)
60 {
61 struct pipe_resource **vsbuf = &r->vs_const_buffer;
62 struct pipe_resource **fsbuf = &r->fs_const_buffer;
63
64 if (*vsbuf)
65 pipe_resource_reference(vsbuf, NULL);
66
67 if (*fsbuf)
68 pipe_resource_reference(fsbuf, NULL);
69
70 if (r->shaders) {
71 xa_shaders_destroy(r->shaders);
72 r->shaders = NULL;
73 }
74
75 if (r->cso) {
76 cso_release_all(r->cso);
77 cso_destroy_context(r->cso);
78 r->cso = NULL;
79 }
80 }
81
82 int
83 xa_surface_dma(struct xa_context *ctx,
84 struct xa_surface *srf,
85 void *data,
86 unsigned int pitch,
87 int to_surface, struct xa_box *boxes, unsigned int num_boxes)
88 {
89 struct pipe_transfer *transfer;
90 void *map;
91 int w, h, i;
92 enum pipe_transfer_usage transfer_direction;
93 struct pipe_context *pipe = ctx->pipe;
94
95 transfer_direction = (to_surface ? PIPE_TRANSFER_WRITE :
96 PIPE_TRANSFER_READ);
97
98 for (i = 0; i < num_boxes; ++i, ++boxes) {
99 w = boxes->x2 - boxes->x1;
100 h = boxes->y2 - boxes->y1;
101
102 transfer = pipe_get_transfer(pipe, srf->tex, 0, 0,
103 transfer_direction, boxes->x1, boxes->y1,
104 w, h);
105 if (!transfer)
106 return -XA_ERR_NORES;
107
108 map = pipe_transfer_map(ctx->pipe, transfer);
109 if (!map)
110 goto out_no_map;
111
112 if (to_surface) {
113 util_copy_rect(map, srf->tex->format, transfer->stride,
114 0, 0, w, h, data, pitch, boxes->x1, boxes->y1);
115 } else {
116 util_copy_rect(data, srf->tex->format, pitch,
117 boxes->x1, boxes->y1, w, h, map, transfer->stride, 0,
118 0);
119 }
120 pipe->transfer_unmap(pipe, transfer);
121 pipe->transfer_destroy(pipe, transfer);
122 if (to_surface)
123 pipe->flush(pipe, &ctx->last_fence);
124 }
125 return XA_ERR_NONE;
126 out_no_map:
127 pipe->transfer_destroy(pipe, transfer);
128 return -XA_ERR_NORES;
129 }
130
131 void *
132 xa_surface_map(struct xa_context *ctx,
133 struct xa_surface *srf, unsigned int usage)
134 {
135 void *map;
136 unsigned int transfer_direction = 0;
137 struct pipe_context *pipe = ctx->pipe;
138
139 if (srf->transfer)
140 return NULL;
141
142 if (usage & XA_MAP_READ)
143 transfer_direction = PIPE_TRANSFER_READ;
144 if (usage & XA_MAP_WRITE)
145 transfer_direction = PIPE_TRANSFER_WRITE;
146
147 if (!transfer_direction)
148 return NULL;
149
150 srf->transfer = pipe_get_transfer(pipe, srf->tex, 0, 0,
151 transfer_direction, 0, 0,
152 srf->tex->width0, srf->tex->height0);
153 if (!srf->transfer)
154 return NULL;
155
156 map = pipe_transfer_map(pipe, srf->transfer);
157 if (!map)
158 pipe->transfer_destroy(pipe, srf->transfer);
159
160 srf->mapping_pipe = pipe;
161 return map;
162 }
163
164 void
165 xa_surface_unmap(struct xa_surface *srf)
166 {
167 if (srf->transfer) {
168 struct pipe_context *pipe = srf->mapping_pipe;
169
170 pipe->transfer_unmap(pipe, srf->transfer);
171 pipe->transfer_destroy(pipe, srf->transfer);
172 srf->transfer = NULL;
173 }
174 }
175
176 int
177 xa_surface_psurf_create(struct xa_context *ctx, struct xa_surface *dst)
178 {
179 struct pipe_screen *screen = ctx->pipe->screen;
180 struct pipe_surface srf_templ;
181
182 if (dst->srf)
183 return -XA_ERR_INVAL;
184
185 if (!screen->is_format_supported(screen, dst->tex->format,
186 PIPE_TEXTURE_2D, 0,
187 PIPE_BIND_RENDER_TARGET))
188 return -XA_ERR_INVAL;
189
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);
193 if (!dst->srf)
194 return -XA_ERR_NORES;
195
196 return XA_ERR_NONE;
197 }
198
199 void
200 xa_surface_psurf_destroy(struct xa_surface *dst)
201 {
202 pipe_surface_reference(&dst->srf, NULL);
203 }
204
205 int
206 xa_copy_prepare(struct xa_context *ctx,
207 struct xa_surface *dst, struct xa_surface *src)
208 {
209 if (src == dst || dst->srf != NULL)
210 return -XA_ERR_INVAL;
211
212 if (src->tex->format != dst->tex->format) {
213 int ret = xa_surface_psurf_create(ctx, dst);
214 if (ret != XA_ERR_NONE)
215 return ret;
216 renderer_copy_prepare(ctx, dst->srf, src->tex);
217 ctx->simple_copy = 0;
218 } else
219 ctx->simple_copy = 1;
220
221 ctx->src = src;
222 ctx->dst = dst;
223
224 return 0;
225 }
226
227 void
228 xa_copy(struct xa_context *ctx,
229 int dx, int dy, int sx, int sy, int width, int height)
230 {
231 struct pipe_box src_box;
232
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,
237 ctx->src->tex,
238 0, &src_box);
239 } else
240 renderer_copy(ctx, dx, dy, sx, sy, width, height,
241 (float) width, (float) height);
242 }
243
244 void
245 xa_copy_done(struct xa_context *ctx)
246 {
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);
251 } else
252 ctx->pipe->flush(ctx->pipe, &ctx->last_fence);
253 }
254
255 static void
256 bind_solid_blend_state(struct xa_context *ctx)
257 {
258 struct pipe_blend_state blend;
259
260 memset(&blend, 0, sizeof(struct pipe_blend_state));
261 blend.rt[0].blend_enable = 0;
262 blend.rt[0].colormask = PIPE_MASK_RGBA;
263
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;
268
269 cso_set_blend(ctx->cso, &blend);
270 }
271
272 int
273 xa_solid_prepare(struct xa_context *ctx, struct xa_surface *dst,
274 uint32_t fg)
275 {
276 unsigned vs_traits, fs_traits;
277 struct xa_shader shader;
278 int width, height;
279 int ret;
280
281 ret = xa_surface_psurf_create(ctx, dst);
282 if (ret != XA_ERR_NONE)
283 return ret;
284
285 if (dst->srf->format == PIPE_FORMAT_L8_UNORM)
286 xa_pixel_to_float4_a8(fg, ctx->solid_color);
287 else
288 xa_pixel_to_float4(fg, ctx->solid_color);
289 ctx->has_solid_color = 1;
290
291 ctx->dst = dst;
292 width = dst->srf->width;
293 height = dst->srf->height;
294
295 #if 0
296 debug_printf("Color Pixel=(%d, %d, %d, %d), RGBA=(%f, %f, %f, %f)\n",
297 (fg >> 24) & 0xff, (fg >> 16) & 0xff,
298 (fg >> 8) & 0xff, (fg >> 0) & 0xff,
299 exa->solid_color[0], exa->solid_color[1],
300 exa->solid_color[2], exa->solid_color[3]);
301 #endif
302
303 vs_traits = VS_SOLID_FILL;
304 fs_traits = FS_SOLID_FILL;
305
306 renderer_bind_destination(ctx, dst->srf, width, height);
307 bind_solid_blend_state(ctx);
308 cso_set_samplers(ctx->cso, 0, NULL);
309 cso_set_fragment_sampler_views(ctx->cso, 0, NULL);
310
311 shader = xa_shaders_get(ctx->shaders, vs_traits, fs_traits);
312 cso_set_vertex_shader_handle(ctx->cso, shader.vs);
313 cso_set_fragment_shader_handle(ctx->cso, shader.fs);
314
315 renderer_begin_solid(ctx);
316
317 xa_surface_psurf_destroy(dst);
318 return XA_ERR_NONE;
319 }
320
321 void
322 xa_solid(struct xa_context *ctx, int x, int y, int width, int height)
323 {
324 renderer_solid(ctx, x, y, x + width, y + height, ctx->solid_color);
325 }
326
327 void
328 xa_solid_done(struct xa_context *ctx)
329 {
330 renderer_draw_flush(ctx);
331 ctx->pipe->flush(ctx->pipe, &ctx->last_fence);
332
333 ctx->comp = NULL;
334 ctx->has_solid_color = FALSE;
335 ctx->num_bound_samplers = 0;
336 }
337
338 struct xa_fence *
339 xa_fence_get(struct xa_context *ctx)
340 {
341 struct xa_fence *fence = malloc(sizeof(*fence));
342 struct pipe_screen *screen = ctx->xa->screen;
343
344 if (!fence)
345 return NULL;
346
347 fence->xa = ctx->xa;
348
349 if (ctx->last_fence == NULL)
350 fence->pipe_fence = NULL;
351 else
352 screen->fence_reference(screen, &fence->pipe_fence, ctx->last_fence);
353
354 return fence;
355 }
356
357 int
358 xa_fence_wait(struct xa_fence *fence, uint64_t timeout)
359 {
360 if (!fence)
361 return XA_ERR_NONE;
362
363 if (fence->pipe_fence) {
364 struct pipe_screen *screen = fence->xa->screen;
365 boolean timed_out;
366
367 timed_out = !screen->fence_finish(screen, fence->pipe_fence, timeout);
368 if (timed_out)
369 return -XA_ERR_BUSY;
370
371 screen->fence_reference(screen, &fence->pipe_fence, NULL);
372 }
373 return XA_ERR_NONE;
374 }
375
376 void
377 xa_fence_destroy(struct xa_fence *fence)
378 {
379 if (!fence)
380 return;
381
382 if (fence->pipe_fence) {
383 struct pipe_screen *screen = fence->xa->screen;
384
385 screen->fence_reference(screen, &fence->pipe_fence, NULL);
386 }
387
388 free(fence);
389 }