Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / egl / main / eglscreen.c
1 /*
2 * Ideas for screen management extension to EGL.
3 *
4 * Each EGLDisplay has one or more screens (CRTs, Flat Panels, etc).
5 * The screens' handles can be obtained with eglGetScreensMESA().
6 *
7 * A new kind of EGLSurface is possible- one which can be directly scanned
8 * out on a screen. Such a surface is created with eglCreateScreenSurface().
9 *
10 * To actually display a screen surface on a screen, the eglShowSurface()
11 * function is called.
12 */
13
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "egldisplay.h"
19 #include "eglglobals.h"
20 #include "eglmode.h"
21 #include "eglconfig.h"
22 #include "eglsurface.h"
23 #include "eglscreen.h"
24
25
26 /**
27 * Return a new screen handle/ID.
28 * NOTE: we never reuse these!
29 */
30 EGLScreenMESA
31 _eglAllocScreenHandle(void)
32 {
33 EGLScreenMESA s = _eglGlobal.FreeScreenHandle;
34 _eglGlobal.FreeScreenHandle++;
35 return s;
36 }
37
38
39 /**
40 * Initialize an _EGLScreen object to default values.
41 */
42 void
43 _eglInitScreen(_EGLScreen *screen)
44 {
45 memset(screen, 0, sizeof(_EGLScreen));
46 screen->StepX = 1;
47 screen->StepY = 1;
48 }
49
50
51 /**
52 * Given a public screen handle, return the internal _EGLScreen object.
53 */
54 _EGLScreen *
55 _eglLookupScreen(EGLDisplay dpy, EGLScreenMESA screen)
56 {
57 EGLint i;
58 _EGLDisplay *display = _eglLookupDisplay(dpy);
59
60 if (!display)
61 return NULL;
62
63 for (i = 0; i < display->NumScreens; i++) {
64 if (display->Screens[i]->Handle == screen)
65 return display->Screens[i];
66 }
67 return NULL;
68 }
69
70
71 /**
72 * Add the given _EGLScreen to the display's list of screens.
73 */
74 void
75 _eglAddScreen(_EGLDisplay *display, _EGLScreen *screen)
76 {
77 EGLint n;
78
79 assert(display);
80 assert(screen);
81
82 screen->Handle = _eglAllocScreenHandle();
83 n = display->NumScreens;
84 display->Screens = realloc(display->Screens, (n+1) * sizeof(_EGLScreen *));
85 display->Screens[n] = screen;
86 display->NumScreens++;
87 }
88
89
90
91 EGLBoolean
92 _eglGetScreensMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA *screens,
93 EGLint max_screens, EGLint *num_screens)
94 {
95 _EGLDisplay *display = _eglLookupDisplay(dpy);
96 EGLint n;
97
98 if (!display) {
99 _eglError(EGL_BAD_DISPLAY, "eglGetScreensMESA");
100 return EGL_FALSE;
101 }
102
103 if (display->NumScreens > max_screens) {
104 n = max_screens;
105 }
106 else {
107 n = display->NumScreens;
108 }
109
110 if (screens) {
111 EGLint i;
112 for (i = 0; i < n; i++)
113 screens[i] = display->Screens[i]->Handle;
114 }
115 if (num_screens)
116 *num_screens = n;
117
118 return EGL_TRUE;
119 }
120
121
122 /**
123 * Example function - drivers should do a proper implementation.
124 */
125 EGLSurface
126 _eglCreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
127 const EGLint *attrib_list)
128 {
129 #if 0 /* THIS IS JUST EXAMPLE CODE */
130 _EGLSurface *surf;
131
132 surf = (_EGLSurface *) calloc(1, sizeof(_EGLSurface));
133 if (!surf)
134 return EGL_NO_SURFACE;
135
136 if (!_eglInitSurface(drv, dpy, surf, EGL_SCREEN_BIT_MESA,
137 config, attrib_list)) {
138 free(surf);
139 return EGL_NO_SURFACE;
140 }
141
142 _eglSaveSurface(surf);
143
144 return surf->Handle;
145 #endif
146 return EGL_NO_SURFACE;
147 }
148
149
150 /**
151 * Show the given surface on the named screen.
152 * If surface is EGL_NO_SURFACE, disable the screen's output.
153 *
154 * This is just a placeholder function; drivers will always override
155 * this with code that _really_ shows the surface.
156 */
157 EGLBoolean
158 _eglShowScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy,
159 EGLScreenMESA screen, EGLSurface surface,
160 EGLModeMESA m)
161 {
162 _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
163 _EGLMode *mode = _eglLookupMode(dpy, m);
164
165 if (!scrn) {
166 _eglError(EGL_BAD_SCREEN_MESA, "eglShowSurfaceMESA");
167 return EGL_FALSE;
168 }
169 if (!mode && (m != EGL_NO_MODE_MESA )) {
170 _eglError(EGL_BAD_MODE_MESA, "eglShowSurfaceMESA");
171 return EGL_FALSE;
172 }
173
174 if (surface == EGL_NO_SURFACE) {
175 scrn->CurrentSurface = NULL;
176 }
177 else {
178 _EGLSurface *surf = _eglLookupSurface(surface);
179 if (!surf || surf->Type != EGL_SCREEN_BIT_MESA) {
180 _eglError(EGL_BAD_SURFACE, "eglShowSurfaceMESA");
181 return EGL_FALSE;
182 }
183 if (surf->Width < mode->Width || surf->Height < mode->Height) {
184 _eglError(EGL_BAD_SURFACE,
185 "eglShowSurfaceMESA(surface smaller than screen size)");
186 return EGL_FALSE;
187 }
188
189 scrn->CurrentSurface = surf;
190 scrn->CurrentMode = mode;
191 }
192 return EGL_TRUE;
193 }
194
195
196 /**
197 * Set a screen's current display mode.
198 * Note: mode = EGL_NO_MODE is valid (turns off the screen)
199 *
200 * This is just a placeholder function; drivers will always override
201 * this with code that _really_ sets the mode.
202 */
203 EGLBoolean
204 _eglScreenModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
205 EGLModeMESA mode)
206 {
207 _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
208
209 if (!scrn) {
210 _eglError(EGL_BAD_SCREEN_MESA, "eglScreenModeMESA");
211 return EGL_FALSE;
212 }
213
214 scrn->CurrentMode = _eglLookupMode(dpy, mode);
215
216 return EGL_TRUE;
217 }
218
219
220 /**
221 * Set a screen's surface origin.
222 */
223 EGLBoolean
224 _eglScreenPositionMESA(_EGLDriver *drv, EGLDisplay dpy,
225 EGLScreenMESA screen, EGLint x, EGLint y)
226 {
227 _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
228 if (!scrn) {
229 _eglError(EGL_BAD_SCREEN_MESA, "eglScreenPositionMESA");
230 return EGL_FALSE;
231 }
232
233 scrn->OriginX = x;
234 scrn->OriginY = y;
235
236 return EGL_TRUE;
237 }
238
239
240 /**
241 * Query a screen's current surface.
242 */
243 EGLBoolean
244 _eglQueryScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy,
245 EGLScreenMESA screen, EGLSurface *surface)
246 {
247 const _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
248 if (scrn->CurrentSurface)
249 *surface = scrn->CurrentSurface->Handle;
250 else
251 *surface = EGL_NO_SURFACE;
252 return EGL_TRUE;
253 }
254
255
256 /**
257 * Query a screen's current mode.
258 */
259 EGLBoolean
260 _eglQueryScreenModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
261 EGLModeMESA *mode)
262 {
263 const _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
264 if (scrn->CurrentMode)
265 *mode = scrn->CurrentMode->Handle;
266 else
267 *mode = EGL_NO_MODE_MESA;
268 return EGL_TRUE;
269 }
270
271
272 EGLBoolean
273 _eglQueryScreenMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
274 EGLint attribute, EGLint *value)
275 {
276 const _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
277
278 if (!scrn) {
279 _eglError(EGL_BAD_SCREEN_MESA, "eglQueryScreenMESA");
280 return EGL_FALSE;
281 }
282
283 switch (attribute) {
284 case EGL_SCREEN_POSITION_MESA:
285 value[0] = scrn->OriginX;
286 value[1] = scrn->OriginY;
287 break;
288 case EGL_SCREEN_POSITION_GRANULARITY_MESA:
289 value[0] = scrn->StepX;
290 value[1] = scrn->StepY;
291 break;
292 default:
293 _eglError(EGL_BAD_ATTRIBUTE, "eglQueryScreenMESA");
294 return EGL_FALSE;
295 }
296
297 return EGL_TRUE;
298 }
299
300
301 /**
302 * Delete the modes associated with given screen.
303 */
304 void
305 _eglDestroyScreenModes(_EGLScreen *scrn)
306 {
307 EGLint i;
308 for (i = 0; i < scrn->NumModes; i++) {
309 if (scrn->Modes[i].Name)
310 free((char *) scrn->Modes[i].Name); /* cast away const */
311 }
312 if (scrn->Modes)
313 free(scrn->Modes);
314 scrn->Modes = NULL;
315 scrn->NumModes = 0;
316 }
317
318
319 /**
320 * Default fallback routine - drivers should usually override this.
321 */
322 void
323 _eglDestroyScreen(_EGLScreen *scrn)
324 {
325 _eglDestroyScreenModes(scrn);
326 free(scrn);
327 }
328