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
use crate::*;
pub struct Surface {
pub(crate) ctx: Context,
pub(crate) id: GlFramebuffer,
pub(crate) texture: Option<Texture>,
}
impl Surface {
pub fn new(ctx: &Context, texture: Texture) -> Result<Surface, GolemError> {
let ctx = Context(ctx.0.clone());
let gl = &ctx.0.gl;
let id = unsafe { gl.create_framebuffer() }?;
unsafe {
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(id));
gl.framebuffer_texture_2d(
glow::FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
glow::TEXTURE_2D,
Some(texture.id),
0,
);
gl.bind_framebuffer(glow::FRAMEBUFFER, *ctx.0.current_surface.borrow());
}
Ok(Surface {
ctx,
id,
texture: Some(texture),
})
}
pub fn has_texture(&self) -> bool {
self.texture.is_some()
}
pub fn is_bound(&self) -> bool {
match *self.ctx.0.current_surface.borrow() {
Some(surface) => self.id == surface,
None => false,
}
}
pub fn take_texture(&mut self) -> Option<Texture> {
let gl = &self.ctx.0.gl;
unsafe {
gl.framebuffer_texture_2d(
glow::FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
glow::TEXTURE_2D,
None,
0,
);
}
self.texture.take()
}
pub fn put_texture(&mut self, texture: Texture) {
let gl = &self.ctx.0.gl;
unsafe {
gl.framebuffer_texture_2d(
glow::FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
glow::TEXTURE_2D,
Some(texture.id),
0,
);
}
self.texture = Some(texture);
}
pub fn bind(&self) {
assert!(
self.has_texture(),
"The surface had no attached image when bind was called"
);
*self.ctx.0.current_surface.borrow_mut() = Some(self.id);
let gl = &self.ctx.0.gl;
unsafe {
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.id));
}
}
pub fn unbind(ctx: &Context) {
*ctx.0.current_surface.borrow_mut() = None;
let gl = &ctx.0.gl;
unsafe {
gl.bind_framebuffer(glow::FRAMEBUFFER, None);
}
}
pub fn get_pixel_data(
&self,
x: u32,
y: u32,
width: u32,
height: u32,
format: ColorFormat,
data: &mut [u8],
) {
assert!(
self.is_bound(),
"The surface wasn't bound when get_pixel_data was called"
);
assert!(
self.has_texture(),
"The surface had no attached image when get_pixel_data was called"
);
let bytes_per_pixel = format.bytes_per_pixel();
let length = (width * height * bytes_per_pixel) as usize;
assert!(
data.len() >= length,
"The buffer was not large enough to hold the data"
);
let format = format.gl_format();
let gl = &self.ctx.0.gl;
unsafe {
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.id));
gl.read_pixels(
x as i32,
y as i32,
width as i32,
height as i32,
format,
glow::UNSIGNED_BYTE,
data,
);
gl.bind_framebuffer(glow::FRAMEBUFFER, None);
}
}
pub fn width(&self) -> Option<u32> {
self.texture.as_ref().map(|tex| tex.width())
}
pub fn height(&self) -> Option<u32> {
self.texture.as_ref().map(|tex| tex.height())
}
}
impl Drop for Surface {
fn drop(&mut self) {
unsafe {
self.ctx.0.gl.delete_framebuffer(self.id);
}
}
}