Wire up playground WSI.

This commit is contained in:
Chinmay Garde
2021-07-02 15:47:09 -07:00
committed by Dan Field
parent c3a2afd16d
commit f08b91396d
14 changed files with 214 additions and 6 deletions

View File

@@ -26,7 +26,10 @@ executable("impeller_unittests") {
deps = [
"compiler:compiler_unittests",
"compositor:compositor_unittests",
"entity:entity_unittests",
"fixtures",
"playground",
"primitives:primitives_unittests",
"//flutter/testing",
]
}

View File

@@ -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 {

View File

@@ -46,6 +46,8 @@ class Context {
std::shared_ptr<CommandBuffer> CreateRenderCommandBuffer() const;
id<MTLDevice> GetMTLDevice() const;
private:
id<MTLDevice> device_ = nullptr;
id<MTLCommandQueue> render_queue_ = nullptr;

View File

@@ -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<MTLLibrary> 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<Allocator> Context::GetTransientsAllocator() const {
return transients_allocator_;
}
id<MTLDevice> Context::GetMTLDevice() const {
return device_;
}
} // namespace impeller

View File

@@ -13,6 +13,10 @@ ShaderLibrary::~ShaderLibrary() = default;
std::shared_ptr<const ShaderFunction> 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()) {

View File

@@ -30,7 +30,7 @@ bool Surface::Present() const {
auto callback = present_callback_;
if (!callback) {
return true;
return false;
}
return callback();

View File

@@ -18,3 +18,11 @@ impeller_component("entity") {
"../primitives",
]
}
impeller_component("entity_unittests") {
testonly = true
sources = [ "entity_unittests.mm" ]
deps = [ "../playground" ]
}

View File

@@ -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

View File

@@ -9,5 +9,6 @@ test_fixtures("fixtures") {
fixtures = [
"sample.vert",
"types.h",
"image.png",
]
}

View File

@@ -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",
]
}
}

View File

@@ -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<bool()> closure);
private:
FML_DISALLOW_COPY_AND_ASSIGN(Playground);
};
} // namespace impeller

View File

@@ -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 <Metal/Metal.h>
#include <QuartzCore/QuartzCore.h>
namespace impeller {
Playground::Playground() = default;
Playground::~Playground() = default;
bool Playground::OpenPlaygroundHere(std::function<bool()> 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<Texture>(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

View File

@@ -25,3 +25,7 @@ impeller_component("primitives") {
"../compositor",
]
}
impeller_component("primitives_unittests") {
sources = [ "primitives_unittests.mm" ]
}

View File

@@ -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.