native.h revision 7c4e9dcdceec1112c91206619fe8b0885be99a79
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
76struct native_surface {
77   /**
78    * Available for caller's use.
79    */
80   void *user_data;
81
82   void (*destroy)(struct native_surface *nsurf);
83
84   /**
85    * Present the given buffer to the native engine.
86    */
87   boolean (*present)(struct native_surface *nsurf,
88                      enum native_attachment natt,
89                      boolean preserve,
90                      uint swap_interval);
91
92   /**
93    * Validate the buffers of the surface.  textures, if not NULL, points to an
94    * array of size NUM_NATIVE_ATTACHMENTS and the returned textures are owned
95    * by the caller.  A sequence number is also returned.  The caller can use
96    * it to check if anything has changed since the last call. Any of the
97    * pointers may be NULL and it indicates the caller has no interest in those
98    * values.
99    *
100    * If this function is called multiple times with different attachment
101    * masks, those not listed in the latest call might be destroyed.  This
102    * behavior might change in the future.
103    */
104   boolean (*validate)(struct native_surface *nsurf, uint attachment_mask,
105                       unsigned int *seq_num, struct pipe_resource **textures,
106                       int *width, int *height);
107
108   /**
109    * Wait until all native commands affecting the surface has been executed.
110    */
111   void (*wait)(struct native_surface *nsurf);
112};
113
114/**
115 * Describe a native display config.
116 */
117struct native_config {
118   /* available buffers and their format */
119   uint buffer_mask;
120   enum pipe_format color_format;
121
122   /* supported surface types */
123   boolean window_bit;
124   boolean pixmap_bit;
125   boolean scanout_bit;
126
127   int native_visual_id;
128   int native_visual_type;
129   int level;
130   boolean transparent_rgb;
131   int transparent_rgb_values[3];
132};
133
134/**
135 * A pipe winsys abstracts the OS.  A pipe screen abstracts the graphcis
136 * hardware.  A native display consists of a pipe winsys, a pipe screen, and
137 * the native display server.
138 */
139struct native_display {
140   /**
141    * The pipe screen of the native display.
142    */
143   struct pipe_screen *screen;
144
145   /**
146    * Context used for copy operations.
147    */
148   struct pipe_context *pipe;
149
150   /**
151    * Available for caller's use.
152    */
153   void *user_data;
154
155   /**
156    * Initialize and create the pipe screen.
157    */
158   boolean (*init_screen)(struct native_display *ndpy);
159
160   void (*destroy)(struct native_display *ndpy);
161
162   /**
163    * Query the parameters of the native display.
164    *
165    * The return value is defined by the parameter.
166    */
167   int (*get_param)(struct native_display *ndpy,
168                    enum native_param_type param);
169
170   /**
171    * Get the supported configs.  The configs are owned by the display, but
172    * the returned array should be FREE()ed.
173    */
174   const struct native_config **(*get_configs)(struct native_display *ndpy,
175                                               int *num_configs);
176
177   /**
178    * Test if a pixmap is supported by the given config.  Required unless no
179    * config has pixmap_bit set.
180    *
181    * This function is usually called to find a config that supports a given
182    * pixmap.  Thus, it is usually called with the same pixmap in a row.
183    *
184    * TODO should be get_pixmap_format() and return the pipe format of the
185    * pixmap.
186    */
187   boolean (*is_pixmap_supported)(struct native_display *ndpy,
188                                  EGLNativePixmapType pix,
189                                  const struct native_config *nconf);
190
191   /**
192    * Copy the contents of the resource to the pixmap's front-left attachment.
193    * This is used to implement eglCopyBuffers.  Required unless no config has
194    * pixmap_bit set.
195    */
196   boolean (*copy_to_pixmap)(struct native_display *ndpy,
197                             EGLNativePixmapType pix,
198                             struct pipe_resource *src);
199
200   /**
201    * Create a window surface.  Required unless no config has window_bit set.
202    */
203   struct native_surface *(*create_window_surface)(struct native_display *ndpy,
204                                                   EGLNativeWindowType win,
205                                                   const struct native_config *nconf);
206
207   /**
208    * Create a pixmap surface.  The native config may be NULL.  In that case, a
209    * "best config" will be picked.  Required unless no config has pixmap_bit
210    * set.
211    */
212   struct native_surface *(*create_pixmap_surface)(struct native_display *ndpy,
213                                                   EGLNativePixmapType pix,
214                                                   const struct native_config *nconf);
215
216   const struct native_display_buffer *buffer;
217   const struct native_display_modeset *modeset;
218   const struct native_display_wayland_bufmgr *wayland_bufmgr;
219};
220
221/**
222 * The handler for events that a native display may generate.  The events are
223 * generated asynchronously and the handler may be called by any thread at any
224 * time.
225 */
226struct native_event_handler {
227   /**
228    * This function is called when a surface needs to be validated.
229    */
230   void (*invalid_surface)(struct native_display *ndpy,
231                           struct native_surface *nsurf,
232                           unsigned int seq_num);
233
234   struct pipe_screen *(*new_drm_screen)(struct native_display *ndpy,
235                                         const char *name, int fd);
236   struct pipe_screen *(*new_sw_screen)(struct native_display *ndpy,
237                                        struct sw_winsys *ws);
238
239   struct pipe_resource *(*lookup_egl_image)(struct native_display *ndpy,
240                                             void *egl_image);
241};
242
243/**
244 * Test whether an attachment is set in the mask.
245 */
246static INLINE boolean
247native_attachment_mask_test(uint mask, enum native_attachment att)
248{
249   return !!(mask & (1 << att));
250}
251
252/**
253 * Get the display copy context
254 */
255static INLINE struct pipe_context *
256ndpy_get_copy_context(struct native_display *ndpy)
257{
258   if (!ndpy->pipe)
259      ndpy->pipe = ndpy->screen->context_create(ndpy->screen, NULL);
260   return ndpy->pipe;
261}
262
263/**
264 * Free display screen and context resources
265 */
266static INLINE void
267ndpy_uninit(struct native_display *ndpy)
268{
269   if (ndpy->pipe)
270      ndpy->pipe->destroy(ndpy->pipe);
271   if (ndpy->screen)
272      ndpy->screen->destroy(ndpy->screen);
273}
274
275struct native_platform {
276   const char *name;
277
278   /**
279    * Create the native display and usually establish a connection to the
280    * display server.
281    *
282    * No event should be generated at this stage.
283    */
284   struct native_display *(*create_display)(void *dpy, boolean use_sw);
285};
286
287const struct native_platform *
288native_get_gdi_platform(const struct native_event_handler *event_handler);
289
290const struct native_platform *
291native_get_x11_platform(const struct native_event_handler *event_handler);
292
293const struct native_platform *
294native_get_wayland_platform(const struct native_event_handler *event_handler);
295
296const struct native_platform *
297native_get_drm_platform(const struct native_event_handler *event_handler);
298
299const struct native_platform *
300native_get_fbdev_platform(const struct native_event_handler *event_handler);
301
302#ifdef __cplusplus
303}
304#endif
305
306#endif /* _NATIVE_H_ */
307