Refactor to separate backend object lifecycle and GpuResource budget decision

Refactor GrGpuResource to contain two different pieces of state:
 a) instance is budgeted or not budgeted
 b) instance references wrapped backend objects or not

The "object lifecycle" was also attached to backend object
handles (ids), which made the code a bit unclear. Backend objects
would be associated with GrGpuResource::LifeCycle, even though
GrGpuResource::LifeCycle refers to the GpuResource, and individual
backend objects in one GpuResource might be governed with different
"lifecycle".

Mark the budgeted/not budgeted with SkBudgeted::kYes, SkBudgeted::kNo.
This was previously GrGpuResource::kCached_LifeCycle,
GrGpuResource::kUncached_LifeCycle.

Mark the "references wrapped object" with boolean. This was previously
GrGpuResource::kBorrowed_LifeCycle,
GrGpuResource::kAdopted_LifeCycle for GrGpuResource.

Associate the backend object ownership status with
GrBackendObjectOwnership for the backend object handles.

The resource type leaf constuctors, such has GrGLTexture or
GrGLTextureRenderTarget take "budgeted" parameter. This parameter
is passed to GrGpuResource::registerWithCache().

The resource type intermediary constructors, such as GrGLTexture
constructors for class GrGLTextureRenderTarget do not take "budgeted"
parameters, intermediary construtors do not call registerWithCache.

Removes the need for tagging GrGpuResource -derived subclass
constructors with "Derived" parameter.

Makes instances that wrap backend objects be registered with
a new function GrGpuResource::registerWithCacheWrapped().

Removes "budgeted" parameter from classes such as StencilAttahment, as
they are always cached and never wrap any external backend objects.

Removes the use of concept "external" from the member function names.
The API refers to the objects as "wrapped", so make all related
functions use the term consistently.

No change in functionality. Resources referencing wrapped objects are
always inserted to the cache with budget decision kNo.

BUG=594928
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1862043002

Review URL: https://codereview.chromium.org/1862043002
diff --git a/include/gpu/GrBuffer.h b/include/gpu/GrBuffer.h
index 7e04577..829596e 100644
--- a/include/gpu/GrBuffer.h
+++ b/include/gpu/GrBuffer.h
@@ -110,22 +110,24 @@
 protected:
     GrBuffer(GrGpu* gpu, size_t gpuMemorySize, GrBufferType intendedType,
              GrAccessPattern accessPattern, bool cpuBacked)
-        : INHERITED(gpu, kCached_LifeCycle),
+        : INHERITED(gpu),
           fMapPtr(nullptr),
           fGpuMemorySize(gpuMemorySize), // TODO: Zero for cpu backed buffers?
           fAccessPattern(accessPattern),
-          fCPUBacked(cpuBacked) {
+          fCPUBacked(cpuBacked),
+          fIntendedType(intendedType) {
+    }
+
+    void computeScratchKey(GrScratchKey* key) const override {
         if (!fCPUBacked && SkIsPow2(fGpuMemorySize) && kDynamic_GrAccessPattern == fAccessPattern) {
-            GrScratchKey key;
-            ComputeScratchKeyForDynamicBuffer(fGpuMemorySize, intendedType, &key);
-            this->setScratchKey(key);
+            ComputeScratchKeyForDynamicBuffer(fGpuMemorySize, fIntendedType, key);
         }
     }
 
     void* fMapPtr;
 
 private:
-    virtual size_t onGpuMemorySize() const { return fGpuMemorySize; }
+    size_t onGpuMemorySize() const override { return fGpuMemorySize; }
 
     virtual void onMap() = 0;
     virtual void onUnmap() = 0;
@@ -134,7 +136,7 @@
     size_t            fGpuMemorySize;
     GrAccessPattern   fAccessPattern;
     bool              fCPUBacked;
-
+    GrBufferType      fIntendedType;
     typedef GrGpuResource INHERITED;
 };
 
diff --git a/include/gpu/GrGpuResource.h b/include/gpu/GrGpuResource.h
index 8103959..37a87d3 100644
--- a/include/gpu/GrGpuResource.h
+++ b/include/gpu/GrGpuResource.h
@@ -140,34 +140,6 @@
 class SK_API GrGpuResource : public GrIORef<GrGpuResource> {
 public:
 
-
-    enum LifeCycle {
-        /**
-         * The resource is cached and owned by Skia. Resources with this status may be kept alive
-         * by the cache as either scratch or unique resources even when there are no refs to them.
-         * The cache may release them whenever there are no refs.
-         */
-        kCached_LifeCycle,
-
-        /**
-         * The resource is uncached. As soon as there are no more refs to it, it is released. Under
-         * the hood the cache may opaquely recycle it as a cached resource.
-         */
-        kUncached_LifeCycle,
-
-        /**
-         * Similar to uncached, but Skia does not manage the lifetime of the underlying backend
-         * 3D API object(s). The client is responsible for freeing those. Used to inject client-
-         * created GPU resources into Skia (e.g. to render to a client-created texture).
-         */
-        kBorrowed_LifeCycle,
-
-        /**
-         * An external resource with ownership transfered into Skia. Skia will free the resource.
-         */
-        kAdopted_LifeCycle,
-    };
-
     /**
      * Tests whether a object has been abandoned or released. All objects will
      * be in this state after their creating GrContext is destroyed or has
@@ -261,11 +233,16 @@
     virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
 
 protected:
-    // This must be called by every GrGpuObject. It should be called once the object is fully
-    // initialized (i.e. not in a base class constructor).
-    void registerWithCache();
+    // This must be called by every non-wrapped GrGpuObject. It should be called once the object is
+    // fully initialized (i.e. only from the constructors of the final class).
+    void registerWithCache(SkBudgeted);
 
-    GrGpuResource(GrGpu*, LifeCycle);
+    // This must be called by every GrGpuObject that references any wrapped backend objects. It
+    // should be called once the object is fully initialized (i.e. only from the constructors of the
+    // final class).
+    void registerWithCacheWrapped();
+
+    GrGpuResource(GrGpu*);
     virtual ~GrGpuResource();
 
     GrGpu* getGpu() const { return fGpu; }
@@ -277,13 +254,6 @@
         backend API calls should be made. */
     virtual void onAbandon() { }
 
-    bool shouldFreeResources() const { return fLifeCycle != kBorrowed_LifeCycle; }
-
-    bool isExternal() const {
-        return GrGpuResource::kAdopted_LifeCycle == fLifeCycle ||
-               GrGpuResource::kBorrowed_LifeCycle == fLifeCycle;
-    }
-
     /**
      * This entry point should be called whenever gpuMemorySize() should report a different size.
      * The cache will call gpuMemorySize() to update the current size of the resource.
@@ -291,12 +261,6 @@
     void didChangeGpuMemorySize() const;
 
     /**
-     * Optionally called by the GrGpuResource subclass if the resource can be used as scratch.
-     * By default resources are not usable as scratch. This should only be called once.
-     **/
-    void setScratchKey(const GrScratchKey& scratchKey);
-
-    /**
      * Allows subclasses to add additional backing information to the SkTraceMemoryDump. Called by
      * onMemoryDump. The default implementation adds no backing information.
      **/
@@ -304,6 +268,14 @@
 
 private:
     /**
+     * Called by the registerWithCache if the resource is available to be used as scratch.
+     * Resource subclasses should override this if the instances should be recycled as scratch
+     * resources and populate the scratchKey with the key.
+     * By default resources are not recycled as scratch.
+     **/
+    virtual void computeScratchKey(GrScratchKey*) const { };
+
+    /**
      * Frees the object in the underlying 3D API. Called by CacheAccess.
      */
     void release();
@@ -341,7 +313,8 @@
     GrGpu*                      fGpu;
     mutable size_t              fGpuMemorySize;
 
-    LifeCycle                   fLifeCycle;
+    SkBudgeted                  fBudgeted;
+    bool                        fRefsWrappedObjects;
     const uint32_t              fUniqueID;
 
     SkAutoTUnref<const SkData>  fData;
diff --git a/include/gpu/GrRenderTarget.h b/include/gpu/GrRenderTarget.h
index d39d284..eb9f142 100644
--- a/include/gpu/GrRenderTarget.h
+++ b/include/gpu/GrRenderTarget.h
@@ -156,9 +156,9 @@
     GrDrawTarget* getLastDrawTarget() { return fLastDrawTarget; }
 
 protected:
-    GrRenderTarget(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc,
+    GrRenderTarget(GrGpu* gpu, const GrSurfaceDesc& desc,
                    SampleConfig sampleConfig, GrStencilAttachment* stencil = nullptr)
-        : INHERITED(gpu, lifeCycle, desc)
+        : INHERITED(gpu, desc)
         , fStencilAttachment(stencil)
         , fSampleConfig(sampleConfig)
         , fLastDrawTarget(nullptr) {
diff --git a/include/gpu/GrSurface.h b/include/gpu/GrSurface.h
index b59d802..b87b2db 100644
--- a/include/gpu/GrSurface.h
+++ b/include/gpu/GrSurface.h
@@ -146,8 +146,8 @@
     // Provides access to methods that should be public within Skia code.
     friend class GrSurfacePriv;
 
-    GrSurface(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
-        : INHERITED(gpu, lifeCycle)
+    GrSurface(GrGpu* gpu, const GrSurfaceDesc& desc)
+        : INHERITED(gpu)
         , fDesc(desc)
         , fReleaseProc(NULL)
         , fReleaseCtx(NULL)
diff --git a/include/gpu/GrTexture.h b/include/gpu/GrTexture.h
index 1d589ed..1aa2cbd 100644
--- a/include/gpu/GrTexture.h
+++ b/include/gpu/GrTexture.h
@@ -46,11 +46,12 @@
     inline const GrTexturePriv texturePriv() const;
 
 protected:
-    GrTexture(GrGpu*, LifeCycle, const GrSurfaceDesc&, GrSLType, bool wasMipMapDataProvided);
+    GrTexture(GrGpu*, const GrSurfaceDesc&, GrSLType, bool wasMipMapDataProvided);
 
     void validateDesc() const;
 
 private:
+    void computeScratchKey(GrScratchKey*) const override;
     size_t onGpuMemorySize() const override;
     void dirtyMipMaps(bool mipMapsDirty);
 
diff --git a/include/gpu/GrTypesPriv.h b/include/gpu/GrTypesPriv.h
index 9a96521..18bc644 100644
--- a/include/gpu/GrTypesPriv.h
+++ b/include/gpu/GrTypesPriv.h
@@ -464,4 +464,14 @@
 #define GrCapsDebugf(caps, ...)
 #endif
 
+/**
+ * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
+ */
+enum class GrBackendObjectOwnership : bool {
+    /** Holder does not destroy the backend object. */
+    kBorrowed = false,
+    /** Holder destroys the backend object. */
+    kOwned = true
+};
+
 #endif