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
use std::ffi::CString;

use crate::gl_context::GLVersion;
use crate::glx;
use crate::glx_extra;
use sparkle::gl;
use std::os::raw::*;
use crate::glx::types::{GLXContext, GLXDrawable, GLXFBConfig, GLXPixmap};
use euclid::Size2D;
use super::utils::{create_offscreen_pixmap_backed_context};

use crate::platform::NativeGLContextMethods;

pub struct NativeGLContextHandle(pub GLXContext, pub *mut glx::types::Display);

unsafe impl Send for NativeGLContextHandle {}

pub struct NativeGLContext {
    native_context: GLXContext,
    native_display: *mut glx::types::Display,
    native_drawable: GLXDrawable,
    weak: bool,
}

impl NativeGLContext {
    pub fn new(share_context: Option<&GLXContext>,
               api_version: GLVersion,
               display: *mut glx::types::Display,
               drawable: GLXDrawable,
               framebuffer_config: GLXFBConfig,
               extensions: String)
        -> Result<NativeGLContext, &'static str> {

        let shared = match share_context {
            Some(ctx) => *ctx,
            None      => 0 as GLXContext,
        };

        let native =  if extensions.split(' ').find(|&i| i == "GLX_ARB_create_context").is_some() {
            let (major, minor) = match api_version {
                GLVersion::Major(major) => { (major, 1) }, // OpenGL 2.1, 3.1
                GLVersion::MajorMinor(major, minor) => { (major, minor) }
            };

            let attributes = [
                glx_extra::CONTEXT_MAJOR_VERSION_ARB as c_int, major as c_int,
                glx_extra::CONTEXT_MINOR_VERSION_ARB as c_int, minor as c_int,
                0
            ];

            // load the extra GLX functions
            let extra_functions = glx_extra::Glx::load_with(|s| {
                let c_str = CString::new(s.as_bytes()).unwrap();
                unsafe { glx::GetProcAddress(c_str.as_ptr() as *const u8) as *const _ }
            });

            unsafe {
                extra_functions.CreateContextAttribsARB(display as *mut _,
                                                        framebuffer_config,
                                                        shared, 1 as glx::types::Bool,
                                                        attributes.as_ptr())
            }
        } else {
             unsafe { 
                 glx::CreateNewContext(display,
                                       framebuffer_config,
                                       glx::RGBA_TYPE as c_int,
                                       shared,
                                       1 as glx::types::Bool)
            }
        };

        if native.is_null() {
            unsafe { glx::DestroyPixmap(display, drawable as GLXPixmap) };
            return Err("Error creating native glx context");
        }

        Ok(NativeGLContext {
            native_context: native,
            native_display: display,
            native_drawable: drawable,
            weak: false,
        })
    }

    pub fn as_native_glx_context(&self) -> GLXContext {
        self.native_context
    }
}

impl Drop for NativeGLContext {
    fn drop(&mut self) {
        // Unbind the current context to free the resources
        // inmediately
        if !self.weak {
            let _ = self.unbind(); // We don't want to panic
            unsafe {
                glx::DestroyContext(self.native_display, self.native_context);
                glx::DestroyPixmap(self.native_display, self.native_drawable as GLXPixmap);
            }
        }
    }
}

impl NativeGLContextMethods for NativeGLContext {
    type Handle = NativeGLContextHandle;

    fn get_proc_address(addr: &str) -> *const () {
        let addr = CString::new(addr.as_bytes()).unwrap();
        let addr = addr.as_ptr();
        unsafe {
            glx::GetProcAddress(addr as *const _) as *const ()
        }
    }

    fn current_handle() -> Option<Self::Handle> {
        let current = unsafe { glx::GetCurrentContext() };
        let dpy = unsafe { glx::GetCurrentDisplay() };

        if current.is_null() || dpy.is_null() {
            None
        } else {
            Some(NativeGLContextHandle(current, dpy))
        }
    }

    fn current() -> Option<NativeGLContext> {
        if let Some(handle) = Self::current_handle() {
            unsafe {
                Some(NativeGLContext {
                    native_context: handle.0,
                    native_display: handle.1,
                    native_drawable: glx::GetCurrentDrawable(),
                    weak: true,
                })
            }
        } else {
            None
        }
    }

    fn create_shared(with: Option<&Self::Handle>,
                     api_type: &gl::GlType,
                     api_version: GLVersion) -> Result<NativeGLContext, &'static str> {
        create_offscreen_pixmap_backed_context(Size2D::new(16, 16), with, api_type, api_version)
    }

    #[inline(always)]
    fn is_current(&self) -> bool {
        unsafe {
            glx::GetCurrentContext() == self.native_context
        }
    }

    fn handle(&self) -> NativeGLContextHandle {
        NativeGLContextHandle(self.native_context, self.native_display)
    }

    fn make_current(&self) -> Result<(), &'static str> {
        unsafe {
            if !self.is_current() &&
               glx::MakeCurrent(self.native_display,
                                self.native_drawable,
                                self.native_context) == 0 {
                Err("glx::MakeCurrent")
            } else {
                Ok(())
            }
        }
    }

    fn unbind(&self) -> Result<(), &'static str> {
        unsafe {
            if self.is_current() &&
               glx::MakeCurrent(self.native_display,
                                0 as GLXDrawable,
                                0 as GLXContext) == 0 {
                Err("glx::MakeCurrent (on unbind)")
            } else {
                Ok(())
            }
        }
    }
}