Fix ref counting of cached layer's texture

R=bsalomon@google.com

Author: robertphillips@google.com

Review URL: https://codereview.chromium.org/431603002
diff --git a/src/gpu/GrLayerCache.cpp b/src/gpu/GrLayerCache.cpp
index 5899479..2c97387 100644
--- a/src/gpu/GrLayerCache.cpp
+++ b/src/gpu/GrLayerCache.cpp
@@ -198,8 +198,10 @@
     // The texture wouldn't fit in the cache - give it it's own texture.
     // This path always uses a new scratch texture and (thus) doesn't cache anything.
     // This can yield a lot of re-rendering
-    layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch),
-                      GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
+    SkAutoTUnref<GrTexture> tex(fContext->lockAndRefScratchTexture(desc,
+                                                        GrContext::kApprox_ScratchTexMatch));
+
+    layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
     layer->setLocked(true);
     return false;
 }
diff --git a/src/gpu/GrLayerCache.h b/src/gpu/GrLayerCache.h
index c5fa236..f4087d6 100644
--- a/src/gpu/GrLayerCache.h
+++ b/src/gpu/GrLayerCache.h
@@ -82,16 +82,15 @@
         SkASSERT(SK_InvalidGenID != pictureID && layerID >= 0);
     }
 
+    ~GrCachedLayer() {
+        SkSafeUnref(fTexture);
+    }
+
     uint32_t pictureID() const { return fKey.getPictureID(); }
     int layerID() const { return fKey.getLayerID(); }
 
-    // This call takes over the caller's ref
     void setTexture(GrTexture* texture, const GrIRect16& rect) {
-        if (NULL != fTexture) {
-            fTexture->unref();
-        }
-
-        fTexture = texture; // just take over caller's ref
+        SkRefCnt_SafeAssign(fTexture, texture);
         fRect = rect;
     }
     GrTexture* texture() { return fTexture; }