Fix android errors when unflattening an SkImageRef_ashmem object.
authordjsollen@google.com <djsollen@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 1 Feb 2013 15:57:13 +0000 (15:57 +0000)
committerdjsollen@google.com <djsollen@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 1 Feb 2013 15:57:13 +0000 (15:57 +0000)
Review URL: https://codereview.appspot.com/7228071

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

gyp/images.gyp
gyp/ports.gyp
include/core/SkPixelRef.h
include/images/SkImageRef.h
src/images/SkImageRef.cpp
src/images/SkImageRef_GlobalPool.cpp
src/images/SkImageRef_ashmem.cpp [moved from src/ports/SkImageRef_ashmem.cpp with 97% similarity]
src/images/SkImageRef_ashmem.h [moved from src/ports/SkImageRef_ashmem.h with 100% similarity]
src/images/SkImages.cpp
src/ports/SkGlobalInitialization_default.cpp

index 1bfd2d3..8c4690e 100644 (file)
@@ -44,6 +44,8 @@
         '../src/images/SkImageRef.cpp',
         '../src/images/SkImageRefPool.cpp',
         '../src/images/SkImageRefPool.h',
+        '../src/images/SkImageRef_ashmem.h',
+        '../src/images/SkImageRef_ashmem.cpp',
         '../src/images/SkImageRef_GlobalPool.cpp',
         '../src/images/SkImages.cpp',
         '../src/images/SkJpegUtility.cpp',
           # end libpng stuff
         }],
         [ 'skia_os == "android"', {
-          'sources!': [
+          'include_dirs': [
+             '../src/utils',
           ],
           'dependencies': [
              'android_deps.gyp:gif',
           'defines': [
             'SK_ENABLE_LIBPNG',
           ],
+        },{ #else if skia_os != android
+          'sources!': [
+            '../src/images/SkImageRef_ashmem.h',
+            '../src/images/SkImageRef_ashmem.cpp',
+          ],
         }],
         [ 'skia_os == "ios"', {
            'include_dirs': [
index 0b298d1..219bf3e 100644 (file)
             '../src/ports/SkFontHost_FreeType.cpp',
             '../src/ports/SkFontHost_FreeType_common.cpp',
             '../src/ports/FontHostConfiguration_android.cpp',
-            '../src/ports/SkImageRef_ashmem.cpp',
           ],
           'dependencies': [
              'freetype.gyp:freetype',
index 618cc71..89ce69a 100644 (file)
@@ -216,13 +216,6 @@ protected:
     // Performance tweak to avoid those calls (esp. in multi-thread use case).
     void setPreLocked(void* pixels, SkColorTable* ctable);
 
-    /**
-     *  If a subclass passed a particular mutex to the base constructor, it can
-     *  override that to go back to the default mutex by calling this. However,
-     *  this should only be called from within the subclass' constructor.
-     */
-    void useDefaultMutex() { this->setMutex(NULL); }
-
 private:
 
     SkBaseMutex*    fMutex; // must remain in scope for the life of this object
index dcc4c0e..bca4305 100644 (file)
@@ -34,7 +34,7 @@ public:
         @param config The preferred config of the decoded bitmap.
         @param sampleSize Requested sampleSize for decoding. Defaults to 1.
     */
-    SkImageRef(SkStream*, SkBitmap::Config config, int sampleSize = 1);
+    SkImageRef(SkStream*, SkBitmap::Config config, int sampleSize = 1, SkBaseMutex* mutex = NULL);
     virtual ~SkImageRef();
 
     /** this value is passed onto the decoder. Default is true
@@ -73,9 +73,9 @@ protected:
 
     virtual void* onLockPixels(SkColorTable**);
     // override this in your subclass to clean up when we're unlocking pixels
-    virtual void onUnlockPixels();
+    virtual void onUnlockPixels() {}
 
-    SkImageRef(SkFlattenableReadBuffer&);
+    SkImageRef(SkFlattenableReadBuffer&, SkBaseMutex* mutex = NULL);
     virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
 
     SkBitmap fBitmap;
index 3ed62fb..299166c 100644 (file)
 
 //#define DUMP_IMAGEREF_LIFECYCLE
 
-// can't be static, as SkImageRef_Pool needs to see it
-SK_DECLARE_GLOBAL_MUTEX(gImageRefMutex);
 
 ///////////////////////////////////////////////////////////////////////////////
 
 SkImageRef::SkImageRef(SkStream* stream, SkBitmap::Config config,
-                       int sampleSize)
-        : SkPixelRef(&gImageRefMutex), fErrorInDecoding(false) {
+                       int sampleSize, SkBaseMutex* mutex)
+        : SkPixelRef(mutex), fErrorInDecoding(false) {
     SkASSERT(stream);
     stream->ref();
     fStream = stream;
@@ -39,7 +37,6 @@ SkImageRef::SkImageRef(SkStream* stream, SkBitmap::Config config,
 }
 
 SkImageRef::~SkImageRef() {
-    SkASSERT(&gImageRefMutex == this->mutex());
 
 #ifdef DUMP_IMAGEREF_LIFECYCLE
     SkDebugf("delete ImageRef %p [%d] data=%d\n",
@@ -51,7 +48,7 @@ SkImageRef::~SkImageRef() {
 }
 
 bool SkImageRef::getInfo(SkBitmap* bitmap) {
-    SkAutoMutexAcquire ac(gImageRefMutex);
+    SkAutoMutexAcquire ac(this->mutex());
 
     if (!this->prepareBitmap(SkImageDecoder::kDecodeBounds_Mode)) {
         return false;
@@ -89,7 +86,6 @@ bool SkImageRef::onDecode(SkImageDecoder* codec, SkStream* stream,
 }
 
 bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) {
-    SkASSERT(&gImageRefMutex == this->mutex());
 
     if (fErrorInDecoding) {
         return false;
@@ -144,8 +140,6 @@ bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) {
 }
 
 void* SkImageRef::onLockPixels(SkColorTable** ct) {
-    SkASSERT(&gImageRefMutex == this->mutex());
-
     if (NULL == fBitmap.getPixels()) {
         (void)this->prepareBitmap(SkImageDecoder::kDecodePixels_Mode);
     }
@@ -156,11 +150,6 @@ void* SkImageRef::onLockPixels(SkColorTable** ct) {
     return fBitmap.getPixels();
 }
 
-void SkImageRef::onUnlockPixels() {
-    // we're already have the mutex locked
-    SkASSERT(&gImageRefMutex == this->mutex());
-}
-
 size_t SkImageRef::ramUsed() const {
     size_t size = 0;
 
@@ -175,8 +164,8 @@ size_t SkImageRef::ramUsed() const {
 
 ///////////////////////////////////////////////////////////////////////////////
 
-SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer)
-        : INHERITED(buffer, &gImageRefMutex), fErrorInDecoding(false) {
+SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
+        : INHERITED(buffer, mutex), fErrorInDecoding(false) {
     fConfig = (SkBitmap::Config)buffer.readUInt();
     fSampleSize = buffer.readInt();
     fDoDither = buffer.readBool();
index e62816a..6af8653 100644 (file)
@@ -9,7 +9,7 @@
 #include "SkImageRefPool.h"
 #include "SkThread.h"
 
-extern SkBaseMutex gImageRefMutex;
+SK_DECLARE_STATIC_MUTEX(gGlobalPoolMutex);
 
 /*
  *  This returns the lazily-allocated global pool. It must be called
@@ -27,16 +27,16 @@ static SkImageRefPool* GetGlobalPool() {
 SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStream* stream,
                                              SkBitmap::Config config,
                                              int sampleSize)
-        : SkImageRef(stream, config, sampleSize) {
-    this->mutex()->acquire();
+        : SkImageRef(stream, config, sampleSize, &gGlobalPoolMutex) {
+    SkASSERT(&gGlobalPoolMutex == this->mutex());
+    SkAutoMutexAcquire ac(gGlobalPoolMutex);
     GetGlobalPool()->addToHead(this);
-    this->mutex()->release();
 }
 
 SkImageRef_GlobalPool::~SkImageRef_GlobalPool() {
-    this->mutex()->acquire();
+    SkASSERT(&gGlobalPoolMutex == this->mutex());
+    SkAutoMutexAcquire ac(gGlobalPoolMutex);
     GetGlobalPool()->detach(this);
-    this->mutex()->release();
 }
 
 /*  By design, onUnlockPixels() already is inside the mutex-lock,
@@ -65,36 +65,36 @@ void SkImageRef_GlobalPool::onUnlockPixels() {
 }
 
 SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkFlattenableReadBuffer& buffer)
-        : INHERITED(buffer) {
-    this->mutex()->acquire();
+        : INHERITED(buffer, &gGlobalPoolMutex) {
+    SkASSERT(&gGlobalPoolMutex == this->mutex());
+    SkAutoMutexAcquire ac(gGlobalPoolMutex);
     GetGlobalPool()->addToHead(this);
-    this->mutex()->release();
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 // global imagerefpool wrappers
 
 size_t SkImageRef_GlobalPool::GetRAMBudget() {
-    SkAutoMutexAcquire ac(gImageRefMutex);
+    SkAutoMutexAcquire ac(gGlobalPoolMutex);
     return GetGlobalPool()->getRAMBudget();
 }
 
 void SkImageRef_GlobalPool::SetRAMBudget(size_t size) {
-    SkAutoMutexAcquire ac(gImageRefMutex);
+    SkAutoMutexAcquire ac(gGlobalPoolMutex);
     GetGlobalPool()->setRAMBudget(size);
 }
 
 size_t SkImageRef_GlobalPool::GetRAMUsed() {
-    SkAutoMutexAcquire ac(gImageRefMutex);
+    SkAutoMutexAcquire ac(gGlobalPoolMutex);
     return GetGlobalPool()->getRAMUsed();
 }
 
 void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) {
-    SkAutoMutexAcquire ac(gImageRefMutex);
+    SkAutoMutexAcquire ac(gGlobalPoolMutex);
     GetGlobalPool()->setRAMUsed(usage);
 }
 
 void SkImageRef_GlobalPool::DumpPool() {
-    SkAutoMutexAcquire ac(gImageRefMutex);
+    SkAutoMutexAcquire ac(gGlobalPoolMutex);
     GetGlobalPool()->dump();
 }
similarity index 97%
rename from src/ports/SkImageRef_ashmem.cpp
rename to src/images/SkImageRef_ashmem.cpp
index f8a9bb9..dc60465 100644 (file)
@@ -42,8 +42,6 @@ SkImageRef_ashmem::SkImageRef_ashmem(SkStream* stream,
     fRec.fPinned = false;
 
     fCT = NULL;
-
-    this->useDefaultMutex();   // we don't need/want the shared imageref mutex
 }
 
 SkImageRef_ashmem::~SkImageRef_ashmem() {
@@ -230,5 +228,4 @@ SkImageRef_ashmem::SkImageRef_ashmem(SkFlattenableReadBuffer& buffer)
         setURI(uri);
         sk_free(uri);
     }
-    this->useDefaultMutex();   // we don't need/want the shared imageref mutex
 }
index 0bcc33f..5b6bf6b 100644 (file)
@@ -9,6 +9,13 @@
 #include "SkImageRef_GlobalPool.h"
 #include "SkImages.h"
 
+#ifdef SK_BUILD_FOR_ANDROID
+#include "SkImageRef_ashmem.h"
+#endif
+
 void SkImages::InitializeFlattenables() {
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkImageRef_GlobalPool)
+#ifdef SK_BUILD_FOR_ANDROID
+    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkImageRef_ashmem)
+#endif
 }
index 824895a..26a61ca 100644 (file)
@@ -8,7 +8,6 @@
 #include "SkTypes.h"
 
 #include "SkBitmapProcShader.h"
-#include "SkImageRef_ashmem.h"
 #include "SkMallocPixelRef.h"
 #include "SkPathEffect.h"
 #include "SkPixelRef.h"