From 372355ad22877b7292a1c310d0f51f4b298a8f5c Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Wed, 25 Nov 2015 13:15:33 -0800 Subject: [PATCH] Make rasterizer a mojo service. --- sky/services/rasterizer/BUILD.gn | 11 +++++++++++ sky/services/rasterizer/rasterizer.mojom | 9 +++++++++ sky/shell/BUILD.gn | 1 + sky/shell/gpu/direct/rasterizer_direct.cc | 24 ++++++++++++++++++----- sky/shell/gpu/direct/rasterizer_direct.h | 11 +++++++++-- sky/shell/gpu/mojo/rasterizer_mojo.cc | 19 ++++++++++++++---- sky/shell/gpu/mojo/rasterizer_mojo.h | 12 +++++++++--- sky/shell/rasterizer.h | 13 +++++++----- sky/shell/shell_view.cc | 11 +++++++++-- sky/shell/ui/animator.cc | 9 +++++---- sky/shell/ui/animator.h | 4 +++- sky/shell/ui/engine.cc | 4 ++-- sky/shell/ui/engine.h | 4 ++-- 13 files changed, 102 insertions(+), 30 deletions(-) create mode 100644 sky/services/rasterizer/BUILD.gn create mode 100644 sky/services/rasterizer/rasterizer.mojom diff --git a/sky/services/rasterizer/BUILD.gn b/sky/services/rasterizer/BUILD.gn new file mode 100644 index 000000000..cdb74ea10 --- /dev/null +++ b/sky/services/rasterizer/BUILD.gn @@ -0,0 +1,11 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//mojo/public/tools/bindings/mojom.gni") + +mojom("interfaces") { + sources = [ + "rasterizer.mojom", + ] +} diff --git a/sky/services/rasterizer/rasterizer.mojom b/sky/services/rasterizer/rasterizer.mojom new file mode 100644 index 000000000..d966bd4b3 --- /dev/null +++ b/sky/services/rasterizer/rasterizer.mojom @@ -0,0 +1,9 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +module rasterizer; + +interface Rasterizer { + Draw(uint64 layer_tree_ptr) => (); +}; diff --git a/sky/shell/BUILD.gn b/sky/shell/BUILD.gn index 746e32a9d..308b58a5e 100644 --- a/sky/shell/BUILD.gn +++ b/sky/shell/BUILD.gn @@ -52,6 +52,7 @@ source_set("common") { "//sky/engine/wtf", "//sky/services/engine:interfaces", "//sky/services/pointer:interfaces", + "//sky/services/rasterizer:interfaces", "//sky/shell/dart", "//ui/gfx", "//ui/gfx/geometry", diff --git a/sky/shell/gpu/direct/rasterizer_direct.cc b/sky/shell/gpu/direct/rasterizer_direct.cc index 0df4e9204..353f35bfd 100644 --- a/sky/shell/gpu/direct/rasterizer_direct.cc +++ b/sky/shell/gpu/direct/rasterizer_direct.cc @@ -29,7 +29,8 @@ scoped_ptr Rasterizer::Create() { } RasterizerDirect::RasterizerDirect() - : share_group_(new gfx::GLShareGroup()), weak_factory_(this) { + : share_group_(new gfx::GLShareGroup()), binding_(this), + weak_factory_(this) { } RasterizerDirect::~RasterizerDirect() { @@ -39,8 +40,13 @@ base::WeakPtr RasterizerDirect::GetWeakPtr() { return weak_factory_.GetWeakPtr(); } -RasterCallback RasterizerDirect::GetRasterCallback() { - return base::Bind(&RasterizerDirect::Draw, weak_factory_.GetWeakPtr()); +base::WeakPtr RasterizerDirect::GetWeakRasterizerPtr() { + return GetWeakPtr(); +} + +void RasterizerDirect::ConnectToRasterizer( + mojo::InterfaceRequest request) { + binding_.Bind(request.Pass()); } void RasterizerDirect::OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) { @@ -49,11 +55,17 @@ void RasterizerDirect::OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widge CHECK(surface_) << "GLSurface required."; } -void RasterizerDirect::Draw(scoped_ptr layer_tree) { +void RasterizerDirect::Draw(uint64_t layer_tree_ptr, + const DrawCallback& callback) { TRACE_EVENT0("flutter", "RasterizerDirect::Draw"); - if (!surface_) + if (!surface_) { + callback.Run(); return; + } + + scoped_ptr layer_tree( + reinterpret_cast(layer_tree_ptr)); gfx::Size size(layer_tree->frame_size().width(), layer_tree->frame_size().height()); @@ -98,6 +110,8 @@ void RasterizerDirect::Draw(scoped_ptr layer_tree) { paint_context_.AcquireFrame(path.AsUTF8Unsafe(), size); layer_tree->root_layer()->Paint(to_file_frame); } + + callback.Run(); } void RasterizerDirect::OnOutputSurfaceDestroyed() { diff --git a/sky/shell/gpu/direct/rasterizer_direct.h b/sky/shell/gpu/direct/rasterizer_direct.h index 93eea774b..0663d8e25 100644 --- a/sky/shell/gpu/direct/rasterizer_direct.h +++ b/sky/shell/gpu/direct/rasterizer_direct.h @@ -30,13 +30,18 @@ class RasterizerDirect : public Rasterizer { ~RasterizerDirect() override; base::WeakPtr GetWeakPtr(); - RasterCallback GetRasterCallback() override; + + base::WeakPtr<::sky::shell::Rasterizer> GetWeakRasterizerPtr() override; + + void ConnectToRasterizer( + mojo::InterfaceRequest request) override; void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget); void OnOutputSurfaceDestroyed(); private: - void Draw(scoped_ptr layer_tree); + void Draw(uint64_t layer_tree_ptr, const DrawCallback& callback) override; + void EnsureGLContext(); void EnsureGaneshSurface(intptr_t window_fbo, const gfx::Size& size); @@ -49,6 +54,8 @@ class RasterizerDirect : public Rasterizer { compositor::PaintContext paint_context_; + mojo::Binding binding_; + base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(RasterizerDirect); diff --git a/sky/shell/gpu/mojo/rasterizer_mojo.cc b/sky/shell/gpu/mojo/rasterizer_mojo.cc index 0c98c77ab..5e346620a 100644 --- a/sky/shell/gpu/mojo/rasterizer_mojo.cc +++ b/sky/shell/gpu/mojo/rasterizer_mojo.cc @@ -24,7 +24,7 @@ scoped_ptr Rasterizer::Create() { return make_scoped_ptr(new RasterizerMojo()); } -RasterizerMojo::RasterizerMojo() : weak_factory_(this) { +RasterizerMojo::RasterizerMojo() : binding_(this), weak_factory_(this) { } RasterizerMojo::~RasterizerMojo() { @@ -34,12 +34,22 @@ base::WeakPtr RasterizerMojo::GetWeakPtr() { return weak_factory_.GetWeakPtr(); } -RasterCallback RasterizerMojo::GetRasterCallback() { - return base::Bind(&RasterizerMojo::Draw, weak_factory_.GetWeakPtr()); +base::WeakPtr RasterizerMojo::GetWeakRasterizerPtr() { + return GetWeakPtr(); } -void RasterizerMojo::Draw(scoped_ptr layer_tree) { +void RasterizerMojo::ConnectToRasterizer ( + mojo::InterfaceRequest request ) { + binding_.Bind(request.Pass()); +} + +void RasterizerMojo::Draw(uint64_t layer_tree_ptr, + const DrawCallback& callback) { TRACE_EVENT0("flutter", "RasterizerMojo::Draw"); + + scoped_ptr layer_tree( + reinterpret_cast(layer_tree_ptr)); + MGLResizeSurface(layer_tree->frame_size().width(), layer_tree->frame_size().height()); SkCanvas* canvas = ganesh_canvas_.GetCanvas(0, layer_tree->frame_size()); @@ -49,6 +59,7 @@ void RasterizerMojo::Draw(scoped_ptr layer_tree) { layer_tree->root_layer()->Paint(frame); canvas->flush(); MGLSwapBuffers(); + callback.Run(); } void RasterizerMojo::OnContextProviderAvailable( diff --git a/sky/shell/gpu/mojo/rasterizer_mojo.h b/sky/shell/gpu/mojo/rasterizer_mojo.h index b1c22c609..8e57d4071 100644 --- a/sky/shell/gpu/mojo/rasterizer_mojo.h +++ b/sky/shell/gpu/mojo/rasterizer_mojo.h @@ -16,14 +16,17 @@ namespace sky { namespace shell { -class RasterizerMojo : public Rasterizer { +class RasterizerMojo : public ::sky::shell::Rasterizer { public: explicit RasterizerMojo(); ~RasterizerMojo() override; base::WeakPtr GetWeakPtr(); - RasterCallback GetRasterCallback() override; + base::WeakPtr<::sky::shell::Rasterizer> GetWeakRasterizerPtr() override; + + void ConnectToRasterizer( + mojo::InterfaceRequest request) override; void OnContextProviderAvailable( mojo::InterfacePtrInfo context_provder); @@ -32,7 +35,8 @@ class RasterizerMojo : public Rasterizer { private: void OnContextCreated(mojo::CommandBufferPtr command_buffer); - void Draw(scoped_ptr layer_tree); + + void Draw(uint64_t layer_tree_ptr, const DrawCallback& callback) override; mojo::ContextProviderPtr context_provider_; skia::RefPtr gr_gl_interface_; @@ -41,6 +45,8 @@ class RasterizerMojo : public Rasterizer { compositor::PaintContext paint_context_; + mojo::Binding binding_; + base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(RasterizerMojo); diff --git a/sky/shell/rasterizer.h b/sky/shell/rasterizer.h index b2f199014..c0ffb8756 100644 --- a/sky/shell/rasterizer.h +++ b/sky/shell/rasterizer.h @@ -9,17 +9,20 @@ #include "base/callback.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" +#include "mojo/public/cpp/bindings/binding.h" #include "sky/compositor/layer_tree.h" +#include "sky/services/rasterizer/rasterizer.mojom.h" namespace sky { namespace shell { -typedef base::Callback)> RasterCallback; - -class Rasterizer { +class Rasterizer : public rasterizer::Rasterizer { public: - virtual ~Rasterizer(); - virtual RasterCallback GetRasterCallback() = 0; + ~Rasterizer() override; + virtual void ConnectToRasterizer( + mojo::InterfaceRequest request) = 0; + virtual base::WeakPtr<::sky::shell::Rasterizer> GetWeakRasterizerPtr() = 0; // Implemented by each GPU backend. static scoped_ptr Create(); diff --git a/sky/shell/shell_view.cc b/sky/shell/shell_view.cc index a6860b8ad..8c039e2a7 100644 --- a/sky/shell/shell_view.cc +++ b/sky/shell/shell_view.cc @@ -6,6 +6,7 @@ #include "base/bind.h" #include "base/single_thread_task_runner.h" +#include "sky/services/rasterizer/rasterizer.mojom.h" #include "sky/shell/platform_view.h" #include "sky/shell/rasterizer.h" #include "sky/shell/shell.h" @@ -40,8 +41,14 @@ ShellView::~ShellView() { void ShellView::CreateEngine() { Engine::Config config; config.gpu_task_runner = shell_.gpu_task_runner(); - config.raster_callback = rasterizer_->GetRasterCallback(); - engine_.reset(new Engine(config)); + rasterizer::RasterizerPtr rasterizer; + mojo::InterfaceRequest request = mojo::GetProxy( + &rasterizer); + shell_.gpu_task_runner()->PostTask( + FROM_HERE, + base::Bind(&Rasterizer::ConnectToRasterizer, + rasterizer_->GetWeakRasterizerPtr(), base::Passed(&request))); + engine_.reset(new Engine(config, rasterizer.Pass())); } void ShellView::CreatePlatformView() { diff --git a/sky/shell/ui/animator.cc b/sky/shell/ui/animator.cc index ae869e491..9734cb0d4 100644 --- a/sky/shell/ui/animator.cc +++ b/sky/shell/ui/animator.cc @@ -7,14 +7,17 @@ #include "base/bind.h" #include "base/message_loop/message_loop.h" #include "base/trace_event/trace_event.h" +#include "sky/services/rasterizer/rasterizer.mojom.h" namespace sky { namespace shell { const int kPipelineDepth = 3; -Animator::Animator(const Engine::Config& config, Engine* engine) +Animator::Animator(const Engine::Config& config, + rasterizer::RasterizerPtr rasterizer, Engine* engine) : config_(config), + rasterizer_(rasterizer.Pass()), engine_(engine), outstanding_requests_(0), did_defer_frame_request_(false), @@ -79,9 +82,7 @@ void Animator::BeginFrame(int64_t time_stamp) { return; } - config_.gpu_task_runner->PostTaskAndReply( - FROM_HERE, - base::Bind(config_.raster_callback, base::Passed(&layer_tree)), + rasterizer_->Draw(reinterpret_cast(layer_tree.release()), base::Bind(&Animator::OnFrameComplete, weak_factory_.GetWeakPtr())); } diff --git a/sky/shell/ui/animator.h b/sky/shell/ui/animator.h index 27db6fd6e..143d31ea6 100644 --- a/sky/shell/ui/animator.h +++ b/sky/shell/ui/animator.h @@ -14,7 +14,8 @@ namespace shell { class Animator { public: - explicit Animator(const Engine::Config& config, Engine* engine); + explicit Animator(const Engine::Config& config, + rasterizer::RasterizerPtr rasterizer, Engine* engine); ~Animator(); void RequestFrame(); @@ -32,6 +33,7 @@ class Animator { bool AwaitVSync(); Engine::Config config_; + rasterizer::RasterizerPtr rasterizer_; Engine* engine_; vsync::VSyncProviderPtr vsync_provider_; int outstanding_requests_; diff --git a/sky/shell/ui/engine.cc b/sky/shell/ui/engine.cc index 99d83cf05..2343dd3d8 100644 --- a/sky/shell/ui/engine.cc +++ b/sky/shell/ui/engine.cc @@ -53,9 +53,9 @@ Engine::Config::Config() { Engine::Config::~Config() { } -Engine::Engine(const Config& config) +Engine::Engine(const Config& config, rasterizer::RasterizerPtr rasterizer) : config_(config), - animator_(new Animator(config, this)), + animator_(new Animator(config, rasterizer.Pass(), this)), binding_(this), activity_running_(false), have_surface_(false), diff --git a/sky/shell/ui/engine.h b/sky/shell/ui/engine.h index a680f9a7a..704f31b78 100644 --- a/sky/shell/ui/engine.h +++ b/sky/shell/ui/engine.h @@ -18,6 +18,7 @@ #include "skia/ext/refptr.h" #include "sky/engine/public/sky/sky_view.h" #include "sky/engine/public/sky/sky_view_client.h" +#include "sky/services/rasterizer/rasterizer.mojom.h" #include "sky/shell/rasterizer.h" #include "sky/shell/ui_delegate.h" #include "third_party/skia/include/core/SkPicture.h" @@ -36,11 +37,10 @@ class Engine : public UIDelegate, Config(); ~Config(); - RasterCallback raster_callback; scoped_refptr gpu_task_runner; }; - explicit Engine(const Config& config); + explicit Engine(const Config& config, rasterizer::RasterizerPtr rasterizer); ~Engine() override; base::WeakPtr GetWeakPtr(); -- GitLab