• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "SkTypes.h"
12 #include "SkExchange.h"
13 
14 #include <chrono>
15 
16 namespace sk_gpu_test {
17 
18 using PlatformTimerQuery = uint64_t;
19 static constexpr PlatformTimerQuery kInvalidTimerQuery = 0;
20 
21 /**
22  * Platform-independent interface for timing operations on the GPU.
23  */
24 class GpuTimer {
25 public:
GpuTimer(bool disjointSupport)26     GpuTimer(bool disjointSupport)
27         : fDisjointSupport(disjointSupport)
28         , fActiveTimer(kInvalidTimerQuery) {
29     }
~GpuTimer()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      */
disjointSupport()36     bool disjointSupport() const { return fDisjointSupport; }
37 
38     /**
39      * Inserts a "start timing" command in the GPU command stream.
40      */
queueStart()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      */
queueStop()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 
68 private:
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
79