Squashed commit of the following:
[profile/ivi/mesa.git] / src / gallium / include / state_tracker / st_api.h
1 /**********************************************************
2  * Copyright 2010 VMware, Inc.  All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  **********************************************************/
25
26
27 #ifndef _ST_API_H_
28 #define _ST_API_H_
29
30 #include "pipe/p_compiler.h"
31 #include "pipe/p_format.h"
32
33 /**
34  * \file API for communication between state trackers and state tracker
35  * managers.
36  *
37  * While both are state tackers, we use the term state tracker for rendering
38  * APIs such as OpenGL or OpenVG, and state tracker manager for window system
39  * APIs such as EGL or GLX in this file.
40  *
41  * This file defines an API to be implemented by both state trackers and state
42  * tracker managers.
43  */
44
45 /**
46  * The entry points of the state trackers.
47  */
48 #define ST_MODULE_OPENGL_SYMBOL      "st_module_OpenGL"
49 #define ST_MODULE_OPENGL_ES1_SYMBOL  "st_module_OpenGL_ES1"
50 #define ST_MODULE_OPENGL_ES2_SYMBOL  "st_module_OpenGL_ES2"
51 #define ST_MODULE_OPENVG_SYMBOL      "st_module_OpenVG"
52
53 /**
54  * The supported rendering API of a state tracker.
55  */
56 enum st_api_type {
57    ST_API_OPENGL,
58    ST_API_OPENGL_ES1,
59    ST_API_OPENGL_ES2,
60    ST_API_OPENVG,
61
62    ST_API_COUNT
63 };
64
65 /**
66  * Used in st_context_iface->teximage.
67  */
68 enum st_texture_type {
69    ST_TEXTURE_1D,
70    ST_TEXTURE_2D,
71    ST_TEXTURE_3D,
72    ST_TEXTURE_RECT,
73 };
74
75 /**
76  * Available attachments of framebuffer.
77  */
78 enum st_attachment_type {
79    ST_ATTACHMENT_FRONT_LEFT,
80    ST_ATTACHMENT_BACK_LEFT,
81    ST_ATTACHMENT_FRONT_RIGHT,
82    ST_ATTACHMENT_BACK_RIGHT,
83    ST_ATTACHMENT_DEPTH_STENCIL,
84    ST_ATTACHMENT_ACCUM,
85    ST_ATTACHMENT_SAMPLE,
86
87    ST_ATTACHMENT_COUNT,
88    ST_ATTACHMENT_INVALID = -1
89 };
90
91 /* for buffer_mask in st_visual */
92 #define ST_ATTACHMENT_FRONT_LEFT_MASK     (1 << ST_ATTACHMENT_FRONT_LEFT)
93 #define ST_ATTACHMENT_BACK_LEFT_MASK      (1 << ST_ATTACHMENT_BACK_LEFT)
94 #define ST_ATTACHMENT_FRONT_RIGHT_MASK    (1 << ST_ATTACHMENT_FRONT_RIGHT)
95 #define ST_ATTACHMENT_BACK_RIGHT_MASK     (1 << ST_ATTACHMENT_BACK_RIGHT)
96 #define ST_ATTACHMENT_DEPTH_STENCIL_MASK  (1 << ST_ATTACHMENT_DEPTH_STENCIL)
97 #define ST_ATTACHMENT_ACCUM_MASK          (1 << ST_ATTACHMENT_ACCUM)
98 #define ST_ATTACHMENT_SAMPLE_MASK         (1 << ST_ATTACHMENT_SAMPLE)
99
100 /**
101  * Enumerations of state tracker context resources.
102  */
103 enum st_context_resource_type {
104    ST_CONTEXT_RESOURCE_OPENGL_TEXTURE_2D,
105    ST_CONTEXT_RESOURCE_OPENGL_TEXTURE_3D,
106    ST_CONTEXT_RESOURCE_OPENGL_TEXTURE_CUBE_MAP_POSITIVE_X,
107    ST_CONTEXT_RESOURCE_OPENGL_TEXTURE_CUBE_MAP_NEGATIVE_X,
108    ST_CONTEXT_RESOURCE_OPENGL_TEXTURE_CUBE_MAP_POSITIVE_Y,
109    ST_CONTEXT_RESOURCE_OPENGL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
110    ST_CONTEXT_RESOURCE_OPENGL_TEXTURE_CUBE_MAP_POSITIVE_Z,
111    ST_CONTEXT_RESOURCE_OPENGL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
112    ST_CONTEXT_RESOURCE_OPENGL_RENDERBUFFER,
113    ST_CONTEXT_RESOURCE_OPENVG_PARENT_IMAGE,
114 };
115
116 /**
117  * The return type of st_api->get_proc_address.
118  */
119 typedef void (*st_proc_t)(void);
120
121 struct pipe_context;
122 struct pipe_resource;
123 struct pipe_fence_handle;
124
125 /**
126  * Used in st_context_iface->get_resource_for_egl_image.
127  */
128 struct st_context_resource
129 {
130    /* these fields are filled by the caller */
131    enum st_context_resource_type type;
132    void *resource;
133
134    /* this is owned by the caller */
135    struct pipe_resource *texture;
136 };
137
138 /**
139  * Used in st_manager_iface->get_egl_image.
140  */
141 struct st_egl_image
142 {
143    /* these fields are filled by the caller */
144    struct st_context_iface *stctxi;
145    void *egl_image;
146
147    /* this is owned by the caller */
148    struct pipe_resource *texture;
149
150    unsigned face;
151    unsigned level;
152    unsigned zslice;
153 };
154
155 /**
156  * Represent the visual of a framebuffer.
157  */
158 struct st_visual
159 {
160    /**
161     * Available buffers.  Tested with ST_FRAMEBUFFER_*_MASK.
162     */
163    unsigned buffer_mask;
164
165    /**
166     * Buffer formats.  The formats are always set even when the buffer is
167     * not available.
168     */
169    enum pipe_format color_format;
170    enum pipe_format depth_stencil_format;
171    enum pipe_format accum_format;
172    int samples;
173
174    /**
175     * Desired render buffer.
176     */
177    enum st_attachment_type render_buffer;
178 };
179
180 /**
181  * Represent a windowing system drawable.
182  *
183  * The framebuffer is implemented by the state tracker manager and
184  * used by the state trackers.
185  *
186  * Instead of the winsys pokeing into the API context to figure
187  * out what buffers that might be needed in the future by the API
188  * context, it calls into the framebuffer to get the textures.
189  *
190  * This structure along with the notify_invalid_framebuffer
191  * allows framebuffers to be shared between different threads
192  * but at the same make the API context free from thread
193  * syncronisation primitves, with the exception of a small
194  * atomic flag used for notification of framebuffer dirty status.
195  *
196  * The thread syncronisation is put inside the framebuffer
197  * and only called once the framebuffer has become dirty.
198  */
199 struct st_framebuffer_iface
200 {
201    /**
202     * Available for the state tracker manager to use.
203     */
204    void *st_manager_private;
205
206    /**
207     * The visual of a framebuffer.
208     */
209    const struct st_visual *visual;
210
211    /**
212     * Flush the front buffer.
213     *
214     * On some window systems, changes to the front buffers are not immediately
215     * visible.  They need to be flushed.
216     *
217     * @att is one of the front buffer attachments.
218     */
219    boolean (*flush_front)(struct st_framebuffer_iface *stfbi,
220                           enum st_attachment_type statt);
221
222    /**
223     * The state tracker asks for the textures it needs.
224     *
225     * It should try to only ask for attachments that it currently renders
226     * to, thus allowing the winsys to delay the allocation of textures not
227     * needed. For example front buffer attachments are not needed if you
228     * only do back buffer rendering.
229     *
230     * The implementor of this function needs to also ensure
231     * thread safty as this call might be done from multiple threads.
232     *
233     * The returned textures are owned by the caller.  They should be
234     * unreferenced when no longer used.  If this function is called multiple
235     * times with different sets of attachments, those buffers not included in
236     * the last call might be destroyed.  This behavior might change in the
237     * future.
238     */
239    boolean (*validate)(struct st_framebuffer_iface *stfbi,
240                        const enum st_attachment_type *statts,
241                        unsigned count,
242                        struct pipe_resource **out);
243 };
244
245 /**
246  * Represent a rendering context.
247  *
248  * This entity is created from st_api and used by the state tracker manager.
249  */
250 struct st_context_iface
251 {
252    /**
253     * Available for the state tracker and the manager to use.
254     */
255    void *st_context_private;
256    void *st_manager_private;
257
258    /**
259     * Destroy the context.
260     */
261    void (*destroy)(struct st_context_iface *stctxi);
262
263    /**
264     * Invalidate the current textures that was taken from a framebuffer.
265     *
266     * The state tracker manager calls this function to let the rendering
267     * context know that it should update the textures it got from
268     * st_framebuffer_iface::validate.  It should do so at the latest time possible.
269     * Possible right before sending triangles to the pipe context.
270     *
271     * For certain platforms this function might be called from a thread other
272     * than the thread that the context is currently bound in, and must
273     * therefore be thread safe. But it is the state tracker manager's
274     * responsibility to make sure that the framebuffer is bound to the context
275     * and the API context is current for the duration of this call.
276     *
277     * Thus reducing the sync primitive needed to a single atomic flag.
278     */
279    void (*notify_invalid_framebuffer)(struct st_context_iface *stctxi,
280                                       struct st_framebuffer_iface *stfbi);
281
282    /**
283     * Flush all drawing from context to the pipe also flushes the pipe.
284     */
285    void (*flush)(struct st_context_iface *stctxi, unsigned flags,
286                  struct pipe_fence_handle **fence);
287
288    /**
289     * Replace the texture image of a texture object at the specified level.
290     *
291     * This function is optional.
292     */
293    boolean (*teximage)(struct st_context_iface *stctxi, enum st_texture_type target,
294                        int level, enum pipe_format internal_format,
295                        struct pipe_resource *tex, boolean mipmap);
296
297    /**
298     * Used to implement glXCopyContext.
299     */
300    void (*copy)(struct st_context_iface *stctxi,
301                 struct st_context_iface *stsrci, unsigned mask);
302
303    /**
304     * Look up and return the info of a resource for EGLImage.
305     *
306     * This function is optional.
307     */
308    boolean (*get_resource_for_egl_image)(struct st_context_iface *stctxi,
309                                          struct st_context_resource *stres);
310 };
311
312
313 /**
314  * Represent a state tracker manager.
315  *
316  * This interface is implemented by the state tracker manager.  It corresponds
317  * to a "display" in the window system.
318  */
319 struct st_manager
320 {
321    struct pipe_screen *screen;
322
323    /**
324     * Look up and return the info of an EGLImage.
325     *
326     * This function is optional.
327     */
328    boolean (*get_egl_image)(struct st_manager *smapi,
329                             struct st_egl_image *stimg);
330 };
331
332 /**
333  * Represent a rendering API such as OpenGL or OpenVG.
334  *
335  * Implemented by the state tracker and used by the state tracker manager.
336  */
337 struct st_api
338 {
339    /**
340     * Destroy the API.
341     */
342    void (*destroy)(struct st_api *stapi);
343
344    /**
345     * Return an API entry point.
346     *
347     * For GL this is the same as _glapi_get_proc_address.
348     */
349    st_proc_t (*get_proc_address)(struct st_api *stapi, const char *procname);
350
351    /**
352     * Return true if the visual is supported by the state tracker.
353     */
354    boolean (*is_visual_supported)(struct st_api *stapi,
355                                   const struct st_visual *visual);
356
357    /**
358     * Create a rendering context.
359     */
360    struct st_context_iface *(*create_context)(struct st_api *stapi,
361                                               struct st_manager *smapi,
362                                               const struct st_visual *visual,
363                                               struct st_context_iface *stsharei);
364
365    /**
366     * Bind the context to the calling thread with draw and read as drawables.
367     *
368     * The framebuffers might have different visuals than the context does.
369     */
370    boolean (*make_current)(struct st_api *stapi,
371                            struct st_context_iface *stctxi,
372                            struct st_framebuffer_iface *stdrawi,
373                            struct st_framebuffer_iface *streadi);
374
375    /**
376     * Get the currently bound context in the calling thread.
377     */
378    struct st_context_iface *(*get_current)(struct st_api *stapi);
379 };
380
381 /**
382  * Represent a state tracker.
383  *
384  * This is the entry point of a state tracker.
385  */
386 struct st_module
387 {
388    enum st_api_type api;
389    struct st_api *(*create_api)(void);
390 };
391
392 /**
393  * Return true if the visual has the specified buffers.
394  */
395 static INLINE boolean
396 st_visual_have_buffers(const struct st_visual *visual, unsigned mask)
397 {
398    return ((visual->buffer_mask & mask) == mask);
399 }
400
401 /* these symbols may need to be dynamically lookup up */
402 extern PUBLIC const struct st_module st_module_OpenGL;
403 extern PUBLIC const struct st_module st_module_OpenGL_ES1;
404 extern PUBLIC const struct st_module st_module_OpenGL_ES2;
405 extern PUBLIC const struct st_module st_module_OpenVG;
406
407 #endif /* _ST_API_H_ */