|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "impeller/renderer/context.h" |
| 8 | +#include "impeller/renderer/pipeline_descriptor.h" |
| 9 | +#include "impeller/scene/shaders/geometry.vert.h" |
| 10 | +#include "impeller/scene/shaders/unlit.frag.h" |
| 11 | + |
| 12 | +namespace impeller { |
| 13 | +namespace scene { |
| 14 | + |
| 15 | +using UnlitPipeline = |
| 16 | + RenderPipelineT<GeometryVertexShader, UnlitFragmentShader>; |
| 17 | + |
| 18 | +struct SceneContextOptions { |
| 19 | + SampleCount sample_count = SampleCount::kCount1; |
| 20 | + PrimitiveType primitive_type = PrimitiveType::kTriangle; |
| 21 | + |
| 22 | + struct Hash { |
| 23 | + constexpr std::size_t operator()(const SceneContextOptions& o) const { |
| 24 | + return fml::HashCombine(o.sample_count, o.primitive_type); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + struct Equal { |
| 29 | + constexpr bool operator()(const SceneContextOptions& lhs, |
| 30 | + const SceneContextOptions& rhs) const { |
| 31 | + return lhs.sample_count == rhs.sample_count && |
| 32 | + lhs.primitive_type == rhs.primitive_type; |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + void ApplyToPipelineDescriptor(PipelineDescriptor& desc) const; |
| 37 | +}; |
| 38 | + |
| 39 | +class SceneContext { |
| 40 | + public: |
| 41 | + explicit SceneContext(std::shared_ptr<Context> context); |
| 42 | + |
| 43 | + ~SceneContext(); |
| 44 | + |
| 45 | + bool IsValid() const; |
| 46 | + |
| 47 | + std::shared_ptr<Context> GetContext() const; |
| 48 | + |
| 49 | + std::shared_ptr<Pipeline<PipelineDescriptor>> GetUnlitPipeline( |
| 50 | + SceneContextOptions opts) const { |
| 51 | + return GetPipeline(unlit_pipeline_, opts); |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + std::shared_ptr<Context> context_; |
| 56 | + |
| 57 | + template <class T> |
| 58 | + using Variants = std::unordered_map<SceneContextOptions, |
| 59 | + std::unique_ptr<T>, |
| 60 | + SceneContextOptions::Hash, |
| 61 | + SceneContextOptions::Equal>; |
| 62 | + |
| 63 | + mutable Variants<UnlitPipeline> unlit_pipeline_; |
| 64 | + |
| 65 | + template <class TypedPipeline> |
| 66 | + std::shared_ptr<Pipeline<PipelineDescriptor>> GetPipeline( |
| 67 | + Variants<TypedPipeline>& container, |
| 68 | + SceneContextOptions opts) const { |
| 69 | + if (!IsValid()) { |
| 70 | + return nullptr; |
| 71 | + } |
| 72 | + |
| 73 | + if (auto found = container.find(opts); found != container.end()) { |
| 74 | + return found->second->WaitAndGet(); |
| 75 | + } |
| 76 | + |
| 77 | + auto prototype = container.find({}); |
| 78 | + |
| 79 | + // The prototype must always be initialized in the constructor. |
| 80 | + FML_CHECK(prototype != container.end()); |
| 81 | + |
| 82 | + auto variant_future = prototype->second->WaitAndGet()->CreateVariant( |
| 83 | + [&opts, variants_count = container.size()](PipelineDescriptor& desc) { |
| 84 | + opts.ApplyToPipelineDescriptor(desc); |
| 85 | + desc.SetLabel( |
| 86 | + SPrintF("%s V#%zu", desc.GetLabel().c_str(), variants_count)); |
| 87 | + }); |
| 88 | + auto variant = std::make_unique<TypedPipeline>(std::move(variant_future)); |
| 89 | + auto variant_pipeline = variant->WaitAndGet(); |
| 90 | + container[opts] = std::move(variant); |
| 91 | + return variant_pipeline; |
| 92 | + } |
| 93 | + |
| 94 | + bool is_valid_ = false; |
| 95 | + |
| 96 | + FML_DISALLOW_COPY_AND_ASSIGN(SceneContext); |
| 97 | +}; |
| 98 | + |
| 99 | +} // namespace scene |
| 100 | +} // namespace impeller |
0 commit comments