From 9818212faf22ec5d37b27b1adc3fba43fede6533 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Thu, 26 Jul 2012 16:21:56 -0700 Subject: [PATCH] servo: Merge #29 - Pipes (from eholk:pipes) Source-Repo: https://github.com/servo/servo Source-Revision: 0f1e778653fc59f511da111e840512d5bfebb819 --- servo/src/servo/content.rs | 4 +- servo/src/servo/engine.rs | 18 +++++--- servo/src/servo/gfx/pngsink.rs | 17 ++++--- servo/src/servo/gfx/renderer.rs | 63 +++++++++++++++----------- servo/src/servo/layout/layout_task.rs | 3 +- servo/src/servo/parser/css_builder.rs | 6 +-- servo/src/servo/parser/css_lexer.rs | 17 ++++--- servo/src/servo/parser/html_builder.rs | 8 ++-- servo/src/servo/platform/osmain.rs | 31 ++++++++----- servo/src/servo/servo.rc | 1 - servo/src/servo/servo.rs | 41 +++++++++-------- 11 files changed, 115 insertions(+), 94 deletions(-) diff --git a/servo/src/servo/content.rs b/servo/src/servo/content.rs index 82e163c15b8c..c5d03b73bed0 100644 --- a/servo/src/servo/content.rs +++ b/servo/src/servo/content.rs @@ -73,8 +73,8 @@ class Document { class Content { let sink: S; let layout: Layout; - let from_master: port; - let event_port: port; + let from_master: comm::port; + let event_port: comm::port; let scope: NodeScope; let jsrt: jsrt; diff --git a/servo/src/servo/engine.rs b/servo/src/servo/engine.rs index c6ffd644a1ca..8cc97b83e3c8 100644 --- a/servo/src/servo/engine.rs +++ b/servo/src/servo/engine.rs @@ -5,14 +5,16 @@ import layout::layout_task; import layout_task::Layout; import content::{Content, ExecuteMsg, ParseMsg, ExitMsg, create_content}; +import pipes::{port, chan}; + class Engine { let sink: S; let renderer: Renderer; let layout: Layout; - let content: chan; + let content: comm::chan; - new(sink: S) { + new(+sink: S) { self.sink = sink; let renderer = Renderer(sink); @@ -24,7 +26,7 @@ class Engine { self.content = content; } - fn start() -> chan { + fn start() -> comm::chan { do spawn_listener:: |request| { while self.handle_request(request.recv()) { // Go on... @@ -47,10 +49,12 @@ class Engine { ExitMsg(sender) { self.content.send(content::ExitMsg); self.layout.send(layout_task::ExitMsg); - do listen |response_channel| { - self.renderer.send(renderer::ExitMsg(response_channel)); - response_channel.recv(); - } + + let (response_chan, response_port) = pipes::stream(); + + self.renderer.send(renderer::ExitMsg(response_chan)); + response_port.recv(); + sender.send(()); ret false; } diff --git a/servo/src/servo/gfx/pngsink.rs b/servo/src/servo/gfx/pngsink.rs index 634f44a124e4..c717fbb4f09a 100644 --- a/servo/src/servo/gfx/pngsink.rs +++ b/servo/src/servo/gfx/pngsink.rs @@ -30,16 +30,16 @@ import layout::display_list::display_list; type PngSink = chan; enum Msg { - BeginDrawing(chan), - Draw(chan, AzDrawTargetRef), + BeginDrawing(pipes::chan), + Draw(pipes::chan, AzDrawTargetRef), Exit } impl PngSink of Sink for chan { - fn begin_drawing(next_dt: chan) { + fn begin_drawing(+next_dt: pipes::chan) { self.send(BeginDrawing(next_dt)) } - fn draw(next_dt: chan, draw_me: AzDrawTargetRef) { + fn draw(+next_dt: pipes::chan, draw_me: AzDrawTargetRef) { self.send(Draw(next_dt, draw_me)) } fn add_event_listener(_listener: chan) { @@ -76,7 +76,7 @@ fn PngSink(output: chan<~[u8]>) -> PngSink { }) } -fn do_draw(sender: chan, +fn do_draw(sender: pipes::chan, dt: AzDrawTargetRef, output: chan<~[u8]>, cairo_surf: *cairo_surface_t) { @@ -131,10 +131,9 @@ fn sanity_check() { let dlist : display_list = dvec(); renderer.send(RenderMsg(dlist)); - listen(|from_renderer| { - renderer.send(renderer::ExitMsg(from_renderer)); - from_renderer.recv(); - }); + let (exit_chan, exit_response_from_engine) = pipes::stream(); + renderer.send(renderer::ExitMsg(exit_chan)); + exit_response_from_engine.recv(); sink.send(Exit) }) diff --git a/servo/src/servo/gfx/renderer.rs b/servo/src/servo/gfx/renderer.rs index 8f11316e8112..a4706bc4c727 100644 --- a/servo/src/servo/gfx/renderer.rs +++ b/servo/src/servo/gfx/renderer.rs @@ -15,11 +15,13 @@ import azure_hl::AsAzureRect; import ptr::addr_of; import arc::arc; -type Renderer = chan; +import pipes::{port, chan}; + +type Renderer = comm::chan; enum Msg { RenderMsg(dl::display_list), - ExitMsg(comm::chan<()>) + ExitMsg(pipes::chan<()>) } #[doc = " @@ -29,38 +31,45 @@ each rendered frame and submit them to be drawn to the display FIXME: Change this name to Compositor. "] iface Sink { - fn begin_drawing(next_dt: chan); - fn draw(next_dt: chan, draw_me: AzDrawTargetRef); - fn add_event_listener(listener: chan); + fn begin_drawing(+next_dt: pipes::chan); + fn draw(+next_dt: pipes::chan, draw_me: AzDrawTargetRef); + fn add_event_listener(listener: comm::chan); } -fn Renderer(sink: S) -> chan { +fn Renderer(sink: S) -> comm::chan { task::spawn_listener::(|po| { - listen(|draw_target_ch| { - #debug("renderer: beginning rendering loop"); - sink.begin_drawing(draw_target_ch); + let (draw_target_ch, draw_target_po) = pipes::stream(); + let mut draw_target_ch = draw_target_ch; + let mut draw_target_po = draw_target_po; - loop { - alt po.recv() { - RenderMsg(display_list) { - #debug("renderer: got render request"); - let draw_target = draw_target_ch.recv(); - #debug("renderer: rendering"); + #debug("renderer: beginning rendering loop"); + sink.begin_drawing(draw_target_ch); - do util::time::time(~"rendering") { - clear(draw_target); - draw_display_list(draw_target, display_list); - #debug("renderer: returning surface"); - sink.draw(draw_target_ch, draw_target); - } - } - ExitMsg(response_ch) { - response_ch.send(()); - break; - } + loop { + alt po.recv() { + RenderMsg(display_list) { + #debug("renderer: got render request"); + let draw_target = draw_target_po.recv(); + let (ch, po) = pipes::stream(); + let mut draw_target_ch_ = some(ch); + draw_target_po = po; + #debug("renderer: rendering"); + do util::time::time(~"rendering") { + let mut draw_target_ch = none; + draw_target_ch_ <-> draw_target_ch; + let draw_target_ch = option::unwrap(draw_target_ch); + clear(draw_target); + draw_display_list(draw_target, display_list); + #debug("renderer: returning surface"); + sink.draw(draw_target_ch, draw_target); } + } + ExitMsg(response_ch) { + response_ch.send(()); + break; + } } - }) + } }) } diff --git a/servo/src/servo/layout/layout_task.rs b/servo/src/servo/layout/layout_task.rs index 08aa27e4a88e..039688d0d21f 100644 --- a/servo/src/servo/layout/layout_task.rs +++ b/servo/src/servo/layout/layout_task.rs @@ -34,7 +34,8 @@ fn Layout(renderer: Renderer) -> Layout { ping_channel.send(content::PongMsg); } ExitMsg { - break; + #debug("layout: ExitMsg received"); + break; } BuildMsg(node, styles) { #debug("layout: received layout request for:"); diff --git a/servo/src/servo/parser/css_builder.rs b/servo/src/servo/parser/css_builder.rs index 2f551e5c6c54..a210f03632c3 100644 --- a/servo/src/servo/parser/css_builder.rs +++ b/servo/src/servo/parser/css_builder.rs @@ -17,7 +17,7 @@ import parser::parser_util::{parse_display_type, parse_font_size, parse_size}; import util::color::parsing::parse_color; import vec::push; -type TokenReader = {stream : port, mut lookahead : option}; +type TokenReader = {stream : pipes::port, mut lookahead : option}; trait util_methods { fn get() -> Token; @@ -28,7 +28,7 @@ impl util_methods of util_methods for TokenReader { fn get() -> Token { alt copy self.lookahead { some(tok) { self.lookahead = none; copy tok } - none { recv(self.stream) } + none { self.stream.recv() } } } @@ -195,7 +195,7 @@ impl parser_methods of parser_methods for TokenReader { } } -fn build_stylesheet(stream : port) -> ~[~style::Rule] { +fn build_stylesheet(+stream : pipes::port) -> ~[~style::Rule] { let mut rule_list = ~[]; let reader = {stream : stream, mut lookahead : none}; diff --git a/servo/src/servo/parser/css_lexer.rs b/servo/src/servo/parser/css_lexer.rs index 169947f37360..1e8bf6d246f0 100644 --- a/servo/src/servo/parser/css_lexer.rs +++ b/servo/src/servo/parser/css_lexer.rs @@ -1,11 +1,12 @@ #[doc = "Code to lex and tokenize css files."] -import comm::{port, chan}; import dom::style; import option::is_none; import str::from_bytes; import vec::push; +import pipes::{port, chan}; + import lexer_util::*; enum ParserState { @@ -243,8 +244,7 @@ fn lex_css_from_bytes(-content : ~[u8], result_chan : chan) { } fn spawn_css_lexer_from_string(-content : ~str) -> port { - let result_port = port(); - let result_chan = chan(result_port); + let (result_chan, result_port) = pipes::stream(); task::spawn(|| lex_css_from_bytes(str::bytes(content), result_chan)); @@ -252,22 +252,21 @@ fn spawn_css_lexer_from_string(-content : ~str) -> port { } #[warn(no_non_implicitly_copyable_typarams)] -fn spawn_css_lexer_from_file(-filename: ~str) -> port { - let result_port = port(); - let result_chan = chan(result_port); +fn spawn_css_lexer_task(-filename: ~str) -> pipes::port { + let (result_chan, result_port) = pipes::stream(); task::spawn(|| { - assert (copy filename).ends_with(~".css"); + assert filename.ends_with(".css"); let file_try = io::read_whole_file(filename); // Check if the given css file existed, if it does, parse it, // otherwise just send an eof. if file_try.is_ok() { - #debug["Lexing css sheet %?", copy filename]; + #debug["Lexing css sheet %?", filename]; let file_data = file_try.get(); lex_css_from_bytes(file_data, result_chan); } else { - #debug["Failed to open css sheet %?", copy filename]; + #debug["Failed to open css sheet %?", filename]; result_chan.send(Eof); } }); diff --git a/servo/src/servo/parser/html_builder.rs b/servo/src/servo/parser/html_builder.rs index 189952ccde62..f2f83342ddf0 100644 --- a/servo/src/servo/parser/html_builder.rs +++ b/servo/src/servo/parser/html_builder.rs @@ -94,7 +94,7 @@ spawned, collates them, and sends them to the given result channel. * `from_parent` - A port on which to receive new links. "] -fn css_link_listener(to_parent : chan, from_parent : port) { +fn css_link_listener(to_parent : comm::chan, from_parent : comm::port) { let mut result_vec = ~[]; loop { @@ -106,7 +106,7 @@ fn css_link_listener(to_parent : chan, from_parent : port, from_parent : port, from_parent : port) { +fn js_script_listener(to_parent : comm::chan<~[~[u8]]>, from_parent : comm::port) { let mut result_vec = ~[]; loop { @@ -155,7 +155,7 @@ fn js_script_listener(to_parent : chan<~[~[u8]]>, from_parent : port } #[warn(no_non_implicitly_copyable_typarams)] -fn build_dom(scope: NodeScope, stream: port) -> (Node, port, port<~[~[u8]]>) { +fn build_dom(scope: NodeScope, stream: comm::port) -> (Node, comm::port, comm::port<~[~[u8]]>) { // The current reference node. let mut cur_node = scope.new_node(Element(ElementData(~"html", ~HTMLDivElement))); // We will spawn a separate task to parse any css that is diff --git a/servo/src/servo/platform/osmain.rs b/servo/src/servo/platform/osmain.rs index 99cb70179ecb..52d6aafe885a 100644 --- a/servo/src/servo/platform/osmain.rs +++ b/servo/src/servo/platform/osmain.rs @@ -16,13 +16,15 @@ import std::cmp::fuzzy_eq; import task::task_builder; import vec::push; -type OSMain = chan; +import pipes::chan; + +type OSMain = comm::chan; enum Msg { - BeginDrawing(chan), - Draw(chan, AzDrawTargetRef), - AddKeyHandler(chan<()>), - AddEventListener(chan), + BeginDrawing(pipes::chan), + Draw(pipes::chan, AzDrawTargetRef), + AddKeyHandler(pipes::chan<()>), + AddEventListener(comm::chan), Exit } @@ -36,12 +38,17 @@ fn OSMain() -> OSMain { } fn mainloop(po: port) { - let key_handlers: @dvec> = @dvec(); - let event_listeners: @dvec> = @dvec(); + let key_handlers: @dvec> = @dvec(); + let event_listeners: @dvec> = @dvec(); glut::init(); glut::init_display_mode(glut::DOUBLE); + #macro[ + [#move[x], + unsafe { let y <- *ptr::addr_of(x); y }] + ]; + let surfaces = @surface_set(); let window = glut::create_window(~"Servo"); @@ -65,7 +72,7 @@ fn mainloop(po: port) { while po.peek() { alt po.recv() { AddKeyHandler(key_ch) { - key_handlers.push(key_ch); + key_handlers.push(#move(key_ch)); } AddEventListener(event_listener) { event_listeners.push(event_listener); @@ -131,13 +138,13 @@ Implementation to allow the osmain channel to be used as a graphics sink for the renderer "] impl OSMain of Sink for OSMain { - fn begin_drawing(next_dt: chan) { + fn begin_drawing(+next_dt: pipes::chan) { self.send(BeginDrawing(next_dt)) } - fn draw(next_dt: chan, draw_me: AzDrawTargetRef) { + fn draw(+next_dt: pipes::chan, draw_me: AzDrawTargetRef) { self.send(Draw(next_dt, draw_me)) } - fn add_event_listener(listener: chan) { + fn add_event_listener(listener: comm::chan) { self.send(AddEventListener(listener)); } } @@ -153,7 +160,7 @@ type surface_set = { } }; -fn lend_surface(surfaces: surface_set, recvr: chan) { +fn lend_surface(surfaces: surface_set, recvr: pipes::chan) { // We are in a position to lend out the surface? assert surfaces.s1.have; // Ok then take it diff --git a/servo/src/servo/servo.rc b/servo/src/servo/servo.rc index 144cf8942979..812d03295aa6 100755 --- a/servo/src/servo/servo.rc +++ b/servo/src/servo/servo.rc @@ -114,4 +114,3 @@ mod opts; mod engine; import servo_text = text; - diff --git a/servo/src/servo/servo.rs b/servo/src/servo/servo.rs index e7d200447f54..8a0f9203e601 100644 --- a/servo/src/servo/servo.rs +++ b/servo/src/servo/servo.rs @@ -5,6 +5,8 @@ import osmain::{OSMain, AddKeyHandler}; import opts::{Opts, Screen, Png}; import engine::{Engine, LoadURLMsg}; +import pipes::{port, chan}; + fn main(args: ~[~str]) { run(opts::from_cmdline_args(args)) } @@ -30,28 +32,30 @@ fn run_pipeline_screen(urls: ~[~str]) { // The platform event handler thread let osmain = OSMain(); + // Send each file to render then wait for keypress + let (keypress_to_engine, keypress_from_osmain) = pipes::stream(); + osmain.send(AddKeyHandler(keypress_to_engine)); + // Create a serve instance let engine = Engine(osmain); let engine_chan = engine.start(); - // Send each file to render then wait for keypress - listen(|keypress_from_osmain| { - osmain.send(AddKeyHandler(keypress_from_osmain)); - - for urls.each |filename| { - #debug["master: Sending filename `%s`", filename]; - engine_chan.send(LoadURLMsg(copy filename)); - #debug["master: Waiting for keypress"]; - keypress_from_osmain.recv(); - } - }); + for urls.each |filename| { + #debug["master: Sending filename `%s`", filename]; + engine_chan.send(LoadURLMsg(copy filename)); + #debug["master: Waiting for keypress"]; + alt keypress_from_osmain.try_recv() { + some(*) { } + none { #error("keypress stream closed unexpectedly") } + }; + } // Shut everything down #debug["master: Shut down"]; - listen(|exit_response_from_engine| { - engine_chan.send(engine::ExitMsg(exit_response_from_engine)); - exit_response_from_engine.recv(); - }); + let (exit_chan, exit_response_from_engine) = pipes::stream(); + engine_chan.send(engine::ExitMsg(exit_chan)); + exit_response_from_engine.recv(); + osmain.send(osmain::Exit); } @@ -75,10 +79,9 @@ fn run_pipeline_png(-url: ~str, outfile: ~str) { } err(e) { fail e } } - listen(|exit_response_from_engine| { - engine_chan.send(engine::ExitMsg(exit_response_from_engine)); - exit_response_from_engine.recv(); - }); + let (exit_chan, exit_response_from_engine) = pipes::stream(); + engine_chan.send(engine::ExitMsg(exit_chan)); + exit_response_from_engine.recv(); sink.send(pngsink::Exit); }) }