Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
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"
24
25namespace impeller {
26
27BlitPassMTL::BlitPassMTL(id<MTLCommandBuffer> buffer) : buffer_(buffer) {
28 if (!buffer_) {
29 return;
30 }
31 is_valid_ = true;
32}
33
34BlitPassMTL::~BlitPassMTL() = default;
35
36bool BlitPassMTL::IsValid() const {
37 return is_valid_;
38}
39
40void BlitPassMTL::OnSetLabel(std::string label) {
41 if (label.empty()) {
42 return;
43 }
44 label_ = std::move(label);
45}
46
47bool 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.
67 [blit_command_encoder]() { [blit_command_encoder endEncoding]; });
68
69 return EncodeCommands(blit_command_encoder);
70}
71
72bool 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|
91bool 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|
109bool 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
126bool BlitPassMTL::OnCopyBufferToTextureCommand(
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|
142bool 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
Wraps a closure that is invoked in the destructor unless released by the caller.
Definition closure.h:32
SkBitmap source
Definition examples.cpp:28
static const uint8_t buffer[]
FlTexture * texture
std::function< void()> closure
Definition closure.h:14
list command
Definition valgrind.py:24
#define TRACE_EVENT0(category_group, name)