blob: 048dec7e519ee2c44c9b8154cde359740bbc65ee [file] [log] [blame]
csmartdaltonc6618dd2016-10-05 08:42:03 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "src/core/SkExchange.h"
Hal Canary8a001442018-09-19 11:31:27 -040013
csmartdaltonc6618dd2016-10-05 08:42:03 -070014#include <chrono>
15
16namespace sk_gpu_test {
17
18using PlatformTimerQuery = uint64_t;
19static constexpr PlatformTimerQuery kInvalidTimerQuery = 0;
20
21/**
22 * Platform-independent interface for timing operations on the GPU.
23 */
24class GpuTimer {
25public:
26 GpuTimer(bool disjointSupport)
27 : fDisjointSupport(disjointSupport)
28 , fActiveTimer(kInvalidTimerQuery) {
29 }
30 virtual ~GpuTimer() { SkASSERT(!fActiveTimer); }
31
32 /**
33 * Returns whether this timer can detect disjoint GPU operations while timing. If false, a query
34 * has less confidence when it completes with QueryStatus::kAccurate.
35 */
36 bool disjointSupport() const { return fDisjointSupport; }
37
38 /**
39 * Inserts a "start timing" command in the GPU command stream.
40 */
41 void queueStart() {
42 SkASSERT(!fActiveTimer);
43 fActiveTimer = this->onQueueTimerStart();
44 }
45
46 /**
47 * Inserts a "stop timing" command in the GPU command stream.
48 *
49 * @return a query object that can retrieve the time elapsed once the timer has completed.
50 */
51 PlatformTimerQuery SK_WARN_UNUSED_RESULT queueStop() {
52 SkASSERT(fActiveTimer);
53 this->onQueueTimerStop(fActiveTimer);
54 return skstd::exchange(fActiveTimer, kInvalidTimerQuery);
55 }
56
57 enum class QueryStatus {
58 kInvalid, //<! the timer query is invalid.
59 kPending, //<! the timer is still running on the GPU.
60 kDisjoint, //<! the query is complete, but dubious due to disjoint GPU operations.
61 kAccurate //<! the query is complete and reliable.
62 };
63
64 virtual QueryStatus checkQueryStatus(PlatformTimerQuery) = 0;
65 virtual std::chrono::nanoseconds getTimeElapsed(PlatformTimerQuery) = 0;
66 virtual void deleteQuery(PlatformTimerQuery) = 0;
67
68private:
69 virtual PlatformTimerQuery onQueueTimerStart() const = 0;
70 virtual void onQueueTimerStop(PlatformTimerQuery) const = 0;
71
72 bool const fDisjointSupport;
73 PlatformTimerQuery fActiveTimer;
74};
75
76} // namespace sk_gpu_test
77
78#endif