st/vega: Get rid of renderer_copy_texture.
[mesa.git] / src / gallium / state_trackers / vega / mask.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #include "mask.h"
28
29 #include "path.h"
30 #include "image.h"
31 #include "shaders_cache.h"
32 #include "renderer.h"
33 #include "asm_util.h"
34 #include "st_inlines.h"
35
36 #include "pipe/p_context.h"
37 #include "pipe/p_screen.h"
38 #include "util/u_inlines.h"
39 #include "util/u_format.h"
40 #include "util/u_memory.h"
41
42 struct vg_mask_layer {
43 struct vg_object base;
44
45 VGint width;
46 VGint height;
47
48 struct pipe_sampler_view *sampler_view;
49 };
50
51 static INLINE struct pipe_surface *
52 alpha_mask_surface(struct vg_context *ctx, int usage)
53 {
54 struct pipe_screen *screen = ctx->pipe->screen;
55 struct st_framebuffer *stfb = ctx->draw_buffer;
56 return screen->get_tex_surface(screen,
57 stfb->alpha_mask_view->texture,
58 0, 0, 0,
59 usage);
60 }
61
62 static INLINE VGboolean
63 intersect_rectangles(VGint dwidth, VGint dheight,
64 VGint swidth, VGint sheight,
65 VGint tx, VGint ty,
66 VGint twidth, VGint theight,
67 VGint *offsets,
68 VGint *location)
69 {
70 if (tx + twidth <= 0 || tx >= dwidth)
71 return VG_FALSE;
72 if (ty + theight <= 0 || ty >= dheight)
73 return VG_FALSE;
74
75 offsets[0] = 0;
76 offsets[1] = 0;
77 location[0] = tx;
78 location[1] = ty;
79
80 if (tx < 0) {
81 offsets[0] -= tx;
82 location[0] = 0;
83
84 location[2] = MIN2(tx + swidth, MIN2(dwidth, tx + twidth));
85 offsets[2] = location[2];
86 } else {
87 offsets[2] = MIN2(twidth, MIN2(dwidth - tx, swidth ));
88 location[2] = offsets[2];
89 }
90
91 if (ty < 0) {
92 offsets[1] -= ty;
93 location[1] = 0;
94
95 location[3] = MIN2(ty + sheight, MIN2(dheight, ty + theight));
96 offsets[3] = location[3];
97 } else {
98 offsets[3] = MIN2(theight, MIN2(dheight - ty, sheight));
99 location[3] = offsets[3];
100 }
101
102 return VG_TRUE;
103 }
104
105 #if DEBUG_MASKS
106 static void read_alpha_mask(void * data, VGint dataStride,
107 VGImageFormat dataFormat,
108 VGint sx, VGint sy,
109 VGint width, VGint height)
110 {
111 struct vg_context *ctx = vg_current_context();
112 struct pipe_context *pipe = ctx->pipe;
113 struct pipe_screen *screen = pipe->screen;
114
115 struct st_framebuffer *stfb = ctx->draw_buffer;
116 struct st_renderbuffer *strb = stfb->alpha_mask;
117
118 VGfloat temp[VEGA_MAX_IMAGE_WIDTH][4];
119 VGfloat *df = (VGfloat*)temp;
120 VGint y = (stfb->height - sy) - 1, yStep = -1;
121 VGint i;
122 VGubyte *dst = (VGubyte *)data;
123 VGint xoffset = 0, yoffset = 0;
124
125 /* make sure rendering has completed */
126 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
127 if (sx < 0) {
128 xoffset = -sx;
129 xoffset *= _vega_size_for_format(dataFormat);
130 width += sx;
131 sx = 0;
132 }
133 if (sy < 0) {
134 yoffset = -sy;
135 height += sy;
136 sy = 0;
137 y = (stfb->height - sy) - 1;
138 yoffset *= dataStride;
139 }
140
141 {
142 struct pipe_surface *surf;
143
144 surf = screen->get_tex_surface(screen, strb->texture, 0, 0, 0,
145 PIPE_BIND_TRANSFER_READ);
146
147 /* Do a row at a time to flip image data vertically */
148 for (i = 0; i < height; i++) {
149 #if 0
150 debug_printf("%d-%d == %d\n", sy, height, y);
151 #endif
152 pipe_get_tile_rgba(surf, sx, y, width, 1, df);
153 y += yStep;
154 _vega_pack_rgba_span_float(ctx, width, temp, dataFormat,
155 dst + yoffset + xoffset);
156 dst += dataStride;
157 }
158
159 pipe_surface_reference(&surf, NULL);
160 }
161 }
162
163 void save_alpha_to_file(const char *filename)
164 {
165 struct vg_context *ctx = vg_current_context();
166 struct st_framebuffer *stfb = ctx->draw_buffer;
167 VGint *data;
168 int i, j;
169
170 data = malloc(sizeof(int) * stfb->width * stfb->height);
171 read_alpha_mask(data, stfb->width * sizeof(int),
172 VG_sRGBA_8888,
173 0, 0, stfb->width, stfb->height);
174 fprintf(stderr, "/*---------- start */\n");
175 fprintf(stderr, "const int image_width = %d;\n",
176 stfb->width);
177 fprintf(stderr, "const int image_height = %d;\n",
178 stfb->height);
179 fprintf(stderr, "const int image_data = {\n");
180 for (i = 0; i < stfb->height; ++i) {
181 for (j = 0; j < stfb->width; ++j) {
182 int rgba = data[i * stfb->height + j];
183 int argb = 0;
184 argb = (rgba >> 8);
185 argb |= ((rgba & 0xff) << 24);
186 fprintf(stderr, "0x%x, ", argb);
187 }
188 fprintf(stderr, "\n");
189 }
190 fprintf(stderr, "};\n");
191 fprintf(stderr, "/*---------- end */\n");
192 }
193 #endif
194
195 /* setup mask shader */
196 static void *setup_mask_operation(VGMaskOperation operation)
197 {
198 struct vg_context *ctx = vg_current_context();
199 void *shader = 0;
200
201 switch (operation) {
202 case VG_UNION_MASK: {
203 if (!ctx->mask.union_fs) {
204 ctx->mask.union_fs = shader_create_from_text(ctx->pipe,
205 union_mask_asm,
206 200,
207 PIPE_SHADER_FRAGMENT);
208 }
209 shader = ctx->mask.union_fs->driver;
210 }
211 break;
212 case VG_INTERSECT_MASK: {
213 if (!ctx->mask.intersect_fs) {
214 ctx->mask.intersect_fs = shader_create_from_text(ctx->pipe,
215 intersect_mask_asm,
216 200,
217 PIPE_SHADER_FRAGMENT);
218 }
219 shader = ctx->mask.intersect_fs->driver;
220 }
221 break;
222 case VG_SUBTRACT_MASK: {
223 if (!ctx->mask.subtract_fs) {
224 ctx->mask.subtract_fs = shader_create_from_text(ctx->pipe,
225 subtract_mask_asm,
226 200,
227 PIPE_SHADER_FRAGMENT);
228 }
229 shader = ctx->mask.subtract_fs->driver;
230 }
231 break;
232 case VG_SET_MASK: {
233 if (!ctx->mask.set_fs) {
234 ctx->mask.set_fs = shader_create_from_text(ctx->pipe,
235 set_mask_asm,
236 200,
237 PIPE_SHADER_FRAGMENT);
238 }
239 shader = ctx->mask.set_fs->driver;
240 }
241 break;
242 default:
243 assert(0);
244 break;
245 }
246
247 return shader;
248 }
249
250 static void mask_resource_fill(struct pipe_resource *dst,
251 int x, int y, int width, int height,
252 VGfloat coverage)
253 {
254 struct vg_context *ctx = vg_current_context();
255 VGfloat color[4] = { 0.0f, 0.0f, 0.0f, coverage };
256 void *fs;
257
258 if (x < 0) {
259 width += x;
260 x = 0;
261 }
262 if (y < 0) {
263 height += y;
264 y = 0;
265 }
266
267 fs = shaders_cache_fill(ctx->sc, VEGA_SOLID_FILL_SHADER);
268
269 if (renderer_filter_begin(ctx->renderer, dst, VG_FALSE,
270 ~0, NULL, NULL, 0, fs, (const void *) color, sizeof(color))) {
271 renderer_filter(ctx->renderer, x, y, width, height, 0, 0, 0, 0);
272 renderer_filter_end(ctx->renderer);
273 }
274
275 #if DEBUG_MASKS
276 save_alpha_to_file(0);
277 #endif
278 }
279
280
281 static void mask_using_texture(struct pipe_sampler_view *sampler_view,
282 VGMaskOperation operation,
283 VGint x, VGint y,
284 VGint width, VGint height)
285 {
286 struct vg_context *ctx = vg_current_context();
287 struct pipe_resource *dst = ctx->draw_buffer->alpha_mask_view->texture;
288 struct pipe_resource *texture = sampler_view->texture;
289 const struct pipe_sampler_state *samplers[2];
290 struct pipe_sampler_view *views[2];
291 struct pipe_sampler_state sampler;
292 VGint offsets[4], loc[4];
293 const VGfloat ones[4] = {1.f, 1.f, 1.f, 1.f};
294 void *fs;
295
296 if (!intersect_rectangles(dst->width0, dst->height0,
297 texture->width0, texture->height0,
298 x, y, width, height,
299 offsets, loc))
300 return;
301 #if 0
302 debug_printf("Offset = [%d, %d, %d, %d]\n", offsets[0],
303 offsets[1], offsets[2], offsets[3]);
304 debug_printf("Locati = [%d, %d, %d, %d]\n", loc[0],
305 loc[1], loc[2], loc[3]);
306 #endif
307
308 sampler = ctx->mask.sampler;
309 sampler.normalized_coords = 1;
310 samplers[0] = &sampler;
311 views[0] = sampler_view;
312
313 /* prepare our blend surface */
314 vg_prepare_blend_surface_from_mask(ctx);
315 samplers[1] = &ctx->mask.sampler;
316 views[1] = ctx->draw_buffer->blend_texture_view;
317
318 fs = setup_mask_operation(operation);
319
320 if (renderer_filter_begin(ctx->renderer, dst, VG_FALSE,
321 ~0, samplers, views, 2, fs, (const void *) ones, sizeof(ones))) {
322 renderer_filter(ctx->renderer,
323 loc[0], loc[1], loc[2], loc[3],
324 offsets[0], offsets[1], offsets[2], offsets[3]);
325 renderer_filter_end(ctx->renderer);
326 }
327 }
328
329
330 #ifdef OPENVG_VERSION_1_1
331
332 struct vg_mask_layer * mask_layer_create(VGint width, VGint height)
333 {
334 struct vg_context *ctx = vg_current_context();
335 struct vg_mask_layer *mask = 0;
336
337 mask = CALLOC_STRUCT(vg_mask_layer);
338 vg_init_object(&mask->base, ctx, VG_OBJECT_MASK);
339 mask->width = width;
340 mask->height = height;
341
342 {
343 struct pipe_resource pt;
344 struct pipe_context *pipe = ctx->pipe;
345 struct pipe_screen *screen = ctx->pipe->screen;
346 struct pipe_sampler_view view_templ;
347 struct pipe_sampler_view *view = NULL;
348 struct pipe_resource *texture;
349
350 memset(&pt, 0, sizeof(pt));
351 pt.target = PIPE_TEXTURE_2D;
352 pt.format = PIPE_FORMAT_B8G8R8A8_UNORM;
353 pt.last_level = 0;
354 pt.width0 = width;
355 pt.height0 = height;
356 pt.depth0 = 1;
357 pt.bind = PIPE_BIND_SAMPLER_VIEW;
358 pt.compressed = 0;
359
360 texture = screen->resource_create(screen, &pt);
361
362 if (texture) {
363 u_sampler_view_default_template(&view_templ, texture, texture->format);
364 view = pipe->create_sampler_view(pipe, texture, &view_templ);
365 }
366 pipe_resource_reference(&texture, NULL);
367 mask->sampler_view = view;
368 }
369
370 vg_context_add_object(ctx, VG_OBJECT_MASK, mask);
371
372 return mask;
373 }
374
375 void mask_layer_destroy(struct vg_mask_layer *layer)
376 {
377 struct vg_context *ctx = vg_current_context();
378
379 vg_context_remove_object(ctx, VG_OBJECT_MASK, layer);
380 pipe_resource_release(&layer->texture);
381 FREE(layer);
382 }
383
384 void mask_layer_fill(struct vg_mask_layer *layer,
385 VGint x, VGint y,
386 VGint width, VGint height,
387 VGfloat value)
388 {
389 VGfloat alpha_color[4] = {0, 0, 0, 0};
390
391 alpha_color[3] = value;
392
393 mask_resource_fill(layer->sampler_view->texture,
394 x, y, width, height, value);
395 }
396
397 void mask_copy(struct vg_mask_layer *layer,
398 VGint sx, VGint sy,
399 VGint dx, VGint dy,
400 VGint width, VGint height)
401 {
402 struct vg_context *ctx = vg_current_context();
403 struct pipe_sampler_view *src = ctx->draw_buffer->alpha_mask_view;
404 struct pipe_surface *surf;
405
406 /* get the destination surface */
407 surf = ctx->pipe->screen->get_tex_surface(ctx->pipe->screen,
408 layer->sampler_view->texture, 0, 0, 0, PIPE_BIND_RENDER_TARGET);
409 if (surf && renderer_copy_begin(ctx->renderer, surf, VG_FALSE, src)) {
410 renderer_copy(ctx->renderer,
411 dx, dy, width, height,
412 sx, sy, width, height);
413 renderer_copy_end(ctx->renderer);
414 }
415
416 pipe_surface_reference(&surf, NULL);
417 }
418
419 static void mask_layer_render_to(struct vg_mask_layer *layer,
420 struct path *path,
421 VGbitfield paint_modes)
422 {
423 #if 0
424 struct vg_context *ctx = vg_current_context();
425 const VGfloat fill_color[4] = {1.f, 1.f, 1.f, 1.f};
426 struct pipe_screen *screen = ctx->pipe->screen;
427 struct pipe_surface *surface;
428
429 surface = screen->get_tex_surface(screen, layer->sampler_view->texture, 0, 0, 0,
430 PIPE_BIND_RENDER_TARGET);
431
432 cso_save_framebuffer(ctx->cso_context);
433 cso_save_fragment_shader(ctx->cso_context);
434
435 setup_mask_blend();
436 setup_mask_fill(fill_color);
437 setup_mask_framebuffer(surface, layer->width, layer->height);
438
439 if (paint_modes & VG_FILL_PATH) {
440 struct matrix *mat = &ctx->state.vg.path_user_to_surface_matrix;
441 path_fill(path, mat);
442 }
443
444 if (paint_modes & VG_STROKE_PATH){
445 path_stroke(path);
446 }
447
448
449 /* make sure rendering has completed */
450 ctx->pipe->flush(ctx->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
451
452 cso_restore_framebuffer(ctx->cso_context);
453 cso_restore_fragment_shader(ctx->cso_context);
454 ctx->state.dirty |= BLEND_DIRTY;
455
456 pipe_surface_reference(&surface, NULL);
457 #endif
458 }
459
460 void mask_render_to(struct path *path,
461 VGbitfield paint_modes,
462 VGMaskOperation operation)
463 {
464 struct vg_context *ctx = vg_current_context();
465 struct st_framebuffer *fb_buffers = ctx->draw_buffer;
466 struct vg_mask_layer *temp_layer;
467 VGint width, height;
468
469 width = fb_buffers->alpha_mask_view->texture->width0;
470 height = fb_buffers->alpha_mask_view->texture->width0;
471
472 temp_layer = mask_layer_create(width, height);
473
474 mask_layer_render_to(temp_layer, path, paint_modes);
475
476 mask_using_layer(temp_layer, 0, 0, width, height,
477 operation);
478
479 mask_layer_destroy(temp_layer);
480 }
481
482 void mask_using_layer(struct vg_mask_layer *layer,
483 VGMaskOperation operation,
484 VGint x, VGint y,
485 VGint width, VGint height)
486 {
487 mask_using_texture(layer->sampler_view, operation,
488 x, y, width, height);
489 }
490
491 VGint mask_layer_width(struct vg_mask_layer *layer)
492 {
493 return layer->width;
494 }
495
496 VGint mask_layer_height(struct vg_mask_layer *layer)
497 {
498 return layer->height;
499 }
500
501
502 #endif
503
504 void mask_using_image(struct vg_image *image,
505 VGMaskOperation operation,
506 VGint x, VGint y,
507 VGint width, VGint height)
508 {
509 mask_using_texture(image->sampler_view, operation,
510 x, y, width, height);
511 }
512
513 void mask_fill(VGint x, VGint y, VGint width, VGint height,
514 VGfloat value)
515 {
516 struct vg_context *ctx = vg_current_context();
517
518 #if DEBUG_MASKS
519 debug_printf("mask_fill(%d, %d, %d, %d) with rgba(%f, %f, %f, %f)\n",
520 x, y, width, height,
521 0.0f, 0.0f, 0.0f, value);
522 #endif
523
524 mask_resource_fill(ctx->draw_buffer->alpha_mask_view->texture,
525 x, y, width, height, value);
526 }
527
528 VGint mask_bind_samplers(struct pipe_sampler_state **samplers,
529 struct pipe_sampler_view **sampler_views)
530 {
531 struct vg_context *ctx = vg_current_context();
532
533 if (ctx->state.vg.masking) {
534 struct st_framebuffer *fb_buffers = ctx->draw_buffer;
535
536 samplers[1] = &ctx->mask.sampler;
537 sampler_views[1] = fb_buffers->alpha_mask_view;
538 return 1;
539 } else
540 return 0;
541 }