api_masks.c revision 75143ef05576ee9f25ee176bc28c3c4d03705bf5
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 "VG/openvg.h" 28 29#include "mask.h" 30#include "renderer.h" 31#include "api.h" 32 33#include "vg_context.h" 34#include "pipe/p_context.h" 35#include "util/u_inlines.h" 36 37#include "util/u_pack_color.h" 38#include "util/u_draw_quad.h" 39#include "util/u_memory.h" 40 41#define DISABLE_1_1_MASKING 1 42 43/** 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. 47 */ 48static void 49draw_clear_quad(struct vg_context *st, 50 float x0, float y0, float x1, float y1, float z, 51 const VGfloat color[4]) 52{ 53 struct pipe_context *pipe = st->pipe; 54 struct pipe_resource *buf; 55 VGuint i; 56 57 /* positions */ 58 st->clear.vertices[0][0][0] = x0; 59 st->clear.vertices[0][0][1] = y0; 60 61 st->clear.vertices[1][0][0] = x1; 62 st->clear.vertices[1][0][1] = y0; 63 64 st->clear.vertices[2][0][0] = x1; 65 st->clear.vertices[2][0][1] = y1; 66 67 st->clear.vertices[3][0][0] = x0; 68 st->clear.vertices[3][0][1] = y1; 69 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]; 78 } 79 80 81 /* put vertex data into vbuf */ 82 buf = pipe_user_buffer_create(pipe->screen, 83 st->clear.vertices, 84 sizeof(st->clear.vertices), 85 PIPE_BIND_VERTEX_BUFFER); 86 87 88 /* draw */ 89 if (buf) { 90 cso_set_vertex_elements(st->cso_context, 2, st->velems); 91 92 util_draw_vertex_buffer(pipe, buf, 0, 93 PIPE_PRIM_TRIANGLE_FAN, 94 4, /* verts */ 95 2); /* attribs/vert */ 96 97 pipe_resource_reference(&buf, NULL); 98 } 99} 100 101/** 102 * Do vgClear by drawing a quadrilateral. 103 */ 104static void 105clear_with_quad(struct vg_context *st, float x0, float y0, 106 float width, float height, const VGfloat clear_color[4]) 107{ 108 VGfloat x1, y1; 109 110 vg_validate_state(st); 111 112 x1 = x0 + width; 113 y1 = y0 + height; 114 115 /* 116 printf("%s %f,%f %f,%f\n", __FUNCTION__, 117 x0, y0, 118 x1, y1); 119 */ 120 121 cso_save_blend(st->cso_context); 122 cso_save_rasterizer(st->cso_context); 123 cso_save_fragment_shader(st->cso_context); 124 cso_save_vertex_shader(st->cso_context); 125 126 /* blend state: RGBA masking */ 127 { 128 struct pipe_blend_state blend; 129 memset(&blend, 0, sizeof(blend)); 130 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE; 131 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE; 132 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO; 133 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO; 134 blend.rt[0].colormask = PIPE_MASK_RGBA; 135 cso_set_blend(st->cso_context, &blend); 136 } 137 138 cso_set_rasterizer(st->cso_context, &st->clear.raster); 139 140 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs); 141 cso_set_vertex_shader_handle(st->cso_context, vg_clear_vs(st)); 142 143 /* draw quad matching scissor rect (XXX verify coord round-off) */ 144 draw_clear_quad(st, x0, y0, x1, y1, 0, clear_color); 145 146 /* Restore pipe state */ 147 cso_restore_blend(st->cso_context); 148 cso_restore_rasterizer(st->cso_context); 149 cso_restore_fragment_shader(st->cso_context); 150 cso_restore_vertex_shader(st->cso_context); 151} 152 153 154void vegaMask(VGHandle mask, VGMaskOperation operation, 155 VGint x, VGint y, 156 VGint width, VGint height) 157{ 158 struct vg_context *ctx = vg_current_context(); 159 160 if (width <=0 || height <= 0) { 161 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 162 return; 163 } 164 165 if (operation < VG_CLEAR_MASK || operation > VG_SUBTRACT_MASK) { 166 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 167 return; 168 } 169 170 171 vg_validate_state(ctx); 172 173 if (operation == VG_CLEAR_MASK) { 174 mask_fill(x, y, width, height, 0.f); 175 } else if (operation == VG_FILL_MASK) { 176 mask_fill(x, y, width, height, 1.f); 177 } else if (vg_object_is_valid((void*)mask, VG_OBJECT_IMAGE)) { 178 struct vg_image *image = (struct vg_image *)mask; 179 mask_using_image(image, operation, x, y, width, height); 180 } else if (vg_object_is_valid((void*)mask, VG_OBJECT_MASK)) { 181#if DISABLE_1_1_MASKING 182 return; 183#else 184 struct vg_mask_layer *layer = (struct vg_mask_layer *)mask; 185 mask_using_layer(layer, operation, x, y, width, height); 186#endif 187 } else { 188 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 189 } 190} 191 192void vegaClear(VGint x, VGint y, 193 VGint width, VGint height) 194{ 195 struct vg_context *ctx = vg_current_context(); 196 struct pipe_framebuffer_state *fb; 197 198 if (width <= 0 || height <= 0) { 199 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 200 return; 201 } 202 203 vg_validate_state(ctx); 204#if 0 205 debug_printf("Clear [%d, %d, %d, %d] with [%f, %f, %f, %f]\n", 206 x, y, width, height, 207 ctx->state.vg.clear_color[0], 208 ctx->state.vg.clear_color[1], 209 ctx->state.vg.clear_color[2], 210 ctx->state.vg.clear_color[3]); 211#endif 212 213 fb = &ctx->state.g3d.fb; 214 /* check for a whole surface clear */ 215 if (!ctx->state.vg.scissoring && 216 (x == 0 && y == 0 && width == fb->width && height == fb->height)) { 217 ctx->pipe->clear(ctx->pipe, PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL, 218 ctx->state.vg.clear_color, 1., 0); 219 } else { 220 clear_with_quad(ctx, x, y, width, height, ctx->state.vg.clear_color); 221 } 222} 223 224 225#ifdef OPENVG_VERSION_1_1 226 227 228void vegaRenderToMask(VGPath path, 229 VGbitfield paintModes, 230 VGMaskOperation operation) 231{ 232 struct vg_context *ctx = vg_current_context(); 233 234 if (path == VG_INVALID_HANDLE) { 235 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 236 return; 237 } 238 if (!paintModes || (paintModes&(~(VG_STROKE_PATH|VG_FILL_PATH)))) { 239 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 240 return; 241 } 242 if (operation < VG_CLEAR_MASK || 243 operation > VG_SUBTRACT_MASK) { 244 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 245 return; 246 } 247 if (!vg_object_is_valid((void*)path, VG_OBJECT_PATH)) { 248 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 249 return; 250 } 251 252#if DISABLE_1_1_MASKING 253 return; 254#endif 255 256 vg_validate_state(ctx); 257 258 mask_render_to((struct path *)path, paintModes, operation); 259} 260 261VGMaskLayer vegaCreateMaskLayer(VGint width, VGint height) 262{ 263 struct vg_context *ctx = vg_current_context(); 264 265 if (width <= 0 || height <= 0 || 266 width > vgGeti(VG_MAX_IMAGE_WIDTH) || 267 height > vgGeti(VG_MAX_IMAGE_HEIGHT)) { 268 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 269 return VG_INVALID_HANDLE; 270 } 271 272 return (VGMaskLayer)mask_layer_create(width, height); 273} 274 275void vegaDestroyMaskLayer(VGMaskLayer maskLayer) 276{ 277 struct vg_mask_layer *mask = 0; 278 struct vg_context *ctx = vg_current_context(); 279 280 if (maskLayer == VG_INVALID_HANDLE) { 281 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 282 return; 283 } 284 if (!vg_object_is_valid((void*)maskLayer, VG_OBJECT_MASK)) { 285 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 286 return; 287 } 288 289 mask = (struct vg_mask_layer *)maskLayer; 290 mask_layer_destroy(mask); 291} 292 293void vegaFillMaskLayer(VGMaskLayer maskLayer, 294 VGint x, VGint y, 295 VGint width, VGint height, 296 VGfloat value) 297{ 298 struct vg_mask_layer *mask = 0; 299 struct vg_context *ctx = vg_current_context(); 300 301 if (maskLayer == VG_INVALID_HANDLE) { 302 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 303 return; 304 } 305 306 if (value < 0 || value > 1) { 307 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 308 return; 309 } 310 311 if (width <= 0 || height <= 0) { 312 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 313 return; 314 } 315 if (x < 0 || y < 0 || (x + width) < 0 || (y + height) < 0) { 316 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 317 return; 318 } 319 320 if (!vg_object_is_valid((void*)maskLayer, VG_OBJECT_MASK)) { 321 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 322 return; 323 } 324 325 mask = (struct vg_mask_layer*)maskLayer; 326 327 if (x + width > mask_layer_width(mask) || 328 y + height > mask_layer_height(mask)) { 329 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 330 return; 331 } 332 333#if DISABLE_1_1_MASKING 334 return; 335#endif 336 mask_layer_fill(mask, x, y, width, height, value); 337} 338 339void vegaCopyMask(VGMaskLayer maskLayer, 340 VGint sx, VGint sy, 341 VGint dx, VGint dy, 342 VGint width, VGint height) 343{ 344 struct vg_context *ctx = vg_current_context(); 345 struct vg_mask_layer *mask = 0; 346 347 if (maskLayer == VG_INVALID_HANDLE) { 348 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 349 return; 350 } 351 if (width <= 0 || height <= 0) { 352 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR); 353 return; 354 } 355 if (!vg_object_is_valid((void*)maskLayer, VG_OBJECT_MASK)) { 356 vg_set_error(ctx, VG_BAD_HANDLE_ERROR); 357 return; 358 } 359 360#if DISABLE_1_1_MASKING 361 return; 362#endif 363 364 mask = (struct vg_mask_layer*)maskLayer; 365 mask_copy(mask, sx, sy, dx, dy, width, height); 366} 367 368#endif 369