Skip to content

Commit

Permalink
Back Vulkan Rasterizer with Mozart View (flutter#3568)
Browse files Browse the repository at this point in the history
This version does not recycle buffers. It is meant only as an initial solution,
and the proper buffer recycling logic has been broken out into a separate change
  • Loading branch information
freiling authored and chinmaygarde committed Apr 8, 2017
1 parent 9bc769a commit 2a4af69
Show file tree
Hide file tree
Showing 14 changed files with 573 additions and 62 deletions.
26 changes: 17 additions & 9 deletions content_handler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ assert(is_fuchsia)

declare_args() {
flutter_enable_vulkan = false
flutter_use_vulkan_native_surface = false
}

executable("content_handler") {
Expand Down Expand Up @@ -66,16 +67,23 @@ executable("content_handler") {
]

if (flutter_enable_vulkan) {
defines += [ "FLUTTER_ENABLE_VULKAN" ]
defines += [ "FLUTTER_ENABLE_VULKAN=1" ]

libs += [ "hid" ]

sources += [
"direct_input.cc",
"direct_input.h",
"vulkan_rasterizer.cc",
"vulkan_rasterizer.h",
]
if (flutter_use_vulkan_native_surface) {
defines += [ "FLUTTER_USE_VULKAN_NATIVE_SURFACE=1" ]
sources += [
"direct_input.cc",
"direct_input.h",
"vulkan_native_rasterizer.cc",
"vulkan_native_rasterizer.h",
]
libs += [ "hid" ]
} else {
sources += [
"vulkan_rasterizer.cc",
"vulkan_rasterizer.h",
]
}

deps += [
"//flutter/vulkan",
Expand Down
8 changes: 8 additions & 0 deletions content_handler/rasterizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#include "flutter/content_handler/software_rasterizer.h"

#if FLUTTER_ENABLE_VULKAN
#if FLUTTER_USE_VULKAN_NATIVE_SURFACE
#include "flutter/content_handler/vulkan_native_rasterizer.h"
#else // FLUTTER_USE_VULKAN_NATIVE_SURFACE
#include "flutter/content_handler/vulkan_rasterizer.h"
#endif // FLUTTER_USE_VULKAN_NATIVE_SURFACE
#endif // FLUTTER_ENABLE_VULKAN

namespace flutter_runner {
Expand All @@ -16,7 +20,11 @@ Rasterizer::~Rasterizer() = default;

std::unique_ptr<Rasterizer> Rasterizer::Create() {
#if FLUTTER_ENABLE_VULKAN
#if FLUTTER_USE_VULKAN_NATIVE_SURFACE
auto vulkan_rasterizer = std::make_unique<VulkanNativeRasterizer>();
#else // FLUTTER_USE_VULKAN_NATIVE_SURFACE
auto vulkan_rasterizer = std::make_unique<VulkanRasterizer>();
#endif // FLUTTER_USE_VULKAN_NATIVE_SURFACE

if (!vulkan_rasterizer->IsValid()) {
FTL_DLOG(INFO) << "Could not initialize a valid vulkan rasterizer. "
Expand Down
12 changes: 6 additions & 6 deletions content_handler/runtime_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ void RuntimeHolder::CreateView(
input_listener_binding_.Bind(GetProxy(&input_listener));
input_connection_->SetEventListener(std::move(input_listener));

#if FLUTTER_ENABLE_VULKAN
#if FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE
direct_input_ = std::make_unique<DirectInput>(
[this](const blink::PointerDataPacket& packet) -> void {
runtime_->DispatchPointerDataPacket(packet);
});
FTL_DCHECK(direct_input_->IsValid());
direct_input_->WaitForReadAvailability();
#endif // FLUTTER_ENABLE_VULKAN
#endif // FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE

mozart::ScenePtr scene;
view_->CreateScene(fidl::GetProxy(&scene));
Expand All @@ -158,9 +158,9 @@ void RuntimeHolder::CreateView(
runtime_ = blink::RuntimeController::Create(this);
runtime_->CreateDartController(script_uri);
runtime_->SetViewportMetrics(viewport_metrics_);
#if FLUTTER_ENABLE_VULKAN
#if FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE
direct_input_->SetViewportMetrics(viewport_metrics_);
#endif // FLUTTER_ENABLE_VULKAN
#endif // FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE
if (!kernel.empty()) {
runtime_->dart_controller()->RunFromKernel(kernel.data(), kernel.size());
} else {
Expand Down Expand Up @@ -377,11 +377,11 @@ void RuntimeHolder::OnInvalidation(mozart::ViewInvalidationPtr invalidation,
// TODO(abarth): Use view_properties_->display_metrics->device_pixel_ratio
// once that's reasonable.
runtime_->SetViewportMetrics(viewport_metrics_);
#if FLUTTER_ENABLE_VULKAN
#if FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE
if (direct_input_) {
direct_input_->SetViewportMetrics(viewport_metrics_);
}
#endif // FLUTTER_ENABLE_VULKAN
#endif // FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE
}

// Remember the scene version for rendering.
Expand Down
8 changes: 4 additions & 4 deletions content_handler/runtime_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include "lib/ftl/macros.h"
#include "lib/ftl/memory/weak_ptr.h"

#if FLUTTER_ENABLE_VULKAN
#if FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE
#include "flutter/content_handler/direct_input.h"
#endif // FLUTTER_ENABLE_VULKAN
#endif // FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE

namespace flutter_runner {
class Rasterizer;
Expand Down Expand Up @@ -92,9 +92,9 @@ class RuntimeHolder : public blink::RuntimeDelegate,
mozart::ViewManagerPtr view_manager_;
fidl::Binding<mozart::ViewListener> view_listener_binding_;
fidl::Binding<mozart::InputListener> input_listener_binding_;
#if FLUTTER_ENABLE_VULKAN
#if FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE
std::unique_ptr<DirectInput> direct_input_;
#endif // FLUTTER_ENABLE_VULKAN
#endif // FLUTTER_ENABLE_VULKAN && FLUTTER_USE_VULKAN_NATIVE_SURFACE
mozart::InputConnectionPtr input_connection_;
mozart::ViewPtr view_;
mozart::ViewPropertiesPtr view_properties_;
Expand Down
16 changes: 13 additions & 3 deletions content_handler/software_rasterizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@

namespace flutter_runner {

SoftwareRasterizer::RasterSurfaceProducer::RasterSurfaceProducer() {
buffer_producer_.reset(new mozart::BufferProducer());
}

sk_sp<SkSurface> SoftwareRasterizer::RasterSurfaceProducer::ProduceSurface(
SkISize size,
mozart::ImagePtr* out_image) {
return mozart::MakeSkSurface(size, buffer_producer_.get(), out_image);
}

SoftwareRasterizer::SoftwareRasterizer() : compositor_context_(nullptr) {}

SoftwareRasterizer::~SoftwareRasterizer() = default;

void SoftwareRasterizer::SetScene(fidl::InterfaceHandle<mozart::Scene> scene) {
scene_.Bind(std::move(scene));
buffer_producer_.reset(new mozart::BufferProducer());
surface_producer_.reset(new RasterSurfaceProducer());
}

void SoftwareRasterizer::Draw(std::unique_ptr<flow::LayerTree> layer_tree,
Expand Down Expand Up @@ -54,7 +64,7 @@ void SoftwareRasterizer::Draw(std::unique_ptr<flow::LayerTree> layer_tree,

layer_tree->Preroll(frame);

flow::SceneUpdateContext context(update.get(), buffer_producer_.get());
flow::SceneUpdateContext context(update.get(), surface_producer_.get());
auto root_node = mozart::Node::New();
root_node->hit_test_behavior = mozart::HitTestBehavior::New();
layer_tree->UpdateScene(context, root_node.get());
Expand All @@ -72,7 +82,7 @@ void SoftwareRasterizer::Draw(std::unique_ptr<flow::LayerTree> layer_tree,
// The image buffer's fence is signalled automatically when the surface
// goes out of scope.
context.ExecutePaintTasks(frame);
buffer_producer_->Tick();
surface_producer_->Tick();

callback();
}
Expand Down
14 changes: 13 additions & 1 deletion content_handler/software_rasterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ class SoftwareRasterizer : public Rasterizer {
ftl::Closure callback) override;

private:
class RasterSurfaceProducer
: public flow::SceneUpdateContext::SurfaceProducer {
public:
RasterSurfaceProducer();
sk_sp<SkSurface> ProduceSurface(SkISize size,
mozart::ImagePtr* out_image) override;
void Tick() { buffer_producer_->Tick(); }

private:
std::unique_ptr<mozart::BufferProducer> buffer_producer_;
};

mozart::ScenePtr scene_;
std::unique_ptr<mozart::BufferProducer> buffer_producer_;
std::unique_ptr<RasterSurfaceProducer> surface_producer_;
flow::CompositorContext compositor_context_;

FTL_DISALLOW_COPY_AND_ASSIGN(SoftwareRasterizer);
Expand Down
90 changes: 90 additions & 0 deletions content_handler/vulkan_native_rasterizer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2017 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.

#include "flutter/content_handler/vulkan_native_rasterizer.h"

#include <utility>

#include "flutter/vulkan/vulkan_native_surface_magma.h"

namespace flutter_runner {

VulkanNativeRasterizer::VulkanNativeRasterizer()
: compositor_context_(nullptr) {
auto proc_table = ftl::MakeRefCounted<vulkan::VulkanProcTable>();

if (!proc_table->HasAcquiredMandatoryProcAddresses()) {
return;
}

auto native_surface = std::make_unique<vulkan::VulkanNativeSurfaceMagma>();

if (!native_surface->IsValid()) {
return;
}

auto window = std::make_unique<vulkan::VulkanWindow>(
proc_table, std::move(native_surface));

if (!window->IsValid()) {
return;
}

window_ = std::move(window);
}

VulkanNativeRasterizer::~VulkanNativeRasterizer() = default;

bool VulkanNativeRasterizer::IsValid() const {
return window_ == nullptr ? false : window_->IsValid();
}

void VulkanNativeRasterizer::SetScene(
fidl::InterfaceHandle<mozart::Scene> scene) {
// TODO: Composition is currently unsupported using the Vulkan backend.
}

void VulkanNativeRasterizer::Draw(std::unique_ptr<flow::LayerTree> layer_tree,
ftl::Closure callback) {
Draw(std::move(layer_tree));
callback();
}

bool VulkanNativeRasterizer::Draw(std::unique_ptr<flow::LayerTree> layer_tree) {
if (layer_tree == nullptr) {
FTL_DLOG(INFO) << "Layer tree was not valid.";
return false;
}

if (!window_->IsValid()) {
FTL_DLOG(INFO) << "Vulkan window was not valid.";
return false;
}

auto surface = window_->AcquireSurface();

if (!surface && surface->getCanvas() != nullptr) {
FTL_DLOG(INFO) << "Could not acquire a vulkan surface.";
return false;
}

{
auto compositor_frame = compositor_context_.AcquireFrame(
window_->GetSkiaGrContext(), // GrContext*
surface->getCanvas(), // SkCanvas
true // instrumentation
);

layer_tree->Raster(compositor_frame);
}

if (!window_->SwapBuffers()) {
FTL_DLOG(INFO) << "Could not swap buffers successfully.";
return false;
}

return true;
}

} // namespace flutter_runner
41 changes: 41 additions & 0 deletions content_handler/vulkan_native_rasterizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017 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.

#ifndef FLUTTER_CONTENT_HANDLER_VULKAN_RASTERIZER_H_
#define FLUTTER_CONTENT_HANDLER_VULKAN_RASTERIZER_H_

#include <memory>

#include "flutter/content_handler/rasterizer.h"
#include "flutter/flow/compositor_context.h"
#include "flutter/vulkan/vulkan_window.h"
#include "lib/ftl/macros.h"

namespace flutter_runner {

class VulkanNativeRasterizer : public Rasterizer {
public:
VulkanNativeRasterizer();

~VulkanNativeRasterizer() override;

bool IsValid() const;

void SetScene(fidl::InterfaceHandle<mozart::Scene> scene) override;

void Draw(std::unique_ptr<flow::LayerTree> layer_tree,
ftl::Closure callback) override;

private:
std::unique_ptr<vulkan::VulkanWindow> window_;
flow::CompositorContext compositor_context_;

bool Draw(std::unique_ptr<flow::LayerTree> layer_tree);

FTL_DISALLOW_COPY_AND_ASSIGN(VulkanNativeRasterizer);
};

} // namespace flutter_runner

#endif // FLUTTER_CONTENT_HANDLER_VULKAN_RASTERIZER_H_
Loading

0 comments on commit 2a4af69

Please sign in to comment.