add optional SkAlphaType parameter to notifyPixelsChanged
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 27 Jan 2014 15:41:07 +0000 (15:41 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 27 Jan 2014 15:41:07 +0000 (15:41 +0000)
R=scroggo@google.com, halcanary@google.com, robertphillips@google.com

Author: reed@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@13192 2bbb7eff-a529-9590-31e7-b0007b416f81

include/core/SkBitmap.h
include/core/SkPixelRef.h
src/core/SkBitmap.cpp
src/core/SkPixelRef.cpp

index 4dcf327..1dd2c80 100644 (file)
@@ -128,6 +128,13 @@ public:
      *  Set the bitmap's alphaType, returning true on success. If false is
      *  returned, then the specified new alphaType is incompatible with the
      *  Config, and the current alphaType is unchanged.
+     *
+     *  Note: this changes the alphatype for the underlying pixels, which means
+     *  that all bitmaps that might be sharing (subsets of) the pixels will
+     *  be affected. This is an expensive change for some backends (e.g. GPU)
+     *  since changing the alphatype can invalidate internal caches. Thus this
+     *  call should only be made if it is associated with real changes to the
+     *  pixel data.
      */
     bool setAlphaType(SkAlphaType);
 
index e65f4a0..62edc3e 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright 2008 The Android Open Source Project
  *
@@ -6,7 +5,6 @@
  * found in the LICENSE file.
  */
 
-
 #ifndef SkPixelRef_DEFINED
 #define SkPixelRef_DEFINED
 
@@ -128,11 +126,19 @@ public:
     */
     uint32_t getGenerationID() const;
 
-    /** Call this if you have changed the contents of the pixels. This will in-
-        turn cause a different generation ID value to be returned from
-        getGenerationID().
-    */
-    void notifyPixelsChanged();
+    /**
+     *  Call this if you have changed the contents of the pixels. This will in-
+     *  turn cause a different generation ID value to be returned from
+     *  getGenerationID().
+     *
+     *  If the alphatype has also changed, specify its new value as well. If
+     *  the new pixels' alphatype is the same, this can be called with no
+     *  parameter.
+     */
+    void notifyPixelsChanged(SkAlphaType);
+    void notifyPixelsChanged() {
+        this->notifyPixelsChanged(fInfo.fAlphaType);
+    }
 
     /** Returns true if this pixelref is marked as immutable, meaning that the
         contents of its pixels will not change for the lifetime of the pixelref.
@@ -333,6 +339,7 @@ protected:
 private:
     SkBaseMutex*    fMutex; // must remain in scope for the life of this object
 
+    // mostly const. fInfo.fAlpahType can be changed at runtime.
     const SkImageInfo fInfo;
 
     // LockRec is only valid if we're in a locked state (isLocked())
index 7074386..825b1dc 100644 (file)
@@ -321,7 +321,12 @@ bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
     if (!validate_alphaType(this->config(), alphaType, &alphaType)) {
         return false;
     }
-    fAlphaType = SkToU8(alphaType);
+    if (fAlphaType != alphaType) {
+        fAlphaType = SkToU8(alphaType);
+        if (fPixelRef) {
+            fPixelRef->notifyPixelsChanged(alphaType);
+        }
+    }
     return true;
 }
 
index 10e1309..507a4fc 100644 (file)
@@ -254,12 +254,13 @@ void SkPixelRef::callGenIDChangeListeners() {
     fGenIDChangeListeners.deleteAll();
 }
 
-void SkPixelRef::notifyPixelsChanged() {
+void SkPixelRef::notifyPixelsChanged(SkAlphaType at) {
 #ifdef SK_DEBUG
     if (fIsImmutable) {
         SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
     }
 #endif
+    *const_cast<SkAlphaType*>(&fInfo.fAlphaType) = at;
     this->callGenIDChangeListeners();
     this->needsNewGenID();
 }