diff --git a/include/core/SkBitmap.h b/include/core/SkBitmap.h index 4dcf327226..1dd2c8034f 100644 --- a/include/core/SkBitmap.h +++ b/include/core/SkBitmap.h @@ -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); diff --git a/include/core/SkPixelRef.h b/include/core/SkPixelRef.h index e65f4a04c5..62edc3ec95 100644 --- a/include/core/SkPixelRef.h +++ b/include/core/SkPixelRef.h @@ -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()) diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index 707438636d..825b1dceca 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -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; } diff --git a/src/core/SkPixelRef.cpp b/src/core/SkPixelRef.cpp index 10e1309c0b..507a4fcd48 100644 --- a/src/core/SkPixelRef.cpp +++ b/src/core/SkPixelRef.cpp @@ -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(&fInfo.fAlphaType) = at; this->callGenIDChangeListeners(); this->needsNewGenID(); }