From f08b91396d209f934c357d6b9f93ea1a6fe5f500 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 2 Jul 2021 15:47:09 -0700 Subject: [PATCH] Wire up playground WSI. --- engine/src/flutter/impeller/BUILD.gn | 3 + .../impeller/compiler/compiler_unittests.cc | 1 - .../src/flutter/impeller/compositor/context.h | 2 + .../flutter/impeller/compositor/context.mm | 23 +++- .../impeller/compositor/shader_library.mm | 4 + .../flutter/impeller/compositor/surface.mm | 2 +- engine/src/flutter/impeller/entity/BUILD.gn | 8 ++ .../impeller/entity/entity_unittests.mm | 16 +++ engine/src/flutter/impeller/fixtures/BUILD.gn | 1 + .../src/flutter/impeller/playground/BUILD.gn | 27 +++++ .../flutter/impeller/playground/playground.h | 23 ++++ .../flutter/impeller/playground/playground.mm | 103 ++++++++++++++++++ .../src/flutter/impeller/primitives/BUILD.gn | 4 + .../primitives/primitives_unittests.mm | 3 + 14 files changed, 214 insertions(+), 6 deletions(-) create mode 100644 engine/src/flutter/impeller/entity/entity_unittests.mm create mode 100644 engine/src/flutter/impeller/playground/BUILD.gn create mode 100644 engine/src/flutter/impeller/playground/playground.h create mode 100644 engine/src/flutter/impeller/playground/playground.mm create mode 100644 engine/src/flutter/impeller/primitives/primitives_unittests.mm diff --git a/engine/src/flutter/impeller/BUILD.gn b/engine/src/flutter/impeller/BUILD.gn index 01e77b316d..4a46e0e8a6 100644 --- a/engine/src/flutter/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/BUILD.gn @@ -26,7 +26,10 @@ executable("impeller_unittests") { deps = [ "compiler:compiler_unittests", "compositor:compositor_unittests", + "entity:entity_unittests", "fixtures", + "playground", + "primitives:primitives_unittests", "//flutter/testing", ] } diff --git a/engine/src/flutter/impeller/compiler/compiler_unittests.cc b/engine/src/flutter/impeller/compiler/compiler_unittests.cc index bdaabc4e76..5c97bfc45a 100644 --- a/engine/src/flutter/impeller/compiler/compiler_unittests.cc +++ b/engine/src/flutter/impeller/compiler/compiler_unittests.cc @@ -5,7 +5,6 @@ #include "flutter/impeller/compiler/compiler.h" #include "flutter/testing/testing.h" #include "gtest/gtest.h" -#include "third_party/inja/include/inja/inja.hpp" namespace impeller { namespace compiler { diff --git a/engine/src/flutter/impeller/compositor/context.h b/engine/src/flutter/impeller/compositor/context.h index 19200cfcc7..66e13b7cda 100644 --- a/engine/src/flutter/impeller/compositor/context.h +++ b/engine/src/flutter/impeller/compositor/context.h @@ -46,6 +46,8 @@ class Context { std::shared_ptr CreateRenderCommandBuffer() const; + id GetMTLDevice() const; + private: id device_ = nullptr; id render_queue_ = nullptr; diff --git a/engine/src/flutter/impeller/compositor/context.mm b/engine/src/flutter/impeller/compositor/context.mm index f02e99f8da..fd93e198a4 100644 --- a/engine/src/flutter/impeller/compositor/context.mm +++ b/engine/src/flutter/impeller/compositor/context.mm @@ -4,6 +4,7 @@ #include "context.h" +#include "flutter/fml/file.h" #include "flutter/fml/logging.h" #include "flutter/fml/paths.h" #include "impeller/compositor/allocator.h" @@ -36,10 +37,20 @@ Context::Context(std::string shaders_directory) NSError* shader_library_error = nil; auto shader_library_path = fml::paths::JoinPaths({shaders_directory, "impeller.metallib"}); - id library = - [device_ newLibraryWithFile:@(shader_library_path.c_str()) - error:&shader_library_error]; - if (!library) { + + auto library_exists = fml::IsFile(shader_library_path); + + if (!library_exists) { + FML_LOG(ERROR) << "Shader library does not exist at path '" + << shader_library_path + << "'. No piplines can be created in this context."; + } + auto library = + library_exists + ? [device_ newLibraryWithFile:@(shader_library_path.c_str()) + error:&shader_library_error] + : [device_ newDefaultLibrary]; + if (!library && shader_library_error) { FML_LOG(ERROR) << "Could not create shader library: " << shader_library_error.localizedDescription.UTF8String; return; @@ -118,4 +129,8 @@ std::shared_ptr Context::GetTransientsAllocator() const { return transients_allocator_; } +id Context::GetMTLDevice() const { + return device_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/shader_library.mm b/engine/src/flutter/impeller/compositor/shader_library.mm index a7009934bd..65db43c6ea 100644 --- a/engine/src/flutter/impeller/compositor/shader_library.mm +++ b/engine/src/flutter/impeller/compositor/shader_library.mm @@ -13,6 +13,10 @@ ShaderLibrary::~ShaderLibrary() = default; std::shared_ptr ShaderLibrary::GetFunction( const std::string_view& name, ShaderStage stage) { + if (!library_) { + return nullptr; + } + ShaderKey key(name, stage); if (auto found = functions_.find(key); found != functions_.end()) { diff --git a/engine/src/flutter/impeller/compositor/surface.mm b/engine/src/flutter/impeller/compositor/surface.mm index 40f57d9e00..a872e8a467 100644 --- a/engine/src/flutter/impeller/compositor/surface.mm +++ b/engine/src/flutter/impeller/compositor/surface.mm @@ -30,7 +30,7 @@ bool Surface::Present() const { auto callback = present_callback_; if (!callback) { - return true; + return false; } return callback(); diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 66e2b7a3db..c3fa607152 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -18,3 +18,11 @@ impeller_component("entity") { "../primitives", ] } + +impeller_component("entity_unittests") { + testonly = true + + sources = [ "entity_unittests.mm" ] + + deps = [ "../playground" ] +} diff --git a/engine/src/flutter/impeller/entity/entity_unittests.mm b/engine/src/flutter/impeller/entity/entity_unittests.mm new file mode 100644 index 0000000000..b53534d35e --- /dev/null +++ b/engine/src/flutter/impeller/entity/entity_unittests.mm @@ -0,0 +1,16 @@ +// 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 "flutter/testing/testing.h" +#include "impeller/playground/playground.h" + +namespace impeller { +namespace testing { + +TEST_F(Playground, CanCreateEntity) { + OpenPlaygroundHere([]() { return true; }); +} + +} // namespace testing +} // namespace impeller diff --git a/engine/src/flutter/impeller/fixtures/BUILD.gn b/engine/src/flutter/impeller/fixtures/BUILD.gn index 519515d514..cd2fc9ad08 100644 --- a/engine/src/flutter/impeller/fixtures/BUILD.gn +++ b/engine/src/flutter/impeller/fixtures/BUILD.gn @@ -9,5 +9,6 @@ test_fixtures("fixtures") { fixtures = [ "sample.vert", "types.h", + "image.png", ] } diff --git a/engine/src/flutter/impeller/playground/BUILD.gn b/engine/src/flutter/impeller/playground/BUILD.gn new file mode 100644 index 0000000000..d4fa603a0b --- /dev/null +++ b/engine/src/flutter/impeller/playground/BUILD.gn @@ -0,0 +1,27 @@ +# 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. + +import("//flutter/impeller/tools/impeller.gni") +import("//flutter/testing/testing.gni") + +impeller_component("playground") { + testonly = true + + sources = [ + "playground.h", + "playground.mm", + ] + + public_deps = [ + "//flutter/testing", + "//third_party/glfw", + ] + + if (is_mac) { + libs = [ + "AppKit.framework", + "QuartzCore.framework", + ] + } +} diff --git a/engine/src/flutter/impeller/playground/playground.h b/engine/src/flutter/impeller/playground/playground.h new file mode 100644 index 0000000000..b340f83f48 --- /dev/null +++ b/engine/src/flutter/impeller/playground/playground.h @@ -0,0 +1,23 @@ +// 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 "flutter/fml/closure.h" +#include "flutter/fml/macros.h" +#include "gtest/gtest.h" + +namespace impeller { + +class Playground : public ::testing::Test { + public: + Playground(); + + ~Playground(); + + bool OpenPlaygroundHere(std::function closure); + + private: + FML_DISALLOW_COPY_AND_ASSIGN(Playground); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/playground/playground.mm b/engine/src/flutter/impeller/playground/playground.mm new file mode 100644 index 0000000000..13e31fd282 --- /dev/null +++ b/engine/src/flutter/impeller/playground/playground.mm @@ -0,0 +1,103 @@ +// 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/playground/playground.h" +#include "flutter/testing/testing.h" +#include "impeller/compositor/context.h" +#include "impeller/compositor/render_pass.h" +#include "impeller/compositor/surface.h" + +#define GLFW_INCLUDE_NONE +#import "third_party/glfw/include/GLFW/glfw3.h" +#define GLFW_EXPOSE_NATIVE_COCOA +#import "third_party/glfw/include/GLFW/glfw3native.h" + +#include +#include + +namespace impeller { + +Playground::Playground() = default; + +Playground::~Playground() = default; + +bool Playground::OpenPlaygroundHere(std::function closure) { + if (!closure) { + return true; + } + + Context context(flutter::testing::GetFixturesPath()); + + if (!context.IsValid()) { + return false; + } + + if (::glfwInit() != GLFW_TRUE) { + return false; + } + fml::ScopedCleanupClosure terminate([]() { ::glfwTerminate(); }); + + ::glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + + auto window = ::glfwCreateWindow(800, 600, "Impeller Playground", NULL, NULL); + if (!window) { + return false; + } + + fml::ScopedCleanupClosure close_window( + [window]() { ::glfwDestroyWindow(window); }); + + NSWindow* cocoa_window = ::glfwGetCocoaWindow(window); + CAMetalLayer* layer = [CAMetalLayer layer]; + layer.device = context.GetMTLDevice(); + layer.pixelFormat = MTLPixelFormatBGRA8Unorm; + cocoa_window.contentView.layer = layer; + cocoa_window.contentView.wantsLayer = YES; + + while (true) { + ::glfwWaitEventsTimeout(1.0 / 30.0); + + if (::glfwWindowShouldClose(window)) { + return true; + } + + auto current_drawable = [layer nextDrawable]; + + if (!current_drawable) { + FML_LOG(ERROR) << "Could not acquire current drawable."; + return false; + } + + ColorRenderPassAttachment color0; + color0.texture = std::make_shared(current_drawable.texture); + color0.clear_color = Color::SkyBlue(); + color0.load_action = LoadAction::kClear; + color0.store_action = StoreAction::kStore; + + RenderPassDescriptor desc; + desc.SetColorAttachment(color0, 0u); + + Surface surface(desc, [current_drawable, closure]() { + if (!closure()) { + return false; + } + [current_drawable present]; + return true; + }); + + if (!surface.IsValid()) { + FML_LOG(ERROR) << "Could not wrap surface to render to into."; + return false; + } + + if (!surface.Present()) { + FML_LOG(ERROR) << "Could not render into playground surface."; + return false; + } + } + + return true; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/primitives/BUILD.gn b/engine/src/flutter/impeller/primitives/BUILD.gn index 3438915576..07a3d2e3a9 100644 --- a/engine/src/flutter/impeller/primitives/BUILD.gn +++ b/engine/src/flutter/impeller/primitives/BUILD.gn @@ -25,3 +25,7 @@ impeller_component("primitives") { "../compositor", ] } + +impeller_component("primitives_unittests") { + sources = [ "primitives_unittests.mm" ] +} diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm new file mode 100644 index 0000000000..e7217c7d7b --- /dev/null +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -0,0 +1,3 @@ +// 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.