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 # Interface between rendering APIs (OpenGL, OpenGL ES, etc.) and the native windowing system.
19 # Most services of this module are a direct wrapper of the underlying
20 # C library. If a method or class is not documented in Nit, refer to
21 # the official documentation by the Khronos Group at:
22 # http://www.khronos.org/registry/egl/sdk/docs/man/xhtml/
25 ldflags
("-lEGL")@android
34 extern class EGLNativeDisplayType `{ EGLNativeDisplayType `}
35 new from_x11(handle: Pointer) `{ return (EGLNativeDisplayType)handle; `}
38 extern class EGLDisplay `{ EGLDisplay `}
39 new current `{ return eglGetCurrentDisplay(); `}
40 new(handle
: Pointer) `{ return eglGetDisplay(handle); `}
42 fun is_valid: Bool `{ return self != EGL_NO_DISPLAY; `}
44 fun initialize
: Bool `{
45 EGLBoolean r = eglInitialize(self, NULL, NULL);
47 fprintf(stderr, "Unable to eglInitialize");
53 fun major_version
: Int `{
55 eglInitialize(self, &val, NULL);
58 fun minor_version
: Int `{
60 eglInitialize(self, NULL, &val);
65 # Returns all configs supported by the hardware
66 #fun get_configs: nullable Array[EGLConfig] import Array[EGLConfig].with_native `{
68 # Returns some configs compatible with the specified `attributes`
69 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
`{
72 int n_attribs = Array_of_Int_length(attribs);
74 int *c_attribs = malloc(sizeof(int)*n_attribs);
75 for (i = 0; i < n_attribs; i ++) {
76 c_attribs[i] = Array_of_Int__index(attribs, i);
79 // get number of configs
80 EGLBoolean r = eglChooseConfig(self, c_attribs, NULL, 0, &n_configs);
83 EGLDisplay_report_egl_error(self, "failed to get number of available configs.");
84 return null_Array_of_EGLConfig();
85 } else if (n_configs == 0) {
86 EGLDisplay_report_egl_error(self, "no config available.");
87 return null_Array_of_EGLConfig();
90 configs = (EGLConfig*)malloc(sizeof(EGLConfig)*n_configs);
92 r = eglChooseConfig(self, c_attribs, configs, n_configs, &n_configs);
95 EGLDisplay_report_egl_error(self, "failed to load config.");
96 return null_Array_of_EGLConfig();
98 Array_of_EGLConfig array = new_Array_of_EGLConfig();
99 for (i=0; i < n_configs; i++)
100 Array_of_EGLConfig_add(array, configs[i]);
102 return Array_of_EGLConfig_as_nullable(array);
106 # Can be used directly, but it is preferable to use a `EGLConfigAttribs`
107 fun config_attrib
(config
: EGLConfig, attribute
: Int): Int `{
109 EGLBoolean r = eglGetConfigAttrib(self, config, attribute, &val);
116 fun terminate
: Bool `{
117 return eglTerminate(self) == EGL_TRUE;
120 fun create_window_surface
(config
: EGLConfig, native_window
: Pointer, attribs
: Array[Int]): EGLSurface `{
121 EGLSurface surface = eglCreateWindowSurface(self, config, (EGLNativeWindowType)native_window, NULL);
125 # TODO add share_context
126 fun create_context
(config
: EGLConfig): EGLContext `{
127 EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE}; // TODO move out!
128 EGLContext context = eglCreateContext(self, config, EGL_NO_CONTEXT, context_attribs);
132 fun make_current
(draw
, read
: EGLSurface, context
: EGLContext): Bool `{
133 if (eglMakeCurrent(self, draw, read, context) == EGL_FALSE) {
134 fprintf(stderr, "Unable to eglMakeCurrent");
140 # Can be used directly, but it is preferable to use a `EGLSurfaceAttribs`
141 fun query_surface
(surface
: EGLSurface, attribute
: Int): Int `{
143 EGLBoolean r = eglQuerySurface(self, surface, attribute, &val);
150 fun destroy_context
(context
: EGLContext): Bool `{
151 return eglDestroyContext(self, context);
154 fun destroy_surface
(surface
: EGLSurface): Bool `{
155 return eglDestroySurface(self, surface);
158 fun error
: EGLError `{ return eglGetError(); `}
160 # Utility method for easier debugging
161 fun assert_no_egl_error
163 var error = self.error
164 if not error.is_success then
165 print "EGL error: {error}"
170 private fun query_string(name: Int): String import NativeString.to_s `{
171 return NativeString_to_s((char
*)eglQueryString
(self, name
));
174 fun vendor: String do return query_string(0x3053)
176 fun version: String do return query_string(0x3054)
178 fun extensions: Array[String] do return query_string(0x3055).split_with(" ")
180 fun client_apis: Array[String] do return query_string(0x308D).split_with(" ")
182 fun swap_buffers(surface: EGLSurface) `{ eglSwapBuffers(self, surface); `}
185 extern class EGLConfig `{ EGLConfig `}
186 fun attribs(display: EGLDisplay): EGLConfigAttribs do
187 return new EGLConfigAttribs(display, self)
191 extern class EGLSurface `{ EGLSurface `}
192 new current_draw
`{ return eglGetCurrentSurface(EGL_DRAW); `}
193 new current_read `{ return eglGetCurrentSurface(EGL_READ); `}
194 new none
`{ return EGL_NO_SURFACE; `}
196 fun is_ok: Bool `{ return self != EGL_NO_SURFACE; `}
198 fun attribs
(display
: EGLDisplay): EGLSurfaceAttribs do
199 return new EGLSurfaceAttribs(display
, self)
203 extern class EGLContext `{ EGLContext `}
204 new current `{ return eglGetCurrentContext(); `}
205 new none
`{ return EGL_NO_CONTEXT; `}
207 fun is_ok: Bool `{ return self != EGL_NO_CONTEXT; `}
210 # Attributes of a config for a given EGL display
211 class EGLConfigAttribs
212 var display
: EGLDisplay
213 var config
: EGLConfig
215 fun buffer_size
: Int do return display
.config_attrib
(config
, 0x3020)
216 fun alpha_size
: Int do return display
.config_attrib
(config
, 0x3021)
217 fun blue_size
: Int do return display
.config_attrib
(config
, 0x3022)
218 fun green_size
: Int do return display
.config_attrib
(config
, 0x3023)
219 fun red_size
: Int do return display
.config_attrib
(config
, 0x3024)
220 fun depth_size
: Int do return display
.config_attrib
(config
, 0x3025)
221 fun stencil_size
: Int do return display
.config_attrib
(config
, 0x3026)
223 fun native_visual_id
: Int do return display
.config_attrib
(config
, 0x302E)
224 fun native_visual_type
: Int do return display
.config_attrib
(config
, 0x302F)
226 fun caveat
: EGLConfigCaveat do
227 return new EGLConfigCaveat.from_i
(display
.config_attrib
(config
, 0x3027))
230 fun conformant
: EGLConformant do
231 return new EGLConformant.from_i
(display
.config_attrib
(config
, 0x3042))
235 extern class EGLConfigCaveat `{ EGLint `}
236 new from_i(val: Int) `{ return (EGLint)val; `}
237 fun to_i
: Int `{ return self; `}
239 new none `{ return EGL_NONE; `}
240 fun is_none
: Bool `{ return self == EGL_NONE; `}
242 new dont_care `{ return EGL_DONT_CARE; `}
243 fun is_dont_care
: Bool `{ return self == EGL_DONT_CARE; `}
245 new slow `{ return EGL_SLOW_CONFIG; `}
246 fun is_slow
: Bool `{ return self == EGL_SLOW_CONFIG; `}
248 # Obselete since EGL 1.3, use EGL_CONFORMANT instead
249 new non_conformant `{ return EGL_NON_CONFORMANT_CONFIG; `}
250 fun is_non_conformant
: Bool `{ return self == EGL_NON_CONFORMANT_CONFIG; `}
254 if is_none then return "EGL_NONE"
255 if is_dont_care then return "EGL_DONT_CARE"
256 if is_slow then return "EGL_SLOW_CONFIG"
257 if is_non_conformant then return "EGL_NON_CONFORMANT"
258 return "Unknown or invalid value"
262 extern class EGLConformant `{ EGLint `}
263 new `{ return (EGLint)0; `}
264 new from_i(val: Int) `{ return (EGLint)val; `}
265 fun to_i
: Int `{ return self; `}
267 fun opengl: Bool `{ return self & EGL_OPENGL_BIT; `}
268 fun with_opengl
: EGLConformant `{ return self | EGL_OPENGL_BIT; `}
270 fun opengl_es: Bool `{ return self & EGL_OPENGL_ES_BIT; `}
271 fun with_opengl_es
: EGLConformant `{ return self | EGL_OPENGL_ES_BIT; `}
273 fun opengl_es2: Bool `{ return self & EGL_OPENGL_ES2_BIT; `}
274 fun with_opengl_es2
: EGLConformant `{ return self | EGL_OPENGL_ES2_BIT; `}
276 fun openvg: Bool `{ return self & EGL_OPENVG_BIT; `}
277 fun with_openvg
: EGLConformant `{ return self | EGL_OPENVG_BIT; `}
279 fun to_a: Array[String]
281 var features = new Array[String]
282 if opengl then features.add("OpenGL")
283 if opengl_es then features.add("OpenGL ES")
284 if opengl_es2 then features.add("OpenGL ES2")
285 if openvg then features.add("OpenVG")
289 redef fun to_s do return to_a.join(", ")
292 # Attributes of a surface for a given EGL display
293 class EGLSurfaceAttribs
294 var display: EGLDisplay
295 var surface: EGLSurface
297 fun height: Int do return display.query_surface(surface, 0x3056)
298 fun width: Int do return display.query_surface(surface, 0x3057)
299 fun largest_pbuffer: Int do return display.query_surface(surface, 0x3058)
300 fun texture_format: Int do return display.query_surface(surface, 0x3080)
301 fun texture_target: Int do return display.query_surface(surface, 0x3081)
302 fun mipmap_texture: Int do return display.query_surface(surface, 0x3082)
303 fun mipmap_level: Int do return display.query_surface(surface, 0x3083)
304 fun render_buffer: Int do return display.query_surface(surface, 0x3086)
305 fun vg_colorspace: Int do return display.query_surface(surface, 0x3087)
306 fun vg_alpha_format: Int do return display.query_surface(surface, 0x3088)
307 fun horizontal_resolution: Int do return display.query_surface(surface, 0x3090)
308 fun vertical_resolution: Int do return display.query_surface(surface, 0x3091)
309 fun pixel_aspect_ratio: Int do return display.query_surface(surface, 0x3092)
310 fun swap_behavior: Int do return display.query_surface(surface, 0x3093)
311 fun multisample_resolve: Int do return display.query_surface(surface, 0x3099)
314 extern class EGLError `{ EGLint `}
315 fun is_success
: Bool `{ return self == EGL_SUCCESS; `}
317 fun is_not_initialized: Bool `{ return self == EGL_NOT_INITIALIZED; `}
318 fun is_bad_access
: Bool `{ return self == EGL_BAD_ACCESS; `}
319 fun is_bad_alloc: Bool `{ return self == EGL_BAD_ALLOC; `}
320 fun is_bad_attribute
: Bool `{ return self == EGL_BAD_ATTRIBUTE; `}
321 fun is_bad_config: Bool `{ return self == EGL_BAD_CONFIG; `}
322 fun is_bad_context
: Bool `{ return self == EGL_BAD_CONTEXT; `}
323 fun is_bad_current_surface: Bool `{ return self == EGL_BAD_CURRENT_SURFACE; `}
324 fun is_bad_display
: Bool `{ return self == EGL_BAD_DISPLAY; `}
325 fun is_bad_match: Bool `{ return self == EGL_BAD_MATCH; `}
326 fun is_bad_native_pixmap
: Bool `{ return self == EGL_BAD_NATIVE_PIXMAP; `}
327 fun is_bad_native_window: Bool `{ return self == EGL_BAD_NATIVE_WINDOW; `}
328 fun is_bad_parameter
: Bool `{ return self == EGL_BAD_PARAMETER; `}
329 fun is_bad_surface: Bool `{ return self == EGL_BAD_SURFACE; `}
330 fun is_context_lost
: Bool `{ return self == EGL_CONTEXT_LOST; `}
334 if is_not_initialized then return "Not initialized"
335 if is_bad_access then return "Bad access"
336 if is_bad_alloc then return "Bad allocation"
337 if is_bad_attribute then return "Bad attribute"
338 if is_bad_config then return "Bad configuration"
339 if is_bad_context then return "Bad context"
340 if is_bad_current_surface then return "Bad current surface"
341 if is_bad_display then return "Bad display"
342 if is_bad_match then return "Bad match"
343 if is_bad_native_pixmap then return "Bad native pixmap"
344 if is_bad_native_window then return "Bad native window"
345 if is_bad_parameter then return "Bad parameter"
346 if is_bad_surface then return "Bad surface"
347 if is_context_lost then return "Context lost"
348 return "Unknown error" # this is not good
352 extern class EGLContextAttribute `{ EGLint `}
353 new buffer_size
`{ return EGL_BUFFER_SIZE; `}
354 new alpha_size `{ return EGL_ALPHA_SIZE; `}
355 new blue_size
`{ return EGL_BLUE_SIZE; `}
356 new green_size `{ return EGL_GREEN_SIZE; `}
357 new red_size
`{ return EGL_RED_SIZE; `}
358 new depth_size `{ return EGL_DEPTH_SIZE; `}
359 new stencil_size
`{ return EGL_STENCIL_SIZE; `}
360 new config_caveat `{ return EGL_CONFIG_CAVEAT; `}
361 new config_id
`{ return EGL_CONFIG_ID; `}
362 new level `{ return EGL_LEVEL; `}
363 new max_pbuffer_height
`{ return EGL_MAX_PBUFFER_HEIGHT; `}
364 new max_pbuffer_pixels `{ return EGL_MAX_PBUFFER_PIXELS; `}
365 new max_pbuffer_width
`{ return EGL_MAX_PBUFFER_WIDTH; `}
366 new native_renderable `{ return EGL_NATIVE_RENDERABLE; `}
367 new native_visual_id
`{ return EGL_NATIVE_VISUAL_ID; `}
368 new native_visual_type `{ return EGL_NATIVE_VISUAL_TYPE; `}
369 new samples
`{ return EGL_SAMPLES; `}
370 new sample_buffers `{ return EGL_SAMPLE_BUFFERS; `}
371 new surface_type
`{ return EGL_SURFACE_TYPE; `}
372 new transparent_type `{ return EGL_TRANSPARENT_TYPE; `}
373 new transparent_blue_value
`{ return EGL_TRANSPARENT_BLUE_VALUE; `}
374 new transparent_green_value `{ return EGL_TRANSPARENT_GREEN_VALUE; `}
375 new transparent_red_value
`{ return EGL_TRANSPARENT_RED_VALUE; `}
376 new bind_to_texture_rgb `{ return EGL_BIND_TO_TEXTURE_RGB; `}
377 new bind_to_texture_rgba
`{ return EGL_BIND_TO_TEXTURE_RGBA; `}
378 new min_swap_interval `{ return EGL_MIN_SWAP_INTERVAL; `}
379 new max_swap_interval
`{ return EGL_MAX_SWAP_INTERVAL; `}
380 new limunance_size `{ return EGL_LUMINANCE_SIZE; `}
381 new alpha_mask_size
`{ return EGL_ALPHA_MASK_SIZE; `}
382 new color_buffer_type `{ return EGL_COLOR_BUFFER_TYPE; `}
383 new renderable_type
`{ return EGL_RENDERABLE_TYPE; `}
384 new match_native_pixmap `{ return EGL_MATCH_NATIVE_PIXMAP; `}
385 new conformant
`{ return EGL_CONFORMANT; `}
387 # Attrib list terminator
388 new none `{ return EGL_NONE; `}
391 # Utility class to choose an EGLConfig
392 class EGLConfigChooser
393 var array
= new Array[Int]
395 protected var inserted_attribs
= new Array[Int]
397 protected fun insert_attrib_key
(key
: Int)
399 assert not inserted_attribs
.has
(key
) else
400 print
"Duplicate attrib passed to EGLConfigChooser"
405 protected fun insert_attrib_with_val
(key
, val
: Int)
407 insert_attrib_key key
412 insert_attrib_key
0x3038
416 fun surface_type
=(flag
: Int) do insert_attrib_with_val
(0x3033, flag
)
417 fun surface_type_egl
do surface_type
= 4
419 # Set which client rendering APIs are supported
420 fun renderable_type
=(flag
: Int) do insert_attrib_with_val
(0x3040, flag
)
422 # Set EGL as the only supported rendering API
423 fun renderable_type_egl
do renderable_type
= 4
425 fun blue_size
=(size
: Int) do insert_attrib_with_val
(0x3022, size
)
426 fun green_size
=(size
: Int) do insert_attrib_with_val
(0x3023, size
)
427 fun red_size
=(size
: Int) do insert_attrib_with_val
(0x3024, size
)
429 fun buffer_size
=(size
: Int) do insert_attrib_with_val
(0x3020, size
)
430 fun alpha_size
=(size
: Int) do insert_attrib_with_val
(0x3021, size
)
431 fun depth_size
=(size
: Int) do insert_attrib_with_val
(0x3025, size
)
432 fun stencil_size
=(size
: Int) do insert_attrib_with_val
(0x3026, size
)
433 fun sample_buffers
=(size
: Int) do insert_attrib_with_val
(0x3031, size
)
435 fun caveat
=(caveat
: EGLConfigCaveat) do insert_attrib_with_val
(0x3050, caveat
.to_i
)
437 fun conformant
=(conformant
: EGLConformant) do insert_attrib_with_val
(0x3042, conformant
.to_i
)
439 fun choose
(display
: EGLDisplay): nullable Array[EGLConfig]
441 assert closed
else print
"EGLConfigChooser not closed."
442 return display
.choose_configs
(array
)
447 private fun report_egl_error
(cmsg
: NativeString)
450 print
"libEGL error: {msg}"
454 fun egl_bind_opengl_api
: Bool `{ return eglBindAPI(EGL_OPENGL_API); `}
455 fun egl_bind_opengl_es_api: Bool `{ return eglBindAPI(EGL_OPENGL_ES_API); `}
456 fun egl_bind_openvg_api
: Bool `{ return eglBindAPI(EGL_OPENVG_API); `}
458 # Handle to the default display to use with EGL
459 fun egl_default_display: Pointer `{ return EGL_DEFAULT_DISPLAY; `}