Bug 1371838 - add a custom thread listener to WR. r=jrmuizel
authorJerryShih <hshih@mozilla.com>
Sat, 11 Nov 2017 18:57:31 +0800
changeset 696845 3fe8d225b7baeb47a4c040d3db49025008f529a1
parent 696844 a6ad193d398eeb72b8a68a7db5cf684ec16e4a4b
child 696846 d352680181fc5034f86fa7658ca2d49d87ad7f79
push id88807
push userbmo:ato@sny.no
push dateSun, 12 Nov 2017 01:46:20 +0000
reviewersjrmuizel
bugs1371838
milestone58.0a1
Bug 1371838 - add a custom thread listener to WR. r=jrmuizel MozReview-Commit-ID: yyFwBr21F3
gfx/webrender_bindings/src/bindings.rs
--- a/gfx/webrender_bindings/src/bindings.rs
+++ b/gfx/webrender_bindings/src/bindings.rs
@@ -2,17 +2,17 @@ use std::ffi::{CStr, CString};
 use std::{mem, slice};
 use std::path::PathBuf;
 use std::ptr;
 use std::sync::Arc;
 use std::os::raw::{c_void, c_char, c_float};
 use gleam::gl;
 
 use webrender_api::*;
-use webrender::{ReadPixelsFormat, Renderer, RendererOptions};
+use webrender::{ReadPixelsFormat, Renderer, RendererOptions, ThreadListener};
 use webrender::{ExternalImage, ExternalImageHandler, ExternalImageSource};
 use webrender::DebugFlags;
 use webrender::{ApiRecordingReceiver, BinaryRecorder};
 use thread_profiler::register_thread_with_profiler;
 use moz2d_renderer::Moz2dImageRenderer;
 use app_units::Au;
 use rayon;
 use euclid::SideOffsets2D;
@@ -587,16 +587,40 @@ pub unsafe extern "C" fn wr_rendered_epo
     Box::from_raw(pipeline_epochs);
 }
 
 extern "C" {
     fn gecko_profiler_register_thread(name: *const ::std::os::raw::c_char);
     fn gecko_profiler_unregister_thread();
 }
 
+struct GeckoProfilerThreadListener {}
+
+impl GeckoProfilerThreadListener {
+    pub fn new() -> GeckoProfilerThreadListener {
+        GeckoProfilerThreadListener{}
+    }
+}
+
+impl ThreadListener for GeckoProfilerThreadListener {
+    fn thread_started(&self, thread_name: &str) {
+        let name = CString::new(thread_name).unwrap();
+        unsafe {
+            // gecko_profiler_register_thread copies the passed name here.
+            gecko_profiler_register_thread(name.as_ptr());
+        }
+    }
+
+    fn thread_stopped(&self, _: &str) {
+        unsafe {
+            gecko_profiler_unregister_thread();
+        }
+    }
+}
+
 pub struct WrThreadPool(Arc<rayon::ThreadPool>);
 
 #[no_mangle]
 pub unsafe extern "C" fn wr_thread_pool_new() -> *mut WrThreadPool {
     let worker_config = rayon::Configuration::new()
         .thread_name(|idx|{ format!("WRWorker#{}", idx) })
         .start_handler(|idx| {
             let name = format!("WRWorker#{}", idx);
@@ -655,16 +679,17 @@ pub extern "C" fn wr_window_new(window_i
     };
 
     let opts = RendererOptions {
         enable_aa: true,
         enable_subpixel_aa: true,
         recorder: recorder,
         blob_image_renderer: Some(Box::new(Moz2dImageRenderer::new(workers.clone()))),
         workers: Some(workers.clone()),
+        thread_listener: Some(Box::new(GeckoProfilerThreadListener::new())),
         enable_render_on_scroll: false,
         resource_override_path: unsafe {
             let override_charptr = gfx_wr_resource_path_override();
             if override_charptr.is_null() {
                 None
             } else {
                 match CStr::from_ptr(override_charptr).to_str() {
                     Ok(override_str) => Some(PathBuf::from(override_str)),