*/
bool empty() const { return 0 == fWidth || 0 == fHeight; }
- /** Return true iff the bitmap has no pixels nor a pixelref. Note: this can
- return true even if the dimensions of the bitmap are > 0 (see empty()).
+ /** Return true iff the bitmap has no pixelref. Note: this can return true even if the
+ dimensions of the bitmap are > 0 (see empty()).
*/
- bool isNull() const { return NULL == fPixels && NULL == fPixelRef; }
+ bool isNull() const { return NULL == fPixelRef; }
/** Return the config for the bitmap.
*/
SkColorTable* getColorTable() const { return fColorTable; }
/** Returns a non-zero, unique value corresponding to the pixels in our
- pixelref (or raw pixels set via setPixels). Each time the pixels are
- changed (and notifyPixelsChanged is called), a different generation ID
- will be returned.
+ pixelref. Each time the pixels are changed (and notifyPixelsChanged
+ is called), a different generation ID will be returned. Finally, if
+ their is no pixelRef then zero is returned.
*/
uint32_t getGenerationID() const;
// or a cache of the returned value from fPixelRef->lockPixels()
mutable void* fPixels;
mutable SkColorTable* fColorTable; // only meaningful for kIndex8
- // When there is no pixel ref (setPixels was called) we still need a
- // gen id for SkDevice implementations that may cache a copy of the
- // pixels (e.g. as a gpu texture)
- mutable int fRawPixelGenerationID;
enum Flags {
kImageIsOpaque_Flag = 0x01,
SK_DEFINE_INST_COUNT(SkBitmap::Allocator)
-extern int32_t SkNextPixelRefGenerationID();
-
static bool isPos32Bits(const Sk64& value) {
return !value.isNeg() && value.is32();
}
SkTSwap(fPixelLockCount, other.fPixelLockCount);
SkTSwap(fMipMap, other.fMipMap);
SkTSwap(fPixels, other.fPixels);
- SkTSwap(fRawPixelGenerationID, other.fRawPixelGenerationID);
SkTSwap(fRowBytes, other.fRowBytes);
SkTSwap(fWidth, other.fWidth);
SkTSwap(fHeight, other.fHeight);
}
bool SkBitmap::lockPixelsAreWritable() const {
- if (fPixelRef) {
- return fPixelRef->lockPixelsAreWritable();
- } else {
- return fPixels != NULL;
- }
+ return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
}
void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
- this->freePixels();
- fPixels = p;
- SkRefCnt_SafeAssign(fColorTable, ctable);
+ Sk64 size = this->getSize64();
+ SkASSERT(!size.isNeg() && size.is32());
+ this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unref();
+ // since we're already allocated, we lockPixels right away
+ this->lockPixels();
SkDEBUGCODE(this->validate();)
}
}
uint32_t SkBitmap::getGenerationID() const {
- if (fPixelRef) {
- return fPixelRef->getGenerationID();
- } else {
- SkASSERT(fPixels || !fRawPixelGenerationID);
- if (fPixels && !fRawPixelGenerationID) {
- fRawPixelGenerationID = SkNextPixelRefGenerationID();
- }
- return fRawPixelGenerationID;
- }
+ return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
}
void SkBitmap::notifyPixelsChanged() const {
SkASSERT(!this->isImmutable());
if (fPixelRef) {
fPixelRef->notifyPixelsChanged();
- } else {
- fRawPixelGenerationID = 0; // will grab next ID in getGenerationID
}
}
bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
SkDEBUGCODE(this->validate();)
- if (NULL == result || (NULL == fPixelRef && NULL == fPixels)) {
+ if (NULL == result || NULL == fPixelRef) {
return false; // no src pixels
}
if (fPixelRef) {
// share the pixelref with a custom offset
dst.setPixelRef(fPixelRef, fPixelRefOffset + offset);
- } else {
- // share the pixels (owned by the caller)
- dst.setPixels((char*)fPixels + offset, this->getColorTable());
}
SkDEBUGCODE(dst.validate();)
enum {
SERIALIZE_PIXELTYPE_NONE,
- SERIALIZE_PIXELTYPE_RAW_WITH_CTABLE,
- SERIALIZE_PIXELTYPE_RAW_NO_CTABLE,
SERIALIZE_PIXELTYPE_REF_DATA,
SERIALIZE_PIXELTYPE_REF_PTR
};
}
// if we get here, we can't record the pixels
buffer.write8(SERIALIZE_PIXELTYPE_NONE);
- } else if (fPixels) {
- if (fColorTable) {
- buffer.write8(SERIALIZE_PIXELTYPE_RAW_WITH_CTABLE);
- buffer.writeFlattenable(fColorTable);
- } else {
- buffer.write8(SERIALIZE_PIXELTYPE_RAW_NO_CTABLE);
- }
- buffer.writePad(fPixels, this->getSafeSize());
- // There is no writeZeroPad() fcn, so write individual bytes.
- if (this->getSize() > this->getSafeSize()) {
- size_t deltaSize = this->getSize() - this->getSafeSize();
- // Need aligned pointer to write into due to internal implementa-
- // tion of SkWriter32.
- memset(buffer.reserve(SkAlign4(deltaSize)), 0, deltaSize);
- }
} else {
buffer.write8(SERIALIZE_PIXELTYPE_NONE);
}
SkSafeUnref(this->setPixelRef(pr, offset));
break;
}
- case SERIALIZE_PIXELTYPE_RAW_WITH_CTABLE:
- case SERIALIZE_PIXELTYPE_RAW_NO_CTABLE: {
- SkColorTable* ctable = NULL;
- if (SERIALIZE_PIXELTYPE_RAW_WITH_CTABLE == reftype) {
- ctable = static_cast<SkColorTable*>(buffer.readFlattenable());
- }
- size_t size = this->getSize();
- if (this->allocPixels(ctable)) {
- this->lockPixels();
- // Just read what we need.
- buffer.read(this->getPixels(), this->getSafeSize());
- // Keep aligned for subsequent reads.
- buffer.skip(size - this->getSafeSize());
- this->unlockPixels();
- } else {
- buffer.skip(size); // Still skip the full-sized buffer though.
- }
- SkSafeUnref(ctable);
- break;
- }
case SERIALIZE_PIXELTYPE_NONE:
break;
default: