1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # EGL is an interface between the rendering APIs OpenGL, OpenGL ES, etc.
18 # and the native windowing system.
20 # Most services of this module are a direct wrapper of the underlying
21 # C library. If a method or class is not documented in Nit, refer to
22 # the official documentation by the Khronos Group at:
23 # http://www.khronos.org/registry/egl/sdk/docs/man/xhtml/
24 module egl
is pkgconfig
("egl")
30 extern class EGLNativeDisplayType `{ EGLNativeDisplayType `}
31 new from_x11(handle: Pointer) `{ return (EGLNativeDisplayType)handle; `}
34 extern class EGLDisplay `{ EGLDisplay `}
35 new current `{ return eglGetCurrentDisplay(); `}
36 new(handle
: Pointer) `{ return eglGetDisplay(handle); `}
38 fun is_valid: Bool `{ return recv != EGL_NO_DISPLAY; `}
40 fun initialize
: Bool `{
41 EGLBoolean r = eglInitialize(recv, NULL, NULL);
43 fprintf(stderr, "Unable to eglInitialize");
49 fun major_version
: Int `{
51 eglInitialize(recv, &val, NULL);
54 fun minor_version
: Int `{
56 eglInitialize(recv, NULL, &val);
61 # Returns all configs supported by the hardware
62 #fun get_configs: nullable Array[EGLConfig] import Array[EGLConfig].with_native `{
64 # Returns some configs compatible with the specified `attributes`
65 fun choose_configs
(attribs
: Array[Int]): nullable Array[EGLConfig] import Array[Int].length
, Array[Int].[], Array[EGLConfig], Array[EGLConfig].add
, Array[EGLConfig].as nullable, report_egl_error
`{
68 int n_attribs = Array_of_Int_length(attribs);
70 int *c_attribs = malloc(sizeof(int)*n_attribs);
71 for (i = 0; i < n_attribs; i ++) {
72 c_attribs[i] = Array_of_Int__index(attribs, i);
75 // get number of configs
76 EGLBoolean r = eglChooseConfig(recv, c_attribs, NULL, 0, &n_configs);
79 EGLDisplay_report_egl_error(recv, "failed to get number of available configs.");
80 return null_Array_of_EGLConfig();
81 } else if (n_configs == 0) {
82 EGLDisplay_report_egl_error(recv, "no config available.");
83 return null_Array_of_EGLConfig();
86 configs = (EGLConfig*)malloc(sizeof(EGLConfig)*n_configs);
88 r = eglChooseConfig(recv, c_attribs, configs, n_configs, &n_configs);
91 EGLDisplay_report_egl_error(recv, "failed to load config.");
92 return null_Array_of_EGLConfig();
94 Array_of_EGLConfig array = new_Array_of_EGLConfig();
95 for (i=0; i < n_configs; i++)
96 Array_of_EGLConfig_add(array, configs[i]);
98 return Array_of_EGLConfig_as_nullable(array);
102 # Can be used directly, but it is preferable to use a `EGLConfigAttribs`
103 fun config_attrib
(config
: EGLConfig, attribute
: Int): Int `{
105 EGLBoolean r = eglGetConfigAttrib(recv, config, attribute, &val);
112 fun terminate
: Bool `{
113 return eglTerminate(recv) == EGL_TRUE;
116 fun create_window_surface
(config
: EGLConfig, native_window
: Pointer, attribs
: Array[Int]): EGLSurface `{
117 EGLSurface surface = eglCreateWindowSurface(recv, config, (EGLNativeWindowType)native_window, NULL);
121 # TODO add share_context
122 fun create_context
(config
: EGLConfig): EGLContext `{
123 EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE}; // TODO move out!
124 EGLContext context = eglCreateContext(recv, config, EGL_NO_CONTEXT, context_attribs);
128 fun make_current
(draw
, read
: EGLSurface, context
: EGLContext): Bool `{
129 if (eglMakeCurrent(recv, draw, read, context) == EGL_FALSE) {
130 fprintf(stderr, "Unable to eglMakeCurrent");
136 # Can be used directly, but it is preferable to use a `EGLSurfaceAttribs`
137 fun query_surface
(surface
: EGLSurface, attribute
: Int): Int `{
139 EGLBoolean r = eglQuerySurface(recv, surface, attribute, &val);
146 fun destroy_context
(context
: EGLContext): Bool `{
147 return eglDestroyContext(recv, context);
150 fun destroy_surface
(surface
: EGLSurface): Bool `{
151 return eglDestroySurface(recv, surface);
154 fun error
: EGLError `{ return eglGetError(); `}
156 # Utility method for easier debugging
157 fun assert_no_egl_error
159 var error = self.error
160 if not error.is_success then
161 print "EGL error: {error}"
166 private fun query_string(name: Int): String import NativeString.to_s `{
167 return NativeString_to_s((char
*)eglQueryString
(recv
, name
));
170 fun vendor: String do return query_string("3053".to_hex)
172 fun version: String do return query_string("3054".to_hex)
174 fun extensions: Array[String] do return query_string("3055".to_hex).split_with(" ")
176 fun client_apis: Array[String] do return query_string("308D".to_hex).split_with(" ")
178 fun swap_buffers(surface: EGLSurface) `{ eglSwapBuffers(recv, surface); `}
181 extern class EGLConfig `{ EGLConfig `}
182 fun attribs(display: EGLDisplay): EGLConfigAttribs do
183 return new EGLConfigAttribs(display, self)
187 extern class EGLSurface `{ EGLSurface `}
188 new current_draw
`{ return eglGetCurrentSurface(EGL_DRAW); `}
189 new current_read `{ return eglGetCurrentSurface(EGL_READ); `}
190 new none
`{ return EGL_NO_SURFACE; `}
192 fun is_ok: Bool `{ return recv != EGL_NO_SURFACE; `}
194 fun attribs
(display
: EGLDisplay): EGLSurfaceAttribs do
195 return new EGLSurfaceAttribs(display
, self)
199 extern class EGLContext `{ EGLContext `}
200 new current `{ return eglGetCurrentContext(); `}
201 new none
`{ return EGL_NO_CONTEXT; `}
203 fun is_ok: Bool `{ return recv != EGL_NO_CONTEXT; `}
206 # Attributes of a config for a given EGL display
207 class EGLConfigAttribs
208 var display
: EGLDisplay
209 var config
: EGLConfig
211 fun buffer_size
: Int do return display
.config_attrib
(config
, "3020".to_hex
)
212 fun alpha_size
: Int do return display
.config_attrib
(config
, "3021".to_hex
)
213 fun blue_size
: Int do return display
.config_attrib
(config
, "3022".to_hex
)
214 fun green_size
: Int do return display
.config_attrib
(config
, "3023".to_hex
)
215 fun red_size
: Int do return display
.config_attrib
(config
, "3024".to_hex
)
216 fun depth_size
: Int do return display
.config_attrib
(config
, "3025".to_hex
)
217 fun stencil_size
: Int do return display
.config_attrib
(config
, "3026".to_hex
)
219 fun native_visual_id
: Int do return display
.config_attrib
(config
, "302E".to_hex
)
220 fun native_visual_type
: Int do return display
.config_attrib
(config
, "302F".to_hex
)
222 fun caveat
: EGLConfigCaveat do
223 return new EGLConfigCaveat.from_i
(display
.config_attrib
(config
, "3027".to_hex
))
226 fun conformant
: EGLConformant do
227 return new EGLConformant.from_i
(display
.config_attrib
(config
, "3042".to_hex
))
231 extern class EGLConfigCaveat `{ EGLint `}
232 new from_i(val: Int) `{ return (EGLint)val; `}
233 fun to_i
: Int `{ return recv; `}
235 new none `{ return EGL_NONE; `}
236 fun is_none
: Bool `{ return recv == EGL_NONE; `}
238 new dont_care `{ return EGL_DONT_CARE; `}
239 fun is_dont_care
: Bool `{ return recv == EGL_DONT_CARE; `}
241 new slow `{ return EGL_SLOW_CONFIG; `}
242 fun is_slow
: Bool `{ return recv == EGL_SLOW_CONFIG; `}
244 # Obselete since EGL 1.3, use EGL_CONFORMANT instead
245 new non_conformant `{ return EGL_NON_CONFORMANT_CONFIG; `}
246 fun is_non_conformant
: Bool `{ return recv == EGL_NON_CONFORMANT_CONFIG; `}
250 if is_none then return "EGL_NONE"
251 if is_dont_care then return "EGL_DONT_CARE"
252 if is_slow then return "EGL_SLOW_CONFIG"
253 if is_non_conformant then return "EGL_NON_CONFORMANT"
254 return "Unknown or invalid value"
258 extern class EGLConformant `{ EGLint `}
259 new `{ return (EGLint)0; `}
260 new from_i(val: Int) `{ return (EGLint)val; `}
261 fun to_i
: Int `{ return recv; `}
263 fun opengl: Bool `{ return recv & EGL_OPENGL_BIT; `}
264 fun with_opengl
: EGLConformant `{ return recv | EGL_OPENGL_BIT; `}
266 fun opengl_es: Bool `{ return recv & EGL_OPENGL_ES_BIT; `}
267 fun with_opengl_es
: EGLConformant `{ return recv | EGL_OPENGL_ES_BIT; `}
269 fun opengl_es2: Bool `{ return recv & EGL_OPENGL_ES2_BIT; `}
270 fun with_opengl_es2
: EGLConformant `{ return recv | EGL_OPENGL_ES2_BIT; `}
272 fun openvg: Bool `{ return recv & EGL_OPENVG_BIT; `}
273 fun with_openvg
: EGLConformant `{ return recv | EGL_OPENVG_BIT; `}
275 fun to_a: Array[String]
277 var features = new Array[String]
278 if opengl then features.add("OpenGL")
279 if opengl_es then features.add("OpenGL ES")
280 if opengl_es2 then features.add("OpenGL ES2")
281 if openvg then features.add("OpenVG")
285 redef fun to_s do return to_a.join(", ")
288 # Attributes of a surface for a given EGL display
289 class EGLSurfaceAttribs
290 var display: EGLDisplay
291 var surface: EGLSurface
293 fun height: Int do return display.query_surface(surface, "3056".to_hex)
294 fun width: Int do return display.query_surface(surface, "3057".to_hex)
295 fun largest_pbuffer: Int do return display.query_surface(surface, "3058".to_hex)
296 fun texture_format: Int do return display.query_surface(surface, "3080".to_hex)
297 fun texture_target: Int do return display.query_surface(surface, "3081".to_hex)
298 fun mipmap_texture: Int do return display.query_surface(surface, "3082".to_hex)
299 fun mipmap_level: Int do return display.query_surface(surface, "3083".to_hex)
300 fun render_buffer: Int do return display.query_surface(surface, "3086".to_hex)
301 fun vg_colorspace: Int do return display.query_surface(surface, "3087".to_hex)
302 fun vg_alpha_format: Int do return display.query_surface(surface, "3088".to_hex)
303 fun horizontal_resolution: Int do return display.query_surface(surface, "3090".to_hex)
304 fun vertical_resolution: Int do return display.query_surface(surface, "3091".to_hex)
305 fun pixel_aspect_ratio: Int do return display.query_surface(surface, "3092".to_hex)
306 fun swap_behavior: Int do return display.query_surface(surface, "3093".to_hex)
307 fun multisample_resolve: Int do return display.query_surface(surface, "3099".to_hex)
310 extern class EGLError `{ EGLint `}
311 fun is_success
: Bool `{ return recv == EGL_SUCCESS; `}
313 fun is_not_initialized: Bool `{ return recv == EGL_NOT_INITIALIZED; `}
314 fun is_bad_access
: Bool `{ return recv == EGL_BAD_ACCESS; `}
315 fun is_bad_alloc: Bool `{ return recv == EGL_BAD_ALLOC; `}
316 fun is_bad_attribute
: Bool `{ return recv == EGL_BAD_ATTRIBUTE; `}
317 fun is_bad_config: Bool `{ return recv == EGL_BAD_CONFIG; `}
318 fun is_bad_context
: Bool `{ return recv == EGL_BAD_CONTEXT; `}
319 fun is_bad_current_surface: Bool `{ return recv == EGL_BAD_CURRENT_SURFACE; `}
320 fun is_bad_display
: Bool `{ return recv == EGL_BAD_DISPLAY; `}
321 fun is_bad_match: Bool `{ return recv == EGL_BAD_MATCH; `}
322 fun is_bad_native_pixmap
: Bool `{ return recv == EGL_BAD_NATIVE_PIXMAP; `}
323 fun is_bad_native_window: Bool `{ return recv == EGL_BAD_NATIVE_WINDOW; `}
324 fun is_bad_parameter
: Bool `{ return recv == EGL_BAD_PARAMETER; `}
325 fun is_bad_surface: Bool `{ return recv == EGL_BAD_SURFACE; `}
326 fun is_context_lost
: Bool `{ return recv == EGL_CONTEXT_LOST; `}
330 if is_not_initialized then return "Not initialized"
331 if is_bad_access then return "Bad access"
332 if is_bad_alloc then return "Bad allocation"
333 if is_bad_attribute then return "Bad attribute"
334 if is_bad_config then return "Bad configuration"
335 if is_bad_context then return "Bad context"
336 if is_bad_current_surface then return "Bad current surface"
337 if is_bad_display then return "Bad display"
338 if is_bad_match then return "Bad match"
339 if is_bad_native_pixmap then return "Bad native pixmap"
340 if is_bad_native_window then return "Bad native window"
341 if is_bad_parameter then return "Bad parameter"
342 if is_bad_surface then return "Bad surface"
343 if is_context_lost then return "Context lost"
344 return "Unknown error" # this is not good
348 extern class EGLContextAttribute `{ EGLint `}
349 new buffer_size
`{ return EGL_BUFFER_SIZE; `}
350 new alpha_size `{ return EGL_ALPHA_SIZE; `}
351 new blue_size
`{ return EGL_BLUE_SIZE; `}
352 new green_size `{ return EGL_GREEN_SIZE; `}
353 new red_size
`{ return EGL_RED_SIZE; `}
354 new depth_size `{ return EGL_DEPTH_SIZE; `}
355 new stencil_size
`{ return EGL_STENCIL_SIZE; `}
356 new config_caveat `{ return EGL_CONFIG_CAVEAT; `}
357 new config_id
`{ return EGL_CONFIG_ID; `}
358 new level `{ return EGL_LEVEL; `}
359 new max_pbuffer_height
`{ return EGL_MAX_PBUFFER_HEIGHT; `}
360 new max_pbuffer_pixels `{ return EGL_MAX_PBUFFER_PIXELS; `}
361 new max_pbuffer_width
`{ return EGL_MAX_PBUFFER_WIDTH; `}
362 new native_renderable `{ return EGL_NATIVE_RENDERABLE; `}
363 new native_visual_id
`{ return EGL_NATIVE_VISUAL_ID; `}
364 new native_visual_type `{ return EGL_NATIVE_VISUAL_TYPE; `}
365 new samples
`{ return EGL_SAMPLES; `}
366 new sample_buffers `{ return EGL_SAMPLE_BUFFERS; `}
367 new surface_type
`{ return EGL_SURFACE_TYPE; `}
368 new transparent_type `{ return EGL_TRANSPARENT_TYPE; `}
369 new transparent_blue_value
`{ return EGL_TRANSPARENT_BLUE_VALUE; `}
370 new transparent_green_value `{ return EGL_TRANSPARENT_GREEN_VALUE; `}
371 new transparent_red_value
`{ return EGL_TRANSPARENT_RED_VALUE; `}
372 new bind_to_texture_rgb `{ return EGL_BIND_TO_TEXTURE_RGB; `}
373 new bind_to_texture_rgba
`{ return EGL_BIND_TO_TEXTURE_RGBA; `}
374 new min_swap_interval `{ return EGL_MIN_SWAP_INTERVAL; `}
375 new max_swap_interval
`{ return EGL_MAX_SWAP_INTERVAL; `}
376 new limunance_size `{ return EGL_LUMINANCE_SIZE; `}
377 new alpha_mask_size
`{ return EGL_ALPHA_MASK_SIZE; `}
378 new color_buffer_type `{ return EGL_COLOR_BUFFER_TYPE; `}
379 new renderable_type
`{ return EGL_RENDERABLE_TYPE; `}
380 new match_native_pixmap `{ return EGL_MATCH_NATIVE_PIXMAP; `}
381 new conformant
`{ return EGL_CONFORMANT; `}
383 # Attrib list terminator
384 new none `{ return EGL_NONE; `}
387 # Utility class to choose an EGLConfig
388 class EGLConfigChooser
389 var array
= new Array[Int]
391 protected var inserted_attribs
= new Array[Int]
393 protected fun insert_attrib_key
(key
: Int)
395 assert not inserted_attribs
.has
(key
) else
396 print
"Duplicate attrib passed to EGLConfigChooser"
401 protected fun insert_attrib_with_val
(key
, val
: Int)
403 insert_attrib_key key
408 insert_attrib_key
"3038".to_hex
412 fun surface_type
=(flag
: Int) do insert_attrib_with_val
("3033".to_hex
, flag
)
413 fun surface_type_egl
do surface_type
= 4
415 fun blue_size
=(size
: Int) do insert_attrib_with_val
("3022".to_hex
, size
)
416 fun green_size
=(size
: Int) do insert_attrib_with_val
("3023".to_hex
, size
)
417 fun red_size
=(size
: Int) do insert_attrib_with_val
("3024".to_hex
, size
)
419 fun buffer_size
=(size
: Int) do insert_attrib_with_val
("3020".to_hex
, size
)
420 fun alpha_size
=(size
: Int) do insert_attrib_with_val
("3021".to_hex
, size
)
421 fun depth_size
=(size
: Int) do insert_attrib_with_val
("3025".to_hex
, size
)
422 fun stencil_size
=(size
: Int) do insert_attrib_with_val
("3026".to_hex
, size
)
423 fun sample_buffers
=(size
: Int) do insert_attrib_with_val
("3031".to_hex
, size
)
425 fun caveat
=(caveat
: EGLConfigCaveat) do insert_attrib_with_val
("3050".to_hex
, caveat
.to_i
)
427 fun conformant
=(conformant
: EGLConformant) do insert_attrib_with_val
("3042".to_hex
, conformant
.to_i
)
429 fun choose
(display
: EGLDisplay): nullable Array[EGLConfig]
431 assert closed
else print
"EGLConfigChooser not closed."
432 return display
.choose_configs
(array
)
437 private fun report_egl_error
(cmsg
: NativeString)
440 print
"libEGL error: {msg}"
444 protected fun egl_bind_opengl_api
: Bool `{ return eglBindAPI(EGL_OPENGL_API); `}
445 protected fun egl_bind_opengl_es_api: Bool `{ return eglBindAPI(EGL_OPENGL_ES_API); `}
446 protected fun egl_bind_openvg_api
: Bool `{ return eglBindAPI(EGL_OPENVG_API); `}