diff --git a/engine/src/flutter/impeller/host/impeller_renderer.mm b/engine/src/flutter/impeller/host/impeller_renderer.mm index f54f7ae82d..87a3906b0a 100644 --- a/engine/src/flutter/impeller/host/impeller_renderer.mm +++ b/engine/src/flutter/impeller/host/impeller_renderer.mm @@ -7,6 +7,7 @@ #import "assets_location.h" #include "flutter/fml/logging.h" +#import "impeller/compositor/renderer.h" #import "impeller_renderer.h" #import "shaders_location.h" @@ -40,6 +41,7 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100; float _rotation; MTKMesh* _mesh; + impeller::Renderer renderer_; } - (nonnull instancetype)initWithMetalKitView:(nonnull MTKView*)view { @@ -51,6 +53,10 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100; [self _loadAssets]; } + if (!renderer_.IsValid()) { + FML_LOG(ERROR) << "Impeller Renderer is not valid."; + } + return self; } @@ -220,6 +226,7 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100; } - (void)drawInMTKView:(nonnull MTKView*)view { + renderer_.Render(); /// Per frame updates here dispatch_semaphore_wait(_inFlightSemaphore, DISPATCH_TIME_FOREVER); @@ -294,7 +301,8 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100; } - (void)mtkView:(nonnull MTKView*)view drawableSizeWillChange:(CGSize)size { - NSLog(@"Drawable sized did change: %@", NSStringFromSize(size)); + renderer_.SurfaceSizeDidChange({size.width, size.height}); + float aspect = size.width / (float)size.height; _projectionMatrix = matrix_perspective_right_hand(65.0f * (M_PI / 180.0f), aspect, 0.1f, 100.0f); @@ -302,11 +310,13 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100; #pragma mark Matrix Math Utilities +// NOLINTNEXTLINE matrix_float4x4 matrix4x4_translation(float tx, float ty, float tz) { return (matrix_float4x4){ {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {tx, ty, tz, 1}}}; } +// NOLINTNEXTLINE static matrix_float4x4 matrix4x4_rotation(float radians, vector_float3 axis) { axis = vector_normalize(axis); float ct = cosf(radians); @@ -321,6 +331,7 @@ static matrix_float4x4 matrix4x4_rotation(float radians, vector_float3 axis) { {0, 0, 0, 1}}}; } +// NOLINTNEXTLINE matrix_float4x4 matrix_perspective_right_hand(float fovyRadians, float aspect, float nearZ, diff --git a/engine/src/flutter/impeller/impeller/BUILD.gn b/engine/src/flutter/impeller/impeller/BUILD.gn index 4932aea7dc..93a7fc4818 100644 --- a/engine/src/flutter/impeller/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/impeller/BUILD.gn @@ -4,7 +4,13 @@ import("//flutter/common/config.gni") +config("impeller_config") { + include_dirs = [ ".." ] +} + source_set("impeller") { + public_configs = [ ":impeller_config" ] + cflags_objc = flutter_cflags_objc_arc cflags_objcc = flutter_cflags_objcc_arc @@ -12,6 +18,20 @@ source_set("impeller") { "entity", "geometry", "image", + "compositor", + ] + + compositor_sources = [ + "compositor/context.h", + "compositor/context.mm", + "compositor/pipeline_library.h", + "compositor/pipeline_library.mm", + "compositor/renderer.h", + "compositor/renderer.mm", + "compositor/shader_library.h", + "compositor/shader_library.mm", + "compositor/surface.cc", + "compositor/surface.h", ] geometry_sources = [ @@ -51,7 +71,8 @@ source_set("impeller") { "image/image_result.h", ] - sources = geometry_sources + entity_sources + image_sources + sources = + compositor_sources + geometry_sources + entity_sources + image_sources deps = [ "//flutter/fml", diff --git a/engine/src/flutter/impeller/impeller/compositor/context.h b/engine/src/flutter/impeller/impeller/compositor/context.h new file mode 100644 index 0000000000..ad126c3d6a --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/context.h @@ -0,0 +1,31 @@ +// 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. + +#pragma once + +#include + +#include "flutter/fml/macros.h" + +namespace impeller { + +class Context { + public: + Context(); + + ~Context(); + + bool IsValid() const; + + private: + id device_; + id render_queue_; + id transfer_queue_; + + bool is_valid_ = false; + + FML_DISALLOW_COPY_AND_ASSIGN(Context); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/context.mm b/engine/src/flutter/impeller/impeller/compositor/context.mm new file mode 100644 index 0000000000..cba0ba9105 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/context.mm @@ -0,0 +1,35 @@ +// 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 "context.h" + +#include "flutter/fml/logging.h" + +namespace impeller { + +Context::Context() : device_(::MTLCreateSystemDefaultDevice()) { + if (!device_) { + return; + } + + render_queue_ = device_.newCommandQueue; + transfer_queue_ = device_.newCommandQueue; + + if (!render_queue_ || !transfer_queue_) { + return; + } + + render_queue_.label = @"Impeller Render Queue"; + transfer_queue_.label = @"Impeller Transfer Queue"; + + is_valid_ = true; +} + +Context::~Context() = default; + +bool Context::IsValid() const { + return is_valid_; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h new file mode 100644 index 0000000000..a20ff2a08c --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h @@ -0,0 +1,21 @@ +// 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. + +#pragma once + +#include "flutter/fml/macros.h" + +namespace impeller { + +class PipelineLibrary { + public: + PipelineLibrary(); + + ~PipelineLibrary(); + + private: + FML_DISALLOW_COPY_AND_ASSIGN(PipelineLibrary); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm new file mode 100644 index 0000000000..e69de29bb2 diff --git a/engine/src/flutter/impeller/impeller/compositor/renderer.h b/engine/src/flutter/impeller/impeller/compositor/renderer.h new file mode 100644 index 0000000000..5a66c0fd06 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/renderer.h @@ -0,0 +1,35 @@ +// 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. + +#pragma once + +#include "context.h" +#include "flutter/fml/macros.h" +#include "impeller/geometry/size.h" + +namespace impeller { + +class Renderer { + public: + Renderer(); + + ~Renderer(); + + bool IsValid() const; + + bool SurfaceSizeDidChange(Size size); + + bool Render(); + + private: + Context context_; + Size size_; + bool is_valid_ = false; + + bool ShouldRender() const; + + FML_DISALLOW_COPY_AND_ASSIGN(Renderer); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/renderer.mm b/engine/src/flutter/impeller/impeller/compositor/renderer.mm new file mode 100644 index 0000000000..adbd310930 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/renderer.mm @@ -0,0 +1,46 @@ +// 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 "renderer.h" + +#include "flutter/fml/logging.h" + +namespace impeller { + +Renderer::Renderer() { + if (!context_.IsValid()) { + return; + } + + is_valid_ = true; +} + +Renderer::~Renderer() = default; + +bool Renderer::IsValid() const { + return is_valid_; +} + +bool Renderer::ShouldRender() const { + return IsValid() && !size_.IsZero(); +} + +bool Renderer::SurfaceSizeDidChange(Size size) { + if (size_ == size) { + return true; + } + + size_ = size; + return true; +} + +bool Renderer::Render() { + if (!ShouldRender()) { + return false; + } + + return true; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/shader_library.h b/engine/src/flutter/impeller/impeller/compositor/shader_library.h new file mode 100644 index 0000000000..6d62e86a97 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/shader_library.h @@ -0,0 +1,21 @@ +// 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. + +#pragma once + +#include "flutter/fml/macros.h" + +namespace impeller { + +class ShaderLibrary { + public: + ShaderLibrary(); + + ~ShaderLibrary(); + + private: + FML_DISALLOW_COPY_AND_ASSIGN(ShaderLibrary); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/shader_library.mm b/engine/src/flutter/impeller/impeller/compositor/shader_library.mm new file mode 100644 index 0000000000..e69de29bb2 diff --git a/engine/src/flutter/impeller/impeller/compositor/surface.cc b/engine/src/flutter/impeller/impeller/compositor/surface.cc new file mode 100644 index 0000000000..e69de29bb2 diff --git a/engine/src/flutter/impeller/impeller/compositor/surface.h b/engine/src/flutter/impeller/impeller/compositor/surface.h new file mode 100644 index 0000000000..106548a31f --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/surface.h @@ -0,0 +1,21 @@ +// 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. + +#pragma once + +#include "flutter/fml/macros.h" + +namespace impeller { + +class Surface { + public: + Surface(); + + ~Surface(); + + private: + FML_DISALLOW_COPY_AND_ASSIGN(Surface); +}; + +} // namespace impeller