Allow for the on-demand creation and caching of pipeline variants from the prototype.

This commit is contained in:
Chinmay Garde
2021-12-01 13:03:43 -08:00
committed by Dan Field
parent 5006c2a019
commit f14398821f
5 changed files with 110 additions and 62 deletions

View File

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

View File

@@ -13,13 +13,15 @@ ContentRenderer::ContentRenderer(std::shared_ptr<Context> context)
}
// Pipelines whose default descriptors work fine for the entity framework.
gradient_fill_pipeline_ = std::make_unique<GradientFillPipeline>(*context_);
solid_fill_pipeline_ = std::make_unique<SolidFillPipeline>(*context_);
texture_pipeline_ = std::make_unique<TexturePipeline>(*context_);
solid_stroke_pipeline_ = std::make_unique<SolidStrokePipeline>(*context_);
gradient_fill_pipelines_[{}] =
std::make_unique<GradientFillPipeline>(*context_);
solid_fill_pipelines_[{}] = std::make_unique<SolidFillPipeline>(*context_);
texture_pipelines_[{}] = std::make_unique<TexturePipeline>(*context_);
solid_stroke_pipelines_[{}] =
std::make_unique<SolidStrokePipeline>(*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> context)
}
clip_pipeline_descriptor.SetColorAttachmentDescriptors(
std::move(color_attachments));
clip_pipeline_ = std::make_unique<ClipPipeline>(
clip_pipelines_[{}] = std::make_unique<ClipPipeline>(
*context_, std::move(clip_pipeline_descriptor));
} else {
return;
@@ -54,43 +56,4 @@ std::shared_ptr<Context> ContentRenderer::GetContext() const {
return context_;
}
std::shared_ptr<Pipeline> ContentRenderer::GetGradientFillPipeline() const {
if (!IsValid()) {
return nullptr;
}
return gradient_fill_pipeline_->WaitAndGet();
}
std::shared_ptr<Pipeline> ContentRenderer::GetSolidFillPipeline() const {
if (!IsValid()) {
return nullptr;
}
return solid_fill_pipeline_->WaitAndGet();
}
std::shared_ptr<Pipeline> ContentRenderer::GetTexturePipeline() const {
if (!IsValid()) {
return nullptr;
}
return texture_pipeline_->WaitAndGet();
}
std::shared_ptr<Pipeline> ContentRenderer::GetSolidStrokePipeline() const {
if (!IsValid()) {
return nullptr;
}
return solid_stroke_pipeline_->WaitAndGet();
}
std::shared_ptr<Pipeline> ContentRenderer::GetClipPipeline() const {
if (!IsValid()) {
return nullptr;
}
return clip_pipeline_->WaitAndGet();
}
} // namespace impeller

View File

@@ -5,7 +5,9 @@
#pragma once
#include <memory>
#include <unordered_map>
#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<SolidFillVertexShader, SolidFillFragmentShader>;
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> context);
~ContentRenderer();
bool IsValid() const;
std::shared_ptr<Pipeline> GetGradientFillPipeline() const;
std::shared_ptr<Pipeline> GetGradientFillPipeline(Options opts) const {
return GetPipeline(gradient_fill_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetSolidFillPipeline() const;
std::shared_ptr<Pipeline> GetSolidFillPipeline(Options opts) const {
return GetPipeline(solid_fill_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetTexturePipeline() const;
std::shared_ptr<Pipeline> GetTexturePipeline(Options opts) const {
return GetPipeline(texture_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetSolidStrokePipeline() const;
std::shared_ptr<Pipeline> GetSolidStrokePipeline(Options opts) const {
return GetPipeline(solid_stroke_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetClipPipeline() const;
std::shared_ptr<Pipeline> GetClipPipeline(Options opts) const {
return GetPipeline(clip_pipelines_, opts);
}
std::shared_ptr<Context> GetContext() const;
private:
std::shared_ptr<Context> context_;
std::unique_ptr<GradientFillPipeline> gradient_fill_pipeline_;
std::unique_ptr<SolidFillPipeline> solid_fill_pipeline_;
std::unique_ptr<TexturePipeline> texture_pipeline_;
std::unique_ptr<SolidStrokePipeline> solid_stroke_pipeline_;
std::unique_ptr<ClipPipeline> clip_pipeline_;
template <class T>
using Variants = std::
unordered_map<Options, std::unique_ptr<T>, 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<GradientFillPipeline> gradient_fill_pipelines_;
mutable Variants<SolidFillPipeline> solid_fill_pipelines_;
mutable Variants<TexturePipeline> texture_pipelines_;
mutable Variants<SolidStrokePipeline> solid_stroke_pipelines_;
mutable Variants<ClipPipeline> clip_pipelines_;
static void ApplyOptionsToDescriptor(PipelineDescriptor& desc,
const Options& options) {
desc.SetSampleCount(options.sample_count);
}
template <class TypedPipeline>
std::shared_ptr<Pipeline> GetPipeline(Variants<TypedPipeline>& 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<TypedPipeline>(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);

View File

@@ -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()));

View File

@@ -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<std::shared_ptr<Pipeline>>;
//------------------------------------------------------------------------------
@@ -76,6 +82,9 @@ class PipelineT {
std::optional<PipelineDescriptor> desc)
: pipeline_future_(CreatePipelineFuture(context, desc)) {}
explicit PipelineT(PipelineFuture future)
: pipeline_future_(std::move(future)) {}
std::shared_ptr<Pipeline> WaitAndGet() {
if (did_wait_) {
return pipeline_;