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