vg_context.h revision 1ed63119452396e764a0f5b11db78903a2594df1
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
37#include "cso_cache/cso_hash.h"
38#include "cso_cache/cso_context.h"
39
40struct renderer;
41struct shaders_cache;
42struct shader;
43struct vg_shader;
44
45struct st_renderbuffer {
46   enum pipe_format   format;
47   struct pipe_surface *surface;
48   struct pipe_texture *texture;
49   VGint width, height;
50};
51
52struct st_framebuffer {
53   VGint width, height;
54   struct st_renderbuffer *strb;
55   struct st_renderbuffer *dsrb;
56
57   struct pipe_texture *alpha_mask;
58
59   struct pipe_texture *blend_texture;
60
61   void *privateData;
62};
63
64enum vg_object_type {
65   VG_OBJECT_UNKNOWN = 0,
66   VG_OBJECT_PAINT,
67   VG_OBJECT_IMAGE,
68   VG_OBJECT_MASK,
69   VG_OBJECT_FONT,
70   VG_OBJECT_PATH,
71
72   VG_OBJECT_LAST
73};
74enum dirty_state {
75   NONE_DIRTY          = 0<<0,
76   BLEND_DIRTY         = 1<<1,
77   RASTERIZER_DIRTY    = 1<<2,
78   VIEWPORT_DIRTY      = 1<<3,
79   VS_DIRTY            = 1<<4,
80   DEPTH_STENCIL_DIRTY = 1<<5,
81   ALL_DIRTY           = BLEND_DIRTY | RASTERIZER_DIRTY |
82   VIEWPORT_DIRTY | VS_DIRTY | DEPTH_STENCIL_DIRTY
83};
84
85struct vg_context
86{
87   struct pipe_context *pipe;
88
89   struct {
90      struct vg_state vg;
91      struct {
92         struct pipe_blend_state blend;
93         struct pipe_rasterizer_state rasterizer;
94         struct pipe_shader_state vs_state;
95         struct pipe_depth_stencil_alpha_state dsa;
96         struct pipe_framebuffer_state fb;
97      } g3d;
98      VGbitfield dirty;
99   } state;
100
101   VGErrorCode _error;
102
103   struct st_framebuffer *draw_buffer;
104
105   struct cso_hash *owned_objects[VG_OBJECT_LAST];
106
107   struct {
108      struct pipe_shader_state vert_shader;
109      struct pipe_shader_state frag_shader;
110      struct pipe_rasterizer_state raster;
111      void *fs;
112      float vertices[4][2][4];  /**< vertex pos + color */
113   } clear;
114
115   struct {
116      struct pipe_buffer *cbuf;
117      struct pipe_sampler_state sampler;
118
119      struct vg_shader *union_fs;
120      struct vg_shader *intersect_fs;
121      struct vg_shader *subtract_fs;
122      struct vg_shader *set_fs;
123   } mask;
124
125   struct vg_shader *pass_through_depth_fs;
126
127   struct cso_context *cso_context;
128
129   struct pipe_buffer *stencil_quad;
130   VGfloat stencil_vertices[4][2][4];
131
132   struct renderer *renderer;
133   struct shaders_cache *sc;
134   struct shader *shader;
135
136   struct pipe_sampler_state blend_sampler;
137   struct {
138      struct pipe_buffer *buffer;
139      void *color_matrix_fs;
140   } filter;
141   struct vg_paint *default_paint;
142
143   struct blit_state *blit;
144
145   struct vg_shader *plain_vs;
146   struct vg_shader *clear_vs;
147   struct vg_shader *texture_vs;
148   struct pipe_buffer *vs_const_buffer;
149};
150
151struct vg_object {
152   enum vg_object_type type;
153   struct vg_context *ctx;
154};
155void vg_init_object(struct vg_object *obj, struct vg_context *ctx, enum vg_object_type type);
156VGboolean vg_object_is_valid(void *ptr, enum vg_object_type type);
157
158struct vg_context *vg_create_context(struct pipe_context *pipe,
159                                     const void *visual,
160                                     struct vg_context *share);
161void vg_destroy_context(struct vg_context *ctx);
162struct vg_context *vg_current_context(void);
163void vg_set_current_context(struct vg_context *ctx);
164
165VGboolean vg_context_is_object_valid(struct vg_context *ctx,
166                                     enum vg_object_type type,
167                                     void *ptr);
168void vg_context_add_object(struct vg_context *ctx,
169                           enum vg_object_type type,
170                           void *ptr);
171void vg_context_remove_object(struct vg_context *ctx,
172                              enum vg_object_type type,
173                              void *ptr);
174
175void vg_validate_state(struct vg_context *ctx);
176
177void vg_set_error(struct vg_context *ctx,
178                  VGErrorCode code);
179
180void vg_prepare_blend_surface(struct vg_context *ctx);
181void vg_prepare_blend_surface_from_mask(struct vg_context *ctx);
182
183
184static INLINE VGboolean is_aligned_to(const void *ptr, VGbyte alignment)
185{
186   void *aligned = align_pointer(ptr, alignment);
187   return (ptr == aligned) ? VG_TRUE : VG_FALSE;
188}
189
190static INLINE VGboolean is_aligned(const void *ptr)
191{
192   return is_aligned_to(ptr, 4);
193}
194
195static INLINE void vg_shift_rectx(VGfloat coords[4],
196                                 const VGfloat *bounds,
197                                 const VGfloat shift)
198{
199   coords[0] += shift;
200   coords[2] -= shift;
201   if (bounds) {
202      coords[2] = MIN2(coords[2], bounds[2]);
203      /* bound x/y + width/height */
204      if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
205         coords[2] = (bounds[0] + bounds[2]) - coords[0];
206      }
207   }
208}
209
210static INLINE void vg_shift_recty(VGfloat coords[4],
211                                 const VGfloat *bounds,
212                                 const VGfloat shift)
213{
214   coords[1] += shift;
215   coords[3] -= shift;
216   if (bounds) {
217      coords[3] = MIN2(coords[3], bounds[3]);
218      if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
219         coords[3] = (bounds[1] + bounds[3]) - coords[1];
220      }
221   }
222}
223
224static INLINE void vg_bound_rect(VGfloat coords[4],
225                                 const VGfloat bounds[4],
226                                 VGfloat shift[4])
227{
228   /* if outside the bounds */
229   if (coords[0] > (bounds[0] + bounds[2]) ||
230       coords[1] > (bounds[1] + bounds[3]) ||
231       (coords[0] + coords[2]) < bounds[0] ||
232       (coords[1] + coords[3]) < bounds[1]) {
233      coords[0] = 0.f;
234      coords[1] = 0.f;
235      coords[2] = 0.f;
236      coords[3] = 0.f;
237      shift[0] = 0.f;
238      shift[1] = 0.f;
239      return;
240   }
241
242   /* bound x */
243   if (coords[0] < bounds[0]) {
244      shift[0] = bounds[0] - coords[0];
245      coords[2] -= shift[0];
246      coords[0] = bounds[0];
247   } else
248      shift[0] = 0.f;
249
250   /* bound y */
251   if (coords[1] < bounds[1]) {
252      shift[1] = bounds[1] - coords[1];
253      coords[3] -= shift[1];
254      coords[1] = bounds[1];
255   } else
256      shift[1] = 0.f;
257
258   shift[2] = bounds[2] - coords[2];
259   shift[3] = bounds[3] - coords[3];
260   /* bound width/height */
261   coords[2] = MIN2(coords[2], bounds[2]);
262   coords[3] = MIN2(coords[3], bounds[3]);
263
264   /* bound x/y + width/height */
265   if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
266      coords[2] = (bounds[0] + bounds[2]) - coords[0];
267   }
268   if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
269      coords[3] = (bounds[1] + bounds[3]) - coords[1];
270   }
271
272   /* if outside the bounds */
273   if ((coords[0] + coords[2]) < bounds[0] ||
274       (coords[1] + coords[3]) < bounds[1]) {
275      coords[0] = 0.f;
276      coords[1] = 0.f;
277      coords[2] = 0.f;
278      coords[3] = 0.f;
279      return;
280   }
281}
282
283void *vg_plain_vs(struct vg_context *ctx);
284void *vg_clear_vs(struct vg_context *ctx);
285void *vg_texture_vs(struct vg_context *ctx);
286typedef enum {
287   VEGA_Y0_TOP,
288   VEGA_Y0_BOTTOM
289} VegaOrientation;
290void vg_set_viewport(struct vg_context *ctx, VegaOrientation orientation);
291
292#endif
293