From 4b44f1916d65da45b662dff58d5a0544cd031d39 Mon Sep 17 00:00:00 2001 From: Sotaro Ikeda Date: Wed, 15 Mar 2017 20:58:00 +0900 Subject: [PATCH] Use Gl::get_type() to select Gl/Gles in WebRender --- Cargo.lock | 16 +-- webrender/Cargo.toml | 2 +- webrender/src/device.rs | 210 ++++++++++++++++++++++++---------------- 3 files changed, 136 insertions(+), 92 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e37f883e38..5e104d08ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,7 +11,7 @@ dependencies = [ "dwrote 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "font-loader 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "gleam 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gleam 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "osmesa-src 12.0.1 (git+https://github.com/servo/osmesa-src)", @@ -117,7 +117,7 @@ name = "cgl" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "gleam 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gleam 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -365,7 +365,7 @@ dependencies = [ [[package]] name = "gleam" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gl_generator 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -598,7 +598,7 @@ dependencies = [ "euclid 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "gl_generator 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", - "gleam 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gleam 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -901,7 +901,7 @@ dependencies = [ "fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "freetype 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "gamma-lut 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "gleam 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gleam 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", @@ -921,7 +921,7 @@ dependencies = [ "core-graphics 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "dwrote 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "gleam 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gleam 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "offscreen_gl_context 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -945,7 +945,7 @@ version = "0.2.0" dependencies = [ "app_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "gleam 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gleam 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "servo-glutin 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "webrender 0.24.0", "webrender_traits 0.25.0", @@ -1042,7 +1042,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0912515a8ff24ba900422ecda800b52f4016a56251922d397c576bf92c690518" "checksum gif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "01c7c19a035de94bd7afbaa62c241aadfbdf1a70f560b348d2312eafa566ca16" "checksum gl_generator 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f1d8edc81c5ae84605a62f5dac661a2313003b26d59839f81d47d46cf0f16a55" -"checksum gleam 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9ab3da24d5b021a630d5a922dd3d25c44211ed427677b4636783c3ab815a8fdb" +"checksum gleam 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2958396a0a358d2de747b31329f5ae2229070602b0f51edd5d682f92c307c332" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum heapsize 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "5a376f7402b85be6e0ba504243ecbc0709c48019ecc6286d0540c2e359050c88" "checksum image 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "979bad0502082fd60053a490282e87d6c89650942e3a270e0d4c83569c7f5899" diff --git a/webrender/Cargo.toml b/webrender/Cargo.toml index fe01433b37..cb7cec3b2f 100644 --- a/webrender/Cargo.toml +++ b/webrender/Cargo.toml @@ -18,7 +18,7 @@ bit-set = "0.4" byteorder = "1.0" euclid = "0.11" fnv = "1.0" -gleam = "0.4.0" +gleam = "0.4.1" lazy_static = "0.2" log = "0.3" num-traits = "0.1.32" diff --git a/webrender/src/device.rs b/webrender/src/device.rs index fc9fe91f3d..0ad8ac0b46 100644 --- a/webrender/src/device.rs +++ b/webrender/src/device.rs @@ -32,17 +32,13 @@ const GL_FORMAT_A: gl::GLuint = gl::RED; #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] const GL_FORMAT_A: gl::GLuint = gl::ALPHA; -#[cfg(any(target_os = "windows", all(unix, not(target_os = "android"))))] -const GL_FORMAT_BGRA: gl::GLuint = gl::BGRA; +const GL_FORMAT_BGRA_GL: gl::GLuint = gl::BGRA; -#[cfg(target_os = "android")] -const GL_FORMAT_BGRA: gl::GLuint = gl::BGRA_EXT; +const GL_FORMAT_BGRA_GLES: gl::GLuint = gl::BGRA_EXT; -#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))] -const SHADER_VERSION: &'static str = "#version 150\n"; +const SHADER_VERSION_GL: &'static str = "#version 150\n"; -#[cfg(any(target_arch = "arm", target_arch = "aarch64"))] -const SHADER_VERSION: &'static str = "#version 300 es\n"; +const SHADER_VERSION_GLES: &'static str = "#version 300 es\n"; static SHADER_PREAMBLE: &'static str = "shared"; @@ -79,6 +75,28 @@ enum FBOTarget { Draw, } +fn get_gl_format_bgra(gl: &gl::Gl) -> gl::GLuint { + match gl.get_type() { + gl::GlType::Gl => { + GL_FORMAT_BGRA_GL + } + gl::GlType::Gles => { + GL_FORMAT_BGRA_GLES + } + } +} + +fn get_shader_version(gl: &gl::Gl) -> &'static str { + match gl.get_type() { + gl::GlType::Gl => { + SHADER_VERSION_GL + } + gl::GlType::Gles => { + SHADER_VERSION_GLES + } + } +} + fn get_optional_shader_source(shader_name: &str, base_path: &Option) -> Option { if let Some(ref base) = *base_path { let shader_path = base.join(&format!("{}.glsl", shader_name)); @@ -509,28 +527,29 @@ pub struct GpuFrameProfile { } impl GpuFrameProfile { - #[cfg(not(target_os = "android"))] fn new(gl: Rc) -> GpuFrameProfile { - let queries = gl.gen_queries(MAX_EVENTS_PER_FRAME as gl::GLint); - - GpuFrameProfile { - gl: gl, - queries: queries, - samples: Vec::new(), - next_query: 0, - pending_query: 0, - frame_id: FrameId(0), - } - } - - #[cfg(target_os = "android")] - fn new() -> GpuFrameProfile { - GpuFrameProfile { - queries: Vec::new(), - samples: Vec::new(), - next_query: 0, - pending_query: 0, - frame_id: FrameId(0), + match gl.get_type() { + gl::GlType::Gl => { + let queries = gl.gen_queries(MAX_EVENTS_PER_FRAME as gl::GLint); + GpuFrameProfile { + gl: gl, + queries: queries, + samples: Vec::new(), + next_query: 0, + pending_query: 0, + frame_id: FrameId(0), + } + } + gl::GlType::Gles => { + GpuFrameProfile { + gl: gl, + queries: Vec::new(), + samples: Vec::new(), + next_query: 0, + pending_query: 0, + frame_id: FrameId(0), + } + } } } @@ -541,19 +560,30 @@ impl GpuFrameProfile { self.samples.clear(); } - #[cfg(not(target_os = "android"))] fn end_frame(&mut self) { - if self.pending_query != 0 { - self.gl.end_query(gl::TIME_ELAPSED); + match self.gl.get_type() { + gl::GlType::Gl => { + if self.pending_query != 0 { + self.gl.end_query(gl::TIME_ELAPSED); + } + } + gl::GlType::Gles => {}, } } - #[cfg(target_os = "android")] - fn end_frame(&mut self) { + fn add_marker(&mut self, tag: T) -> GpuMarker + where T: NamedTag { + match self.gl.get_type() { + gl::GlType::Gl => { + self.add_marker_gl(tag) + } + gl::GlType::Gles => { + self.add_marker_gles(tag) + } + } } - #[cfg(not(target_os = "android"))] - fn add_marker(&mut self, tag: T) -> GpuMarker + fn add_marker_gl(&mut self, tag: T) -> GpuMarker where T: NamedTag { if self.pending_query != 0 { self.gl.end_query(gl::TIME_ELAPSED); @@ -576,20 +606,32 @@ impl GpuFrameProfile { marker } - #[cfg(target_os = "android")] - fn add_marker(&mut self, tag: T) { + fn add_marker_gles(&mut self, tag: T) -> GpuMarker + where T: NamedTag { + let marker = GpuMarker::new(&self.gl, tag.get_label()); self.samples.push(GpuSample { tag: tag, time_ns: 0, }); + marker } fn is_valid(&self) -> bool { self.next_query > 0 && self.next_query <= MAX_EVENTS_PER_FRAME } - #[cfg(not(target_os = "android"))] fn build_samples(&mut self) -> Vec> { + match self.gl.get_type() { + gl::GlType::Gl => { + self.build_samples_gl() + } + gl::GlType::Gles => { + self.build_samples_gles() + } + } + } + + fn build_samples_gl(&mut self) -> Vec> { for (index, sample) in self.samples.iter_mut().enumerate() { sample.time_ns = self.gl.get_query_object_ui64v(self.queries[index], gl::QUERY_RESULT) } @@ -597,20 +639,19 @@ impl GpuFrameProfile { mem::replace(&mut self.samples, Vec::new()) } - #[cfg(target_os = "android")] - fn build_samples(&mut self) -> Vec> { + fn build_samples_gles(&mut self) -> Vec> { mem::replace(&mut self.samples, Vec::new()) } } impl Drop for GpuFrameProfile { - #[cfg(not(target_os = "android"))] - fn drop(&mut self) { - self.gl.delete_queries(&self.queries); - } - - #[cfg(target_os = "android")] fn drop(&mut self) { + match self.gl.get_type() { + gl::GlType::Gl => { + self.gl.delete_queries(&self.queries); + } + gl::GlType::Gles => {}, + } } } @@ -652,16 +693,10 @@ impl GpuProfiler { self.next_frame = (self.next_frame + 1) % MAX_PROFILE_FRAMES; } - #[cfg(not(target_os = "android"))] pub fn add_marker(&mut self, tag: T) -> GpuMarker where T: NamedTag { self.frames[self.next_frame].add_marker(tag) } - - #[cfg(target_os = "android")] - pub fn add_marker(&mut self, tag: T) { - self.frames[self.next_frame].add_marker(tag) - } } #[must_use] @@ -669,36 +704,42 @@ pub struct GpuMarker{ gl: Rc, } -#[cfg(any(target_arch="arm", target_arch="aarch64"))] -impl GpuMarker { - pub fn new(gl: &Rc, _: &str) -> GpuMarker { - GpuMarker{ - gl: gl.clone(), - } - } - - pub fn fire(gl: &gl::Gl, _: &str) {} -} - - -#[cfg(not(any(target_arch="arm", target_arch="aarch64")))] impl GpuMarker { pub fn new(gl: &Rc, message: &str) -> GpuMarker { - gl.push_group_marker_ext(message); - GpuMarker{ - gl: gl.clone(), + match gl.get_type() { + gl::GlType::Gl => { + gl.push_group_marker_ext(message); + GpuMarker{ + gl: gl.clone(), + } + } + gl::GlType::Gles => { + GpuMarker{ + gl: gl.clone(), + } + } } } pub fn fire(gl: &gl::Gl, message: &str) { - gl.insert_event_marker_ext(message); + match gl.get_type() { + gl::GlType::Gl => { + gl.insert_event_marker_ext(message); + } + gl::GlType::Gles => {}, + } } } #[cfg(not(any(target_arch="arm", target_arch="aarch64")))] impl Drop for GpuMarker { fn drop(&mut self) { - self.gl.pop_group_marker_ext(); + match self.gl.get_type() { + gl::GlType::Gl => { + self.gl.pop_group_marker_ext(); + } + gl::GlType::Gles => {}, + } } } @@ -919,7 +960,7 @@ impl Device { debug!("compile {:?}", name); let mut s = String::new(); - s.push_str(SHADER_VERSION); + s.push_str(get_shader_version(gl)); for prefix in shader_preamble { s.push_str(&prefix); } @@ -1139,7 +1180,7 @@ impl Device { texture.mode = mode; } - let (internal_format, gl_format) = gl_texture_formats_for_image_format(format); + let (internal_format, gl_format) = gl_texture_formats_for_image_format(self.gl(), format); let type_ = gl_type_for_texture_format(format); match mode { @@ -1204,7 +1245,7 @@ impl Device { return; } - let (internal_format, gl_format) = gl_texture_formats_for_image_format(texture.format); + let (internal_format, gl_format) = gl_texture_formats_for_image_format(&*self.gl, texture.format); let type_ = gl_type_for_texture_format(texture.format); self.gl.tex_image_3d(texture_id.target, @@ -1348,7 +1389,7 @@ impl Device { self.bind_texture(DEFAULT_TEXTURE, texture_id); let texture = self.textures.get_mut(&texture_id).unwrap(); - let (internal_format, gl_format) = gl_texture_formats_for_image_format(texture.format); + let (internal_format, gl_format) = gl_texture_formats_for_image_format(&*self.gl, texture.format); let type_ = gl_type_for_texture_format(texture.format); self.gl.tex_image_2d(texture_id.target, @@ -1661,13 +1702,13 @@ impl Device { expanded_data.push(*byte); expanded_data.push(*byte); } - (GL_FORMAT_BGRA, 4, expanded_data.as_slice()) + (get_gl_format_bgra(self.gl()), 4, expanded_data.as_slice()) } else { (GL_FORMAT_A, 1, data) } } ImageFormat::RGB8 => (gl::RGB, 3, data), - ImageFormat::RGBA8 => (GL_FORMAT_BGRA, 4, data), + ImageFormat::RGBA8 => (get_gl_format_bgra(self.gl()), 4, data), ImageFormat::Invalid | ImageFormat::RGBAF32 => unreachable!(), }; @@ -2034,21 +2075,24 @@ impl Drop for Device { } } -fn gl_texture_formats_for_image_format(format: ImageFormat) -> (gl::GLint, gl::GLuint) { +fn gl_texture_formats_for_image_format(gl: &gl::Gl, format: ImageFormat) -> (gl::GLint, gl::GLuint) { match format { ImageFormat::A8 => { if cfg!(any(target_arch="arm", target_arch="aarch64")) { - (GL_FORMAT_BGRA as gl::GLint, GL_FORMAT_BGRA) + (get_gl_format_bgra(gl) as gl::GLint, get_gl_format_bgra(gl)) } else { (GL_FORMAT_A as gl::GLint, GL_FORMAT_A) } }, ImageFormat::RGB8 => (gl::RGB as gl::GLint, gl::RGB), ImageFormat::RGBA8 => { - if cfg!(any(target_arch="arm", target_arch="aarch64")) { - (GL_FORMAT_BGRA as gl::GLint, GL_FORMAT_BGRA) - } else { - (gl::RGBA as gl::GLint, GL_FORMAT_BGRA) + match gl.get_type() { + gl::GlType::Gl => { + (gl::RGBA as gl::GLint, get_gl_format_bgra(gl)) + } + gl::GlType::Gles => { + (get_gl_format_bgra(gl) as gl::GLint, get_gl_format_bgra(gl)) + } } } ImageFormat::RGBAF32 => (gl::RGBA32F as gl::GLint, gl::RGBA),