diff --git a/engine/src/flutter/impeller/impeller/compositor/BUILD.gn b/engine/src/flutter/impeller/impeller/compositor/BUILD.gn index 1765dfbe64..35aa61cd0b 100644 --- a/engine/src/flutter/impeller/impeller/compositor/BUILD.gn +++ b/engine/src/flutter/impeller/impeller/compositor/BUILD.gn @@ -6,6 +6,8 @@ import("//flutter/impeller/tools/impeller.gni") impeller_component("compositor") { sources = [ + "comparable.cc", + "comparable.h", "context.h", "context.mm", "pipeline.h", diff --git a/engine/src/flutter/impeller/impeller/compositor/comparable.cc b/engine/src/flutter/impeller/impeller/compositor/comparable.cc new file mode 100644 index 0000000000..86c8e18740 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/comparable.cc @@ -0,0 +1,11 @@ +// 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/comparable.h" + +namespace impeller { + +// + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/comparable.h b/engine/src/flutter/impeller/impeller/compositor/comparable.h new file mode 100644 index 0000000000..b546153aaf --- /dev/null +++ b/engine/src/flutter/impeller/impeller/compositor/comparable.h @@ -0,0 +1,85 @@ +// 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 +#include +#include +#include + +#include "flutter/fml/hash_combine.h" +#include "flutter/fml/macros.h" + +namespace impeller { + +class ComparableBase {}; + +template +class Comparable : ComparableBase { + public: + virtual std::size_t GetHash() const = 0; + + virtual bool IsEqual(const Type& other) const = 0; +}; + +template < + class ComparableType, + class = std::enable_if_t>> +struct ComparableHash { + std::size_t operator()(const ComparableType& object) const { + return object.GetHash(); + } +}; + +template < + class ComparableType, + class = std::enable_if_t>> +struct ComparableEqual { + bool operator()(const ComparableType& lhs, const ComparableType& rhs) const { + return lhs.IsEqual(rhs); + } +}; + +template < + class ComparableType, + class = std::enable_if_t>> +bool DeepComparePointer(const std::shared_ptr& lhs, + const std::shared_ptr& rhs) { + if (lhs == rhs) { + return true; + } + + if (lhs && rhs) { + return lhs->IsEqual(*rhs); + } + + return false; +} + +template < + class Key, + class ComparableType, + class = std::enable_if_t>> +bool DeepCompareMap(const std::map>& lhs, + const std::map>& rhs) { + if (lhs.size() != rhs.size()) { + return false; + } + + for (auto i = lhs.begin(), j = rhs.begin(); i != lhs.end(); i++, j++) { + if (i->first != j->first) { + return false; + } + + if (!DeepComparePointer(i->second, j->second)) { + return false; + } + } + + return true; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h index 0b5869e756..0370aee54e 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.h @@ -14,6 +14,7 @@ #include #include "flutter/fml/macros.h" +#include "impeller/compositor/comparable.h" #include "impeller/shader_glue/shader_types.h" namespace impeller { @@ -21,14 +22,8 @@ namespace impeller { class ShaderFunction; class VertexDescriptor; -class PipelineDescriptor { +class PipelineDescriptor : public Comparable { public: - struct HashEqual { - std::size_t operator()(const PipelineDescriptor& des) const; - bool operator()(const PipelineDescriptor& d1, - const PipelineDescriptor& d2) const; - }; - PipelineDescriptor(); ~PipelineDescriptor(); @@ -45,13 +40,17 @@ class PipelineDescriptor { MTLRenderPipelineDescriptor* GetMTLRenderPipelineDescriptor() const; + // Comparable + std::size_t GetHash() const override; + + // Comparable + bool IsEqual(const PipelineDescriptor& other) const override; + private: std::string label_; size_t sample_count_ = 1; std::map> entrypoints_; std::shared_ptr vertex_descriptor_; - - FML_DISALLOW_COPY_AND_ASSIGN(PipelineDescriptor); }; } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm index a9d199763c..bcfb38c67f 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_descriptor.mm @@ -4,8 +4,8 @@ #include "impeller/compositor/pipeline_descriptor.h" -#include "flutter/fml/hash_combine.h" #include "impeller/compositor/shader_library.h" +#include "impeller/compositor/vertex_descriptor.h" namespace impeller { @@ -13,18 +13,29 @@ PipelineDescriptor::PipelineDescriptor() = default; PipelineDescriptor::~PipelineDescriptor() = default; -std::size_t PipelineDescriptor::HashEqual::operator()( - const PipelineDescriptor& des) const { +// Comparable +std::size_t PipelineDescriptor::GetHash() const { auto seed = fml::HashCombine(); - fml::HashCombineSeed(seed, des.label_); - for (const auto& entry : des.entrypoints_) { - fml::HashCombineSeed(seed, entry) + fml::HashCombineSeed(seed, label_); + fml::HashCombineSeed(seed, sample_count_); + for (const auto& entry : entrypoints_) { + fml::HashCombineSeed(seed, entry.first); + if (auto second = entry.second) { + fml::HashCombineSeed(seed, second->GetHash()); + } } + if (vertex_descriptor_) { + fml::HashCombineSeed(seed, vertex_descriptor_->GetHash()); + } + return seed; } -bool PipelineDescriptor::HashEqual::operator()( - const PipelineDescriptor& d1, - const PipelineDescriptor& d2) const {} +// Comparable +bool PipelineDescriptor::IsEqual(const PipelineDescriptor& other) const { + return label_ == other.label_ && sample_count_ == other.sample_count_ && + DeepCompareMap(entrypoints_, other.entrypoints_) && + DeepComparePointer(vertex_descriptor_, other.vertex_descriptor_); +} PipelineDescriptor& PipelineDescriptor::SetLabel( const std::string_view& label) { diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h index 1740df363e..aa0ae7b594 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h @@ -27,8 +27,8 @@ class PipelineLibrary { private: using Pipelines = std::unordered_map, - PipelineDescriptor::HashEqual, - PipelineDescriptor::HashEqual>; + ComparableHash, + ComparableEqual>; id device_; Pipelines pipelines_; diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm index 26abd1b303..c70fcdcef2 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm @@ -23,6 +23,10 @@ std::future> PipelineLibrary::GetRenderPipeline( return future; } + // WIP Pipeline is not saved. + + pipelines_[descriptor] = nullptr; + auto completion_handler = ^(id _Nullable render_pipeline_state, NSError* _Nullable error) { diff --git a/engine/src/flutter/impeller/impeller/compositor/shader_library.h b/engine/src/flutter/impeller/impeller/compositor/shader_library.h index 76c280f5d9..1a96ebad00 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shader_library.h +++ b/engine/src/flutter/impeller/impeller/compositor/shader_library.h @@ -13,20 +13,27 @@ #include "flutter/fml/hash_combine.h" #include "flutter/fml/macros.h" +#include "impeller/compositor/comparable.h" #include "impeller/shader_glue/shader_types.h" namespace impeller { class Context; -class ShaderFunction { +class ShaderFunction : public Comparable { public: - ~ShaderFunction(); + virtual ~ShaderFunction(); ShaderStage GetStage() const; id GetMTLFunction() const; + // Comparable + std::size_t GetHash() const override; + + // Comparable + bool IsEqual(const ShaderFunction& other) const override; + private: friend class ShaderLibrary; diff --git a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h index bda80d5a19..641886e4f4 100644 --- a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h +++ b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h @@ -5,14 +5,21 @@ #pragma once #include "flutter/fml/macros.h" +#include "impeller/compositor/comparable.h" namespace impeller { -class VertexDescriptor { +class VertexDescriptor : public Comparable { public: VertexDescriptor(); - ~VertexDescriptor(); + virtual ~VertexDescriptor(); + + // Comparable + std::size_t GetHash() const override; + + // Comparable + bool IsEqual(const VertexDescriptor& other) const override; private: FML_DISALLOW_COPY_AND_ASSIGN(VertexDescriptor);