check effective cache-size for fixed-budget caches

BUG=skia:

Review URL: https://codereview.chromium.org/876743002
diff --git a/src/core/SkBitmapProcState.cpp b/src/core/SkBitmapProcState.cpp
index 367b3ff..30a64ed 100644
--- a/src/core/SkBitmapProcState.cpp
+++ b/src/core/SkBitmapProcState.cpp
@@ -113,7 +113,7 @@
 // Check to see that the size of the bitmap that would be produced by
 // scaling by the given inverted matrix is less than the maximum allowed.
 static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) {
-    size_t maximumAllocation = SkResourceCache::GetSingleAllocationByteLimit();
+    size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByteLimit();
     if (0 == maximumAllocation) {
         return true;
     }
diff --git a/src/core/SkResourceCache.cpp b/src/core/SkResourceCache.cpp
index efcff26..4ed889a 100644
--- a/src/core/SkResourceCache.cpp
+++ b/src/core/SkResourceCache.cpp
@@ -435,6 +435,22 @@
     return fSingleAllocationByteLimit;
 }
 
+size_t SkResourceCache::getEffectiveSingleAllocationByteLimit() const {
+    // fSingleAllocationByteLimit == 0 means the caller is asking for our default
+    size_t limit = fSingleAllocationByteLimit;
+
+    // if we're not discardable (i.e. we are fixed-budget) then cap the single-limit
+    // to our budget.
+    if (NULL == fDiscardableFactory) {
+        if (0 == limit) {
+            limit = fTotalByteLimit;
+        } else {
+            limit = SkTMin(limit, fTotalByteLimit);
+        }
+    }
+    return limit;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "SkThread.h"
@@ -511,6 +527,11 @@
     return get_cache()->getSingleAllocationByteLimit();
 }
 
+size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() {
+    SkAutoMutexAcquire am(gMutex);
+    return get_cache()->getEffectiveSingleAllocationByteLimit();
+}
+
 void SkResourceCache::PurgeAll() {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->purgeAll();
diff --git a/src/core/SkResourceCache.h b/src/core/SkResourceCache.h
index 883ed18..88ccb87 100644
--- a/src/core/SkResourceCache.h
+++ b/src/core/SkResourceCache.h
@@ -123,6 +123,7 @@
 
     static size_t SetSingleAllocationByteLimit(size_t);
     static size_t GetSingleAllocationByteLimit();
+    static size_t GetEffectiveSingleAllocationByteLimit();
 
     static void PurgeAll();
 
@@ -186,6 +187,10 @@
      */
     size_t setSingleAllocationByteLimit(size_t maximumAllocationSize);
     size_t getSingleAllocationByteLimit() const;
+    // returns the logical single allocation size (pinning against the budget when the cache
+    // is not backed by discardable memory.
+    size_t getEffectiveSingleAllocationByteLimit() const;
+
     /**
      *  Set the maximum number of bytes available to this cache. If the current
      *  cache exceeds this new value, it will be purged to try to fit within