1 /**************************************************************************
2 *
3 * Copyright 2010 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
28 /**
29 * Convert opaque VG object handles into pointers and vice versa.
30 * XXX This is not yet 64-bit safe! All VG handles are 32 bits in size.
31 */
32
33
34 #ifndef HANDLE_H
35 #define HANDLE_H
36
37 #include "pipe/p_compiler.h"
38 #include "util/u_hash_table.h"
39 #include "util/u_pointer.h"
40
41 #include "VG/openvg.h"
42 #include "vg_context.h"
43
44
45 extern struct util_hash_table *handle_hash;
46
47
48 struct vg_mask_layer;
49 struct vg_font;
50 struct vg_image;
51 struct vg_paint;
52 struct path;
53
54
55 extern void
56 init_handles(void);
57
58
59 extern void
60 free_handles(void);
61
62
63 extern VGHandle
64 create_handle(void *object);
65
66
67 extern void
68 destroy_handle(VGHandle h);
69
70
71 static INLINE VGHandle
object_to_handle(struct vg_object * obj)72 object_to_handle(struct vg_object *obj)
73 {
74 return obj ? obj->handle : VG_INVALID_HANDLE;
75 }
76
77
78 static INLINE VGHandle
image_to_handle(struct vg_image * img)79 image_to_handle(struct vg_image *img)
80 {
81 /* vg_image is derived from vg_object */
82 return object_to_handle((struct vg_object *) img);
83 }
84
85
86 static INLINE VGHandle
masklayer_to_handle(struct vg_mask_layer * mask)87 masklayer_to_handle(struct vg_mask_layer *mask)
88 {
89 /* vg_object is derived from vg_object */
90 return object_to_handle((struct vg_object *) mask);
91 }
92
93
94 static INLINE VGHandle
font_to_handle(struct vg_font * font)95 font_to_handle(struct vg_font *font)
96 {
97 return object_to_handle((struct vg_object *) font);
98 }
99
100
101 static INLINE VGHandle
paint_to_handle(struct vg_paint * paint)102 paint_to_handle(struct vg_paint *paint)
103 {
104 return object_to_handle((struct vg_object *) paint);
105 }
106
107
108 static INLINE VGHandle
path_to_handle(struct path * path)109 path_to_handle(struct path *path)
110 {
111 return object_to_handle((struct vg_object *) path);
112 }
113
114
115 static INLINE void *
handle_to_pointer(VGHandle h)116 handle_to_pointer(VGHandle h)
117 {
118 void *v = util_hash_table_get(handle_hash, intptr_to_pointer(h));
119 #ifdef DEBUG
120 if (v) {
121 struct vg_object *obj = (struct vg_object *) v;
122 assert(obj->handle == h);
123 }
124 #endif
125 return v;
126 }
127
128
129 static INLINE struct vg_font *
handle_to_font(VGHandle h)130 handle_to_font(VGHandle h)
131 {
132 return (struct vg_font *) handle_to_pointer(h);
133 }
134
135
136 static INLINE struct vg_image *
handle_to_image(VGHandle h)137 handle_to_image(VGHandle h)
138 {
139 return (struct vg_image *) handle_to_pointer(h);
140 }
141
142
143 static INLINE struct vg_mask_layer *
handle_to_masklayer(VGHandle h)144 handle_to_masklayer(VGHandle h)
145 {
146 return (struct vg_mask_layer *) handle_to_pointer(h);
147 }
148
149
150 static INLINE struct vg_object *
handle_to_object(VGHandle h)151 handle_to_object(VGHandle h)
152 {
153 return (struct vg_object *) handle_to_pointer(h);
154 }
155
156
157 static INLINE struct vg_paint *
handle_to_paint(VGHandle h)158 handle_to_paint(VGHandle h)
159 {
160 return (struct vg_paint *) handle_to_pointer(h);
161 }
162
163
164 static INLINE struct path *
handle_to_path(VGHandle h)165 handle_to_path(VGHandle h)
166 {
167 return (struct path *) handle_to_pointer(h);
168 }
169
170
171 #endif /* HANDLE_H */
172