diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 14d9b8d5a7..b6ee69e3e5 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -127,7 +127,7 @@ TEST_F(AiksTest, CanRenderGroupOpacity) { canvas.Restore(); - // ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } TEST_F(AiksTest, CanPerformFullScreenMSAA) { @@ -138,7 +138,7 @@ TEST_F(AiksTest, CanPerformFullScreenMSAA) { canvas.DrawCircle({250, 250}, 125, red); - ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); + // ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } } // namespace testing diff --git a/engine/src/flutter/impeller/entity/content_renderer.cc b/engine/src/flutter/impeller/entity/content_renderer.cc index 6fba04b83b..d22e81f104 100644 --- a/engine/src/flutter/impeller/entity/content_renderer.cc +++ b/engine/src/flutter/impeller/entity/content_renderer.cc @@ -13,13 +13,15 @@ ContentRenderer::ContentRenderer(std::shared_ptr context) } // Pipelines whose default descriptors work fine for the entity framework. - gradient_fill_pipeline_ = std::make_unique(*context_); - solid_fill_pipeline_ = std::make_unique(*context_); - texture_pipeline_ = std::make_unique(*context_); - solid_stroke_pipeline_ = std::make_unique(*context_); + gradient_fill_pipelines_[{}] = + std::make_unique(*context_); + solid_fill_pipelines_[{}] = std::make_unique(*context_); + texture_pipelines_[{}] = std::make_unique(*context_); + solid_stroke_pipelines_[{}] = + std::make_unique(*context_); // Pipelines that are variants of the base pipelines with custom descriptors. - if (auto solid_fill_pipeline = solid_fill_pipeline_->WaitAndGet()) { + if (auto solid_fill_pipeline = solid_fill_pipelines_[{}]->WaitAndGet()) { auto clip_pipeline_descriptor = solid_fill_pipeline->GetDescriptor(); // Write to the stencil buffer. StencilAttachmentDescriptor stencil0; @@ -35,7 +37,7 @@ ContentRenderer::ContentRenderer(std::shared_ptr context) } clip_pipeline_descriptor.SetColorAttachmentDescriptors( std::move(color_attachments)); - clip_pipeline_ = std::make_unique( + clip_pipelines_[{}] = std::make_unique( *context_, std::move(clip_pipeline_descriptor)); } else { return; @@ -54,43 +56,4 @@ std::shared_ptr ContentRenderer::GetContext() const { return context_; } -std::shared_ptr ContentRenderer::GetGradientFillPipeline() const { - if (!IsValid()) { - return nullptr; - } - return gradient_fill_pipeline_->WaitAndGet(); -} - -std::shared_ptr ContentRenderer::GetSolidFillPipeline() const { - if (!IsValid()) { - return nullptr; - } - - return solid_fill_pipeline_->WaitAndGet(); -} - -std::shared_ptr ContentRenderer::GetTexturePipeline() const { - if (!IsValid()) { - return nullptr; - } - - return texture_pipeline_->WaitAndGet(); -} - -std::shared_ptr ContentRenderer::GetSolidStrokePipeline() const { - if (!IsValid()) { - return nullptr; - } - - return solid_stroke_pipeline_->WaitAndGet(); -} - -std::shared_ptr ContentRenderer::GetClipPipeline() const { - if (!IsValid()) { - return nullptr; - } - - return clip_pipeline_->WaitAndGet(); -} - } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/content_renderer.h b/engine/src/flutter/impeller/entity/content_renderer.h index 1172d17f18..19edbb8167 100644 --- a/engine/src/flutter/impeller/entity/content_renderer.h +++ b/engine/src/flutter/impeller/entity/content_renderer.h @@ -5,7 +5,9 @@ #pragma once #include +#include +#include "flutter/fml/hash_combine.h" #include "flutter/fml/macros.h" #include "flutter/impeller/entity/gradient_fill.frag.h" #include "flutter/impeller/entity/gradient_fill.vert.h" @@ -33,31 +35,99 @@ using ClipPipeline = PipelineT; class ContentRenderer { public: + struct Options { + SampleCount sample_count = SampleCount::kCount1; + + Options() {} + + struct Hash { + constexpr std::size_t operator()(const Options& o) const { + return fml::HashCombine(o.sample_count); + } + }; + + struct Equal { + constexpr bool operator()(const Options& lhs, const Options& rhs) const { + return lhs.sample_count == rhs.sample_count; + } + }; + }; + ContentRenderer(std::shared_ptr context); ~ContentRenderer(); bool IsValid() const; - std::shared_ptr GetGradientFillPipeline() const; + std::shared_ptr GetGradientFillPipeline(Options opts) const { + return GetPipeline(gradient_fill_pipelines_, opts); + } - std::shared_ptr GetSolidFillPipeline() const; + std::shared_ptr GetSolidFillPipeline(Options opts) const { + return GetPipeline(solid_fill_pipelines_, opts); + } - std::shared_ptr GetTexturePipeline() const; + std::shared_ptr GetTexturePipeline(Options opts) const { + return GetPipeline(texture_pipelines_, opts); + } - std::shared_ptr GetSolidStrokePipeline() const; + std::shared_ptr GetSolidStrokePipeline(Options opts) const { + return GetPipeline(solid_stroke_pipelines_, opts); + } - std::shared_ptr GetClipPipeline() const; + std::shared_ptr GetClipPipeline(Options opts) const { + return GetPipeline(clip_pipelines_, opts); + } std::shared_ptr GetContext() const; private: std::shared_ptr context_; - std::unique_ptr gradient_fill_pipeline_; - std::unique_ptr solid_fill_pipeline_; - std::unique_ptr texture_pipeline_; - std::unique_ptr solid_stroke_pipeline_; - std::unique_ptr clip_pipeline_; + + template + using Variants = std:: + unordered_map, Options::Hash, Options::Equal>; + + // These are mutable because while the prototypes are created eagerly, any + // variants requested from that are lazily created and cached in the variants + // map. + mutable Variants gradient_fill_pipelines_; + mutable Variants solid_fill_pipelines_; + mutable Variants texture_pipelines_; + mutable Variants solid_stroke_pipelines_; + mutable Variants clip_pipelines_; + + static void ApplyOptionsToDescriptor(PipelineDescriptor& desc, + const Options& options) { + desc.SetSampleCount(options.sample_count); + } + + template + std::shared_ptr GetPipeline(Variants& container, + Options opts) const { + if (!IsValid()) { + return nullptr; + } + + if (auto found = container.find(opts); found != container.end()) { + return found->second->WaitAndGet(); + } + + auto found = container.find({}); + + // The prototype must always be initialized in the constructor. + FML_CHECK(found != container.end()); + + auto variant_future = found->second->WaitAndGet()->CreateVariant( + [&opts](PipelineDescriptor& desc) { + ApplyOptionsToDescriptor(desc, opts); + }); + auto variant = std::make_unique(std::move(variant_future)); + auto variant_pipeline = variant->WaitAndGet(); + container[opts] = std::move(variant); + return variant_pipeline; + } + bool is_valid_ = false; FML_DISALLOW_COPY_AND_ASSIGN(ContentRenderer); diff --git a/engine/src/flutter/impeller/entity/contents.cc b/engine/src/flutter/impeller/entity/contents.cc index 48be6459c9..7de2f68d6e 100644 --- a/engine/src/flutter/impeller/entity/contents.cc +++ b/engine/src/flutter/impeller/entity/contents.cc @@ -19,6 +19,12 @@ namespace impeller { +static ContentRenderer::Options OptionsFromPass(const RenderPass& pass) { + ContentRenderer::Options opts; + opts.sample_count = pass.GetRenderTarget().GetSampleCount(); + return opts; +} + /******************************************************************************* ******* Contents ******************************************************************************/ @@ -85,7 +91,7 @@ bool LinearGradientContents::Render(const ContentRenderer& renderer, Command cmd; cmd.label = "LinearGradientFill"; - cmd.pipeline = renderer.GetGradientFillPipeline(); + cmd.pipeline = renderer.GetGradientFillPipeline(OptionsFromPass(pass)); cmd.stencil_reference = entity.GetStencilDepth(); cmd.BindVertices( vertices_builder.CreateVertexBuffer(pass.GetTransientsBuffer())); @@ -142,7 +148,7 @@ bool SolidColorContents::Render(const ContentRenderer& renderer, Command cmd; cmd.label = "SolidFill"; - cmd.pipeline = renderer.GetSolidFillPipeline(); + cmd.pipeline = renderer.GetSolidFillPipeline(OptionsFromPass(pass)); cmd.stencil_reference = entity.GetStencilDepth(); cmd.BindVertices( CreateSolidFillVertices(entity.GetPath(), pass.GetTransientsBuffer())); @@ -241,7 +247,7 @@ bool TextureContents::Render(const ContentRenderer& renderer, Command cmd; cmd.label = "TextureFill"; - cmd.pipeline = renderer.GetTexturePipeline(); + cmd.pipeline = renderer.GetTexturePipeline(OptionsFromPass(pass)); cmd.stencil_reference = entity.GetStencilDepth(); cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_buffer)); VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); @@ -336,7 +342,7 @@ bool SolidStrokeContents::Render(const ContentRenderer& renderer, Command cmd; cmd.primitive_type = PrimitiveType::kTriangleStrip; cmd.label = "SolidStroke"; - cmd.pipeline = renderer.GetSolidStrokePipeline(); + cmd.pipeline = renderer.GetSolidStrokePipeline(OptionsFromPass(pass)); cmd.stencil_reference = entity.GetStencilDepth(); cmd.BindVertices( CreateSolidStrokeVertices(entity.GetPath(), pass.GetTransientsBuffer())); @@ -372,7 +378,7 @@ bool ClipContents::Render(const ContentRenderer& renderer, Command cmd; cmd.label = "Clip"; - cmd.pipeline = renderer.GetClipPipeline(); + cmd.pipeline = renderer.GetClipPipeline(OptionsFromPass(pass)); cmd.stencil_reference = entity.GetStencilDepth() + 1u; cmd.BindVertices( CreateSolidFillVertices(entity.GetPath(), pass.GetTransientsBuffer())); diff --git a/engine/src/flutter/impeller/renderer/pipeline.h b/engine/src/flutter/impeller/renderer/pipeline.h index 2d6760cc0d..69182785fb 100644 --- a/engine/src/flutter/impeller/renderer/pipeline.h +++ b/engine/src/flutter/impeller/renderer/pipeline.h @@ -15,6 +15,12 @@ namespace impeller { class PipelineLibrary; class Pipeline; +// TODO(csg): Using a simple future is sub-optimal since callers that want to +// eagerly create and cache pipeline variants will have to await on the future +// to get its pipeline descriptor (unless they have explicitly cached it). This +// would be a concurrency pessimization. +// +// Use a struct that stores the future and the descriptor separately. using PipelineFuture = std::future>; //------------------------------------------------------------------------------ @@ -76,6 +82,9 @@ class PipelineT { std::optional desc) : pipeline_future_(CreatePipelineFuture(context, desc)) {} + explicit PipelineT(PipelineFuture future) + : pipeline_future_(std::move(future)) {} + std::shared_ptr WaitAndGet() { if (did_wait_) { return pipeline_;