friend class SkSurface_Raster;
- void init(SkBitmap::Config config, int width, int height, bool isOpaque);
+ // only call from constructor, to try to allocate the pixels
+ void init(SkBitmap::Config, int width, int height, bool isOpaque);
// used to change the backend's pixels (and possibly config/rowbytes)
// but cannot change the width/height, so there should be no change to
/**
* Return a new SkMallocPixelRef, automatically allocating storage for the
- * pixels. If rowBytes are 0, an optimal value will be chosen automatically.
- * If rowBytes is > 0, then it will be respected, or NULL will be returned
- * if rowBytes is invalid for the specified info.
+ * pixels.
+ *
+ * If rowBytes is 0, an optimal value will be chosen automatically.
+ * If rowBytes is > 0, then it will be used, unless it is invald for the
+ * specified info, in which case NULL will be returned (failure).
*
* This pixelref will ref() the specified colortable (if not NULL).
*
SkMallocPixelRef(SkFlattenableReadBuffer& buffer);
virtual ~SkMallocPixelRef();
- virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
+ virtual void* onLockPixels(SkColorTable**) SK_OVERRIDE;
virtual void onUnlockPixels() SK_OVERRIDE;
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
#include "SkRefCnt.h"
#include "SkString.h"
#include "SkFlattenable.h"
-#include "SkImageInfo.h"
#include "SkTDArray.h"
-//#define SK_SUPPORT_LEGACY_ONLOCKPIXELS
-
#ifdef SK_DEBUG
/**
* Defining SK_IGNORE_PIXELREF_SETPRELOCKED will force all pixelref
/** Return the pixel memory returned from lockPixels, or null if the
lockCount is 0.
*/
- void* pixels() const { return fRec.fPixels; }
+ void* pixels() const { return fPixels; }
/** Return the current colorTable (if any) if pixels are locked, or null.
*/
- SkColorTable* colorTable() const { return fRec.fColorTable; }
+ SkColorTable* colorTable() const { return fColorTable; }
/**
* To access the actual pixels of a pixelref, it must be "locked".
SkDEBUGCODE(int getLockCount() const { return fLockCount; })
- /**
- * Call to access the pixel memory. Return true on success. Balance this
- * with a call to unlockPixels().
- */
- bool lockPixels();
-
- /**
- * Call to access the pixel memory. On success, return true and fill out
- * the specified rec. On failure, return false and ignore the rec parameter.
- * Balance this with a call to unlockPixels().
- */
- bool lockPixels(LockRec* rec);
-
+ /** Call to access the pixel memory, which is returned. Balance with a call
+ to unlockPixels().
+ */
+ void lockPixels();
/** Call to balanace a previous call to lockPixels(). Returns the pixels
(or null) after the unlock. NOTE: lock calls can be nested, but the
matching number of unlock calls must be made in order to free the
void addGenIDChangeListener(GenIDChangeListener* listener);
protected:
-#ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS
- virtual void* onLockPixels(SkColorTable**);
- virtual bool onNewLockPixels(LockRec*);
-#else
- /**
- * On success, returns true and fills out the LockRec for the pixels. On
- * failure returns false and ignores the LockRec parameter.
- *
- * The caller will have already acquired a mutex for thread safety, so this
- * method need not do that.
- */
- virtual bool onNewLockPixels(LockRec*) = 0;
-#endif
+ /** Called when the lockCount goes from 0 to 1. The caller will have already
+ acquire a mutex for thread safety, so this method need not do that.
+ */
+ virtual void* onLockPixels(SkColorTable**) = 0;
/**
- * Balancing the previous successful call to onNewLockPixels. The locked
- * pixel address will no longer be referenced, so the subclass is free to
- * move or discard that memory.
+ * Called when the lock count goes from 1 to 0. The caller will have
+ * already acquire a mutex for thread safety, so this method need not do
+ * that.
*
- * The caller will have already acquired a mutex for thread safety, so this
- * method need not do that.
+ * If the previous call to onLockPixels failed (i.e. returned NULL), then
+ * the onUnlockPixels will NOT be called.
*/
virtual void onUnlockPixels() = 0;
// only call from constructor. Flags this to always be locked, removing
// the need to grab the mutex and call onLockPixels/onUnlockPixels.
// Performance tweak to avoid those calls (esp. in multi-thread use case).
- void setPreLocked(void*, size_t rowBytes, SkColorTable*);
+ void setPreLocked(void* pixels, SkColorTable* ctable);
private:
SkBaseMutex* fMutex; // must remain in scope for the life of this object
const SkImageInfo fInfo;
- // LockRec is only valid if we're in a locked state (isLocked())
- LockRec fRec;
+ void* fPixels;
+ SkColorTable* fColorTable; // we do not track ownership, subclass does
int fLockCount;
mutable uint32_t fGenerationID;
virtual ~SkROLockPixelsPixelRef();
protected:
- virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
- virtual void onUnlockPixels() SK_OVERRIDE;
- virtual bool onLockPixelsAreWritable() const SK_OVERRIDE; // return false;
+ // override from SkPixelRef
+ virtual void* onLockPixels(SkColorTable** ptr);
+ virtual void onUnlockPixels();
+ virtual bool onLockPixelsAreWritable() const; // return false;
private:
SkBitmap fBitmap;
When these are called, we will have already acquired the mutex!
*/
- virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
+ virtual void* onLockPixels(SkColorTable**);
// override this in your subclass to clean up when we're unlocking pixels
- virtual void onUnlockPixels() SK_OVERRIDE {}
+ virtual void onUnlockPixels() {}
SkImageRef(SkFlattenableReadBuffer&, SkBaseMutex* mutex = NULL);
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
void SkBitmapDevice::init(SkBitmap::Config config, int width, int height, bool isOpaque) {
fBitmap.setConfig(config, width, height, 0, isOpaque ?
kOpaque_SkAlphaType : kPremul_SkAlphaType);
-
+
if (SkBitmap::kNo_Config != config) {
if (!fBitmap.allocPixels()) {
// indicate failure by zeroing our bitmap
SkBitmapDevice::SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque,
const SkDeviceProperties& deviceProperties)
- : SkBaseDevice(deviceProperties)
+: SkBaseDevice(deviceProperties)
{
this->init(config, width, height, isOpaque);
}
fRB = rowBytes;
SkSafeRef(ctable);
- this->setPreLocked(fStorage, fRB, fCTable);
+ this->setPreLocked(fStorage, fCTable);
}
SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
fRB = rowBytes;
SkSafeRef(ctable);
- this->setPreLocked(fStorage, fRB, fCTable);
+ this->setPreLocked(fStorage, fCTable);
}
}
}
-bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) {
- rec->fPixels = fStorage;
- rec->fRowBytes = fRB;
- rec->fColorTable = fCTable;
- return true;
+void* SkMallocPixelRef::onLockPixels(SkColorTable** ctable) {
+ *ctable = fCTable;
+ return fStorage;
}
void SkMallocPixelRef::onUnlockPixels() {
fCTable = NULL;
}
- this->setPreLocked(fStorage, fRB, fCTable);
+ this->setPreLocked(fStorage, fCTable);
}
// just need a > 0 value, so pick a funny one to aid in debugging
#define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789
-SkPixelRef::SkPixelRef(const SkImageInfo& info) : fInfo(info) {
- this->setMutex(NULL);
- fRec.zero();
+SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) : fInfo(info) {
+ this->setMutex(mutex);
+ fPixels = NULL;
+ fColorTable = NULL; // we do not track ownership of this
fLockCount = 0;
this->needsNewGenID();
fIsImmutable = false;
fPreLocked = false;
}
-
-SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) : fInfo(info) {
- this->setMutex(mutex);
- fRec.zero();
+SkPixelRef::SkPixelRef(const SkImageInfo& info) : fInfo(info) {
+ this->setMutex(NULL);
+ fPixels = NULL;
+ fColorTable = NULL; // we do not track ownership of this
fLockCount = 0;
this->needsNewGenID();
fIsImmutable = false;
, fInfo(read_info(buffer))
{
this->setMutex(mutex);
- fRec.zero();
+ fPixels = NULL;
+ fColorTable = NULL; // we do not track ownership of this
fLockCount = 0;
fIsImmutable = buffer.readBool();
fGenerationID = buffer.readUInt();
that.fUniqueGenerationID = false;
}
-void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctable) {
+void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) {
#ifndef SK_IGNORE_PIXELREF_SETPRELOCKED
// only call me in your constructor, otherwise fLockCount tracking can get
// out of sync.
- fRec.fPixels = pixels;
- fRec.fColorTable = ctable;
- fRec.fRowBytes = rowBytes;
+ fPixels = pixels;
+ fColorTable = ctable;
fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT;
fPreLocked = true;
#endif
}
}
-bool SkPixelRef::lockPixels(LockRec* rec) {
+void SkPixelRef::lockPixels() {
SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
if (!fPreLocked) {
SkAutoMutexAcquire ac(*fMutex);
if (1 == ++fLockCount) {
- SkASSERT(fRec.isZero());
-
- LockRec rec;
- if (!this->onNewLockPixels(&rec)) {
- return false;
+ fPixels = this->onLockPixels(&fColorTable);
+ // If onLockPixels failed, it will return NULL
+ if (NULL == fPixels) {
+ fColorTable = NULL;
}
- 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() {
SkASSERT(fLockCount > 0);
if (0 == --fLockCount) {
// don't call onUnlockPixels unless onLockPixels succeeded
- if (fRec.fPixels) {
+ if (fPixels) {
this->onUnlockPixels();
- fRec.zero();
+ fPixels = NULL;
+ fColorTable = NULL;
} else {
- SkASSERT(fRec.isZero());
+ SkASSERT(NULL == fColorTable);
}
}
}
///////////////////////////////////////////////////////////////////////////////
-#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();
SK_DECLARE_UNFLATTENABLE_OBJECT()
protected:
- virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
+ virtual void* onLockPixels(SkColorTable**) 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;
, fDM(dm)
, fRB(rowBytes)
{
+ fInfo = info; // remove this redundant field when SkPixelRef has info
+
SkASSERT(dm->data());
fFirstTime = true;
}
SkDELETE(fDM);
}
-bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) {
+void* SkOneShotDiscardablePixelRef::onLockPixels(SkColorTable** ctable) {
if (fFirstTime) {
// we're already locked
SkASSERT(fDM->data());
fFirstTime = false;
- goto SUCCESS;
+ return fDM->data();
}
// A previous call to onUnlock may have deleted our DM, so check for that
if (NULL == fDM) {
- return false;
+ return NULL;
}
if (!fDM->lock()) {
// since it failed, we delete it now, to free-up the resource
delete fDM;
fDM = NULL;
- return false;
+ return NULL;
}
-
-SUCCESS:
- rec->fPixels = fDM->data();
- rec->fColorTable = NULL;
- rec->fRowBytes = fRB;
- return true;
+ return fDM->data();
}
void SkOneShotDiscardablePixelRef::onUnlockPixels() {
}
size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const {
- return this->info().getSafeSize(fRB);
+ return fInfo.fHeight * fRB;
}
class SkScaledImageCacheDiscardableAllocator : public SkBitmap::Allocator {
SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {}
-bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) {
+void* SkROLockPixelsPixelRef::onLockPixels(SkColorTable** ctable) {
+ if (ctable) {
+ *ctable = NULL;
+ }
fBitmap.reset();
// SkDebugf("---------- calling readpixels in support of lockpixels\n");
if (!this->onReadPixels(&fBitmap, NULL)) {
SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n");
- return false;
+ return NULL;
}
fBitmap.lockPixels();
- if (NULL == fBitmap.getPixels()) {
- return false;
- }
-
- rec->fPixels = fBitmap.getPixels();
- rec->fColorTable = NULL;
- rec->fRowBytes = fBitmap.rowBytes();
- return true;
+ return fBitmap.getPixels();
}
void SkROLockPixelsPixelRef::onUnlockPixels() {
//#define DUMP_IMAGEREF_LIFECYCLE
+
///////////////////////////////////////////////////////////////////////////////
SkImageRef::SkImageRef(const SkImageInfo& info, SkStreamRewindable* stream,
int sampleSize, SkBaseMutex* mutex)
- : INHERITED(info, mutex), fErrorInDecoding(false)
-{
+ : SkPixelRef(info, mutex), fErrorInDecoding(false) {
SkASSERT(stream);
stream->ref();
fStream = stream;
#ifdef DUMP_IMAGEREF_LIFECYCLE
SkDebugf("delete ImageRef %p [%d] data=%d\n",
- this, this->info().fColorType, (int)fStream->getLength());
+ this, fConfig, (int)fStream->getLength());
#endif
fStream->unref();
return false;
}
-bool SkImageRef::onNewLockPixels(LockRec* rec) {
+void* SkImageRef::onLockPixels(SkColorTable** ct) {
if (NULL == fBitmap.getPixels()) {
(void)this->prepareBitmap(SkImageDecoder::kDecodePixels_Mode);
}
- if (NULL == fBitmap.getPixels()) {
- return false;
+ if (ct) {
+ *ct = fBitmap.getColorTable();
}
- rec->fPixels = fBitmap.getPixels();
- rec->fColorTable = NULL;
- rec->fRowBytes = fBitmap.rowBytes();
- return true;
+ return fBitmap.getPixels();
}
size_t SkImageRef::ramUsed() const {
}
}
-bool SkImageRef_ashmem::onNewLockPixels(LockRec* rec) {
+void* SkImageRef_ashmem::onLockPixels(SkColorTable** ct) {
SkASSERT(fBitmap.getPixels() == NULL);
SkASSERT(fBitmap.getColorTable() == NULL);
#endif
} else {
SkDebugf("===== ashmem pin_region(%d) returned %d\n", fRec.fFD, pin);
- return false;
+ // return null result for failure
+ if (ct) {
+ *ct = NULL;
+ }
+ return NULL;
}
} else {
// no FD, will create an ashmem region in allocator
}
- return this->INHERITED::onNewLockPixels(rec);
+ return this->INHERITED::onLockPixels(ct);
}
void SkImageRef_ashmem::onUnlockPixels() {
SkBitmap* bitmap, SkBitmap::Config config,
SkImageDecoder::Mode mode);
- virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
- virtual void onUnlockPixels() SK_OVERRIDE;
+ virtual void* onLockPixels(SkColorTable**);
+ virtual void onUnlockPixels();
private:
void closeFD();
#include "SkCachingPixelRef.h"
#include "SkScaledImageCache.h"
+
bool SkCachingPixelRef::Install(SkImageGenerator* generator,
SkBitmap* dst) {
SkImageInfo info;
// Assert always unlock before unref.
}
-bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) {
+void* SkCachingPixelRef::onLockPixels(SkColorTable**) {
if (fErrorInDecoding) {
- return false; // don't try again.
+ return NULL; // don't try again.
}
-
+
const SkImageInfo& info = this->info();
+
SkBitmap bitmap;
SkASSERT(NULL == fScaledCacheId);
fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
// Cache has been purged, must re-decode.
if ((!bitmap.setConfig(info, fRowBytes)) || !bitmap.allocPixels()) {
fErrorInDecoding = true;
- return false;
+ return NULL;
}
SkAutoLockPixels autoLockPixels(bitmap);
if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) {
fErrorInDecoding = true;
- return false;
+ return NULL;
}
fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
info.fWidth,
// bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
// reference to the concrete PixelRef while this record is
// locked.
- rec->fPixels = pixels;
- rec->fColorTable = NULL;
- rec->fRowBytes = bitmap.rowBytes();
- return true;
+ return pixels;
}
void SkCachingPixelRef::onUnlockPixels() {
protected:
virtual ~SkCachingPixelRef();
- virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
+ virtual void* onLockPixels(SkColorTable** colorTable) SK_OVERRIDE;
virtual void onUnlockPixels() SK_OVERRIDE;
virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
SkDELETE(fGenerator);
}
-bool SkDiscardablePixelRef::onNewLockPixels(LockRec* rec) {
+void* SkDiscardablePixelRef::onLockPixels(SkColorTable**) {
if (fDiscardableMemory != NULL) {
if (fDiscardableMemory->lock()) {
- rec->fPixels = fDiscardableMemory->data();
- rec->fColorTable = NULL;
- rec->fRowBytes = fRowBytes;
- return true;
+ return fDiscardableMemory->data();
}
SkDELETE(fDiscardableMemory);
fDiscardableMemory = NULL;
fDiscardableMemory = SkDiscardableMemory::Create(size);
}
if (NULL == fDiscardableMemory) {
- return false; // Memory allocation failed.
+ return NULL; // Memory allocation failed.
}
-
void* pixels = fDiscardableMemory->data();
if (!fGenerator->getPixels(this->info(), pixels, fRowBytes)) {
fDiscardableMemory->unlock();
SkDELETE(fDiscardableMemory);
fDiscardableMemory = NULL;
- return false;
+ return NULL;
}
-
- rec->fPixels = pixels;
- rec->fColorTable = NULL;
- rec->fRowBytes = fRowBytes;
- return true;
+ return pixels;
}
-
void SkDiscardablePixelRef::onUnlockPixels() {
fDiscardableMemory->unlock();
}
protected:
~SkDiscardablePixelRef();
-
- virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
+ virtual void* onLockPixels(SkColorTable**) SK_OVERRIDE;
virtual void onUnlockPixels() SK_OVERRIDE;
virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
SkDiscardablePixelRef(const SkImageInfo&, SkImageGenerator*,
size_t rowBytes,
SkDiscardableMemory::Factory* factory);
-
friend bool SkInstallDiscardablePixelRef(SkImageGenerator*,
SkBitmap*,
SkDiscardableMemory::Factory*);
-
typedef SkPixelRef INHERITED;
};
-
#endif // SkDiscardablePixelRef_DEFINED