| Flutter Impeller |

scene_contents.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/scene_contents.h"
6
7 #include "impeller/core/formats.h"
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/contents/tiled_texture_contents.h"
10 #include "impeller/entity/entity.h"
11 #include "impeller/geometry/path_builder.h"
12 #include "impeller/scene/camera.h"
13 #include "impeller/scene/scene.h"
14
15 namespace impeller {
16
17 SceneContents::SceneContents() = default;
18
19 SceneContents::~SceneContents() = default;
20
21 void SceneContents::SetCameraTransform(Matrix matrix) {
22 camera_transform_ = matrix;
23 }
24
25 void SceneContents::SetNode(std::shared_ptr<scene::Node> node) {
26 node_ = std::move(node);
27 }
28
29 bool SceneContents::Render(const ContentContext& renderer,
30 const Entity& entity,
31 RenderPass& pass) const {
32 if (!node_) {
33 return true;
34 }
35
36 auto coverage = GetCoverage(entity);
37 if (!coverage.has_value()) {
38 return true;
39 }
40
41 // This happens for CoverGeometry (DrawPaint). In this situation,
42 // Draw the scene to the full layer.
43 if (coverage.value().IsMaximum()) {
44 coverage = Rect::MakeSize(pass.GetRenderTargetSize());
45 }
46
47 RenderTarget subpass_target;
48 if (renderer.GetContext()->GetCapabilities()->SupportsOffscreenMSAA()) {
49 subpass_target = renderer.GetRenderTargetCache()->CreateOffscreenMSAA(
50 *renderer.GetContext(), // context
51 ISize(coverage.value().GetSize()), // size
52 /*mip_count=*/1,
53 "SceneContents", // label
54 RenderTarget::AttachmentConfigMSAA{
55 .storage_mode = StorageMode::kDeviceTransient,
56 .resolve_storage_mode = StorageMode::kDevicePrivate,
57 .load_action = LoadAction::kClear,
58 .store_action = StoreAction::kMultisampleResolve,
59 }, // color_attachment_config
60 RenderTarget::AttachmentConfig{
61 .storage_mode = StorageMode::kDeviceTransient,
62 .load_action = LoadAction::kDontCare,
63 .store_action = StoreAction::kDontCare,
64 } // stencil_attachment_config
65 );
66 } else {
67 subpass_target = renderer.GetRenderTargetCache()->CreateOffscreen(
68 *renderer.GetContext(), // context
69 ISize(coverage.value().GetSize()), // size
70 /*mip_count=*/1,
71 "SceneContents", // label
72 RenderTarget::AttachmentConfig{
73 .storage_mode = StorageMode::kDevicePrivate,
74 .load_action = LoadAction::kClear,
75 .store_action = StoreAction::kStore,
76 }, // color_attachment_config
77 RenderTarget::AttachmentConfig{
78 .storage_mode = StorageMode::kDeviceTransient,
79 .load_action = LoadAction::kClear,
80 .store_action = StoreAction::kDontCare,
81 } // stencil_attachment_config
82 );
83 }
84
85 if (!subpass_target.IsValid()) {
86 return false;
87 }
88
89 scene::Scene scene(renderer.GetSceneContext());
90 scene.GetRoot().AddChild(node_);
91
92 if (!scene.Render(subpass_target, camera_transform_)) {
93 return false;
94 }
95
96 // Render the texture to the pass.
97 TiledTextureContents contents;
98 contents.SetGeometry(GetGeometry());
99 contents.SetTexture(subpass_target.GetRenderTargetTexture());
100 contents.SetEffectTransform(
101 Matrix::MakeScale(1 / entity.GetTransform().GetScale()));
102 return contents.Render(renderer, entity, pass);
103 }
104
105 } // namespace impeller
impeller::SceneContents::SetCameraTransform
void SetCameraTransform(Matrix matrix)
Definition: scene_contents.cc:21
impeller::ColorSourceContents::SetGeometry
void SetGeometry(std::shared_ptr< Geometry > geometry)
Set the geometry that this contents will use to render.
Definition: color_source_contents.cc:16
impeller::ColorSourceContents::SetEffectTransform
void SetEffectTransform(Matrix matrix)
Set the effect transform for this color source.
Definition: color_source_contents.cc:32
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
entity.h
formats.h
tiled_texture_contents.h
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
impeller::SceneContents::SetNode
void SetNode(std::shared_ptr< scene::Node > node)
Definition: scene_contents.cc:25
impeller::SceneContents::SceneContents
SceneContents()
impeller::scene::Scene::GetRoot
Node & GetRoot()
Definition: scene.cc:30
impeller::RenderTarget::AttachmentConfigMSAA
Definition: render_target.h:47
impeller::RenderTarget::AttachmentConfig
Definition: render_target.h:40
path_builder.h
impeller::Entity
Definition: entity.h:21
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:43
impeller::TSize< int64_t >
scene.h
impeller::RenderTarget::GetRenderTargetTexture
std::shared_ptr< Texture > GetRenderTargetTexture() const
Definition: render_target.cc:144
scene_contents.h
impeller::SceneContents::~SceneContents
~SceneContents() override
impeller::Matrix::GetScale
constexpr Vector3 GetScale() const
Definition: matrix.h:306
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:564
impeller::RenderTarget
Definition: render_target.h:38
impeller::SceneContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: scene_contents.cc:29
impeller::RenderTarget::IsValid
bool IsValid() const
Definition: render_target.cc:23
camera.h
impeller::scene::Node::AddChild
bool AddChild(std::shared_ptr< Node > child)
Definition: node.cc:300
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
content_context.h
impeller::ContentContext::GetRenderTargetCache
const std::shared_ptr< RenderTargetAllocator > & GetRenderTargetCache() const
Definition: content_context.h:805
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: color_source_contents.cc:44
impeller::TiledTextureContents
Definition: tiled_texture_contents.h:22
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
impeller::TiledTextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: tiled_texture_contents.cc:111
impeller::scene::Scene
Definition: scene.h:21
impeller::scene::Scene::Render
bool Render(const RenderTarget &render_target, const Matrix &camera_transform)
Definition: scene.cc:34
impeller
Definition: aiks_blur_unittests.cc:20
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::ContentContext
Definition: content_context.h:392
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::TiledTextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: tiled_texture_contents.cc:41