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