Flutter Impeller
content_context.h
Go to the documentation of this file.
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 #ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_CONTENT_CONTEXT_H_
6 #define FLUTTER_IMPELLER_ENTITY_CONTENTS_CONTENT_CONTEXT_H_
7 
8 #include <initializer_list>
9 #include <memory>
10 #include <optional>
11 #include <unordered_map>
12 #include <utility>
13 
14 #include "flutter/fml/logging.h"
15 #include "flutter/fml/status_or.h"
17 #include "impeller/core/formats.h"
28 
29 namespace impeller {
30 /// Pipeline state configuration.
31 ///
32 /// Each unique combination of these options requires a different pipeline state
33 /// object to be built. This struct is used as a key for the per-pipeline
34 /// variant cache.
35 ///
36 /// When adding fields to this key, reliant features should take care to limit
37 /// the combinatorical explosion of variations. A sufficiently complicated
38 /// Flutter application may easily require building hundreds of PSOs in total,
39 /// but they shouldn't require e.g. 10s of thousands.
41  enum class StencilMode : uint8_t {
42  /// Turn the stencil test off. Used when drawing without stencil-then-cover.
43  kIgnore,
44 
45  // Operations used for stencil-then-cover.
46 
47  /// Draw the stencil for the NonZero fill path rule.
48  ///
49  /// The stencil ref should always be 0 on commands using this mode.
51  /// Draw the stencil for the EvenOdd fill path rule.
52  ///
53  /// The stencil ref should always be 0 on commands using this mode.
55  /// Draw a stencil which always increments once for everything in the vertex
56  /// coverage regardless of triangle overlap.
57  ///
58  /// The stencil ref should always be 0 on commands using this mode.
60  /// Used for draw calls which fill in the stenciled area. Intended to be
61  /// used after `kStencilNonZeroFill` or `kStencilEvenOddFill` is used to set
62  /// up the stencil buffer. Also cleans up the stencil buffer by resetting
63  /// everything to zero.
64  ///
65  /// The stencil ref should always be 0 on commands using this mode.
67  /// The opposite of `kCoverCompare`. Used for draw calls which fill in the
68  /// non-stenciled area (intersection clips). Intended to be used after
69  /// `kStencilNonZeroFill` or `kStencilEvenOddFill` is used to set up the
70  /// stencil buffer. Also cleans up the stencil buffer by resetting
71  /// everything to zero.
72  ///
73  /// The stencil ref should always be 0 on commands using this mode.
75  };
76 
84  bool depth_write_enabled = false;
86 
87  constexpr uint64_t ToKey() const {
88  static_assert(sizeof(sample_count) == 1);
89  static_assert(sizeof(blend_mode) == 1);
90  static_assert(sizeof(sample_count) == 1);
91  static_assert(sizeof(depth_compare) == 1);
92  static_assert(sizeof(stencil_mode) == 1);
93  static_assert(sizeof(primitive_type) == 1);
94  static_assert(sizeof(color_attachment_pixel_format) == 1);
95 
96  return (is_for_rrect_blur_clear ? 1llu : 0llu) << 0 |
97  (0) << 1 | // // Unused, previously wireframe.
98  (has_depth_stencil_attachments ? 1llu : 0llu) << 2 |
99  (depth_write_enabled ? 1llu : 0llu) << 3 |
100  // enums
101  static_cast<uint64_t>(color_attachment_pixel_format) << 8 |
102  static_cast<uint64_t>(primitive_type) << 16 |
103  static_cast<uint64_t>(stencil_mode) << 24 |
104  static_cast<uint64_t>(depth_compare) << 32 |
105  static_cast<uint64_t>(blend_mode) << 40 |
106  static_cast<uint64_t>(sample_count) << 48;
107  }
108 
110 };
111 
117 };
118 
119 class Tessellator;
120 class RenderTargetCache;
121 
123  public:
124  explicit ContentContext(
125  std::shared_ptr<Context> context,
126  std::shared_ptr<TypographerContext> typographer_context,
127  std::shared_ptr<RenderTargetAllocator> render_target_allocator = nullptr);
128 
130 
131  bool IsValid() const;
132 
133  Tessellator& GetTessellator() const;
134 
135  // clang-format off
217 #ifdef IMPELLER_ENABLE_OPENGLES
218 #if !defined(FML_OS_EMSCRIPTEN)
219  PipelineRef GetTiledTextureExternalPipeline(ContentContextOptions opts) const;
220  PipelineRef GetTiledTextureUvExternalPipeline(ContentContextOptions opts) const;
221 #endif
222  PipelineRef GetDownsampleTextureGlesPipeline(ContentContextOptions opts) const;
223 #endif // IMPELLER_ENABLE_OPENGLES
224  // clang-format on
225 
226  // An empty 1x1 texture for binding drawVertices/drawAtlas or other cases
227  // that don't always have a texture (due to blending).
228  std::shared_ptr<Texture> GetEmptyTexture() const;
229 
230  std::shared_ptr<Context> GetContext() const;
231 
232  const Capabilities& GetDeviceCapabilities() const;
233 
235  std::function<bool(const ContentContext&, RenderPass&)>;
236 
237  /// @brief Creates a new texture of size `texture_size` and calls
238  /// `subpass_callback` with a `RenderPass` for drawing to the texture.
239  fml::StatusOr<RenderTarget> MakeSubpass(
240  std::string_view label,
241  ISize texture_size,
242  const std::shared_ptr<CommandBuffer>& command_buffer,
243  const SubpassCallback& subpass_callback,
244  bool msaa_enabled = true,
245  bool depth_stencil_enabled = false,
246  int32_t mip_count = 1) const;
247 
248  /// Makes a subpass that will render to `subpass_target`.
249  fml::StatusOr<RenderTarget> MakeSubpass(
250  std::string_view label,
251  const RenderTarget& subpass_target,
252  const std::shared_ptr<CommandBuffer>& command_buffer,
253  const SubpassCallback& subpass_callback) const;
254 
255  const std::shared_ptr<LazyGlyphAtlas>& GetLazyGlyphAtlas() const {
256  return lazy_glyph_atlas_;
257  }
258 
259  const std::shared_ptr<RenderTargetAllocator>& GetRenderTargetCache() const {
260  return render_target_cache_;
261  }
262 
263  /// RuntimeEffect pipelines must be obtained via this method to avoid
264  /// re-creating them every frame.
265  ///
266  /// The unique_entrypoint_name comes from RuntimeEffect::GetEntrypoint.
267  /// Impellerc generates a unique entrypoint name for runtime effect shaders
268  /// based on the input file name and shader stage.
269  ///
270  /// The create_callback is synchronously invoked exactly once if a cached
271  /// pipeline is not found.
273  const std::string& unique_entrypoint_name,
274  const ContentContextOptions& options,
275  const std::function<std::shared_ptr<Pipeline<PipelineDescriptor>>()>&
276  create_callback) const;
277 
278  /// Used by hot reload/hot restart to clear a cached pipeline from
279  /// GetCachedRuntimeEffectPipeline.
281  const std::string& unique_entrypoint_name) const;
282 
283  /// @brief Retrieve the current host buffer for transient storage of indexes
284  /// used for indexed draws.
285  ///
286  /// This may or may not return the same value as `GetTransientsDataBuffer`
287  /// depending on the backend.
288  ///
289  /// This is only safe to use from the raster threads. Other threads should
290  /// allocate their own device buffers.
292  return *indexes_host_buffer_;
293  }
294 
295  /// @brief Retrieve the current host buffer for transient storage of other
296  /// non-index data.
297  ///
298  /// This is only safe to use from the raster threads. Other threads should
299  /// allocate their own device buffers.
300  HostBuffer& GetTransientsDataBuffer() const { return *data_host_buffer_; }
301 
302  /// @brief Resets the transients buffers held onto by the content context.
303  void ResetTransientsBuffers();
304 
305  TextShadowCache& GetTextShadowCache() const { return *text_shadow_cache_; }
306 
307  protected:
308  // Visible for testing.
309  void SetTransientsIndexesBuffer(std::shared_ptr<HostBuffer> host_buffer) {
310  indexes_host_buffer_ = std::move(host_buffer);
311  }
312 
313  // Visible for testing.
314  void SetTransientsDataBuffer(std::shared_ptr<HostBuffer> host_buffer) {
315  data_host_buffer_ = std::move(host_buffer);
316  }
317 
318  private:
319  std::shared_ptr<Context> context_;
320  std::shared_ptr<LazyGlyphAtlas> lazy_glyph_atlas_;
321 
322  /// Run backend specific additional setup and create common shader variants.
323  ///
324  /// This bootstrap is intended to improve the performance of several
325  /// first frame benchmarks that are tracked in the flutter device lab.
326  /// The workload includes initializing commonly used but not default
327  /// shader variants, as well as forcing driver initialization.
328  void InitializeCommonlyUsedShadersIfNeeded() const;
329 
330  struct RuntimeEffectPipelineKey {
331  std::string unique_entrypoint_name;
332  ContentContextOptions options;
333 
334  struct Hash {
335  std::size_t operator()(const RuntimeEffectPipelineKey& key) const {
336  return fml::HashCombine(key.unique_entrypoint_name,
337  key.options.ToKey());
338  }
339  };
340 
341  struct Equal {
342  inline bool operator()(const RuntimeEffectPipelineKey& lhs,
343  const RuntimeEffectPipelineKey& rhs) const {
344  return lhs.unique_entrypoint_name == rhs.unique_entrypoint_name &&
345  lhs.options.ToKey() == rhs.options.ToKey();
346  }
347  };
348  };
349 
350  mutable std::unordered_map<RuntimeEffectPipelineKey,
351  std::shared_ptr<Pipeline<PipelineDescriptor>>,
352  RuntimeEffectPipelineKey::Hash,
353  RuntimeEffectPipelineKey::Equal>
354  runtime_effect_pipelines_;
355 
356  struct Pipelines;
357  std::unique_ptr<Pipelines> pipelines_;
358 
359  bool is_valid_ = false;
360  std::shared_ptr<Tessellator> tessellator_;
361  std::shared_ptr<RenderTargetAllocator> render_target_cache_;
362  std::shared_ptr<HostBuffer> data_host_buffer_;
363  std::shared_ptr<HostBuffer> indexes_host_buffer_;
364  std::shared_ptr<Texture> empty_texture_;
365  std::unique_ptr<TextShadowCache> text_shadow_cache_;
366 
367  ContentContext(const ContentContext&) = delete;
368 
369  ContentContext& operator=(const ContentContext&) = delete;
370 };
371 
372 } // namespace impeller
373 
374 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_CONTENT_CONTEXT_H_
PipelineRef GetBlendLuminosityPipeline(ContentContextOptions opts) const
PipelineRef GetTiledTexturePipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendColorPipeline(ContentContextOptions opts) const
PipelineRef GetDownsamplePipeline(ContentContextOptions opts) const
PipelineRef GetSourceInBlendPipeline(ContentContextOptions opts) const
void ClearCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name) const
PipelineRef GetLinearGradientFillPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendOverlayPipeline(ContentContextOptions opts) const
PipelineRef GetBlendColorDodgePipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendColorBurnPipeline(ContentContextOptions opts) const
PipelineRef GetPorterDuffPipeline(BlendMode mode, ContentContextOptions opts) const
std::shared_ptr< Texture > GetEmptyTexture() const
PipelineRef GetSourceOutBlendPipeline(ContentContextOptions opts) const
void SetTransientsDataBuffer(std::shared_ptr< HostBuffer > host_buffer)
PipelineRef GetScreenBlendPipeline(ContentContextOptions opts) const
PipelineRef GetBlendColorPipeline(ContentContextOptions opts) const
PipelineRef GetLinePipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendLuminosityPipeline(ContentContextOptions opts) const
PipelineRef GetDownsampleBoundedPipeline(ContentContextOptions opts) const
PipelineRef GetPlusBlendPipeline(ContentContextOptions opts) const
PipelineRef GetUberSDFPipeline(ContentContextOptions opts) const
PipelineRef GetFastGradientPipeline(ContentContextOptions opts) const
const std::shared_ptr< LazyGlyphAtlas > & GetLazyGlyphAtlas() const
ContentContext(std::shared_ptr< Context > context, std::shared_ptr< TypographerContext > typographer_context, std::shared_ptr< RenderTargetAllocator > render_target_allocator=nullptr)
void ResetTransientsBuffers()
Resets the transients buffers held onto by the content context.
PipelineRef GetSolidFillPipeline(ContentContextOptions opts) const
fml::StatusOr< RenderTarget > MakeSubpass(std::string_view label, ISize texture_size, const std::shared_ptr< CommandBuffer > &command_buffer, const SubpassCallback &subpass_callback, bool msaa_enabled=true, bool depth_stencil_enabled=false, int32_t mip_count=1) const
Creates a new texture of size texture_size and calls subpass_callback with a RenderPass for drawing t...
const Capabilities & GetDeviceCapabilities() const
PipelineRef GetModulateBlendPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendHardLightPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendColorDodgePipeline(ContentContextOptions opts) const
PipelineRef GetSweepGradientUniformFillPipeline(ContentContextOptions opts) const
PipelineRef GetBlendSoftLightPipeline(ContentContextOptions opts) const
PipelineRef GetDestinationATopBlendPipeline(ContentContextOptions opts) const
PipelineRef GetTextureStrictSrcPipeline(ContentContextOptions opts) const
PipelineRef GetDrawShadowVerticesPipeline(ContentContextOptions opts) const
PipelineRef GetCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name, const ContentContextOptions &options, const std::function< std::shared_ptr< Pipeline< PipelineDescriptor >>()> &create_callback) const
PipelineRef GetRadialGradientSSBOFillPipeline(ContentContextOptions opts) const
PipelineRef GetBlendColorBurnPipeline(ContentContextOptions opts) const
PipelineRef GetSweepGradientSSBOFillPipeline(ContentContextOptions opts) const
PipelineRef GetLinearGradientUniformFillPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendScreenPipeline(ContentContextOptions opts) const
PipelineRef GetLinearGradientSSBOFillPipeline(ContentContextOptions opts) const
PipelineRef GetRadialGradientFillPipeline(ContentContextOptions opts) const
PipelineRef GetTexturePipeline(ContentContextOptions opts) const
PipelineRef GetBlendHardLightPipeline(ContentContextOptions opts) const
PipelineRef GetClearBlendPipeline(ContentContextOptions opts) const
PipelineRef GetCirclePipeline(ContentContextOptions opts) const
PipelineRef GetConicalGradientUniformFillPipeline(ContentContextOptions opts, ConicalKind kind) const
PipelineRef GetRadialGradientUniformFillPipeline(ContentContextOptions opts) const
PipelineRef GetSweepGradientFillPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendExclusionPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendDarkenPipeline(ContentContextOptions opts) const
PipelineRef GetBlendSaturationPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendSoftLightPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendLightenPipeline(ContentContextOptions opts) const
PipelineRef GetMorphologyFilterPipeline(ContentContextOptions opts) const
HostBuffer & GetTransientsIndexesBuffer() const
Retrieve the current host buffer for transient storage of indexes used for indexed draws.
PipelineRef GetFramebufferBlendSaturationPipeline(ContentContextOptions opts) const
PipelineRef GetBlendDifferencePipeline(ContentContextOptions opts) const
PipelineRef GetGaussianBlurPipeline(ContentContextOptions opts) const
PipelineRef GetBlendHuePipeline(ContentContextOptions opts) const
PipelineRef GetSrgbToLinearFilterPipeline(ContentContextOptions opts) const
PipelineRef GetDestinationOutBlendPipeline(ContentContextOptions opts) const
PipelineRef GetYUVToRGBFilterPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendDifferencePipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendHuePipeline(ContentContextOptions opts) const
PipelineRef GetSourceATopBlendPipeline(ContentContextOptions opts) const
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
PipelineRef GetXorBlendPipeline(ContentContextOptions opts) const
PipelineRef GetGlyphAtlasPipeline(ContentContextOptions opts) const
PipelineRef GetClipPipeline(ContentContextOptions opts) const
const std::shared_ptr< RenderTargetAllocator > & GetRenderTargetCache() const
void SetTransientsIndexesBuffer(std::shared_ptr< HostBuffer > host_buffer)
PipelineRef GetRRectBlurPipeline(ContentContextOptions opts) const
PipelineRef GetBlendScreenPipeline(ContentContextOptions opts) const
std::function< bool(const ContentContext &, RenderPass &)> SubpassCallback
PipelineRef GetBlendDarkenPipeline(ContentContextOptions opts) const
PipelineRef GetSourceBlendPipeline(ContentContextOptions opts) const
PipelineRef GetLinearToSrgbFilterPipeline(ContentContextOptions opts) const
TextShadowCache & GetTextShadowCache() const
PipelineRef GetBlendOverlayPipeline(ContentContextOptions opts) const
PipelineRef GetDestinationBlendPipeline(ContentContextOptions opts) const
PipelineRef GetDestinationOverBlendPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendMultiplyPipeline(ContentContextOptions opts) const
PipelineRef GetConicalGradientSSBOFillPipeline(ContentContextOptions opts, ConicalKind kind) const
Tessellator & GetTessellator() const
PipelineRef GetBlendLightenPipeline(ContentContextOptions opts) const
PipelineRef GetBlendMultiplyPipeline(ContentContextOptions opts) const
PipelineRef GetSourceOverBlendPipeline(ContentContextOptions opts) const
PipelineRef GetBlendExclusionPipeline(ContentContextOptions opts) const
PipelineRef GetColorMatrixColorFilterPipeline(ContentContextOptions opts) const
PipelineRef GetConicalGradientFillPipeline(ContentContextOptions opts, ConicalKind kind) const
std::shared_ptr< Context > GetContext() const
PipelineRef GetDestinationInBlendPipeline(ContentContextOptions opts) const
PipelineRef GetRSuperellipseBlurPipeline(ContentContextOptions opts) const
PipelineRef GetBorderMaskBlurPipeline(ContentContextOptions opts) const
PipelineRef GetDrawVerticesUberPipeline(BlendMode blend_mode, ContentContextOptions opts) const
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
A utility that generates triangles of the specified fill type given a polyline. This happens on the C...
Definition: tessellator.h:37
A cache for blurred text that re-uses these across frames.
std::vector< std::pair< uint64_t, std::unique_ptr< GenericRenderPipelineHandle > > > pipelines_
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:355
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
CompareFunction
Definition: formats.h:556
@ kAlways
Comparison test passes always passes.
BlendMode
Definition: color.h:58
SampleCount
Definition: formats.h:298
bool operator()(const RuntimeEffectPipelineKey &lhs, const RuntimeEffectPipelineKey &rhs) const
std::size_t operator()(const RuntimeEffectPipelineKey &key) const
void ApplyToPipelineDescriptor(PipelineDescriptor &desc) const
constexpr uint64_t ToKey() const
@ kIgnore
Turn the stencil test off. Used when drawing without stencil-then-cover.