vg_context.h revision d7a6901cac48cc3c4eea24113e108ef9dce843c4
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#ifndef VG_CONTEXT_H
28#define VG_CONTEXT_H
29
30#include "vg_state.h"
31
32#include "pipe/p_format.h"
33#include "pipe/p_state.h"
34#include "util/u_pointer.h"
35#include "util/u_math.h"
36#include "state_tracker/st_api.h"
37
38#include "cso_cache/cso_hash.h"
39#include "cso_cache/cso_context.h"
40
41struct renderer;
42struct shaders_cache;
43struct shader;
44struct vg_shader;
45struct mapi_table;
46
47struct st_renderbuffer {
48   enum pipe_format   format;
49   struct pipe_surface *surface;
50   struct pipe_resource *texture;
51   VGint width, height;
52};
53
54struct st_framebuffer {
55   VGint width, height;
56   struct st_renderbuffer *strb;
57   struct st_renderbuffer *dsrb;
58
59   struct pipe_sampler_view *surface_mask_view;
60
61   struct pipe_sampler_view *blend_texture_view;
62
63
64   struct st_framebuffer_iface *iface;
65   enum st_attachment_type strb_att;
66
67   void *privateData;
68};
69
70enum vg_object_type {
71   VG_OBJECT_UNKNOWN = 0,
72   VG_OBJECT_PAINT,
73   VG_OBJECT_IMAGE,
74   VG_OBJECT_MASK,
75   VG_OBJECT_FONT,
76   VG_OBJECT_PATH,
77
78   VG_OBJECT_LAST
79};
80enum dirty_state {
81   BLEND_DIRTY         = 1 << 0,
82   FRAMEBUFFER_DIRTY   = 1 << 1,
83   DEPTH_STENCIL_DIRTY = 1 << 2,
84
85   ALL_DIRTY           = BLEND_DIRTY |
86                         FRAMEBUFFER_DIRTY |
87                         DEPTH_STENCIL_DIRTY
88};
89
90struct vg_context
91{
92   struct st_context_iface iface;
93   struct mapi_table *dispatch;
94
95   struct pipe_context *pipe;
96   enum pipe_format ds_format;
97
98   struct {
99      struct vg_state vg;
100      VGbitfield dirty;
101   } state;
102
103   VGErrorCode _error;
104
105   struct st_framebuffer *draw_buffer;
106   int32_t draw_buffer_invalid;
107
108   struct cso_hash *owned_objects[VG_OBJECT_LAST];
109
110   struct {
111      struct pipe_resource *cbuf;
112      struct pipe_sampler_state sampler;
113
114      struct vg_shader *union_fs;
115      struct vg_shader *intersect_fs;
116      struct vg_shader *subtract_fs;
117      struct vg_shader *set_fs;
118   } mask;
119
120   struct cso_context *cso_context;
121
122   struct renderer *renderer;
123   struct shaders_cache *sc;
124   struct shader *shader;
125
126   struct pipe_sampler_state blend_sampler;
127   struct vg_paint *default_paint;
128
129   struct blit_state *blit;
130};
131
132struct vg_object {
133   enum vg_object_type type;
134   struct vg_context *ctx;
135};
136void vg_init_object(struct vg_object *obj, struct vg_context *ctx, enum vg_object_type type);
137VGboolean vg_object_is_valid(void *ptr, enum vg_object_type type);
138
139struct vg_context *vg_create_context(struct pipe_context *pipe,
140                                     const void *visual,
141                                     struct vg_context *share);
142void vg_destroy_context(struct vg_context *ctx);
143struct vg_context *vg_current_context(void);
144void vg_set_current_context(struct vg_context *ctx);
145
146VGboolean vg_context_is_object_valid(struct vg_context *ctx,
147                                     enum vg_object_type type,
148                                     void *ptr);
149void vg_context_add_object(struct vg_context *ctx,
150                           enum vg_object_type type,
151                           void *ptr);
152void vg_context_remove_object(struct vg_context *ctx,
153                              enum vg_object_type type,
154                              void *ptr);
155
156void vg_validate_state(struct vg_context *ctx);
157
158void vg_set_error(struct vg_context *ctx,
159                  VGErrorCode code);
160
161struct pipe_sampler_view *vg_prepare_blend_surface(struct vg_context *ctx);
162struct pipe_sampler_view *vg_prepare_blend_surface_from_mask(struct vg_context *ctx);
163
164struct pipe_sampler_view *vg_get_surface_mask(struct vg_context *ctx);
165
166VGboolean vg_get_paint_matrix(struct vg_context *ctx,
167                              const struct matrix *paint_to_user,
168                              const struct matrix *user_to_surface,
169                              struct matrix *mat);
170
171static INLINE VGboolean is_aligned_to(const void *ptr, VGbyte alignment)
172{
173   void *aligned = align_pointer(ptr, alignment);
174   return (ptr == aligned) ? VG_TRUE : VG_FALSE;
175}
176
177static INLINE VGboolean is_aligned(const void *ptr)
178{
179   return is_aligned_to(ptr, 4);
180}
181
182static INLINE void vg_shift_rectx(VGfloat coords[4],
183                                 const VGfloat *bounds,
184                                 const VGfloat shift)
185{
186   coords[0] += shift;
187   coords[2] -= shift;
188   if (bounds) {
189      coords[2] = MIN2(coords[2], bounds[2]);
190      /* bound x/y + width/height */
191      if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
192         coords[2] = (bounds[0] + bounds[2]) - coords[0];
193      }
194   }
195}
196
197static INLINE void vg_shift_recty(VGfloat coords[4],
198                                 const VGfloat *bounds,
199                                 const VGfloat shift)
200{
201   coords[1] += shift;
202   coords[3] -= shift;
203   if (bounds) {
204      coords[3] = MIN2(coords[3], bounds[3]);
205      if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
206         coords[3] = (bounds[1] + bounds[3]) - coords[1];
207      }
208   }
209}
210
211static INLINE void vg_bound_rect(VGfloat coords[4],
212                                 const VGfloat bounds[4],
213                                 VGfloat shift[4])
214{
215   /* if outside the bounds */
216   if (coords[0] > (bounds[0] + bounds[2]) ||
217       coords[1] > (bounds[1] + bounds[3]) ||
218       (coords[0] + coords[2]) < bounds[0] ||
219       (coords[1] + coords[3]) < bounds[1]) {
220      coords[0] = 0.f;
221      coords[1] = 0.f;
222      coords[2] = 0.f;
223      coords[3] = 0.f;
224      shift[0] = 0.f;
225      shift[1] = 0.f;
226      return;
227   }
228
229   /* bound x */
230   if (coords[0] < bounds[0]) {
231      shift[0] = bounds[0] - coords[0];
232      coords[2] -= shift[0];
233      coords[0] = bounds[0];
234   } else
235      shift[0] = 0.f;
236
237   /* bound y */
238   if (coords[1] < bounds[1]) {
239      shift[1] = bounds[1] - coords[1];
240      coords[3] -= shift[1];
241      coords[1] = bounds[1];
242   } else
243      shift[1] = 0.f;
244
245   shift[2] = bounds[2] - coords[2];
246   shift[3] = bounds[3] - coords[3];
247   /* bound width/height */
248   coords[2] = MIN2(coords[2], bounds[2]);
249   coords[3] = MIN2(coords[3], bounds[3]);
250
251   /* bound x/y + width/height */
252   if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
253      coords[2] = (bounds[0] + bounds[2]) - coords[0];
254   }
255   if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
256      coords[3] = (bounds[1] + bounds[3]) - coords[1];
257   }
258
259   /* if outside the bounds */
260   if ((coords[0] + coords[2]) < bounds[0] ||
261       (coords[1] + coords[3]) < bounds[1]) {
262      coords[0] = 0.f;
263      coords[1] = 0.f;
264      coords[2] = 0.f;
265      coords[3] = 0.f;
266      return;
267   }
268}
269
270#endif
271