api_masks.c revision 96c6637a1360f146bbf49ffb207ae943ecbbdf49
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#include "renderer.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
40#define DISABLE_1_1_MASKING 1
41
42void vegaMask(VGHandle mask, VGMaskOperation operation,
43              VGint x, VGint y,
44              VGint width, VGint height)
45{
46   struct vg_context *ctx = vg_current_context();
47
48   if (width <=0 || height <= 0) {
49      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
50      return;
51   }
52
53   if (operation < VG_CLEAR_MASK || operation > VG_SUBTRACT_MASK) {
54      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
55      return;
56   }
57
58
59   vg_validate_state(ctx);
60
61   if (operation == VG_CLEAR_MASK) {
62      mask_fill(x, y, width, height, 0.f);
63   } else if (operation == VG_FILL_MASK) {
64      mask_fill(x, y, width, height, 1.f);
65   } else if (vg_object_is_valid((void*)mask, VG_OBJECT_IMAGE)) {
66      struct vg_image *image = (struct vg_image *)mask;
67      mask_using_image(image, operation, x, y, width, height);
68   } else if (vg_object_is_valid((void*)mask, VG_OBJECT_MASK)) {
69#if DISABLE_1_1_MASKING
70      return;
71#else
72      struct vg_mask_layer *layer = (struct vg_mask_layer *)mask;
73      mask_using_layer(layer, operation, x, y, width, height);
74#endif
75   } else {
76      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
77   }
78}
79
80void vegaClear(VGint x, VGint y,
81               VGint width, VGint height)
82{
83   struct vg_context *ctx = vg_current_context();
84   struct st_framebuffer *stfb = ctx->draw_buffer;
85
86   if (width <= 0 || height <= 0) {
87      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
88      return;
89   }
90
91   vg_validate_state(ctx);
92#if 0
93   debug_printf("Clear [%d, %d, %d, %d] with [%f, %f, %f, %f]\n",
94                x, y, width, height,
95                ctx->state.vg.clear_color[0],
96                ctx->state.vg.clear_color[1],
97                ctx->state.vg.clear_color[2],
98                ctx->state.vg.clear_color[3]);
99#endif
100
101   /* check for a whole surface clear */
102   if (!ctx->state.vg.scissoring &&
103       (x == 0 && y == 0 && width == stfb->width && height == stfb->height)) {
104      ctx->pipe->clear(ctx->pipe, PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
105                       ctx->state.vg.clear_color, 1., 0);
106   } else if (renderer_clear_begin(ctx->renderer)) {
107      /* XXX verify coord round-off */
108      renderer_clear(ctx->renderer, x, y, width, height, ctx->state.vg.clear_color);
109      renderer_clear_end(ctx->renderer);
110   }
111}
112
113
114#ifdef OPENVG_VERSION_1_1
115
116
117void vegaRenderToMask(VGPath path,
118                      VGbitfield paintModes,
119                      VGMaskOperation operation)
120{
121   struct vg_context *ctx = vg_current_context();
122
123   if (path == VG_INVALID_HANDLE) {
124      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
125      return;
126   }
127   if (!paintModes || (paintModes&(~(VG_STROKE_PATH|VG_FILL_PATH)))) {
128      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
129      return;
130   }
131   if (operation < VG_CLEAR_MASK ||
132       operation > VG_SUBTRACT_MASK) {
133      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
134      return;
135   }
136   if (!vg_object_is_valid((void*)path, VG_OBJECT_PATH)) {
137      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
138      return;
139   }
140
141#if DISABLE_1_1_MASKING
142   return;
143#endif
144
145   vg_validate_state(ctx);
146
147   mask_render_to((struct path *)path, paintModes, operation);
148}
149
150VGMaskLayer vegaCreateMaskLayer(VGint width, VGint height)
151{
152   struct vg_context *ctx = vg_current_context();
153
154   if (width <= 0 || height <= 0 ||
155       width > vgGeti(VG_MAX_IMAGE_WIDTH) ||
156       height > vgGeti(VG_MAX_IMAGE_HEIGHT)) {
157      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
158      return VG_INVALID_HANDLE;
159   }
160
161   return (VGMaskLayer)mask_layer_create(width, height);
162}
163
164void vegaDestroyMaskLayer(VGMaskLayer maskLayer)
165{
166   struct vg_mask_layer *mask = 0;
167   struct vg_context *ctx = vg_current_context();
168
169   if (maskLayer == VG_INVALID_HANDLE) {
170      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
171      return;
172   }
173   if (!vg_object_is_valid((void*)maskLayer, VG_OBJECT_MASK)) {
174      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
175      return;
176   }
177
178   mask = (struct vg_mask_layer *)maskLayer;
179   mask_layer_destroy(mask);
180}
181
182void vegaFillMaskLayer(VGMaskLayer maskLayer,
183                       VGint x, VGint y,
184                       VGint width, VGint height,
185                       VGfloat value)
186{
187   struct vg_mask_layer *mask = 0;
188   struct vg_context *ctx = vg_current_context();
189
190   if (maskLayer == VG_INVALID_HANDLE) {
191      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
192      return;
193   }
194
195   if (value < 0 || value > 1) {
196      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
197      return;
198   }
199
200   if (width <= 0 || height <= 0) {
201      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
202      return;
203   }
204   if (x < 0 || y < 0 || (x + width) < 0 || (y + height) < 0) {
205      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
206      return;
207   }
208
209   if (!vg_object_is_valid((void*)maskLayer, VG_OBJECT_MASK)) {
210      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
211      return;
212   }
213
214   mask = (struct vg_mask_layer*)maskLayer;
215
216   if (x + width > mask_layer_width(mask) ||
217       y + height > mask_layer_height(mask)) {
218      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
219      return;
220   }
221
222#if DISABLE_1_1_MASKING
223   return;
224#endif
225   mask_layer_fill(mask, x, y, width, height, value);
226}
227
228void vegaCopyMask(VGMaskLayer maskLayer,
229                  VGint sx, VGint sy,
230                  VGint dx, VGint dy,
231                  VGint width, VGint height)
232{
233   struct vg_context *ctx = vg_current_context();
234   struct vg_mask_layer *mask = 0;
235
236   if (maskLayer == VG_INVALID_HANDLE) {
237      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
238      return;
239   }
240   if (width <= 0 || height <= 0) {
241      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
242      return;
243   }
244   if (!vg_object_is_valid((void*)maskLayer, VG_OBJECT_MASK)) {
245      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
246      return;
247   }
248
249#if DISABLE_1_1_MASKING
250   return;
251#endif
252
253   mask = (struct vg_mask_layer*)maskLayer;
254   mask_copy(mask, sx, sy, dx, dy, width, height);
255}
256
257#endif
258