aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/main/pipeline.rs
blob: 1b64868877861020bf0d35e5d0af88081e3daa7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use compositing::CompositorChan;
use layout::layout_task::LayoutTask;

use extra::url::Url;
use geom::size::Size2D;
use gfx::opts::Opts;
use gfx::render_task::{PaintPermissionGranted, PaintPermissionRevoked};
use gfx::render_task::{RenderChan, RenderTask};
use layout::util::OpaqueNode;
use script::layout_interface::LayoutChan;
use script::script_task::LoadMsg;
use script::script_task::{AttachLayoutMsg, NewLayoutInfo, ScriptTask, ScriptChan};
use script::script_task;
use servo_msg::constellation_msg::{ConstellationChan, Failure, PipelineId, SubpageId};
use servo_net::image_cache_task::ImageCacheTask;
use servo_net::resource_task::ResourceTask;
use servo_util::time::ProfilerChan;

/// A uniquely-identifiable pipeline of script task, layout task, and render task. 
pub struct Pipeline {
    id: PipelineId,
    subpage_id: Option<SubpageId>,
    script_chan: ScriptChan,
    layout_chan: LayoutChan,
    render_chan: RenderChan<OpaqueNode>,
    layout_shutdown_port: Port<()>,
    render_shutdown_port: Port<()>,
    /// The most recently loaded url
    url: Option<Url>,
}

/// The subset of the pipeline that is needed for layer composition.
#[deriving(Clone)]
pub struct CompositionPipeline {
    id: PipelineId,
    script_chan: ScriptChan,
    render_chan: RenderChan<OpaqueNode>,
}

impl Pipeline {
    /// Starts a render task, layout task, and script task. Returns the channels wrapped in a
    /// struct.
    pub fn with_script(id: PipelineId,
                       subpage_id: Option<SubpageId>,
                       constellation_chan: ConstellationChan,
                       compositor_chan: CompositorChan,
                       image_cache_task: ImageCacheTask,
                       profiler_chan: ProfilerChan,
                       opts: Opts,
                       script_pipeline: &Pipeline)
                       -> Pipeline {
        let (layout_port, layout_chan) = LayoutChan::new();
        let (render_port, render_chan) = RenderChan::new();
        let (render_shutdown_port, render_shutdown_chan) = Chan::new();
        let (layout_shutdown_port, layout_shutdown_chan) = Chan::new();

        let failure = Failure {
            pipeline_id: id,
            subpage_id: subpage_id,
        };

        RenderTask::create(id,
                           render_port,
                           compositor_chan.clone(),
                           constellation_chan.clone(),
                           failure.clone(),
                           opts.clone(),
                           profiler_chan.clone(),
                           render_shutdown_chan);

        LayoutTask::create(id,
                           layout_port,
                           layout_chan.clone(),
                           constellation_chan,
                           failure,
                           script_pipeline.script_chan.clone(),
                           render_chan.clone(),
                           image_cache_task.clone(),
                           opts.clone(),
                           profiler_chan,
                           layout_shutdown_chan);

        let new_layout_info = NewLayoutInfo {
            old_id: script_pipeline.id.clone(),
            new_id: id,
            layout_chan: layout_chan.clone(),
        };

        script_pipeline.script_chan.send(AttachLayoutMsg(new_layout_info));

        Pipeline::new(id,
                      subpage_id,
                      script_pipeline.script_chan.clone(),
                      layout_chan,
                      render_chan,
                      layout_shutdown_port,
                      render_shutdown_port)
    }

    pub fn create(id: PipelineId,
                  subpage_id: Option<SubpageId>,
                  constellation_chan: ConstellationChan,
                  compositor_chan: CompositorChan,
                  image_cache_task: ImageCacheTask,
                  resource_task: ResourceTask,
                  profiler_chan: ProfilerChan,
                  window_size: Size2D<uint>,
                  opts: Opts)
                  -> Pipeline {
        let (script_port, script_chan) = ScriptChan::new();
        let (layout_port, layout_chan) = LayoutChan::new();
        let (render_port, render_chan) = RenderChan::new();
        let (render_shutdown_port, render_shutdown_chan) = Chan::new();
        let (layout_shutdown_port, layout_shutdown_chan) = Chan::new();
        let pipeline = Pipeline::new(id,
                                     subpage_id,
                                     script_chan.clone(),
                                     layout_chan.clone(),
                                     render_chan.clone(),
                                     layout_shutdown_port,
                                     render_shutdown_port);

        let failure = Failure {
            pipeline_id: id,
            subpage_id: subpage_id,
        };

        ScriptTask::create(id,
                           compositor_chan.clone(),
                           layout_chan.clone(),
                           script_port,
                           script_chan.clone(),
                           constellation_chan.clone(),
                           failure.clone(),
                           resource_task,
                           image_cache_task.clone(),
                           window_size);

        RenderTask::create(id,
                           render_port,
                           compositor_chan.clone(),
                           constellation_chan.clone(),
                           failure.clone(),
                           opts.clone(),
                           profiler_chan.clone(),
                           render_shutdown_chan);

        LayoutTask::create(id,
                           layout_port,
                           layout_chan.clone(),
                           constellation_chan,
                           failure,
                           script_chan.clone(),
                           render_chan.clone(),
                           image_cache_task,
                           opts.clone(),
                           profiler_chan,
                           layout_shutdown_chan);

        pipeline
    }

    pub fn new(id: PipelineId,
               subpage_id: Option<SubpageId>,
               script_chan: ScriptChan,
               layout_chan: LayoutChan,
               render_chan: RenderChan<OpaqueNode>,
               layout_shutdown_port: Port<()>,
               render_shutdown_port: Port<()>)
               -> Pipeline {
        Pipeline {
            id: id,
            subpage_id: subpage_id,
            script_chan: script_chan,
            layout_chan: layout_chan,
            render_chan: render_chan,
            layout_shutdown_port: layout_shutdown_port,
            render_shutdown_port: render_shutdown_port,
            url: None,
        }
    }

    pub fn load(&mut self, url: Url) {
        self.url = Some(url.clone());
        self.script_chan.send(LoadMsg(self.id, url));
    }

    pub fn grant_paint_permission(&self) {
        self.render_chan.try_send(PaintPermissionGranted);
    }

    pub fn revoke_paint_permission(&self) {
        debug!("pipeline revoking render channel paint permission");
        self.render_chan.try_send(PaintPermissionRevoked);
    }

    pub fn reload(&mut self) {
        self.url.clone().map(|url| {
            self.load(url);
        });
    }

    pub fn exit(&self) {
        // Script task handles shutting down layout, and layout handles shutting down the renderer.
        // For now, if the script task has failed, we give up on clean shutdown.
        if self.script_chan.try_send(script_task::ExitPipelineMsg(self.id)) {
            // 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
            self.render_shutdown_port.recv_opt();
            self.layout_shutdown_port.recv_opt();
        }
    }

    pub fn to_sendable(&self) -> CompositionPipeline {
        CompositionPipeline {
            id: self.id.clone(),
            script_chan: self.script_chan.clone(),
            render_chan: self.render_chan.clone(),
        }
    }
}