Skip to content

Commit

Permalink
Make the engine provide layer trees to the rasterizer via a pipeline. (
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored Aug 18, 2016
1 parent 9a9efdf commit ace7ce8
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 250 deletions.
2 changes: 0 additions & 2 deletions services/engine/sky_engine.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module sky;
import "mojo/public/interfaces/application/service_provider.mojom";
import "mojo/public/interfaces/application/shell.mojom";
import "mojo/services/asset_bundle/interfaces/asset_bundle.mojom";
import "mojo/services/gfx/composition/interfaces/scheduling.mojom";
import "mojo/services/ui/views/interfaces/views.mojom";
import "flutter/services/engine/input_event.mojom";
import "flutter/services/pointer/pointer.mojom";
Expand All @@ -34,7 +33,6 @@ struct ServicesData {
mojo.ServiceProvider&? outgoing_services;
mojo.ui.View? view;
mojo.ServiceProvider? view_services;
mojo.gfx.composition.FrameScheduler? frame_scheduler;
};

[ServiceName="sky::SkyEngine"]
Expand Down
11 changes: 0 additions & 11 deletions services/rasterizer/BUILD.gn

This file was deleted.

9 changes: 0 additions & 9 deletions services/rasterizer/rasterizer.mojom

This file was deleted.

3 changes: 1 addition & 2 deletions services/vsync/ios/vsync_provider_ios_impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static inline uint64_t CurrentTimeMicroseconds() {
@implementation VSyncClient {
CADisplayLink* _displayLink;
std::vector<::vsync::VSyncProvider::AwaitVSyncCallback> _pendingCallbacks;
BOOL _traceLevel;
}

- (instancetype)init {
Expand All @@ -51,7 +50,7 @@ - (void)await:(::vsync::VSyncProvider::AwaitVSyncCallback)callback {
}

- (void)onDisplayLink:(CADisplayLink*)link {
TRACE_COUNTER1("vsync", "PlatformVSync", _traceLevel = !_traceLevel);
TRACE_EVENT_INSTANT0("flutter", "PlatformVSync", TRACE_EVENT_SCOPE_PROCESS);
_displayLink.paused = YES;
uint64_t micros = CurrentTimeMicroseconds();
for (const auto& callback : _pendingCallbacks) {
Expand Down
6 changes: 3 additions & 3 deletions services/vsync/mac/vsync_provider_mac_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace vsync {

VsyncProviderMacImpl::VsyncProviderMacImpl(
mojo::InterfaceRequest<::vsync::VSyncProvider> request)
: binding_(this, request.Pass()), opaque_(nullptr), trace_level_(false) {
: binding_(this, request.Pass()), opaque_(nullptr) {
// Create the link.
CVDisplayLinkRef link = nullptr;
CVDisplayLinkCreateWithActiveCGDisplays(&link);
Expand Down Expand Up @@ -53,8 +53,8 @@ void VsyncProviderMacImpl::OnDisplayLink(void* thiz) {
}

void VsyncProviderMacImpl::OnDisplayLink() {
TRACE_COUNTER1("vsync", "PlatformVSync", trace_level_ = !trace_level_);

TRACE_EVENT_INSTANT1("flutter", "PlatformVSync", TRACE_EVENT_SCOPE_PROCESS,
"items", pending_callbacks_.size());
// Stop the link.
CVDisplayLinkStop(link_);

Expand Down
3 changes: 1 addition & 2 deletions services/vsync/mac/vsync_provider_mac_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ class VsyncProviderMacImpl : public ::vsync::VSyncProvider {
mojo::StrongBinding<::vsync::VSyncProvider> binding_;
void* opaque_;
std::vector<::vsync::VSyncProvider::AwaitVSyncCallback> pending_callbacks_;
bool trace_level_;

static void OnDisplayLink(void *thiz);
static void OnDisplayLink(void* thiz);
void OnDisplayLink();

DISALLOW_COPY_AND_ASSIGN(VsyncProviderMacImpl);
Expand Down
2 changes: 1 addition & 1 deletion sky/shell/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ source_set("common") {
"//flutter/glue",
"//flutter/lib/ui",
"//flutter/runtime",
"//flutter/synchronization",
"//flutter/services/editing:interfaces",
"//flutter/services/engine:interfaces",
"//flutter/services/platform",
"//flutter/services/pointer:interfaces",
"//flutter/services/rasterizer:interfaces",
"//flutter/services/semantics:interfaces",
"//flutter/skia",
"//flutter/sky/engine",
Expand Down
59 changes: 36 additions & 23 deletions sky/shell/gpu/direct/rasterizer_direct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@

#include "flutter/sky/shell/gpu/direct/rasterizer_direct.h"

#include <string>
#include <utility>

#include "flutter/common/threads.h"
#include "flutter/glue/trace_event.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "flutter/sky/engine/wtf/PassRefPtr.h"
#include "flutter/sky/engine/wtf/RefPtr.h"
#include "flutter/sky/shell/gpu/picture_serializer.h"
#include "flutter/sky/shell/platform_view.h"
#include "flutter/sky/shell/shell.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPicture.h"

namespace sky {
namespace shell {

RasterizerDirect::RasterizerDirect()
: binding_(this), platform_view_(nullptr), weak_factory_(this) {}
: platform_view_(nullptr), weak_factory_(this) {
auto weak_ptr = weak_factory_.GetWeakPtr();
blink::Threads::Gpu()->PostTask(
[weak_ptr]() { Shell::Shared().AddRasterizer(weak_ptr); });
}

RasterizerDirect::~RasterizerDirect() {
weak_factory_.InvalidateWeakPtrs();
Expand All @@ -35,14 +43,6 @@ ftl::WeakPtr<Rasterizer> RasterizerDirect::GetWeakRasterizerPtr() {
return weak_factory_.GetWeakPtr();
}

// sky::shell::Rasterizer override.
void RasterizerDirect::ConnectToRasterizer(
mojo::InterfaceRequest<rasterizer::Rasterizer> request) {
binding_.Bind(request.Pass());

Shell::Shared().AddRasterizer(GetWeakRasterizerPtr());
}

// sky::shell::Rasterizer override.
void RasterizerDirect::Setup(
PlatformView* platform_view,
Expand Down Expand Up @@ -86,33 +86,48 @@ flow::LayerTree* RasterizerDirect::GetLastLayerTree() {
return last_layer_tree_.get();
}

void RasterizerDirect::Draw(uint64_t layer_tree_ptr,
const DrawCallback& callback) {
void RasterizerDirect::Draw(
ftl::RefPtr<flutter::Pipeline<flow::LayerTree>> pipeline) {
TRACE_EVENT0("flutter", "RasterizerDirect::Draw");

if (platform_view_ == nullptr) {
callback.Run();
return;
}

std::unique_ptr<flow::LayerTree> layer_tree(
reinterpret_cast<flow::LayerTree*>(layer_tree_ptr));
flutter::Pipeline<flow::LayerTree>::Consumer consumer =
std::bind(&RasterizerDirect::DoDraw, this, std::placeholders::_1);

// Consume as many pipeline items as possible. But yield the event loop
// between successive tries.
switch (pipeline->Consume(consumer)) {
case flutter::PipelineConsumeResult::MoreAvailable: {
auto weak_this = weak_factory_.GetWeakPtr();
blink::Threads::Gpu()->PostTask([weak_this, pipeline]() {
if (weak_this) {
weak_this->Draw(pipeline);
}
});
} break;
default:
break;
}
}

void RasterizerDirect::DoDraw(std::unique_ptr<flow::LayerTree> layer_tree) {
// There is no way for the compositor to know how long the layer tree
// construction took. Fortunately, the layer tree does. Grab that time
// for instrumentation.
compositor_context_.engine_time().SetLapTime(layer_tree->construction_time());

SkISize size = layer_tree->frame_size();
if (platform_view_->GetSize() != size) {
platform_view_->Resize(size);
}

if (!platform_view_->ContextMakeCurrent() || !layer_tree->root_layer()) {
callback.Run();
return;
}

// There is no way for the compositor to know how long the layer tree
// construction took. Fortunately, the layer tree does. Grab that time
// for instrumentation.
compositor_context_.engine_time().SetLapTime(layer_tree->construction_time());

{
SkCanvas* canvas = ganesh_canvas_.GetCanvas(
platform_view_->DefaultFramebuffer(), layer_tree->frame_size());
Expand Down Expand Up @@ -159,8 +174,6 @@ void RasterizerDirect::Draw(uint64_t layer_tree_ptr,
SerializePicture(path, picture.get());
}

callback.Run();

last_layer_tree_ = std::move(layer_tree);
}

Expand Down
11 changes: 4 additions & 7 deletions sky/shell/gpu/direct/rasterizer_direct.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ class RasterizerDirect : public Rasterizer {

~RasterizerDirect() override;

// sky::shell::Rasterizer override.
void ConnectToRasterizer(
mojo::InterfaceRequest<rasterizer::Rasterizer> request) override;

// sky::shell::Rasterizer override.
void Setup(PlatformView* platform_view,
ftl::Closure continuation,
Expand All @@ -39,16 +35,17 @@ class RasterizerDirect : public Rasterizer {
// sky::shell::Rasterizer override.
flow::LayerTree* GetLastLayerTree() override;

// sky::shell::Rasterizer override.
void Draw(ftl::RefPtr<flutter::Pipeline<flow::LayerTree>> pipeline) override;

private:
GaneshCanvas ganesh_canvas_;
flow::CompositorContext compositor_context_;
mojo::Binding<rasterizer::Rasterizer> binding_;
std::unique_ptr<flow::LayerTree> last_layer_tree_;
PlatformView* platform_view_;
ftl::WeakPtrFactory<RasterizerDirect> weak_factory_;

// sky::services::rasterizer::Rasterizer (from rasterizer.mojom) override.
void Draw(uint64_t layer_tree_ptr, const DrawCallback& callback) override;
void DoDraw(std::unique_ptr<flow::LayerTree> tree);

FTL_DISALLOW_COPY_AND_ASSIGN(RasterizerDirect);
};
Expand Down
15 changes: 1 addition & 14 deletions sky/shell/platform_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,7 @@ PlatformView::Config::~Config() = default;

PlatformView::PlatformView()
: rasterizer_(Rasterizer::Create()), size_(SkISize::Make(0, 0)) {
// Create the engine for this platform view.
Engine::Config engine_config;

ftl::WeakPtr<Rasterizer> rasterizer_impl =
rasterizer_->GetWeakRasterizerPtr();
rasterizer::RasterizerPtr rasterizer;
auto request = glue::WrapMovable(mojo::GetProxy(&rasterizer));

blink::Threads::Gpu()->PostTask([rasterizer_impl, request]() mutable {
if (rasterizer_impl)
rasterizer_impl->ConnectToRasterizer(request.Unwrap());
});

engine_.reset(new Engine(engine_config, rasterizer.Pass()));
engine_.reset(new Engine(rasterizer_.get()));

// Setup the platform config.
config_.ui_delegate = engine_->GetWeakPtr();
Expand Down
14 changes: 7 additions & 7 deletions sky/shell/rasterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@
#include <memory>

#include "flutter/flow/layers/layer_tree.h"
#include "flutter/synchronization/pipeline.h"
#include "lib/ftl/functional/closure.h"
#include "lib/ftl/memory/weak_ptr.h"
#include "lib/ftl/synchronization/waitable_event.h"
#include "lib/ftl/functional/closure.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "flutter/services/rasterizer/rasterizer.mojom.h"

namespace sky {
namespace shell {

class PlatformView;

class Rasterizer : public rasterizer::Rasterizer {
class Rasterizer {
public:
~Rasterizer() override;

virtual void ConnectToRasterizer(
mojo::InterfaceRequest<rasterizer::Rasterizer> request) = 0;
virtual ~Rasterizer();

virtual void Setup(PlatformView* platform_view,
ftl::Closure rasterizer_continuation,
Expand All @@ -37,6 +34,9 @@ class Rasterizer : public rasterizer::Rasterizer {

virtual flow::LayerTree* GetLastLayerTree() = 0;

virtual void Draw(
ftl::RefPtr<flutter::Pipeline<flow::LayerTree>> pipeline) = 0;

// Implemented by each GPU backend.
static std::unique_ptr<Rasterizer> Create();
};
Expand Down
Loading

0 comments on commit ace7ce8

Please sign in to comment.