This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
surface.rs
278 lines (240 loc) · 9.93 KB
/
surface.rs
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
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Implementation of cross-process surfaces. This delegates to the platform-specific
//! implementation.
use texturegl::Texture;
use euclid::size::Size2D;
use skia::gl_rasterization_context::GLRasterizationContext;
use skia::gl_context::GLContext;
use std::sync::Arc;
#[cfg(not(target_os="android"))]
use gleam::gl;
#[cfg(target_os="macos")]
pub use platform::macos::surface::{NativeDisplay,
IOSurfaceNativeSurface};
#[cfg(target_os="linux")]
pub use platform::linux::surface::{NativeDisplay,
PixmapNativeSurface};
#[cfg(target_os="linux")]
use std::ptr;
#[cfg(any(target_os="android",target_os="linux"))]
pub use platform::egl::surface::{EGLImageNativeSurface};
#[cfg(target_os="android")]
pub use platform::android::surface::NativeDisplay;
#[cfg(target_os="windows")]
pub use platform::windows::surface::NativeDisplay;
pub enum NativeSurface {
MemoryBuffer(MemoryBufferNativeSurface),
#[cfg(target_os="linux")]
Pixmap(PixmapNativeSurface),
#[cfg(target_os="macos")]
IOSurface(IOSurfaceNativeSurface),
#[cfg(any(target_os="android",target_os="linux"))]
EGLImage(EGLImageNativeSurface),
}
#[cfg(target_os="linux")]
impl NativeSurface {
/// Creates a new native surface with uninitialized data.
pub fn new(display: &NativeDisplay, size: Size2D<i32>) -> NativeSurface {
match display {
&NativeDisplay::EGL(_info) => {
NativeSurface::EGLImage(EGLImageNativeSurface::new(display, size))
}
&NativeDisplay::GLX(info) => {
if info.display == ptr::null_mut() {
NativeSurface::MemoryBuffer(MemoryBufferNativeSurface::new(display, size))
} else {
NativeSurface::Pixmap(PixmapNativeSurface::new(&info, size))
}
}
}
}
}
#[cfg(target_os="macos")]
impl NativeSurface {
/// Creates a new native surface with uninitialized data.
pub fn new(display: &NativeDisplay, size: Size2D<i32>) -> NativeSurface {
NativeSurface::IOSurface(IOSurfaceNativeSurface::new(display, size))
}
}
#[cfg(target_os="android")]
impl NativeSurface {
/// Creates a new native surface with uninitialized data.
pub fn new(display: &NativeDisplay, size: Size2D<i32>) -> NativeSurface {
NativeSurface::EGLImage(EGLImageNativeSurface::new(display, size))
}
}
#[cfg(target_os="windows")]
impl NativeSurface {
/// Creates a new native surface with uninitialized data.
pub fn new(display: &NativeDisplay, size: Size2D<i32>) -> NativeSurface {
NativeSurface::MemoryBuffer(MemoryBufferNativeSurface::new(display, size))
}
}
macro_rules! native_surface_method_with_mutability {
($self_:ident, $function_name:ident, $surface:ident, $pattern:pat, $($argument:ident),*) => {
match *$self_ {
NativeSurface::MemoryBuffer($pattern) =>
$surface.$function_name($($argument), *),
#[cfg(target_os="linux")]
NativeSurface::Pixmap($pattern) =>
$surface.$function_name($($argument), *),
#[cfg(target_os="macos")]
NativeSurface::IOSurface($pattern) =>
$surface.$function_name($($argument), *),
#[cfg(any(target_os="android",target_os="linux"))]
NativeSurface::EGLImage($pattern) =>
$surface.$function_name($($argument), *),
}
};
}
macro_rules! native_surface_method_mut {
($self_:ident $function_name:ident ($($argument:ident),*)) => {
native_surface_method_with_mutability!($self_,
$function_name,
surface,
ref mut surface,
$($argument),
*)
};
}
macro_rules! native_surface_method {
($self_:ident $function_name:ident ($($argument:ident),*)) => {
native_surface_method_with_mutability!($self_,
$function_name,
surface,
ref surface,
$($argument),
*)
};
}
macro_rules! native_surface_property {
($self_:ident $property_name:ident) => {
match *$self_ {
NativeSurface::MemoryBuffer(ref surface) => surface.$property_name,
#[cfg(target_os="linux")]
NativeSurface::Pixmap(ref surface) => surface.$property_name,
#[cfg(target_os="macos")]
NativeSurface::IOSurface(ref surface) => surface.$property_name,
#[cfg(any(target_os="android",target_os="linux"))]
NativeSurface::EGLImage(ref surface) => surface.$property_name,
}
};
}
impl NativeSurface {
/// Binds the surface to a GPU texture. Compositing task only.
pub fn bind_to_texture(&self, display: &NativeDisplay, texture: &Texture) {
native_surface_method!(self bind_to_texture (display, texture))
}
/// Uploads pixel data to the surface. Painting task only.
pub fn upload(&mut self, display: &NativeDisplay, data: &[u8]) {
native_surface_method_mut!(self upload (display, data))
}
/// Returns an opaque ID identifying the surface for debugging.
pub fn get_id(&self) -> isize {
native_surface_method!(self get_id ())
}
/// Destroys the surface. After this, it is an error to use the surface. Painting task only.
pub fn destroy(&mut self, display: &NativeDisplay) {
native_surface_method_mut!(self destroy (display))
}
/// Records that the surface will leak if destroyed. This is done by the compositor immediately
/// after receiving the surface.
pub fn mark_will_leak(&mut self) {
native_surface_method_mut!(self mark_will_leak ())
}
/// Marks the surface as not leaking. The painting task and the compositing task call this when
/// they are certain that the surface will not leak. For example:
///
/// 1. When sending buffers back to the render task, either the render task will receive them
/// or the render task has crashed. In the former case, they're the render task's
/// responsibility, so this is OK. In the latter case, the kernel or window server is going
/// to clean up the layer buffers. Either way, no leaks.
///
/// 2. If the compositor is shutting down, the render task is also shutting down. In that case
/// it will destroy all its pixmaps, so no leak.
///
/// 3. If the painting task is sending buffers to the compositor, then they are marked as not
/// leaking, because of the possibility that the compositor will die before the buffers are
/// destroyed.
///
/// This helps debug leaks. For performance this may want to become a no-op in the future.
pub fn mark_wont_leak(&mut self) {
native_surface_method_mut!(self mark_wont_leak ())
}
pub fn gl_rasterization_context(&mut self,
gl_context: Arc<GLContext>)
-> Option<Arc<GLRasterizationContext>> {
match native_surface_method_mut!(self gl_rasterization_context (gl_context)) {
Some(context) => Some(Arc::new(context)),
None => None,
}
}
/// Get the memory usage of this native surface. This memory may be allocated
/// on the GPU or on the heap.
pub fn get_memory_usage(&self) -> usize {
// This works for now, but in the future we may want a better heuristic
let size = self.get_size();
size.width as usize * size.height as usize
}
/// Get the size of this native surface.
pub fn get_size(&self) -> Size2D<i32> {
native_surface_property!(self size)
}
}
#[derive(RustcDecodable, RustcEncodable)]
pub struct MemoryBufferNativeSurface {
bytes: Vec<u8>,
pub size: Size2D<i32>,
}
impl MemoryBufferNativeSurface {
pub fn new(_: &NativeDisplay, size: Size2D<i32>) -> MemoryBufferNativeSurface {
MemoryBufferNativeSurface{
bytes: vec!(),
size: size,
}
}
/// This may only be called on the compositor side.
#[cfg(not(target_os="android"))]
pub fn bind_to_texture(&self, _: &NativeDisplay, texture: &Texture) {
let _bound = texture.bind();
gl::tex_image_2d(gl::TEXTURE_2D,
0,
gl::RGBA as i32,
self.size.width as i32,
self.size.height as i32,
0,
gl::BGRA,
gl::UNSIGNED_BYTE,
Some(&self.bytes));
}
#[cfg(target_os="android")]
pub fn bind_to_texture(&self, _: &NativeDisplay, _: &Texture) {
panic!("Binding a memory surface to a texture is not yet supported on Android.");
}
/// This may only be called on the painting side.
pub fn upload(&mut self, _: &NativeDisplay, data: &[u8]) {
self.bytes.clear();
self.bytes.extend_from_slice(data);
}
pub fn get_id(&self) -> isize {
0
}
pub fn destroy(&mut self, _: &NativeDisplay) {
}
pub fn mark_will_leak(&mut self) {
}
pub fn mark_wont_leak(&mut self) {
}
pub fn gl_rasterization_context(&mut self,
_: Arc<GLContext>)
-> Option<GLRasterizationContext> {
None
}
}