5c503872e3ddcff2fe63a1ce74360b86c630cae3
[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 # EGL is an interface between the rendering APIs OpenGL, OpenGL ES, etc.
18 # and the native windowing system.
19 #
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
25
26 in "C Header" `{
27 #include <EGL/egl.h>
28 `}
29
30 extern class EGLNativeDisplayType `{ EGLNativeDisplayType `}
31 new from_x11(handle: Pointer) `{ return (EGLNativeDisplayType)handle; `}
32 end
33
34 extern class EGLDisplay `{ EGLDisplay `}
35 new current `{ return eglGetCurrentDisplay(); `}
36 new(handle: Pointer) `{ return eglGetDisplay(handle); `}
37
38 fun is_valid: Bool `{ return recv != EGL_NO_DISPLAY; `}
39
40 fun initialize: Bool `{
41 EGLBoolean r = eglInitialize(recv, NULL, NULL);
42 if (r == EGL_FALSE) {
43 fprintf(stderr, "Unable to eglInitialize");
44 return 0;
45 }
46 return 1;
47 `}
48
49 fun major_version: Int `{
50 EGLint val;
51 eglInitialize(recv, &val, NULL);
52 return val;
53 `}
54 fun minor_version: Int `{
55 EGLint val;
56 eglInitialize(recv, NULL, &val);
57 return val;
58 `}
59
60 # TODO if useful
61 # Returns all configs supported by the hardware
62 #fun get_configs: nullable Array[EGLConfig] import Array[EGLConfig].with_native `{
63
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 `{
66 EGLConfig *configs;
67 int n_configs;
68 int n_attribs = Array_of_Int_length(attribs);
69 int i;
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);
73 }
74
75 // get number of configs
76 EGLBoolean r = eglChooseConfig(recv, c_attribs, NULL, 0, &n_configs);
77
78 if (r == EGL_FALSE) {
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();
84 }
85
86 configs = (EGLConfig*)malloc(sizeof(EGLConfig)*n_configs);
87
88 r = eglChooseConfig(recv, c_attribs, configs, n_configs, &n_configs);
89
90 if (r == EGL_FALSE) {
91 EGLDisplay_report_egl_error(recv, "failed to load config.");
92 return null_Array_of_EGLConfig();
93 } else {
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]);
97
98 return Array_of_EGLConfig_as_nullable(array);
99 }
100 `}
101
102 # Can be used directly, but it is preferable to use a `EGLConfigAttribs`
103 fun config_attrib(config: EGLConfig, attribute: Int): Int `{
104 EGLint val;
105 EGLBoolean r = eglGetConfigAttrib(recv, config, attribute, &val);
106 if (r == EGL_FALSE)
107 return -1;
108 else
109 return val;
110 `}
111
112 fun terminate: Bool `{
113 return eglTerminate(recv) == EGL_TRUE;
114 `}
115
116 fun create_window_surface(config: EGLConfig, native_window: Pointer, attribs: Array[Int]): EGLSurface `{
117 EGLSurface surface = eglCreateWindowSurface(recv, config, (EGLNativeWindowType)native_window, NULL);
118 return surface;
119 `}
120
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);
125 return context;
126 `}
127
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");
131 return 0;
132 }
133 return 1;
134 `}
135
136 # Can be used directly, but it is preferable to use a `EGLSurfaceAttribs`
137 fun query_surface(surface: EGLSurface, attribute: Int): Int `{
138 int val;
139 EGLBoolean r = eglQuerySurface(recv, surface, attribute, &val);
140 if (r == EGL_FALSE)
141 return -1;
142 else
143 return val;
144 `}
145
146 fun destroy_context(context: EGLContext): Bool `{
147 return eglDestroyContext(recv, context);
148 `}
149
150 fun destroy_surface(surface: EGLSurface): Bool `{
151 return eglDestroySurface(recv, surface);
152 `}
153
154 fun error: EGLError `{ return eglGetError(); `}
155
156 # Utility method for easier debugging
157 fun assert_no_egl_error
158 do
159 var error = self.error
160 if not error.is_success then
161 print "EGL error: {error}"
162 abort
163 end
164 end
165
166 private fun query_string(name: Int): String import NativeString.to_s `{
167 return NativeString_to_s((char *)eglQueryString(recv, name));
168 `}
169
170 fun vendor: String do return query_string(0x3053)
171
172 fun version: String do return query_string(0x3054)
173
174 fun extensions: Array[String] do return query_string(0x3055).split_with(" ")
175
176 fun client_apis: Array[String] do return query_string(0x308D).split_with(" ")
177
178 fun swap_buffers(surface: EGLSurface) `{ eglSwapBuffers(recv, surface); `}
179 end
180
181 extern class EGLConfig `{ EGLConfig `}
182 fun attribs(display: EGLDisplay): EGLConfigAttribs do
183 return new EGLConfigAttribs(display, self)
184 end
185 end
186
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; `}
191
192 fun is_ok: Bool `{ return recv != EGL_NO_SURFACE; `}
193
194 fun attribs(display: EGLDisplay): EGLSurfaceAttribs do
195 return new EGLSurfaceAttribs(display, self)
196 end
197 end
198
199 extern class EGLContext `{ EGLContext `}
200 new current `{ return eglGetCurrentContext(); `}
201 new none `{ return EGL_NO_CONTEXT; `}
202
203 fun is_ok: Bool `{ return recv != EGL_NO_CONTEXT; `}
204 end
205
206 # Attributes of a config for a given EGL display
207 class EGLConfigAttribs
208 var display: EGLDisplay
209 var config: EGLConfig
210
211 fun buffer_size: Int do return display.config_attrib(config, 0x3020)
212 fun alpha_size: Int do return display.config_attrib(config, 0x3021)
213 fun blue_size: Int do return display.config_attrib(config, 0x3022)
214 fun green_size: Int do return display.config_attrib(config, 0x3023)
215 fun red_size: Int do return display.config_attrib(config, 0x3024)
216 fun depth_size: Int do return display.config_attrib(config, 0x3025)
217 fun stencil_size: Int do return display.config_attrib(config, 0x3026)
218
219 fun native_visual_id: Int do return display.config_attrib(config, 0x302E)
220 fun native_visual_type: Int do return display.config_attrib(config, 0x302F)
221
222 fun caveat: EGLConfigCaveat do
223 return new EGLConfigCaveat.from_i(display.config_attrib(config, 0x3027))
224 end
225
226 fun conformant: EGLConformant do
227 return new EGLConformant.from_i(display.config_attrib(config, 0x3042))
228 end
229 end
230
231 extern class EGLConfigCaveat `{ EGLint `}
232 new from_i(val: Int) `{ return (EGLint)val; `}
233 fun to_i: Int `{ return recv; `}
234
235 new none `{ return EGL_NONE; `}
236 fun is_none: Bool `{ return recv == EGL_NONE; `}
237
238 new dont_care `{ return EGL_DONT_CARE; `}
239 fun is_dont_care: Bool `{ return recv == EGL_DONT_CARE; `}
240
241 new slow `{ return EGL_SLOW_CONFIG; `}
242 fun is_slow: Bool `{ return recv == EGL_SLOW_CONFIG; `}
243
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; `}
247
248 redef fun to_s
249 do
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"
255 end
256 end
257
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; `}
262
263 fun opengl: Bool `{ return recv & EGL_OPENGL_BIT; `}
264 fun with_opengl: EGLConformant `{ return recv | EGL_OPENGL_BIT; `}
265
266 fun opengl_es: Bool `{ return recv & EGL_OPENGL_ES_BIT; `}
267 fun with_opengl_es: EGLConformant `{ return recv | EGL_OPENGL_ES_BIT; `}
268
269 fun opengl_es2: Bool `{ return recv & EGL_OPENGL_ES2_BIT; `}
270 fun with_opengl_es2: EGLConformant `{ return recv | EGL_OPENGL_ES2_BIT; `}
271
272 fun openvg: Bool `{ return recv & EGL_OPENVG_BIT; `}
273 fun with_openvg: EGLConformant `{ return recv | EGL_OPENVG_BIT; `}
274
275 fun to_a: Array[String]
276 do
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")
282 return features
283 end
284
285 redef fun to_s do return to_a.join(", ")
286 end
287
288 # Attributes of a surface for a given EGL display
289 class EGLSurfaceAttribs
290 var display: EGLDisplay
291 var surface: EGLSurface
292
293 fun height: Int do return display.query_surface(surface, 0x3056)
294 fun width: Int do return display.query_surface(surface, 0x3057)
295 fun largest_pbuffer: Int do return display.query_surface(surface, 0x3058)
296 fun texture_format: Int do return display.query_surface(surface, 0x3080)
297 fun texture_target: Int do return display.query_surface(surface, 0x3081)
298 fun mipmap_texture: Int do return display.query_surface(surface, 0x3082)
299 fun mipmap_level: Int do return display.query_surface(surface, 0x3083)
300 fun render_buffer: Int do return display.query_surface(surface, 0x3086)
301 fun vg_colorspace: Int do return display.query_surface(surface, 0x3087)
302 fun vg_alpha_format: Int do return display.query_surface(surface, 0x3088)
303 fun horizontal_resolution: Int do return display.query_surface(surface, 0x3090)
304 fun vertical_resolution: Int do return display.query_surface(surface, 0x3091)
305 fun pixel_aspect_ratio: Int do return display.query_surface(surface, 0x3092)
306 fun swap_behavior: Int do return display.query_surface(surface, 0x3093)
307 fun multisample_resolve: Int do return display.query_surface(surface, 0x3099)
308 end
309
310 extern class EGLError `{ EGLint `}
311 fun is_success: Bool `{ return recv == EGL_SUCCESS; `}
312
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; `}
327
328 redef fun to_s
329 do
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
345 end
346 end
347
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; `}
382
383 # Attrib list terminator
384 new none `{ return EGL_NONE; `}
385 end
386
387 # Utility class to choose an EGLConfig
388 class EGLConfigChooser
389 var array = new Array[Int]
390 var closed = false
391 protected var inserted_attribs = new Array[Int]
392
393 protected fun insert_attrib_key(key: Int)
394 do
395 assert not inserted_attribs.has(key) else
396 print "Duplicate attrib passed to EGLConfigChooser"
397 end
398 array.add key
399 end
400
401 protected fun insert_attrib_with_val(key, val: Int)
402 do
403 insert_attrib_key key
404 array.add val
405 end
406
407 fun close do
408 insert_attrib_key 0x3038
409 closed = true
410 end
411
412 fun surface_type=(flag: Int) do insert_attrib_with_val(0x3033, flag)
413 fun surface_type_egl do surface_type = 4
414
415 fun blue_size=(size: Int) do insert_attrib_with_val(0x3022, size)
416 fun green_size=(size: Int) do insert_attrib_with_val(0x3023, size)
417 fun red_size=(size: Int) do insert_attrib_with_val(0x3024, size)
418
419 fun buffer_size=(size: Int) do insert_attrib_with_val(0x3020, size)
420 fun alpha_size=(size: Int) do insert_attrib_with_val(0x3021, size)
421 fun depth_size=(size: Int) do insert_attrib_with_val(0x3025, size)
422 fun stencil_size=(size: Int) do insert_attrib_with_val(0x3026, size)
423 fun sample_buffers=(size: Int) do insert_attrib_with_val(0x3031, size)
424
425 fun caveat=(caveat: EGLConfigCaveat) do insert_attrib_with_val(0x3050, caveat.to_i)
426
427 fun conformant=(conformant: EGLConformant) do insert_attrib_with_val(0x3042, conformant.to_i)
428
429 fun choose(display: EGLDisplay): nullable Array[EGLConfig]
430 do
431 assert closed else print "EGLConfigChooser not closed."
432 return display.choose_configs(array)
433 end
434 end
435
436 redef class Object
437 private fun report_egl_error(cmsg: NativeString)
438 do
439 var msg = cmsg.to_s
440 print "libEGL error: {msg}"
441 end
442 end
443
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); `}