blob: 9e7956a559fd0b96104fe9594f07f1ce4cd5b0ba [file] [log] [blame]
Greg Daniel02497d42020-02-21 15:46:27 -05001/*
2 * Copyright 2020 Google LLC
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 FlushFinishTracker_DEFINED
9#define FlushFinishTracker_DEFINED
10
11#include "include/core/SkRefCnt.h"
12#include "include/gpu/GrContext.h"
13#include "src/core/SkTraceEvent.h"
14
15#include <chrono>
16
17namespace sk_gpu_test {
18
19class FlushFinishTracker : public SkRefCnt {
20public:
21 static void FlushFinished(void* finishedContext) {
22 auto tracker = static_cast<FlushFinishTracker*>(finishedContext);
23 tracker->setFinished();
24 tracker->unref();
25 }
26
27 FlushFinishTracker(GrContext* context) : fContext(context) {}
28
29 void setFinished() { fIsFinished = true; }
30
31 void waitTillFinished() {
32 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
33 auto begin = std::chrono::steady_clock::now();
34 auto end = begin;
35 while (!fIsFinished && (end - begin) < std::chrono::seconds(2)) {
36 fContext->checkAsyncWorkCompletion();
37 end = std::chrono::steady_clock::now();
38 }
39 if (!fIsFinished) {
40 SkDebugf("WARNING: Wait failed for flush sync. Timings might not be accurate.\n");
41 }
42 }
43
44private:
45 GrContext* fContext;
46
47 // Currently we don't have the this bool be atomic cause all current uses of this class happen
48 // on a single thread. In other words we call flush, checkAsyncWorkCompletion, and
49 // waitTillFinished all on the same thread. If we ever want to support the flushing and waiting
50 // to happen on different threads then we should make this atomic.
51 bool fIsFinished = false;
52};
53
54} //namespace sk_gpu_test
55
56#endif