sync with latest EGL_MESA_screen_surface spec (EGLScreenMESA handles)
[mesa.git] / src / egl / main / eglmode.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include "egldisplay.h"
4 #include "egldriver.h"
5 #include "eglmode.h"
6 #include "eglglobals.h"
7 #include "eglscreen.h"
8
9
10 #define MIN2(A, B) (((A) < (B)) ? (A) : (B))
11
12
13 /**
14 * Given an EGLModeMESA handle, return the corresponding _EGLMode object
15 * or null if non-existant.
16 */
17 _EGLMode *
18 _eglLookupMode(EGLDisplay dpy, EGLModeMESA mode)
19 {
20 const _EGLDisplay *disp = _eglLookupDisplay(dpy);
21 EGLint scrnum;
22
23 /* loop over all screens on the display */
24 for (scrnum = 0; scrnum < disp->NumScreens; scrnum++) {
25 const _EGLScreen *scrn = disp->Screens[scrnum];
26 EGLint i;
27 /* search list of modes for handle */
28 for (i = 0; i < scrn->NumModes; i++) {
29 if (scrn->Modes[i].Handle == mode) {
30 return scrn->Modes + i;
31 }
32 }
33 }
34
35 return NULL;
36 }
37
38
39 /**
40 * Add a new mode with the given attributes (width, height, depth, refreshRate)
41 * to the given screen.
42 * Assign a new mode ID/handle to the mode as well.
43 * \return pointer to the new _EGLMode
44 */
45 _EGLMode *
46 _eglAddMode(_EGLScreen *screen, EGLint width, EGLint height,
47 EGLint depth, EGLint refreshRate)
48 {
49 EGLint n;
50 _EGLMode *newModes;
51
52 assert(screen);
53 assert(width > 0);
54 assert(height > 0);
55 assert(depth > 0);
56 assert(refreshRate > 0);
57
58 n = screen->NumModes;
59 newModes = (_EGLMode *) realloc(screen->Modes, (n+1) * sizeof(_EGLMode));
60 if (newModes) {
61 screen->Modes = newModes;
62 screen->Modes[n].Handle = n + 1;
63 screen->Modes[n].Width = width;
64 screen->Modes[n].Height = height;
65 screen->Modes[n].Depth = depth;
66 screen->Modes[n].RefreshRate = refreshRate;
67 screen->Modes[n].Stereo = EGL_FALSE;
68 screen->NumModes++;
69 return screen->Modes + n;
70 }
71 else {
72 return NULL;
73 }
74 }
75
76
77
78 /**
79 * Search for the EGLMode that best matches the given attribute list.
80 */
81 EGLBoolean
82 _eglChooseModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
83 const EGLint *attrib_list, EGLModeMESA *modes,
84 EGLint modes_size, EGLint *num_modes)
85 {
86 EGLint i;
87
88 /* XXX incomplete */
89
90 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
91 switch (attrib_list[i]) {
92 case EGL_WIDTH:
93 i++;
94 break;
95 case EGL_HEIGHT:
96 i++;
97 break;
98 case EGL_REFRESH_RATE_MESA:
99 i++;
100 break;
101 #if 0
102 case EGL_STEREO_MESA:
103 i++;
104 break;
105 #endif
106 default:
107 _eglError(EGL_BAD_ATTRIBUTE, "eglChooseMode");
108 return EGL_FALSE;
109 }
110 }
111
112 return EGL_TRUE;
113 }
114
115
116
117 /**
118 * Return all possible modes for the given screen
119 */
120 EGLBoolean
121 _eglGetModesMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
122 EGLModeMESA *modes, EGLint modes_size, EGLint *num_modes)
123 {
124 _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
125 EGLint i;
126
127 if (!scrn) {
128 _eglError(EGL_BAD_SCREEN_MESA, "eglGetModes");
129 return EGL_FALSE;
130 }
131
132 *num_modes = MIN2(modes_size, scrn->NumModes);
133 for (i = 0; i < *num_modes; i++) {
134 modes[i] = scrn->Modes[i].Handle;
135 }
136
137 return EGL_TRUE;
138 }
139
140
141 /**
142 * Query an attribute of a mode.
143 */
144 EGLBoolean
145 _eglGetModeAttribMESA(_EGLDriver *drv, EGLDisplay dpy,
146 EGLModeMESA mode, EGLint attribute, EGLint *value)
147 {
148 _EGLMode *m = _eglLookupMode(dpy, mode);
149
150 switch (attribute) {
151 case EGL_MODE_ID_MESA:
152 *value = m->Handle;
153 break;
154 case EGL_WIDTH:
155 *value = m->Width;
156 break;
157 case EGL_HEIGHT:
158 *value = m->Height;
159 break;
160 #if 0
161 case EGL_DEPTH_MESA:
162 *value = m->Depth;
163 break;
164 #endif
165 case EGL_REFRESH_RATE_MESA:
166 *value = m->RefreshRate;
167 break;
168 #if 0
169 case EGL_STEREO_MESA:
170 *value = m->Stereo;
171 break;
172 #endif
173 default:
174 _eglError(EGL_BAD_ATTRIBUTE, "eglGetModeAttrib");
175 return EGL_FALSE;
176 }
177 return EGL_TRUE;
178 }