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
219#ifdef IMPELLER_ENABLE_OPENGLES
220#if !defined(FML_OS_EMSCRIPTEN)
221 PipelineRef GetTiledTextureExternalPipeline(ContentContextOptions opts) const;
222 PipelineRef GetTiledTextureUvExternalPipeline(ContentContextOptions opts) const;
223#endif
224 PipelineRef GetDownsampleTextureGlesPipeline(ContentContextOptions opts) const;
225#endif // IMPELLER_ENABLE_OPENGLES
226 // clang-format on
227
228 // An empty 1x1 texture for binding drawVertices/drawAtlas or other cases
229 // that don't always have a texture (due to blending).
230 std::shared_ptr<Texture> GetEmptyTexture() const;
231
232 std::shared_ptr<Context> GetContext() const;
233
234 const Capabilities& GetDeviceCapabilities() const;
235
237 std::function<bool(const ContentContext&, RenderPass&)>;
238
239 /// @brief Creates a new texture of size `texture_size` and calls
240 /// `subpass_callback` with a `RenderPass` for drawing to the texture.
242 std::string_view label,
243 ISize texture_size,
244 const std::shared_ptr<CommandBuffer>& command_buffer,
245 const SubpassCallback& subpass_callback,
246 bool msaa_enabled = true,
247 bool depth_stencil_enabled = false,
248 int32_t mip_count = 1) const;
249
250 /// Makes a subpass that will render to `subpass_target`.
252 std::string_view label,
253 const RenderTarget& subpass_target,
254 const std::shared_ptr<CommandBuffer>& command_buffer,
255 const SubpassCallback& subpass_callback) const;
256
257 const std::shared_ptr<LazyGlyphAtlas>& GetLazyGlyphAtlas() const {
258 return lazy_glyph_atlas_;
259 }
260
261 const std::shared_ptr<RenderTargetAllocator>& GetRenderTargetCache() const {
262 return render_target_cache_;
263 }
264
265 /// RuntimeEffect pipelines must be obtained via this method to avoid
266 /// re-creating them every frame.
267 ///
268 /// The unique_entrypoint_name comes from RuntimeEffect::GetEntrypoint.
269 /// Impellerc generates a unique entrypoint name for runtime effect shaders
270 /// based on the input file name and shader stage.
271 ///
272 /// The create_callback is synchronously invoked exactly once if a cached
273 /// pipeline is not found.
275 const std::string& unique_entrypoint_name,
276 const ContentContextOptions& options,
277 const std::function<std::shared_ptr<Pipeline<PipelineDescriptor>>()>&
278 create_callback) const;
279
280 /// Used by hot reload/hot restart to clear a cached pipeline from
281 /// GetCachedRuntimeEffectPipeline.
283 const std::string& unique_entrypoint_name) const;
284
285 /// @brief Enable or disable texture caching.
286 void SetTextureCachingEnabled(bool enabled);
287
288 /// @brief Get a cached texture for the given image.
289 std::shared_ptr<Texture> GetCachedTexture(
290 const flutter::DlImage* image) const;
291
292 /// @brief Set a cached texture for the given image.
294 const std::shared_ptr<Texture>& texture) const;
295
296 /// @brief Remove a cached texture for the given image.
297 void RemoveCachedTexture(const flutter::DlImage* image) const;
298
299 /// @brief Clear all cached textures.
300 void ClearCachedTextures() const;
301
302 /// @brief Retrieve the current host buffer for transient storage of indexes
303 /// used for indexed draws.
304 ///
305 /// This may or may not return the same value as `GetTransientsDataBuffer`
306 /// depending on the backend.
307 ///
308 /// This is only safe to use from the raster threads. Other threads should
309 /// allocate their own device buffers.
311 return *indexes_host_buffer_;
312 }
313
314 /// @brief Retrieve the current host buffer for transient storage of other
315 /// non-index data.
316 ///
317 /// This is only safe to use from the raster threads. Other threads should
318 /// allocate their own device buffers.
319 HostBuffer& GetTransientsDataBuffer() const { return *data_host_buffer_; }
320
321 /// @brief Resets the transients buffers held onto by the content context.
323
324 TextShadowCache& GetTextShadowCache() const { return *text_shadow_cache_; }
325
326 protected:
327 // Visible for testing.
328 void SetTransientsIndexesBuffer(std::shared_ptr<HostBuffer> host_buffer) {
329 indexes_host_buffer_ = std::move(host_buffer);
330 }
331
332 // Visible for testing.
333 void SetTransientsDataBuffer(std::shared_ptr<HostBuffer> host_buffer) {
334 data_host_buffer_ = std::move(host_buffer);
335 }
336
337 private:
338 std::shared_ptr<Context> context_;
339 std::shared_ptr<LazyGlyphAtlas> lazy_glyph_atlas_;
340
341 /// Run backend specific additional setup and create common shader variants.
342 ///
343 /// This bootstrap is intended to improve the performance of several
344 /// first frame benchmarks that are tracked in the flutter device lab.
345 /// The workload includes initializing commonly used but not default
346 /// shader variants, as well as forcing driver initialization.
347 void InitializeCommonlyUsedShadersIfNeeded() const;
348
349 struct RuntimeEffectPipelineKey {
350 std::string unique_entrypoint_name;
351 ContentContextOptions options;
352
353 struct Hash {
354 std::size_t operator()(const RuntimeEffectPipelineKey& key) const {
355 return fml::HashCombine(key.unique_entrypoint_name,
356 key.options.ToKey());
357 }
358 };
359
360 struct Equal {
361 inline bool operator()(const RuntimeEffectPipelineKey& lhs,
362 const RuntimeEffectPipelineKey& rhs) const {
363 return lhs.unique_entrypoint_name == rhs.unique_entrypoint_name &&
364 lhs.options.ToKey() == rhs.options.ToKey();
365 }
366 };
367 };
368
369 mutable std::unordered_map<RuntimeEffectPipelineKey,
370 std::shared_ptr<Pipeline<PipelineDescriptor>>,
371 RuntimeEffectPipelineKey::Hash,
372 RuntimeEffectPipelineKey::Equal>
373 runtime_effect_pipelines_;
374
375 struct Pipelines;
376 std::unique_ptr<Pipelines> pipelines_;
377
378 bool is_valid_ = false;
379 std::shared_ptr<Tessellator> tessellator_;
380 std::shared_ptr<RenderTargetAllocator> render_target_cache_;
381 std::shared_ptr<HostBuffer> data_host_buffer_;
382 std::shared_ptr<HostBuffer> indexes_host_buffer_;
383 std::shared_ptr<Texture> empty_texture_;
384 std::unique_ptr<TextShadowCache> text_shadow_cache_;
385
386 bool is_texture_caching_enabled_ = false;
387 mutable std::unordered_map<const flutter::DlImage*, std::shared_ptr<Texture>>
388 texture_cache_;
389
390 ContentContext(const ContentContext&) = delete;
391
392 ContentContext& operator=(const ContentContext&) = delete;
393};
394
395} // namespace impeller
396
397#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 GetComplexRSEPipeline(ContentContextOptions opts) 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:529
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:99
CompareFunction
Definition formats.h:804
@ kAlways
Comparison test passes always passes.
BlendMode
Definition color.h:58
std::shared_ptr< ContextGLES > context
std::shared_ptr< CommandBuffer > command_buffer
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.