Skip to content

Commit

Permalink
Ensure render and layout tasks get destructed before main thread fini…
Browse files Browse the repository at this point in the history
…shes

Fixes #1097.
  • Loading branch information
dhedlund committed Dec 15, 2013
1 parent 4440476 commit e7a591a
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 96 deletions.
83 changes: 44 additions & 39 deletions src/components/gfx/render_task.rs
Expand Up @@ -136,47 +136,52 @@ impl<C: RenderListener + Send,T:Send+Freeze> RenderTask<C,T> {
compositor: C,
constellation_chan: ConstellationChan,
opts: Opts,
profiler_chan: ProfilerChan) {
do spawn_with((port, compositor, constellation_chan, opts, profiler_chan))
|(port, compositor, constellation_chan, opts, profiler_chan)| {

let native_graphics_context = compositor.get_graphics_metadata().map(
|md| NativePaintingGraphicsContext::from_metadata(&md));
let cpu_painting = opts.cpu_painting;

// FIXME: rust/#5967
let mut render_task = RenderTask {
id: id,
port: port,
compositor: compositor,
constellation_chan: constellation_chan,
font_ctx: ~FontContext::new(opts.render_backend.clone(),
false,
profiler_chan.clone()),
opts: opts,
profiler_chan: profiler_chan,

graphics_context: if cpu_painting {
CpuGraphicsContext
} else {
GpuGraphicsContext
},

native_graphics_context: native_graphics_context,

render_layer: None,

paint_permission: false,
epoch: Epoch(0),
buffer_map: BufferMap::new(10000000),
};
profiler_chan: ProfilerChan,
shutdown_chan: Chan<()>) {
do spawn_with((port, compositor, constellation_chan, opts, profiler_chan, shutdown_chan))
|(port, compositor, constellation_chan, opts, profiler_chan, shutdown_chan)| {

{ // Ensures RenderTask and graphics context are destroyed before shutdown msg
let native_graphics_context = compositor.get_graphics_metadata().map(
|md| NativePaintingGraphicsContext::from_metadata(&md));
let cpu_painting = opts.cpu_painting;

// FIXME: rust/#5967
let mut render_task = RenderTask {
id: id,
port: port,
compositor: compositor,
constellation_chan: constellation_chan,
font_ctx: ~FontContext::new(opts.render_backend.clone(),
false,
profiler_chan.clone()),
opts: opts,
profiler_chan: profiler_chan,

graphics_context: if cpu_painting {
CpuGraphicsContext
} else {
GpuGraphicsContext
},

native_graphics_context: native_graphics_context,

render_layer: None,

render_task.start();
paint_permission: false,
epoch: Epoch(0),
buffer_map: BufferMap::new(10000000),
};

render_task.start();

// Destroy all the buffers.
render_task.native_graphics_context.as_ref().map(|ctx|
render_task.buffer_map.clear(ctx)
);
}

// Destroy all the buffers.
render_task.native_graphics_context.as_ref().map(|ctx|
render_task.buffer_map.clear(ctx)
);
shutdown_chan.send(());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/components/main/compositing/compositor_layer.rs
Expand Up @@ -15,7 +15,7 @@ use layers::layers::TextureLayerKind;
use layers::platform::surface::{NativeCompositingGraphicsContext, NativeSurfaceMethods};
use layers::texturegl::{Texture, TextureTarget};
#[cfg(target_os="macos")] use layers::texturegl::TextureTargetRectangle;
use pipeline::Pipeline;
use pipeline::CompositionPipeline;
use script::dom::event::{ClickEvent, MouseDownEvent, MouseUpEvent};
use script::script_task::SendEventMsg;
use servo_msg::compositor_msg::{LayerBuffer, LayerBufferSet, Epoch, Tile};
Expand All @@ -34,7 +34,7 @@ use layers::texturegl::TextureTarget2D;
/// Each layer can also have child layers.
pub struct CompositorLayer {
/// This layer's pipeline. BufferRequests and mouse events will be sent through this.
pipeline: Pipeline,
pipeline: CompositionPipeline,

/// The size of the underlying page in page coordinates. This is an option
/// because we may not know the size of the page until layout is finished completely.
Expand Down Expand Up @@ -104,7 +104,7 @@ enum ScrollBehavior {
impl CompositorLayer {
/// Creates a new CompositorLayer with an optional page size. If no page size is given,
/// the layer is initially hidden and initialized without a quadtree.
pub fn new(pipeline: Pipeline,
pub fn new(pipeline: CompositionPipeline,
page_size: Option<Size2D<f32>>,
tile_size: uint,
max_mem: Option<uint>,
Expand Down Expand Up @@ -669,7 +669,7 @@ impl CompositorLayer {
}

// Adds a child.
pub fn add_child(&mut self, pipeline: Pipeline, page_size: Option<Size2D<f32>>, tile_size: uint,
pub fn add_child(&mut self, pipeline: CompositionPipeline, page_size: Option<Size2D<f32>>, tile_size: uint,
max_mem: Option<uint>, clipping_rect: Rect<f32>) {
let container = @mut ContainerLayer();
container.scissor = Some(clipping_rect);
Expand Down
31 changes: 21 additions & 10 deletions src/components/main/compositing/mod.rs
Expand Up @@ -5,7 +5,8 @@
pub use windowing;

use constellation::SendableFrameTree;
use windowing::WindowMethods;
use windowing::{ApplicationMethods, WindowMethods};
use platform::Application;

use azure::azure_hl::{SourceSurfaceMethods, Color};
use geom::point::Point2D;
Expand Down Expand Up @@ -145,27 +146,38 @@ pub enum Msg {
SetUnRenderedColor(PipelineId, Color),
}

pub enum CompositorMode {
Windowed(Application),
Headless
}

pub struct CompositorTask {
mode: CompositorMode,
opts: Opts,
port: Port<Msg>,
constellation_chan: ConstellationChan,
profiler_chan: ProfilerChan,
shutdown_chan: SharedChan<()>,
}

impl CompositorTask {
pub fn new(opts: Opts,
port: Port<Msg>,
constellation_chan: ConstellationChan,
profiler_chan: ProfilerChan,
shutdown_chan: Chan<()>)
profiler_chan: ProfilerChan)
-> CompositorTask {

let mode: CompositorMode = if opts.headless {
Headless
} else {
Windowed(ApplicationMethods::new())
};

CompositorTask {
mode: mode,
opts: opts,
port: port,
constellation_chan: constellation_chan,
profiler_chan: profiler_chan,
shutdown_chan: SharedChan::new(shutdown_chan),
profiler_chan: profiler_chan
}
}

Expand All @@ -182,10 +194,9 @@ impl CompositorTask {
}

pub fn run(&self) {
if self.opts.headless {
run_headless::run_compositor(self);
} else {
run::run_compositor(self);
match self.mode {
Windowed(ref app) => run::run_compositor(self, app),
Headless => run_headless::run_compositor(self),
}
}
}
11 changes: 5 additions & 6 deletions src/components/main/compositing/run.rs
Expand Up @@ -4,8 +4,10 @@

use compositing::compositor_layer::CompositorLayer;
use compositing::*;

use platform::{Application, Window};
use windowing::{ApplicationMethods, WindowEvent, WindowMethods};

use windowing::{WindowEvent, WindowMethods};
use windowing::{IdleWindowEvent, ResizeWindowEvent, LoadUrlWindowEvent, MouseWindowEventClass};
use windowing::{ScrollWindowEvent, ZoomWindowEvent, NavigationWindowEvent, FinishedWindowEvent};
use windowing::{QuitWindowEvent, MouseWindowClickEvent, MouseWindowMouseDownEvent, MouseWindowMouseUpEvent};
Expand Down Expand Up @@ -34,9 +36,8 @@ use std::rt::io::timer::Timer;
use std::vec;

/// Starts the compositor, which listens for messages on the specified port.
pub fn run_compositor(compositor: &CompositorTask) {
let app: Application = ApplicationMethods::new();
let window: @mut Window = WindowMethods::new(&app);
pub fn run_compositor(compositor: &CompositorTask, app: &Application) {
let window: @mut Window = WindowMethods::new(app);

// Create an initial layer tree.
//
Expand Down Expand Up @@ -419,8 +420,6 @@ pub fn run_compositor(compositor: &CompositorTask) {

}

compositor.shutdown_chan.send(());

// Clear out the compositor layers so that painting tasks can destroy the buffers.
match compositor_layer {
None => {}
Expand Down
1 change: 0 additions & 1 deletion src/components/main/compositing/run_headless.rs
Expand Up @@ -37,5 +37,4 @@ pub fn run_compositor(compositor: &CompositorTask) {
=> ()
}
}
compositor.shutdown_chan.send(())
}
6 changes: 3 additions & 3 deletions src/components/main/constellation.rs
Expand Up @@ -8,7 +8,7 @@ use extra::url::Url;
use geom::rect::Rect;
use geom::size::Size2D;
use gfx::opts::Opts;
use pipeline::Pipeline;
use pipeline::{Pipeline, CompositionPipeline};
use script::script_task::{ResizeMsg, ResizeInactiveMsg};
use servo_msg::constellation_msg::{ConstellationChan, ExitMsg, FailureMsg, FrameRectMsg};
use servo_msg::constellation_msg::{IFrameSandboxState, IFrameUnsandboxed, InitLoadUrlMsg};
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Clone for ChildFrameTree {
}

pub struct SendableFrameTree {
pipeline: Pipeline,
pipeline: CompositionPipeline,
children: ~[SendableChildFrameTree],
}

Expand Down Expand Up @@ -129,7 +129,7 @@ impl FrameTree {

fn to_sendable(&self) -> SendableFrameTree {
let sendable_frame_tree = SendableFrameTree {
pipeline: (*self.pipeline).clone(),
pipeline: self.pipeline.to_sendable(),
children: self.children.iter().map(|frame_tree| frame_tree.to_sendable()).collect(),
};
sendable_frame_tree
Expand Down
27 changes: 16 additions & 11 deletions src/components/main/layout/layout_task.rs
Expand Up @@ -207,18 +207,23 @@ impl LayoutTask {
render_chan: RenderChan<AbstractNode<()>>,
img_cache_task: ImageCacheTask,
opts: Opts,
profiler_chan: ProfilerChan) {
profiler_chan: ProfilerChan,
shutdown_chan: Chan<()>) {
spawn_with!(task::task(), [port, constellation_chan, script_chan,
render_chan, img_cache_task, profiler_chan], {
let mut layout = LayoutTask::new(id,
port,
constellation_chan,
script_chan,
render_chan,
img_cache_task,
&opts,
profiler_chan);
layout.start();
render_chan, img_cache_task, profiler_chan, shutdown_chan], {
{ // Ensures LayoutTask gets destroyed before we send the shutdown message
let mut layout = LayoutTask::new(id,
port,
constellation_chan,
script_chan,
render_chan,
img_cache_task,
&opts,
profiler_chan);
layout.start();
}

shutdown_chan.send(());
});
}

Expand Down

5 comments on commit e7a591a

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at dhedlund@e7a591a

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging dhedlund/servo/_1097_2 = e7a591a into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dhedlund/servo/_1097_2 = e7a591a merged ok, testing candidate = a31535a

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = a31535a

Please sign in to comment.