From edab9d7ef28512fc60fa265b4dc1877a225060a6 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Sat, 8 May 2021 17:49:33 -0700 Subject: [PATCH] Wire up render loop. --- engine/src/flutter/impeller/impeller/BUILD.gn | 2 +- .../impeller/impeller/compositor/context.h | 5 +- .../impeller/impeller/compositor/context.mm | 8 +++ .../impeller/impeller/compositor/renderer.h | 8 ++- .../impeller/impeller/compositor/renderer.mm | 14 +++-- .../impeller/impeller/compositor/surface.cc | 0 .../impeller/impeller/compositor/surface.h | 15 +++++- .../impeller/impeller/compositor/surface.mm | 52 +++++++++++++++++++ 8 files changed, 95 insertions(+), 9 deletions(-) delete mode 100644 engine/src/flutter/impeller/impeller/compositor/surface.cc create mode 100644 engine/src/flutter/impeller/impeller/compositor/surface.mm diff --git a/engine/src/flutter/impeller/impeller/BUILD.gn b/engine/src/flutter/impeller/impeller/BUILD.gn index 93a7fc48188..8e7fb5c88bb 100644 --- a/engine/src/flutter/impeller/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/impeller/BUILD.gn @@ -30,8 +30,8 @@ source_set("impeller") { "compositor/renderer.mm", "compositor/shader_library.h", "compositor/shader_library.mm", - "compositor/surface.cc", "compositor/surface.h", + "compositor/surface.mm", ] geometry_sources = [ diff --git a/engine/src/flutter/impeller/impeller/compositor/context.h b/engine/src/flutter/impeller/impeller/compositor/context.h index ad126c3d6a1..163239a7c34 100644 --- a/engine/src/flutter/impeller/impeller/compositor/context.h +++ b/engine/src/flutter/impeller/impeller/compositor/context.h @@ -18,11 +18,14 @@ class Context { bool IsValid() const; + id GetRenderQueue() const; + + id GetTransferQueue() const; + private: id device_; id render_queue_; id transfer_queue_; - bool is_valid_ = false; FML_DISALLOW_COPY_AND_ASSIGN(Context); diff --git a/engine/src/flutter/impeller/impeller/compositor/context.mm b/engine/src/flutter/impeller/impeller/compositor/context.mm index cba0ba9105f..e64025f84c0 100644 --- a/engine/src/flutter/impeller/impeller/compositor/context.mm +++ b/engine/src/flutter/impeller/impeller/compositor/context.mm @@ -32,4 +32,12 @@ bool Context::IsValid() const { return is_valid_; } +id Context::GetRenderQueue() const { + return render_queue_; +} + +id Context::GetTransferQueue() const { + return transfer_queue_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/renderer.h b/engine/src/flutter/impeller/impeller/compositor/renderer.h index 5a66c0fd06c..8fc32a5e89d 100644 --- a/engine/src/flutter/impeller/impeller/compositor/renderer.h +++ b/engine/src/flutter/impeller/impeller/compositor/renderer.h @@ -4,8 +4,11 @@ #pragma once -#include "context.h" +#include + #include "flutter/fml/macros.h" +#include "impeller/compositor/context.h" +#include "impeller/compositor/surface.h" #include "impeller/geometry/size.h" namespace impeller { @@ -23,7 +26,8 @@ class Renderer { bool Render(); private: - Context context_; + std::shared_ptr context_; + std::unique_ptr surface_; Size size_; bool is_valid_ = false; diff --git a/engine/src/flutter/impeller/impeller/compositor/renderer.mm b/engine/src/flutter/impeller/impeller/compositor/renderer.mm index adbd310930b..5d5b8ac45af 100644 --- a/engine/src/flutter/impeller/impeller/compositor/renderer.mm +++ b/engine/src/flutter/impeller/impeller/compositor/renderer.mm @@ -2,14 +2,20 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "renderer.h" +#include "impeller/compositor/renderer.h" #include "flutter/fml/logging.h" namespace impeller { -Renderer::Renderer() { - if (!context_.IsValid()) { +Renderer::Renderer() + : context_(std::make_shared()), + surface_(std::make_unique(context_)) { + if (!context_->IsValid()) { + return; + } + + if (!surface_->IsValid()) { return; } @@ -40,7 +46,7 @@ bool Renderer::Render() { return false; } - return true; + return surface_->Render(); } } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/surface.cc b/engine/src/flutter/impeller/impeller/compositor/surface.cc deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/engine/src/flutter/impeller/impeller/compositor/surface.h b/engine/src/flutter/impeller/impeller/compositor/surface.h index 106548a31f0..a172a0e6b30 100644 --- a/engine/src/flutter/impeller/impeller/compositor/surface.h +++ b/engine/src/flutter/impeller/impeller/compositor/surface.h @@ -4,17 +4,30 @@ #pragma once +#include + +#include + #include "flutter/fml/macros.h" +#include "impeller/compositor/context.h" namespace impeller { class Surface { public: - Surface(); + Surface(std::shared_ptr context); ~Surface(); + bool IsValid() const; + + bool Render() const; + private: + std::shared_ptr context_; + dispatch_semaphore_t frames_in_flight_sema_; + bool is_valid_ = false; + FML_DISALLOW_COPY_AND_ASSIGN(Surface); }; diff --git a/engine/src/flutter/impeller/impeller/compositor/surface.mm b/engine/src/flutter/impeller/impeller/compositor/surface.mm new file mode 100644 index 00000000000..7cfa292a113 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/surface.mm @@ -0,0 +1,52 @@ +// Copyright 2013 The Flutter 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 "impeller/compositor/surface.h" + +#include "flutter/fml/logging.h" + +namespace impeller { + +constexpr size_t kMaxFramesInFlight = 3u; + +Surface::Surface(std::shared_ptr context) + : context_(std::move(context)), + frames_in_flight_sema_(::dispatch_semaphore_create(kMaxFramesInFlight)) { + if (!context_ || !context_->IsValid()) { + return; + } + + is_valid_ = true; +} + +Surface::~Surface() = default; + +bool Surface::IsValid() const { + return is_valid_; +} + +bool Surface::Render() const { + if (!IsValid()) { + return false; + } + + auto command_buffer = [context_->GetRenderQueue() commandBuffer]; + + if (!command_buffer) { + return false; + } + + ::dispatch_semaphore_wait(frames_in_flight_sema_, DISPATCH_TIME_FOREVER); + + __block dispatch_semaphore_t block_sema = frames_in_flight_sema_; + [command_buffer addCompletedHandler:^(id buffer) { + ::dispatch_semaphore_signal(block_sema); + }]; + + [command_buffer commit]; + + return true; +} + +} // namespace impeller