| 1 |
/* |
| 2 |
* Copyright 2025 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 skgpu_Token_DEFINED |
| 9 |
#define skgpu_Token_DEFINED |
| 10 |
|
| 11 |
#include <cstdint> |
| 12 |
|
| 13 |
class GrOpFlushState; |
| 14 |
class TestingUploadTarget; |
| 15 |
namespace skgpu::graphite { |
| 16 |
class RecorderPriv; |
| 17 |
} |
| 18 |
|
| 19 |
namespace skgpu { |
| 20 |
|
| 21 |
/** |
| 22 |
* A generic token used to sequence operations within a Recorder. |
| 23 |
*/ |
| 24 |
class Token { |
| 25 |
public: |
| 26 |
static Token InvalidToken() { return Token(0); } |
| 27 |
|
| 28 |
Token(const Token&) = default; |
| 29 |
Token& operator=(const Token&) = default; |
| 30 |
|
| 31 |
bool operator==(const Token& that) const { return fSequenceNumber == that.fSequenceNumber; } |
| 32 |
bool operator!=(const Token& that) const { return !(*this == that); } |
| 33 |
bool operator<(const Token that) const { return fSequenceNumber < that.fSequenceNumber; } |
| 34 |
bool operator<=(const Token that) const { return fSequenceNumber <= that.fSequenceNumber; } |
| 35 |
bool operator>(const Token that) const { return fSequenceNumber > that.fSequenceNumber; } |
| 36 |
bool operator>=(const Token that) const { return fSequenceNumber >= that.fSequenceNumber; } |
| 37 |
|
| 38 |
Token& operator++() { |
| 39 |
++fSequenceNumber; |
| 40 |
return *this; |
| 41 |
} |
| 42 |
Token operator++(int) { |
| 43 |
auto old = fSequenceNumber; |
| 44 |
++fSequenceNumber; |
| 45 |
return Token(old); |
| 46 |
} |
| 47 |
|
| 48 |
Token next() const { return Token(fSequenceNumber + 1); } |
| 49 |
|
| 50 |
/** Returns the raw value for debugging and comparison. */ |
| 51 |
uint64_t value() const { return fSequenceNumber; } |
| 52 |
|
| 53 |
/** Is this token in the [start, end] inclusive interval? */ |
| 54 |
bool inInterval(const Token& start, const Token& end) { |
| 55 |
return *this >= start && *this <= end; |
| 56 |
} |
| 57 |
|
| 58 |
private: |
| 59 |
explicit Token(uint64_t sequenceNumber) : fSequenceNumber(sequenceNumber) {} |
| 60 |
uint64_t fSequenceNumber; |
| 61 |
|
| 62 |
friend class TokenTracker; // Allow TokenTracker to construct one |
| 63 |
}; |
| 64 |
|
| 65 |
/** |
| 66 |
* Encapsulates the incrementing and distribution of Tokens for a Recorder. |
| 67 |
*/ |
| 68 |
class TokenTracker { |
| 69 |
public: |
| 70 |
/** |
| 71 |
* Gets the token one beyond the last token that has been flushed. |
| 72 |
* This represents the ID for the *current* batch of work being recorded. |
| 73 |
*/ |
| 74 |
Token nextFlushToken() const { return fCurrentFlushToken.next(); } |
| 75 |
|
| 76 |
/** |
| 77 |
* Gets the token that was *just issued*. This represents the ID of the |
| 78 |
* flush that was most recently completed. |
| 79 |
*/ |
| 80 |
Token currentFlushToken() const { return fCurrentFlushToken; } |
| 81 |
|
| 82 |
/** |
| 83 |
* Gets the next draw token. |
| 84 |
*/ |
| 85 |
Token nextDrawToken() const { return fCurrentDrawToken.next(); } |
| 86 |
|
| 87 |
private: |
| 88 |
// Only these classes get to increment the token counters |
| 89 |
friend class ::GrOpFlushState; // Ganesh |
| 90 |
friend class ::TestingUploadTarget; // `` |
| 91 |
friend class skgpu::graphite::RecorderPriv; // Graphite |
| 92 |
|
| 93 |
Token issueDrawToken() { return ++fCurrentDrawToken; } |
| 94 |
Token issueFlushToken() { return ++fCurrentFlushToken; } |
| 95 |
|
| 96 |
Token fCurrentDrawToken = Token::InvalidToken(); |
| 97 |
Token fCurrentFlushToken = Token::InvalidToken(); |
| 98 |
}; |
| 99 |
|
| 100 |
} // namespace skgpu |
| 101 |
|
| 102 |
#endif // skgpu_Token_DEFINED |