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