Refactor HWUI to better handle Canvas DrawFilters.

First, this CL removes the need to decompose the DrawFilters
in Java and instead passes the SkDrawFilter to HWUI directly.
This also allows the removal of duplicated logic between HWUI
and other Canvas implementations regarding Paint filter levels.

Second, the DrawFilter is now stored in the DisplayListRenderer
where we apply it to every paint BEFORE it is stored in the
DisplayList.  This eliminates the need to filter all Paints on
playback and removes additional complexity at playback.

Finally, as a result of storing the filtered paint we can now
do a better job caching the paints.  This takes advantage of
recent changes in Skia to quickly enable quick hashing and
comparison of paint objects.

Change-Id: Iec507a2d894827975cc4f1d22241542bb0534b4e
diff --git a/libs/hwui/DeferredDisplayList.cpp b/libs/hwui/DeferredDisplayList.cpp
index a998594..d716957 100644
--- a/libs/hwui/DeferredDisplayList.cpp
+++ b/libs/hwui/DeferredDisplayList.cpp
@@ -235,26 +235,6 @@
             return false;
         }
 
-        /* Draw Modifiers compatibility check
-         *
-         * Shadows are ignored, as only text uses them, and in that case they are drawn
-         * per-DrawTextOp, before the unified text draw. Because of this, it's always safe to merge
-         * text UNLESS a later draw's shadow should overlays a previous draw's text. This is covered
-         * above with the intersection check.
-         *
-         * OverrideLayerAlpha is also ignored, as it's only used for drawing layers, which are never
-         * merged.
-         *
-         * These ignore cases prevent us from simply memcmp'ing the drawModifiers
-         */
-        const DrawModifiers& lhsMod = lhs->mDrawModifiers;
-        const DrawModifiers& rhsMod = rhs->mDrawModifiers;
-
-        // Draw filter testing expects bit fields to be clear if filter not set.
-        if (lhsMod.mHasDrawFilter != rhsMod.mHasDrawFilter) return false;
-        if (lhsMod.mPaintFilterClearBits != rhsMod.mPaintFilterClearBits) return false;
-        if (lhsMod.mPaintFilterSetBits != rhsMod.mPaintFilterSetBits) return false;
-
         return true;
     }
 
diff --git a/libs/hwui/DisplayListOp.h b/libs/hwui/DisplayListOp.h
index 39f89e5..26b4540 100644
--- a/libs/hwui/DisplayListOp.h
+++ b/libs/hwui/DisplayListOp.h
@@ -201,10 +201,6 @@
     }
 
 protected:
-    const SkPaint* getPaint(OpenGLRenderer& renderer) {
-        return renderer.filterPaint(mPaint);
-    }
-
     // Helper method for determining op opaqueness. Assumes op fills its bounds in local
     // coordinates, and that paint's alpha is used
     inline bool isOpaqueOverBounds(const DeferredDisplayState& state) {
@@ -234,7 +230,7 @@
 
     }
 
-    const SkPaint* mPaint; // should be accessed via getPaint() when applying
+    const SkPaint* mPaint;
     bool mQuickRejected;
 };
 
@@ -608,39 +604,6 @@
     const SkRegion* mRegion;
 };
 
-class ResetPaintFilterOp : public StateOp {
-public:
-    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const {
-        renderer.resetPaintFilter();
-    }
-
-    virtual void output(int level, uint32_t logFlags) const {
-        OP_LOGS("ResetPaintFilter");
-    }
-
-    virtual const char* name() { return "ResetPaintFilter"; }
-};
-
-class SetupPaintFilterOp : public StateOp {
-public:
-    SetupPaintFilterOp(int clearBits, int setBits)
-            : mClearBits(clearBits), mSetBits(setBits) {}
-
-    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const {
-        renderer.setupPaintFilter(mClearBits, mSetBits);
-    }
-
-    virtual void output(int level, uint32_t logFlags) const {
-        OP_LOG("SetupPaintFilter, clear %#x, set %#x", mClearBits, mSetBits);
-    }
-
-    virtual const char* name() { return "SetupPaintFilter"; }
-
-private:
-    int mClearBits;
-    int mSetBits;
-};
-
 ///////////////////////////////////////////////////////////////////////////////
 // DRAW OPERATIONS - these are operations that can draw to the canvas's device
 ///////////////////////////////////////////////////////////////////////////////
@@ -659,7 +622,7 @@
     }
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawBitmap(mBitmap, getPaint(renderer));
+        return renderer.drawBitmap(mBitmap, mPaint);
     }
 
     AssetAtlas::Entry* getAtlasEntry() {
@@ -764,7 +727,7 @@
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
         return renderer.drawBitmap(mBitmap, mSrc.left, mSrc.top, mSrc.right, mSrc.bottom,
                 mLocalBounds.left, mLocalBounds.top, mLocalBounds.right, mLocalBounds.bottom,
-                getPaint(renderer));
+                mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -790,7 +753,7 @@
             : DrawBitmapOp(bitmap, paint) {}
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawBitmapData(mBitmap, getPaint(renderer));
+        return renderer.drawBitmapData(mBitmap, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -815,7 +778,7 @@
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
         return renderer.drawBitmapMesh(mBitmap, mMeshWidth, mMeshHeight,
-                mVertices, mColors, getPaint(renderer));
+                mVertices, mColors, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -945,7 +908,7 @@
         }
 
         return renderer.drawPatches(mBitmap, getAtlasEntry(),
-                &vertices[0], indexCount, getPaint(renderer));
+                &vertices[0], indexCount, mPaint);
     }
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
@@ -953,7 +916,7 @@
         // This method won't perform the quickReject() since we've already done it at this point
         return renderer.drawPatch(mBitmap, getMesh(renderer), getAtlasEntry(),
                 mLocalBounds.left, mLocalBounds.top, mLocalBounds.right, mLocalBounds.bottom,
-                getPaint(renderer));
+                mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1037,7 +1000,7 @@
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
         return renderer.drawRect(mLocalBounds.left, mLocalBounds.top,
-                mLocalBounds.right, mLocalBounds.bottom, getPaint(renderer));
+                mLocalBounds.right, mLocalBounds.bottom, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1061,7 +1024,7 @@
             mRects(rects), mCount(count) {}
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawRects(mRects, mCount, getPaint(renderer));
+        return renderer.drawRects(mRects, mCount, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1088,7 +1051,7 @@
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
         return renderer.drawRoundRect(mLocalBounds.left, mLocalBounds.top,
-                mLocalBounds.right, mLocalBounds.bottom, mRx, mRy, getPaint(renderer));
+                mLocalBounds.right, mLocalBounds.bottom, mRx, mRy, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1120,7 +1083,7 @@
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
         return renderer.drawRoundRect(*mLeft, *mTop, *mRight, *mBottom,
-                *mRx, *mRy, getPaint(renderer));
+                *mRx, *mRy, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1146,7 +1109,7 @@
             mX(x), mY(y), mRadius(radius) {}
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawCircle(mX, mY, mRadius, getPaint(renderer));
+        return renderer.drawCircle(mX, mY, mRadius, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1167,7 +1130,7 @@
             : DrawOp(paint), mX(x), mY(y), mRadius(radius) {}
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawCircle(*mX, *mY, *mRadius, getPaint(renderer));
+        return renderer.drawCircle(*mX, *mY, *mRadius, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1189,7 +1152,7 @@
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
         return renderer.drawOval(mLocalBounds.left, mLocalBounds.top,
-                mLocalBounds.right, mLocalBounds.bottom, getPaint(renderer));
+                mLocalBounds.right, mLocalBounds.bottom, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1209,7 +1172,7 @@
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
         return renderer.drawArc(mLocalBounds.left, mLocalBounds.top,
                 mLocalBounds.right, mLocalBounds.bottom,
-                mStartAngle, mSweepAngle, mUseCenter, getPaint(renderer));
+                mStartAngle, mSweepAngle, mUseCenter, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1238,13 +1201,12 @@
     }
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawPath(mPath, getPaint(renderer));
+        return renderer.drawPath(mPath, mPaint);
     }
 
     virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo,
             const DeferredDisplayState& state) {
-        const SkPaint* paint = getPaint(renderer);
-        renderer.getCaches().pathCache.precache(mPath, paint);
+        renderer.getCaches().pathCache.precache(mPath, mPaint);
 
         deferInfo.batchId = DeferredDisplayList::kOpBatch_AlphaMaskTexture;
     }
@@ -1268,7 +1230,7 @@
     }
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawLines(mPoints, mCount, getPaint(renderer));
+        return renderer.drawLines(mPoints, mCount, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1295,7 +1257,7 @@
             : DrawLinesOp(points, count, paint) {}
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawPoints(mPoints, mCount, getPaint(renderer));
+        return renderer.drawPoints(mPoints, mCount, mPaint);
     }
 
     virtual void output(int level, uint32_t logFlags) const {
@@ -1320,9 +1282,8 @@
 
     virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo,
             const DeferredDisplayState& state) {
-        const SkPaint* paint = getPaint(renderer);
-        FontRenderer& fontRenderer = renderer.getCaches().fontRenderer->getFontRenderer(paint);
-        fontRenderer.precache(paint, mText, mCount, SkMatrix::I());
+        FontRenderer& fontRenderer = renderer.getCaches().fontRenderer->getFontRenderer(mPaint);
+        fontRenderer.precache(mPaint, mText, mCount, SkMatrix::I());
 
         deferInfo.batchId = mPaint->getColor() == SK_ColorBLACK ?
                 DeferredDisplayList::kOpBatch_Text :
@@ -1346,7 +1307,7 @@
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
         return renderer.drawTextOnPath(mText, mBytesCount, mCount, mPath,
-                mHOffset, mVOffset, getPaint(renderer));
+                mHOffset, mVOffset, mPaint);
     }
 
     virtual const char* name() { return "DrawTextOnPath"; }
@@ -1366,7 +1327,7 @@
     }
 
     virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) {
-        return renderer.drawPosText(mText, mBytesCount, mCount, mPositions, getPaint(renderer));
+        return renderer.drawPosText(mText, mBytesCount, mCount, mPositions, mPaint);
     }
 
     virtual const char* name() { return "DrawPosText"; }
@@ -1386,12 +1347,11 @@
 
     virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo,
             const DeferredDisplayState& state) {
-        const SkPaint* paint = getPaint(renderer);
-        FontRenderer& fontRenderer = renderer.getCaches().fontRenderer->getFontRenderer(paint);
+        FontRenderer& fontRenderer = renderer.getCaches().fontRenderer->getFontRenderer(mPaint);
         SkMatrix transform;
         renderer.findBestFontTransform(state.mMatrix, &transform);
         if (mPrecacheTransform != transform) {
-            fontRenderer.precache(paint, mText, mCount, transform);
+            fontRenderer.precache(mPaint, mText, mCount, transform);
             mPrecacheTransform = transform;
         }
         deferInfo.batchId = mPaint->getColor() == SK_ColorBLACK ?
@@ -1413,7 +1373,7 @@
         Rect bounds;
         getLocalBounds(bounds);
         return renderer.drawText(mText, mBytesCount, mCount, mX, mY,
-                mPositions, getPaint(renderer), mTotalAdvance, bounds);
+                mPositions, mPaint, mTotalAdvance, bounds);
     }
 
     virtual status_t multiDraw(OpenGLRenderer& renderer, Rect& dirty,
@@ -1428,7 +1388,7 @@
             // quickReject() will not occure in drawText() so we can use mLocalBounds
             // directly, we do not need to account for shadow by calling getLocalBounds()
             status |= renderer.drawText(op.mText, op.mBytesCount, op.mCount, op.mX, op.mY,
-                    op.mPositions, op.getPaint(renderer), op.mTotalAdvance, op.mLocalBounds,
+                    op.mPositions, op.mPaint, op.mTotalAdvance, op.mLocalBounds,
                     drawOpMode);
         }
         return status;
diff --git a/libs/hwui/DisplayListRenderer.cpp b/libs/hwui/DisplayListRenderer.cpp
index 1f70921..bcb34b9 100644
--- a/libs/hwui/DisplayListRenderer.cpp
+++ b/libs/hwui/DisplayListRenderer.cpp
@@ -437,12 +437,8 @@
     return DrawGlInfo::kStatusDone;
 }
 
-void DisplayListRenderer::resetPaintFilter() {
-    addStateOp(new (alloc()) ResetPaintFilterOp());
-}
-
-void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) {
-    addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits));
+void DisplayListRenderer::setDrawFilter(SkDrawFilter* filter) {
+    mDrawFilter.reset(filter);
 }
 
 void DisplayListRenderer::insertReorderBarrier(bool enableReorder) {
diff --git a/libs/hwui/DisplayListRenderer.h b/libs/hwui/DisplayListRenderer.h
index 0012080..f407617 100644
--- a/libs/hwui/DisplayListRenderer.h
+++ b/libs/hwui/DisplayListRenderer.h
@@ -17,10 +17,12 @@
 #ifndef ANDROID_HWUI_DISPLAY_LIST_RENDERER_H
 #define ANDROID_HWUI_DISPLAY_LIST_RENDERER_H
 
+#include <SkDrawFilter.h>
 #include <SkMatrix.h>
 #include <SkPaint.h>
 #include <SkPath.h>
 #include <SkRegion.h>
+#include <SkTLazy.h>
 #include <cutils/compiler.h>
 
 #include "DisplayList.h"
@@ -96,9 +98,8 @@
     virtual bool clipPath(const SkPath* path, SkRegion::Op op);
     virtual bool clipRegion(const SkRegion* region, SkRegion::Op op);
 
-    // Misc - should be implemented with SkPaint inspection
-    virtual void resetPaintFilter();
-    virtual void setupPaintFilter(int clearBits, int setBits);
+    // Misc
+    virtual void setDrawFilter(SkDrawFilter* filter);
 
     bool isCurrentTransformSimple() {
         return currentTransform()->isSimple();
@@ -221,15 +222,27 @@
     inline const SkPaint* refPaint(const SkPaint* paint) {
         if (!paint) return NULL;
 
-        const SkPaint* paintCopy = mPaintMap.valueFor(paint);
-        if (paintCopy == NULL || paintCopy->getGenerationID() != paint->getGenerationID()) {
-            paintCopy = new SkPaint(*paint);
-            // replaceValueFor() performs an add if the entry doesn't exist
-            mPaintMap.replaceValueFor(paint, paintCopy);
-            mDisplayListData->paints.add(paintCopy);
+        // If there is a draw filter apply it here and store the modified paint
+        // so that we don't need to modify the paint every time we access it.
+        SkTLazy<SkPaint> filteredPaint;
+        if (mDrawFilter.get()) {
+           paint = filteredPaint.init();
+           mDrawFilter->filter(filteredPaint.get(), SkDrawFilter::kPaint_Type);
         }
 
-        return paintCopy;
+        // compute the hash key for the paint and check the cache.
+        const uint32_t key = paint->getHash();
+        const SkPaint* cachedPaint = mPaintMap.valueFor(key);
+        // In the unlikely event that 2 unique paints have the same hash we do a
+        // object equality check to ensure we don't erroneously dedup them.
+        if (cachedPaint == NULL || *cachedPaint != *paint) {
+            cachedPaint =  new SkPaint(*paint);
+            // replaceValueFor() performs an add if the entry doesn't exist
+            mPaintMap.replaceValueFor(key, cachedPaint);
+            mDisplayListData->paints.add(cachedPaint);
+        }
+
+        return cachedPaint;
     }
 
     inline SkPaint* copyPaint(const SkPaint* paint) {
@@ -285,7 +298,7 @@
         return patch;
     }
 
-    DefaultKeyedVector<const SkPaint*, const SkPaint*> mPaintMap;
+    DefaultKeyedVector<uint32_t, const SkPaint*> mPaintMap;
     DefaultKeyedVector<const SkPath*, const SkPath*> mPathMap;
     DefaultKeyedVector<const SkRegion*, const SkRegion*> mRegionMap;
 
@@ -300,6 +313,8 @@
 
     int mRestoreSaveCount;
 
+    SkAutoTUnref<SkDrawFilter> mDrawFilter;
+
     friend class RenderNode;
 
 }; // class DisplayListRenderer
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index b691019..c40258b 100755
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -3057,47 +3057,10 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Draw filters
 ///////////////////////////////////////////////////////////////////////////////
-
-void OpenGLRenderer::resetPaintFilter() {
-    // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
-    // comparison, see MergingDrawBatch::canMergeWith
-    mDrawModifiers.mHasDrawFilter = false;
-    mDrawModifiers.mPaintFilterClearBits = 0;
-    mDrawModifiers.mPaintFilterSetBits = 0;
-}
-
-void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
-    // TODO: don't bother with boolean, it's redundant with clear/set bits
-    mDrawModifiers.mHasDrawFilter = true;
-    mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
-    mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
-}
-
-const SkPaint* OpenGLRenderer::filterPaint(const SkPaint* paint) {
-    // TODO: use CompatFlagsDrawFilter here, and combine logic with android/graphics/DrawFilter.cpp
-    // to avoid clobbering 0x02 paint flag
-
-    // Equivalent to the Java Paint's FILTER_BITMAP_FLAG.
-    static const uint32_t sFilterBitmapFlag = 0x02;
-
-    if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
-        return paint;
-    }
-
-    const uint32_t clearBits = mDrawModifiers.mPaintFilterClearBits;
-    const uint32_t setBits = mDrawModifiers.mPaintFilterSetBits;
-
-    const uint32_t flags = (paint->getFlags() & ~clearBits) | setBits;
-    mFilteredPaint = *paint;
-    mFilteredPaint.setFlags(flags);
-
-    // check if paint filter trying to override bitmap filter
-    if ((clearBits | setBits) & sFilterBitmapFlag) {
-        mFilteredPaint.setFilterLevel(flags & sFilterBitmapFlag
-                ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
-    }
-
-    return &mFilteredPaint;
+void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
+    // We should never get here since we apply the draw filter when stashing
+    // the paints in the DisplayList.
+    LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 4b92900..b3b008a 100755
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -70,11 +70,6 @@
     }
 
     float mOverrideLayerAlpha;
-
-    // Draw filters
-    bool mHasDrawFilter;
-    int mPaintFilterClearBits;
-    int mPaintFilterSetBits;
 };
 
 enum StateDeferFlags {
@@ -205,14 +200,11 @@
     status_t drawShadow(float casterAlpha,
             const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer);
 
-    virtual void resetPaintFilter();
-    virtual void setupPaintFilter(int clearBits, int setBits);
+    virtual void setDrawFilter(SkDrawFilter* filter);
 
     // If this value is set to < 1.0, it overrides alpha set on layer (see drawBitmap, drawLayer)
     void setOverrideLayerAlpha(float alpha) { mDrawModifiers.mOverrideLayerAlpha = alpha; }
 
-    const SkPaint* filterPaint(const SkPaint* paint);
-
     /**
      * Store the current display state (most importantly, the current clip and transform), and
      * additionally map the state's bounds from local to window coordinates.
diff --git a/libs/hwui/Renderer.h b/libs/hwui/Renderer.h
index a2f8c05..4a36f31 100644
--- a/libs/hwui/Renderer.h
+++ b/libs/hwui/Renderer.h
@@ -20,11 +20,12 @@
 #include <SkColorFilter.h>
 #include <SkPaint.h>
 #include <SkRegion.h>
-
 #include <utils/String8.h>
 
 #include "AssetAtlas.h"
 
+class SkDrawFilter;
+
 namespace android {
 
 class Functor;
@@ -173,9 +174,8 @@
     virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
     virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
 
-    // Misc - should be implemented with SkPaint inspection
-    virtual void resetPaintFilter() = 0;
-    virtual void setupPaintFilter(int clearBits, int setBits) = 0;
+    // Misc
+    virtual void setDrawFilter(SkDrawFilter* filter) = 0;
 
 // ----------------------------------------------------------------------------
 // Canvas draw operations