Stall PaintTask exit until it can release all buffers

It is possible for a PaintTask to start exiting soon after sending new
buffers to the compositor. In that case, the compositor should return
the now unnecessary buffers to the PaintTask so that it can properly
free them.

To accomplish this, the compositor now keeps a hash map of paint task
channels per pipeline id. When a PaintTask exists, the constellation
informs the compositor that it can forget about it. Additionally, the
PaintTask should not wait for any buffers when the engine is doing a
complete shutdown. In that case, the compositor is already halted and
has simply let all buffers leak. We pipe through the shutdown type when
destroying the pipeline to make this decision.

Fixes #2641.
This commit is contained in:
Martin Robinson 2015-01-02 09:34:52 -08:00
parent 141b5d038f
commit c0b397322f
13 changed files with 180 additions and 103 deletions

View file

@ -129,6 +129,9 @@ pub struct IOCompositor<Window: WindowMethods> {
/// Pending scroll events.
pending_scroll_events: Vec<ScrollEvent>,
/// PaintTask channels that we know about, used to return unused buffers.
paint_channels: HashMap<PipelineId, PaintChan>,
}
pub struct ScrollEvent {
@ -198,6 +201,7 @@ impl<Window: WindowMethods> IOCompositor<Window> {
fragment_point: None,
outstanding_paint_msgs: 0,
last_composite_time: 0,
paint_channels: HashMap::new(),
}
}
@ -341,6 +345,12 @@ impl<Window: WindowMethods> IOCompositor<Window> {
self.window.set_cursor(cursor)
}
(Msg::PaintTaskExited(pipeline_id), ShutdownState::NotShuttingDown) => {
if self.paint_channels.remove(&pipeline_id).is_none() {
panic!("Saw PaintTaskExited message from an unknown pipeline!");
}
}
// When we are shutting_down, we need to avoid performing operations
// such as Paint that may crash because we have begun tearing down
// the rest of our resources.
@ -456,6 +466,38 @@ impl<Window: WindowMethods> IOCompositor<Window> {
self.composite_if_necessary();
}
fn create_root_layer_for_pipeline_and_rect(&mut self,
pipeline: &CompositionPipeline,
frame_rect: Option<TypedRect<PagePx, f32>>)
-> Rc<Layer<CompositorData>> {
let layer_properties = LayerProperties {
pipeline_id: pipeline.id,
epoch: Epoch(0),
id: LayerId::null(),
rect: Rect::zero(),
background_color: azure_hl::Color::new(0., 0., 0., 0.),
scroll_policy: Scrollable,
};
let root_layer = CompositorData::new_layer(pipeline.clone(),
layer_properties,
WantsScrollEventsFlag::WantsScrollEvents,
opts::get().tile_size);
if !self.paint_channels.contains_key(&pipeline.id) {
self.paint_channels.insert(pipeline.id, pipeline.paint_chan.clone());
}
// All root layers mask to bounds.
*root_layer.masks_to_bounds.borrow_mut() = true;
if let Some(ref frame_rect) = frame_rect {
let frame_rect = frame_rect.to_untyped();
*root_layer.bounds.borrow_mut() = Rect::from_untyped(&frame_rect);
}
return root_layer;
}
fn create_frame_tree_root_layers(&mut self,
frame_tree: &SendableFrameTree,
frame_rect: Option<TypedRect<PagePx, f32>>)
@ -464,7 +506,8 @@ impl<Window: WindowMethods> IOCompositor<Window> {
self.ready_states.insert(frame_tree.pipeline.id, Blank);
self.paint_states.insert(frame_tree.pipeline.id, PaintingPaintState);
let root_layer = create_root_layer_for_pipeline_and_rect(&frame_tree.pipeline, frame_rect);
let root_layer = self.create_root_layer_for_pipeline_and_rect(&frame_tree.pipeline,
frame_rect);
for kid in frame_tree.children.iter() {
root_layer.add_child(self.create_frame_tree_root_layers(&kid.frame_tree, kid.rect));
}
@ -474,8 +517,8 @@ impl<Window: WindowMethods> IOCompositor<Window> {
fn update_frame_tree(&mut self, frame_tree_diff: &FrameTreeDiff) {
let parent_layer = self.find_pipeline_root_layer(frame_tree_diff.parent_pipeline.id);
parent_layer.add_child(
create_root_layer_for_pipeline_and_rect(&frame_tree_diff.pipeline,
frame_tree_diff.rect));
self.create_root_layer_for_pipeline_and_rect(&frame_tree_diff.pipeline,
frame_tree_diff.rect));
}
fn find_pipeline_root_layer(&self, pipeline_id: PipelineId) -> Rc<Layer<CompositorData>> {
@ -614,6 +657,24 @@ impl<Window: WindowMethods> IOCompositor<Window> {
layer_id: LayerId,
new_layer_buffer_set: Box<LayerBufferSet>,
epoch: Epoch) {
match self.find_layer_with_pipeline_and_layer_id(pipeline_id, layer_id) {
Some(layer) => self.paint_to_layer(layer, new_layer_buffer_set, epoch),
None => {
match self.paint_channels.entry(pipeline_id) {
Occupied(entry) => {
let message = UnusedBufferMsg(new_layer_buffer_set.buffers);
let _ = entry.get().send_opt(message);
},
Vacant(_) => panic!("Received a buffer from an unknown pipeline!"),
}
}
}
}
fn paint_to_layer(&mut self,
layer: Rc<Layer<CompositorData>>,
new_layer_buffer_set: Box<LayerBufferSet>,
epoch: Epoch) {
debug!("compositor received new frame at size {}x{}",
self.window_size.width.get(),
self.window_size.height.get());
@ -622,20 +683,10 @@ impl<Window: WindowMethods> IOCompositor<Window> {
let mut new_layer_buffer_set = new_layer_buffer_set;
new_layer_buffer_set.mark_will_leak();
match self.find_layer_with_pipeline_and_layer_id(pipeline_id, layer_id) {
Some(ref layer) => {
// FIXME(pcwalton): This is going to cause problems with inconsistent frames since
// we only composite one layer at a time.
assert!(layer.add_buffers(new_layer_buffer_set, epoch));
self.composite_if_necessary();
}
None => {
// FIXME: This may potentially be triggered by a race condition where a
// buffers are being painted but the layer is removed before painting
// completes.
panic!("compositor given paint command for non-existent layer");
}
}
// FIXME(pcwalton): This is going to cause problems with inconsistent frames since
// we only composite one layer at a time.
assert!(layer.add_buffers(new_layer_buffer_set, epoch));
self.composite_if_necessary();
}
fn scroll_fragment_to_point(&mut self,
@ -1196,37 +1247,6 @@ fn find_layer_with_pipeline_and_layer_id_for_layer(layer: Rc<Layer<CompositorDat
return None;
}
fn create_root_layer_for_pipeline_and_rect(pipeline: &CompositionPipeline,
frame_rect: Option<TypedRect<PagePx, f32>>)
-> Rc<Layer<CompositorData>> {
let layer_properties = LayerProperties {
pipeline_id: pipeline.id,
epoch: Epoch(0),
id: LayerId::null(),
rect: Rect::zero(),
background_color: azure_hl::Color::new(0., 0., 0., 0.),
scroll_policy: Scrollable,
};
let root_layer = CompositorData::new_layer(pipeline.clone(),
layer_properties,
WantsScrollEventsFlag::WantsScrollEvents,
opts::get().tile_size);
// All root layers mask to bounds.
*root_layer.masks_to_bounds.borrow_mut() = true;
match frame_rect {
Some(ref frame_rect) => {
let frame_rect = frame_rect.to_untyped();
*root_layer.bounds.borrow_mut() = Rect::from_untyped(&frame_rect);
}
None => {}
}
return root_layer;
}
impl<Window> CompositorEventListener for IOCompositor<Window> where Window: WindowMethods {
fn handle_event(&mut self, msg: WindowEvent) -> bool {
// Check for new messages coming from the other tasks in the system.

View file

@ -216,6 +216,8 @@ pub enum Msg {
KeyEvent(Key, KeyModifiers),
/// Changes the cursor.
SetCursor(Cursor),
/// Informs the compositor that the paint task for the given pipeline has exited.
PaintTaskExited(PipelineId),
}
impl Show for Msg {
@ -240,6 +242,7 @@ impl Show for Msg {
Msg::ScrollTimeout(..) => write!(f, "ScrollTimeout"),
Msg::KeyEvent(..) => write!(f, "KeyEvent"),
Msg::SetCursor(..) => write!(f, "SetCursor"),
Msg::PaintTaskExited(..) => write!(f, "PaintTaskExited"),
}
}
}

View file

@ -11,11 +11,10 @@ use devtools_traits::DevtoolsControlChan;
use geom::rect::{Rect, TypedRect};
use geom::scale_factor::ScaleFactor;
use gfx::font_cache_task::FontCacheTask;
use gfx::paint_task;
use layers::geometry::DevicePixel;
use layout_traits::{LayoutControlChan, LayoutTaskFactory, ExitNowMsg};
use layout_traits::LayoutTaskFactory;
use libc;
use script_traits::{mod, GetTitleMsg, ResizeMsg, ResizeInactiveMsg, ExitPipelineMsg, SendEventMsg};
use script_traits::{mod, GetTitleMsg, ResizeMsg, ResizeInactiveMsg, SendEventMsg};
use script_traits::{ScriptControlChan, ScriptTaskFactory};
use servo_msg::compositor_msg::LayerId;
use servo_msg::constellation_msg::{mod, ConstellationChan, ExitMsg, FailureMsg, Failure};
@ -23,7 +22,7 @@ use servo_msg::constellation_msg::{FrameRectMsg, GetPipelineTitleMsg};
use servo_msg::constellation_msg::{IFrameSandboxState, IFrameUnsandboxed, InitLoadUrlMsg};
use servo_msg::constellation_msg::{KeyEvent, Key, KeyState, KeyModifiers, LoadCompleteMsg};
use servo_msg::constellation_msg::{LoadData, LoadUrlMsg, NavigateMsg, NavigationType};
use servo_msg::constellation_msg::{PainterReadyMsg, PipelineId, ResizedWindowMsg};
use servo_msg::constellation_msg::{PainterReadyMsg, PipelineExitType, PipelineId, ResizedWindowMsg};
use servo_msg::constellation_msg::{ScriptLoadedURLInIFrameMsg, SetCursorMsg, SubpageId};
use servo_msg::constellation_msg::{WindowSizeData};
use servo_msg::constellation_msg::Msg as ConstellationMsg;
@ -515,7 +514,7 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
fn handle_exit(&mut self) {
for (_id, ref pipeline) in self.pipelines.iter() {
pipeline.exit();
pipeline.exit(PipelineExitType::Complete);
}
self.image_cache_task.exit();
self.resource_task.send(resource_task::Exit);
@ -547,14 +546,8 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
Some(pipeline) => pipeline.clone()
};
fn force_pipeline_exit(old_pipeline: &Rc<Pipeline>) {
let ScriptControlChan(ref old_script) = old_pipeline.script_chan;
let _ = old_script.send_opt(ExitPipelineMsg(old_pipeline.id));
let _ = old_pipeline.paint_chan.send_opt(paint_task::ExitMsg(None));
let LayoutControlChan(ref old_layout) = old_pipeline.layout_chan;
let _ = old_layout.send_opt(ExitNowMsg);
}
force_pipeline_exit(&old_pipeline);
old_pipeline.force_exit();
self.compositor_proxy.send(CompositorMsg::PaintTaskExited(old_pipeline.id));
self.pipelines.remove(&pipeline_id);
loop {
@ -564,7 +557,8 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
match idx {
Some(idx) => {
debug!("removing pending frame change for failed pipeline");
force_pipeline_exit(&self.pending_frames[idx].after.pipeline);
self.pending_frames[idx].after.pipeline.force_exit();
self.compositor_proxy.send(CompositorMsg::PaintTaskExited(old_pipeline.id));
self.pending_frames.remove(idx);
},
None => break,
@ -987,7 +981,8 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
// TODO(tkuehn): should only exit once per unique script task,
// and then that script task will handle sub-exits
for frame_tree in frame_tree.iter() {
frame_tree.pipeline.exit();
frame_tree.pipeline.exit(PipelineExitType::PipelineOnly);
self.compositor_proxy.send(CompositorMsg::PaintTaskExited(frame_tree.pipeline.id));
self.pipelines.remove(&frame_tree.pipeline.id);
}
}

View file

@ -111,6 +111,7 @@ impl CompositorEventListener for NullCompositor {
Msg::ChangePageLoadData(..) |
Msg::KeyEvent(..) |
Msg::SetCursor(..) => {}
Msg::PaintTaskExited(..) => {}
}
true
}

View file

@ -5,7 +5,7 @@
#![comment = "The Servo Parallel Browser Project"]
#![license = "MPL"]
#![feature(globs, phase, macro_rules)]
#![feature(globs, phase, macro_rules, if_let)]
#![deny(unused_imports)]
#![deny(unused_variables)]

View file

@ -3,15 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use CompositorProxy;
use layout_traits::{LayoutTaskFactory, LayoutControlChan};
use layout_traits::{ExitNowMsg, LayoutTaskFactory, LayoutControlChan};
use script_traits::{ScriptControlChan, ScriptTaskFactory};
use script_traits::{AttachLayoutMsg, LoadMsg, NewLayoutInfo, ExitPipelineMsg};
use devtools_traits::DevtoolsControlChan;
use gfx::paint_task;
use gfx::paint_task::{PaintPermissionGranted, PaintPermissionRevoked};
use gfx::paint_task::{PaintChan, PaintTask};
use servo_msg::constellation_msg::{ConstellationChan, Failure, PipelineId, SubpageId};
use servo_msg::constellation_msg::{LoadData, WindowSizeData};
use servo_msg::constellation_msg::{ConstellationChan, ExitMsg, Failure, PipelineId, SubpageId};
use servo_msg::constellation_msg::{LoadData, WindowSizeData, PipelineExitType};
use servo_net::image_cache_task::ImageCacheTask;
use gfx::font_cache_task::FontCacheTask;
use servo_net::resource_task::ResourceTask;
@ -173,18 +174,27 @@ impl Pipeline {
let _ = self.paint_chan.send_opt(PaintPermissionRevoked);
}
pub fn exit(&self) {
pub fn exit(&self, exit_type: PipelineExitType) {
debug!("pipeline {} exiting", self.id);
// Script task handles shutting down layout, and layout handles shutting down the painter.
// For now, if the script task has failed, we give up on clean shutdown.
let ScriptControlChan(ref chan) = self.script_chan;
if chan.send_opt(ExitPipelineMsg(self.id)).is_ok() {
if chan.send_opt(ExitPipelineMsg(self.id, exit_type)).is_ok() {
// Wait until all slave tasks have terminated and run destructors
// NOTE: We don't wait for script task as we don't always own it
let _ = self.paint_shutdown_port.recv_opt();
let _ = self.layout_shutdown_port.recv_opt();
}
}
pub fn force_exit(&self) {
let ScriptControlChan(ref script_channel) = self.script_chan;
let _ = script_channel.send_opt( ExitPipelineMsg(self.id, PipelineExitType::PipelineOnly));
let _ = self.paint_chan.send_opt(paint_task::ExitMsg(None, PipelineExitType::PipelineOnly));
let LayoutControlChan(ref layout_channel) = self.layout_chan;
let _ = layout_channel.send_opt(ExitNowMsg(PipelineExitType::PipelineOnly));
}
pub fn to_sendable(&self) -> CompositionPipeline {