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 **************************************************************************/
27 #include "VG/openvg.h"
32 #include "vg_context.h"
33 #include "pipe/p_context.h"
34 #include "util/u_inlines.h"
36 #include "util/u_pack_color.h"
37 #include "util/u_draw_quad.h"
38 #include "util/u_memory.h"
41 #define DISABLE_1_1_MASKING 1
44 * Draw a screen-aligned quadrilateral.
45 * Coords are window coords with y=0=bottom. These coords will be transformed
46 * by the vertex shader and viewport transform.
49 draw_clear_quad(struct vg_context
*st
,
50 float x0
, float y0
, float x1
, float y1
, float z
,
51 const VGfloat color
[4])
53 struct pipe_context
*pipe
= st
->pipe
;
54 struct pipe_buffer
*buf
;
58 st
->clear
.vertices
[0][0][0] = x0
;
59 st
->clear
.vertices
[0][0][1] = y0
;
61 st
->clear
.vertices
[1][0][0] = x1
;
62 st
->clear
.vertices
[1][0][1] = y0
;
64 st
->clear
.vertices
[2][0][0] = x1
;
65 st
->clear
.vertices
[2][0][1] = y1
;
67 st
->clear
.vertices
[3][0][0] = x0
;
68 st
->clear
.vertices
[3][0][1] = y1
;
70 /* same for all verts: */
71 for (i
= 0; i
< 4; i
++) {
72 st
->clear
.vertices
[i
][0][2] = z
;
73 st
->clear
.vertices
[i
][0][3] = 1.0;
74 st
->clear
.vertices
[i
][1][0] = color
[0];
75 st
->clear
.vertices
[i
][1][1] = color
[1];
76 st
->clear
.vertices
[i
][1][2] = color
[2];
77 st
->clear
.vertices
[i
][1][3] = color
[3];
81 /* put vertex data into vbuf */
82 buf
= pipe_user_buffer_create(pipe
->screen
,
84 sizeof(st
->clear
.vertices
));
89 cso_set_vertex_elements(st
->cso_context
, 2, st
->velems
);
91 util_draw_vertex_buffer(pipe
, buf
, 0,
92 PIPE_PRIM_TRIANGLE_FAN
,
94 2); /* attribs/vert */
96 pipe_buffer_reference(&buf
, NULL
);
101 * Do vgClear by drawing a quadrilateral.
104 clear_with_quad(struct vg_context
*st
, float x0
, float y0
,
105 float width
, float height
, const VGfloat clear_color
[4])
109 vg_validate_state(st
);
115 printf("%s %f,%f %f,%f\n", __FUNCTION__,
120 if (st
->pipe
->screen
&& st
->pipe
->screen
->update_buffer
)
121 st
->pipe
->screen
->update_buffer( st
->pipe
->screen
,
124 cso_save_blend(st
->cso_context
);
125 cso_save_rasterizer(st
->cso_context
);
126 cso_save_fragment_shader(st
->cso_context
);
127 cso_save_vertex_shader(st
->cso_context
);
129 /* blend state: RGBA masking */
131 struct pipe_blend_state blend
;
132 memset(&blend
, 0, sizeof(blend
));
133 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
134 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
135 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
136 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
137 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
138 cso_set_blend(st
->cso_context
, &blend
);
141 cso_set_rasterizer(st
->cso_context
, &st
->clear
.raster
);
143 cso_set_fragment_shader_handle(st
->cso_context
, st
->clear
.fs
);
144 cso_set_vertex_shader_handle(st
->cso_context
, vg_clear_vs(st
));
146 /* draw quad matching scissor rect (XXX verify coord round-off) */
147 draw_clear_quad(st
, x0
, y0
, x1
, y1
, 0, clear_color
);
149 /* Restore pipe state */
150 cso_restore_blend(st
->cso_context
);
151 cso_restore_rasterizer(st
->cso_context
);
152 cso_restore_fragment_shader(st
->cso_context
);
153 cso_restore_vertex_shader(st
->cso_context
);
157 void vgMask(VGHandle mask
, VGMaskOperation operation
,
159 VGint width
, VGint height
)
161 struct vg_context
*ctx
= vg_current_context();
163 if (width
<=0 || height
<= 0) {
164 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
168 if (operation
< VG_CLEAR_MASK
|| operation
> VG_SUBTRACT_MASK
) {
169 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
174 vg_validate_state(ctx
);
176 if (operation
== VG_CLEAR_MASK
) {
177 mask_fill(x
, y
, width
, height
, 0.f
);
178 } else if (operation
== VG_FILL_MASK
) {
179 mask_fill(x
, y
, width
, height
, 1.f
);
180 } else if (vg_object_is_valid((void*)mask
, VG_OBJECT_IMAGE
)) {
181 struct vg_image
*image
= (struct vg_image
*)mask
;
182 mask_using_image(image
, operation
, x
, y
, width
, height
);
183 } else if (vg_object_is_valid((void*)mask
, VG_OBJECT_MASK
)) {
184 #if DISABLE_1_1_MASKING
187 struct vg_mask_layer
*layer
= (struct vg_mask_layer
*)mask
;
188 mask_using_layer(layer
, operation
, x
, y
, width
, height
);
191 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
195 void vgClear(VGint x
, VGint y
,
196 VGint width
, VGint height
)
198 struct vg_context
*ctx
= vg_current_context();
199 struct pipe_framebuffer_state
*fb
;
201 if (width
<= 0 || height
<= 0) {
202 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
206 vg_validate_state(ctx
);
208 debug_printf("Clear [%d, %d, %d, %d] with [%f, %f, %f, %f]\n",
210 ctx
->state
.vg
.clear_color
[0],
211 ctx
->state
.vg
.clear_color
[1],
212 ctx
->state
.vg
.clear_color
[2],
213 ctx
->state
.vg
.clear_color
[3]);
216 fb
= &ctx
->state
.g3d
.fb
;
217 /* check for a whole surface clear */
218 if (!ctx
->state
.vg
.scissoring
&&
219 (x
== 0 && y
== 0 && width
== fb
->width
&& height
== fb
->height
)) {
220 ctx
->pipe
->clear(ctx
->pipe
, PIPE_CLEAR_COLOR
| PIPE_CLEAR_DEPTHSTENCIL
,
221 ctx
->state
.vg
.clear_color
, 1., 0);
223 clear_with_quad(ctx
, x
, y
, width
, height
, ctx
->state
.vg
.clear_color
);
228 #ifdef OPENVG_VERSION_1_1
231 void vgRenderToMask(VGPath path
,
232 VGbitfield paintModes
,
233 VGMaskOperation operation
)
235 struct vg_context
*ctx
= vg_current_context();
237 if (path
== VG_INVALID_HANDLE
) {
238 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
241 if (!paintModes
|| (paintModes
&(~(VG_STROKE_PATH
|VG_FILL_PATH
)))) {
242 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
245 if (operation
< VG_CLEAR_MASK
||
246 operation
> VG_SUBTRACT_MASK
) {
247 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
250 if (!vg_object_is_valid((void*)path
, VG_OBJECT_PATH
)) {
251 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
255 #if DISABLE_1_1_MASKING
259 vg_validate_state(ctx
);
261 mask_render_to((struct path
*)path
, paintModes
, operation
);
264 VGMaskLayer
vgCreateMaskLayer(VGint width
, VGint height
)
266 struct vg_context
*ctx
= vg_current_context();
268 if (width
<= 0 || height
<= 0 ||
269 width
> vgGeti(VG_MAX_IMAGE_WIDTH
) ||
270 height
> vgGeti(VG_MAX_IMAGE_HEIGHT
)) {
271 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
272 return VG_INVALID_HANDLE
;
275 return (VGMaskLayer
)mask_layer_create(width
, height
);
278 void vgDestroyMaskLayer(VGMaskLayer maskLayer
)
280 struct vg_mask_layer
*mask
= 0;
281 struct vg_context
*ctx
= vg_current_context();
283 if (maskLayer
== VG_INVALID_HANDLE
) {
284 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
287 if (!vg_object_is_valid((void*)maskLayer
, VG_OBJECT_MASK
)) {
288 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
292 mask
= (struct vg_mask_layer
*)maskLayer
;
293 mask_layer_destroy(mask
);
296 void vgFillMaskLayer(VGMaskLayer maskLayer
,
298 VGint width
, VGint height
,
301 struct vg_mask_layer
*mask
= 0;
302 struct vg_context
*ctx
= vg_current_context();
304 if (maskLayer
== VG_INVALID_HANDLE
) {
305 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
309 if (value
< 0 || value
> 1) {
310 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
314 if (width
<= 0 || height
<= 0) {
315 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
318 if (x
< 0 || y
< 0 || (x
+ width
) < 0 || (y
+ height
) < 0) {
319 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
323 if (!vg_object_is_valid((void*)maskLayer
, VG_OBJECT_MASK
)) {
324 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
328 mask
= (struct vg_mask_layer
*)maskLayer
;
330 if (x
+ width
> mask_layer_width(mask
) ||
331 y
+ height
> mask_layer_height(mask
)) {
332 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
336 #if DISABLE_1_1_MASKING
339 mask_layer_fill(mask
, x
, y
, width
, height
, value
);
342 void vgCopyMask(VGMaskLayer maskLayer
,
345 VGint width
, VGint height
)
347 struct vg_context
*ctx
= vg_current_context();
348 struct vg_mask_layer
*mask
= 0;
350 if (maskLayer
== VG_INVALID_HANDLE
) {
351 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
354 if (width
<= 0 || height
<= 0) {
355 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
358 if (!vg_object_is_valid((void*)maskLayer
, VG_OBJECT_MASK
)) {
359 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
363 #if DISABLE_1_1_MASKING
367 mask
= (struct vg_mask_layer
*)maskLayer
;
368 mask_copy(mask
, sx
, sy
, dx
, dy
, width
, height
);