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