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