vg_context.h revision b730f0fc52a208b5f2a308199724ab02aa391fec
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 *alpha_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   NONE_DIRTY          = 0<<0,
82   BLEND_DIRTY         = 1<<1,
83   RASTERIZER_DIRTY    = 1<<2,
84   FRAMEBUFFER_DIRTY   = 1<<3,
85   VS_DIRTY            = 1<<4,
86   DEPTH_STENCIL_DIRTY = 1<<5,
87   ALL_DIRTY           = BLEND_DIRTY | RASTERIZER_DIRTY |
88   FRAMEBUFFER_DIRTY | VS_DIRTY | DEPTH_STENCIL_DIRTY
89};
90
91struct vg_context
92{
93   struct st_context_iface iface;
94   struct mapi_table *dispatch;
95
96   struct pipe_context *pipe;
97   enum pipe_format ds_format;
98
99   struct {
100      struct vg_state vg;
101      VGbitfield dirty;
102   } state;
103
104   VGErrorCode _error;
105
106   struct st_framebuffer *draw_buffer;
107   int32_t draw_buffer_invalid;
108
109   struct cso_hash *owned_objects[VG_OBJECT_LAST];
110
111   struct {
112      struct pipe_shader_state vert_shader;
113      struct pipe_shader_state frag_shader;
114      struct pipe_rasterizer_state raster;
115      void *fs;
116      float vertices[4][2][4];  /**< vertex pos + color */
117   } clear;
118
119   struct {
120      struct pipe_resource *cbuf;
121      struct pipe_sampler_state sampler;
122
123      struct vg_shader *union_fs;
124      struct vg_shader *intersect_fs;
125      struct vg_shader *subtract_fs;
126      struct vg_shader *set_fs;
127   } mask;
128
129   struct vg_shader *pass_through_depth_fs;
130
131   struct cso_context *cso_context;
132
133   struct pipe_resource *stencil_quad;
134   VGfloat stencil_vertices[4][2][4];
135
136   struct renderer *renderer;
137   struct shaders_cache *sc;
138   struct shader *shader;
139
140   struct pipe_sampler_state blend_sampler;
141   struct {
142      struct pipe_resource *buffer;
143      void *color_matrix_fs;
144   } filter;
145   struct vg_paint *default_paint;
146
147   struct blit_state *blit;
148
149   struct vg_shader *plain_vs;
150   struct vg_shader *clear_vs;
151   struct vg_shader *texture_vs;
152   struct pipe_resource *vs_const_buffer;
153   struct pipe_vertex_element velems[2];
154};
155
156struct vg_object {
157   enum vg_object_type type;
158   struct vg_context *ctx;
159};
160void vg_init_object(struct vg_object *obj, struct vg_context *ctx, enum vg_object_type type);
161VGboolean vg_object_is_valid(void *ptr, enum vg_object_type type);
162
163struct vg_context *vg_create_context(struct pipe_context *pipe,
164                                     const void *visual,
165                                     struct vg_context *share);
166void vg_destroy_context(struct vg_context *ctx);
167struct vg_context *vg_current_context(void);
168void vg_set_current_context(struct vg_context *ctx);
169
170VGboolean vg_context_is_object_valid(struct vg_context *ctx,
171                                     enum vg_object_type type,
172                                     void *ptr);
173void vg_context_add_object(struct vg_context *ctx,
174                           enum vg_object_type type,
175                           void *ptr);
176void vg_context_remove_object(struct vg_context *ctx,
177                              enum vg_object_type type,
178                              void *ptr);
179
180void vg_validate_state(struct vg_context *ctx);
181
182void vg_set_error(struct vg_context *ctx,
183                  VGErrorCode code);
184
185void vg_prepare_blend_surface(struct vg_context *ctx);
186void vg_prepare_blend_surface_from_mask(struct vg_context *ctx);
187
188
189static INLINE VGboolean is_aligned_to(const void *ptr, VGbyte alignment)
190{
191   void *aligned = align_pointer(ptr, alignment);
192   return (ptr == aligned) ? VG_TRUE : VG_FALSE;
193}
194
195static INLINE VGboolean is_aligned(const void *ptr)
196{
197   return is_aligned_to(ptr, 4);
198}
199
200static INLINE void vg_shift_rectx(VGfloat coords[4],
201                                 const VGfloat *bounds,
202                                 const VGfloat shift)
203{
204   coords[0] += shift;
205   coords[2] -= shift;
206   if (bounds) {
207      coords[2] = MIN2(coords[2], bounds[2]);
208      /* bound x/y + width/height */
209      if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
210         coords[2] = (bounds[0] + bounds[2]) - coords[0];
211      }
212   }
213}
214
215static INLINE void vg_shift_recty(VGfloat coords[4],
216                                 const VGfloat *bounds,
217                                 const VGfloat shift)
218{
219   coords[1] += shift;
220   coords[3] -= shift;
221   if (bounds) {
222      coords[3] = MIN2(coords[3], bounds[3]);
223      if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
224         coords[3] = (bounds[1] + bounds[3]) - coords[1];
225      }
226   }
227}
228
229static INLINE void vg_bound_rect(VGfloat coords[4],
230                                 const VGfloat bounds[4],
231                                 VGfloat shift[4])
232{
233   /* if outside the bounds */
234   if (coords[0] > (bounds[0] + bounds[2]) ||
235       coords[1] > (bounds[1] + bounds[3]) ||
236       (coords[0] + coords[2]) < bounds[0] ||
237       (coords[1] + coords[3]) < bounds[1]) {
238      coords[0] = 0.f;
239      coords[1] = 0.f;
240      coords[2] = 0.f;
241      coords[3] = 0.f;
242      shift[0] = 0.f;
243      shift[1] = 0.f;
244      return;
245   }
246
247   /* bound x */
248   if (coords[0] < bounds[0]) {
249      shift[0] = bounds[0] - coords[0];
250      coords[2] -= shift[0];
251      coords[0] = bounds[0];
252   } else
253      shift[0] = 0.f;
254
255   /* bound y */
256   if (coords[1] < bounds[1]) {
257      shift[1] = bounds[1] - coords[1];
258      coords[3] -= shift[1];
259      coords[1] = bounds[1];
260   } else
261      shift[1] = 0.f;
262
263   shift[2] = bounds[2] - coords[2];
264   shift[3] = bounds[3] - coords[3];
265   /* bound width/height */
266   coords[2] = MIN2(coords[2], bounds[2]);
267   coords[3] = MIN2(coords[3], bounds[3]);
268
269   /* bound x/y + width/height */
270   if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
271      coords[2] = (bounds[0] + bounds[2]) - coords[0];
272   }
273   if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
274      coords[3] = (bounds[1] + bounds[3]) - coords[1];
275   }
276
277   /* if outside the bounds */
278   if ((coords[0] + coords[2]) < bounds[0] ||
279       (coords[1] + coords[3]) < bounds[1]) {
280      coords[0] = 0.f;
281      coords[1] = 0.f;
282      coords[2] = 0.f;
283      coords[3] = 0.f;
284      return;
285   }
286}
287
288void *vg_plain_vs(struct vg_context *ctx);
289void *vg_clear_vs(struct vg_context *ctx);
290void *vg_texture_vs(struct vg_context *ctx);
291
292#endif
293