5 #include "egldisplay.h"
7 #include "eglcurrent.h"
11 #ifdef EGL_MESA_screen_surface
14 #define MIN2(A, B) (((A) < (B)) ? (A) : (B))
18 * Given an EGLModeMESA handle, return the corresponding _EGLMode object
19 * or null if non-existant.
22 _eglLookupMode(EGLModeMESA mode
, _EGLDisplay
*disp
)
26 if (!disp
|| !disp
->Screens
)
29 /* loop over all screens on the display */
30 for (scrnum
= 0; scrnum
< disp
->Screens
->Size
; scrnum
++) {
31 const _EGLScreen
*scrn
= disp
->Screens
->Elements
[scrnum
];
35 * the mode ids of a screen ranges from scrn->Handle to scrn->Handle +
38 if (mode
>= scrn
->Handle
&&
39 mode
< scrn
->Handle
+ _EGL_SCREEN_MAX_MODES
) {
40 idx
= mode
- scrn
->Handle
;
42 assert(idx
< scrn
->NumModes
&& scrn
->Modes
[idx
].Handle
== mode
);
44 return &scrn
->Modes
[idx
];
53 * Parse the attrib_list to fill in the fields of the given _eglMode
54 * Return EGL_FALSE if any errors, EGL_TRUE otherwise.
57 _eglParseModeAttribs(_EGLMode
*mode
, const EGLint
*attrib_list
)
61 /* init all attribs to EGL_DONT_CARE */
62 mode
->Handle
= EGL_DONT_CARE
;
63 mode
->Width
= EGL_DONT_CARE
;
64 mode
->Height
= EGL_DONT_CARE
;
65 mode
->RefreshRate
= EGL_DONT_CARE
;
66 mode
->Optimal
= EGL_DONT_CARE
;
67 mode
->Interlaced
= EGL_DONT_CARE
;
70 for (i
= 0; attrib_list
&& attrib_list
[i
] != EGL_NONE
; i
++) {
71 switch (attrib_list
[i
]) {
72 case EGL_MODE_ID_MESA
:
73 mode
->Handle
= attrib_list
[++i
];
74 if (mode
->Handle
<= 0) {
75 _eglError(EGL_BAD_PARAMETER
, "eglChooseModeMESA(handle)");
80 mode
->Width
= attrib_list
[++i
];
81 if (mode
->Width
<= 0) {
82 _eglError(EGL_BAD_PARAMETER
, "eglChooseModeMESA(width)");
87 mode
->Height
= attrib_list
[++i
];
88 if (mode
->Height
<= 0) {
89 _eglError(EGL_BAD_PARAMETER
, "eglChooseModeMESA(height)");
93 case EGL_REFRESH_RATE_MESA
:
94 mode
->RefreshRate
= attrib_list
[++i
];
95 if (mode
->RefreshRate
<= 0) {
96 _eglError(EGL_BAD_PARAMETER
, "eglChooseModeMESA(refresh rate)");
100 case EGL_INTERLACED_MESA
:
101 mode
->Interlaced
= attrib_list
[++i
];
102 if (mode
->Interlaced
!= EGL_TRUE
&& mode
->Interlaced
!= EGL_FALSE
) {
103 _eglError(EGL_BAD_PARAMETER
, "eglChooseModeMESA(interlaced)");
107 case EGL_OPTIMAL_MESA
:
108 mode
->Optimal
= attrib_list
[++i
];
109 if (mode
->Optimal
!= EGL_TRUE
&& mode
->Optimal
!= EGL_FALSE
) {
110 _eglError(EGL_BAD_PARAMETER
, "eglChooseModeMESA(optimal)");
115 _eglError(EGL_BAD_ATTRIBUTE
, "eglChooseModeMESA");
124 * Determine if the candidate mode's attributes are at least as good
125 * as the minimal mode's.
126 * \return EGL_TRUE if qualifies, EGL_FALSE otherwise
129 _eglModeQualifies(const _EGLMode
*c
, const _EGLMode
*min
)
131 if (min
->Handle
!= EGL_DONT_CARE
&& c
->Handle
!= min
->Handle
)
133 if (min
->Width
!= EGL_DONT_CARE
&& c
->Width
< min
->Width
)
135 if (min
->Height
!= EGL_DONT_CARE
&& c
->Height
< min
->Height
)
137 if (min
->RefreshRate
!= EGL_DONT_CARE
&& c
->RefreshRate
< min
->RefreshRate
)
139 if (min
->Optimal
!= EGL_DONT_CARE
&& c
->Optimal
!= min
->Optimal
)
141 if (min
->Interlaced
!= EGL_DONT_CARE
&& c
->Interlaced
!= min
->Interlaced
)
149 * Return value of given mode attribute, or -1 if bad attrib.
152 getModeAttrib(const _EGLMode
*m
, EGLint attrib
)
155 case EGL_MODE_ID_MESA
:
161 case EGL_REFRESH_RATE_MESA
:
162 return m
->RefreshRate
;
163 case EGL_OPTIMAL_MESA
:
165 case EGL_INTERLACED_MESA
:
166 return m
->Interlaced
;
178 EGLint Order
; /* SMALLER or LARGER */
181 /* the order of these entries is the priority */
182 static struct sort_info SortInfo
[] = {
183 { EGL_OPTIMAL_MESA
, LARGER
},
184 { EGL_INTERLACED_MESA
, SMALLER
},
185 { EGL_WIDTH
, LARGER
},
186 { EGL_HEIGHT
, LARGER
},
187 { EGL_REFRESH_RATE_MESA
, LARGER
},
188 { EGL_MODE_ID_MESA
, SMALLER
},
194 * Compare modes 'a' and 'b' and return -1 if a belongs before b, or 1 if a
195 * belongs after b, or 0 if they're equal.
199 _eglCompareModes(const void *a
, const void *b
)
201 const _EGLMode
*aMode
= *((const _EGLMode
**) a
);
202 const _EGLMode
*bMode
= *((const _EGLMode
**) b
);
205 for (i
= 0; SortInfo
[i
].Attrib
; i
++) {
206 const EGLint aVal
= getModeAttrib(aMode
, SortInfo
[i
].Attrib
);
207 const EGLint bVal
= getModeAttrib(bMode
, SortInfo
[i
].Attrib
);
212 else if (SortInfo
[i
].Order
== SMALLER
) {
213 return (aVal
< bVal
) ? -1 : 1;
215 else if (SortInfo
[i
].Order
== LARGER
) {
216 return (aVal
> bVal
) ? -1 : 1;
220 /* all attributes identical */
226 * Search for EGLModes which match the given attribute list.
227 * Called via eglChooseModeMESA API function.
230 _eglChooseModeMESA(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLScreen
*scrn
,
231 const EGLint
*attrib_list
, EGLModeMESA
*modes
,
232 EGLint modes_size
, EGLint
*num_modes
)
234 _EGLMode
**modeList
, min
;
237 if (!_eglParseModeAttribs(&min
, attrib_list
)) {
238 /* error code will have been recorded */
242 /* allocate array of mode pointers */
243 modeList
= (_EGLMode
**) malloc(modes_size
* sizeof(_EGLMode
*));
245 _eglError(EGL_BAD_MODE_MESA
, "eglChooseModeMESA(out of memory)");
249 /* make array of pointers to qualifying modes */
250 for (i
= count
= 0; i
< scrn
->NumModes
&& count
< modes_size
; i
++) {
251 if (_eglModeQualifies(scrn
->Modes
+ i
, &min
)) {
252 modeList
[count
++] = scrn
->Modes
+ i
;
256 /* sort array of pointers */
257 qsort(modeList
, count
, sizeof(_EGLMode
*), _eglCompareModes
);
259 /* copy mode handles to output array */
260 for (i
= 0; i
< count
; i
++) {
261 modes
[i
] = modeList
[i
]->Handle
;
274 * Return all possible modes for the given screen. No sorting of results.
275 * Called via eglGetModesMESA() API function.
278 _eglGetModesMESA(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLScreen
*scrn
,
279 EGLModeMESA
*modes
, EGLint modes_size
, EGLint
*num_modes
)
283 *num_modes
= MIN2(scrn
->NumModes
, modes_size
);
284 for (i
= 0; i
< *num_modes
; i
++) {
285 modes
[i
] = scrn
->Modes
[i
].Handle
;
289 /* just return total number of supported modes */
290 *num_modes
= scrn
->NumModes
;
298 * Query an attribute of a mode.
301 _eglGetModeAttribMESA(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
302 _EGLMode
*m
, EGLint attribute
, EGLint
*value
)
306 v
= getModeAttrib(m
, attribute
);
308 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetModeAttribMESA");
317 * Return human-readable string for given mode.
318 * This is the default function called by eglQueryModeStringMESA().
321 _eglQueryModeStringMESA(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLMode
*m
)
327 #endif /* EGL_MESA_screen_surface */