Add explicit UniqueID classes for GrGpuResource & GrSurfaceProxy

This sets the stage for using the Proxy's/RenderTargetContext's ID above the flush and the RenderTarget's/GrGpuResource's below the flush.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=4650

Change-Id: I9f1e6b00c02a0691d90b58c49e1d8c60684884c1
Reviewed-on: https://skia-review.googlesource.com/4650
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/include/private/GrAuditTrail.h b/include/private/GrAuditTrail.h
index b39f13f..6e8c92d 100644
--- a/include/private/GrAuditTrail.h
+++ b/include/private/GrAuditTrail.h
@@ -9,6 +9,7 @@
 #define GrAuditTrail_DEFINED
 
 #include "GrConfig.h"
+#include "GrGpuResource.h"
 #include "SkRect.h"
 #include "SkString.h"
 #include "SkTArray.h"
@@ -107,13 +108,14 @@
     // We could just return our internal bookkeeping struct if copying the data out becomes
     // a performance issue, but until then its nice to decouple
     struct BatchInfo {
-        SkRect fBounds;
-        uint32_t fRenderTargetUniqueID;
+        SkRect                  fBounds;
+        // TODO: switch over to GrSurfaceProxy::UniqueID
+        GrGpuResource::UniqueID fRenderTargetUniqueID;
         struct Batch {
             int fClientID;
             SkRect fBounds;
         };
-        SkTArray<Batch> fBatches;
+        SkTArray<Batch>                 fBatches;
     };
 
     void getBoundsByClientID(SkTArray<BatchInfo>* outInfo, int clientID);
@@ -139,10 +141,11 @@
     typedef SkTArray<Batch*> Batches;
 
     struct BatchNode {
+        BatchNode(const GrGpuResource::UniqueID& id) : fRenderTargetUniqueID(id) { }
         SkString toJson() const;
-        SkRect fBounds;
-        Batches fChildren;
-        uint32_t fRenderTargetUniqueID;
+        SkRect                         fBounds;
+        Batches                        fChildren;
+        const GrGpuResource::UniqueID  fRenderTargetUniqueID;
     };
     typedef SkTArray<std::unique_ptr<BatchNode>, true> BatchList;
 
diff --git a/include/private/GrSurfaceProxy.h b/include/private/GrSurfaceProxy.h
index 1e846c5..dd5ece1 100644
--- a/include/private/GrSurfaceProxy.h
+++ b/include/private/GrSurfaceProxy.h
@@ -99,7 +99,44 @@
     int height() const { return fDesc.fHeight; }
     GrPixelConfig config() const { return fDesc.fConfig; }
 
-    uint32_t uniqueID() const { return fUniqueID; }
+    class UniqueID {
+    public:
+        // wrapped
+        explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
+        // deferred
+        UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
+
+        uint32_t asUInt() const { return fID; }
+
+        bool operator==(const UniqueID& other) const {
+            return fID == other.fID;
+        }
+        bool operator!=(const UniqueID& other) const {
+            return !(*this == other);
+        }
+
+        bool isInvalid() const { return SK_InvalidUniqueID == fID; }
+
+    private:
+        const uint32_t fID;
+    };
+
+    /*
+     * The contract for the uniqueID is:
+     *   for wrapped resources:
+     *      the uniqueID will match that of the wrapped resource
+     *
+     *   for deferred resources:
+     *      the uniqueID will be different from the real resource, when it is allocated
+     *      the proxy's uniqueID will not change across the instantiate call
+     *
+     *    the uniqueIDs of the proxies and the resources draw from the same pool
+     *
+     * What this boils down to is that the uniqueID of a proxy can be used to consistently
+     * track/identify a proxy but should never be used to distinguish between
+     * resources and proxies - beware!
+     */
+    UniqueID uniqueID() const { return fUniqueID; }
 
     GrSurface* instantiate(GrTextureProvider* texProvider);
 
@@ -151,9 +188,9 @@
         : fDesc(desc)
         , fFit(fit)
         , fBudgeted(budgeted)
-        , fUniqueID(GrGpuResource::CreateUniqueID())
         , fGpuMemorySize(kInvalidGpuMemorySize)
         , fLastOpList(nullptr) {
+        // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
     }
 
     // Wrapped version
@@ -162,10 +199,10 @@
     virtual ~GrSurfaceProxy();
 
     // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
-    const GrSurfaceDesc fDesc;
-    const SkBackingFit  fFit;      // always exact for wrapped resources
-    const SkBudgeted    fBudgeted; // set from the backing resource for wrapped resources
-    const uint32_t      fUniqueID; // set from the backing resource for wrapped resources
+    const GrSurfaceDesc  fDesc;
+    const SkBackingFit   fFit;      // always exact for wrapped resources
+    const SkBudgeted     fBudgeted; // set from the backing resource for wrapped resources
+    const UniqueID       fUniqueID; // set from the backing resource for wrapped resources
 
     static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
     SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })