diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-01-03 16:22:48 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-01-03 16:22:48 +0000 |
commit | f1ce05288a8fee6e691c3dff5db9dcd47818060f (patch) | |
tree | 935a2efe666716d19521786d0950a40153496842 /src/core | |
parent | 0bf919c89a5cd8d1779cdbd5d17536c2fbf534c8 (diff) |
Add onNewLockPixels, that returns rowbytes and relies on info in pixelref
This reverts commit 890a6ec633c1f54891104a072a8964b4c2c81af9.
BUG=
R=scroggo@google.com
Review URL: https://codereview.chromium.org/110593003
git-svn-id: http://skia.googlecode.com/svn/trunk@12883 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/SkBitmapDevice.cpp | 35 | ||||
-rw-r--r-- | src/core/SkMallocPixelRef.cpp | 16 | ||||
-rw-r--r-- | src/core/SkPixelRef.cpp | 79 | ||||
-rw-r--r-- | src/core/SkScaledImageCache.cpp | 23 |
4 files changed, 93 insertions, 60 deletions
diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp index 1668618cf7..368c807511 100644 --- a/src/core/SkBitmapDevice.cpp +++ b/src/core/SkBitmapDevice.cpp @@ -24,31 +24,30 @@ SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkDeviceProperties& , fBitmap(bitmap) { } -SkBitmapDevice::SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque) { +void SkBitmapDevice::init(SkBitmap::Config config, int width, int height, bool isOpaque) { fBitmap.setConfig(config, width, height, 0, isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); - if (!fBitmap.allocPixels()) { - fBitmap.setConfig(config, 0, 0, 0, isOpaque ? - kOpaque_SkAlphaType : kPremul_SkAlphaType); - } - if (!isOpaque) { - fBitmap.eraseColor(SK_ColorTRANSPARENT); + + if (SkBitmap::kNo_Config != config) { + if (!fBitmap.allocPixels()) { + // indicate failure by zeroing our bitmap + fBitmap.setConfig(config, 0, 0, 0, isOpaque ? + kOpaque_SkAlphaType : kPremul_SkAlphaType); + } else if (!isOpaque) { + fBitmap.eraseColor(SK_ColorTRANSPARENT); + } } } +SkBitmapDevice::SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque) { + this->init(config, width, height, isOpaque); +} + SkBitmapDevice::SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque, const SkDeviceProperties& deviceProperties) - : SkBaseDevice(deviceProperties) { - - fBitmap.setConfig(config, width, height, 0, isOpaque ? - kOpaque_SkAlphaType : kPremul_SkAlphaType); - if (!fBitmap.allocPixels()) { - fBitmap.setConfig(config, 0, 0, 0, isOpaque ? - kOpaque_SkAlphaType : kPremul_SkAlphaType); - } - if (!isOpaque) { - fBitmap.eraseColor(SK_ColorTRANSPARENT); - } + : SkBaseDevice(deviceProperties) +{ + this->init(config, width, height, isOpaque); } SkBitmapDevice::~SkBitmapDevice() { diff --git a/src/core/SkMallocPixelRef.cpp b/src/core/SkMallocPixelRef.cpp index c3e605c358..c86d33bf18 100644 --- a/src/core/SkMallocPixelRef.cpp +++ b/src/core/SkMallocPixelRef.cpp @@ -152,7 +152,7 @@ SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, fRB = rowBytes; SkSafeRef(ctable); - this->setPreLocked(fStorage, fCTable); + this->setPreLocked(fStorage, fRB, fCTable); } SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, @@ -174,8 +174,8 @@ SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, fCTable = ctable; fRB = rowBytes; SkSafeRef(ctable); - - this->setPreLocked(fStorage, fCTable); + + this->setPreLocked(fStorage, fRB, fCTable); } @@ -186,9 +186,11 @@ SkMallocPixelRef::~SkMallocPixelRef() { } } -void* SkMallocPixelRef::onLockPixels(SkColorTable** ctable) { - *ctable = fCTable; - return fStorage; +bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) { + rec->fPixels = fStorage; + rec->fRowBytes = fRB; + rec->fColorTable = fCTable; + return true; } void SkMallocPixelRef::onUnlockPixels() { @@ -234,5 +236,5 @@ SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer) fCTable = NULL; } - this->setPreLocked(fStorage, fCTable); + this->setPreLocked(fStorage, fRB, fCTable); } diff --git a/src/core/SkPixelRef.cpp b/src/core/SkPixelRef.cpp index fedbb5ac7d..7ec5929669 100644 --- a/src/core/SkPixelRef.cpp +++ b/src/core/SkPixelRef.cpp @@ -82,20 +82,19 @@ void SkPixelRef::setMutex(SkBaseMutex* mutex) { // just need a > 0 value, so pick a funny one to aid in debugging #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 -SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) : fInfo(info) { - this->setMutex(mutex); - fPixels = NULL; - fColorTable = NULL; // we do not track ownership of this +SkPixelRef::SkPixelRef(const SkImageInfo& info) : fInfo(info) { + this->setMutex(NULL); + fRec.zero(); fLockCount = 0; this->needsNewGenID(); fIsImmutable = false; fPreLocked = false; } -SkPixelRef::SkPixelRef(const SkImageInfo& info) : fInfo(info) { - this->setMutex(NULL); - fPixels = NULL; - fColorTable = NULL; // we do not track ownership of this + +SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) : fInfo(info) { + this->setMutex(mutex); + fRec.zero(); fLockCount = 0; this->needsNewGenID(); fIsImmutable = false; @@ -113,8 +112,7 @@ SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) , fInfo(read_info(buffer)) { this->setMutex(mutex); - fPixels = NULL; - fColorTable = NULL; // we do not track ownership of this + fRec.zero(); fLockCount = 0; fIsImmutable = buffer.readBool(); fGenerationID = buffer.readUInt(); @@ -138,12 +136,13 @@ void SkPixelRef::cloneGenID(const SkPixelRef& that) { that.fUniqueGenerationID = false; } -void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { +void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctable) { #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED // only call me in your constructor, otherwise fLockCount tracking can get // out of sync. - fPixels = pixels; - fColorTable = ctable; + fRec.fPixels = pixels; + fRec.fColorTable = ctable; + fRec.fRowBytes = rowBytes; fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; fPreLocked = true; #endif @@ -166,20 +165,30 @@ void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { } } -void SkPixelRef::lockPixels() { +bool SkPixelRef::lockPixels(LockRec* rec) { SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); - + if (!fPreLocked) { SkAutoMutexAcquire ac(*fMutex); - + if (1 == ++fLockCount) { - fPixels = this->onLockPixels(&fColorTable); - // If onLockPixels failed, it will return NULL - if (NULL == fPixels) { - fColorTable = NULL; + SkASSERT(fRec.isZero()); + + LockRec rec; + if (!this->onNewLockPixels(&rec)) { + return false; } + SkASSERT(!rec.isZero()); // else why did onNewLock return true? + fRec = rec; } } + *rec = fRec; + return true; +} + +bool SkPixelRef::lockPixels() { + LockRec rec; + return this->lockPixels(&rec); } void SkPixelRef::unlockPixels() { @@ -191,12 +200,11 @@ void SkPixelRef::unlockPixels() { SkASSERT(fLockCount > 0); if (0 == --fLockCount) { // don't call onUnlockPixels unless onLockPixels succeeded - if (fPixels) { + if (fRec.fPixels) { this->onUnlockPixels(); - fPixels = NULL; - fColorTable = NULL; + fRec.zero(); } else { - SkASSERT(NULL == fColorTable); + SkASSERT(fRec.isZero()); } } } @@ -278,6 +286,29 @@ size_t SkPixelRef::getAllocatedSizeInBytes() const { /////////////////////////////////////////////////////////////////////////////// +#ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS + +void* SkPixelRef::onLockPixels(SkColorTable** ctable) { + return NULL; +} + +bool SkPixelRef::onNewLockPixels(LockRec* rec) { + SkColorTable* ctable; + void* pixels = this->onLockPixels(&ctable); + if (!pixels) { + return false; + } + + rec->fPixels = pixels; + rec->fColorTable = ctable; + rec->fRowBytes = 0; // callers don't currently need this (thank goodness) + return true; +} + +#endif + +/////////////////////////////////////////////////////////////////////////////// + #ifdef SK_BUILD_FOR_ANDROID void SkPixelRef::globalRef(void* data) { this->ref(); diff --git a/src/core/SkScaledImageCache.cpp b/src/core/SkScaledImageCache.cpp index 45a5684638..5a772a7cd5 100644 --- a/src/core/SkScaledImageCache.cpp +++ b/src/core/SkScaledImageCache.cpp @@ -199,13 +199,11 @@ public: SK_DECLARE_UNFLATTENABLE_OBJECT() protected: - virtual void* onLockPixels(SkColorTable**) SK_OVERRIDE; + virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE; virtual void onUnlockPixels() SK_OVERRIDE; virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE; private: - SkImageInfo fInfo; // remove when SkPixelRef gets this in baseclass - SkDiscardableMemory* fDM; size_t fRB; bool fFirstTime; @@ -220,8 +218,6 @@ SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& in , fDM(dm) , fRB(rowBytes) { - fInfo = info; // remove this redundant field when SkPixelRef has info - SkASSERT(dm->data()); fFirstTime = true; } @@ -230,26 +226,31 @@ SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { SkDELETE(fDM); } -void* SkOneShotDiscardablePixelRef::onLockPixels(SkColorTable** ctable) { +bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { if (fFirstTime) { // we're already locked SkASSERT(fDM->data()); fFirstTime = false; - return fDM->data(); + goto SUCCESS; } // A previous call to onUnlock may have deleted our DM, so check for that if (NULL == fDM) { - return NULL; + return false; } if (!fDM->lock()) { // since it failed, we delete it now, to free-up the resource delete fDM; fDM = NULL; - return NULL; + return false; } - return fDM->data(); + +SUCCESS: + rec->fPixels = fDM->data(); + rec->fColorTable = NULL; + rec->fRowBytes = fRB; + return true; } void SkOneShotDiscardablePixelRef::onUnlockPixels() { @@ -258,7 +259,7 @@ void SkOneShotDiscardablePixelRef::onUnlockPixels() { } size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { - return fInfo.fHeight * fRB; + return this->info().getSafeSize(fRB); } class SkScaledImageCacheDiscardableAllocator : public SkBitmap::Allocator { |