Tizen 2.0 Release
[profile/ivi/osmesa.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 struct mapi_table;
46
47 struct st_renderbuffer {
48    enum pipe_format   format;
49    struct pipe_surface *surface;
50    struct pipe_resource *texture;
51    VGint width, height;
52 };
53
54 struct 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
70 enum 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 };
80 enum dirty_state {
81    BLEND_DIRTY         = 1 << 0,
82    FRAMEBUFFER_DIRTY   = 1 << 1,
83    DEPTH_STENCIL_DIRTY = 1 << 2,
84    PAINT_DIRTY         = 1 << 3,
85
86    ALL_DIRTY           = BLEND_DIRTY |
87                          FRAMEBUFFER_DIRTY |
88                          DEPTH_STENCIL_DIRTY |
89                          PAINT_DIRTY
90 };
91
92 struct vg_context
93 {
94    struct st_context_iface iface;
95    struct mapi_table *dispatch;
96
97    struct pipe_context *pipe;
98    enum pipe_format ds_format;
99
100    struct {
101       struct vg_state vg;
102       VGbitfield dirty;
103    } state;
104
105    VGErrorCode _error;
106
107    struct st_framebuffer *draw_buffer;
108    int32_t draw_buffer_invalid;
109
110    struct cso_hash *owned_objects[VG_OBJECT_LAST];
111
112    struct {
113       struct pipe_resource *cbuf;
114       struct pipe_sampler_state sampler;
115
116       struct vg_shader *union_fs;
117       struct vg_shader *intersect_fs;
118       struct vg_shader *subtract_fs;
119       struct vg_shader *set_fs;
120    } mask;
121
122    struct cso_context *cso_context;
123
124    struct renderer *renderer;
125    struct shaders_cache *sc;
126    struct shader *shader;
127
128    struct pipe_sampler_state blend_sampler;
129    struct vg_paint *default_paint;
130
131    struct blit_state *blit;
132 };
133
134
135 /**
136  *  Base class for VG objects like paths, images, fonts.
137  */
138 struct vg_object {
139    enum vg_object_type type;
140    VGHandle handle;
141    struct vg_context *ctx;
142 };
143
144
145 void vg_init_object(struct vg_object *obj, struct vg_context *ctx, enum vg_object_type type);
146 void vg_free_object(struct vg_object *obj);
147
148 VGboolean vg_object_is_valid(VGHandle object, enum vg_object_type type);
149
150 struct vg_context *vg_create_context(struct pipe_context *pipe,
151                                      const void *visual,
152                                      struct vg_context *share);
153 void vg_destroy_context(struct vg_context *ctx);
154 struct vg_context *vg_current_context(void);
155 void vg_set_current_context(struct vg_context *ctx);
156
157 VGboolean vg_context_is_object_valid(struct vg_context *ctx,
158                                      enum vg_object_type type,
159                                      VGHandle object);
160 void vg_context_add_object(struct vg_context *ctx,
161                            enum vg_object_type type,
162                            void *ptr);
163 void vg_context_remove_object(struct vg_context *ctx,
164                               enum vg_object_type type,
165                               void *ptr);
166
167 void vg_validate_state(struct vg_context *ctx);
168
169 void vg_set_error(struct vg_context *ctx,
170                   VGErrorCode code);
171
172 struct pipe_sampler_view *vg_prepare_blend_surface(struct vg_context *ctx);
173 struct pipe_sampler_view *vg_prepare_blend_surface_from_mask(struct vg_context *ctx);
174
175 struct pipe_sampler_view *vg_get_surface_mask(struct vg_context *ctx);
176
177 VGboolean vg_get_paint_matrix(struct vg_context *ctx,
178                               const struct matrix *paint_to_user,
179                               const struct matrix *user_to_surface,
180                               struct matrix *mat);
181
182 static INLINE VGboolean is_aligned_to(const void *ptr, VGbyte alignment)
183 {
184    void *aligned = align_pointer(ptr, alignment);
185    return (ptr == aligned) ? VG_TRUE : VG_FALSE;
186 }
187
188 static INLINE VGboolean is_aligned(const void *ptr)
189 {
190    return is_aligned_to(ptr, 4);
191 }
192
193 static INLINE void vg_shift_rectx(VGfloat coords[4],
194                                  const VGfloat *bounds,
195                                  const VGfloat shift)
196 {
197    coords[0] += shift;
198    coords[2] -= shift;
199    if (bounds) {
200       coords[2] = MIN2(coords[2], bounds[2]);
201       /* bound x/y + width/height */
202       if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
203          coords[2] = (bounds[0] + bounds[2]) - coords[0];
204       }
205    }
206 }
207
208 static INLINE void vg_shift_recty(VGfloat coords[4],
209                                  const VGfloat *bounds,
210                                  const VGfloat shift)
211 {
212    coords[1] += shift;
213    coords[3] -= shift;
214    if (bounds) {
215       coords[3] = MIN2(coords[3], bounds[3]);
216       if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
217          coords[3] = (bounds[1] + bounds[3]) - coords[1];
218       }
219    }
220 }
221
222 static INLINE void vg_bound_rect(VGfloat coords[4],
223                                  const VGfloat bounds[4],
224                                  VGfloat shift[4])
225 {
226    /* if outside the bounds */
227    if (coords[0] > (bounds[0] + bounds[2]) ||
228        coords[1] > (bounds[1] + bounds[3]) ||
229        (coords[0] + coords[2]) < bounds[0] ||
230        (coords[1] + coords[3]) < bounds[1]) {
231       coords[0] = 0.f;
232       coords[1] = 0.f;
233       coords[2] = 0.f;
234       coords[3] = 0.f;
235       shift[0] = 0.f;
236       shift[1] = 0.f;
237       return;
238    }
239
240    /* bound x */
241    if (coords[0] < bounds[0]) {
242       shift[0] = bounds[0] - coords[0];
243       coords[2] -= shift[0];
244       coords[0] = bounds[0];
245    } else
246       shift[0] = 0.f;
247
248    /* bound y */
249    if (coords[1] < bounds[1]) {
250       shift[1] = bounds[1] - coords[1];
251       coords[3] -= shift[1];
252       coords[1] = bounds[1];
253    } else
254       shift[1] = 0.f;
255
256    shift[2] = bounds[2] - coords[2];
257    shift[3] = bounds[3] - coords[3];
258    /* bound width/height */
259    coords[2] = MIN2(coords[2], bounds[2]);
260    coords[3] = MIN2(coords[3], bounds[3]);
261
262    /* bound x/y + width/height */
263    if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
264       coords[2] = (bounds[0] + bounds[2]) - coords[0];
265    }
266    if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
267       coords[3] = (bounds[1] + bounds[3]) - coords[1];
268    }
269
270    /* if outside the bounds */
271    if ((coords[0] + coords[2]) < bounds[0] ||
272        (coords[1] + coords[3]) < bounds[1]) {
273       coords[0] = 0.f;
274       coords[1] = 0.f;
275       coords[2] = 0.f;
276       coords[3] = 0.f;
277       return;
278    }
279 }
280
281 #endif