| Flutter Impeller |

picture.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/aiks/picture.h"
6
7 #include <memory>
8 #include <optional>
9
10 #include "impeller/base/validation.h"
11 #include "impeller/entity/entity.h"
12 #include "impeller/renderer/render_target.h"
13 #include "impeller/renderer/snapshot.h"
14
15 namespace impeller {
16
17 std::optional<Snapshot> Picture::Snapshot(AiksContext& context) {
18 auto coverage = pass->GetElementsCoverage(std::nullopt);
19 if (!coverage.has_value() || coverage->IsEmpty()) {
20 return std::nullopt;
21 }
22
23 const auto translate = Matrix::MakeTranslation(-coverage->GetOrigin());
24 auto texture =
25 RenderToTexture(context, ISize(coverage->GetSize()), translate);
26 return impeller::Snapshot{
27 .texture = std::move(texture),
28 .transform = Matrix::MakeTranslation(coverage->GetOrigin())};
29 }
30
31 std::shared_ptr<Image> Picture::ToImage(AiksContext& context,
32 ISize size) const {
33 if (size.IsEmpty()) {
34 return nullptr;
35 }
36 auto texture = RenderToTexture(context, size);
37 return texture ? std::make_shared<Image>(texture) : nullptr;
38 }
39
40 std::shared_ptr<Texture> Picture::RenderToTexture(
41 AiksContext& context,
42 ISize size,
43 std::optional<const Matrix> translate) const {
44 FML_DCHECK(!size.IsEmpty());
45
46 pass->IterateAllEntities([&translate](auto& entity) -> bool {
47 auto matrix = translate.has_value()
48 ? translate.value() * entity.GetTransform()
49 : entity.GetTransform();
50 entity.SetTransform(matrix);
51 return true;
52 });
53
54 // This texture isn't host visible, but we might want to add host visible
55 // features to Image someday.
56 const std::shared_ptr<Context>& impeller_context = context.GetContext();
57 // Do not use the render target cache as the lifecycle of this texture
58 // will outlive a particular frame.
59 RenderTargetAllocator render_target_allocator =
60 RenderTargetAllocator(impeller_context->GetResourceAllocator());
61 RenderTarget target;
62 if (impeller_context->GetCapabilities()->SupportsOffscreenMSAA()) {
63 target = render_target_allocator.CreateOffscreenMSAA(
64 *impeller_context, // context
65 size, // size
66 /*mip_count=*/1,
67 "Picture Snapshot MSAA", // label
68 RenderTarget::
69 kDefaultColorAttachmentConfigMSAA // color_attachment_config
70 );
71 } else {
72 target = render_target_allocator.CreateOffscreen(
73 *impeller_context, // context
74 size, // size
75 /*mip_count=*/1,
76 "Picture Snapshot", // label
77 RenderTarget::kDefaultColorAttachmentConfig // color_attachment_config
78 );
79 }
80 if (!target.IsValid()) {
81 VALIDATION_LOG << "Could not create valid RenderTarget.";
82 return nullptr;
83 }
84
85 if (!context.Render(*this, target, false)) {
86 VALIDATION_LOG << "Could not render Picture to Texture.";
87 return nullptr;
88 }
89
90 auto texture = target.GetRenderTargetTexture();
91 if (!texture) {
92 VALIDATION_LOG << "RenderTarget has no target texture.";
93 return nullptr;
94 }
95
96 return texture;
97 }
98
99 } // namespace impeller
impeller::Picture::pass
std::unique_ptr< EntityPass > pass
Definition: picture.h:21
impeller::AiksContext
Definition: aiks_context.h:20
impeller::RenderTarget::kDefaultColorAttachmentConfig
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:55
entity.h
impeller::AiksContext::Render
bool Render(const Picture &picture, RenderTarget &render_target, bool reset_host_buffer)
Definition: aiks_context.cc:48
picture.h
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
validation.h
impeller::TSize< int64_t >
impeller::Picture::Snapshot
std::optional< Snapshot > Snapshot(AiksContext &context)
Definition: picture.cc:17
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:25
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:138
impeller::AiksContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: aiks_context.cc:40
impeller::Picture::ToImage
std::shared_ptr< Image > ToImage(AiksContext &context, ISize size) const
Definition: picture.cc:31
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
snapshot.h
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: size.h:105
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:26
render_target.h
impeller
Definition: aiks_blur_unittests.cc:20