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
/*
* 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.
*/
extern crate glutin;
use gl_rasterization_context;
use skia;
use euclid::Size2D;
use gleam::gl;
use std::ptr;
use std::rc::Rc;
pub struct PlatformDisplayData;
impl PlatformDisplayData {
pub fn new() -> PlatformDisplayData {
PlatformDisplayData
}
}
pub struct GLPlatformContext {
gl: Rc<gl::Gl>,
pub context: glutin::HeadlessContext,
pub framebuffer_id: gl::GLuint,
texture_id: gl::GLuint,
depth_stencil_renderbuffer_id: gl::GLuint,
}
impl Drop for GLPlatformContext {
fn drop(&mut self) {
self.make_current();
gl_rasterization_context::destroy_framebuffer(self.gl(),
self.framebuffer_id,
self.texture_id,
self.depth_stencil_renderbuffer_id);
self.destroy();
}
}
impl GLPlatformContext {
pub fn new(gl: Rc<gl::Gl>,
_: PlatformDisplayData,
size: Size2D<i32>)
-> Option<GLPlatformContext> {
unsafe {
// 32x32 is just the size of the dummy underlying context; the real
// size is used below when we create the FBO
let cx = glutin::HeadlessRendererBuilder::new(32, 32).build().unwrap();
cx.make_current();
let gl_interface = skia::SkiaGrGLCreateNativeInterface();
if gl_interface == ptr::null_mut() {
//cx.destroy();
return None
}
let (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);
}).unwrap();
skia::SkiaGrGLInterfaceRelease(gl_interface);
Some(GLPlatformContext {
gl: gl,
context: cx,
framebuffer_id: framebuffer_id,
texture_id: texture_id,
depth_stencil_renderbuffer_id: depth_stencil_renderbuffer_id,
})
}
}
fn gl(&self) -> &gl::Gl {
&*self.gl
}
pub fn drop_current_context(&self) {
// TODO; should not be necessary
}
pub fn destroy(&self) {
// TODO; need to extend glutin
}
pub fn make_current(&self) {
unsafe {
self.context.make_current();
}
}
}