Flutter Engine
The Flutter Engine
GpuTimer.h
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GpuTimer_DEFINED
9#define GpuTimer_DEFINED
10
12
13#include <chrono>
14
15namespace sk_gpu_test {
16
17using PlatformTimerQuery = uint64_t;
19
20/**
21 * Platform-independent interface for timing operations on the GPU.
22 */
23class GpuTimer {
24public:
26 : fDisjointSupport(disjointSupport)
27 , fActiveTimer(kInvalidTimerQuery) {
28 }
29 virtual ~GpuTimer() { SkASSERT(!fActiveTimer); }
30
31 /**
32 * Returns whether this timer can detect disjoint GPU operations while timing. If false, a query
33 * has less confidence when it completes with QueryStatus::kAccurate.
34 */
35 bool disjointSupport() const { return fDisjointSupport; }
36
37 /**
38 * Inserts a "start timing" command in the GPU command stream.
39 */
40 void queueStart() {
41 SkASSERT(!fActiveTimer);
42 fActiveTimer = this->onQueueTimerStart();
43 }
44
45 /**
46 * Inserts a "stop timing" command in the GPU command stream.
47 *
48 * @return a query object that can retrieve the time elapsed once the timer has completed.
49 */
50 [[nodiscard]] PlatformTimerQuery queueStop() {
51 SkASSERT(fActiveTimer);
52 this->onQueueTimerStop(fActiveTimer);
53 return std::exchange(fActiveTimer, kInvalidTimerQuery);
54 }
55
56 enum class QueryStatus {
57 kInvalid, //<! the timer query is invalid.
58 kPending, //<! the timer is still running on the GPU.
59 kDisjoint, //<! the query is complete, but dubious due to disjoint GPU operations.
60 kAccurate //<! the query is complete and reliable.
61 };
62
64 virtual std::chrono::nanoseconds getTimeElapsed(PlatformTimerQuery) = 0;
66
67private:
69 virtual void onQueueTimerStop(PlatformTimerQuery) const = 0;
70
71 bool const fDisjointSupport;
72 PlatformTimerQuery fActiveTimer;
73};
74
75} // namespace sk_gpu_test
76
77#endif
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kInvalid
virtual ~GpuTimer()
Definition: GpuTimer.h:29
virtual std::chrono::nanoseconds getTimeElapsed(PlatformTimerQuery)=0
PlatformTimerQuery queueStop()
Definition: GpuTimer.h:50
virtual QueryStatus checkQueryStatus(PlatformTimerQuery)=0
bool disjointSupport() const
Definition: GpuTimer.h:35
virtual PlatformTimerQuery onQueueTimerStart() const =0
virtual void deleteQuery(PlatformTimerQuery)=0
GpuTimer(bool disjointSupport)
Definition: GpuTimer.h:25
virtual void onQueueTimerStop(PlatformTimerQuery) const =0
uint64_t PlatformTimerQuery
Definition: GpuTimer.h:17
static constexpr PlatformTimerQuery kInvalidTimerQuery
Definition: GpuTimer.h:18