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