Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
6
7#include <memory>
8#include <optional>
9
14
15namespace impeller {
16
17std::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
31std::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
40std::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
bool Render(const Picture &picture, RenderTarget &render_target, bool reset_host_buffer)
std::shared_ptr< Context > GetContext() const
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
uint32_t * target
#define FML_DCHECK(condition)
Definition logging.h:103
FlTexture * texture
TSize< int64_t > ISize
Definition size.h:138
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition matrix.h:95
std::shared_ptr< Image > ToImage(AiksContext &context, ISize size) const
Definition picture.cc:31
std::unique_ptr< EntityPass > pass
Definition picture.h:21
std::optional< Snapshot > Snapshot(AiksContext &context)
Definition picture.cc:17
Represents a texture and its intended draw transform/sampler configuration.
Definition snapshot.h:24
std::shared_ptr< Texture > texture
Definition snapshot.h:25
#define VALIDATION_LOG
Definition validation.h:73