api_paint.c revision 75143ef05576ee9f25ee176bc28c3c4d03705bf5
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 "vg_context.h"
30#include "paint.h"
31#include "image.h"
32#include "api.h"
33
34VGPaint vegaCreatePaint(void)
35{
36   return (VGPaint) paint_create(vg_current_context());
37}
38
39void vegaDestroyPaint(VGPaint p)
40{
41   struct vg_context *ctx = vg_current_context();
42   struct vg_paint *paint;
43
44   if (p == VG_INVALID_HANDLE) {
45      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
46      return;
47   }
48
49   paint = (struct vg_paint *)p;
50   paint_destroy(paint);
51}
52
53void vegaSetPaint(VGPaint paint, VGbitfield paintModes)
54{
55   struct vg_context *ctx = vg_current_context();
56
57   if (paint == VG_INVALID_HANDLE) {
58      /* restore the default */
59      paint = (VGPaint)ctx->default_paint;
60   } else if (!vg_object_is_valid((void*)paint, VG_OBJECT_PAINT)) {
61      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
62      return;
63   }
64
65   if (!(paintModes & ((VG_FILL_PATH|VG_STROKE_PATH)))) {
66      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
67      return;
68   }
69
70   if (paintModes & VG_FILL_PATH) {
71      ctx->state.vg.fill_paint = (struct vg_paint *)paint;
72   }
73   if (paintModes & VG_STROKE_PATH) {
74      ctx->state.vg.stroke_paint = (struct vg_paint *)paint;
75   }
76}
77
78VGPaint vegaGetPaint(VGPaintMode paintMode)
79{
80   struct vg_context *ctx = vg_current_context();
81   VGPaint paint = VG_INVALID_HANDLE;
82
83   if (paintMode < VG_STROKE_PATH || paintMode > VG_FILL_PATH) {
84      vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
85      return VG_INVALID_HANDLE;
86   }
87
88   if (paintMode == VG_FILL_PATH)
89      paint = (VGPaint)ctx->state.vg.fill_paint;
90   else if (paintMode == VG_STROKE_PATH)
91      paint = (VGPaint)ctx->state.vg.stroke_paint;
92
93   if (paint == (VGPaint)ctx->default_paint)
94      paint = VG_INVALID_HANDLE;
95
96   return paint;
97}
98
99void vegaSetColor(VGPaint paint, VGuint rgba)
100{
101   struct vg_context *ctx = vg_current_context();
102
103   if (paint == VG_INVALID_HANDLE) {
104      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
105      return;
106   }
107
108   if (!vg_object_is_valid((void*)paint, VG_OBJECT_PAINT)) {
109      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
110      return;
111   }
112   {
113      struct vg_paint *p = (struct vg_paint *)paint;
114      paint_set_colori(p, rgba);
115   }
116}
117
118VGuint vegaGetColor(VGPaint paint)
119{
120   struct vg_context *ctx = vg_current_context();
121   struct vg_paint *p;
122   VGuint rgba = 0;
123
124   if (paint == VG_INVALID_HANDLE) {
125      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
126      return rgba;
127   }
128
129   if (!vg_object_is_valid((void*)paint, VG_OBJECT_PAINT)) {
130      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
131      return rgba;
132   }
133   p = (struct vg_paint *)paint;
134
135   return paint_colori(p);
136}
137
138void vegaPaintPattern(VGPaint paint, VGImage pattern)
139{
140   struct vg_context *ctx = vg_current_context();
141
142   if (paint == VG_INVALID_HANDLE ||
143       !vg_context_is_object_valid(ctx, VG_OBJECT_PAINT, (void *)paint)) {
144      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
145      return;
146   }
147
148   if (pattern == VG_INVALID_HANDLE) {
149      paint_set_type((struct vg_paint*)paint, VG_PAINT_TYPE_COLOR);
150      return;
151   }
152
153   if (!vg_context_is_object_valid(ctx, VG_OBJECT_IMAGE, (void *)pattern)) {
154      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
155      return;
156   }
157
158
159   if (!vg_object_is_valid((void*)paint, VG_OBJECT_PAINT) ||
160       !vg_object_is_valid((void*)pattern, VG_OBJECT_IMAGE)) {
161      vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
162      return;
163   }
164   paint_set_pattern((struct vg_paint*)paint,
165                     (struct vg_image*)pattern);
166}
167
168