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 supported rendering API of a state tracker.
47 */
48enum st_api_type {
49   ST_API_OPENGL,
50   ST_API_OPENVG,
51
52   ST_API_COUNT
53};
54
55/**
56 * The profile of a context.
57 */
58enum st_profile_type
59{
60   ST_PROFILE_DEFAULT,			/**< OpenGL compatibility profile */
61   ST_PROFILE_OPENGL_CORE,		/**< OpenGL 3.2+ core profile */
62   ST_PROFILE_OPENGL_ES1,		/**< OpenGL ES 1.x */
63   ST_PROFILE_OPENGL_ES2		/**< OpenGL ES 2.0 */
64};
65
66/* for profile_mask in st_api */
67#define ST_PROFILE_DEFAULT_MASK      (1 << ST_PROFILE_DEFAULT)
68#define ST_PROFILE_OPENGL_CORE_MASK  (1 << ST_PROFILE_OPENGL_CORE)
69#define ST_PROFILE_OPENGL_ES1_MASK   (1 << ST_PROFILE_OPENGL_ES1)
70#define ST_PROFILE_OPENGL_ES2_MASK   (1 << ST_PROFILE_OPENGL_ES2)
71
72/**
73 * Optional API/state tracker features.
74 */
75enum st_api_feature
76{
77   ST_API_FEATURE_MS_VISUALS  /**< support for multisample visuals */
78};
79
80/* for feature_mask in st_api */
81#define ST_API_FEATURE_MS_VISUALS_MASK (1 << ST_API_FEATURE_MS_VISUALS)
82
83/**
84 * New context flags for GL 3.0 and beyond.
85 *
86 * Profile information (core vs. compatibilty for OpenGL 3.2+) is communicated
87 * through the \c st_profile_type, not through flags.
88 */
89#define ST_CONTEXT_FLAG_DEBUG               (1 << 0)
90#define ST_CONTEXT_FLAG_FORWARD_COMPATIBLE  (1 << 1)
91#define ST_CONTEXT_FLAG_ROBUST_ACCESS       (1 << 2)
92#define ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED (1 << 3)
93#define ST_CONTEXT_FLAG_NO_ERROR            (1 << 4)
94#define ST_CONTEXT_FLAG_RELEASE_NONE	    (1 << 5)
95#define ST_CONTEXT_FLAG_HIGH_PRIORITY       (1 << 6)
96#define ST_CONTEXT_FLAG_LOW_PRIORITY        (1 << 7)
97
98/**
99 * Reasons that context creation might fail.
100 */
101enum st_context_error {
102   ST_CONTEXT_SUCCESS = 0,
103   ST_CONTEXT_ERROR_NO_MEMORY,
104   ST_CONTEXT_ERROR_BAD_API,
105   ST_CONTEXT_ERROR_BAD_VERSION,
106   ST_CONTEXT_ERROR_BAD_FLAG,
107   ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE,
108   ST_CONTEXT_ERROR_UNKNOWN_FLAG
109};
110
111/**
112 * Used in st_context_iface->teximage.
113 */
114enum st_texture_type {
115   ST_TEXTURE_1D,
116   ST_TEXTURE_2D,
117   ST_TEXTURE_3D,
118   ST_TEXTURE_RECT
119};
120
121/**
122 * Available attachments of framebuffer.
123 */
124enum st_attachment_type {
125   ST_ATTACHMENT_FRONT_LEFT,
126   ST_ATTACHMENT_BACK_LEFT,
127   ST_ATTACHMENT_FRONT_RIGHT,
128   ST_ATTACHMENT_BACK_RIGHT,
129   ST_ATTACHMENT_DEPTH_STENCIL,
130   ST_ATTACHMENT_ACCUM,
131   ST_ATTACHMENT_SAMPLE,
132
133   ST_ATTACHMENT_COUNT,
134   ST_ATTACHMENT_INVALID = -1
135};
136
137/* for buffer_mask in st_visual */
138#define ST_ATTACHMENT_FRONT_LEFT_MASK     (1 << ST_ATTACHMENT_FRONT_LEFT)
139#define ST_ATTACHMENT_BACK_LEFT_MASK      (1 << ST_ATTACHMENT_BACK_LEFT)
140#define ST_ATTACHMENT_FRONT_RIGHT_MASK    (1 << ST_ATTACHMENT_FRONT_RIGHT)
141#define ST_ATTACHMENT_BACK_RIGHT_MASK     (1 << ST_ATTACHMENT_BACK_RIGHT)
142#define ST_ATTACHMENT_DEPTH_STENCIL_MASK  (1 << ST_ATTACHMENT_DEPTH_STENCIL)
143#define ST_ATTACHMENT_ACCUM_MASK          (1 << ST_ATTACHMENT_ACCUM)
144#define ST_ATTACHMENT_SAMPLE_MASK         (1 << ST_ATTACHMENT_SAMPLE)
145
146/**
147 * Flush flags.
148 */
149#define ST_FLUSH_FRONT                    (1 << 0)
150#define ST_FLUSH_END_OF_FRAME             (1 << 1)
151#define ST_FLUSH_WAIT                     (1 << 2)
152#define ST_FLUSH_FENCE_FD                 (1 << 3)
153
154/**
155 * Value to st_manager->get_param function.
156 */
157enum st_manager_param {
158   /**
159    * The dri state tracker on old libGL's doesn't do the right thing
160    * with regards to invalidating the framebuffers.
161    *
162    * For the mesa state tracker that means that it needs to invalidate
163    * the framebuffer in glViewport itself.
164    */
165   ST_MANAGER_BROKEN_INVALIDATE
166};
167
168struct pipe_context;
169struct pipe_resource;
170struct pipe_fence_handle;
171struct util_queue_monitoring;
172
173/**
174 * Used in st_manager_iface->get_egl_image.
175 */
176struct st_egl_image
177{
178   /* this is owned by the caller */
179   struct pipe_resource *texture;
180
181   /* format only differs from texture->format for multi-planar (YUV): */
182   enum pipe_format format;
183
184   unsigned level;
185   unsigned layer;
186};
187
188/**
189 * Represent the visual of a framebuffer.
190 */
191struct st_visual
192{
193   bool no_config;
194
195   /**
196    * Available buffers.  Bitfield of ST_ATTACHMENT_*_MASK bits.
197    */
198   unsigned buffer_mask;
199
200   /**
201    * Buffer formats.  The formats are always set even when the buffer is
202    * not available.
203    */
204   enum pipe_format color_format;
205   enum pipe_format depth_stencil_format;
206   enum pipe_format accum_format;
207   unsigned samples;
208
209   /**
210    * Desired render buffer.
211    */
212   enum st_attachment_type render_buffer;
213};
214
215
216/**
217 * Configuration options from driconf
218 */
219struct st_config_options
220{
221   boolean disable_blend_func_extended;
222   boolean disable_glsl_line_continuations;
223   boolean force_glsl_extensions_warn;
224   unsigned force_glsl_version;
225   boolean allow_glsl_extension_directive_midshader;
226   boolean allow_glsl_builtin_const_expression;
227   boolean allow_glsl_relaxed_es;
228   boolean allow_glsl_builtin_variable_redeclaration;
229   boolean allow_higher_compat_version;
230   boolean glsl_zero_init;
231   boolean force_glsl_abs_sqrt;
232   boolean allow_glsl_cross_stage_interpolation_mismatch;
233   boolean allow_glsl_layout_qualifier_on_function_parameters;
234   unsigned char config_options_sha1[20];
235};
236
237/**
238 * Represent the attributes of a context.
239 */
240struct st_context_attribs
241{
242   /**
243    * The profile and minimal version to support.
244    *
245    * The valid profiles and versions are rendering API dependent.  The latest
246    * version satisfying the request should be returned.
247    */
248   enum st_profile_type profile;
249   int major, minor;
250
251   /** Mask of ST_CONTEXT_FLAG_x bits */
252   unsigned flags;
253
254   /**
255    * The visual of the framebuffers the context will be bound to.
256    */
257   struct st_visual visual;
258
259   /**
260    * Configuration options.
261    */
262   struct st_config_options options;
263};
264
265struct st_context_iface;
266struct st_manager;
267
268/**
269 * Represent a windowing system drawable.
270 *
271 * The framebuffer is implemented by the state tracker manager and
272 * used by the state trackers.
273 *
274 * Instead of the winsys poking into the API context to figure
275 * out what buffers that might be needed in the future by the API
276 * context, it calls into the framebuffer to get the textures.
277 *
278 * This structure along with the notify_invalid_framebuffer
279 * allows framebuffers to be shared between different threads
280 * but at the same make the API context free from thread
281 * synchronization primitves, with the exception of a small
282 * atomic flag used for notification of framebuffer dirty status.
283 *
284 * The thread synchronization is put inside the framebuffer
285 * and only called once the framebuffer has become dirty.
286 */
287struct st_framebuffer_iface
288{
289   /**
290    * Atomic stamp which changes when framebuffers need to be updated.
291    */
292   int32_t stamp;
293
294   /**
295    * Identifier that uniquely identifies the framebuffer interface object.
296    */
297   uint32_t ID;
298
299   /**
300    * The state tracker manager that manages this object.
301    */
302   struct st_manager *state_manager;
303
304   /**
305    * Available for the state tracker manager to use.
306    */
307   void *st_manager_private;
308
309   /**
310    * The visual of a framebuffer.
311    */
312   const struct st_visual *visual;
313
314   /**
315    * Flush the front buffer.
316    *
317    * On some window systems, changes to the front buffers are not immediately
318    * visible.  They need to be flushed.
319    *
320    * @att is one of the front buffer attachments.
321    */
322   boolean (*flush_front)(struct st_context_iface *stctx,
323                          struct st_framebuffer_iface *stfbi,
324                          enum st_attachment_type statt);
325
326   /**
327    * The state tracker asks for the textures it needs.
328    *
329    * It should try to only ask for attachments that it currently renders
330    * to, thus allowing the winsys to delay the allocation of textures not
331    * needed. For example front buffer attachments are not needed if you
332    * only do back buffer rendering.
333    *
334    * The implementor of this function needs to also ensure
335    * thread safty as this call might be done from multiple threads.
336    *
337    * The returned textures are owned by the caller.  They should be
338    * unreferenced when no longer used.  If this function is called multiple
339    * times with different sets of attachments, those buffers not included in
340    * the last call might be destroyed.  This behavior might change in the
341    * future.
342    */
343   boolean (*validate)(struct st_context_iface *stctx,
344                       struct st_framebuffer_iface *stfbi,
345                       const enum st_attachment_type *statts,
346                       unsigned count,
347                       struct pipe_resource **out);
348   boolean (*flush_swapbuffers) (struct st_context_iface *stctx,
349                                 struct st_framebuffer_iface *stfbi);
350};
351
352/**
353 * Represent a rendering context.
354 *
355 * This entity is created from st_api and used by the state tracker manager.
356 */
357struct st_context_iface
358{
359   /**
360    * Available for the state tracker and the manager to use.
361    */
362   void *st_context_private;
363   void *st_manager_private;
364
365   /**
366    * The state tracker manager that manages this object.
367    */
368   struct st_manager *state_manager;
369
370   /**
371    * The CSO context associated with this context in case we need to draw
372    * something before swap buffers.
373    */
374   struct cso_context *cso_context;
375
376   /**
377    * The gallium context.
378    */
379   struct pipe_context *pipe;
380
381   /**
382    * Destroy the context.
383    */
384   void (*destroy)(struct st_context_iface *stctxi);
385
386   /**
387    * Flush all drawing from context to the pipe also flushes the pipe.
388    */
389   void (*flush)(struct st_context_iface *stctxi, unsigned flags,
390                 struct pipe_fence_handle **fence);
391
392   /**
393    * Replace the texture image of a texture object at the specified level.
394    *
395    * This function is optional.
396    */
397   boolean (*teximage)(struct st_context_iface *stctxi,
398                       enum st_texture_type target,
399                       int level, enum pipe_format internal_format,
400                       struct pipe_resource *tex, boolean mipmap);
401
402   /**
403    * Used to implement glXCopyContext.
404    */
405   void (*copy)(struct st_context_iface *stctxi,
406                struct st_context_iface *stsrci, unsigned mask);
407
408   /**
409    * Used to implement wglShareLists.
410    */
411   boolean (*share)(struct st_context_iface *stctxi,
412                    struct st_context_iface *stsrci);
413
414   /**
415    * Start the thread if the API has a worker thread.
416    * Called after the context has been created and fully initialized on both
417    * sides (e.g. st/mesa and st/dri).
418    */
419   void (*start_thread)(struct st_context_iface *stctxi);
420
421   /**
422    * If the API is multithreaded, wait for all queued commands to complete.
423    * Called from the main thread.
424    */
425   void (*thread_finish)(struct st_context_iface *stctxi);
426};
427
428
429/**
430 * Represent a state tracker manager.
431 *
432 * This interface is implemented by the state tracker manager.  It corresponds
433 * to a "display" in the window system.
434 */
435struct st_manager
436{
437   struct pipe_screen *screen;
438
439   /**
440    * Look up and return the info of an EGLImage.
441    *
442    * This is used to implement for example EGLImageTargetTexture2DOES.
443    * The GLeglImageOES agrument of that call is passed directly to this
444    * function call and the information needed to access this is returned
445    * in the given struct out.
446    *
447    * @smapi: manager owning the caller context
448    * @stctx: caller context
449    * @egl_image: EGLImage that caller recived
450    * @out: return struct filled out with access information.
451    *
452    * This function is optional.
453    */
454   boolean (*get_egl_image)(struct st_manager *smapi,
455                            void *egl_image,
456                            struct st_egl_image *out);
457
458   /**
459    * Query an manager param.
460    */
461   int (*get_param)(struct st_manager *smapi,
462                    enum st_manager_param param);
463
464   /**
465    * Call the loader function setBackgroundContext. Called from the worker
466    * thread.
467    */
468   void (*set_background_context)(struct st_context_iface *stctxi,
469                                  struct util_queue_monitoring *queue_info);
470
471   /**
472    * Destroy any private data used by the state tracker manager.
473    */
474   void (*destroy)(struct st_manager *smapi);
475
476   /**
477    * Available for the state tracker manager to use.
478    */
479   void *st_manager_private;
480};
481
482/**
483 * Represent a rendering API such as OpenGL or OpenVG.
484 *
485 * Implemented by the state tracker and used by the state tracker manager.
486 */
487struct st_api
488{
489   /**
490    * The name of the rendering API.  This is informative.
491    */
492   const char *name;
493
494   /**
495    * The supported rendering API.
496    */
497   enum st_api_type api;
498
499   /**
500    * The supported profiles.  Tested with ST_PROFILE_*_MASK.
501    */
502   unsigned profile_mask;
503
504   /**
505    * The supported optional features.  Tested with ST_FEATURE_*_MASK.
506    */
507   unsigned feature_mask;
508
509   /**
510    * Destroy the API.
511    */
512   void (*destroy)(struct st_api *stapi);
513
514   /**
515    * Query supported OpenGL versions. (if applicable)
516    * The format is (major*10+minor).
517    */
518   void (*query_versions)(struct st_api *stapi, struct st_manager *sm,
519                          struct st_config_options *options,
520                          int *gl_core_version,
521                          int *gl_compat_version,
522                          int *gl_es1_version,
523                          int *gl_es2_version);
524
525   /**
526    * Create a rendering context.
527    */
528   struct st_context_iface *(*create_context)(struct st_api *stapi,
529                                              struct st_manager *smapi,
530                                              const struct st_context_attribs *attribs,
531                                              enum st_context_error *error,
532                                              struct st_context_iface *stsharei);
533
534   /**
535    * Bind the context to the calling thread with draw and read as drawables.
536    *
537    * The framebuffers might be NULL, or might have different visuals than the
538    * context does.
539    */
540   boolean (*make_current)(struct st_api *stapi,
541                           struct st_context_iface *stctxi,
542                           struct st_framebuffer_iface *stdrawi,
543                           struct st_framebuffer_iface *streadi);
544
545   /**
546    * Get the currently bound context in the calling thread.
547    */
548   struct st_context_iface *(*get_current)(struct st_api *stapi);
549
550   /**
551    * Notify the st manager the framebuffer interface object
552    * is no longer valid.
553    */
554   void (*destroy_drawable)(struct st_api *stapi,
555                            struct st_framebuffer_iface *stfbi);
556};
557
558/**
559 * Return true if the visual has the specified buffers.
560 */
561static inline boolean
562st_visual_have_buffers(const struct st_visual *visual, unsigned mask)
563{
564   return ((visual->buffer_mask & mask) == mask);
565}
566
567#endif /* _ST_API_H_ */
568