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"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/u_inlines.h"
38 #include "util/u_format.h"
39 #include "util/u_memory.h"
40 #include "util/u_sampler.h"
42 struct vg_mask_layer
{
43 struct vg_object base
;
48 struct pipe_sampler_view
*sampler_view
;
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
,
57 stfb
->alpha_mask_view
->texture
,
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
;
118 VGfloat temp
[VEGA_MAX_IMAGE_WIDTH
][4];
119 VGfloat
*df
= (VGfloat
*)temp
;
120 VGint y
= (stfb
->height
- sy
) - 1, yStep
= -1;
122 VGubyte
*dst
= (VGubyte
*)data
;
123 VGint xoffset
= 0, yoffset
= 0;
125 /* make sure rendering has completed */
126 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
129 xoffset
*= _vega_size_for_format(dataFormat
);
137 y
= (stfb
->height
- sy
) - 1;
138 yoffset
*= dataStride
;
142 struct pipe_surface
*surf
;
144 surf
= screen
->get_tex_surface(screen
, strb
->texture
, 0, 0, 0,
145 PIPE_BIND_TRANSFER_READ
);
147 /* Do a row at a time to flip image data vertically */
148 for (i
= 0; i
< height
; i
++) {
150 debug_printf("%d-%d == %d\n", sy
, height
, y
);
152 pipe_get_tile_rgba(surf
, sx
, y
, width
, 1, df
);
154 _vega_pack_rgba_span_float(ctx
, width
, temp
, dataFormat
,
155 dst
+ yoffset
+ xoffset
);
159 pipe_surface_reference(&surf
, NULL
);
163 void save_alpha_to_file(const char *filename
)
165 struct vg_context
*ctx
= vg_current_context();
166 struct st_framebuffer
*stfb
= ctx
->draw_buffer
;
170 data
= malloc(sizeof(int) * stfb
->width
* stfb
->height
);
171 read_alpha_mask(data
, stfb
->width
* sizeof(int),
173 0, 0, stfb
->width
, stfb
->height
);
174 fprintf(stderr
, "/*---------- start */\n");
175 fprintf(stderr
, "const int image_width = %d;\n",
177 fprintf(stderr
, "const int image_height = %d;\n",
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
];
185 argb
|= ((rgba
& 0xff) << 24);
186 fprintf(stderr
, "0x%x, ", argb
);
188 fprintf(stderr
, "\n");
190 fprintf(stderr
, "};\n");
191 fprintf(stderr
, "/*---------- end */\n");
195 /* setup mask shader */
196 static void *setup_mask_operation(VGMaskOperation operation
)
198 struct vg_context
*ctx
= vg_current_context();
202 case VG_UNION_MASK
: {
203 if (!ctx
->mask
.union_fs
) {
204 ctx
->mask
.union_fs
= shader_create_from_text(ctx
->pipe
,
207 PIPE_SHADER_FRAGMENT
);
209 shader
= ctx
->mask
.union_fs
->driver
;
212 case VG_INTERSECT_MASK
: {
213 if (!ctx
->mask
.intersect_fs
) {
214 ctx
->mask
.intersect_fs
= shader_create_from_text(ctx
->pipe
,
217 PIPE_SHADER_FRAGMENT
);
219 shader
= ctx
->mask
.intersect_fs
->driver
;
222 case VG_SUBTRACT_MASK
: {
223 if (!ctx
->mask
.subtract_fs
) {
224 ctx
->mask
.subtract_fs
= shader_create_from_text(ctx
->pipe
,
227 PIPE_SHADER_FRAGMENT
);
229 shader
= ctx
->mask
.subtract_fs
->driver
;
233 if (!ctx
->mask
.set_fs
) {
234 ctx
->mask
.set_fs
= shader_create_from_text(ctx
->pipe
,
237 PIPE_SHADER_FRAGMENT
);
239 shader
= ctx
->mask
.set_fs
->driver
;
250 static void mask_resource_fill(struct pipe_resource
*dst
,
251 int x
, int y
, int width
, int height
,
254 struct vg_context
*ctx
= vg_current_context();
255 VGfloat fs_consts
[12] = {
256 0.0f
, 0.0f
, 0.0f
, 0.0f
, /* not used */
257 0.0f
, 0.0f
, 0.0f
, 0.0f
, /* not used */
258 0.0f
, 0.0f
, 0.0f
, coverage
/* color */
271 fs
= shaders_cache_fill(ctx
->sc
, VEGA_SOLID_FILL_SHADER
);
273 if (renderer_filter_begin(ctx
->renderer
, dst
, VG_FALSE
, ~0,
274 NULL
, NULL
, 0, fs
, (const void *) fs_consts
, sizeof(fs_consts
))) {
275 renderer_filter(ctx
->renderer
, x
, y
, width
, height
, 0, 0, 0, 0);
276 renderer_filter_end(ctx
->renderer
);
280 save_alpha_to_file(0);
285 static void mask_using_texture(struct pipe_sampler_view
*sampler_view
,
287 VGMaskOperation operation
,
289 VGint width
, VGint height
)
291 struct vg_context
*ctx
= vg_current_context();
292 struct pipe_resource
*dst
= ctx
->draw_buffer
->alpha_mask_view
->texture
;
293 struct pipe_resource
*texture
= sampler_view
->texture
;
294 const struct pipe_sampler_state
*samplers
[2];
295 struct pipe_sampler_view
*views
[2];
296 struct pipe_sampler_state sampler
;
297 VGint offsets
[4], loc
[4];
298 const VGfloat ones
[4] = {1.f
, 1.f
, 1.f
, 1.f
};
301 if (!intersect_rectangles(dst
->width0
, dst
->height0
,
302 texture
->width0
, texture
->height0
,
307 debug_printf("Offset = [%d, %d, %d, %d]\n", offsets
[0],
308 offsets
[1], offsets
[2], offsets
[3]);
309 debug_printf("Locati = [%d, %d, %d, %d]\n", loc
[0],
310 loc
[1], loc
[2], loc
[3]);
313 sampler
= ctx
->mask
.sampler
;
314 sampler
.normalized_coords
= 1;
315 samplers
[0] = &sampler
;
316 views
[0] = sampler_view
;
318 /* prepare our blend surface */
319 samplers
[1] = &ctx
->mask
.sampler
;
320 views
[1] = vg_prepare_blend_surface_from_mask(ctx
);
322 fs
= setup_mask_operation(operation
);
324 if (renderer_filter_begin(ctx
->renderer
, dst
, VG_FALSE
,
325 ~0, samplers
, views
, 2, fs
, (const void *) ones
, sizeof(ones
))) {
326 /* layer should be flipped when used as a texture */
328 offsets
[1] += offsets
[3];
329 offsets
[3] = -offsets
[3];
331 renderer_filter(ctx
->renderer
,
332 loc
[0], loc
[1], loc
[2], loc
[3],
333 offsets
[0], offsets
[1], offsets
[2], offsets
[3]);
334 renderer_filter_end(ctx
->renderer
);
339 #ifdef OPENVG_VERSION_1_1
341 struct vg_mask_layer
* mask_layer_create(VGint width
, VGint height
)
343 struct vg_context
*ctx
= vg_current_context();
344 struct vg_mask_layer
*mask
= 0;
346 mask
= CALLOC_STRUCT(vg_mask_layer
);
347 vg_init_object(&mask
->base
, ctx
, VG_OBJECT_MASK
);
349 mask
->height
= height
;
352 struct pipe_resource pt
;
353 struct pipe_context
*pipe
= ctx
->pipe
;
354 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
355 struct pipe_sampler_view view_templ
;
356 struct pipe_sampler_view
*view
= NULL
;
357 struct pipe_resource
*texture
;
359 memset(&pt
, 0, sizeof(pt
));
360 pt
.target
= PIPE_TEXTURE_2D
;
361 pt
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
366 pt
.bind
= PIPE_BIND_SAMPLER_VIEW
;
368 texture
= screen
->resource_create(screen
, &pt
);
371 u_sampler_view_default_template(&view_templ
, texture
, texture
->format
);
372 view
= pipe
->create_sampler_view(pipe
, texture
, &view_templ
);
374 pipe_resource_reference(&texture
, NULL
);
375 mask
->sampler_view
= view
;
378 vg_context_add_object(ctx
, VG_OBJECT_MASK
, mask
);
383 void mask_layer_destroy(struct vg_mask_layer
*layer
)
385 struct vg_context
*ctx
= vg_current_context();
387 vg_context_remove_object(ctx
, VG_OBJECT_MASK
, layer
);
388 pipe_sampler_view_reference(&layer
->sampler_view
, NULL
);
392 void mask_layer_fill(struct vg_mask_layer
*layer
,
394 VGint width
, VGint height
,
397 VGfloat alpha_color
[4] = {0, 0, 0, 0};
399 alpha_color
[3] = value
;
401 mask_resource_fill(layer
->sampler_view
->texture
,
402 x
, y
, width
, height
, value
);
405 void mask_copy(struct vg_mask_layer
*layer
,
408 VGint width
, VGint height
)
410 struct vg_context
*ctx
= vg_current_context();
411 struct pipe_sampler_view
*src
= ctx
->draw_buffer
->alpha_mask_view
;
412 struct pipe_surface
*surf
;
414 /* get the destination surface */
415 surf
= ctx
->pipe
->screen
->get_tex_surface(ctx
->pipe
->screen
,
416 layer
->sampler_view
->texture
, 0, 0, 0, PIPE_BIND_RENDER_TARGET
);
417 if (surf
&& renderer_copy_begin(ctx
->renderer
, surf
, VG_FALSE
, src
)) {
418 /* layer should be flipped when used as a texture */
422 renderer_copy(ctx
->renderer
,
423 dx
, dy
, width
, height
,
424 sx
, sy
, width
, height
);
425 renderer_copy_end(ctx
->renderer
);
428 pipe_surface_reference(&surf
, NULL
);
431 static void mask_layer_render_to(struct vg_mask_layer
*layer
,
433 VGbitfield paint_modes
)
435 struct vg_context
*ctx
= vg_current_context();
436 struct matrix
*mat
= &ctx
->state
.vg
.path_user_to_surface_matrix
;
437 struct pipe_surface
*surf
;
439 surf
= alpha_mask_surface(ctx
, PIPE_BIND_RENDER_TARGET
);
441 renderer_validate_for_mask_rendering(ctx
->renderer
, surf
);
443 if (paint_modes
& VG_FILL_PATH
) {
444 path_fill(path
, mat
);
447 if (paint_modes
& VG_STROKE_PATH
){
448 path_stroke(path
, mat
);
452 void mask_render_to(struct path
*path
,
453 VGbitfield paint_modes
,
454 VGMaskOperation operation
)
456 struct vg_context
*ctx
= vg_current_context();
457 struct st_framebuffer
*fb_buffers
= ctx
->draw_buffer
;
458 struct vg_mask_layer
*temp_layer
;
461 width
= fb_buffers
->alpha_mask_view
->texture
->width0
;
462 height
= fb_buffers
->alpha_mask_view
->texture
->height0
;
464 temp_layer
= mask_layer_create(width
, height
);
465 mask_layer_fill(temp_layer
, 0, 0, width
, height
, 0.0f
);
467 mask_layer_render_to(temp_layer
, path
, paint_modes
);
469 mask_using_layer(temp_layer
, operation
, 0, 0, width
, height
);
471 mask_layer_destroy(temp_layer
);
474 void mask_using_layer(struct vg_mask_layer
*layer
,
475 VGMaskOperation operation
,
477 VGint width
, VGint height
)
479 mask_using_texture(layer
->sampler_view
, VG_TRUE
, operation
,
480 x
, y
, width
, height
);
483 VGint
mask_layer_width(struct vg_mask_layer
*layer
)
488 VGint
mask_layer_height(struct vg_mask_layer
*layer
)
490 return layer
->height
;
496 void mask_using_image(struct vg_image
*image
,
497 VGMaskOperation operation
,
499 VGint width
, VGint height
)
501 mask_using_texture(image
->sampler_view
, VG_FALSE
, operation
,
502 x
, y
, width
, height
);
505 void mask_fill(VGint x
, VGint y
, VGint width
, VGint height
,
508 struct vg_context
*ctx
= vg_current_context();
511 debug_printf("mask_fill(%d, %d, %d, %d) with rgba(%f, %f, %f, %f)\n",
513 0.0f
, 0.0f
, 0.0f
, value
);
516 mask_resource_fill(ctx
->draw_buffer
->alpha_mask_view
->texture
,
517 x
, y
, width
, height
, value
);
520 VGint
mask_bind_samplers(struct pipe_sampler_state
**samplers
,
521 struct pipe_sampler_view
**sampler_views
)
523 struct vg_context
*ctx
= vg_current_context();
525 if (ctx
->state
.vg
.masking
) {
526 struct st_framebuffer
*fb_buffers
= ctx
->draw_buffer
;
528 samplers
[1] = &ctx
->mask
.sampler
;
529 sampler_views
[1] = fb_buffers
->alpha_mask_view
;