12 #include "fml/logging.h"
13 #include "fml/trace_event.h"
19 #include "vulkan/vulkan.hpp"
26 bool enable_gpu_tracing)
27 : context_(
std::move(context)) {
28 if (!enable_gpu_tracing) {
31 timestamp_period_ = context_.lock()
35 .limits.timestampPeriod;
36 if (timestamp_period_ <= 0) {
43 #endif // IMPELLER_DEBUG
50 Lock lock(trace_state_mutex_);
54 for (
auto i = 0u; i < kTraceStatesSize; i++) {
55 vk::QueryPoolCreateInfo info;
57 info.queryType = vk::QueryType::eTimestamp;
59 auto [status, pool] = context.
GetDevice().createQueryPoolUnique(info);
60 if (status != vk::Result::eSuccess) {
64 trace_states_[i].query_pool = std::move(pool);
65 buffer_vk.
GetEncoder()->GetCommandBuffer().resetQueryPool(
66 trace_states_[i].query_pool.get(), 0,
kPoolSize);
82 FML_DCHECK(!in_frame_);
84 raster_thread_id_ = std::this_thread::get_id();
94 Lock lock(trace_state_mutex_);
95 current_state_ = (current_state_ + 1) % kTraceStatesSize;
97 auto& state = trace_states_[current_state_];
103 FML_DCHECK(state.pending_buffers == 0u);
104 state.pending_buffers = 0;
105 state.current_index = 0;
109 return std::make_unique<GPUProbe>(weak_from_this());
112 void GPUTracerVK::RecordCmdBufferStart(
const vk::CommandBuffer& buffer,
114 if (!enabled_ || std::this_thread::get_id() != raster_thread_id_ ||
118 Lock lock(trace_state_mutex_);
119 auto& state = trace_states_[current_state_];
122 if (!states_to_reset_.empty()) {
123 for (
auto i = 0u; i < states_to_reset_.size(); i++) {
124 buffer.resetQueryPool(trace_states_[states_to_reset_[i]].query_pool.get(),
127 states_to_reset_.clear();
137 buffer.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe,
138 trace_states_[current_state_].query_pool.get(),
139 state.current_index);
140 state.current_index += 1;
141 probe.index_ = current_state_;
142 state.pending_buffers += 1;
145 void GPUTracerVK::RecordCmdBufferEnd(
const vk::CommandBuffer& buffer,
147 if (!enabled_ || std::this_thread::get_id() != raster_thread_id_ ||
148 !in_frame_ || !probe.index_.has_value()) {
151 Lock lock(trace_state_mutex_);
152 GPUTraceState& state = trace_states_[current_state_];
158 buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe,
159 state.query_pool.get(), state.current_index);
161 state.current_index += 1;
164 void GPUTracerVK::OnFenceComplete(
size_t frame_index) {
170 size_t query_count = 0;
173 Lock lock(trace_state_mutex_);
174 GPUTraceState& state = trace_states_[frame_index];
176 FML_DCHECK(state.pending_buffers > 0);
177 state.pending_buffers -= 1;
178 pending = state.pending_buffers;
179 query_count = state.current_index;
180 pool = state.query_pool.get();
184 std::vector<uint64_t> bits(query_count);
185 std::shared_ptr<ContextVK> context = context_.lock();
190 auto result = context->GetDevice().getQueryPoolResults(
191 pool, 0, query_count, query_count *
sizeof(uint64_t), bits.data(),
192 sizeof(uint64_t), vk::QueryResultFlagBits::e64);
201 if (result == vk::Result::eSuccess) {
202 uint64_t smallest_timestamp = std::numeric_limits<uint64_t>::max();
203 uint64_t largest_timestamp = 0;
204 for (
auto i = 0u; i < bits.size(); i++) {
205 smallest_timestamp = std::min(smallest_timestamp, bits[i]);
206 largest_timestamp = std::max(largest_timestamp, bits[i]);
209 (((largest_timestamp - smallest_timestamp) * timestamp_period_) /
211 FML_TRACE_COUNTER(
"flutter",
"GPUTracer",
212 reinterpret_cast<int64_t
>(
this),
213 "FrameTimeMS", gpu_ms);
217 Lock lock(trace_state_mutex_);
218 states_to_reset_.push_back(frame_index);
226 if (!index_.has_value()) {
229 auto tracer = tracer_.lock();
233 tracer->OnFenceComplete(index_.value());
237 auto tracer = tracer_.lock();
241 tracer->RecordCmdBufferStart(buffer, *
this);
245 auto tracer = tracer_.lock();
249 tracer->RecordCmdBufferEnd(buffer, *
this);