Adding a new SkSurface factory for generating surfaces from the scratch texture pool.

TEST=Surface unit test
BUG=crbug.com/351798
R=bsalomon@google.com, robertphillips@google.com, reed@google.com

Author: junov@chromium.org

Review URL: https://codereview.chromium.org/201153023

git-svn-id: http://skia.googlecode.com/svn/trunk@13864 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkSurface.h b/include/core/SkSurface.h
index 8e430d8..f5052ca 100644
--- a/include/core/SkSurface.h
+++ b/include/core/SkSurface.h
@@ -73,6 +73,20 @@
      */
     static SkSurface* NewRenderTarget(GrContext*, const SkImageInfo&, int sampleCount = 0);
 
+    /**
+     *  Return a new surface whose contents will be drawn to an offscreen
+     *  render target, allocated by the surface from the scratch texture pool
+     *  managed by the GrContext. The scratch texture pool serves the purpose
+     *  of retaining textures after they are no longer in use in order to
+     *  re-use them later without having to re-allocate.  Scratch textures
+     *  should be used in cases where high turnover is expected. This allows,
+     *  for example, the copy on write to recycle a texture from a recently
+     *  released SkImage snapshot of the surface.
+     *  Note: Scratch textures count against the GrContext's cached resource
+     *  budget.
+     */
+    static SkSurface* NewScratchRenderTarget(GrContext*, const SkImageInfo&, int sampleCount = 0);
+
     int width() const { return fWidth; }
     int height() const { return fHeight; }
 
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 1834586..52a25b4 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -111,6 +111,11 @@
      */
     size_t getGpuTextureCacheBytes() const;
 
+    /**
+     * Returns the number of resources hosted by the texture cache.
+     */
+    int getGpuTextureCacheResourceCount() const;
+
     ///////////////////////////////////////////////////////////////////////////
     // Textures
 
diff --git a/include/gpu/SkGpuDevice.h b/include/gpu/SkGpuDevice.h
index ad4ebd7..3e20e16 100644
--- a/include/gpu/SkGpuDevice.h
+++ b/include/gpu/SkGpuDevice.h
@@ -29,12 +29,18 @@
  */
 class SK_API SkGpuDevice : public SkBitmapDevice {
 public:
+    enum Flags {
+        kNeedClear_Flag = 1 << 0,  //!< Surface requires an initial clear 
+        kCached_Flag    = 1 << 1,  //!< Surface is cached and needs to be unlocked when released
+    };
 
     /**
      * Creates an SkGpuDevice from a GrSurface. This will fail if the surface is not a render
-     * target. The caller owns a ref on the returned device.
+     * target. The caller owns a ref on the returned device. If the surface is cached, 
+     * the kCached_Flag should be specified to make the device responsible for unlocking
+     * the surface when it is released.
      */
-    static SkGpuDevice* Create(GrSurface* surface);
+    static SkGpuDevice* Create(GrSurface* surface, unsigned flags = 0);
 
     /**
      *  New device that will create an offscreen renderTarget based on the
@@ -58,7 +64,7 @@
      *  DEPRECATED -- need to make this private, call Create(surface)
      *  New device that will render to the specified renderTarget.
      */
-    SkGpuDevice(GrContext*, GrRenderTarget*);
+    SkGpuDevice(GrContext*, GrRenderTarget*, unsigned flags = 0);
 
     /**
      *  DEPRECATED -- need to make this private, call Create(surface)
@@ -66,7 +72,7 @@
      *  The GrTexture's asRenderTarget() must be non-NULL or device will not
      *  function.
      */
-    SkGpuDevice(GrContext*, GrTexture*);
+    SkGpuDevice(GrContext*, GrTexture*, unsigned flags = 0);
 
     virtual ~SkGpuDevice();
 
@@ -173,10 +179,7 @@
     bool                fNeedClear;
 
     // called from rt and tex cons
-    void initFromRenderTarget(GrContext*, GrRenderTarget*, bool cached);
-
-    // used by createCompatibleDevice
-    SkGpuDevice(GrContext*, GrTexture* texture, bool needClear);
+    void initFromRenderTarget(GrContext*, GrRenderTarget*, unsigned flags);
 
     virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;