native.h revision f4c37d6ab256f860a755fe69dfea5fb8df217a2f
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.8
4 *
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef _NATIVE_H_
27#define _NATIVE_H_
28
29#include "EGL/egl.h"  /* for EGL native types */
30
31#include "pipe/p_compiler.h"
32#include "pipe/p_screen.h"
33#include "pipe/p_context.h"
34#include "pipe/p_state.h"
35#include "state_tracker/sw_winsys.h"
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41#include "native_buffer.h"
42#include "native_modeset.h"
43#include "native_wayland_bufmgr.h"
44
45/**
46 * Only color buffers are listed.  The others are allocated privately through,
47 * for example, st_renderbuffer_alloc_storage().
48 */
49enum native_attachment {
50   NATIVE_ATTACHMENT_FRONT_LEFT,
51   NATIVE_ATTACHMENT_BACK_LEFT,
52   NATIVE_ATTACHMENT_FRONT_RIGHT,
53   NATIVE_ATTACHMENT_BACK_RIGHT,
54
55   NUM_NATIVE_ATTACHMENTS
56};
57
58enum native_param_type {
59   /*
60    * Return TRUE if window/pixmap surfaces use the buffers of the native
61    * types.
62    */
63   NATIVE_PARAM_USE_NATIVE_BUFFER,
64
65   /**
66    * Return TRUE if native_surface::present can preserve the buffer.
67    */
68   NATIVE_PARAM_PRESERVE_BUFFER,
69
70   /**
71    * Return the maximum supported swap interval.
72    */
73   NATIVE_PARAM_MAX_SWAP_INTERVAL,
74
75   /**
76    * Return TRUE if the display supports premultiplied alpha, regardless of
77    * the surface color format.
78    */
79   NATIVE_PARAM_PREMULTIPLIED_ALPHA
80};
81
82/**
83 * Control how a surface presentation should happen.
84 */
85struct native_present_control {
86   /**< the attachment to present */
87   enum native_attachment natt;
88
89   /**< the contents of the presented attachment should be preserved */
90   boolean preserve;
91
92   /**< wait until the given vsyncs has passed since the last presentation */
93   uint swap_interval;
94
95   /**< pixels use premultiplied alpha */
96   boolean premultiplied_alpha;
97};
98
99struct native_surface {
100   /**
101    * Available for caller's use.
102    */
103   void *user_data;
104
105   void (*destroy)(struct native_surface *nsurf);
106
107   /**
108    * Present the given buffer to the native engine.
109    */
110   boolean (*present)(struct native_surface *nsurf,
111                      const struct native_present_control *ctrl);
112
113   /**
114    * Validate the buffers of the surface.  textures, if not NULL, points to an
115    * array of size NUM_NATIVE_ATTACHMENTS and the returned textures are owned
116    * by the caller.  A sequence number is also returned.  The caller can use
117    * it to check if anything has changed since the last call. Any of the
118    * pointers may be NULL and it indicates the caller has no interest in those
119    * values.
120    *
121    * If this function is called multiple times with different attachment
122    * masks, those not listed in the latest call might be destroyed.  This
123    * behavior might change in the future.
124    */
125   boolean (*validate)(struct native_surface *nsurf, uint attachment_mask,
126                       unsigned int *seq_num, struct pipe_resource **textures,
127                       int *width, int *height);
128
129   /**
130    * Wait until all native commands affecting the surface has been executed.
131    */
132   void (*wait)(struct native_surface *nsurf);
133};
134
135/**
136 * Describe a native display config.
137 */
138struct native_config {
139   /* available buffers and their format */
140   uint buffer_mask;
141   enum pipe_format color_format;
142
143   /* supported surface types */
144   boolean window_bit;
145   boolean pixmap_bit;
146   boolean scanout_bit;
147
148   int native_visual_id;
149   int native_visual_type;
150   int level;
151   boolean transparent_rgb;
152   int transparent_rgb_values[3];
153};
154
155/**
156 * A pipe winsys abstracts the OS.  A pipe screen abstracts the graphcis
157 * hardware.  A native display consists of a pipe winsys, a pipe screen, and
158 * the native display server.
159 */
160struct native_display {
161   /**
162    * The pipe screen of the native display.
163    */
164   struct pipe_screen *screen;
165
166   /**
167    * Context used for copy operations.
168    */
169   struct pipe_context *pipe;
170
171   /**
172    * Available for caller's use.
173    */
174   void *user_data;
175
176   /**
177    * Initialize and create the pipe screen.
178    */
179   boolean (*init_screen)(struct native_display *ndpy);
180
181   void (*destroy)(struct native_display *ndpy);
182
183   /**
184    * Query the parameters of the native display.
185    *
186    * The return value is defined by the parameter.
187    */
188   int (*get_param)(struct native_display *ndpy,
189                    enum native_param_type param);
190
191   /**
192    * Get the supported configs.  The configs are owned by the display, but
193    * the returned array should be FREE()ed.
194    */
195   const struct native_config **(*get_configs)(struct native_display *ndpy,
196                                               int *num_configs);
197
198   /**
199    * Get the color format of the pixmap.  Required unless no config has
200    * pixmap_bit set.
201    */
202   boolean (*get_pixmap_format)(struct native_display *ndpy,
203                                EGLNativePixmapType pix,
204                                enum pipe_format *format);
205
206   /**
207    * Copy the contents of the resource to the pixmap's front-left attachment.
208    * This is used to implement eglCopyBuffers.  Required unless no config has
209    * pixmap_bit set.
210    */
211   boolean (*copy_to_pixmap)(struct native_display *ndpy,
212                             EGLNativePixmapType pix,
213                             struct pipe_resource *src);
214
215   /**
216    * Create a window surface.  Required unless no config has window_bit set.
217    */
218   struct native_surface *(*create_window_surface)(struct native_display *ndpy,
219                                                   EGLNativeWindowType win,
220                                                   const struct native_config *nconf);
221
222   /**
223    * Create a pixmap surface.  The native config may be NULL.  In that case, a
224    * "best config" will be picked.  Required unless no config has pixmap_bit
225    * set.
226    */
227   struct native_surface *(*create_pixmap_surface)(struct native_display *ndpy,
228                                                   EGLNativePixmapType pix,
229                                                   const struct native_config *nconf);
230
231   const struct native_display_buffer *buffer;
232   const struct native_display_modeset *modeset;
233   const struct native_display_wayland_bufmgr *wayland_bufmgr;
234};
235
236/**
237 * The handler for events that a native display may generate.  The events are
238 * generated asynchronously and the handler may be called by any thread at any
239 * time.
240 */
241struct native_event_handler {
242   /**
243    * This function is called when a surface needs to be validated.
244    */
245   void (*invalid_surface)(struct native_display *ndpy,
246                           struct native_surface *nsurf,
247                           unsigned int seq_num);
248
249   struct pipe_screen *(*new_drm_screen)(struct native_display *ndpy,
250                                         const char *name, int fd);
251   struct pipe_screen *(*new_sw_screen)(struct native_display *ndpy,
252                                        struct sw_winsys *ws);
253
254   struct pipe_resource *(*lookup_egl_image)(struct native_display *ndpy,
255                                             void *egl_image);
256};
257
258/**
259 * Test whether an attachment is set in the mask.
260 */
261static INLINE boolean
262native_attachment_mask_test(uint mask, enum native_attachment att)
263{
264   return !!(mask & (1 << att));
265}
266
267/**
268 * Get the display copy context
269 */
270static INLINE struct pipe_context *
271ndpy_get_copy_context(struct native_display *ndpy)
272{
273   if (!ndpy->pipe)
274      ndpy->pipe = ndpy->screen->context_create(ndpy->screen, NULL);
275   return ndpy->pipe;
276}
277
278/**
279 * Free display screen and context resources
280 */
281static INLINE void
282ndpy_uninit(struct native_display *ndpy)
283{
284   if (ndpy->pipe)
285      ndpy->pipe->destroy(ndpy->pipe);
286   if (ndpy->screen)
287      ndpy->screen->destroy(ndpy->screen);
288}
289
290struct native_platform {
291   const char *name;
292
293   /**
294    * Create the native display and usually establish a connection to the
295    * display server.
296    *
297    * No event should be generated at this stage.
298    */
299   struct native_display *(*create_display)(void *dpy, boolean use_sw);
300};
301
302const struct native_platform *
303native_get_gdi_platform(const struct native_event_handler *event_handler);
304
305const struct native_platform *
306native_get_x11_platform(const struct native_event_handler *event_handler);
307
308const struct native_platform *
309native_get_wayland_platform(const struct native_event_handler *event_handler);
310
311const struct native_platform *
312native_get_drm_platform(const struct native_event_handler *event_handler);
313
314const struct native_platform *
315native_get_fbdev_platform(const struct native_event_handler *event_handler);
316
317const struct native_platform *
318native_get_android_platform(const struct native_event_handler *event_handler);
319
320#ifdef __cplusplus
321}
322#endif
323
324#endif /* _NATIVE_H_ */
325