From 266a082206fe55f7d49163a015c2a65c4a360a8b Mon Sep 17 00:00:00 2001 From: "Ngo Iok Ui (Wu Yu Wei)" Date: Fri, 26 Jan 2024 19:25:12 +0900 Subject: [PATCH] Update Servo::get_events to return drain type (#31181) * Update Servo::get_events to return drain type to prevent constant allocation * Remove type alias for tlwc * Fix libsimpleservo --- components/servo/lib.rs | 13 +++++++------ ports/libsimpleservo/api/src/lib.rs | 17 +++++++++++++---- ports/servoshell/app.rs | 2 +- ports/servoshell/webview.rs | 3 ++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/components/servo/lib.rs b/components/servo/lib.rs index 11af054132a..fdddd33e1df 100644 --- a/components/servo/lib.rs +++ b/components/servo/lib.rs @@ -23,6 +23,7 @@ use std::collections::HashMap; use std::path::PathBuf; use std::rc::Rc; use std::sync::{Arc, Mutex}; +use std::vec::Drain; use bluetooth::BluetoothThreadFactory; use bluetooth_traits::BluetoothRequest; @@ -64,7 +65,7 @@ pub use gleam::gl; use ipc_channel::ipc::{self, IpcSender}; use log::{error, trace, warn, Log, Metadata, Record}; use media::{GLPlayerThreads, WindowGLContext}; -pub use msg::constellation_msg::TopLevelBrowsingContextId as BrowserId; +pub use msg::constellation_msg::TopLevelBrowsingContextId; use msg::constellation_msg::{PipelineNamespace, PipelineNamespaceId}; use net::resource_thread::new_resource_threads; use net_traits::IpcSend; @@ -162,7 +163,7 @@ pub struct Servo { compositor: IOCompositor, constellation_chan: Sender, embedder_receiver: EmbedderReceiver, - messages_for_embedder: Vec<(Option, EmbedderMsg)>, + messages_for_embedder: Vec<(Option, EmbedderMsg)>, profiler_enabled: bool, /// For single-process Servo instances, this field controls the initialization /// and deinitialization of the JS Engine. Multiprocess Servo instances have their @@ -213,7 +214,7 @@ impl webrender_api::RenderNotifier for RenderNotifier { pub struct InitializedServo { pub servo: Servo, - pub browser_id: BrowserId, + pub browser_id: TopLevelBrowsingContextId, } impl Servo @@ -282,7 +283,7 @@ where // Reserving a namespace to create TopLevelBrowsingContextId. PipelineNamespace::install(PipelineNamespaceId(0)); - let top_level_browsing_context_id = BrowserId::new(); + let top_level_browsing_context_id = TopLevelBrowsingContextId::new(); // Get both endpoints of a special channel for communication between // the client window and the compositor. This channel is unique because @@ -722,8 +723,8 @@ where } } - pub fn get_events(&mut self) -> Vec<(Option, EmbedderMsg)> { - ::std::mem::replace(&mut self.messages_for_embedder, Vec::new()) + pub fn get_events(&mut self) -> Drain<'_, (Option, EmbedderMsg)> { + self.messages_for_embedder.drain(..) } pub fn handle_events(&mut self, events: impl IntoIterator) -> bool { diff --git a/ports/libsimpleservo/api/src/lib.rs b/ports/libsimpleservo/api/src/lib.rs index 069e5745ae5..6c38eb0891f 100644 --- a/ports/libsimpleservo/api/src/lib.rs +++ b/ports/libsimpleservo/api/src/lib.rs @@ -40,7 +40,7 @@ pub use servo::webrender_api::units::DeviceIntRect; use servo::webrender_api::units::DevicePixel; use servo::webrender_api::ScrollLocation; use servo::webrender_surfman::WebrenderSurfman; -use servo::{self, gl, BrowserId, Servo}; +use servo::{self, gl, Servo, TopLevelBrowsingContextId}; use servo_media::player::context as MediaPlayerContext; use surfman::{Connection, SurfaceType}; @@ -336,7 +336,7 @@ pub fn deinit() { } impl ServoGlue { - fn get_browser_id(&self) -> Result { + fn get_browser_id(&self) -> Result { let webview_id = match self.focused_webview_id { Some(id) => id, None => return Err("No focused WebViewId yet."), @@ -646,6 +646,8 @@ impl ServoGlue { } fn handle_servo_events(&mut self) -> Result<(), &'static str> { + let mut need_update = false; + let mut need_present = false; for (browser_id, event) in self.servo.get_events() { match event { EmbedderMsg::ChangePageTitle(title) => { @@ -660,7 +662,7 @@ impl ServoGlue { let window_event = EmbedderEvent::AllowNavigationResponse(pipeline_id, data); self.events.push(window_event); - let _ = self.perform_updates(); + need_update = true; } }, EmbedderMsg::HistoryChanged(entries, current) => { @@ -824,7 +826,7 @@ impl ServoGlue { self.callbacks.host_callbacks.on_panic(reason, backtrace); }, EmbedderMsg::ReadyToPresent => { - self.servo.present(); + need_present = true; }, EmbedderMsg::Status(..) | EmbedderMsg::SelectFiles(..) | @@ -839,6 +841,13 @@ impl ServoGlue { EmbedderMsg::EventDelivered(..) => {}, } } + + if need_update { + let _ = self.perform_updates(); + } + if need_present { + self.servo.present(); + } Ok(()) } } diff --git a/ports/servoshell/app.rs b/ports/servoshell/app.rs index 59b7709769f..e02bd165043 100644 --- a/ports/servoshell/app.rs +++ b/ports/servoshell/app.rs @@ -462,7 +462,7 @@ impl App { // Take any new embedder messages from Servo itself. embedder_messages = self.servo.as_mut().unwrap().get_events(); - if embedder_messages.is_empty() { + if embedder_messages.len() == 0 { break; } } diff --git a/ports/servoshell/webview.rs b/ports/servoshell/webview.rs index cf0400faff7..4b21a182beb 100644 --- a/ports/servoshell/webview.rs +++ b/ports/servoshell/webview.rs @@ -7,6 +7,7 @@ use std::fs::File; use std::io::Write; use std::rc::Rc; use std::time::Duration; +use std::vec::Drain; use std::{env, thread}; use arboard::Clipboard; @@ -292,7 +293,7 @@ where /// Returns true if the caller needs to manually present a new frame. pub fn handle_servo_events( &mut self, - events: Vec<(Option, EmbedderMsg)>, + events: Drain<'_, (Option, EmbedderMsg)>, ) -> ServoEventResponse { let mut need_present = false; let mut history_changed = false;