Fix android errors when unflattening an SkImageRef_ashmem object.
Review URL: https://codereview.appspot.com/7228071
git-svn-id: http://skia.googlecode.com/svn/trunk@7514 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/images.gyp b/gyp/images.gyp
index 1bfd2d3..8c4690e 100644
--- a/gyp/images.gyp
+++ b/gyp/images.gyp
@@ -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',
@@ -109,7 +111,8 @@
# end libpng stuff
}],
[ 'skia_os == "android"', {
- 'sources!': [
+ 'include_dirs': [
+ '../src/utils',
],
'dependencies': [
'android_deps.gyp:gif',
@@ -118,6 +121,11 @@
'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': [
diff --git a/gyp/ports.gyp b/gyp/ports.gyp
index 0b298d1..219bf3e 100644
--- a/gyp/ports.gyp
+++ b/gyp/ports.gyp
@@ -161,7 +161,6 @@
'../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',
diff --git a/include/core/SkPixelRef.h b/include/core/SkPixelRef.h
index 618cc71..89ce69a 100644
--- a/include/core/SkPixelRef.h
+++ b/include/core/SkPixelRef.h
@@ -216,13 +216,6 @@
// 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
diff --git a/include/images/SkImageRef.h b/include/images/SkImageRef.h
index dcc4c0e..bca4305 100644
--- a/include/images/SkImageRef.h
+++ b/include/images/SkImageRef.h
@@ -34,7 +34,7 @@
@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 @@
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;
diff --git a/src/images/SkImageRef.cpp b/src/images/SkImageRef.cpp
index 3ed62fb..299166c 100644
--- a/src/images/SkImageRef.cpp
+++ b/src/images/SkImageRef.cpp
@@ -15,14 +15,12 @@
//#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() {
- SkASSERT(&gImageRefMutex == this->mutex());
#ifdef DUMP_IMAGEREF_LIFECYCLE
SkDebugf("delete ImageRef %p [%d] data=%d\n",
@@ -51,7 +48,7 @@
}
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::prepareBitmap(SkImageDecoder::Mode mode) {
- SkASSERT(&gImageRefMutex == this->mutex());
if (fErrorInDecoding) {
return false;
@@ -144,8 +140,6 @@
}
void* SkImageRef::onLockPixels(SkColorTable** ct) {
- SkASSERT(&gImageRefMutex == this->mutex());
-
if (NULL == fBitmap.getPixels()) {
(void)this->prepareBitmap(SkImageDecoder::kDecodePixels_Mode);
}
@@ -156,11 +150,6 @@
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 @@
///////////////////////////////////////////////////////////////////////////////
-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();
diff --git a/src/images/SkImageRef_GlobalPool.cpp b/src/images/SkImageRef_GlobalPool.cpp
index e62816a..6af8653 100644
--- a/src/images/SkImageRef_GlobalPool.cpp
+++ b/src/images/SkImageRef_GlobalPool.cpp
@@ -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 @@
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 @@
}
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();
}
diff --git a/src/ports/SkImageRef_ashmem.cpp b/src/images/SkImageRef_ashmem.cpp
similarity index 97%
rename from src/ports/SkImageRef_ashmem.cpp
rename to src/images/SkImageRef_ashmem.cpp
index f8a9bb9..dc60465 100644
--- a/src/ports/SkImageRef_ashmem.cpp
+++ b/src/images/SkImageRef_ashmem.cpp
@@ -42,8 +42,6 @@
fRec.fPinned = false;
fCT = NULL;
-
- this->useDefaultMutex(); // we don't need/want the shared imageref mutex
}
SkImageRef_ashmem::~SkImageRef_ashmem() {
@@ -230,5 +228,4 @@
setURI(uri);
sk_free(uri);
}
- this->useDefaultMutex(); // we don't need/want the shared imageref mutex
}
diff --git a/src/ports/SkImageRef_ashmem.h b/src/images/SkImageRef_ashmem.h
similarity index 100%
rename from src/ports/SkImageRef_ashmem.h
rename to src/images/SkImageRef_ashmem.h
diff --git a/src/images/SkImages.cpp b/src/images/SkImages.cpp
index 0bcc33f..5b6bf6b 100644
--- a/src/images/SkImages.cpp
+++ b/src/images/SkImages.cpp
@@ -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
}
diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp
index 824895a..26a61ca 100644
--- a/src/ports/SkGlobalInitialization_default.cpp
+++ b/src/ports/SkGlobalInitialization_default.cpp
@@ -8,7 +8,6 @@
#include "SkTypes.h"
#include "SkBitmapProcShader.h"
-#include "SkImageRef_ashmem.h"
#include "SkMallocPixelRef.h"
#include "SkPathEffect.h"
#include "SkPixelRef.h"