xa_context.c revision c04b9d1d561cc3a1300e65bd410f33dfff6fe1e0
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
38struct xa_context *
39xa_context_default(struct xa_tracker *xa)
40{
41    return xa->default_ctx;
42}
43
44struct xa_context *
45xa_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
58void
59xa_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    xa_ctx_sampler_views_destroy(r);
76
77    if (r->cso) {
78	cso_release_all(r->cso);
79	cso_destroy_context(r->cso);
80	r->cso = NULL;
81    }
82
83    r->pipe->destroy(r->pipe);
84}
85
86int
87xa_surface_dma(struct xa_context *ctx,
88	       struct xa_surface *srf,
89	       void *data,
90	       unsigned int pitch,
91	       int to_surface, struct xa_box *boxes, unsigned int num_boxes)
92{
93    struct pipe_transfer *transfer;
94    void *map;
95    int w, h, i;
96    enum pipe_transfer_usage transfer_direction;
97    struct pipe_context *pipe = ctx->pipe;
98
99    transfer_direction = (to_surface ? PIPE_TRANSFER_WRITE :
100			  PIPE_TRANSFER_READ);
101
102    for (i = 0; i < num_boxes; ++i, ++boxes) {
103	w = boxes->x2 - boxes->x1;
104	h = boxes->y2 - boxes->y1;
105
106	transfer = pipe_get_transfer(pipe, srf->tex, 0, 0,
107				     transfer_direction, boxes->x1, boxes->y1,
108				     w, h);
109	if (!transfer)
110	    return -XA_ERR_NORES;
111
112	map = pipe_transfer_map(ctx->pipe, transfer);
113	if (!map)
114	    goto out_no_map;
115
116	if (to_surface) {
117	    util_copy_rect(map, srf->tex->format, transfer->stride,
118			   0, 0, w, h, data, pitch, boxes->x1, boxes->y1);
119	} else {
120	    util_copy_rect(data, srf->tex->format, pitch,
121			   boxes->x1, boxes->y1, w, h, map, transfer->stride, 0,
122			   0);
123	}
124	pipe->transfer_unmap(pipe, transfer);
125	pipe->transfer_destroy(pipe, transfer);
126	if (to_surface)
127	    pipe->flush(pipe, &ctx->last_fence);
128    }
129    return XA_ERR_NONE;
130 out_no_map:
131    pipe->transfer_destroy(pipe, transfer);
132    return -XA_ERR_NORES;
133}
134
135void *
136xa_surface_map(struct xa_context *ctx,
137	       struct xa_surface *srf, unsigned int usage)
138{
139    void *map;
140    unsigned int transfer_direction = 0;
141    struct pipe_context *pipe = ctx->pipe;
142
143    /*
144     * A surface may only have a single map.
145     */
146    if (srf->transfer)
147	return NULL;
148
149    if (usage & XA_MAP_READ)
150	transfer_direction = PIPE_TRANSFER_READ;
151    if (usage & XA_MAP_WRITE)
152	transfer_direction = PIPE_TRANSFER_WRITE;
153
154    if (!transfer_direction)
155	return NULL;
156
157    srf->transfer = pipe_get_transfer(pipe, srf->tex, 0, 0,
158				      transfer_direction, 0, 0,
159				      srf->tex->width0, srf->tex->height0);
160    if (!srf->transfer)
161	return NULL;
162
163    map = pipe_transfer_map(pipe, srf->transfer);
164    if (!map)
165	pipe->transfer_destroy(pipe, srf->transfer);
166
167    srf->mapping_pipe = pipe;
168    return map;
169}
170
171void
172xa_surface_unmap(struct xa_surface *srf)
173{
174    if (srf->transfer) {
175	struct pipe_context *pipe = srf->mapping_pipe;
176
177	pipe->transfer_unmap(pipe, srf->transfer);
178	pipe->transfer_destroy(pipe, srf->transfer);
179	srf->transfer = NULL;
180    }
181}
182
183int
184xa_ctx_srf_create(struct xa_context *ctx, struct xa_surface *dst)
185{
186    struct pipe_screen *screen = ctx->pipe->screen;
187    struct pipe_surface srf_templ;
188
189    if (ctx->srf)
190	return -XA_ERR_INVAL;
191
192    if (!screen->is_format_supported(screen,  dst->tex->format,
193				     PIPE_TEXTURE_2D, 0,
194				     PIPE_BIND_RENDER_TARGET))
195	return -XA_ERR_INVAL;
196
197    u_surface_default_template(&srf_templ, dst->tex,
198			       PIPE_BIND_RENDER_TARGET);
199    ctx->srf = ctx->pipe->create_surface(ctx->pipe, dst->tex, &srf_templ);
200    if (!ctx->srf)
201	return -XA_ERR_NORES;
202
203    return XA_ERR_NONE;
204}
205
206void
207xa_ctx_srf_destroy(struct xa_context *ctx)
208{
209    pipe_surface_reference(&ctx->srf, NULL);
210}
211
212int
213xa_copy_prepare(struct xa_context *ctx,
214		struct xa_surface *dst, struct xa_surface *src)
215{
216    if (src == dst || ctx->srf != NULL)
217	return -XA_ERR_INVAL;
218
219    if (src->tex->format != dst->tex->format) {
220	int ret = xa_ctx_srf_create(ctx, dst);
221	if (ret != XA_ERR_NONE)
222	    return ret;
223	renderer_copy_prepare(ctx, ctx->srf, src->tex);
224	ctx->simple_copy = 0;
225    } else
226	ctx->simple_copy = 1;
227
228    ctx->src = src;
229    ctx->dst = dst;
230    xa_ctx_srf_destroy(ctx);
231
232    return 0;
233}
234
235void
236xa_copy(struct xa_context *ctx,
237	int dx, int dy, int sx, int sy, int width, int height)
238{
239    struct pipe_box src_box;
240
241    if (ctx->simple_copy) {
242	u_box_2d(sx, sy, width, height, &src_box);
243	ctx->pipe->resource_copy_region(ctx->pipe,
244					ctx->dst->tex, 0, dx, dy, 0,
245					ctx->src->tex,
246					0, &src_box);
247    } else
248	renderer_copy(ctx, dx, dy, sx, sy, width, height,
249		      (float) ctx->src->tex->width0,
250		      (float) ctx->src->tex->height0);
251}
252
253void
254xa_copy_done(struct xa_context *ctx)
255{
256    if (!ctx->simple_copy) {
257	   renderer_draw_flush(ctx);
258	   ctx->pipe->flush(ctx->pipe, &ctx->last_fence);
259    } else
260	ctx->pipe->flush(ctx->pipe, &ctx->last_fence);
261}
262
263static void
264bind_solid_blend_state(struct xa_context *ctx)
265{
266    struct pipe_blend_state blend;
267
268    memset(&blend, 0, sizeof(struct pipe_blend_state));
269    blend.rt[0].blend_enable = 0;
270    blend.rt[0].colormask = PIPE_MASK_RGBA;
271
272    blend.rt[0].rgb_src_factor   = PIPE_BLENDFACTOR_ONE;
273    blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
274    blend.rt[0].rgb_dst_factor   = PIPE_BLENDFACTOR_ZERO;
275    blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
276
277    cso_set_blend(ctx->cso, &blend);
278}
279
280int
281xa_solid_prepare(struct xa_context *ctx, struct xa_surface *dst,
282		 uint32_t fg)
283{
284    unsigned vs_traits, fs_traits;
285    struct xa_shader shader;
286    int width, height;
287    int ret;
288
289    ret = xa_ctx_srf_create(ctx, dst);
290    if (ret != XA_ERR_NONE)
291	return ret;
292
293    if (ctx->srf->format == PIPE_FORMAT_L8_UNORM)
294	xa_pixel_to_float4_a8(fg, ctx->solid_color);
295    else
296	xa_pixel_to_float4(fg, ctx->solid_color);
297    ctx->has_solid_color = 1;
298
299    ctx->dst = dst;
300    width = ctx->srf->width;
301    height = ctx->srf->height;
302
303#if 0
304    debug_printf("Color Pixel=(%d, %d, %d, %d), RGBA=(%f, %f, %f, %f)\n",
305		 (fg >> 24) & 0xff, (fg >> 16) & 0xff,
306		 (fg >> 8) & 0xff,  (fg >> 0) & 0xff,
307		 exa->solid_color[0], exa->solid_color[1],
308		 exa->solid_color[2], exa->solid_color[3]);
309#endif
310
311    vs_traits = VS_SOLID_FILL;
312    fs_traits = FS_SOLID_FILL;
313
314    renderer_bind_destination(ctx, ctx->srf, width, height);
315    bind_solid_blend_state(ctx);
316    cso_set_samplers(ctx->cso, 0, NULL);
317    cso_set_fragment_sampler_views(ctx->cso, 0, NULL);
318
319    shader = xa_shaders_get(ctx->shaders, vs_traits, fs_traits);
320    cso_set_vertex_shader_handle(ctx->cso, shader.vs);
321    cso_set_fragment_shader_handle(ctx->cso, shader.fs);
322
323    renderer_begin_solid(ctx);
324
325    xa_ctx_srf_destroy(ctx);
326    return XA_ERR_NONE;
327}
328
329void
330xa_solid(struct xa_context *ctx, int x, int y, int width, int height)
331{
332    renderer_solid(ctx, x, y, x + width, y + height, ctx->solid_color);
333}
334
335void
336xa_solid_done(struct xa_context *ctx)
337{
338    renderer_draw_flush(ctx);
339    ctx->pipe->flush(ctx->pipe, &ctx->last_fence);
340
341    ctx->comp = NULL;
342    ctx->has_solid_color = FALSE;
343    ctx->num_bound_samplers = 0;
344}
345
346struct xa_fence *
347xa_fence_get(struct xa_context *ctx)
348{
349    struct xa_fence *fence = calloc(1, sizeof(*fence));
350    struct pipe_screen *screen = ctx->xa->screen;
351
352    if (!fence)
353	return NULL;
354
355    fence->xa = ctx->xa;
356
357    if (ctx->last_fence == NULL)
358	fence->pipe_fence = NULL;
359    else
360	screen->fence_reference(screen, &fence->pipe_fence, ctx->last_fence);
361
362    return fence;
363}
364
365int
366xa_fence_wait(struct xa_fence *fence, uint64_t timeout)
367{
368    if (!fence)
369	return XA_ERR_NONE;
370
371    if (fence->pipe_fence) {
372	struct pipe_screen *screen = fence->xa->screen;
373	boolean timed_out;
374
375	timed_out = !screen->fence_finish(screen, fence->pipe_fence, timeout);
376	if (timed_out)
377	    return -XA_ERR_BUSY;
378
379	screen->fence_reference(screen, &fence->pipe_fence, NULL);
380    }
381    return XA_ERR_NONE;
382}
383
384void
385xa_fence_destroy(struct xa_fence *fence)
386{
387    if (!fence)
388	return;
389
390    if (fence->pipe_fence) {
391	struct pipe_screen *screen = fence->xa->screen;
392
393	screen->fence_reference(screen, &fence->pipe_fence, NULL);
394    }
395
396    free(fence);
397}
398
399void
400xa_ctx_sampler_views_destroy(struct xa_context *ctx)
401{
402    int i;
403
404    for (i = 0; i < ctx->num_bound_samplers; ++i)
405	pipe_sampler_view_reference(&ctx->bound_sampler_views[i], NULL);
406    ctx->num_bound_samplers = 0;
407}
408