Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | namespace sk_gpu_test { |
| 18 | |
| 19 | class FlushFinishTracker : public SkRefCnt { |
| 20 | public: |
| 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 | |
| 44 | private: |
| 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 |