Add a DirectContextID to GrDirectContexts

Bug: skia:11728
Change-Id: I80ab54f2cfa0bbc69f688e1e598eeeb4bde1a1a4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/383023
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrDirectContext.cpp b/src/gpu/GrDirectContext.cpp
index 42ad297..535538d 100644
--- a/src/gpu/GrDirectContext.cpp
+++ b/src/gpu/GrDirectContext.cpp
@@ -51,8 +51,18 @@
 
 #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
 
+GrDirectContext::DirectContextID GrDirectContext::DirectContextID::NextID() {
+    static std::atomic<uint32_t> nextID{1};
+    uint32_t id;
+    do {
+        id = nextID.fetch_add(1, std::memory_order_relaxed);
+    } while (id == SK_InvalidUniqueID);
+    return DirectContextID(id);
+}
+
 GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
-        : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options)) {
+        : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options))
+        , fDirectContextID(DirectContextID::NextID()) {
 }
 
 GrDirectContext::~GrDirectContext() {