| Flutter Impeller |

gpu_tracer_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 <Metal/Metal.h>
6 #include "fml/trace_event.h"
7 #include "impeller/renderer/backend/metal/context_mtl.h"
8 #include "impeller/renderer/backend/metal/formats_mtl.h"
9
10 #include <memory>
11
12 #include "impeller/renderer/backend/metal/gpu_tracer_mtl.h"
13
14 namespace impeller {
15
16 void GPUTracerMTL::MarkFrameEnd() {
17 if (@available(ios 10.3, tvos 10.2, macos 10.15, macCatalyst 13.0, *)) {
18 Lock lock(trace_state_mutex_);
19 current_state_ = (current_state_ + 1) % 16;
20 }
21 }
22
23 void GPUTracerMTL::RecordCmdBuffer(id<MTLCommandBuffer> buffer) {
24 if (@available(ios 10.3, tvos 10.2, macos 10.15, macCatalyst 13.0, *)) {
25 Lock lock(trace_state_mutex_);
26 auto current_state = current_state_;
27 trace_states_[current_state].pending_buffers += 1;
28
29 auto weak_self = weak_from_this();
30 [buffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
31 auto self = weak_self.lock();
32 if (!self) {
33 return;
34 }
35 Lock lock(self->trace_state_mutex_);
36 auto& state = self->trace_states_[current_state];
37 state.pending_buffers--;
38 state.smallest_timestamp = std::min(
39 state.smallest_timestamp, static_cast<Scalar>(buffer.GPUStartTime));
40 state.largest_timestamp = std::max(
41 state.largest_timestamp, static_cast<Scalar>(buffer.GPUEndTime));
42
43 if (state.pending_buffers == 0) {
44 auto gpu_ms =
45 (state.largest_timestamp - state.smallest_timestamp) * 1000;
46 state.smallest_timestamp = std::numeric_limits<float>::max();
47 state.largest_timestamp = 0;
48 FML_TRACE_COUNTER("flutter", "GPUTracer",
49 reinterpret_cast<int64_t>(this), // Trace Counter ID
50 "FrameTimeMS", gpu_ms);
51 }
52 }];
53 }
54 }
55
56 } // namespace impeller
impeller::Scalar
float Scalar
Definition: scalar.h:18
context_mtl.h
impeller::GPUTracerMTL::MarkFrameEnd
void MarkFrameEnd()
Record that the current frame has ended. Any additional cmd buffers will be attributed to the "next" ...
Definition: gpu_tracer_mtl.mm:16
impeller::Lock
Definition: thread.h:75
formats_mtl.h
impeller::GPUTracerMTL::RecordCmdBuffer
void RecordCmdBuffer(id< MTLCommandBuffer > buffer)
Record the current cmd buffer GPU execution timestamps into an aggregate frame workload metric.
Definition: gpu_tracer_mtl.mm:23
gpu_tracer_mtl.h
impeller
Definition: aiks_blur_unittests.cc:20