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