Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
15#include "flutter/fml/logging.h"
29
30namespace impeller {
31/// Pipeline state configuration.
32///
33/// Each unique combination of these options requires a different pipeline state
34/// object to be built. This struct is used as a key for the per-pipeline
35/// variant cache.
36///
37/// When adding fields to this key, reliant features should take care to limit
38/// the combinatorical explosion of variations. A sufficiently complicated
39/// Flutter application may easily require building hundreds of PSOs in total,
40/// but they shouldn't require e.g. 10s of thousands.
42 enum class StencilMode : uint8_t {
43 /// Turn the stencil test off. Used when drawing without stencil-then-cover.
44 kIgnore,
45
46 // Operations used for stencil-then-cover.
47
48 /// Draw the stencil for the NonZero fill path rule.
49 ///
50 /// The stencil ref should always be 0 on commands using this mode.
52 /// Draw the stencil for the EvenOdd fill path rule.
53 ///
54 /// The stencil ref should always be 0 on commands using this mode.
56 /// Draw a stencil which always increments once for everything in the vertex
57 /// coverage regardless of triangle overlap.
58 ///
59 /// The stencil ref should always be 0 on commands using this mode.
61 /// Used for draw calls which fill in the stenciled area. Intended to be
62 /// used after `kStencilNonZeroFill` or `kStencilEvenOddFill` is used to set
63 /// up the stencil buffer. Also cleans up the stencil buffer by resetting
64 /// everything to zero.
65 ///
66 /// The stencil ref should always be 0 on commands using this mode.
68 /// The opposite of `kCoverCompare`. Used for draw calls which fill in the
69 /// non-stenciled area (intersection clips). Intended to be used after
70 /// `kStencilNonZeroFill` or `kStencilEvenOddFill` is used to set up the
71 /// stencil buffer. Also cleans up the stencil buffer by resetting
72 /// everything to zero.
73 ///
74 /// The stencil ref should always be 0 on commands using this mode.
76 };
77
85 bool depth_write_enabled = false;
87
88 constexpr uint64_t ToKey() const {
89 static_assert(sizeof(sample_count) == 1);
90 static_assert(sizeof(blend_mode) == 1);
91 static_assert(sizeof(sample_count) == 1);
92 static_assert(sizeof(depth_compare) == 1);
93 static_assert(sizeof(stencil_mode) == 1);
94 static_assert(sizeof(primitive_type) == 1);
95 static_assert(sizeof(color_attachment_pixel_format) == 1);
96
97 return (is_for_rrect_blur_clear ? 1llu : 0llu) << 0 |
98 (0) << 1 | // // Unused, previously wireframe.
99 (has_depth_stencil_attachments ? 1llu : 0llu) << 2 |
100 (depth_write_enabled ? 1llu : 0llu) << 3 |
101 // enums
102 static_cast<uint64_t>(color_attachment_pixel_format) << 8 |
103 static_cast<uint64_t>(primitive_type) << 16 |
104 static_cast<uint64_t>(stencil_mode) << 24 |
105 static_cast<uint64_t>(depth_compare) << 32 |
106 static_cast<uint64_t>(blend_mode) << 40 |
107 static_cast<uint64_t>(sample_count) << 48;
108 }
109
111};
112
119
120class Tessellator;
121class RenderTargetCache;
122
124 public:
125 explicit ContentContext(
126 std::shared_ptr<Context> context,
127 std::shared_ptr<TypographerContext> typographer_context,
128 std::shared_ptr<RenderTargetAllocator> render_target_allocator = nullptr);
129
131
132 bool IsValid() const;
133
135
136 // clang-format off
218#ifdef IMPELLER_ENABLE_OPENGLES
219#if !defined(FML_OS_EMSCRIPTEN)
220 PipelineRef GetTiledTextureExternalPipeline(ContentContextOptions opts) const;
221 PipelineRef GetTiledTextureUvExternalPipeline(ContentContextOptions opts) const;
222#endif
223 PipelineRef GetDownsampleTextureGlesPipeline(ContentContextOptions opts) const;
224#endif // IMPELLER_ENABLE_OPENGLES
225 // clang-format on
226
227 // An empty 1x1 texture for binding drawVertices/drawAtlas or other cases
228 // that don't always have a texture (due to blending).
229 std::shared_ptr<Texture> GetEmptyTexture() const;
230
231 std::shared_ptr<Context> GetContext() const;
232
233 const Capabilities& GetDeviceCapabilities() const;
234
236 std::function<bool(const ContentContext&, RenderPass&)>;
237
238 /// @brief Creates a new texture of size `texture_size` and calls
239 /// `subpass_callback` with a `RenderPass` for drawing to the texture.
241 std::string_view label,
242 ISize texture_size,
243 const std::shared_ptr<CommandBuffer>& command_buffer,
244 const SubpassCallback& subpass_callback,
245 bool msaa_enabled = true,
246 bool depth_stencil_enabled = false,
247 int32_t mip_count = 1) const;
248
249 /// Makes a subpass that will render to `subpass_target`.
251 std::string_view label,
252 const RenderTarget& subpass_target,
253 const std::shared_ptr<CommandBuffer>& command_buffer,
254 const SubpassCallback& subpass_callback) const;
255
256 const std::shared_ptr<LazyGlyphAtlas>& GetLazyGlyphAtlas() const {
257 return lazy_glyph_atlas_;
258 }
259
260 const std::shared_ptr<RenderTargetAllocator>& GetRenderTargetCache() const {
261 return render_target_cache_;
262 }
263
264 /// RuntimeEffect pipelines must be obtained via this method to avoid
265 /// re-creating them every frame.
266 ///
267 /// The unique_entrypoint_name comes from RuntimeEffect::GetEntrypoint.
268 /// Impellerc generates a unique entrypoint name for runtime effect shaders
269 /// based on the input file name and shader stage.
270 ///
271 /// The create_callback is synchronously invoked exactly once if a cached
272 /// pipeline is not found.
274 const std::string& unique_entrypoint_name,
275 const ContentContextOptions& options,
276 const std::function<std::shared_ptr<Pipeline<PipelineDescriptor>>()>&
277 create_callback) const;
278
279 /// Used by hot reload/hot restart to clear a cached pipeline from
280 /// GetCachedRuntimeEffectPipeline.
282 const std::string& unique_entrypoint_name) const;
283
284 /// @brief Enable or disable texture caching.
285 void SetTextureCachingEnabled(bool enabled);
286
287 /// @brief Get a cached texture for the given image.
288 std::shared_ptr<Texture> GetCachedTexture(
289 const flutter::DlImage* image) const;
290
291 /// @brief Set a cached texture for the given image.
293 const std::shared_ptr<Texture>& texture) const;
294
295 /// @brief Remove a cached texture for the given image.
296 void RemoveCachedTexture(const flutter::DlImage* image) const;
297
298 /// @brief Clear all cached textures.
299 void ClearCachedTextures() const;
300
301 /// @brief Retrieve the current host buffer for transient storage of indexes
302 /// used for indexed draws.
303 ///
304 /// This may or may not return the same value as `GetTransientsDataBuffer`
305 /// depending on the backend.
306 ///
307 /// This is only safe to use from the raster threads. Other threads should
308 /// allocate their own device buffers.
310 return *indexes_host_buffer_;
311 }
312
313 /// @brief Retrieve the current host buffer for transient storage of other
314 /// non-index data.
315 ///
316 /// This is only safe to use from the raster threads. Other threads should
317 /// allocate their own device buffers.
318 HostBuffer& GetTransientsDataBuffer() const { return *data_host_buffer_; }
319
320 /// @brief Resets the transients buffers held onto by the content context.
322
323 TextShadowCache& GetTextShadowCache() const { return *text_shadow_cache_; }
324
325 protected:
326 // Visible for testing.
327 void SetTransientsIndexesBuffer(std::shared_ptr<HostBuffer> host_buffer) {
328 indexes_host_buffer_ = std::move(host_buffer);
329 }
330
331 // Visible for testing.
332 void SetTransientsDataBuffer(std::shared_ptr<HostBuffer> host_buffer) {
333 data_host_buffer_ = std::move(host_buffer);
334 }
335
336 private:
337 std::shared_ptr<Context> context_;
338 std::shared_ptr<LazyGlyphAtlas> lazy_glyph_atlas_;
339
340 /// Run backend specific additional setup and create common shader variants.
341 ///
342 /// This bootstrap is intended to improve the performance of several
343 /// first frame benchmarks that are tracked in the flutter device lab.
344 /// The workload includes initializing commonly used but not default
345 /// shader variants, as well as forcing driver initialization.
346 void InitializeCommonlyUsedShadersIfNeeded() const;
347
348 struct RuntimeEffectPipelineKey {
349 std::string unique_entrypoint_name;
350 ContentContextOptions options;
351
352 struct Hash {
353 std::size_t operator()(const RuntimeEffectPipelineKey& key) const {
354 return fml::HashCombine(key.unique_entrypoint_name,
355 key.options.ToKey());
356 }
357 };
358
359 struct Equal {
360 inline bool operator()(const RuntimeEffectPipelineKey& lhs,
361 const RuntimeEffectPipelineKey& rhs) const {
362 return lhs.unique_entrypoint_name == rhs.unique_entrypoint_name &&
363 lhs.options.ToKey() == rhs.options.ToKey();
364 }
365 };
366 };
367
368 mutable std::unordered_map<RuntimeEffectPipelineKey,
369 std::shared_ptr<Pipeline<PipelineDescriptor>>,
370 RuntimeEffectPipelineKey::Hash,
371 RuntimeEffectPipelineKey::Equal>
372 runtime_effect_pipelines_;
373
374 struct Pipelines;
375 std::unique_ptr<Pipelines> pipelines_;
376
377 bool is_valid_ = false;
378 std::shared_ptr<Tessellator> tessellator_;
379 std::shared_ptr<RenderTargetAllocator> render_target_cache_;
380 std::shared_ptr<HostBuffer> data_host_buffer_;
381 std::shared_ptr<HostBuffer> indexes_host_buffer_;
382 std::shared_ptr<Texture> empty_texture_;
383 std::unique_ptr<TextShadowCache> text_shadow_cache_;
384
385 bool is_texture_caching_enabled_ = false;
386 mutable std::unordered_map<const flutter::DlImage*, std::shared_ptr<Texture>>
387 texture_cache_;
388
389 ContentContext(const ContentContext&) = delete;
390
391 ContentContext& operator=(const ContentContext&) = delete;
392};
393
394} // namespace impeller
395
396#endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_CONTENT_CONTEXT_H_
Represents an image whose allocation is (usually) resident on device memory.
Definition dl_image.h:34
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
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
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 > GetCachedTexture(const flutter::DlImage *image) const
Get a cached texture for the given image.
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< RenderTargetAllocator > & GetRenderTargetCache() const
void ResetTransientsBuffers()
Resets the transients buffers held onto by the content context.
const std::shared_ptr< LazyGlyphAtlas > & GetLazyGlyphAtlas() const
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 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
TextShadowCache & GetTextShadowCache() const
PipelineRef GetTexturePipeline(ContentContextOptions opts) const
PipelineRef GetBlendHardLightPipeline(ContentContextOptions opts) const
PipelineRef GetClearBlendPipeline(ContentContextOptions opts) const
PipelineRef GetCirclePipeline(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 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
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
PipelineRef GetXorBlendPipeline(ContentContextOptions opts) const
PipelineRef GetGlyphAtlasPipeline(ContentContextOptions opts) const
PipelineRef GetClipPipeline(ContentContextOptions opts) const
void SetTransientsIndexesBuffer(std::shared_ptr< HostBuffer > host_buffer)
PipelineRef GetRRectBlurPipeline(ContentContextOptions opts) const
HostBuffer & GetTransientsIndexesBuffer() const
Retrieve the current host buffer for transient storage of indexes used for indexed draws.
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
PipelineRef GetBlendOverlayPipeline(ContentContextOptions opts) const
PipelineRef GetDestinationBlendPipeline(ContentContextOptions opts) const
PipelineRef GetDestinationOverBlendPipeline(ContentContextOptions opts) const
PipelineRef GetFramebufferBlendMultiplyPipeline(ContentContextOptions opts) const
void SetTextureCachingEnabled(bool enabled)
Enable or disable texture caching.
PipelineRef GetConicalGradientSSBOFillPipeline(ContentContextOptions opts, ConicalKind kind) const
Tessellator & GetTessellator() const
void SetCachedTexture(const flutter::DlImage *image, const std::shared_ptr< Texture > &texture) const
Set a cached texture for the given image.
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
void RemoveCachedTexture(const flutter::DlImage *image) const
Remove a cached texture for the given image.
PipelineRef GetConicalGradientFillPipeline(ContentContextOptions opts, ConicalKind kind) const
std::shared_ptr< Context > GetContext() const
void ClearCachedTextures() const
Clear all cached textures.
PipelineRef GetDestinationInBlendPipeline(ContentContextOptions opts) const
PipelineRef GetRSuperellipseBlurPipeline(ContentContextOptions opts) const
PipelineRef GetBorderMaskBlurPipeline(ContentContextOptions opts) const
PipelineRef GetDrawVerticesUberPipeline(BlendMode blend_mode, ContentContextOptions opts) const
Describes the fixed function and programmable aspects of rendering and compute operations performed b...
Definition pipeline.h:53
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_
FlutterVulkanImage * image
FlTexture * texture
constexpr std::size_t HashCombine()
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
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.