69df8e5eae68ff06feadec0b24686cb5e1b61fbd
[nit.git] / lib / egl.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Interface between rendering APIs (OpenGL, OpenGL ES, etc.) and the native windowing system.
18 #
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/
23 module egl is
24 pkgconfig
25 ldflags("-lEGL")@android
26 end
27
28 import android::aware
29
30 in "C Header" `{
31 #include <EGL/egl.h>
32 `}
33
34 extern class EGLNativeDisplayType `{ EGLNativeDisplayType `}
35 new from_x11(handle: Pointer) `{ return (EGLNativeDisplayType)handle; `}
36 end
37
38 extern class EGLDisplay `{ EGLDisplay `}
39 new current `{ return eglGetCurrentDisplay(); `}
40 new(handle: Pointer) `{ return eglGetDisplay(handle); `}
41
42 fun is_valid: Bool `{ return self != EGL_NO_DISPLAY; `}
43
44 fun initialize: Bool `{
45 EGLBoolean r = eglInitialize(self, NULL, NULL);
46 if (r == EGL_FALSE) {
47 fprintf(stderr, "Unable to eglInitialize");
48 return 0;
49 }
50 return 1;
51 `}
52
53 fun major_version: Int `{
54 EGLint val;
55 eglInitialize(self, &val, NULL);
56 return val;
57 `}
58 fun minor_version: Int `{
59 EGLint val;
60 eglInitialize(self, NULL, &val);
61 return val;
62 `}
63
64 # TODO if useful
65 # Returns all configs supported by the hardware
66 #fun get_configs: nullable Array[EGLConfig] import Array[EGLConfig].with_native `{
67
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 `{
70 EGLConfig *configs;
71 int n_configs;
72 int n_attribs = Array_of_Int_length(attribs);
73 int i;
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);
77 }
78
79 // get number of configs
80 EGLBoolean r = eglChooseConfig(self, c_attribs, NULL, 0, &n_configs);
81
82 if (r == EGL_FALSE) {
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();
88 }
89
90 configs = (EGLConfig*)malloc(sizeof(EGLConfig)*n_configs);
91
92 r = eglChooseConfig(self, c_attribs, configs, n_configs, &n_configs);
93
94 if (r == EGL_FALSE) {
95 EGLDisplay_report_egl_error(self, "failed to load config.");
96 return null_Array_of_EGLConfig();
97 } else {
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]);
101
102 return Array_of_EGLConfig_as_nullable(array);
103 }
104 `}
105
106 # Can be used directly, but it is preferable to use a `EGLConfigAttribs`
107 fun config_attrib(config: EGLConfig, attribute: Int): Int `{
108 EGLint val;
109 EGLBoolean r = eglGetConfigAttrib(self, config, attribute, &val);
110 if (r == EGL_FALSE)
111 return -1;
112 else
113 return val;
114 `}
115
116 fun terminate: Bool `{
117 return eglTerminate(self) == EGL_TRUE;
118 `}
119
120 fun create_window_surface(config: EGLConfig, native_window: Pointer, attribs: Array[Int]): EGLSurface `{
121 EGLSurface surface = eglCreateWindowSurface(self, config, (EGLNativeWindowType)native_window, NULL);
122 return surface;
123 `}
124
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);
129 return context;
130 `}
131
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");
135 return 0;
136 }
137 return 1;
138 `}
139
140 # Can be used directly, but it is preferable to use a `EGLSurfaceAttribs`
141 fun query_surface(surface: EGLSurface, attribute: Int): Int `{
142 int val;
143 EGLBoolean r = eglQuerySurface(self, surface, attribute, &val);
144 if (r == EGL_FALSE)
145 return -1;
146 else
147 return val;
148 `}
149
150 fun destroy_context(context: EGLContext): Bool `{
151 return eglDestroyContext(self, context);
152 `}
153
154 fun destroy_surface(surface: EGLSurface): Bool `{
155 return eglDestroySurface(self, surface);
156 `}
157
158 fun error: EGLError `{ return eglGetError(); `}
159
160 # Utility method for easier debugging
161 fun assert_no_egl_error
162 do
163 var error = self.error
164 if not error.is_success then
165 print "EGL error: {error}"
166 abort
167 end
168 end
169
170 private fun query_string(name: Int): String import NativeString.to_s `{
171 return NativeString_to_s((char *)eglQueryString(self, name));
172 `}
173
174 fun vendor: String do return query_string(0x3053)
175
176 fun version: String do return query_string(0x3054)
177
178 fun extensions: Array[String] do return query_string(0x3055).split_with(" ")
179
180 fun client_apis: Array[String] do return query_string(0x308D).split_with(" ")
181
182 fun swap_buffers(surface: EGLSurface) `{ eglSwapBuffers(self, surface); `}
183 end
184
185 extern class EGLConfig `{ EGLConfig `}
186 fun attribs(display: EGLDisplay): EGLConfigAttribs do
187 return new EGLConfigAttribs(display, self)
188 end
189 end
190
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; `}
195
196 fun is_ok: Bool `{ return self != EGL_NO_SURFACE; `}
197
198 fun attribs(display: EGLDisplay): EGLSurfaceAttribs do
199 return new EGLSurfaceAttribs(display, self)
200 end
201 end
202
203 extern class EGLContext `{ EGLContext `}
204 new current `{ return eglGetCurrentContext(); `}
205 new none `{ return EGL_NO_CONTEXT; `}
206
207 fun is_ok: Bool `{ return self != EGL_NO_CONTEXT; `}
208 end
209
210 # Attributes of a config for a given EGL display
211 class EGLConfigAttribs
212 var display: EGLDisplay
213 var config: EGLConfig
214
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)
222
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)
225
226 fun caveat: EGLConfigCaveat do
227 return new EGLConfigCaveat.from_i(display.config_attrib(config, 0x3027))
228 end
229
230 fun conformant: EGLConformant do
231 return new EGLConformant.from_i(display.config_attrib(config, 0x3042))
232 end
233 end
234
235 extern class EGLConfigCaveat `{ EGLint `}
236 new from_i(val: Int) `{ return (EGLint)val; `}
237 fun to_i: Int `{ return self; `}
238
239 new none `{ return EGL_NONE; `}
240 fun is_none: Bool `{ return self == EGL_NONE; `}
241
242 new dont_care `{ return EGL_DONT_CARE; `}
243 fun is_dont_care: Bool `{ return self == EGL_DONT_CARE; `}
244
245 new slow `{ return EGL_SLOW_CONFIG; `}
246 fun is_slow: Bool `{ return self == EGL_SLOW_CONFIG; `}
247
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; `}
251
252 redef fun to_s
253 do
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"
259 end
260 end
261
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; `}
266
267 fun opengl: Bool `{ return self & EGL_OPENGL_BIT; `}
268 fun with_opengl: EGLConformant `{ return self | EGL_OPENGL_BIT; `}
269
270 fun opengl_es: Bool `{ return self & EGL_OPENGL_ES_BIT; `}
271 fun with_opengl_es: EGLConformant `{ return self | EGL_OPENGL_ES_BIT; `}
272
273 fun opengl_es2: Bool `{ return self & EGL_OPENGL_ES2_BIT; `}
274 fun with_opengl_es2: EGLConformant `{ return self | EGL_OPENGL_ES2_BIT; `}
275
276 fun openvg: Bool `{ return self & EGL_OPENVG_BIT; `}
277 fun with_openvg: EGLConformant `{ return self | EGL_OPENVG_BIT; `}
278
279 fun to_a: Array[String]
280 do
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")
286 return features
287 end
288
289 redef fun to_s do return to_a.join(", ")
290 end
291
292 # Attributes of a surface for a given EGL display
293 class EGLSurfaceAttribs
294 var display: EGLDisplay
295 var surface: EGLSurface
296
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)
312 end
313
314 extern class EGLError `{ EGLint `}
315 fun is_success: Bool `{ return self == EGL_SUCCESS; `}
316
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; `}
331
332 redef fun to_s
333 do
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
349 end
350 end
351
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; `}
386
387 # Attrib list terminator
388 new none `{ return EGL_NONE; `}
389 end
390
391 # Utility class to choose an EGLConfig
392 class EGLConfigChooser
393 var array = new Array[Int]
394 var closed = false
395 protected var inserted_attribs = new Array[Int]
396
397 protected fun insert_attrib_key(key: Int)
398 do
399 assert not inserted_attribs.has(key) else
400 print "Duplicate attrib passed to EGLConfigChooser"
401 end
402 array.add key
403 end
404
405 protected fun insert_attrib_with_val(key, val: Int)
406 do
407 insert_attrib_key key
408 array.add val
409 end
410
411 fun close do
412 insert_attrib_key 0x3038
413 closed = true
414 end
415
416 fun surface_type=(flag: Int) do insert_attrib_with_val(0x3033, flag)
417 fun surface_type_egl do surface_type = 4
418
419 # Set which client rendering APIs are supported
420 fun renderable_type=(flag: Int) do insert_attrib_with_val(0x3040, flag)
421
422 # Set EGL as the only supported rendering API
423 fun renderable_type_egl do renderable_type = 4
424
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)
428
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)
434
435 fun caveat=(caveat: EGLConfigCaveat) do insert_attrib_with_val(0x3050, caveat.to_i)
436
437 fun conformant=(conformant: EGLConformant) do insert_attrib_with_val(0x3042, conformant.to_i)
438
439 fun choose(display: EGLDisplay): nullable Array[EGLConfig]
440 do
441 assert closed else print "EGLConfigChooser not closed."
442 return display.choose_configs(array)
443 end
444 end
445
446 redef class Object
447 private fun report_egl_error(cmsg: NativeString)
448 do
449 var msg = cmsg.to_s
450 print "libEGL error: {msg}"
451 end
452 end
453
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); `}
457
458 # Handle to the default display to use with EGL
459 fun egl_default_display: Pointer `{ return EGL_DEFAULT_DISPLAY; `}