Exposing the isImmutable/setImmutable API of SkPixelRef in SkBitmap
authorjunov@chromium.org <junov@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 15 Dec 2011 20:14:06 +0000 (20:14 +0000)
committerjunov@chromium.org <junov@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 15 Dec 2011 20:14:06 +0000 (20:14 +0000)
REVIEW=http://codereview.appspot.com/5491055/

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

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

index f280a24..57b80e5 100644 (file)
@@ -158,6 +158,19 @@ public:
     */
     Sk64 getSafeSize64() const ;
 
+    /** Returns true if this bitmap is marked as immutable, meaning that the
+        contents of its pixels will not change for the lifetime of the bitmap.
+    */
+    bool isImmutable() const;
+
+    /** Marks this bitmap as immutable, meaning that the contents of its
+        pixels will not change for the lifetime of the bitmap and of the
+        underlying pixelref. This state can be set, but it cannot be 
+        cleared once it is set. This state propagates to all other bitmaps
+        that share the same pixelref.
+    */
+    void setImmutable();
+
     /** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
     */
     bool isOpaque() const;
@@ -589,8 +602,9 @@ private:
     mutable int         fRawPixelGenerationID;
 
     enum Flags {
-        kImageIsOpaque_Flag = 0x01,
-        kImageIsVolatile_Flag     = 0x02
+        kImageIsOpaque_Flag     = 0x01,
+        kImageIsVolatile_Flag   = 0x02,
+        kImageIsImmutable_Flag  = 0x04
     };
 
     uint32_t    fRowBytes;
index 24a1839..2376dd3 100644 (file)
@@ -409,6 +409,7 @@ uint32_t SkBitmap::getGenerationID() const {
 }
 
 void SkBitmap::notifyPixelsChanged() const {
+    SkASSERT(!this->isImmutable());
     if (fPixelRef) {
         fPixelRef->notifyPixelsChanged();
     } else {
@@ -504,6 +505,19 @@ bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
 
 ///////////////////////////////////////////////////////////////////////////////
 
+bool SkBitmap::isImmutable() const { 
+    return fPixelRef ? fPixelRef->isImmutable() :
+        fFlags & kImageIsImmutable_Flag; 
+}
+
+void SkBitmap::setImmutable() {
+    if (fPixelRef) {
+        fPixelRef->setImmutable();
+    } else {
+        fFlags |= kImageIsImmutable_Flag;
+    }
+}
+
 bool SkBitmap::isOpaque() const {
     switch (fConfig) {
         case kNo_Config: