| Flutter Impeller |

blit_pass_mtl.mm
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/backend/metal/blit_pass_mtl.h"
6 #include <Metal/Metal.h>
7 #include <memory>
8 #include <variant>
9
10 #include "flutter/fml/closure.h"
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/trace_event.h"
13 #include "impeller/base/backend_cast.h"
14 #include "impeller/core/formats.h"
15 #include "impeller/core/host_buffer.h"
16 #include "impeller/core/shader_types.h"
17 #include "impeller/renderer/backend/metal/blit_command_mtl.h"
18 #include "impeller/renderer/backend/metal/device_buffer_mtl.h"
19 #include "impeller/renderer/backend/metal/formats_mtl.h"
20 #include "impeller/renderer/backend/metal/pipeline_mtl.h"
21 #include "impeller/renderer/backend/metal/sampler_mtl.h"
22 #include "impeller/renderer/backend/metal/texture_mtl.h"
23 #include "impeller/renderer/blit_command.h"
24
25 namespace impeller {
26
27 BlitPassMTL::BlitPassMTL(id<MTLCommandBuffer> buffer) : buffer_(buffer) {
28 if (!buffer_) {
29 return;
30 }
31 is_valid_ = true;
32 }
33
34 BlitPassMTL::~BlitPassMTL() = default;
35
36 bool BlitPassMTL::IsValid() const {
37 return is_valid_;
38 }
39
40 void BlitPassMTL::OnSetLabel(std::string label) {
41 if (label.empty()) {
42 return;
43 }
44 label_ = std::move(label);
45 }
46
47 bool BlitPassMTL::EncodeCommands(
48 const std::shared_ptr<Allocator>& transients_allocator) const {
49 TRACE_EVENT0("impeller", "BlitPassMTL::EncodeCommands");
50 if (!IsValid()) {
51 return false;
52 }
53
54 auto blit_command_encoder = [buffer_ blitCommandEncoder];
55
56 if (!blit_command_encoder) {
57 return false;
58 }
59
60 if (!label_.empty()) {
61 [blit_command_encoder setLabel:@(label_.c_str())];
62 }
63
64 // Success or failure, the pass must end. The buffer can only process one pass
65 // at a time.
66 fml::ScopedCleanupClosure auto_end(
67 [blit_command_encoder]() { [blit_command_encoder endEncoding]; });
68
69 return EncodeCommands(blit_command_encoder);
70 }
71
72 bool BlitPassMTL::EncodeCommands(id<MTLBlitCommandEncoder> encoder) const {
73 fml::closure pop_debug_marker = [encoder]() { [encoder popDebugGroup]; };
74 for (const auto& command : commands_) {
75 fml::ScopedCleanupClosure auto_pop_debug_marker(pop_debug_marker);
76 auto label = command->GetLabel();
77 if (!label.empty()) {
78 [encoder pushDebugGroup:@(label.c_str())];
79 } else {
80 auto_pop_debug_marker.Release();
81 }
82
83 if (!command->Encode(encoder)) {
84 return false;
85 }
86 }
87 return true;
88 }
89
90 // |BlitPass|
91 bool BlitPassMTL::OnCopyTextureToTextureCommand(
92 std::shared_ptr<Texture> source,
93 std::shared_ptr<Texture> destination,
94 IRect source_region,
95 IPoint destination_origin,
96 std::string label) {
97 auto command = std::make_unique<BlitCopyTextureToTextureCommandMTL>();
98 command->label = label;
99 command->source = std::move(source);
100 command->destination = std::move(destination);
101 command->source_region = source_region;
102 command->destination_origin = destination_origin;
103
104 commands_.emplace_back(std::move(command));
105 return true;
106 }
107
108 // |BlitPass|
109 bool BlitPassMTL::OnCopyTextureToBufferCommand(
110 std::shared_ptr<Texture> source,
111 std::shared_ptr<DeviceBuffer> destination,
112 IRect source_region,
113 size_t destination_offset,
114 std::string label) {
115 auto command = std::make_unique<BlitCopyTextureToBufferCommandMTL>();
116 command->label = label;
117 command->source = std::move(source);
118 command->destination = std::move(destination);
119 command->source_region = source_region;
120 command->destination_offset = destination_offset;
121
122 commands_.emplace_back(std::move(command));
123 return true;
124 }
125
126 bool BlitPassMTL::OnCopyBufferToTextureCommand(
127 BufferView source,
128 std::shared_ptr<Texture> destination,
129 IPoint destination_origin,
130 std::string label) {
131 auto command = std::make_unique<BlitCopyBufferToTextureCommandMTL>();
132 command->label = label;
133 command->source = std::move(source);
134 command->destination = std::move(destination);
135 command->destination_origin = destination_origin;
136
137 commands_.emplace_back(std::move(command));
138 return true;
139 }
140
141 // |BlitPass|
142 bool BlitPassMTL::OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
143 std::string label) {
144 auto command = std::make_unique<BlitGenerateMipmapCommandMTL>();
145 command->label = label;
146 command->texture = std::move(texture);
147
148 commands_.emplace_back(std::move(command));
149 return true;
150 }
151
152 } // namespace impeller
host_buffer.h
impeller::BlitPassMTL::~BlitPassMTL
~BlitPassMTL() override
blit_command.h
formats.h
formats_mtl.h
blit_command_mtl.h
pipeline_mtl.h
blit_pass_mtl.h
backend_cast.h
impeller::IRect
TRect< int64_t > IRect
Definition: rect.h:662
impeller::IPoint
TPoint< int64_t > IPoint
Definition: point.h:317
texture_mtl.h
sampler_mtl.h
shader_types.h
impeller
Definition: aiks_blur_unittests.cc:20
device_buffer_mtl.h