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/
26 ldflags
("-lEGL")@android
35 extern class EGLNativeDisplayType `{ EGLNativeDisplayType `}
36 new from_x11(handle: Pointer) `{ return (EGLNativeDisplayType)handle; `}
39 extern class EGLDisplay `{ EGLDisplay `}
40 new current `{ return eglGetCurrentDisplay(); `}
41 new(handle
: Pointer) `{ return eglGetDisplay(handle); `}
43 fun is_valid: Bool `{ return recv != EGL_NO_DISPLAY; `}
45 fun initialize
: Bool `{
46 EGLBoolean r = eglInitialize(recv, NULL, NULL);
48 fprintf(stderr, "Unable to eglInitialize");
54 fun major_version
: Int `{
56 eglInitialize(recv, &val, NULL);
59 fun minor_version
: Int `{
61 eglInitialize(recv, NULL, &val);
66 # Returns all configs supported by the hardware
67 #fun get_configs: nullable Array[EGLConfig] import Array[EGLConfig].with_native `{
69 # Returns some configs compatible with the specified `attributes`
70 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
`{
73 int n_attribs = Array_of_Int_length(attribs);
75 int *c_attribs = malloc(sizeof(int)*n_attribs);
76 for (i = 0; i < n_attribs; i ++) {
77 c_attribs[i] = Array_of_Int__index(attribs, i);
80 // get number of configs
81 EGLBoolean r = eglChooseConfig(recv, c_attribs, NULL, 0, &n_configs);
84 EGLDisplay_report_egl_error(recv, "failed to get number of available configs.");
85 return null_Array_of_EGLConfig();
86 } else if (n_configs == 0) {
87 EGLDisplay_report_egl_error(recv, "no config available.");
88 return null_Array_of_EGLConfig();
91 configs = (EGLConfig*)malloc(sizeof(EGLConfig)*n_configs);
93 r = eglChooseConfig(recv, c_attribs, configs, n_configs, &n_configs);
96 EGLDisplay_report_egl_error(recv, "failed to load config.");
97 return null_Array_of_EGLConfig();
99 Array_of_EGLConfig array = new_Array_of_EGLConfig();
100 for (i=0; i < n_configs; i++)
101 Array_of_EGLConfig_add(array, configs[i]);
103 return Array_of_EGLConfig_as_nullable(array);
107 # Can be used directly, but it is preferable to use a `EGLConfigAttribs`
108 fun config_attrib
(config
: EGLConfig, attribute
: Int): Int `{
110 EGLBoolean r = eglGetConfigAttrib(recv, config, attribute, &val);
117 fun terminate
: Bool `{
118 return eglTerminate(recv) == EGL_TRUE;
121 fun create_window_surface
(config
: EGLConfig, native_window
: Pointer, attribs
: Array[Int]): EGLSurface `{
122 EGLSurface surface = eglCreateWindowSurface(recv, config, (EGLNativeWindowType)native_window, NULL);
126 # TODO add share_context
127 fun create_context
(config
: EGLConfig): EGLContext `{
128 EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE}; // TODO move out!
129 EGLContext context = eglCreateContext(recv, config, EGL_NO_CONTEXT, context_attribs);
133 fun make_current
(draw
, read
: EGLSurface, context
: EGLContext): Bool `{
134 if (eglMakeCurrent(recv, draw, read, context) == EGL_FALSE) {
135 fprintf(stderr, "Unable to eglMakeCurrent");
141 # Can be used directly, but it is preferable to use a `EGLSurfaceAttribs`
142 fun query_surface
(surface
: EGLSurface, attribute
: Int): Int `{
144 EGLBoolean r = eglQuerySurface(recv, surface, attribute, &val);
151 fun destroy_context
(context
: EGLContext): Bool `{
152 return eglDestroyContext(recv, context);
155 fun destroy_surface
(surface
: EGLSurface): Bool `{
156 return eglDestroySurface(recv, surface);
159 fun error
: EGLError `{ return eglGetError(); `}
161 # Utility method for easier debugging
162 fun assert_no_egl_error
164 var error = self.error
165 if not error.is_success then
166 print "EGL error: {error}"
171 private fun query_string(name: Int): String import NativeString.to_s `{
172 return NativeString_to_s((char
*)eglQueryString
(recv
, name
));
175 fun vendor: String do return query_string(0x3053)
177 fun version: String do return query_string(0x3054)
179 fun extensions: Array[String] do return query_string(0x3055).split_with(" ")
181 fun client_apis: Array[String] do return query_string(0x308D).split_with(" ")
183 fun swap_buffers(surface: EGLSurface) `{ eglSwapBuffers(recv, surface); `}
186 extern class EGLConfig `{ EGLConfig `}
187 fun attribs(display: EGLDisplay): EGLConfigAttribs do
188 return new EGLConfigAttribs(display, self)
192 extern class EGLSurface `{ EGLSurface `}
193 new current_draw
`{ return eglGetCurrentSurface(EGL_DRAW); `}
194 new current_read `{ return eglGetCurrentSurface(EGL_READ); `}
195 new none
`{ return EGL_NO_SURFACE; `}
197 fun is_ok: Bool `{ return recv != EGL_NO_SURFACE; `}
199 fun attribs
(display
: EGLDisplay): EGLSurfaceAttribs do
200 return new EGLSurfaceAttribs(display
, self)
204 extern class EGLContext `{ EGLContext `}
205 new current `{ return eglGetCurrentContext(); `}
206 new none
`{ return EGL_NO_CONTEXT; `}
208 fun is_ok: Bool `{ return recv != EGL_NO_CONTEXT; `}
211 # Attributes of a config for a given EGL display
212 class EGLConfigAttribs
213 var display
: EGLDisplay
214 var config
: EGLConfig
216 fun buffer_size
: Int do return display
.config_attrib
(config
, 0x3020)
217 fun alpha_size
: Int do return display
.config_attrib
(config
, 0x3021)
218 fun blue_size
: Int do return display
.config_attrib
(config
, 0x3022)
219 fun green_size
: Int do return display
.config_attrib
(config
, 0x3023)
220 fun red_size
: Int do return display
.config_attrib
(config
, 0x3024)
221 fun depth_size
: Int do return display
.config_attrib
(config
, 0x3025)
222 fun stencil_size
: Int do return display
.config_attrib
(config
, 0x3026)
224 fun native_visual_id
: Int do return display
.config_attrib
(config
, 0x302E)
225 fun native_visual_type
: Int do return display
.config_attrib
(config
, 0x302F)
227 fun caveat
: EGLConfigCaveat do
228 return new EGLConfigCaveat.from_i
(display
.config_attrib
(config
, 0x3027))
231 fun conformant
: EGLConformant do
232 return new EGLConformant.from_i
(display
.config_attrib
(config
, 0x3042))
236 extern class EGLConfigCaveat `{ EGLint `}
237 new from_i(val: Int) `{ return (EGLint)val; `}
238 fun to_i
: Int `{ return recv; `}
240 new none `{ return EGL_NONE; `}
241 fun is_none
: Bool `{ return recv == EGL_NONE; `}
243 new dont_care `{ return EGL_DONT_CARE; `}
244 fun is_dont_care
: Bool `{ return recv == EGL_DONT_CARE; `}
246 new slow `{ return EGL_SLOW_CONFIG; `}
247 fun is_slow
: Bool `{ return recv == EGL_SLOW_CONFIG; `}
249 # Obselete since EGL 1.3, use EGL_CONFORMANT instead
250 new non_conformant `{ return EGL_NON_CONFORMANT_CONFIG; `}
251 fun is_non_conformant
: Bool `{ return recv == EGL_NON_CONFORMANT_CONFIG; `}
255 if is_none then return "EGL_NONE"
256 if is_dont_care then return "EGL_DONT_CARE"
257 if is_slow then return "EGL_SLOW_CONFIG"
258 if is_non_conformant then return "EGL_NON_CONFORMANT"
259 return "Unknown or invalid value"
263 extern class EGLConformant `{ EGLint `}
264 new `{ return (EGLint)0; `}
265 new from_i(val: Int) `{ return (EGLint)val; `}
266 fun to_i
: Int `{ return recv; `}
268 fun opengl: Bool `{ return recv & EGL_OPENGL_BIT; `}
269 fun with_opengl
: EGLConformant `{ return recv | EGL_OPENGL_BIT; `}
271 fun opengl_es: Bool `{ return recv & EGL_OPENGL_ES_BIT; `}
272 fun with_opengl_es
: EGLConformant `{ return recv | EGL_OPENGL_ES_BIT; `}
274 fun opengl_es2: Bool `{ return recv & EGL_OPENGL_ES2_BIT; `}
275 fun with_opengl_es2
: EGLConformant `{ return recv | EGL_OPENGL_ES2_BIT; `}
277 fun openvg: Bool `{ return recv & EGL_OPENVG_BIT; `}
278 fun with_openvg
: EGLConformant `{ return recv | EGL_OPENVG_BIT; `}
280 fun to_a: Array[String]
282 var features = new Array[String]
283 if opengl then features.add("OpenGL")
284 if opengl_es then features.add("OpenGL ES")
285 if opengl_es2 then features.add("OpenGL ES2")
286 if openvg then features.add("OpenVG")
290 redef fun to_s do return to_a.join(", ")
293 # Attributes of a surface for a given EGL display
294 class EGLSurfaceAttribs
295 var display: EGLDisplay
296 var surface: EGLSurface
298 fun height: Int do return display.query_surface(surface, 0x3056)
299 fun width: Int do return display.query_surface(surface, 0x3057)
300 fun largest_pbuffer: Int do return display.query_surface(surface, 0x3058)
301 fun texture_format: Int do return display.query_surface(surface, 0x3080)
302 fun texture_target: Int do return display.query_surface(surface, 0x3081)
303 fun mipmap_texture: Int do return display.query_surface(surface, 0x3082)
304 fun mipmap_level: Int do return display.query_surface(surface, 0x3083)
305 fun render_buffer: Int do return display.query_surface(surface, 0x3086)
306 fun vg_colorspace: Int do return display.query_surface(surface, 0x3087)
307 fun vg_alpha_format: Int do return display.query_surface(surface, 0x3088)
308 fun horizontal_resolution: Int do return display.query_surface(surface, 0x3090)
309 fun vertical_resolution: Int do return display.query_surface(surface, 0x3091)
310 fun pixel_aspect_ratio: Int do return display.query_surface(surface, 0x3092)
311 fun swap_behavior: Int do return display.query_surface(surface, 0x3093)
312 fun multisample_resolve: Int do return display.query_surface(surface, 0x3099)
315 extern class EGLError `{ EGLint `}
316 fun is_success
: Bool `{ return recv == EGL_SUCCESS; `}
318 fun is_not_initialized: Bool `{ return recv == EGL_NOT_INITIALIZED; `}
319 fun is_bad_access
: Bool `{ return recv == EGL_BAD_ACCESS; `}
320 fun is_bad_alloc: Bool `{ return recv == EGL_BAD_ALLOC; `}
321 fun is_bad_attribute
: Bool `{ return recv == EGL_BAD_ATTRIBUTE; `}
322 fun is_bad_config: Bool `{ return recv == EGL_BAD_CONFIG; `}
323 fun is_bad_context
: Bool `{ return recv == EGL_BAD_CONTEXT; `}
324 fun is_bad_current_surface: Bool `{ return recv == EGL_BAD_CURRENT_SURFACE; `}
325 fun is_bad_display
: Bool `{ return recv == EGL_BAD_DISPLAY; `}
326 fun is_bad_match: Bool `{ return recv == EGL_BAD_MATCH; `}
327 fun is_bad_native_pixmap
: Bool `{ return recv == EGL_BAD_NATIVE_PIXMAP; `}
328 fun is_bad_native_window: Bool `{ return recv == EGL_BAD_NATIVE_WINDOW; `}
329 fun is_bad_parameter
: Bool `{ return recv == EGL_BAD_PARAMETER; `}
330 fun is_bad_surface: Bool `{ return recv == EGL_BAD_SURFACE; `}
331 fun is_context_lost
: Bool `{ return recv == EGL_CONTEXT_LOST; `}
335 if is_not_initialized then return "Not initialized"
336 if is_bad_access then return "Bad access"
337 if is_bad_alloc then return "Bad allocation"
338 if is_bad_attribute then return "Bad attribute"
339 if is_bad_config then return "Bad configuration"
340 if is_bad_context then return "Bad context"
341 if is_bad_current_surface then return "Bad current surface"
342 if is_bad_display then return "Bad display"
343 if is_bad_match then return "Bad match"
344 if is_bad_native_pixmap then return "Bad native pixmap"
345 if is_bad_native_window then return "Bad native window"
346 if is_bad_parameter then return "Bad parameter"
347 if is_bad_surface then return "Bad surface"
348 if is_context_lost then return "Context lost"
349 return "Unknown error" # this is not good
353 extern class EGLContextAttribute `{ EGLint `}
354 new buffer_size
`{ return EGL_BUFFER_SIZE; `}
355 new alpha_size `{ return EGL_ALPHA_SIZE; `}
356 new blue_size
`{ return EGL_BLUE_SIZE; `}
357 new green_size `{ return EGL_GREEN_SIZE; `}
358 new red_size
`{ return EGL_RED_SIZE; `}
359 new depth_size `{ return EGL_DEPTH_SIZE; `}
360 new stencil_size
`{ return EGL_STENCIL_SIZE; `}
361 new config_caveat `{ return EGL_CONFIG_CAVEAT; `}
362 new config_id
`{ return EGL_CONFIG_ID; `}
363 new level `{ return EGL_LEVEL; `}
364 new max_pbuffer_height
`{ return EGL_MAX_PBUFFER_HEIGHT; `}
365 new max_pbuffer_pixels `{ return EGL_MAX_PBUFFER_PIXELS; `}
366 new max_pbuffer_width
`{ return EGL_MAX_PBUFFER_WIDTH; `}
367 new native_renderable `{ return EGL_NATIVE_RENDERABLE; `}
368 new native_visual_id
`{ return EGL_NATIVE_VISUAL_ID; `}
369 new native_visual_type `{ return EGL_NATIVE_VISUAL_TYPE; `}
370 new samples
`{ return EGL_SAMPLES; `}
371 new sample_buffers `{ return EGL_SAMPLE_BUFFERS; `}
372 new surface_type
`{ return EGL_SURFACE_TYPE; `}
373 new transparent_type `{ return EGL_TRANSPARENT_TYPE; `}
374 new transparent_blue_value
`{ return EGL_TRANSPARENT_BLUE_VALUE; `}
375 new transparent_green_value `{ return EGL_TRANSPARENT_GREEN_VALUE; `}
376 new transparent_red_value
`{ return EGL_TRANSPARENT_RED_VALUE; `}
377 new bind_to_texture_rgb `{ return EGL_BIND_TO_TEXTURE_RGB; `}
378 new bind_to_texture_rgba
`{ return EGL_BIND_TO_TEXTURE_RGBA; `}
379 new min_swap_interval `{ return EGL_MIN_SWAP_INTERVAL; `}
380 new max_swap_interval
`{ return EGL_MAX_SWAP_INTERVAL; `}
381 new limunance_size `{ return EGL_LUMINANCE_SIZE; `}
382 new alpha_mask_size
`{ return EGL_ALPHA_MASK_SIZE; `}
383 new color_buffer_type `{ return EGL_COLOR_BUFFER_TYPE; `}
384 new renderable_type
`{ return EGL_RENDERABLE_TYPE; `}
385 new match_native_pixmap `{ return EGL_MATCH_NATIVE_PIXMAP; `}
386 new conformant
`{ return EGL_CONFORMANT; `}
388 # Attrib list terminator
389 new none `{ return EGL_NONE; `}
392 # Utility class to choose an EGLConfig
393 class EGLConfigChooser
394 var array
= new Array[Int]
396 protected var inserted_attribs
= new Array[Int]
398 protected fun insert_attrib_key
(key
: Int)
400 assert not inserted_attribs
.has
(key
) else
401 print
"Duplicate attrib passed to EGLConfigChooser"
406 protected fun insert_attrib_with_val
(key
, val
: Int)
408 insert_attrib_key key
413 insert_attrib_key
0x3038
417 fun surface_type
=(flag
: Int) do insert_attrib_with_val
(0x3033, flag
)
418 fun surface_type_egl
do surface_type
= 4
420 # Set which client rendering APIs are supported
421 fun renderable_type
=(flag
: Int) do insert_attrib_with_val
(0x3040, flag
)
423 # Set EGL as the only supported rendering API
424 fun renderable_type_egl
do renderable_type
= 4
426 fun blue_size
=(size
: Int) do insert_attrib_with_val
(0x3022, size
)
427 fun green_size
=(size
: Int) do insert_attrib_with_val
(0x3023, size
)
428 fun red_size
=(size
: Int) do insert_attrib_with_val
(0x3024, size
)
430 fun buffer_size
=(size
: Int) do insert_attrib_with_val
(0x3020, size
)
431 fun alpha_size
=(size
: Int) do insert_attrib_with_val
(0x3021, size
)
432 fun depth_size
=(size
: Int) do insert_attrib_with_val
(0x3025, size
)
433 fun stencil_size
=(size
: Int) do insert_attrib_with_val
(0x3026, size
)
434 fun sample_buffers
=(size
: Int) do insert_attrib_with_val
(0x3031, size
)
436 fun caveat
=(caveat
: EGLConfigCaveat) do insert_attrib_with_val
(0x3050, caveat
.to_i
)
438 fun conformant
=(conformant
: EGLConformant) do insert_attrib_with_val
(0x3042, conformant
.to_i
)
440 fun choose
(display
: EGLDisplay): nullable Array[EGLConfig]
442 assert closed
else print
"EGLConfigChooser not closed."
443 return display
.choose_configs
(array
)
448 private fun report_egl_error
(cmsg
: NativeString)
451 print
"libEGL error: {msg}"
455 protected fun egl_bind_opengl_api
: Bool `{ return eglBindAPI(EGL_OPENGL_API); `}
456 protected fun egl_bind_opengl_es_api: Bool `{ return eglBindAPI(EGL_OPENGL_ES_API); `}
457 protected fun egl_bind_openvg_api
: Bool `{ return eglBindAPI(EGL_OPENVG_API); `}