1 /**************************************************************************
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
31 #include "shaders_cache.h"
34 #include "st_inlines.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_screen.h"
38 #include "pipe/p_inlines.h"
39 #include "util/u_format.h"
40 #include "util/u_memory.h"
42 struct vg_mask_layer
{
43 struct vg_object base
;
48 struct pipe_texture
*texture
;
51 static INLINE
struct pipe_surface
*
52 alpha_mask_surface(struct vg_context
*ctx
, int usage
)
54 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
55 struct st_framebuffer
*stfb
= ctx
->draw_buffer
;
56 return screen
->get_tex_surface(screen
,
62 static INLINE VGboolean
63 intersect_rectangles(VGint dwidth
, VGint dheight
,
64 VGint swidth
, VGint sheight
,
66 VGint twidth
, VGint theight
,
70 if (tx
+ twidth
<= 0 || tx
>= dwidth
)
72 if (ty
+ theight
<= 0 || ty
>= dheight
)
84 location
[2] = MIN2(tx
+ swidth
, MIN2(dwidth
, tx
+ twidth
));
85 offsets
[2] = location
[2];
87 offsets
[2] = MIN2(twidth
, MIN2(dwidth
- tx
, swidth
));
88 location
[2] = offsets
[2];
95 location
[3] = MIN2(ty
+ sheight
, MIN2(dheight
, ty
+ theight
));
96 offsets
[3] = location
[3];
98 offsets
[3] = MIN2(theight
, MIN2(dheight
- ty
, sheight
));
99 location
[3] = offsets
[3];
106 static void read_alpha_mask(void * data
, VGint dataStride
,
107 VGImageFormat dataFormat
,
109 VGint width
, VGint height
)
111 struct vg_context
*ctx
= vg_current_context();
112 struct pipe_context
*pipe
= ctx
->pipe
;
113 struct pipe_screen
*screen
= pipe
->screen
;
115 struct st_framebuffer
*stfb
= ctx
->draw_buffer
;
116 struct st_renderbuffer
*strb
= stfb
->alpha_mask
;
117 struct pipe_framebuffer_state
*fb
= &ctx
->state
.g3d
.fb
;
119 VGfloat temp
[VEGA_MAX_IMAGE_WIDTH
][4];
120 VGfloat
*df
= (VGfloat
*)temp
;
121 VGint y
= (fb
->height
- sy
) - 1, yStep
= -1;
123 VGubyte
*dst
= (VGubyte
*)data
;
124 VGint xoffset
= 0, yoffset
= 0;
126 /* make sure rendering has completed */
127 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
130 xoffset
*= _vega_size_for_format(dataFormat
);
138 y
= (fb
->height
- sy
) - 1;
139 yoffset
*= dataStride
;
143 struct pipe_surface
*surf
;
145 surf
= screen
->get_tex_surface(screen
, strb
->texture
, 0, 0, 0,
146 PIPE_BUFFER_USAGE_CPU_READ
);
148 /* Do a row at a time to flip image data vertically */
149 for (i
= 0; i
< height
; i
++) {
151 debug_printf("%d-%d == %d\n", sy
, height
, y
);
153 pipe_get_tile_rgba(surf
, sx
, y
, width
, 1, df
);
155 _vega_pack_rgba_span_float(ctx
, width
, temp
, dataFormat
,
156 dst
+ yoffset
+ xoffset
);
160 pipe_surface_reference(&surf
, NULL
);
164 void save_alpha_to_file(const char *filename
)
166 struct vg_context
*ctx
= vg_current_context();
167 struct pipe_framebuffer_state
*fb
= &ctx
->state
.g3d
.fb
;
171 data
= malloc(sizeof(int) * fb
->width
* fb
->height
);
172 read_alpha_mask(data
, fb
->width
* sizeof(int),
174 0, 0, fb
->width
, fb
->height
);
175 fprintf(stderr
, "/*---------- start */\n");
176 fprintf(stderr
, "const int image_width = %d;\n",
178 fprintf(stderr
, "const int image_height = %d;\n",
180 fprintf(stderr
, "const int image_data = {\n");
181 for (i
= 0; i
< fb
->height
; ++i
) {
182 for (j
= 0; j
< fb
->width
; ++j
) {
183 int rgba
= data
[i
* fb
->height
+ j
];
186 argb
|= ((rgba
& 0xff) << 24);
187 fprintf(stderr
, "0x%x, ", argb
);
189 fprintf(stderr
, "\n");
191 fprintf(stderr
, "};\n");
192 fprintf(stderr
, "/*---------- end */\n");
196 static void setup_mask_framebuffer(struct pipe_surface
*surf
,
197 VGint surf_width
, VGint surf_height
)
199 struct vg_context
*ctx
= vg_current_context();
200 struct pipe_framebuffer_state fb
;
202 memset(&fb
, 0, sizeof(fb
));
203 fb
.width
= surf_width
;
204 fb
.height
= surf_height
;
209 for (i
= 1; i
< PIPE_MAX_COLOR_BUFS
; ++i
)
212 cso_set_framebuffer(ctx
->cso_context
, &fb
);
216 /* setup shader constants */
217 static void setup_mask_operation(VGMaskOperation operation
)
219 struct vg_context
*ctx
= vg_current_context();
220 struct pipe_buffer
**cbuf
= &ctx
->mask
.cbuf
;
221 const VGint param_bytes
= 4 * sizeof(VGfloat
);
222 const VGfloat ones
[4] = {1.f
, 1.f
, 1.f
, 1.f
};
225 /* We always need to get a new buffer, to keep the drivers simple and
226 * avoid gratuitous rendering synchronization.
228 pipe_buffer_reference(cbuf
, NULL
);
230 *cbuf
= pipe_buffer_create(ctx
->pipe
->screen
, 1,
231 PIPE_BUFFER_USAGE_CONSTANT
,
234 st_no_flush_pipe_buffer_write(ctx
, *cbuf
,
235 0, param_bytes
, ones
);
238 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, *cbuf
);
240 case VG_UNION_MASK
: {
241 if (!ctx
->mask
.union_fs
) {
242 ctx
->mask
.union_fs
= shader_create_from_text(ctx
->pipe
,
245 PIPE_SHADER_FRAGMENT
);
247 shader
= ctx
->mask
.union_fs
->driver
;
250 case VG_INTERSECT_MASK
: {
251 if (!ctx
->mask
.intersect_fs
) {
252 ctx
->mask
.intersect_fs
= shader_create_from_text(ctx
->pipe
,
255 PIPE_SHADER_FRAGMENT
);
257 shader
= ctx
->mask
.intersect_fs
->driver
;
260 case VG_SUBTRACT_MASK
: {
261 if (!ctx
->mask
.subtract_fs
) {
262 ctx
->mask
.subtract_fs
= shader_create_from_text(ctx
->pipe
,
265 PIPE_SHADER_FRAGMENT
);
267 shader
= ctx
->mask
.subtract_fs
->driver
;
271 if (!ctx
->mask
.set_fs
) {
272 ctx
->mask
.set_fs
= shader_create_from_text(ctx
->pipe
,
275 PIPE_SHADER_FRAGMENT
);
277 shader
= ctx
->mask
.set_fs
->driver
;
284 cso_set_fragment_shader_handle(ctx
->cso_context
, shader
);
287 static void setup_mask_samplers(struct pipe_texture
*umask
)
289 struct vg_context
*ctx
= vg_current_context();
290 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
291 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
292 struct st_framebuffer
*fb_buffers
= ctx
->draw_buffer
;
293 struct pipe_texture
*uprev
= NULL
;
294 struct pipe_sampler_state sampler
;
296 uprev
= fb_buffers
->blend_texture
;
297 sampler
= ctx
->mask
.sampler
;
298 sampler
.normalized_coords
= 1;
307 samplers
[0] = &sampler
;
308 samplers
[1] = &ctx
->mask
.sampler
;
313 cso_set_samplers(ctx
->cso_context
, 2,
314 (const struct pipe_sampler_state
**)samplers
);
315 cso_set_sampler_textures(ctx
->cso_context
, 2, textures
);
319 /* setup shader constants */
320 static void setup_mask_fill(const VGfloat color
[4])
322 struct vg_context
*ctx
= vg_current_context();
323 struct pipe_buffer
**cbuf
= &ctx
->mask
.cbuf
;
324 const VGint param_bytes
= 4 * sizeof(VGfloat
);
326 /* We always need to get a new buffer, to keep the drivers simple and
327 * avoid gratuitous rendering synchronization.
329 pipe_buffer_reference(cbuf
, NULL
);
331 *cbuf
= pipe_buffer_create(ctx
->pipe
->screen
, 1,
332 PIPE_BUFFER_USAGE_CONSTANT
,
335 st_no_flush_pipe_buffer_write(ctx
, *cbuf
, 0, param_bytes
, color
);
338 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, *cbuf
);
339 cso_set_fragment_shader_handle(ctx
->cso_context
,
340 shaders_cache_fill(ctx
->sc
,
341 VEGA_SOLID_FILL_SHADER
));
344 static void setup_mask_viewport()
346 struct vg_context
*ctx
= vg_current_context();
347 vg_set_viewport(ctx
, VEGA_Y0_TOP
);
350 static void setup_mask_blend()
352 struct vg_context
*ctx
= vg_current_context();
354 struct pipe_blend_state blend
;
356 memset(&blend
, 0, sizeof(struct pipe_blend_state
));
357 blend
.rt
[0].blend_enable
= 0;
358 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
359 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
360 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
361 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
362 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
364 cso_set_blend(ctx
->cso_context
, &blend
);
368 static void surface_fill(struct pipe_surface
*surf
,
369 int surf_width
, int surf_height
,
370 int x
, int y
, int width
, int height
,
371 const VGfloat color
[4])
373 struct vg_context
*ctx
= vg_current_context();
384 cso_save_framebuffer(ctx
->cso_context
);
385 cso_save_blend(ctx
->cso_context
);
386 cso_save_fragment_shader(ctx
->cso_context
);
387 cso_save_viewport(ctx
->cso_context
);
390 setup_mask_fill(color
);
391 setup_mask_framebuffer(surf
, surf_width
, surf_height
);
392 setup_mask_viewport();
394 renderer_draw_quad(ctx
->renderer
, x
, y
,
395 x
+ width
, y
+ height
, 0.0f
/*depth should be disabled*/);
398 /* make sure rendering has completed */
399 ctx
->pipe
->flush(ctx
->pipe
,
400 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
,
404 save_alpha_to_file(0);
407 cso_restore_blend(ctx
->cso_context
);
408 cso_restore_framebuffer(ctx
->cso_context
);
409 cso_restore_fragment_shader(ctx
->cso_context
);
410 cso_restore_viewport(ctx
->cso_context
);
414 static void mask_using_texture(struct pipe_texture
*texture
,
415 VGMaskOperation operation
,
417 VGint width
, VGint height
)
419 struct vg_context
*ctx
= vg_current_context();
420 struct pipe_surface
*surface
=
421 alpha_mask_surface(ctx
, PIPE_BUFFER_USAGE_GPU_WRITE
);
422 VGint offsets
[4], loc
[4];
426 if (!intersect_rectangles(surface
->width
, surface
->height
,
427 texture
->width0
, texture
->height0
,
432 debug_printf("Offset = [%d, %d, %d, %d]\n", offsets
[0],
433 offsets
[1], offsets
[2], offsets
[3]);
434 debug_printf("Locati = [%d, %d, %d, %d]\n", loc
[0],
435 loc
[1], loc
[2], loc
[3]);
438 /* prepare our blend surface */
439 vg_prepare_blend_surface_from_mask(ctx
);
441 cso_save_samplers(ctx
->cso_context
);
442 cso_save_sampler_textures(ctx
->cso_context
);
443 cso_save_framebuffer(ctx
->cso_context
);
444 cso_save_blend(ctx
->cso_context
);
445 cso_save_fragment_shader(ctx
->cso_context
);
446 cso_save_viewport(ctx
->cso_context
);
448 setup_mask_samplers(texture
);
450 setup_mask_operation(operation
);
451 setup_mask_framebuffer(surface
, surface
->width
, surface
->height
);
452 setup_mask_viewport();
454 /* render the quad to propagate the rendering from stencil */
455 renderer_draw_texture(ctx
->renderer
, texture
,
456 offsets
[0], offsets
[1],
457 offsets
[0] + offsets
[2], offsets
[1] + offsets
[3],
458 loc
[0], loc
[1], loc
[0] + loc
[2], loc
[1] + loc
[3]);
460 /* make sure rendering has completed */
461 ctx
->pipe
->flush(ctx
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
462 cso_restore_blend(ctx
->cso_context
);
463 cso_restore_framebuffer(ctx
->cso_context
);
464 cso_restore_fragment_shader(ctx
->cso_context
);
465 cso_restore_samplers(ctx
->cso_context
);
466 cso_restore_sampler_textures(ctx
->cso_context
);
467 cso_restore_viewport(ctx
->cso_context
);
469 pipe_surface_reference(&surface
, NULL
);
473 #ifdef OPENVG_VERSION_1_1
475 struct vg_mask_layer
* mask_layer_create(VGint width
, VGint height
)
477 struct vg_context
*ctx
= vg_current_context();
478 struct vg_mask_layer
*mask
= 0;
480 mask
= CALLOC_STRUCT(vg_mask_layer
);
481 vg_init_object(&mask
->base
, ctx
, VG_OBJECT_MASK
);
483 mask
->height
= height
;
486 struct pipe_texture pt
;
487 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
489 memset(&pt
, 0, sizeof(pt
));
490 pt
.target
= PIPE_TEXTURE_2D
;
491 pt
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
496 pt
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
499 mask
->texture
= screen
->texture_create(screen
, &pt
);
502 vg_context_add_object(ctx
, VG_OBJECT_MASK
, mask
);
507 void mask_layer_destroy(struct vg_mask_layer
*layer
)
509 struct vg_context
*ctx
= vg_current_context();
511 vg_context_remove_object(ctx
, VG_OBJECT_MASK
, layer
);
512 pipe_texture_release(&layer
->texture
);
516 void mask_layer_fill(struct vg_mask_layer
*layer
,
518 VGint width
, VGint height
,
521 struct vg_context
*ctx
= vg_current_context();
522 VGfloat alpha_color
[4] = {0, 0, 0, 0};
523 struct pipe_surface
*surface
;
525 alpha_color
[3] = value
;
527 surface
= ctx
->pipe
->screen
->get_tex_surface(
528 ctx
->pipe
->screen
, layer
->texture
,
530 PIPE_BUFFER_USAGE_GPU_WRITE
);
532 surface_fill(surface
,
533 layer
->width
, layer
->height
,
534 x
, y
, width
, height
, alpha_color
);
536 ctx
->pipe
->screen
->tex_surface_release(ctx
->pipe
->screen
, &surface
);
539 void mask_copy(struct vg_mask_layer
*layer
,
542 VGint width
, VGint height
)
544 struct vg_context
*ctx
= vg_current_context();
545 struct st_framebuffer
*fb_buffers
= ctx
->draw_buffer
;
547 renderer_copy_texture(ctx
->renderer
,
550 sx
+ width
, sy
+ height
,
551 fb_buffers
->alpha_mask
,
553 dx
+ width
, dy
+ height
);
556 static void mask_layer_render_to(struct vg_mask_layer
*layer
,
558 VGbitfield paint_modes
)
560 struct vg_context
*ctx
= vg_current_context();
561 const VGfloat fill_color
[4] = {1.f
, 1.f
, 1.f
, 1.f
};
562 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
563 struct pipe_surface
*surface
;
565 surface
= screen
->get_tex_surface(screen
, layer
->texture
, 0, 0, 0,
566 PIPE_BUFFER_USAGE_GPU_WRITE
);
568 cso_save_framebuffer(ctx
->cso_context
);
569 cso_save_fragment_shader(ctx
->cso_context
);
570 cso_save_viewport(ctx
->cso_context
);
573 setup_mask_fill(fill_color
);
574 setup_mask_framebuffer(surface
, layer
->width
, layer
->height
);
575 setup_mask_viewport();
577 if (paint_modes
& VG_FILL_PATH
) {
578 struct matrix
*mat
= &ctx
->state
.vg
.path_user_to_surface_matrix
;
579 path_fill(path
, mat
);
582 if (paint_modes
& VG_STROKE_PATH
){
587 /* make sure rendering has completed */
588 ctx
->pipe
->flush(ctx
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
590 cso_restore_framebuffer(ctx
->cso_context
);
591 cso_restore_fragment_shader(ctx
->cso_context
);
592 cso_restore_viewport(ctx
->cso_context
);
593 ctx
->state
.dirty
|= BLEND_DIRTY
;
595 screen
->tex_surface_release(ctx
->pipe
->screen
, &surface
);
598 void mask_render_to(struct path
*path
,
599 VGbitfield paint_modes
,
600 VGMaskOperation operation
)
602 struct vg_context
*ctx
= vg_current_context();
603 struct st_framebuffer
*fb_buffers
= ctx
->draw_buffer
;
604 struct vg_mask_layer
*temp_layer
;
607 width
= fb_buffers
->alpha_mask
->width0
;
608 height
= fb_buffers
->alpha_mask
->width0
;
610 temp_layer
= mask_layer_create(width
, height
);
612 mask_layer_render_to(temp_layer
, path
, paint_modes
);
614 mask_using_layer(temp_layer
, 0, 0, width
, height
,
617 mask_layer_destroy(temp_layer
);
620 void mask_using_layer(struct vg_mask_layer
*layer
,
621 VGMaskOperation operation
,
623 VGint width
, VGint height
)
625 mask_using_texture(layer
->texture
, operation
,
626 x
, y
, width
, height
);
629 VGint
mask_layer_width(struct vg_mask_layer
*layer
)
634 VGint
mask_layer_height(struct vg_mask_layer
*layer
)
636 return layer
->height
;
642 void mask_using_image(struct vg_image
*image
,
643 VGMaskOperation operation
,
645 VGint width
, VGint height
)
647 mask_using_texture(image
->texture
, operation
,
648 x
, y
, width
, height
);
651 void mask_fill(VGint x
, VGint y
, VGint width
, VGint height
,
654 struct vg_context
*ctx
= vg_current_context();
655 VGfloat alpha_color
[4] = {.0f
, .0f
, .0f
, value
};
656 struct pipe_surface
*surf
= alpha_mask_surface(
657 ctx
, PIPE_BUFFER_USAGE_GPU_WRITE
);
660 debug_printf("mask_fill(%d, %d, %d, %d) with rgba(%f, %f, %f, %f)\n",
662 alpha_color
[0], alpha_color
[1],
663 alpha_color
[2], alpha_color
[3]);
664 debug_printf("XXX %f === %f \n",
665 alpha_color
[3], value
);
668 surface_fill(surf
, surf
->width
, surf
->height
,
669 x
, y
, width
, height
, alpha_color
);
671 pipe_surface_reference(&surf
, NULL
);
674 VGint
mask_bind_samplers(struct pipe_sampler_state
**samplers
,
675 struct pipe_texture
**textures
)
677 struct vg_context
*ctx
= vg_current_context();
679 if (ctx
->state
.vg
.masking
) {
680 struct st_framebuffer
*fb_buffers
= ctx
->draw_buffer
;
682 samplers
[1] = &ctx
->mask
.sampler
;
683 textures
[1] = fb_buffers
->alpha_mask
;