| Flutter Impeller |

content_context.cc
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 #include "impeller/entity/contents/content_context.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "fml/trace_event.h"
11 #include "impeller/base/strings.h"
12 #include "impeller/base/validation.h"
13 #include "impeller/core/formats.h"
14 #include "impeller/core/texture_descriptor.h"
15 #include "impeller/entity/contents/framebuffer_blend_contents.h"
16 #include "impeller/entity/entity.h"
17 #include "impeller/entity/render_target_cache.h"
18 #include "impeller/renderer/command_buffer.h"
19 #include "impeller/renderer/pipeline_descriptor.h"
20 #include "impeller/renderer/pipeline_library.h"
21 #include "impeller/renderer/render_target.h"
22 #include "impeller/renderer/texture_mipmap.h"
23 #include "impeller/tessellator/tessellator.h"
24 #include "impeller/typographer/typographer_context.h"
25
26 namespace impeller {
27
28 void ContentContextOptions::ApplyToPipelineDescriptor(
29 PipelineDescriptor& desc) const {
30 auto pipeline_blend = blend_mode;
31 if (blend_mode > Entity::kLastPipelineBlendMode) {
32 VALIDATION_LOG << "Cannot use blend mode " << static_cast<int>(blend_mode)
33 << " as a pipeline blend.";
34 pipeline_blend = BlendMode::kSourceOver;
35 }
36
37 desc.SetSampleCount(sample_count);
38
39 ColorAttachmentDescriptor color0 = *desc.GetColorAttachmentDescriptor(0u);
40 color0.format = color_attachment_pixel_format;
41 color0.alpha_blend_op = BlendOperation::kAdd;
42 color0.color_blend_op = BlendOperation::kAdd;
43 color0.write_mask = ColorWriteMaskBits::kAll;
44
45 switch (pipeline_blend) {
46 case BlendMode::kClear:
47 if (is_for_rrect_blur_clear) {
48 color0.alpha_blend_op = BlendOperation::kReverseSubtract;
49 color0.color_blend_op = BlendOperation::kReverseSubtract;
50 color0.dst_alpha_blend_factor = BlendFactor::kOne;
51 color0.dst_color_blend_factor = BlendFactor::kOne;
52 color0.src_alpha_blend_factor = BlendFactor::kDestinationColor;
53 color0.src_color_blend_factor = BlendFactor::kDestinationColor;
54 } else {
55 color0.dst_alpha_blend_factor = BlendFactor::kZero;
56 color0.dst_color_blend_factor = BlendFactor::kZero;
57 color0.src_alpha_blend_factor = BlendFactor::kZero;
58 color0.src_color_blend_factor = BlendFactor::kZero;
59 }
60 break;
61 case BlendMode::kSource:
62 color0.blending_enabled = false;
63 color0.dst_alpha_blend_factor = BlendFactor::kZero;
64 color0.dst_color_blend_factor = BlendFactor::kZero;
65 color0.src_alpha_blend_factor = BlendFactor::kOne;
66 color0.src_color_blend_factor = BlendFactor::kOne;
67 break;
68 case BlendMode::kDestination:
69 color0.dst_alpha_blend_factor = BlendFactor::kOne;
70 color0.dst_color_blend_factor = BlendFactor::kOne;
71 color0.src_alpha_blend_factor = BlendFactor::kZero;
72 color0.src_color_blend_factor = BlendFactor::kZero;
73 color0.write_mask = ColorWriteMaskBits::kNone;
74 break;
75 case BlendMode::kSourceOver:
76 color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha;
77 color0.dst_color_blend_factor = BlendFactor::kOneMinusSourceAlpha;
78 color0.src_alpha_blend_factor = BlendFactor::kOne;
79 color0.src_color_blend_factor = BlendFactor::kOne;
80 break;
81 case BlendMode::kDestinationOver:
82 color0.dst_alpha_blend_factor = BlendFactor::kOne;
83 color0.dst_color_blend_factor = BlendFactor::kOne;
84 color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
85 color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
86 break;
87 case BlendMode::kSourceIn:
88 color0.dst_alpha_blend_factor = BlendFactor::kZero;
89 color0.dst_color_blend_factor = BlendFactor::kZero;
90 color0.src_alpha_blend_factor = BlendFactor::kDestinationAlpha;
91 color0.src_color_blend_factor = BlendFactor::kDestinationAlpha;
92 break;
93 case BlendMode::kDestinationIn:
94 color0.dst_alpha_blend_factor = BlendFactor::kSourceAlpha;
95 color0.dst_color_blend_factor = BlendFactor::kSourceAlpha;
96 color0.src_alpha_blend_factor = BlendFactor::kZero;
97 color0.src_color_blend_factor = BlendFactor::kZero;
98 break;
99 case BlendMode::kSourceOut:
100 color0.dst_alpha_blend_factor = BlendFactor::kZero;
101 color0.dst_color_blend_factor = BlendFactor::kZero;
102 color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
103 color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
104 break;
105 case BlendMode::kDestinationOut:
106 color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha;
107 color0.dst_color_blend_factor = BlendFactor::kOneMinusSourceAlpha;
108 color0.src_alpha_blend_factor = BlendFactor::kZero;
109 color0.src_color_blend_factor = BlendFactor::kZero;
110 break;
111 case BlendMode::kSourceATop:
112 color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha;
113 color0.dst_color_blend_factor = BlendFactor::kOneMinusSourceAlpha;
114 color0.src_alpha_blend_factor = BlendFactor::kDestinationAlpha;
115 color0.src_color_blend_factor = BlendFactor::kDestinationAlpha;
116 break;
117 case BlendMode::kDestinationATop:
118 color0.dst_alpha_blend_factor = BlendFactor::kSourceAlpha;
119 color0.dst_color_blend_factor = BlendFactor::kSourceAlpha;
120 color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
121 color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
122 break;
123 case BlendMode::kXor:
124 color0.dst_alpha_blend_factor = BlendFactor::kOneMinusSourceAlpha;
125 color0.dst_color_blend_factor = BlendFactor::kOneMinusSourceAlpha;
126 color0.src_alpha_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
127 color0.src_color_blend_factor = BlendFactor::kOneMinusDestinationAlpha;
128 break;
129 case BlendMode::kPlus:
130 color0.dst_alpha_blend_factor = BlendFactor::kOne;
131 color0.dst_color_blend_factor = BlendFactor::kOne;
132 color0.src_alpha_blend_factor = BlendFactor::kOne;
133 color0.src_color_blend_factor = BlendFactor::kOne;
134 break;
135 case BlendMode::kModulate:
136 color0.dst_alpha_blend_factor = BlendFactor::kSourceAlpha;
137 color0.dst_color_blend_factor = BlendFactor::kSourceColor;
138 color0.src_alpha_blend_factor = BlendFactor::kZero;
139 color0.src_color_blend_factor = BlendFactor::kZero;
140 break;
141 default:
142 FML_UNREACHABLE();
143 }
144 desc.SetColorAttachmentDescriptor(0u, color0);
145
146 if (!has_depth_stencil_attachments) {
147 desc.ClearDepthAttachment();
148 desc.ClearStencilAttachments();
149 }
150
151 auto maybe_stencil = desc.GetFrontStencilAttachmentDescriptor();
152 auto maybe_depth = desc.GetDepthStencilAttachmentDescriptor();
153 FML_DCHECK(has_depth_stencil_attachments == maybe_depth.has_value())
154 << "Depth attachment doesn't match expected pipeline state. "
155 "has_depth_stencil_attachments="
156 << has_depth_stencil_attachments;
157 FML_DCHECK(has_depth_stencil_attachments == maybe_stencil.has_value())
158 << "Stencil attachment doesn't match expected pipeline state. "
159 "has_depth_stencil_attachments="
160 << has_depth_stencil_attachments;
161 if (maybe_stencil.has_value()) {
162 StencilAttachmentDescriptor front_stencil = maybe_stencil.value();
163 StencilAttachmentDescriptor back_stencil = front_stencil;
164
165 switch (stencil_mode) {
166 case StencilMode::kIgnore:
167 front_stencil.stencil_compare = CompareFunction::kAlways;
168 front_stencil.depth_stencil_pass = StencilOperation::kKeep;
169 desc.SetStencilAttachmentDescriptors(front_stencil);
170 break;
171 case StencilMode::kStencilNonZeroFill:
172 // The stencil ref should be 0 on commands that use this mode.
173 front_stencil.stencil_compare = CompareFunction::kAlways;
174 front_stencil.depth_stencil_pass = StencilOperation::kIncrementWrap;
175 back_stencil.stencil_compare = CompareFunction::kAlways;
176 back_stencil.depth_stencil_pass = StencilOperation::kDecrementWrap;
177 desc.SetStencilAttachmentDescriptors(front_stencil, back_stencil);
178 break;
179 case StencilMode::kStencilEvenOddFill:
180 // The stencil ref should be 0 on commands that use this mode.
181 front_stencil.stencil_compare = CompareFunction::kEqual;
182 front_stencil.depth_stencil_pass = StencilOperation::kIncrementWrap;
183 front_stencil.stencil_failure = StencilOperation::kDecrementWrap;
184 desc.SetStencilAttachmentDescriptors(front_stencil);
185 break;
186 case StencilMode::kCoverCompare:
187 // The stencil ref should be 0 on commands that use this mode.
188 front_stencil.stencil_compare = CompareFunction::kNotEqual;
189 front_stencil.depth_stencil_pass =
190 StencilOperation::kSetToReferenceValue;
191 desc.SetStencilAttachmentDescriptors(front_stencil);
192 break;
193 case StencilMode::kCoverCompareInverted:
194 // The stencil ref should be 0 on commands that use this mode.
195 front_stencil.stencil_compare = CompareFunction::kEqual;
196 front_stencil.stencil_failure = StencilOperation::kSetToReferenceValue;
197 desc.SetStencilAttachmentDescriptors(front_stencil);
198 break;
199 case StencilMode::kOverdrawPreventionIncrement:
200 front_stencil.stencil_compare = CompareFunction::kEqual;
201 front_stencil.depth_stencil_pass = StencilOperation::kIncrementClamp;
202 desc.SetStencilAttachmentDescriptors(front_stencil);
203 break;
204 case StencilMode::kOverdrawPreventionRestore:
205 front_stencil.stencil_compare = CompareFunction::kLess;
206 front_stencil.depth_stencil_pass =
207 StencilOperation::kSetToReferenceValue;
208 desc.SetStencilAttachmentDescriptors(front_stencil);
209 break;
210 }
211 }
212 if (maybe_depth.has_value()) {
213 DepthAttachmentDescriptor depth = maybe_depth.value();
214 depth.depth_write_enabled = depth_write_enabled;
215 depth.depth_compare = depth_compare;
216 desc.SetDepthStencilAttachmentDescriptor(depth);
217 }
218
219 desc.SetPrimitiveType(primitive_type);
220
221 desc.SetPolygonMode(wireframe ? PolygonMode::kLine : PolygonMode::kFill);
222 }
223
224 template <typename PipelineT>
225 static std::unique_ptr<PipelineT> CreateDefaultPipeline(
226 const Context& context) {
227 auto desc = PipelineT::Builder::MakeDefaultPipelineDescriptor(context);
228 if (!desc.has_value()) {
229 return nullptr;
230 }
231 // Apply default ContentContextOptions to the descriptor.
232 const auto default_color_format =
233 context.GetCapabilities()->GetDefaultColorFormat();
234 ContentContextOptions{.sample_count = SampleCount::kCount4,
235 .primitive_type = PrimitiveType::kTriangleStrip,
236 .color_attachment_pixel_format = default_color_format}
237 .ApplyToPipelineDescriptor(*desc);
238 return std::make_unique<PipelineT>(context, desc);
239 }
240
241 ContentContext::ContentContext(
242 std::shared_ptr<Context> context,
243 std::shared_ptr<TypographerContext> typographer_context,
244 std::shared_ptr<RenderTargetAllocator> render_target_allocator)
245 : context_(std::move(context)),
246 lazy_glyph_atlas_(
247 std::make_shared<LazyGlyphAtlas>(std::move(typographer_context))),
248 tessellator_(std::make_shared<Tessellator>()),
249 render_target_cache_(render_target_allocator == nullptr
250 ? std::make_shared<RenderTargetCache>(
251 context_->GetResourceAllocator())
252 : std::move(render_target_allocator)),
253 host_buffer_(HostBuffer::Create(context_->GetResourceAllocator())) {
254 if (!context_ || !context_->IsValid()) {
255 return;
256 }
257
258 {
259 TextureDescriptor desc;
260 desc.storage_mode = StorageMode::kHostVisible;
261 desc.format = PixelFormat::kR8G8B8A8UNormInt;
262 desc.size = ISize{1, 1};
263 empty_texture_ = GetContext()->GetResourceAllocator()->CreateTexture(desc);
264 auto data = Color::BlackTransparent().ToR8G8B8A8();
265 auto cmd_buffer = GetContext()->CreateCommandBuffer();
266 auto blit_pass = cmd_buffer->CreateBlitPass();
267 auto& host_buffer = GetTransientsBuffer();
268 auto buffer_view = host_buffer.Emplace(data);
269 blit_pass->AddCopy(buffer_view, empty_texture_);
270
271 if (!blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()) ||
272 !GetContext()
273 ->GetCommandQueue()
274 ->Submit({std::move(cmd_buffer)})
275 .ok()) {
276 VALIDATION_LOG << "Failed to create empty texture.";
277 }
278 }
279
280 auto options = ContentContextOptions{
281 .sample_count = SampleCount::kCount4,
282 .color_attachment_pixel_format =
283 context_->GetCapabilities()->GetDefaultColorFormat()};
284 auto options_trianglestrip = ContentContextOptions{
285 .sample_count = SampleCount::kCount4,
286 .primitive_type = PrimitiveType::kTriangleStrip,
287 .color_attachment_pixel_format =
288 context_->GetCapabilities()->GetDefaultColorFormat()};
289 const auto supports_decal = static_cast<Scalar>(
290 context_->GetCapabilities()->SupportsDecalSamplerAddressMode());
291
292 // Futures for the following pipelines may block in case the first frame is
293 // rendered without the pipelines being ready. Put pipelines that are more
294 // likely to be used first.
295 {
296 glyph_atlas_pipelines_.CreateDefault(
297 *context_, options,
298 {static_cast<Scalar>(
299 GetContext()->GetCapabilities()->GetDefaultGlyphAtlasFormat() ==
300 PixelFormat::kA8UNormInt)});
301 solid_fill_pipelines_.CreateDefault(*context_, options);
302 texture_pipelines_.CreateDefault(*context_, options);
303 fast_gradient_pipelines_.CreateDefault(*context_, options);
304
305 if (context_->GetCapabilities()->SupportsSSBO()) {
306 linear_gradient_ssbo_fill_pipelines_.CreateDefault(*context_, options);
307 radial_gradient_ssbo_fill_pipelines_.CreateDefault(*context_, options);
308 conical_gradient_ssbo_fill_pipelines_.CreateDefault(*context_, options);
309 sweep_gradient_ssbo_fill_pipelines_.CreateDefault(*context_, options);
310 } else {
311 linear_gradient_fill_pipelines_.CreateDefault(*context_, options);
312 radial_gradient_fill_pipelines_.CreateDefault(*context_, options);
313 conical_gradient_fill_pipelines_.CreateDefault(*context_, options);
314 sweep_gradient_fill_pipelines_.CreateDefault(*context_, options);
315 }
316
317 /// Setup default clip pipeline.
318 auto clip_pipeline_descriptor =
319 ClipPipeline::Builder::MakeDefaultPipelineDescriptor(*context_);
320 if (!clip_pipeline_descriptor.has_value()) {
321 return;
322 }
323 ContentContextOptions{
324 .sample_count = SampleCount::kCount4,
325 .color_attachment_pixel_format =
326 context_->GetCapabilities()->GetDefaultColorFormat()}
327 .ApplyToPipelineDescriptor(*clip_pipeline_descriptor);
328 // Disable write to all color attachments.
329 auto clip_color_attachments =
330 clip_pipeline_descriptor->GetColorAttachmentDescriptors();
331 for (auto& color_attachment : clip_color_attachments) {
332 color_attachment.second.write_mask = ColorWriteMaskBits::kNone;
333 }
334 clip_pipeline_descriptor->SetColorAttachmentDescriptors(
335 std::move(clip_color_attachments));
336 clip_pipelines_.SetDefault(
337 options,
338 std::make_unique<ClipPipeline>(*context_, clip_pipeline_descriptor));
339 texture_downsample_pipelines_.CreateDefault(*context_,
340 options_trianglestrip);
341 rrect_blur_pipelines_.CreateDefault(*context_, options_trianglestrip);
342 texture_strict_src_pipelines_.CreateDefault(*context_, options);
343 tiled_texture_pipelines_.CreateDefault(*context_, options,
344 {supports_decal});
345 gaussian_blur_pipelines_.CreateDefault(*context_, options_trianglestrip,
346 {supports_decal});
347 border_mask_blur_pipelines_.CreateDefault(*context_, options_trianglestrip);
348 color_matrix_color_filter_pipelines_.CreateDefault(*context_,
349 options_trianglestrip);
350 porter_duff_blend_pipelines_.CreateDefault(*context_, options_trianglestrip,
351 {supports_decal});
352 vertices_uber_shader_.CreateDefault(*context_, options, {supports_decal});
353 }
354
355 if (context_->GetCapabilities()->SupportsFramebufferFetch()) {
356 framebuffer_blend_color_pipelines_.CreateDefault(
357 *context_, options_trianglestrip,
358 {static_cast<Scalar>(BlendSelectValues::kColor), supports_decal});
359 framebuffer_blend_colorburn_pipelines_.CreateDefault(
360 *context_, options_trianglestrip,
361 {static_cast<Scalar>(BlendSelectValues::kColorBurn), supports_decal});
362 framebuffer_blend_colordodge_pipelines_.CreateDefault(
363 *context_, options_trianglestrip,
364 {static_cast<Scalar>(BlendSelectValues::kColorDodge), supports_decal});
365 framebuffer_blend_darken_pipelines_.CreateDefault(
366 *context_, options_trianglestrip,
367 {static_cast<Scalar>(BlendSelectValues::kDarken), supports_decal});
368 framebuffer_blend_difference_pipelines_.CreateDefault(
369 *context_, options_trianglestrip,
370 {static_cast<Scalar>(BlendSelectValues::kDifference), supports_decal});
371 framebuffer_blend_exclusion_pipelines_.CreateDefault(
372 *context_, options_trianglestrip,
373 {static_cast<Scalar>(BlendSelectValues::kExclusion), supports_decal});
374 framebuffer_blend_hardlight_pipelines_.CreateDefault(
375 *context_, options_trianglestrip,
376 {static_cast<Scalar>(BlendSelectValues::kHardLight), supports_decal});
377 framebuffer_blend_hue_pipelines_.CreateDefault(
378 *context_, options_trianglestrip,
379 {static_cast<Scalar>(BlendSelectValues::kHue), supports_decal});
380 framebuffer_blend_lighten_pipelines_.CreateDefault(
381 *context_, options_trianglestrip,
382 {static_cast<Scalar>(BlendSelectValues::kLighten), supports_decal});
383 framebuffer_blend_luminosity_pipelines_.CreateDefault(
384 *context_, options_trianglestrip,
385 {static_cast<Scalar>(BlendSelectValues::kLuminosity), supports_decal});
386 framebuffer_blend_multiply_pipelines_.CreateDefault(
387 *context_, options_trianglestrip,
388 {static_cast<Scalar>(BlendSelectValues::kMultiply), supports_decal});
389 framebuffer_blend_overlay_pipelines_.CreateDefault(
390 *context_, options_trianglestrip,
391 {static_cast<Scalar>(BlendSelectValues::kOverlay), supports_decal});
392 framebuffer_blend_saturation_pipelines_.CreateDefault(
393 *context_, options_trianglestrip,
394 {static_cast<Scalar>(BlendSelectValues::kSaturation), supports_decal});
395 framebuffer_blend_screen_pipelines_.CreateDefault(
396 *context_, options_trianglestrip,
397 {static_cast<Scalar>(BlendSelectValues::kScreen), supports_decal});
398 framebuffer_blend_softlight_pipelines_.CreateDefault(
399 *context_, options_trianglestrip,
400 {static_cast<Scalar>(BlendSelectValues::kSoftLight), supports_decal});
401 } else {
402 blend_color_pipelines_.CreateDefault(
403 *context_, options_trianglestrip,
404 {static_cast<Scalar>(BlendSelectValues::kColor), supports_decal});
405 blend_colorburn_pipelines_.CreateDefault(
406 *context_, options_trianglestrip,
407 {static_cast<Scalar>(BlendSelectValues::kColorBurn), supports_decal});
408 blend_colordodge_pipelines_.CreateDefault(
409 *context_, options_trianglestrip,
410 {static_cast<Scalar>(BlendSelectValues::kColorDodge), supports_decal});
411 blend_darken_pipelines_.CreateDefault(
412 *context_, options_trianglestrip,
413 {static_cast<Scalar>(BlendSelectValues::kDarken), supports_decal});
414 blend_difference_pipelines_.CreateDefault(
415 *context_, options_trianglestrip,
416 {static_cast<Scalar>(BlendSelectValues::kDifference), supports_decal});
417 blend_exclusion_pipelines_.CreateDefault(
418 *context_, options_trianglestrip,
419 {static_cast<Scalar>(BlendSelectValues::kExclusion), supports_decal});
420 blend_hardlight_pipelines_.CreateDefault(
421 *context_, options_trianglestrip,
422 {static_cast<Scalar>(BlendSelectValues::kHardLight), supports_decal});
423 blend_hue_pipelines_.CreateDefault(
424 *context_, options_trianglestrip,
425 {static_cast<Scalar>(BlendSelectValues::kHue), supports_decal});
426 blend_lighten_pipelines_.CreateDefault(
427 *context_, options_trianglestrip,
428 {static_cast<Scalar>(BlendSelectValues::kLighten), supports_decal});
429 blend_luminosity_pipelines_.CreateDefault(
430 *context_, options_trianglestrip,
431 {static_cast<Scalar>(BlendSelectValues::kLuminosity), supports_decal});
432 blend_multiply_pipelines_.CreateDefault(
433 *context_, options_trianglestrip,
434 {static_cast<Scalar>(BlendSelectValues::kMultiply), supports_decal});
435 blend_overlay_pipelines_.CreateDefault(
436 *context_, options_trianglestrip,
437 {static_cast<Scalar>(BlendSelectValues::kOverlay), supports_decal});
438 blend_saturation_pipelines_.CreateDefault(
439 *context_, options_trianglestrip,
440 {static_cast<Scalar>(BlendSelectValues::kSaturation), supports_decal});
441 blend_screen_pipelines_.CreateDefault(
442 *context_, options_trianglestrip,
443 {static_cast<Scalar>(BlendSelectValues::kScreen), supports_decal});
444 blend_softlight_pipelines_.CreateDefault(
445 *context_, options_trianglestrip,
446 {static_cast<Scalar>(BlendSelectValues::kSoftLight), supports_decal});
447 }
448
449 morphology_filter_pipelines_.CreateDefault(*context_, options_trianglestrip,
450 {supports_decal});
451 linear_to_srgb_filter_pipelines_.CreateDefault(*context_,
452 options_trianglestrip);
453 srgb_to_linear_filter_pipelines_.CreateDefault(*context_,
454 options_trianglestrip);
455 yuv_to_rgb_filter_pipelines_.CreateDefault(*context_, options_trianglestrip);
456
457 // GLES only shader that is unsupported on macOS.
458 #if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_MACOSX)
459 if (GetContext()->GetBackendType() == Context::BackendType::kOpenGLES) {
460 tiled_texture_external_pipelines_.CreateDefault(*context_, options);
461 }
462 #endif // IMPELLER_ENABLE_OPENGLES
463
464 is_valid_ = true;
465 InitializeCommonlyUsedShadersIfNeeded();
466 }
467
468 ContentContext::~ContentContext() = default;
469
470 bool ContentContext::IsValid() const {
471 return is_valid_;
472 }
473
474 std::shared_ptr<Texture> ContentContext::GetEmptyTexture() const {
475 return empty_texture_;
476 }
477
478 fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
479 std::string_view label,
480 ISize texture_size,
481 const std::shared_ptr<CommandBuffer>& command_buffer,
482 const SubpassCallback& subpass_callback,
483 bool msaa_enabled,
484 bool depth_stencil_enabled,
485 int32_t mip_count) const {
486 const std::shared_ptr<Context>& context = GetContext();
487 RenderTarget subpass_target;
488
489 std::optional<RenderTarget::AttachmentConfig> depth_stencil_config =
490 depth_stencil_enabled ? RenderTarget::kDefaultStencilAttachmentConfig
491 : std::optional<RenderTarget::AttachmentConfig>();
492
493 if (context->GetCapabilities()->SupportsOffscreenMSAA() && msaa_enabled) {
494 subpass_target = GetRenderTargetCache()->CreateOffscreenMSAA(
495 *context, texture_size,
496 /*mip_count=*/mip_count, SPrintF("%s Offscreen", label.data()),
497 RenderTarget::kDefaultColorAttachmentConfigMSAA, depth_stencil_config);
498 } else {
499 subpass_target = GetRenderTargetCache()->CreateOffscreen(
500 *context, texture_size,
501 /*mip_count=*/mip_count, SPrintF("%s Offscreen", label.data()),
502 RenderTarget::kDefaultColorAttachmentConfig, depth_stencil_config);
503 }
504 return MakeSubpass(label, subpass_target, command_buffer, subpass_callback);
505 }
506
507 fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
508 std::string_view label,
509 const RenderTarget& subpass_target,
510 const std::shared_ptr<CommandBuffer>& command_buffer,
511 const SubpassCallback& subpass_callback) const {
512 const std::shared_ptr<Context>& context = GetContext();
513
514 auto subpass_texture = subpass_target.GetRenderTargetTexture();
515 if (!subpass_texture) {
516 return fml::Status(fml::StatusCode::kUnknown, "");
517 }
518
519 auto sub_renderpass = command_buffer->CreateRenderPass(subpass_target);
520 if (!sub_renderpass) {
521 return fml::Status(fml::StatusCode::kUnknown, "");
522 }
523 sub_renderpass->SetLabel(SPrintF("%s RenderPass", label.data()));
524
525 if (!subpass_callback(*this, *sub_renderpass)) {
526 return fml::Status(fml::StatusCode::kUnknown, "");
527 }
528
529 if (!sub_renderpass->EncodeCommands()) {
530 return fml::Status(fml::StatusCode::kUnknown, "");
531 }
532
533 const std::shared_ptr<Texture>& target_texture =
534 subpass_target.GetRenderTargetTexture();
535 if (target_texture->GetMipCount() > 1) {
536 fml::Status mipmap_status =
537 AddMipmapGeneration(command_buffer, context, target_texture);
538 if (!mipmap_status.ok()) {
539 return mipmap_status;
540 }
541 }
542
543 return subpass_target;
544 }
545
546 std::shared_ptr<Tessellator> ContentContext::GetTessellator() const {
547 return tessellator_;
548 }
549
550 std::shared_ptr<Context> ContentContext::GetContext() const {
551 return context_;
552 }
553
554 const Capabilities& ContentContext::GetDeviceCapabilities() const {
555 return *context_->GetCapabilities();
556 }
557
558 void ContentContext::SetWireframe(bool wireframe) {
559 wireframe_ = wireframe;
560 }
561
562 std::shared_ptr<Pipeline<PipelineDescriptor>>
563 ContentContext::GetCachedRuntimeEffectPipeline(
564 const std::string& unique_entrypoint_name,
565 const ContentContextOptions& options,
566 const std::function<std::shared_ptr<Pipeline<PipelineDescriptor>>()>&
567 create_callback) const {
568 RuntimeEffectPipelineKey key{unique_entrypoint_name, options};
569 auto it = runtime_effect_pipelines_.find(key);
570 if (it == runtime_effect_pipelines_.end()) {
571 it = runtime_effect_pipelines_.insert(it, {key, create_callback()});
572 }
573 return it->second;
574 }
575
576 void ContentContext::ClearCachedRuntimeEffectPipeline(
577 const std::string& unique_entrypoint_name) const {
578 for (auto it = runtime_effect_pipelines_.begin();
579 it != runtime_effect_pipelines_.end();) {
580 if (it->first.unique_entrypoint_name == unique_entrypoint_name) {
581 it = runtime_effect_pipelines_.erase(it);
582 } else {
583 it++;
584 }
585 }
586 }
587
588 void ContentContext::InitializeCommonlyUsedShadersIfNeeded() const {
589 TRACE_EVENT0("flutter", "InitializeCommonlyUsedShadersIfNeeded");
590 GetContext()->InitializeCommonlyUsedShadersIfNeeded();
591 }
592
593 } // namespace impeller
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:24
impeller::Pipeline< PipelineDescriptor >
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
impeller::BlendSelectValues::kSaturation
@ kSaturation
impeller::ContentContext::ClearCachedRuntimeEffectPipeline
void ClearCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name) const
Definition: content_context.cc:576
impeller::ColorAttachmentDescriptor::src_color_blend_factor
BlendFactor src_color_blend_factor
Definition: formats.h:522
impeller::PipelineDescriptor::SetPolygonMode
void SetPolygonMode(PolygonMode mode)
Definition: pipeline_descriptor.cc:276
impeller::Entity::kLastPipelineBlendMode
static constexpr BlendMode kLastPipelineBlendMode
Definition: entity.h:22
impeller::BlendMode::kDestinationATop
@ kDestinationATop
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::BlendSelectValues::kColorBurn
@ kColorBurn
impeller::RenderTarget::kDefaultColorAttachmentConfig
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:55
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:616
impeller::ContentContext::ContentContext
ContentContext(std::shared_ptr< Context > context, std::shared_ptr< TypographerContext > typographer_context, std::shared_ptr< RenderTargetAllocator > render_target_allocator=nullptr)
Definition: content_context.cc:241
impeller::BlendSelectValues::kMultiply
@ kMultiply
impeller::AddMipmapGeneration
fml::Status AddMipmapGeneration(const std::shared_ptr< CommandBuffer > &command_buffer, const std::shared_ptr< Context > &context, const std::shared_ptr< Texture > &texture)
Adds a blit command to the render pass.
Definition: texture_mipmap.cc:11
impeller::ColorWriteMaskBits::kNone
@ kNone
impeller::PixelFormat::kA8UNormInt
@ kA8UNormInt
impeller::PipelineDescriptor::SetStencilAttachmentDescriptors
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
Definition: pipeline_descriptor.cc:157
entity.h
impeller::ContentContextOptions::StencilMode::kOverdrawPreventionIncrement
@ kOverdrawPreventionIncrement
impeller::PipelineDescriptor::SetColorAttachmentDescriptor
PipelineDescriptor & SetColorAttachmentDescriptor(size_t index, ColorAttachmentDescriptor desc)
Definition: pipeline_descriptor.cc:110
impeller::Tessellator
A utility that generates triangles of the specified fill type given a polyline. This happens on the C...
Definition: tessellator.h:31
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::BlendFactor::kOneMinusSourceAlpha
@ kOneMinusSourceAlpha
impeller::Context::GetCapabilities
virtual const std::shared_ptr< const Capabilities > & GetCapabilities() const =0
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
impeller::HostBuffer
Definition: host_buffer.h:28
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
impeller::StencilOperation::kKeep
@ kKeep
Don't modify the current stencil value.
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
impeller::PipelineDescriptor::GetColorAttachmentDescriptor
const ColorAttachmentDescriptor * GetColorAttachmentDescriptor(size_t index) const
Definition: pipeline_descriptor.cc:124
impeller::BlendMode::kSource
@ kSource
impeller::PipelineDescriptor::SetPrimitiveType
void SetPrimitiveType(PrimitiveType type)
Definition: pipeline_descriptor.cc:268
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::BlendMode::kDestination
@ kDestination
texture_descriptor.h
impeller::RenderTarget::kDefaultColorAttachmentConfigMSAA
static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA
Definition: render_target.h:61
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::ColorAttachmentDescriptor::alpha_blend_op
BlendOperation alpha_blend_op
Definition: formats.h:527
formats.h
impeller::ContentContextOptions::has_depth_stencil_attachments
bool has_depth_stencil_attachments
Definition: content_context.h:313
impeller::BlendMode::kDestinationOver
@ kDestinationOver
impeller::BlendMode::kPlus
@ kPlus
impeller::BlendSelectValues::kSoftLight
@ kSoftLight
impeller::ContentContextOptions::blend_mode
BlendMode blend_mode
Definition: content_context.h:308
typographer_context.h
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:128
impeller::BlendFactor::kDestinationAlpha
@ kDestinationAlpha
impeller::BlendSelectValues::kDifference
@ kDifference
impeller::ContentContextOptions::ApplyToPipelineDescriptor
void ApplyToPipelineDescriptor(PipelineDescriptor &desc) const
Definition: content_context.cc:28
impeller::StorageMode::kHostVisible
@ kHostVisible
framebuffer_blend_contents.h
impeller::ContentContextOptions::StencilMode::kStencilEvenOddFill
@ kStencilEvenOddFill
impeller::ContentContext::GetCachedRuntimeEffectPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name, const ContentContextOptions &options, const std::function< std::shared_ptr< Pipeline< PipelineDescriptor >>()> &create_callback) const
Definition: content_context.cc:563
validation.h
impeller::ContentContext::GetEmptyTexture
std::shared_ptr< Texture > GetEmptyTexture() const
Definition: content_context.cc:474
impeller::BlendMode::kModulate
@ kModulate
impeller::PolygonMode::kFill
@ kFill
impeller::ContentContextOptions::wireframe
bool wireframe
Definition: content_context.h:315
impeller::StencilOperation::kSetToReferenceValue
@ kSetToReferenceValue
Reset the stencil value to the reference value.
impeller::BlendFactor::kSourceColor
@ kSourceColor
impeller::BlendMode::kSourceOut
@ kSourceOut
tessellator.h
impeller::BlendFactor::kDestinationColor
@ kDestinationColor
impeller::BlendSelectValues::kDarken
@ kDarken
impeller::BlendSelectValues::kExclusion
@ kExclusion
impeller::BlendFactor::kZero
@ kZero
impeller::Capabilities
Definition: capabilities.h:14
impeller::ColorWriteMaskBits::kAll
@ kAll
impeller::TSize
Definition: size.h:19
impeller::StencilOperation::kDecrementWrap
@ kDecrementWrap
Decrement the current stencil value by 1. If at zero, set to maximum.
impeller::DepthAttachmentDescriptor::depth_write_enabled
bool depth_write_enabled
Definition: formats.h:598
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::ContentContextOptions::color_attachment_pixel_format
PixelFormat color_attachment_pixel_format
Definition: content_context.h:312
impeller::ContentContext::~ContentContext
~ContentContext()
impeller::RenderTarget::GetRenderTargetTexture
std::shared_ptr< Texture > GetRenderTargetTexture() const
Definition: render_target.cc:144
impeller::BlendOperation::kReverseSubtract
@ kReverseSubtract
impeller::BlendSelectValues::kColor
@ kColor
impeller::Context::BackendType::kOpenGLES
@ kOpenGLES
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::BlendMode::kClear
@ kClear
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:550
impeller::BlendOperation::kAdd
@ kAdd
impeller::RenderTarget::kDefaultStencilAttachmentConfig
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
Definition: render_target.h:68
impeller::ContentContextOptions::StencilMode::kStencilNonZeroFill
@ kStencilNonZeroFill
impeller::ContentContext::MakeSubpass
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...
Definition: content_context.cc:478
impeller::Color::ToR8G8B8A8
constexpr std::array< uint8_t, 4 > ToR8G8B8A8() const
Convert to R8G8B8A8 representation.
Definition: color.h:245
texture_mipmap.h
impeller::StencilAttachmentDescriptor::stencil_failure
StencilOperation stencil_failure
Definition: formats.h:620
impeller::ColorAttachmentDescriptor::format
PixelFormat format
Definition: formats.h:519
impeller::StencilOperation::kIncrementWrap
@ kIncrementWrap
Increment the current stencil value by 1. If at maximum, set to zero.
impeller::StencilAttachmentDescriptor::depth_stencil_pass
StencilOperation depth_stencil_pass
Definition: formats.h:629
impeller::RenderTarget
Definition: render_target.h:38
impeller::BlendFactor::kOne
@ kOne
impeller::ContentContextOptions::is_for_rrect_blur_clear
bool is_for_rrect_blur_clear
Definition: content_context.h:316
impeller::ContentContextOptions::stencil_mode
StencilMode stencil_mode
Definition: content_context.h:310
strings.h
impeller::ContentContextOptions::depth_write_enabled
bool depth_write_enabled
Definition: content_context.h:314
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
impeller::ContentContext::GetTessellator
std::shared_ptr< Tessellator > GetTessellator() const
Definition: content_context.cc:546
impeller::ContentContextOptions::primitive_type
PrimitiveType primitive_type
Definition: content_context.h:311
pipeline_library.h
impeller::ContentContext::SubpassCallback
std::function< bool(const ContentContext &, RenderPass &)> SubpassCallback
Definition: content_context.h:701
impeller::PipelineDescriptor::SetSampleCount
PipelineDescriptor & SetSampleCount(SampleCount samples)
Definition: pipeline_descriptor.cc:76
impeller::ColorAttachmentDescriptor::src_alpha_blend_factor
BlendFactor src_alpha_blend_factor
Definition: formats.h:526
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::BlendMode::kDestinationIn
@ kDestinationIn
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
command_buffer.h
impeller::ColorAttachmentDescriptor::dst_color_blend_factor
BlendFactor dst_color_blend_factor
Definition: formats.h:524
content_context.h
impeller::BlendMode::kDestinationOut
@ kDestinationOut
impeller::ContentContext::GetRenderTargetCache
const std::shared_ptr< RenderTargetAllocator > & GetRenderTargetCache() const
Definition: content_context.h:725
impeller::PipelineDescriptor::GetFrontStencilAttachmentDescriptor
std::optional< StencilAttachmentDescriptor > GetFrontStencilAttachmentDescriptor() const
Definition: pipeline_descriptor.cc:202
impeller::ColorAttachmentDescriptor::dst_alpha_blend_factor
BlendFactor dst_alpha_blend_factor
Definition: formats.h:528
impeller::ContentContextOptions::depth_compare
CompareFunction depth_compare
Definition: content_context.h:309
impeller::PipelineDescriptor::ClearDepthAttachment
void ClearDepthAttachment()
Definition: pipeline_descriptor.cc:176
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:554
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:46
std
Definition: comparable.h:95
impeller::BlendSelectValues::kHardLight
@ kHardLight
impeller::ContentContextOptions::sample_count
SampleCount sample_count
Definition: content_context.h:307
impeller::BlendSelectValues::kScreen
@ kScreen
impeller::DepthAttachmentDescriptor
Definition: formats.h:590
impeller::RenderTargetCache
An implementation of the [RenderTargetAllocator] that caches all allocated texture data for one frame...
Definition: render_target_cache.h:16
impeller::Color::BlackTransparent
static constexpr Color BlackTransparent()
Definition: color.h:269
impeller::StencilAttachmentDescriptor
Definition: formats.h:610
impeller::BlendSelectValues::kLighten
@ kLighten
impeller::BlendMode::kSourceIn
@ kSourceIn
impeller::PipelineDescriptor::GetDepthStencilAttachmentDescriptor
std::optional< DepthAttachmentDescriptor > GetDepthStencilAttachmentDescriptor() const
Definition: pipeline_descriptor.cc:207
impeller::ContentContext::SetWireframe
void SetWireframe(bool wireframe)
Definition: content_context.cc:558
impeller::BlendSelectValues::kHue
@ kHue
impeller::SampleCount::kCount4
@ kCount4
impeller::PolygonMode::kLine
@ kLine
impeller::BlendSelectValues::kLuminosity
@ kLuminosity
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::PipelineDescriptor::SetDepthStencilAttachmentDescriptor
PipelineDescriptor & SetDepthStencilAttachmentDescriptor(std::optional< DepthAttachmentDescriptor > desc)
Definition: pipeline_descriptor.cc:151
impeller::ContentContextOptions::StencilMode::kOverdrawPreventionRestore
@ kOverdrawPreventionRestore
impeller::ContentContextOptions::StencilMode::kCoverCompareInverted
@ kCoverCompareInverted
pipeline_descriptor.h
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::ContentContextOptions::StencilMode::kCoverCompare
@ kCoverCompare
impeller::ColorAttachmentDescriptor::color_blend_op
BlendOperation color_blend_op
Definition: formats.h:523
impeller::CompareFunction::kNotEqual
@ kNotEqual
Comparison test passes if new_value != current_value.
render_target.h
impeller::BlendMode::kXor
@ kXor
impeller::PipelineBuilder::MakeDefaultPipelineDescriptor
static std::optional< PipelineDescriptor > MakeDefaultPipelineDescriptor(const Context &context, const std::vector< Scalar > &constants={})
Create a default pipeline descriptor using the combination reflected shader information....
Definition: pipeline_builder.h:49
render_target_cache.h
impeller::BlendFactor::kSourceAlpha
@ kSourceAlpha
impeller::interop::Create
ScopedObject< Object > Create(CtorArgs &&... args)
Definition: object.h:160
impeller::ContentContextOptions
Definition: content_context.h:254
impeller::LazyGlyphAtlas
Definition: lazy_glyph_atlas.h:15
impeller::PipelineDescriptor::ClearStencilAttachments
void ClearStencilAttachments()
Definition: pipeline_descriptor.cc:170
impeller::CompareFunction::kLess
@ kLess
Comparison test passes if new_value < current_value.
impeller
Definition: allocation.cc:12
impeller::ContentContext::IsValid
bool IsValid() const
Definition: content_context.cc:470
impeller::BlendMode::kSourceATop
@ kSourceATop
impeller::ColorAttachmentDescriptor::blending_enabled
bool blending_enabled
Definition: formats.h:520
impeller::BlendSelectValues::kColorDodge
@ kColorDodge
impeller::CreateDefaultPipeline
static std::unique_ptr< PipelineT > CreateDefaultPipeline(const Context &context)
Definition: content_context.cc:225
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::BlendSelectValues::kOverlay
@ kOverlay
impeller::BlendFactor::kOneMinusDestinationAlpha
@ kOneMinusDestinationAlpha
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:753
impeller::DepthAttachmentDescriptor::depth_compare
CompareFunction depth_compare
Definition: formats.h:594
impeller::ColorAttachmentDescriptor::write_mask
ColorWriteMask write_mask
Definition: formats.h:530
impeller::ColorAttachmentDescriptor
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:518