diff --git a/engine/src/flutter/impeller/impeller/compositor/comparable.cc b/engine/src/flutter/impeller/impeller/compositor/comparable.cc index 86c8e18740..f58bdf6b8c 100644 --- a/engine/src/flutter/impeller/impeller/compositor/comparable.cc +++ b/engine/src/flutter/impeller/impeller/compositor/comparable.cc @@ -6,6 +6,7 @@ namespace impeller { -// +static std::atomic_size_t sLastID; +UniqueID::UniqueID() : id(++sLastID) {} } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/comparable.h b/engine/src/flutter/impeller/impeller/compositor/comparable.h index b546153aaf..0e6b0e8c18 100644 --- a/engine/src/flutter/impeller/impeller/compositor/comparable.h +++ b/engine/src/flutter/impeller/impeller/compositor/comparable.h @@ -15,6 +15,16 @@ namespace impeller { +struct UniqueID { + size_t id; + + UniqueID(); + + constexpr bool operator==(const UniqueID& other) const { + return id == other.id; + } +}; + class ComparableBase {}; template @@ -83,3 +93,14 @@ bool DeepCompareMap(const std::map>& lhs, } } // namespace impeller + +namespace std { + +template <> +struct hash { + constexpr std::size_t operator()(const impeller::UniqueID& id) { + return id.id; + } +}; + +} // namespace std diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h index aa0ae7b594..e3b000a483 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.h @@ -7,6 +7,7 @@ #include #include +#include #include #include "flutter/fml/macros.h" @@ -15,10 +16,8 @@ namespace impeller { -class PipelineLibrary { +class PipelineLibrary : public std::enable_shared_from_this { public: - PipelineLibrary(id device); - ~PipelineLibrary(); std::future> GetRenderPipeline( @@ -32,6 +31,11 @@ class PipelineLibrary { id device_; Pipelines pipelines_; + PipelineLibrary(id device); + + void SavePipeline(PipelineDescriptor descriptor, + std::shared_ptr pipeline); + FML_DISALLOW_COPY_AND_ASSIGN(PipelineLibrary); }; diff --git a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm index c70fcdcef2..6ede19f03f 100644 --- a/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm +++ b/engine/src/flutter/impeller/impeller/compositor/pipeline_library.mm @@ -23,9 +23,7 @@ std::future> PipelineLibrary::GetRenderPipeline( return future; } - // WIP Pipeline is not saved. - - pipelines_[descriptor] = nullptr; + auto thiz = shared_from_this(); auto completion_handler = ^(id _Nullable render_pipeline_state, @@ -35,8 +33,10 @@ std::future> PipelineLibrary::GetRenderPipeline( << error.localizedDescription.UTF8String; promise->set_value(nullptr); } else { - promise->set_value( - std::shared_ptr(new Pipeline(render_pipeline_state))); + auto new_pipeline = + std::shared_ptr(new Pipeline(render_pipeline_state)); + promise->set_value(new_pipeline); + this->SavePipeline(descriptor, new_pipeline); } }; [device_ @@ -46,4 +46,9 @@ std::future> PipelineLibrary::GetRenderPipeline( return future; } +void PipelineLibrary::SavePipeline(PipelineDescriptor descriptor, + std::shared_ptr pipeline) { + pipelines_[descriptor] = std::move(pipeline); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/shader_function.h b/engine/src/flutter/impeller/impeller/compositor/shader_function.h index 2adaf90b90..c78601673d 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shader_function.h +++ b/engine/src/flutter/impeller/impeller/compositor/shader_function.h @@ -21,19 +21,24 @@ class ShaderFunction : public Comparable { id GetMTLFunction() const; - // Comparable + // |Comparable| std::size_t GetHash() const override; - // Comparable + // |Comparable| bool IsEqual(const ShaderFunction& other) const override; private: friend class ShaderLibrary; + UniqueID parent_library_id_; id function_ = nullptr; + std::string name_; ShaderStage stage_; - ShaderFunction(id function, ShaderStage stage); + ShaderFunction(UniqueID parent_library_id, + id function, + std::string name, + ShaderStage stage); FML_DISALLOW_COPY_AND_ASSIGN(ShaderFunction); }; diff --git a/engine/src/flutter/impeller/impeller/compositor/shader_function.mm b/engine/src/flutter/impeller/impeller/compositor/shader_function.mm index 1a2653aaeb..58c9d65d87 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shader_function.mm +++ b/engine/src/flutter/impeller/impeller/compositor/shader_function.mm @@ -6,8 +6,14 @@ namespace impeller { -ShaderFunction::ShaderFunction(id function, ShaderStage stage) - : function_(function), stage_(stage) {} +ShaderFunction::ShaderFunction(UniqueID parent_library_id, + id function, + std::string name, + ShaderStage stage) + : parent_library_id_(parent_library_id), + function_(function), + name_(std::move(name)), + stage_(stage) {} ShaderFunction::~ShaderFunction() = default; @@ -19,4 +25,15 @@ ShaderStage ShaderFunction::GetStage() const { return stage_; } +// |Comparable| +std::size_t ShaderFunction::GetHash() const { + return fml::HashCombine(parent_library_id_, name_, stage_); +} + +// |Comparable| +bool ShaderFunction::IsEqual(const ShaderFunction& other) const { + return parent_library_id_ == other.parent_library_id_ && + name_ == other.name_ && stage_ == other.stage_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/impeller/compositor/shader_library.h b/engine/src/flutter/impeller/impeller/compositor/shader_library.h index 06595e786d..4a9b76c4e1 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shader_library.h +++ b/engine/src/flutter/impeller/impeller/compositor/shader_library.h @@ -12,6 +12,7 @@ #include #include "flutter/fml/macros.h" +#include "impeller/compositor/comparable.h" #include "impeller/compositor/shader_function.h" namespace impeller { @@ -50,11 +51,13 @@ class ShaderLibrary { }; }; - id library_ = nullptr; using Functions = std::unordered_map, ShaderKey::Hash, ShaderKey::Equal>; + + UniqueID library_id_; + id library_ = nullptr; Functions functions_; ShaderLibrary(id library); diff --git a/engine/src/flutter/impeller/impeller/compositor/shader_library.mm b/engine/src/flutter/impeller/impeller/compositor/shader_library.mm index 852bb88bf0..a7009934bd 100644 --- a/engine/src/flutter/impeller/impeller/compositor/shader_library.mm +++ b/engine/src/flutter/impeller/impeller/compositor/shader_library.mm @@ -24,8 +24,8 @@ std::shared_ptr ShaderLibrary::GetFunction( return nullptr; } - auto func = - std::shared_ptr(new ShaderFunction(function, stage)); + auto func = std::shared_ptr(new ShaderFunction( + library_id_, function, {name.data(), name.size()}, stage)); functions_[key] = func; return func; } diff --git a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h index 641886e4f4..7be7c10819 100644 --- a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h +++ b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.h @@ -15,10 +15,10 @@ class VertexDescriptor : public Comparable { virtual ~VertexDescriptor(); - // Comparable + //| Comparable| std::size_t GetHash() const override; - // Comparable + // |Comparable| bool IsEqual(const VertexDescriptor& other) const override; private: diff --git a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm index f146a9265c..9758e23518 100644 --- a/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm +++ b/engine/src/flutter/impeller/impeller/compositor/vertex_descriptor.mm @@ -4,10 +4,24 @@ #include "impeller/compositor/vertex_descriptor.h" +#include "flutter/fml/logging.h" + namespace impeller { VertexDescriptor::VertexDescriptor() = default; VertexDescriptor::~VertexDescriptor() = default; +//| Comparable| +std::size_t VertexDescriptor::GetHash() const { + FML_CHECK(false); + return 0; +} + +// |Comparable| +bool VertexDescriptor::IsEqual(const VertexDescriptor& other) const { + FML_CHECK(false); + return false; +} + } // namespace impeller