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