aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/state_trackers/xa/xa_context.c
blob: ebfb290af13e32baa48e5ce25ef6b1d00c0ecc27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**********************************************************
 * Copyright 2009-2011 VMware, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *********************************************************
 * Authors:
 * Zack Rusin <zackr-at-vmware-dot-com>
 * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
 */
#include "xa_context.h"
#include "xa_priv.h"
#include "cso_cache/cso_context.h"
#include "util/u_inlines.h"
#include "util/u_rect.h"
#include "util/u_surface.h"
#include "pipe/p_context.h"

XA_EXPORT void
xa_context_flush(struct xa_context *ctx)
{
    if (ctx->last_fence) {
        struct pipe_screen *screen = ctx->xa->screen;
        screen->fence_reference(screen, &ctx->last_fence, NULL);
    }
    ctx->pipe->flush(ctx->pipe, &ctx->last_fence, 0);
}

XA_EXPORT struct xa_context *
xa_context_default(struct xa_tracker *xa)
{
    return xa->default_ctx;
}

XA_EXPORT struct xa_context *
xa_context_create(struct xa_tracker *xa)
{
    struct xa_context *ctx = calloc(1, sizeof(*ctx));

    ctx->xa = xa;
    ctx->pipe = xa->screen->context_create(xa->screen, NULL);
    ctx->cso = cso_create_context(ctx->pipe);
    ctx->shaders = xa_shaders_create(ctx);
    renderer_init_state(ctx);

    return ctx;
}

XA_EXPORT void
xa_context_destroy(struct xa_context *r)
{
    struct pipe_resource **vsbuf = &r->vs_const_buffer;
    struct pipe_resource **fsbuf = &r->fs_const_buffer;

    if (*vsbuf)
	pipe_resource_reference(vsbuf, NULL);

    if (*fsbuf)
	pipe_resource_reference(fsbuf, NULL);

    if (r->shaders) {
	xa_shaders_destroy(r->shaders);
	r->shaders = NULL;
    }

    xa_ctx_sampler_views_destroy(r);
    if (r->srf)
        pipe_surface_reference(&r->srf, NULL);

    if (r->cso) {
	cso_destroy_context(r->cso);
	r->cso = NULL;
    }

    r->pipe->destroy(r->pipe);
}

XA_EXPORT int
xa_surface_dma(struct xa_context *ctx,
	       struct xa_surface *srf,
	       void *data,
	       unsigned int pitch,
	       int to_surface, struct xa_box *boxes, unsigned int num_boxes)
{
    struct pipe_transfer *transfer;
    void *map;
    int w, h, i;
    enum pipe_transfer_usage transfer_direction;
    struct pipe_context *pipe = ctx->pipe;

    transfer_direction = (to_surface ? PIPE_TRANSFER_WRITE :
			  PIPE_TRANSFER_READ);

    for (i = 0; i < num_boxes; ++i, ++boxes) {
	w = boxes->x2 - boxes->x1;
	h = boxes->y2 - boxes->y1;

	map = pipe_transfer_map(pipe, srf->tex, 0, 0,
                                transfer_direction, boxes->x1, boxes->y1,
                                w, h, &transfer);
	if (!map)
	    return -XA_ERR_NORES;

	if (to_surface) {
	    util_copy_rect(map, srf->tex->format, transfer->stride,
			   0, 0, w, h, data, pitch, boxes->x1, boxes->y1);
	} else {
	    util_copy_rect(data, srf->tex->format, pitch,
			   boxes->x1, boxes->y1, w, h, map, transfer->stride, 0,
			   0);
	}
	pipe->transfer_unmap(pipe, transfer);
    }
    return XA_ERR_NONE;
}

XA_EXPORT void *
xa_surface_map(struct xa_context *ctx,
	       struct xa_surface *srf, unsigned int usage)
{
    void *map;
    unsigned int gallium_usage = 0;
    struct pipe_context *pipe = ctx->pipe;

    /*
     * A surface may only have a single map.
     */
    if (srf->transfer)
	return NULL;

    if (usage & XA_MAP_READ)
	gallium_usage |= PIPE_TRANSFER_READ;
    if (usage & XA_MAP_WRITE)
	gallium_usage |= PIPE_TRANSFER_WRITE;
    if (usage & XA_MAP_MAP_DIRECTLY)
	gallium_usage |= PIPE_TRANSFER_MAP_DIRECTLY;
    if (usage & XA_MAP_UNSYNCHRONIZED)
	gallium_usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
    if (usage & XA_MAP_DONTBLOCK)
	gallium_usage |= PIPE_TRANSFER_DONTBLOCK;
    if (usage & XA_MAP_DISCARD_WHOLE_RESOURCE)
	gallium_usage |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;

    if (!(gallium_usage & (PIPE_TRANSFER_READ_WRITE)))
	return NULL;

    map = pipe_transfer_map(pipe, srf->tex, 0, 0,
                            gallium_usage, 0, 0,
                            srf->tex->width0, srf->tex->height0,
                            &srf->transfer);
    if (!map)
	return NULL;

    srf->mapping_pipe = pipe;
    return map;
}

XA_EXPORT void
xa_surface_unmap(struct xa_surface *srf)
{
    if (srf->transfer) {
	struct pipe_context *pipe = srf->mapping_pipe;

	pipe->transfer_unmap(pipe, srf->transfer);
	srf->transfer = NULL;
    }
}

int
xa_ctx_srf_create(struct xa_context *ctx, struct xa_surface *dst)
{
    struct pipe_screen *screen = ctx->pipe->screen;
    struct pipe_surface srf_templ;

    /*
     * Cache surfaces unless we change render target
     */
    if (ctx->srf) {
        if (ctx->srf->texture == dst->tex)
            return XA_ERR_NONE;

        pipe_surface_reference(&ctx->srf, NULL);
    }

    if (!screen->is_format_supported(screen,  dst->tex->format,
				     PIPE_TEXTURE_2D, 0,
				     PIPE_BIND_RENDER_TARGET))
	return -XA_ERR_INVAL;

    u_surface_default_template(&srf_templ, dst->tex);
    ctx->srf = ctx->pipe->create_surface(ctx->pipe, dst->tex, &srf_templ);
    if (!ctx->srf)
	return -XA_ERR_NORES;

    return XA_ERR_NONE;
}

void
xa_ctx_srf_destroy(struct xa_context *ctx)
{
    /*
     * Cache surfaces unless we change render target.
     * Final destruction on context destroy.
     */
}

XA_EXPORT int
xa_copy_prepare(struct xa_context *ctx,
		struct xa_surface *dst, struct xa_surface *src)
{
    if (src == dst)
	return -XA_ERR_INVAL;

    if (src->tex->format != dst->tex->format) {
	int ret = xa_ctx_srf_create(ctx, dst);
	if (ret != XA_ERR_NONE)
	    return ret;
	renderer_copy_prepare(ctx, ctx->srf, src->tex,
			      src->fdesc.xa_format,
			      dst->fdesc.xa_format);
	ctx->simple_copy = 0;
    } else
	ctx->simple_copy = 1;

    ctx->src = src;
    ctx->dst = dst;
    xa_ctx_srf_destroy(ctx);

    return 0;
}

XA_EXPORT void
xa_copy(struct xa_context *ctx,
	int dx, int dy, int sx, int sy, int width, int height)
{
    struct pipe_box src_box;

    xa_scissor_update(ctx, dx, dy, dx + width, dy + height);

    if (ctx->simple_copy) {
	u_box_2d(sx, sy, width, height, &src_box);
	ctx->pipe->resource_copy_region(ctx->pipe,
					ctx->dst->tex, 0, dx, dy, 0,
					ctx->src->tex,
					0, &src_box);
    } else
	renderer_copy(ctx, dx, dy, sx, sy, width, height,
		      (float) ctx->src->tex->width0,
		      (float) ctx->src->tex->height0);
}

XA_EXPORT void
xa_copy_done(struct xa_context *ctx)
{
    if (!ctx->simple_copy) {
	renderer_draw_flush(ctx);
    }
}

static void
bind_solid_blend_state(struct xa_context *ctx)
{
    struct pipe_blend_state blend;

    memset(&blend, 0, sizeof(struct pipe_blend_state));
    blend.rt[0].blend_enable = 0;
    blend.rt[0].colormask = PIPE_MASK_RGBA;

    blend.rt[0].rgb_src_factor   = PIPE_BLENDFACTOR_ONE;
    blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
    blend.rt[0].rgb_dst_factor   = PIPE_BLENDFACTOR_ZERO;
    blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;

    cso_set_blend(ctx->cso, &blend);
}

XA_EXPORT int
xa_solid_prepare(struct xa_context *ctx, struct xa_surface *dst,
		 uint32_t fg)
{
    unsigned vs_traits, fs_traits;
    struct xa_shader shader;
    int ret;

    ret = xa_ctx_srf_create(ctx, dst);
    if (ret != XA_ERR_NONE)
	return ret;

    if (ctx->srf->format == PIPE_FORMAT_L8_UNORM)
	xa_pixel_to_float4_a8(fg, ctx->solid_color);
    else
	xa_pixel_to_float4(fg, ctx->solid_color);
    ctx->has_solid_color = 1;

    ctx->dst = dst;

#if 0
    debug_printf("Color Pixel=(%d, %d, %d, %d), RGBA=(%f, %f, %f, %f)\n",
		 (fg >> 24) & 0xff, (fg >> 16) & 0xff,
		 (fg >> 8) & 0xff,  (fg >> 0) & 0xff,
		 exa->solid_color[0], exa->solid_color[1],
		 exa->solid_color[2], exa->solid_color[3]);
#endif

    vs_traits = VS_SOLID_FILL;
    fs_traits = FS_SOLID_FILL;

    renderer_bind_destination(ctx, ctx->srf);
    bind_solid_blend_state(ctx);
    cso_set_samplers(ctx->cso, PIPE_SHADER_FRAGMENT, 0, NULL);
    cso_set_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT, 0, NULL);

    shader = xa_shaders_get(ctx->shaders, vs_traits, fs_traits);
    cso_set_vertex_shader_handle(ctx->cso, shader.vs);
    cso_set_fragment_shader_handle(ctx->cso, shader.fs);

    renderer_begin_solid(ctx);

    xa_ctx_srf_destroy(ctx);
    return XA_ERR_NONE;
}

XA_EXPORT void
xa_solid(struct xa_context *ctx, int x, int y, int width, int height)
{
    xa_scissor_update(ctx, x, y, x + width, y + height);
    renderer_solid(ctx, x, y, x + width, y + height, ctx->solid_color);
}

XA_EXPORT void
xa_solid_done(struct xa_context *ctx)
{
    renderer_draw_flush(ctx);
    ctx->comp = NULL;
    ctx->has_solid_color = FALSE;
    ctx->num_bound_samplers = 0;
}

XA_EXPORT struct xa_fence *
xa_fence_get(struct xa_context *ctx)
{
    struct xa_fence *fence = calloc(1, sizeof(*fence));
    struct pipe_screen *screen = ctx->xa->screen;

    if (!fence)
	return NULL;

    fence->xa = ctx->xa;

    if (ctx->last_fence == NULL)
	fence->pipe_fence = NULL;
    else
	screen->fence_reference(screen, &fence->pipe_fence, ctx->last_fence);

    return fence;
}

XA_EXPORT int
xa_fence_wait(struct xa_fence *fence, uint64_t timeout)
{
    if (!fence)
	return XA_ERR_NONE;

    if (fence->pipe_fence) {
	struct pipe_screen *screen = fence->xa->screen;
	boolean timed_out;

	timed_out = !screen->fence_finish(screen, fence->pipe_fence, timeout);
	if (timed_out)
	    return -XA_ERR_BUSY;

	screen->fence_reference(screen, &fence->pipe_fence, NULL);
    }
    return XA_ERR_NONE;
}

XA_EXPORT void
xa_fence_destroy(struct xa_fence *fence)
{
    if (!fence)
	return;

    if (fence->pipe_fence) {
	struct pipe_screen *screen = fence->xa->screen;

	screen->fence_reference(screen, &fence->pipe_fence, NULL);
    }

    free(fence);
}

void
xa_ctx_sampler_views_destroy(struct xa_context *ctx)
{
    int i;

    for (i = 0; i < ctx->num_bound_samplers; ++i)
	pipe_sampler_view_reference(&ctx->bound_sampler_views[i], NULL);
    ctx->num_bound_samplers = 0;
}