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
/*
 * Copyright 2015 The Servo Project Developers
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

use gl_rasterization_context;
use skia;

use euclid::Size2D;
use glx;
use std::ptr;
use std::rc::Rc;
use x11::xlib;

use gleam::gl;

pub struct PlatformDisplayData {
    pub display: *mut xlib::Display,
    pub visual_info: *mut xlib::XVisualInfo,
}

pub struct GLPlatformContext {
    gl: Rc<gl::Gl>,
    pub display: *mut xlib::Display,
    glx_context: xlib::XID,
    pub glx_pixmap: xlib::XID,
    pub pixmap: xlib::XID,

    pub framebuffer_id: gl::GLuint,
    pub texture_id: gl::GLuint,
    pub depth_stencil_renderbuffer_id: gl::GLuint,
}

impl Drop for GLPlatformContext {
    fn drop(&mut self) {
        // We need this thread to grab the GLX context before we can make
        // OpenGL calls. But glXMakeCurrent() will flush the old context,
        // which might have been uninitialized. Dropping the current context
        // first solves this problem somehow.
        self.drop_current_context();
        self.make_current();

        gl_rasterization_context::destroy_framebuffer(self.gl(),
                                                      self.framebuffer_id,
                                                      self.texture_id,
                                                      self.depth_stencil_renderbuffer_id);

        unsafe {
            let glx_display = self.display as *mut glx::types::Display;
            glx::MakeCurrent(glx_display, 0 /* None */, ptr::null_mut());
            glx::DestroyContext(glx_display, self.glx_context as glx::types::GLXContext);
            glx::DestroyGLXPixmap(glx_display, self.glx_pixmap);
            xlib::XFreePixmap(self.display, self.pixmap);
        }
    }
}

impl GLPlatformContext {
    pub fn new(gl: Rc<gl::Gl>,
               platform_display_data: PlatformDisplayData,
               size: Size2D<i32>)
               -> Option<GLPlatformContext> {
        unsafe {
            let display = platform_display_data.display;
            let visual_info = platform_display_data.visual_info;
            let glx_display = display as *mut glx::types::Display;
            let glx_visual_info = visual_info as *mut glx::types::XVisualInfo;

            let root_window = xlib::XRootWindow(display, xlib::XDefaultScreen(display));
            let pixmap = xlib::XCreatePixmap(display,
                                             root_window,
                                             size.width as u32,
                                             size.height as u32,
                                             (*visual_info).depth as u32);
            let glx_pixmap = glx::CreateGLXPixmap(glx_display,
                                                  glx_visual_info,
                                                  pixmap);

            let glx_context = glx::CreateContext(glx_display,
                                                 glx_visual_info,
                                                 ptr::null_mut(),
                                                 1);

            if glx_context == ptr::null() {
                glx::DestroyGLXPixmap(glx_display, glx_pixmap);
            }

            glx::MakeCurrent(display as *mut glx::types::Display,
                             glx_pixmap,
                             glx_context as glx::types::GLXContext);

            let gl_interface = skia::SkiaGrGLCreateNativeInterface();
            if gl_interface == ptr::null_mut() {
                glx::MakeCurrent(display as *mut glx::types::Display,
                                 0 /* None */,
                                 ptr::null_mut());
                return None;
            }

            if let Some((framebuffer_id, texture_id, depth_stencil_renderbuffer_id)) =
                gl_rasterization_context::setup_framebuffer(&*gl,
                                                            gl::TEXTURE_2D,
                                                            size,
                                                            gl_interface,
                                                            || {
                gl.tex_image_2d(gl::TEXTURE_2D, 0,
                                 gl::RGBA as gl::GLint,
                                 size.width, size.height, 0,
                                 gl::RGBA, gl::UNSIGNED_BYTE, None);
            }) {
                skia::SkiaGrGLInterfaceRelease(gl_interface);
                return Some(GLPlatformContext {
                    gl: gl,
                    display: display,
                    glx_context: glx_context as xlib::XID,
                    pixmap: pixmap,
                    glx_pixmap: glx_pixmap as xlib::XID,
                    framebuffer_id: framebuffer_id,
                    texture_id: texture_id,
                    depth_stencil_renderbuffer_id: depth_stencil_renderbuffer_id,
                })
            }
            skia::SkiaGrGLInterfaceRelease(gl_interface);
            None
        }
    }

    fn gl(&self) -> &gl::Gl {
        &*self.gl
    }

    pub fn drop_current_context(&self) {
        unsafe {
            glx::MakeCurrent(self.display as *mut glx::types::Display,
                             0 /* None */,
                             ptr::null_mut());
        }
    }

    pub fn make_current(&self) {
        unsafe {
            glx::MakeCurrent(self.display as *mut glx::types::Display,
                             self.glx_pixmap,
                             self.glx_context as glx::types::GLXContext);
        }
    }
}