refactor bitmapshader to use a controller

BUG=skia:

Review URL: https://codereview.chromium.org/1153123003
diff --git a/gyp/core.gypi b/gyp/core.gypi
index 48e691e..7ff11ba 100644
--- a/gyp/core.gypi
+++ b/gyp/core.gypi
@@ -26,6 +26,7 @@
         '<(skia_src_path)/core/SkBigPicture.cpp',
         '<(skia_src_path)/core/SkBitmap.cpp',
         '<(skia_src_path)/core/SkBitmapCache.cpp',
+        '<(skia_src_path)/core/SkBitmapController.cpp',
         '<(skia_src_path)/core/SkBitmapDevice.cpp',
         '<(skia_src_path)/core/SkBitmapFilter.h',
         '<(skia_src_path)/core/SkBitmapFilter.cpp',
diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h
index 0488a29..2ddc5be 100644
--- a/include/core/SkTemplates.h
+++ b/include/core/SkTemplates.h
@@ -480,14 +480,47 @@
     };
 };
 
+//////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+ *  Pass the object and the storage that was offered during SkInPlaceNewCheck, and this will
+ *  safely destroy (and free if it was dynamically allocated) the object.
+ */
+template <typename T> void SkInPlaceDeleteCheck(T* obj, void* storage) {
+    if (storage == obj) {
+        obj->~T();
+    } else {
+        SkDELETE(obj);
+    }
+}
+
+/**
+ *  Allocates T, using storage if it is large enough, and allocating on the heap (via new) if
+ *  storage is not large enough.
+ *
+ *      obj = SkInPlaceNewCheck<Type>(storage, size);
+ *      ...
+ *      SkInPlaceDeleteCheck(obj, storage);
+ */
+template <typename T> T* SkInPlaceNewCheck(void* storage, size_t size) {
+    return (sizeof(T) <= size) ? new (storage) T : SkNEW(T);
+}
+
+template <typename T, typename A1, typename A2, typename A3>
+T* SkInPlaceNewCheck(void* storage, size_t size, const A1& a1, const A2& a2, const A3& a3) {
+    return (sizeof(T) <= size) ? new (storage) T(a1, a2, a3) : SkNEW_ARGS(T, (a1, a2, a3));
+}
+
 /**
  * Reserves memory that is aligned on double and pointer boundaries.
  * Hopefully this is sufficient for all practical purposes.
  */
 template <size_t N> class SkAlignedSStorage : SkNoncopyable {
 public:
+    size_t size() const { return N; }
     void* get() { return fData; }
     const void* get() const { return fData; }
+
 private:
     union {
         void*   fPtr;
diff --git a/src/core/SkBitmapController.cpp b/src/core/SkBitmapController.cpp
new file mode 100644
index 0000000..484db8e
--- /dev/null
+++ b/src/core/SkBitmapController.cpp
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBitmap.h"
+#include "SkBitmapController.h"
+#include "SkMatrix.h"
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+static bool valid_for_drawing(const SkBitmap& bm) {
+    if (0 == bm.width() || 0 == bm.height()) {
+        return false;   // nothing to draw
+    }
+    if (NULL == bm.pixelRef()) {
+        return false;   // no pixels to read
+    }
+    if (bm.getTexture()) {
+        // we can handle texture (ugh) since lockPixels will perform a read-back
+        return true;
+    }
+    if (kIndex_8_SkColorType == bm.colorType()) {
+        SkAutoLockPixels alp(bm); // but we need to call it before getColorTable() is safe.
+        if (!bm.getColorTable()) {
+            return false;
+        }
+    }
+    return true;
+}
+
+SkBitmapController::State* SkBitmapController::requestBitmap(const SkBitmap& bm,
+                                                             const SkMatrix& inv,
+                                                             SkFilterQuality quality,
+                                                             void* storage, size_t storageSize) {
+    
+    if (!valid_for_drawing(bm)) {
+        return NULL;
+    }
+
+    State* state = this->onRequestBitmap(bm, inv, quality, storage, storageSize);
+    if (state) {
+        if (!state->fLockedBitmap.getPixels()) {
+            state->fLockedBitmap.lockPixels();
+            if (!state->fLockedBitmap.getPixels()) {
+                SkInPlaceDeleteCheck(state, storage);
+                state = NULL;
+            }
+        }
+    }
+    return state;
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#include "SkBitmapCache.h"
+#include "SkBitmapScaler.h"
+#include "SkMipMap.h"
+#include "SkResourceCache.h"
+
+class SkDefaultBitmapControllerState : public SkBitmapController::State {
+public:
+    SkDefaultBitmapControllerState(const SkBitmap& src, const SkMatrix& inv, SkFilterQuality qual);
+
+private:
+    SkAutoTUnref<const SkMipMap> fCurrMip;
+    
+    bool processHQRequest(const SkBitmap& orig);
+    bool processMediumRequest(const SkBitmap& orig);
+};
+
+// 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::GetEffectiveSingleAllocationByteLimit();
+    if (0 == maximumAllocation) {
+        return true;
+    }
+    // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY);
+    // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize);
+    // Skip the division step:
+    const size_t size = bm.info().getSafeSize(bm.info().minRowBytes());
+    return size < (maximumAllocation * invMat.getScaleX() * invMat.getScaleY());
+}
+
+/*
+ *  High quality is implemented by performing up-right scale-only filtering and then
+ *  using bilerp for any remaining transformations.
+ */
+bool SkDefaultBitmapControllerState::processHQRequest(const SkBitmap& origBitmap) {
+    if (fQuality != kHigh_SkFilterQuality) {
+        return false;
+    }
+    
+    // Our default return state is to downgrade the request to Medium, w/ or w/o setting fBitmap
+    // to a valid bitmap. If we succeed, we will set this to Low instead.
+    fQuality = kMedium_SkFilterQuality;
+    
+    if (kN32_SkColorType != origBitmap.colorType() || !cache_size_okay(origBitmap, fInvMatrix) ||
+        fInvMatrix.hasPerspective())
+    {
+        return false; // can't handle the reqeust
+    }
+    
+    SkScalar invScaleX = fInvMatrix.getScaleX();
+    SkScalar invScaleY = fInvMatrix.getScaleY();
+    if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) {
+        SkSize scale;
+        if (!fInvMatrix.decomposeScale(&scale)) {
+            return false;
+        }
+        invScaleX = scale.width();
+        invScaleY = scale.height();
+    }
+    if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1)) {
+        return false; // no need for HQ
+    }
+    
+    SkScalar trueDestWidth  = origBitmap.width() / invScaleX;
+    SkScalar trueDestHeight = origBitmap.height() / invScaleY;
+    SkScalar roundedDestWidth = SkScalarRoundToScalar(trueDestWidth);
+    SkScalar roundedDestHeight = SkScalarRoundToScalar(trueDestHeight);
+    
+    if (!SkBitmapCache::Find(origBitmap, roundedDestWidth, roundedDestHeight, &fLockedBitmap)) {
+        if (!SkBitmapScaler::Resize(&fLockedBitmap,
+                                    origBitmap,
+                                    SkBitmapScaler::RESIZE_BEST,
+                                    roundedDestWidth,
+                                    roundedDestHeight,
+                                    SkResourceCache::GetAllocator())) {
+            return false; // we failed to create fScaledBitmap
+        }
+        
+        SkASSERT(fLockedBitmap.getPixels());
+        fLockedBitmap.setImmutable();
+        SkBitmapCache::Add(origBitmap, roundedDestWidth, roundedDestHeight, fLockedBitmap);
+    }
+    
+    SkASSERT(fLockedBitmap.getPixels());
+    
+    fInvMatrix.postScale(roundedDestWidth / origBitmap.width(),
+                         roundedDestHeight / origBitmap.height());
+    fQuality = kLow_SkFilterQuality;
+    return true;
+}
+
+/*
+ *  Modulo internal errors, this should always succeed *if* the matrix is downscaling
+ *  (in this case, we have the inverse, so it succeeds if fInvMatrix is upscaling)
+ */
+bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmap& origBitmap) {
+    SkASSERT(fQuality <= kMedium_SkFilterQuality);
+    if (fQuality != kMedium_SkFilterQuality) {
+        return false;
+    }
+    
+    // Our default return state is to downgrade the request to Low, w/ or w/o setting fBitmap
+    // to a valid bitmap.
+    fQuality = kLow_SkFilterQuality;
+    
+    SkSize invScaleSize;
+    if (!fInvMatrix.decomposeScale(&invScaleSize, NULL)) {
+        return false;
+    }
+    SkScalar invScale = SkScalarSqrt(invScaleSize.width() * invScaleSize.height());
+    
+    if (invScale > SK_Scalar1) {
+        fCurrMip.reset(SkMipMapCache::FindAndRef(origBitmap));
+        if (NULL == fCurrMip.get()) {
+            fCurrMip.reset(SkMipMapCache::AddAndRef(origBitmap));
+            if (NULL == fCurrMip.get()) {
+                return false;
+            }
+        }
+        // diagnostic for a crasher...
+        if (NULL == fCurrMip->data()) {
+            sk_throw();
+        }
+        
+        SkScalar levelScale = SkScalarInvert(invScale);
+        SkMipMap::Level level;
+        if (fCurrMip->extractLevel(levelScale, &level)) {
+            SkScalar invScaleFixup = level.fScale;
+            fInvMatrix.postScale(invScaleFixup, invScaleFixup);
+            
+            const SkImageInfo info = origBitmap.info().makeWH(level.fWidth, level.fHeight);
+            // todo: if we could wrap the fCurrMip in a pixelref, then we could just install
+            //       that here, and not need to explicitly track it ourselves.
+            return fLockedBitmap.installPixels(info, level.fPixels, level.fRowBytes);
+        } else {
+            // failed to extract, so release the mipmap
+            fCurrMip.reset(NULL);
+        }
+    }
+    return false;
+}
+
+SkDefaultBitmapControllerState::SkDefaultBitmapControllerState(const SkBitmap& src,
+                                                               const SkMatrix& inv,
+                                                               SkFilterQuality qual) {
+    fInvMatrix = inv;
+    fQuality = qual;
+
+    if (!this->processHQRequest(src) && !this->processMediumRequest(src)) {
+        fLockedBitmap = src;
+    }
+    SkASSERT(fQuality <= kLow_SkFilterQuality);
+}
+
+SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBitmap& bm,
+                                                                      const SkMatrix& inverse,
+                                                                      SkFilterQuality quality,
+                                                                      void* storage, size_t size) {
+    return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm, inverse, quality);
+}
+
diff --git a/src/core/SkBitmapController.h b/src/core/SkBitmapController.h
new file mode 100644
index 0000000..bc3c696
--- /dev/null
+++ b/src/core/SkBitmapController.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkBitmapController_DEFINED
+#define SkBitmapController_DEFINED
+
+#include "SkBitmap.h"
+#include "SkFilterQuality.h"
+#include "SkMatrix.h"
+
+/**
+ *  Handles request to scale, filter, and lock a bitmap to be rasterized.
+ */
+class SkBitmapController : ::SkNoncopyable {
+public:
+    class State : ::SkNoncopyable {
+    public:
+        virtual ~State() {}
+
+        const SkBitmap& lockedBitmap() const { return fLockedBitmap; }
+        const SkMatrix& invMatrix() const { return fInvMatrix; }
+        SkFilterQuality quality() const { return fQuality; }
+    
+    protected:
+        SkBitmap        fLockedBitmap;
+        SkMatrix        fInvMatrix;
+        SkFilterQuality fQuality;
+    
+    private:
+        friend class SkBitmapController;
+    };
+
+    virtual ~SkBitmapController() {}
+
+    State* requestBitmap(const SkBitmap&, const SkMatrix& inverse, SkFilterQuality,
+                         void* storage, size_t storageSize);
+
+    State* requestBitmap(const SkBitmap& bm, const SkMatrix& inverse, SkFilterQuality quality) {
+        return this->requestBitmap(bm, inverse, quality, NULL, 0);
+    }
+
+protected:
+    virtual State* onRequestBitmap(const SkBitmap&, const SkMatrix& inverse, SkFilterQuality,
+                                   void* storage, size_t storageSize) = 0;
+};
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+class SkDefaultBitmapController : public SkBitmapController {
+public:
+    SkDefaultBitmapController() {}
+    
+protected:
+    State* onRequestBitmap(const SkBitmap&, const SkMatrix& inverse, SkFilterQuality,
+                           void* storage, size_t storageSize) override;
+};
+
+#endif
diff --git a/src/core/SkBitmapProcState.cpp b/src/core/SkBitmapProcState.cpp
index 0b50fbc..314fdd7 100644
--- a/src/core/SkBitmapProcState.cpp
+++ b/src/core/SkBitmapProcState.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "SkBitmapCache.h"
+#include "SkBitmapController.h"
 #include "SkBitmapProcState.h"
 #include "SkColorPriv.h"
 #include "SkFilterProc.h"
@@ -36,6 +37,12 @@
 #include "SkBitmapProcState_filter.h"
 #include "SkBitmapProcState_procs.h"
 
+SkBitmapProcState::SkBitmapProcState() : fBMState(NULL) {}
+
+SkBitmapProcState::~SkBitmapProcState() {
+    SkInPlaceDeleteCheck(fBMState, fBMStateStorage.get());
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 // true iff the matrix contains, at most, scale and translate elements
@@ -90,166 +97,12 @@
     return true;
 }
 
-///////////////////////////////////////////////////////////////////////////////
-
 static bool valid_for_filtering(unsigned dimension) {
     // for filtering, width and height must fit in 14bits, since we use steal
     // 2 bits from each to store our 4bit subpixel data
     return (dimension & ~0x3FFF) == 0;
 }
 
-// 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::GetEffectiveSingleAllocationByteLimit();
-    if (0 == maximumAllocation) {
-        return true;
-    }
-    // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY);
-    // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize);
-    // Skip the division step:
-    return bm.info().getSafeSize(bm.info().minRowBytes())
-        < (maximumAllocation * invMat.getScaleX() * invMat.getScaleY());
-}
-
-/*
- *  High quality is implemented by performing up-right scale-only filtering and then
- *  using bilerp for any remaining transformations.
- */
-void SkBitmapProcState::processHQRequest() {
-    SkASSERT(kHigh_SkFilterQuality == fFilterLevel);
-
-    // Our default return state is to downgrade the request to Medium, w/ or w/o setting fBitmap
-    // to a valid bitmap. If we succeed, we will set this to Low instead.
-    fFilterLevel = kMedium_SkFilterQuality;
-
-    if (kN32_SkColorType != fOrigBitmap.colorType() || !cache_size_okay(fOrigBitmap, fInvMatrix) ||
-        fInvMatrix.hasPerspective())
-    {
-        return; // can't handle the reqeust
-    }
-
-    SkScalar invScaleX = fInvMatrix.getScaleX();
-    SkScalar invScaleY = fInvMatrix.getScaleY();
-    if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) {
-        SkSize scale;
-        if (!fInvMatrix.decomposeScale(&scale)) {
-            return;
-        }
-        invScaleX = scale.width();
-        invScaleY = scale.height();
-    }
-    if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1)) {
-        return; // no need for HQ
-    }
-
-    SkScalar trueDestWidth  = fOrigBitmap.width() / invScaleX;
-    SkScalar trueDestHeight = fOrigBitmap.height() / invScaleY;
-    SkScalar roundedDestWidth = SkScalarRoundToScalar(trueDestWidth);
-    SkScalar roundedDestHeight = SkScalarRoundToScalar(trueDestHeight);
-
-    if (!SkBitmapCache::Find(fOrigBitmap, roundedDestWidth, roundedDestHeight, &fScaledBitmap)) {
-        if (!SkBitmapScaler::Resize(&fScaledBitmap,
-                                    fOrigBitmap,
-                                    SkBitmapScaler::RESIZE_BEST,
-                                    roundedDestWidth,
-                                    roundedDestHeight,
-                                    SkResourceCache::GetAllocator())) {
-            return; // we failed to create fScaledBitmap
-        }
-
-        SkASSERT(fScaledBitmap.getPixels());
-        fScaledBitmap.setImmutable();
-        SkBitmapCache::Add(fOrigBitmap, roundedDestWidth, roundedDestHeight, fScaledBitmap);
-    }
-
-    SkASSERT(fScaledBitmap.getPixels());
-    fBitmap = &fScaledBitmap;
-
-    fInvMatrix.postScale(roundedDestWidth / fOrigBitmap.width(),
-                         roundedDestHeight / fOrigBitmap.height());
-    fFilterLevel = kLow_SkFilterQuality;
-}
-
-/*
- *  Modulo internal errors, this should always succeed *if* the matrix is downscaling
- *  (in this case, we have the inverse, so it succeeds if fInvMatrix is upscaling)
- */
-void SkBitmapProcState::processMediumRequest() {
-    SkASSERT(kMedium_SkFilterQuality == fFilterLevel);
-
-    // Our default return state is to downgrade the request to Low, w/ or w/o setting fBitmap
-    // to a valid bitmap.
-    fFilterLevel = kLow_SkFilterQuality;
-
-    SkSize invScaleSize;
-    if (!fInvMatrix.decomposeScale(&invScaleSize, NULL)) {
-        return;
-    }
-    SkScalar invScale = SkScalarSqrt(invScaleSize.width() * invScaleSize.height());
-
-    if (invScale > SK_Scalar1) {
-        fCurrMip.reset(SkMipMapCache::FindAndRef(fOrigBitmap));
-        if (NULL == fCurrMip.get()) {
-            fCurrMip.reset(SkMipMapCache::AddAndRef(fOrigBitmap));
-            if (NULL == fCurrMip.get()) {
-                return;
-            }
-        }
-        // diagnostic for a crasher...
-        if (NULL == fCurrMip->data()) {
-            sk_throw();
-        }
-
-        SkScalar levelScale = SkScalarInvert(invScale);
-        SkMipMap::Level level;
-        if (fCurrMip->extractLevel(levelScale, &level)) {
-            SkScalar invScaleFixup = level.fScale;
-            fInvMatrix.postScale(invScaleFixup, invScaleFixup);
-
-            const SkImageInfo info = fOrigBitmap.info().makeWH(level.fWidth, level.fHeight);
-            // todo: if we could wrap the fCurrMip in a pixelref, then we could just install
-            //       that here, and not need to explicitly track it ourselves.
-            fScaledBitmap.installPixels(info, level.fPixels, level.fRowBytes);
-            fBitmap = &fScaledBitmap;
-        } else {
-            // failed to extract, so release the mipmap
-            fCurrMip.reset(NULL);
-        }
-    }
-}
-
-bool SkBitmapProcState::lockBaseBitmap() {
-    // TODO(reed): use bitmap cache here?
-    fScaledBitmap = fOrigBitmap;
-    fScaledBitmap.lockPixels();
-    if (NULL == fScaledBitmap.getPixels()) {
-        return false;
-    }
-    fBitmap = &fScaledBitmap;
-    return true;
-}
-
-static bool valid_for_drawing(const SkBitmap& bm) {
-    if (0 == bm.width() || 0 == bm.height()) {
-        return false;   // nothing to draw
-    }
-    if (NULL == bm.pixelRef()) {
-        return false;   // no pixels to read
-    }
-    if (bm.getTexture()) {
-        // we can handle texture (ugh) since lockPixels will perform a read-back
-        return true;
-    }
-    if (kIndex_8_SkColorType == bm.colorType()) {
-        SkAutoLockPixels alp(bm); // but we need to call it before getColorTable() is safe.
-        if (!bm.getColorTable()) {
-            return false;
-        }
-    }
-    return true;
-}
-
 /*
  *  Analyze filter-quality and matrix, and decide how to implement that.
  *
@@ -261,31 +114,21 @@
  *    and may be removed.
  */
 bool SkBitmapProcState::chooseProcs(const SkMatrix& inv, const SkPaint& paint) {
-    if (!valid_for_drawing(fOrigBitmap)) {
-        return false;
-    }
-
     fBitmap = NULL;
     fInvMatrix = inv;
     fFilterLevel = paint.getFilterQuality();
 
-    if (kHigh_SkFilterQuality == fFilterLevel) {
-        this->processHQRequest();
+    SkDefaultBitmapController controller;
+    fBMState = controller.requestBitmap(fOrigBitmap, inv, paint.getFilterQuality(),
+                                        fBMStateStorage.get(), fBMStateStorage.size());
+    if (NULL == fBMState) {
+        return false;
     }
-    SkASSERT(fFilterLevel < kHigh_SkFilterQuality);
-
-    if (kMedium_SkFilterQuality == fFilterLevel) {
-        this->processMediumRequest();
-    }
-    SkASSERT(fFilterLevel < kMedium_SkFilterQuality);
-
-    if (NULL == fBitmap) {
-        if (!this->lockBaseBitmap()) {
-            return false;
-        }
-    }
-    SkASSERT(fBitmap);
-
+    fBitmap = &fBMState->lockedBitmap();
+    fInvMatrix = fBMState->invMatrix();
+    fFilterLevel = fBMState->quality();
+    SkASSERT(fBitmap->getPixels());
+    
     bool trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
     bool clampClamp = SkShader::kClamp_TileMode == fTileModeX &&
                       SkShader::kClamp_TileMode == fTileModeY;
diff --git a/src/core/SkBitmapProcState.h b/src/core/SkBitmapProcState.h
index 36e1ae0..0afc773 100644
--- a/src/core/SkBitmapProcState.h
+++ b/src/core/SkBitmapProcState.h
@@ -9,6 +9,7 @@
 #define SkBitmapProcState_DEFINED
 
 #include "SkBitmap.h"
+#include "SkBitmapController.h"
 #include "SkBitmapFilter.h"
 #include "SkMatrix.h"
 #include "SkMipMap.h"
@@ -23,6 +24,9 @@
 class SkPaint;
 
 struct SkBitmapProcState {
+    SkBitmapProcState();
+    ~SkBitmapProcState();
+
     typedef void (*ShaderProc32)(const SkBitmapProcState&, int x, int y,
                                  SkPMColor[], int count);
 
@@ -49,7 +53,8 @@
     typedef U16CPU (*IntTileProc)(int value, int count);   // returns 0..count-1
 
     const SkBitmap*     fBitmap;            // chooseProcs - orig or scaled
-    SkMatrix            fInvMatrix;         // chooseProcs
+    SkMatrix            fInvMatrix;         // copy of what is in fBMState, can we remove the dup?
+
     SkMatrix::MapXYProc fInvProc;           // chooseProcs
 
     SkFractionalInt     fInvSxFractionalInt;
@@ -122,22 +127,18 @@
     SampleProc16        fSampleProc16;      // chooseProcs
 
     SkBitmap            fOrigBitmap;        // CONSTRUCTOR
-    SkBitmap            fScaledBitmap;      // chooseProcs
 
-    SkAutoTUnref<const SkMipMap> fCurrMip;
-
-    void processHQRequest();
-    void processMediumRequest();
+    enum {
+        kBMStateSize = 136  // found by inspection. if too small, we will call new/delete
+    };
+    SkAlignedSStorage<kBMStateSize> fBMStateStorage;
+    SkBitmapController::State* fBMState;
 
     MatrixProc chooseMatrixProc(bool trivial_matrix);
     bool chooseProcs(const SkMatrix& inv, const SkPaint&);
     bool chooseScanlineProcs(bool trivialMatrix, bool clampClamp, const SkPaint& paint);
     ShaderProc32 chooseShaderProc32();
 
-    // returns false if we failed to "lock" the pixels at all. Typically this
-    // means we have to abort the shader.
-    bool lockBaseBitmap();
-
     // Return false if we failed to setup for fast translate (e.g. overflow)
     bool setupForTranslate();