makepad-platform 1.0.0

Makepad platform layer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
use {
    self::super::{
        super::gl_sys::LibGl,
        super::gl_sys, arkts_obj_ref::ArkTsObjRef, oh_callbacks::*, oh_media::CxOpenHarmonyMedia,
        raw_file::RawFileMgr,
    },
    crate::{
        cx::{Cx, OpenHarmonyParams, OsType}, cx_api::{CxOsApi, CxOsOp, OpenUrlInPlace}, cx_stdin::{PollTimer, PollTimers}, egl_sys::{self, LibEgl, EGL_NONE}, event::{Event, KeyCode, KeyEvent, TouchUpdateEvent, VirtualKeyboardEvent, WindowGeom}, gpu_info::GpuPerformance, makepad_math::*, os::cx_native::EventFlow, pass::{CxPassParent, PassClearColor, PassClearDepth, PassId}, thread::SignalToUI, window::CxWindowPool, WindowGeomChangeEvent
    },
    napi_derive_ohos::napi,
    napi_ohos::{sys::*, Env, JsObject, NapiRaw},
    std::{ffi::CString, os::raw::c_void, ptr::null_mut, rc::Rc, sync::mpsc, time::Instant},
};

#[napi(js_name = "onCreate")]
pub fn ohos_ability_on_create(env: Env, ark_ts: JsObject) -> napi_ohos::Result<()> {
    let raw_env = env.raw();
    let raw_ark = unsafe { ark_ts.raw() };
    let mut arkts_ref = std::ptr::null_mut();

    let status = unsafe { napi_create_reference(raw_env, raw_ark, 1, &mut arkts_ref) };
    assert!(status == 0);

    let arkts_obj = ArkTsObjRef::new(raw_env, arkts_ref);
    let device_type = arkts_obj
        .get_string("deviceType")
        .unwrap_or("phone".to_string());
    let os_full_name = arkts_obj
        .get_string("osFullName")
        .unwrap_or("OpenHarmony".to_string());
    let display_density = arkts_obj.get_number("displayDensity").unwrap_or(3.25);
    let files_dir = arkts_obj.get_string("filesDir").unwrap();
    let cache_dir = arkts_obj.get_string("cacheDir").unwrap();
    let temp_dir = arkts_obj.get_string("tempDir").unwrap();
    let res_mgr = arkts_obj.get_property("resMgr").unwrap();

    let raw_file = RawFileMgr::new(raw_env, res_mgr);

    crate::log!("call onCreate, device_type = {}, os_full_name = {}, display_density = {}, files_dir = {}, cache_dir = {}, temp_dir = {}", device_type, os_full_name, display_density, files_dir,cache_dir,temp_dir);

    send_from_ohos_message(FromOhosMessage::Init {
        device_type,
        os_full_name,
        display_density,
        files_dir,
        cache_dir,
        temp_dir,
        raw_env,
        arkts_ref,
        raw_file,
    });
    Ok(())
}

impl Cx {
    fn main_loop(&mut self, from_ohos_rx: mpsc::Receiver<FromOhosMessage>) {
        crate::log!("entry main_loop");

        self.gpu_info.performance = GpuPerformance::Tier1;

        self.call_event_handler(&Event::Startup);
        self.redraw_all();

        while !self.os.quit {
            match from_ohos_rx.recv() {
                Ok(FromOhosMessage::VSync) => {
                    self.handle_all_pending_messages(&from_ohos_rx);
                    self.handle_other_events();
                    self.handle_drawing();
                }
                Ok(message) => self.handle_message(message),
                Err(e) => {
                    crate::error!("Error receiving message: {:?}", e);
                }
            }
        }
    }

    fn handle_all_pending_messages(&mut self, from_ohos_rx: &mpsc::Receiver<FromOhosMessage>) {
        // Handle the messages that arrived during the last frame
        while let Ok(msg) = from_ohos_rx.try_recv() {
            self.handle_message(msg);
        }
    }

    fn handle_other_events(&mut self) {
        // Timers
        for event in self.os.timers.get_dispatch() {
            self.call_event_handler(&event);
        }

        // Signals
        if SignalToUI::check_and_clear_ui_signal() {
            self.handle_media_signals();
            self.call_event_handler(&Event::Signal);
        }

        // Video updates
        // let to_dispatch = self.get_video_updates();
        // for video_id in to_dispatch {
        //     let e = Event::VideoTextureUpdated(
        //         VideoTextureUpdatedEvent {
        //             video_id,
        //         }
        //     );
        //     self.call_event_handler(&e);
        // }

        // Live edits
        if self.handle_live_edit() {
            self.call_event_handler(&Event::LiveEdit);
            self.redraw_all();
        }

        // Platform operations
        self.handle_platform_ops();
    }

    fn handle_drawing(&mut self) {
        if self.any_passes_dirty() || self.need_redrawing() || !self.new_next_frames.is_empty() {
            if !self.new_next_frames.is_empty() {
                self.call_next_frame_event(self.os.timers.time_now());
            }
            if self.need_redrawing() {
                self.call_draw_event();
                self.opengl_compile_shaders();
            }

            if self.os.first_after_resize {
                self.os.first_after_resize = false;
                self.redraw_all();
            }

            self.handle_repaint();
        }
    }

    fn handle_message(&mut self, msg: FromOhosMessage) {
        match msg {
            FromOhosMessage::SurfaceCreated {
                window,
                width: _,
                height: _,
            } => unsafe {
                self.os.display.as_mut().unwrap().update_surface(window);
            },
            FromOhosMessage::SurfaceDestroyed => unsafe {
                self.os.display.as_mut().unwrap().destroy_surface();
            },
            FromOhosMessage::SurfaceChanged {
                window,
                width,
                height,
            } => {
                unsafe {
                    self.os.display.as_mut().unwrap().update_surface(window);
                }
                self.os.display_size = dvec2(width as f64, height as f64);
                let window_id = CxWindowPool::id_zero();
                let window = &mut self.windows[window_id];
                let old_geom = window.window_geom.clone();

                let dpi_factor = window.dpi_override.unwrap_or(self.os.dpi_factor);
                let size = self.os.display_size / dpi_factor;
                window.window_geom = WindowGeom {
                    dpi_factor,
                    can_fullscreen: false,
                    xr_is_presenting: false,
                    is_fullscreen: true,
                    is_topmost: true,
                    position: dvec2(0.0, 0.0),
                    inner_size: size,
                    outer_size: size,
                };
                let new_geom = window.window_geom.clone();
                self.call_event_handler(&Event::WindowGeomChange(WindowGeomChangeEvent {
                    window_id,
                    new_geom,
                    old_geom,
                }));
                if let Some(main_pass_id) = self.windows[window_id].main_pass_id {
                    self.redraw_pass_and_child_passes(main_pass_id);
                }
                self.redraw_all();
                self.os.first_after_resize = true;
                self.call_event_handler(&Event::ClearAtlasses);
            }
            FromOhosMessage::Touch(mut touches) => {
                let time = touches[0].time;
                let window = &mut self.windows[CxWindowPool::id_zero()];
                let dpi_factor = window.dpi_override.unwrap_or(self.os.dpi_factor);
                for touch in &mut touches {
                    // When the software keyboard shifted the UI in the vertical axis,
                    //we need to make the math here to keep touch events positions synchronized.
                    //if self.os.keyboard_visible {touch.abs.y += self.os.keyboard_panning_offset as f64};
                    //crate::log!("{} {:?} {} {}", time, touch.state, touch.uid, touch.abs);
                    touch.abs /= dpi_factor;
                }
                self.fingers.process_touch_update_start(time, &touches);
                let e = Event::TouchUpdate(
                    TouchUpdateEvent {
                        time,
                        window_id: CxWindowPool::id_zero(),
                        touches,
                        modifiers: Default::default()
                    }
                );
                self.call_event_handler(&e);
                let e = if let Event::TouchUpdate(e) = e {e}else {panic!()};
                self.fingers.process_touch_update_end(&e.touches);
            }
            FromOhosMessage::TextInput(e) => {
                self.call_event_handler(&Event::TextInput(e));
            }
            FromOhosMessage::DeleteLeft(length) => {
                for _ in 0..length {
                    let time = self.os.timers.time_now();
                    let e = KeyEvent {
                        key_code: KeyCode::Backspace,
                        is_repeat: false,
                        modifiers: Default::default(),
                        time,
                    };
                    self.keyboard.process_key_down(e.clone());
                    self.call_event_handler(&Event::KeyDown(e.clone()));
                    self.keyboard.process_key_up(e.clone());
                    self.call_event_handler(&Event::KeyUp(e));
                }
            }
            FromOhosMessage::ResizeTextIME(is_open, keyboard_height) => {
                let keyboard_height = (keyboard_height as f64) / self.os.dpi_factor;
                if is_open {
                    self.call_event_handler(&Event::VirtualKeyboard(
                        VirtualKeyboardEvent::DidShow {
                            height: keyboard_height,
                            time: self.os.timers.time_now(),
                        },
                    ))
                } else {
                    self.text_ime_was_dismissed();
                    self.call_event_handler(&Event::VirtualKeyboard(
                        VirtualKeyboardEvent::DidHide {
                            time: self.os.timers.time_now(),
                        },
                    ))
                }
            }
            _ => {}
        }
    }

    fn wait_init(&mut self, from_ohos_rx: &mpsc::Receiver<FromOhosMessage>) -> bool {
        if let Ok(FromOhosMessage::Init {
            device_type,
            os_full_name,
            display_density,
            files_dir,
            cache_dir,
            temp_dir,
            raw_env,
            arkts_ref,
            raw_file,
        }) = from_ohos_rx.recv()
        {
            self.os.dpi_factor = display_density;
            self.os.raw_file = Some(raw_file);
            self.os_type = OsType::OpenHarmony(OpenHarmonyParams {
                files_dir,
                cache_dir,
                temp_dir,
                device_type,
                os_full_name,
                display_density,
            });
            self.os.arkts_obj = Some(ArkTsObjRef::new(raw_env, arkts_ref));
            return true;
        } else {
            crate::error!("Failed to receive init message from ArkTS layer");
            return false;
        }
    }

    fn wait_surface_created(
        &mut self,
        from_ohos_rx: &mpsc::Receiver<FromOhosMessage>,
    ) -> *mut c_void {
        if let Ok(FromOhosMessage::SurfaceCreated {
            window,
            width,
            height,
        }) = from_ohos_rx.recv()
        {
            self.os.display_size = dvec2(width as f64, height as f64);
            crate::log!(
                "handle surface created, width={}, height={}, display_density={}",
                width,
                height,
                self.os.dpi_factor
            );
            return window;
        } else {
            crate::error!("Can't recv SurfaceCreated from arkts");
            return null_mut();
        }
    }

    pub fn ohos_init<F>(exports: JsObject, env: Env, startup: F)
    where
        F: FnOnce() -> Box<Cx> + Send + 'static,
    {
        crate::log!("ohos init");
        static ONCE: std::sync::Once = std::sync::Once::new();
        ONCE.call_once(move || {
            std::panic::set_hook(Box::new(|info| {
                crate::log!("custom panic hook: {}", info);
            }));
            Cx::ohos_startup(startup);
        });

        if let Ok(xcomponent) = exports.get_named_property::<JsObject>("__NATIVE_XCOMPONENT_OBJ__")
        {
            register_xcomponent_callbacks(&env, &xcomponent);
        } else {
            crate::log!("Failed to get xcomponent in ohos_init");
        }
    }

    fn ohos_startup<F>(startup: F)
    where
        F: FnOnce() -> Box<Cx> + Send + 'static,
    {
        crate::log!("ohos startup");
        let (from_ohos_tx, from_ohos_rx) = mpsc::channel();
        let ohos_tx = from_ohos_tx.clone();
        init_globals(ohos_tx);

        std::thread::spawn(move || {
            let mut cx = startup();
            assert!(cx.wait_init(&from_ohos_rx));
            cx.ohos_load_dependencies();

            let window = cx.wait_surface_created(&from_ohos_rx);

            let mut libegl = LibEgl::try_load().expect("can't load LibEGL");
            let (egl_context, egl_config, egl_display) = unsafe {
                egl_sys::create_egl_context(&mut libegl).expect("Can't create EGL context")
            };
            let libgl = LibGl::try_load(| s | {
                for s in s{
                    let s = CString::new(*s).unwrap();
                    let p = unsafe{libegl.eglGetProcAddress.unwrap()(s.as_ptr())};
                    if !p.is_null(){
                        return p
                    }
                }
                0 as _
            }).expect("Cant load openGL functions");
            
            let win_attr = vec![EGL_NONE];
            let surface = unsafe {
                (libegl.eglCreateWindowSurface.unwrap())(
                    egl_display,
                    egl_config,
                    window as _,
                    win_attr.as_ptr() as _,
                )
            };

            if surface.is_null() {
                let err_code = unsafe { (libegl.eglGetError.unwrap())() };
                crate::log!("eglCreateWindowSurface error code:{}", err_code);
            }
            assert!(!surface.is_null());

            crate::log!("eglCreateWindowSurface success");
            unsafe {
                (libegl.eglSwapBuffers.unwrap())(egl_display, surface);
            }

            if unsafe {
                (libegl.eglMakeCurrent.unwrap())(egl_display, surface, surface, egl_context)
            } == 0
            {
                panic!();
            }

            cx.os.display = Some(CxOhosDisplay {
                libegl,
                libgl,
                egl_display,
                egl_config,
                egl_context,
                surface,
                window,
            });

            register_vsync_callback(from_ohos_tx);
            cx.main_loop(from_ohos_rx);
            //TODO, destroy surface
        });
    }

    pub fn ohos_load_dependencies(&mut self) {
        for (path, dep) in &mut self.dependencies {
            let mut buffer = Vec::<u8>::new();
            if let Ok(_) = self
                .os
                .raw_file
                .as_mut()
                .unwrap()
                .read_to_end(path, &mut buffer)
            {
                dep.data = Some(Ok(Rc::new(buffer)));
            } else {
                dep.data = Some(Err("read_to_end failed".to_string()));
            }
        }
    }

    pub fn draw_pass_to_fullscreen(&mut self, pass_id: PassId) {
        let draw_list_id = self.passes[pass_id].main_draw_list_id.unwrap();

        self.setup_render_pass(pass_id);

        // keep repainting in a loop
        //self.passes[pass_id].paint_dirty = false;
        let gl = self.os.gl();
        unsafe {
            //direct_app.egl.make_current();
            (gl.glViewport)(
                0,
                0,
                self.os.display_size.x as i32,
                self.os.display_size.y as i32,
            );
        }

        let clear_color = if self.passes[pass_id].color_textures.len() == 0 {
            self.passes[pass_id].clear_color
        } else {
            match self.passes[pass_id].color_textures[0].clear_color {
                PassClearColor::InitWith(color) => color,
                PassClearColor::ClearWith(color) => color,
            }
        };
        let clear_depth = match self.passes[pass_id].clear_depth {
            PassClearDepth::InitWith(depth) => depth,
            PassClearDepth::ClearWith(depth) => depth,
        };

        if !self.passes[pass_id].dont_clear {
            unsafe {
                (gl.glBindFramebuffer)(gl_sys::FRAMEBUFFER, 0);
                (gl.glClearDepthf)(clear_depth as f32);
                (gl.glClearColor)(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
                (gl.glClear)(gl_sys::COLOR_BUFFER_BIT | gl_sys::DEPTH_BUFFER_BIT);
            }
        }
        Self::set_default_depth_and_blend_mode(self.os.gl());

        let mut zbias = 0.0;
        let zbias_step = self.passes[pass_id].zbias_step;

        self.render_view(pass_id, draw_list_id, &mut zbias, zbias_step);

        unsafe { self.os.display.as_mut().unwrap().swap_buffers() };

        //unsafe {
        //direct_app.drm.swap_buffers_and_wait(&direct_app.egl);
        //}
    }

    pub(crate) fn handle_repaint(&mut self) {
        let mut passes_todo = Vec::new();
        self.compute_pass_repaint_order(&mut passes_todo);
        self.repaint_id += 1;
        for pass_id in &passes_todo {
            self.passes[*pass_id].set_time(self.os.timers.time_now() as f32);
            match self.passes[*pass_id].parent.clone() {
                CxPassParent::Xr => {}
                CxPassParent::Window(_window_id) => {
                    self.draw_pass_to_fullscreen(*pass_id);
                }
                CxPassParent::Pass(_) => {
                    self.draw_pass_to_texture(*pass_id, None);
                }
                CxPassParent::None => {
                    self.draw_pass_to_texture(*pass_id, None);
                }
            }
        }
    }

    fn handle_platform_ops(&mut self) -> EventFlow {
        while let Some(op) = self.platform_ops.pop() {
            //crate::log!("============ handle_platform_ops");
            match op {
                CxOsOp::CreateWindow(window_id) => {
                    let window = &mut self.windows[window_id];
                    let size = dvec2(
                        self.os.display_size.x / self.os.dpi_factor,
                        self.os.display_size.y / self.os.dpi_factor,
                    );
                    window.window_geom = WindowGeom {
                        dpi_factor: self.os.dpi_factor,
                        can_fullscreen: false,
                        xr_is_presenting: false,
                        is_fullscreen: true,
                        is_topmost: true,
                        position: dvec2(0.0, 0.0),
                        inner_size: size,
                        outer_size: size,
                    };
                    window.is_created = true;
                }
                CxOsOp::StartTimer {
                    timer_id,
                    interval,
                    repeats,
                } => {
                    self.os
                        .timers
                        .timers
                        .insert(timer_id, PollTimer::new(interval, repeats));
                }
                CxOsOp::StopTimer(timer_id) => {
                    self.os.timers.timers.remove(&timer_id);
                }
                CxOsOp::ShowTextIME(_area, _pos) => {
                    let _ = self.os.arkts_obj.as_mut().unwrap().call_js_function(
                        "showKeyBoard",
                        0,
                        std::ptr::null_mut(),
                    );
                }
                CxOsOp::HideTextIME => {
                    let _ = self.os.arkts_obj.as_mut().unwrap().call_js_function(
                        "hideKeyBoard",
                        0,
                        std::ptr::null_mut(),
                    );
                    //self.os.keyboard_visible = false;
                    //unsafe {android_jni::to_java_show_keyboard(false);}
                }
                e=>{
                    crate::error!("Not implemented on this platform: CxOsOp::{:?}", e);
                }
            }
        }
        EventFlow::Poll
    }
}

impl CxOsApi for Cx {
    fn init_cx_os(&mut self) {
        self.live_registry.borrow_mut().package_root = Some("makepad".to_string());
        self.live_expand();
        self.live_scan_dependencies();
    }

    fn spawn_thread<F>(&mut self, f: F)
    where
        F: FnOnce() + Send + 'static,
    {
        std::thread::spawn(f);
    }

    fn open_url(&mut self, _url: &str, _in_place: OpenUrlInPlace) {
        crate::error!("open_url not implemented on this platform");
    }

    fn seconds_since_app_start(&self) -> f64 {
        Instant::now()
            .duration_since(self.os.start_time)
            .as_secs_f64()
    }
}

pub struct CxOhosDisplay {
    pub libegl: LibEgl,
    pub libgl: LibGl,
    pub egl_display: egl_sys::EGLDisplay,
    pub egl_config: egl_sys::EGLConfig,
    pub egl_context: egl_sys::EGLContext,
    pub surface: egl_sys::EGLSurface,
    pub window: *mut c_void, //event_handler: Box<dyn EventHandler>,
}

pub struct CxOs {
    pub first_after_resize: bool,
    pub display_size: DVec2,
    pub dpi_factor: f64,
    pub media: CxOpenHarmonyMedia,
    pub quit: bool,
    pub timers: PollTimers,
    pub raw_file: Option<RawFileMgr>,
    pub arkts_obj: Option<ArkTsObjRef>,
    pub(crate) start_time: Instant,
    pub(crate) display: Option<CxOhosDisplay>,
}

impl CxOs{
    pub (crate) fn gl(&self)->&LibGl{
        &self.display.as_ref().unwrap().libgl
    }
}

impl Default for CxOs {
    fn default() -> Self {
        Self {
            first_after_resize: true,
            display_size: dvec2(1260 as f64, 2503 as f64),
            dpi_factor: 3.25,
            media: Default::default(),
            quit: false,
            timers: Default::default(),
            raw_file: None,
            arkts_obj: None,
            start_time: Instant::now(),
            display: None,
        }
    }
}

impl CxOhosDisplay {
    unsafe fn destroy_surface(&mut self) {
        (self.libegl.eglMakeCurrent.unwrap())(
            self.egl_display,
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            std::ptr::null_mut(),
        );
        (self.libegl.eglDestroySurface.unwrap())(self.egl_display, self.surface);
        self.surface = std::ptr::null_mut();
    }

    unsafe fn update_surface(&mut self, window: *mut c_void) {
        if !self.window.is_null() {
            //todo release window
        }
        self.window = window;
        if self.surface.is_null() == false {
            self.destroy_surface();
        }

        let win_attr = vec![EGL_NONE];
        self.surface = (self.libegl.eglCreateWindowSurface.unwrap())(
            self.egl_display,
            self.egl_config,
            self.window as _,
            win_attr.as_ptr() as _,
        );

        if self.surface.is_null() {
            let err_code = unsafe { (self.libegl.eglGetError.unwrap())() };
            crate::log!("eglCreateWindowSurface error code:{}", err_code);
        }

        assert!(!self.surface.is_null());

        self.make_current();
    }

    unsafe fn swap_buffers(&mut self) {
        (self.libegl.eglSwapBuffers.unwrap())(self.egl_display, self.surface);
    }

    unsafe fn make_current(&mut self) {
        if (self.libegl.eglMakeCurrent.unwrap())(
            self.egl_display,
            self.surface,
            self.surface,
            self.egl_context,
        ) == 0
        {
            panic!();
        }
    }
}