| Flutter Impeller |

blit_pass.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/renderer/blit_pass.h"
6 #include <memory>
7 #include <utility>
8
9 #include "impeller/base/strings.h"
10 #include "impeller/base/validation.h"
11 #include "impeller/core/formats.h"
12
13 namespace impeller {
14
15 BlitPass::BlitPass() {}
16
17 BlitPass::~BlitPass() = default;
18
19 void BlitPass::SetLabel(std::string label) {
20 if (label.empty()) {
21 return;
22 }
23 OnSetLabel(std::move(label));
24 }
25
26 bool BlitPass::AddCopy(std::shared_ptr<Texture> source,
27 std::shared_ptr<Texture> destination,
28 std::optional<IRect> source_region,
29 IPoint destination_origin,
30 std::string label) {
31 if (!source) {
32 VALIDATION_LOG << "Attempted to add a texture blit with no source.";
33 return false;
34 }
35 if (!destination) {
36 VALIDATION_LOG << "Attempted to add a texture blit with no destination.";
37 return false;
38 }
39
40 if (source->GetTextureDescriptor().sample_count !=
41 destination->GetTextureDescriptor().sample_count) {
42 VALIDATION_LOG << SPrintF(
43 "The source sample count (%d) must match the destination sample count "
44 "(%d) for blits.",
45 static_cast<int>(source->GetTextureDescriptor().sample_count),
46 static_cast<int>(destination->GetTextureDescriptor().sample_count));
47 return false;
48 }
49 if (source->GetTextureDescriptor().format !=
50 destination->GetTextureDescriptor().format) {
51 VALIDATION_LOG << SPrintF(
52 "The source pixel format (%s) must match the destination pixel format "
53 "(%s) "
54 "for blits.",
55 PixelFormatToString(source->GetTextureDescriptor().format),
56 PixelFormatToString(destination->GetTextureDescriptor().format));
57 return false;
58 }
59
60 if (!source_region.has_value()) {
61 source_region = IRect::MakeSize(source->GetSize());
62 }
63
64 // Clip the source image.
65 source_region =
66 source_region->Intersection(IRect::MakeSize(source->GetSize()));
67 if (!source_region.has_value()) {
68 return true; // Nothing to blit.
69 }
70
71 // Clip the destination image.
72 source_region = source_region->Intersection(
73 IRect::MakeOriginSize(-destination_origin, destination->GetSize()));
74 if (!source_region.has_value()) {
75 return true; // Nothing to blit.
76 }
77
78 return OnCopyTextureToTextureCommand(
79 std::move(source), std::move(destination), source_region.value(),
80 destination_origin, std::move(label));
81 }
82
83 bool BlitPass::AddCopy(std::shared_ptr<Texture> source,
84 std::shared_ptr<DeviceBuffer> destination,
85 std::optional<IRect> source_region,
86 size_t destination_offset,
87 std::string label) {
88 if (!source) {
89 VALIDATION_LOG << "Attempted to add a texture blit with no source.";
90 return false;
91 }
92 if (!destination) {
93 VALIDATION_LOG << "Attempted to add a texture blit with no destination.";
94 return false;
95 }
96
97 if (!source_region.has_value()) {
98 source_region = IRect::MakeSize(source->GetSize());
99 }
100
101 auto bytes_per_pixel =
102 BytesPerPixelForPixelFormat(source->GetTextureDescriptor().format);
103 auto bytes_per_image = source_region->Area() * bytes_per_pixel;
104 if (destination_offset + bytes_per_image >
105 destination->GetDeviceBufferDescriptor().size) {
106 VALIDATION_LOG
107 << "Attempted to add a texture blit with out of bounds access.";
108 return false;
109 }
110
111 // Clip the source image.
112 source_region =
113 source_region->Intersection(IRect::MakeSize(source->GetSize()));
114 if (!source_region.has_value()) {
115 return true; // Nothing to blit.
116 }
117
118 return OnCopyTextureToBufferCommand(std::move(source), std::move(destination),
119 source_region.value(), destination_offset,
120 std::move(label));
121 }
122
123 bool BlitPass::AddCopy(BufferView source,
124 std::shared_ptr<Texture> destination,
125 IPoint destination_origin,
126 std::string label) {
127 if (!destination) {
128 VALIDATION_LOG << "Attempted to add a texture blit with no destination.";
129 return false;
130 }
131
132 auto bytes_per_pixel =
133 BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format);
134 auto bytes_per_image =
135 destination->GetTextureDescriptor().size.Area() * bytes_per_pixel;
136
137 if (source.range.length != bytes_per_image) {
138 VALIDATION_LOG
139 << "Attempted to add a texture blit with out of bounds access.";
140 return false;
141 }
142
143 return OnCopyBufferToTextureCommand(std::move(source), std::move(destination),
144 destination_origin, std::move(label));
145 }
146
147 bool BlitPass::GenerateMipmap(std::shared_ptr<Texture> texture,
148 std::string label) {
149 if (!texture) {
150 VALIDATION_LOG << "Attempted to add an invalid mipmap generation command "
151 "with no texture.";
152 return false;
153 }
154
155 return OnGenerateMipmapCommand(std::move(texture), std::move(label));
156 }
157
158 } // namespace impeller
impeller::BlitPass::OnCopyTextureToBufferCommand
virtual bool OnCopyTextureToBufferCommand(std::shared_ptr< Texture > source, std::shared_ptr< DeviceBuffer > destination, IRect source_region, size_t destination_offset, std::string label)=0
impeller::PixelFormatToString
constexpr const char * PixelFormatToString(PixelFormat format)
Definition: formats.h:141
formats.h
impeller::BlitPass::~BlitPass
virtual ~BlitPass()
impeller::BlitPass::AddCopy
bool AddCopy(std::shared_ptr< Texture > source, std::shared_ptr< Texture > destination, std::optional< IRect > source_region=std::nullopt, IPoint destination_origin={}, std::string label="")
Record a command to copy the contents of one texture to another texture. The blit area is limited by ...
Definition: blit_pass.cc:26
impeller::BlitPass::BlitPass
BlitPass()
Definition: blit_pass.cc:15
impeller::BufferView::range
Range range
Definition: buffer_view.h:17
impeller::BlitPass::GenerateMipmap
bool GenerateMipmap(std::shared_ptr< Texture > texture, std::string label="")
Record a command to generate all mip levels for a texture. No work is encoded into the command buffer...
Definition: blit_pass.cc:147
impeller::BlitPass::OnCopyTextureToTextureCommand
virtual bool OnCopyTextureToTextureCommand(std::shared_ptr< Texture > source, std::shared_ptr< Texture > destination, IRect source_region, IPoint destination_origin, std::string label)=0
validation.h
impeller::BlitPass::OnCopyBufferToTextureCommand
virtual bool OnCopyBufferToTextureCommand(BufferView source, std::shared_ptr< Texture > destination, IPoint destination_origin, std::string label)=0
impeller::BytesPerPixelForPixelFormat
constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format)
Definition: formats.h:448
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::TRect< int64_t >::MakeOriginSize
constexpr static TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition: rect.h:140
blit_pass.h
impeller::BlitPass::OnGenerateMipmapCommand
virtual bool OnGenerateMipmapCommand(std::shared_ptr< Texture > texture, std::string label)=0
impeller::BlitPass::OnSetLabel
virtual void OnSetLabel(std::string label)=0
strings.h
impeller::BlitPass::SetLabel
void SetLabel(std::string label)
Definition: blit_pass.cc:19
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::BufferView
Definition: buffer_view.h:15
impeller::TRect< int64_t >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
impeller::TPoint< int64_t >
impeller::Range::length
size_t length
Definition: range.h:16
impeller
Definition: aiks_blur_unittests.cc:20