1d41e694cf78ada8c9258f96995115c9da8437894Brian Paul/**************************************************************************
2d41e694cf78ada8c9258f96995115c9da8437894Brian Paul *
3d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * Copyright 2010 VMware, Inc.  All Rights Reserved.
4d41e694cf78ada8c9258f96995115c9da8437894Brian Paul *
5d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * Permission is hereby granted, free of charge, to any person obtaining a
6d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * copy of this software and associated documentation files (the
7d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * "Software"), to deal in the Software without restriction, including
8d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * without limitation the rights to use, copy, modify, merge, publish,
9d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * distribute, sub license, and/or sell copies of the Software, and to
10d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * permit persons to whom the Software is furnished to do so, subject to
11d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * the following conditions:
12d41e694cf78ada8c9258f96995115c9da8437894Brian Paul *
13d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * The above copyright notice and this permission notice (including the
14d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * next paragraph) shall be included in all copies or substantial portions
15d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * of the Software.
16d41e694cf78ada8c9258f96995115c9da8437894Brian Paul *
17d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24d41e694cf78ada8c9258f96995115c9da8437894Brian Paul *
25d41e694cf78ada8c9258f96995115c9da8437894Brian Paul **************************************************************************/
26d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
27d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
28d41e694cf78ada8c9258f96995115c9da8437894Brian Paul/**
29d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * Convert opaque VG object handles into pointers and vice versa.
30d41e694cf78ada8c9258f96995115c9da8437894Brian Paul * XXX This is not yet 64-bit safe!  All VG handles are 32 bits in size.
31d41e694cf78ada8c9258f96995115c9da8437894Brian Paul */
32d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
33d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
34d41e694cf78ada8c9258f96995115c9da8437894Brian Paul#ifndef HANDLE_H
35d41e694cf78ada8c9258f96995115c9da8437894Brian Paul#define HANDLE_H
36d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
37d41e694cf78ada8c9258f96995115c9da8437894Brian Paul#include "pipe/p_compiler.h"
3899c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul#include "util/u_hash_table.h"
3999c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul#include "util/u_pointer.h"
4099c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
4199c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul#include "VG/openvg.h"
4299c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul#include "vg_context.h"
4399c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
4499c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
4599c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulextern struct util_hash_table *handle_hash;
46d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
47d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
48d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstruct vg_mask_layer;
49d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstruct vg_font;
50d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstruct vg_image;
51d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstruct vg_paint;
52d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstruct path;
53d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
54d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
5599c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulextern void
5699c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulinit_handles(void);
5799c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
5899c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
5999c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulextern void
6099c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulfree_handles(void);
6199c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
6299c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
6399c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulextern VGHandle
6499c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulcreate_handle(void *object);
6599c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
6699c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
6799c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulextern void
6899c67f27d35a4bbbbefada8117d5972c7583cf42Brian Pauldestroy_handle(VGHandle h);
6999c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
7099c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
7199c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulstatic INLINE VGHandle
7299c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paulobject_to_handle(struct vg_object *obj)
7399c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul{
7499c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   return obj ? obj->handle : VG_INVALID_HANDLE;
7599c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul}
7699c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
7799c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
78d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE VGHandle
79d41e694cf78ada8c9258f96995115c9da8437894Brian Paulimage_to_handle(struct vg_image *img)
80d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
8199c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   /* vg_image is derived from vg_object */
8299c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   return object_to_handle((struct vg_object *) img);
83d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
84d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
85d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
86d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE VGHandle
87d41e694cf78ada8c9258f96995115c9da8437894Brian Paulmasklayer_to_handle(struct vg_mask_layer *mask)
88d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
8999c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   /* vg_object is derived from vg_object */
9099c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   return object_to_handle((struct vg_object *) mask);
91d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
92d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
93d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
94d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE VGHandle
95d41e694cf78ada8c9258f96995115c9da8437894Brian Paulfont_to_handle(struct vg_font *font)
96d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
9799c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   return object_to_handle((struct vg_object *) font);
98d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
99d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
10099c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
101d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE VGHandle
102d41e694cf78ada8c9258f96995115c9da8437894Brian Paulpaint_to_handle(struct vg_paint *paint)
103d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
10499c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   return object_to_handle((struct vg_object *) paint);
105d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
106d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
10799c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul
108d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE VGHandle
109d41e694cf78ada8c9258f96995115c9da8437894Brian Paulpath_to_handle(struct path *path)
110d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
11199c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   return object_to_handle((struct vg_object *) path);
112d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
113d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
114d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
115d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE void *
116d41e694cf78ada8c9258f96995115c9da8437894Brian Paulhandle_to_pointer(VGHandle h)
117d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
11899c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   void *v = util_hash_table_get(handle_hash, intptr_to_pointer(h));
11999c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul#ifdef DEBUG
12099c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   if (v) {
12199c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul      struct vg_object *obj = (struct vg_object *) v;
12299c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul      assert(obj->handle == h);
12399c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   }
12499c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul#endif
12599c67f27d35a4bbbbefada8117d5972c7583cf42Brian Paul   return v;
126d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
127d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
128d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
129d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE struct vg_font *
130d41e694cf78ada8c9258f96995115c9da8437894Brian Paulhandle_to_font(VGHandle h)
131d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
132d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   return (struct vg_font *) handle_to_pointer(h);
133d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
134d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
135d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
136d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE struct vg_image *
137d41e694cf78ada8c9258f96995115c9da8437894Brian Paulhandle_to_image(VGHandle h)
138d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
139d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   return (struct vg_image *) handle_to_pointer(h);
140d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
141d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
142d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
143d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE struct vg_mask_layer *
144d41e694cf78ada8c9258f96995115c9da8437894Brian Paulhandle_to_masklayer(VGHandle h)
145d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
146d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   return (struct vg_mask_layer *) handle_to_pointer(h);
147d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
148d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
149d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
150d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE struct vg_object *
151d41e694cf78ada8c9258f96995115c9da8437894Brian Paulhandle_to_object(VGHandle h)
152d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
153d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   return (struct vg_object *) handle_to_pointer(h);
154d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
155d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
156d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
157d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE struct vg_paint *
158d41e694cf78ada8c9258f96995115c9da8437894Brian Paulhandle_to_paint(VGHandle h)
159d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
160d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   return (struct vg_paint *) handle_to_pointer(h);
161d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
162d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
163d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
164d41e694cf78ada8c9258f96995115c9da8437894Brian Paulstatic INLINE struct path *
165d41e694cf78ada8c9258f96995115c9da8437894Brian Paulhandle_to_path(VGHandle h)
166d41e694cf78ada8c9258f96995115c9da8437894Brian Paul{
167d41e694cf78ada8c9258f96995115c9da8437894Brian Paul   return (struct path *) handle_to_pointer(h);
168d41e694cf78ada8c9258f96995115c9da8437894Brian Paul}
169d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
170d41e694cf78ada8c9258f96995115c9da8437894Brian Paul
171d41e694cf78ada8c9258f96995115c9da8437894Brian Paul#endif /* HANDLE_H */
172