Reverting r12665 & r12666 (Remove duplicate impl for SkImageInfo flattening) due to Chromium/Blink compilation errors
https://codereview.chromium.org/112603003/
git-svn-id: http://skia.googlecode.com/svn/trunk@12667 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkBitmapDevice.h b/include/core/SkBitmapDevice.h
index f3d40d0..83f480c 100644
--- a/include/core/SkBitmapDevice.h
+++ b/include/core/SkBitmapDevice.h
@@ -258,8 +258,6 @@
friend class SkSurface_Raster;
- void init(SkBitmap::Config 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
// any clip information.
diff --git a/include/core/SkMallocPixelRef.h b/include/core/SkMallocPixelRef.h
index 5ef70d6..100a15d 100644
--- a/include/core/SkMallocPixelRef.h
+++ b/include/core/SkMallocPixelRef.h
@@ -17,52 +17,33 @@
*/
class SkMallocPixelRef : public SkPixelRef {
public:
- /**
- * Return a new SkMallocPixelRef with the provided pixel storage, rowBytes,
- * and optional colortable. The caller is responsible for managing the
- * lifetime of the pixel storage buffer, as the pixelref will not try
- * to delete the storage.
- *
- * This pixelref will ref() the specified colortable (if not NULL).
- *
- * Returns NULL on failure.
+ /** Allocate the specified buffer for pixels. The memory is freed when the
+ last owner of this pixelref is gone. If addr is NULL, sk_malloc_throw()
+ is called to allocate it.
*/
- static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr,
- size_t rowBytes, SkColorTable*);
-
- /**
- * 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.
- *
- * This pixelref will ref() the specified colortable (if not NULL).
- *
- * Returns NULL on failure.
- */
- static SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
- size_t rowBytes, SkColorTable*);
+ SkMallocPixelRef(void* addr, size_t size, SkColorTable* ctable, bool ownPixels = true);
+ virtual ~SkMallocPixelRef();
void* getAddr() const { return fStorage; }
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
protected:
- virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
- virtual void onUnlockPixels() SK_OVERRIDE;
- virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
- virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
+ // overrides from SkPixelRef
+ virtual void* onLockPixels(SkColorTable**);
+ virtual void onUnlockPixels();
SkMallocPixelRef(SkFlattenableReadBuffer& buffer);
- SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
- bool ownsPixels);
- virtual ~SkMallocPixelRef();
+ virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
+
+ // Returns the allocation size for the pixels
+ virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE { return fSize; }
private:
- void* fStorage;
- SkColorTable* fCTable;
- size_t fRB;
- const bool fOwnPixels;
+ void* fStorage;
+ size_t fSize;
+ SkColorTable* fCTable;
+ bool fOwnPixels;
typedef SkPixelRef INHERITED;
};
diff --git a/include/core/SkPicture.h b/include/core/SkPicture.h
index cd6b3bc..bce343e 100644
--- a/include/core/SkPicture.h
+++ b/include/core/SkPicture.h
@@ -220,11 +220,10 @@
// V14: Add flags word to PathRef serialization
// V15: Remove A1 bitmpa config (and renumber remaining configs)
// V16: Move SkPath's isOval flag to SkPathRef
- // V17: Changes to PixelRef to store SkImageInfo
#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO
static const uint32_t PRIOR_PICTURE_VERSION = 15; // TODO: remove when .skps regenerated
#endif
- static const uint32_t PICTURE_VERSION = 17;
+ static const uint32_t PICTURE_VERSION = 16;
// fPlayback, fRecord, fWidth & fHeight are protected to allow derived classes to
// install their own SkPicturePlayback-derived players,SkPictureRecord-derived
diff --git a/include/core/SkPixelRef.h b/include/core/SkPixelRef.h
index b096c3c..d4c3532 100644
--- a/include/core/SkPixelRef.h
+++ b/include/core/SkPixelRef.h
@@ -14,12 +14,9 @@
#include "SkRefCnt.h"
#include "SkString.h"
#include "SkFlattenable.h"
-#include "SkImageInfo.h"
#include "SkTDArray.h"
-//#define SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR
-
-#define SK_SUPPORT_LEGACY_ONLOCKPIXELS
+#define SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR
#ifdef SK_DEBUG
/**
@@ -70,48 +67,23 @@
/** 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".
- * Calling lockPixels returns a LockRec struct (on success).
- */
- struct LockRec {
- void* fPixels;
- SkColorTable* fColorTable;
- size_t fRowBytes;
-
- void zero() { sk_bzero(this, sizeof(*this)); }
-
- bool isZero() const {
- return NULL == fPixels && NULL == fColorTable && 0 == fRowBytes;
- }
- };
-
- /**
* Returns true if the lockcount > 0
*/
bool isLocked() const { return fLockCount > 0; }
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
@@ -268,27 +240,18 @@
void addGenIDChangeListener(GenIDChangeListener* listener);
protected:
-#ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS
- virtual void* onLockPixels(SkColorTable**);
- virtual bool onNewLockPixels(LockRec*);
-#else
+ /** 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;
+
/**
- * On success, returns true and fills out the LockRec for the pixels. On
- * failure returns false and ignores the LockRec parameter.
+ * 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.
- */
- virtual bool onNewLockPixels(LockRec*) = 0;
-#endif
-
- /**
- * 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.
- *
- * 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;
@@ -333,16 +296,16 @@
// 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
// FIXME: fInfo should be const once we remove old constructor that does
// not set it.
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;
diff --git a/include/gpu/GrSurface.h b/include/gpu/GrSurface.h
index 15e44ab..c401a90 100644
--- a/include/gpu/GrSurface.h
+++ b/include/gpu/GrSurface.h
@@ -15,7 +15,6 @@
class GrTexture;
class GrRenderTarget;
-struct SkImageInfo;
class GrSurface : public GrResource {
public:
@@ -59,8 +58,6 @@
*/
const GrTextureDesc& desc() const { return fDesc; }
- void asImageInfo(SkImageInfo*) const;
-
/**
* @return the texture associated with the surface, may be NULL.
*/
diff --git a/include/gpu/SkGr.h b/include/gpu/SkGr.h
index db08548..5e5ca4b 100644
--- a/include/gpu/SkGr.h
+++ b/include/gpu/SkGr.h
@@ -50,7 +50,6 @@
* kUnknown_PixelConfig if the conversion cannot be done.
*/
GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config);
-bool GrPixelConfig2ColorType(GrPixelConfig, SkColorType*);
static inline GrColor SkColor2GrColor(SkColor c) {
SkPMColor pm = SkPreMultiplyColor(c);
diff --git a/include/gpu/SkGrPixelRef.h b/include/gpu/SkGrPixelRef.h
index 4d33b9d..c29c27f 100644
--- a/include/gpu/SkGrPixelRef.h
+++ b/include/gpu/SkGrPixelRef.h
@@ -23,13 +23,14 @@
*/
class SK_API SkROLockPixelsPixelRef : public SkPixelRef {
public:
- SkROLockPixelsPixelRef(const SkImageInfo&);
+ SkROLockPixelsPixelRef();
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;
@@ -46,6 +47,7 @@
* cache and would like the pixel ref to unlock it in its destructor then transferCacheLock
* should be set to true.
*/
+ SkGrPixelRef(GrSurface*, bool transferCacheLock = false);
SkGrPixelRef(const SkImageInfo&, GrSurface*, bool transferCacheLock = false);
virtual ~SkGrPixelRef();
diff --git a/include/images/SkImageRef.h b/include/images/SkImageRef.h
index 36f95e6..0599a8d 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(const SkImageInfo&, SkStreamRewindable*, int sampleSize = 1,
+ SkImageRef(SkStreamRewindable*, SkBitmap::Config config, int sampleSize = 1,
SkBaseMutex* mutex = NULL);
virtual ~SkImageRef();
@@ -72,9 +72,9 @@
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;
@@ -89,6 +89,7 @@
SkImageDecoderFactory* fFactory; // may be null
SkStreamRewindable* fStream;
+ SkBitmap::Config fConfig;
int fSampleSize;
bool fDoDither;
bool fErrorInDecoding;
diff --git a/include/images/SkImageRef_GlobalPool.h b/include/images/SkImageRef_GlobalPool.h
index caaf248..3adc0f6 100644
--- a/include/images/SkImageRef_GlobalPool.h
+++ b/include/images/SkImageRef_GlobalPool.h
@@ -15,7 +15,7 @@
class SkImageRef_GlobalPool : public SkImageRef {
public:
// if pool is null, use the global pool
- SkImageRef_GlobalPool(const SkImageInfo&, SkStreamRewindable*,
+ SkImageRef_GlobalPool(SkStreamRewindable*, SkBitmap::Config,
int sampleSize = 1);
virtual ~SkImageRef_GlobalPool();
diff --git a/samplecode/SamplePicture.cpp b/samplecode/SamplePicture.cpp
index 2f7d691..66289af 100644
--- a/samplecode/SamplePicture.cpp
+++ b/samplecode/SamplePicture.cpp
@@ -40,9 +40,7 @@
if (SkImageDecoder::DecodeStream(stream, &bm, SkBitmap::kNo_Config,
SkImageDecoder::kDecodeBounds_Mode)) {
- SkImageInfo info;
- bm.asImageInfo(&info);
- SkPixelRef* pr = new SkImageRef_GlobalPool(info, stream, 1);
+ SkPixelRef* pr = new SkImageRef_GlobalPool(stream, bm.config(), 1);
bm.setPixelRef(pr)->unref();
}
}
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index 7a7f690..25a6b1d 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -453,20 +453,10 @@
return;
}
- SkImageInfo info;
- if (!this->asImageInfo(&info)) {
- this->setPixelRef(NULL, 0);
- return;
- }
+ Sk64 size = this->getSize64();
+ SkASSERT(!size.isNeg() && size.is32());
- SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable);
- if (NULL == pr) {
- this->setPixelRef(NULL, 0);
- return;
- }
-
- this->setPixelRef(pr)->unref();
-
+ this->setPixelRef(new SkMallocPixelRef(p, size.get32(), ctable, false))->unref();
// since we're already allocated, we lockPixels right away
this->lockPixels();
SkDEBUGCODE(this->validate();)
@@ -531,19 +521,17 @@
*/
bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
SkColorTable* ctable) {
- SkImageInfo info;
- if (!dst->asImageInfo(&info)) {
-// SkDebugf("unsupported config for info %d\n", dst->config());
- return false;
- }
-
- SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(),
- ctable);
- if (NULL == pr) {
+ Sk64 size = dst->getSize64();
+ if (size.isNeg() || !size.is32()) {
return false;
}
- dst->setPixelRef(pr, 0)->unref();
+ void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure
+ if (NULL == addr) {
+ return false;
+ }
+
+ dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref();
// since we're already allocated, we lockPixels right away
dst->lockPixels();
return true;
diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp
index 368c807..1668618 100644
--- a/src/core/SkBitmapDevice.cpp
+++ b/src/core/SkBitmapDevice.cpp
@@ -24,30 +24,31 @@
, fBitmap(bitmap) {
}
-void SkBitmapDevice::init(SkBitmap::Config config, int width, int height, bool isOpaque) {
+SkBitmapDevice::SkBitmapDevice(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
- fBitmap.setConfig(config, 0, 0, 0, isOpaque ?
- kOpaque_SkAlphaType : kPremul_SkAlphaType);
- } else if (!isOpaque) {
- fBitmap.eraseColor(SK_ColorTRANSPARENT);
- }
+ if (!fBitmap.allocPixels()) {
+ fBitmap.setConfig(config, 0, 0, 0, isOpaque ?
+ kOpaque_SkAlphaType : kPremul_SkAlphaType);
}
-}
-
-SkBitmapDevice::SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque) {
- this->init(config, width, height, isOpaque);
+ if (!isOpaque) {
+ fBitmap.eraseColor(SK_ColorTRANSPARENT);
+ }
}
SkBitmapDevice::SkBitmapDevice(SkBitmap::Config config, int width, int height, bool isOpaque,
const SkDeviceProperties& deviceProperties)
- : SkBaseDevice(deviceProperties)
-{
- this->init(config, width, height, isOpaque);
+ : 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);
+ }
}
SkBitmapDevice::~SkBitmapDevice() {
diff --git a/src/core/SkImageFilterUtils.cpp b/src/core/SkImageFilterUtils.cpp
index e535d93..8385fb4 100644
--- a/src/core/SkImageFilterUtils.cpp
+++ b/src/core/SkImageFilterUtils.cpp
@@ -15,14 +15,8 @@
#include "SkGr.h"
bool SkImageFilterUtils::WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result) {
- SkImageInfo info;
- info.fWidth = width;
- info.fHeight = height;
- info.fColorType = kPMColor_SkColorType;
- info.fAlphaType = kPremul_SkAlphaType;
-
- result->setConfig(info);
- result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
+ result->setConfig(SkBitmap::kARGB_8888_Config, width, height);
+ result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
return true;
}
@@ -42,12 +36,8 @@
} else {
if (filter->filterImage(proxy, src, ctm, result, offset)) {
if (!result->getTexture()) {
- SkImageInfo info;
- if (!result->asImageInfo(&info)) {
- return false;
- }
GrTexture* resultTex = GrLockAndRefCachedBitmapTexture(context, *result, NULL);
- result->setPixelRef(new SkGrPixelRef(info, resultTex))->unref();
+ result->setPixelRef(new SkGrPixelRef(resultTex))->unref();
GrUnlockAndUnrefCachedBitmapTexture(resultTex);
}
return true;
diff --git a/src/core/SkMallocPixelRef.cpp b/src/core/SkMallocPixelRef.cpp
index 613491a..f229e9d 100644
--- a/src/core/SkMallocPixelRef.cpp
+++ b/src/core/SkMallocPixelRef.cpp
@@ -1,105 +1,27 @@
+
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
-
#include "SkMallocPixelRef.h"
#include "SkBitmap.h"
#include "SkFlattenableBuffers.h"
-static bool check_info(const SkImageInfo& info, SkColorTable* ctable) {
- if (info.fWidth < 0 ||
- info.fHeight < 0 ||
- (unsigned)info.fColorType > (unsigned)kLastEnum_SkColorType ||
- (unsigned)info.fAlphaType > (unsigned)kLastEnum_SkAlphaType)
- {
- return false;
+SkMallocPixelRef::SkMallocPixelRef(void* storage, size_t size,
+ SkColorTable* ctable, bool ownPixels) {
+ if (NULL == storage) {
+ SkASSERT(ownPixels);
+ storage = sk_malloc_throw(size);
}
-
- // these seem like good checks, but currently we have (at least) tests
- // that expect the pixelref to succeed even when there is a mismatch
- // with colortables. fix?
-#if 0
- if (kIndex8_SkColorType == info.fColorType && NULL == ctable) {
- return false;
- }
- if (kIndex8_SkColorType != info.fColorType && NULL != ctable) {
- return false;
- }
-#endif
- return true;
-}
-
-SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info,
- void* addr,
- size_t rowBytes,
- SkColorTable* ctable) {
- if (!check_info(info, ctable)) {
- return NULL;
- }
- return SkNEW_ARGS(SkMallocPixelRef, (info, addr, rowBytes, ctable, false));
-}
-
-SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info,
- size_t requestedRowBytes,
- SkColorTable* ctable) {
- if (!check_info(info, ctable)) {
- return NULL;
- }
-
- int32_t minRB = info.minRowBytes();
- if (minRB < 0) {
- return NULL; // allocation will be too large
- }
- if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) {
- return NULL; // cannot meet requested rowbytes
- }
-
- int32_t rowBytes;
- if (requestedRowBytes) {
- rowBytes = requestedRowBytes;
- } else {
- rowBytes = minRB;
- }
-
- Sk64 bigSize;
- bigSize.setMul(info.fHeight, rowBytes);
- if (!bigSize.is32()) {
- return NULL;
- }
-
- size_t size = bigSize.get32();
- void* addr = sk_malloc_flags(size, 0);
- if (NULL == addr) {
- return NULL;
- }
-
- return SkNEW_ARGS(SkMallocPixelRef, (info, addr, rowBytes, ctable, true));
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
- size_t rowBytes, SkColorTable* ctable,
- bool ownsPixels)
- : SkPixelRef(info)
- , fOwnPixels(ownsPixels)
-{
- SkASSERT(check_info(info, ctable));
- SkASSERT(rowBytes >= info.minRowBytes());
-
- if (kIndex_8_SkColorType != info.fColorType) {
- ctable = NULL;
- }
-
fStorage = storage;
+ fSize = size;
fCTable = ctable;
- fRB = rowBytes;
SkSafeRef(ctable);
-
- this->setPreLocked(fStorage, fRB, fCTable);
+ fOwnPixels = ownPixels;
+
+ this->setPreLocked(fStorage, fCTable);
}
SkMallocPixelRef::~SkMallocPixelRef() {
@@ -109,30 +31,19 @@
}
}
-bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) {
- rec->fPixels = fStorage;
- rec->fRowBytes = fRB;
- rec->fColorTable = fCTable;
- return true;
+void* SkMallocPixelRef::onLockPixels(SkColorTable** ct) {
+ *ct = fCTable;
+ return fStorage;
}
void SkMallocPixelRef::onUnlockPixels() {
// nothing to do
}
-size_t SkMallocPixelRef::getAllocatedSizeInBytes() const {
- return this->info().getSafeSize(fRB);
-}
-
void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
- buffer.write32(fRB);
-
- // TODO: replace this bulk write with a chunky one that can trim off any
- // trailing bytes on each scanline (in case rowbytes > width*size)
- size_t size = this->info().getSafeSize(fRB);
- buffer.writeByteArray(fStorage, size);
+ buffer.writeByteArray(fStorage, fSize);
buffer.writeBool(fCTable != NULL);
if (fCTable) {
fCTable->writeToBuffer(buffer);
@@ -140,18 +51,16 @@
}
SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer)
- : INHERITED(buffer, NULL)
- , fOwnPixels(true)
-{
- fRB = buffer.read32();
- size_t size = this->info().getSafeSize(fRB);
- fStorage = sk_malloc_throw(size);
- buffer.readByteArray(fStorage, size);
+ : INHERITED(buffer, NULL) {
+ fSize = buffer.getArrayCount();
+ fStorage = sk_malloc_throw(fSize);
+ buffer.readByteArray(fStorage, fSize);
if (buffer.readBool()) {
fCTable = SkNEW_ARGS(SkColorTable, (buffer));
} else {
fCTable = NULL;
}
+ fOwnPixels = true;
- this->setPreLocked(fStorage, fRB, fCTable);
+ this->setPreLocked(fStorage, fCTable);
}
diff --git a/src/core/SkMaskFilter.cpp b/src/core/SkMaskFilter.cpp
index adfed41..f062f13 100644
--- a/src/core/SkMaskFilter.cpp
+++ b/src/core/SkMaskFilter.cpp
@@ -349,14 +349,10 @@
if (!result) {
return false;
}
- SkAutoUnref aur(dst);
- SkImageInfo info;
resultBM->setConfig(srcBM.config(), dst->width(), dst->height());
- if (resultBM->asImageInfo(&info)) {
- return false;
- }
- resultBM->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, dst)))->unref();
+ resultBM->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (dst)))->unref();
+ dst->unref();
return true;
}
diff --git a/src/core/SkPixelRef.cpp b/src/core/SkPixelRef.cpp
index 5d394d5..da88ca5 100644
--- a/src/core/SkPixelRef.cpp
+++ b/src/core/SkPixelRef.cpp
@@ -82,32 +82,48 @@
// just need a > 0 value, so pick a funny one to aid in debugging
#define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789
-SkPixelRef::SkPixelRef(const SkImageInfo& info) {
- this->setMutex(NULL);
+SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) {
+ this->setMutex(mutex);
fInfo = info;
- fRec.zero();
+ 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) {
- this->setMutex(mutex);
+SkPixelRef::SkPixelRef(const SkImageInfo& info) {
+ this->setMutex(NULL);
fInfo = info;
- fRec.zero();
+ fPixels = NULL;
+ fColorTable = NULL; // we do not track ownership of this
fLockCount = 0;
this->needsNewGenID();
fIsImmutable = false;
fPreLocked = false;
}
+#ifdef SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR
+// THIS GUY IS DEPRECATED -- don't use me!
+SkPixelRef::SkPixelRef(SkBaseMutex* mutex) {
+ this->setMutex(mutex);
+ // Fill with dummy values.
+ sk_bzero(&fInfo, sizeof(fInfo));
+ fPixels = NULL;
+ fColorTable = NULL; // we do not track ownership of this
+ fLockCount = 0;
+ this->needsNewGenID();
+ fIsImmutable = false;
+ fPreLocked = false;
+}
+#endif
+
SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
: INHERITED(buffer) {
this->setMutex(mutex);
-
- fInfo.unflatten(buffer);
- fRec.zero();
+ fPixels = NULL;
+ fColorTable = NULL; // we do not track ownership of this
fLockCount = 0;
fIsImmutable = buffer.readBool();
fGenerationID = buffer.readUInt();
@@ -131,13 +147,12 @@
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
@@ -145,8 +160,6 @@
void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
-
- fInfo.flatten(buffer);
buffer.writeBool(fIsImmutable);
// We write the gen ID into the picture for within-process recording. This
// is safe since the same genID will never refer to two different sets of
@@ -161,30 +174,20 @@
}
}
-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;
+ if (1 == ++fLockCount) {
+ 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() {
@@ -196,11 +199,12 @@
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);
}
}
}
@@ -282,29 +286,6 @@
///////////////////////////////////////////////////////////////////////////////
-#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/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp
index 5d200d1..2776199 100644
--- a/src/effects/gradients/SkGradientShader.cpp
+++ b/src/effects/gradients/SkGradientShader.cpp
@@ -513,14 +513,13 @@
const SkPMColor* SkGradientShaderBase::getCache32() const {
if (fCache32 == NULL) {
- SkImageInfo info;
- info.fWidth = kCache32Count;
- info.fHeight = 4; // for our 4 dither rows
- info.fAlphaType = kPremul_SkAlphaType;
- info.fColorType = kPMColor_SkColorType;
+ // double the count for dither entries
+ const int entryCount = kCache32Count * 4;
+ const size_t allocSize = sizeof(SkPMColor) * entryCount;
if (NULL == fCache32PixelRef) {
- fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL);
+ fCache32PixelRef = SkNEW_ARGS(SkMallocPixelRef,
+ (NULL, allocSize, NULL));
}
fCache32 = (SkPMColor*)fCache32PixelRef->getAddr();
if (fColorCount == 2) {
@@ -542,7 +541,8 @@
}
if (fMapper) {
- SkMallocPixelRef* newPR = SkMallocPixelRef::NewAllocate(info, 0, NULL);
+ SkMallocPixelRef* newPR = SkNEW_ARGS(SkMallocPixelRef,
+ (NULL, allocSize, NULL));
SkPMColor* linear = fCache32; // just computed linear data
SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapped data
SkUnitMapper* map = fMapper;
diff --git a/src/gpu/GrSurface.cpp b/src/gpu/GrSurface.cpp
index 1fcc4ff..fed95f2 100644
--- a/src/gpu/GrSurface.cpp
+++ b/src/gpu/GrSurface.cpp
@@ -8,19 +8,9 @@
#include "GrSurface.h"
#include "SkBitmap.h"
-#include "SkGr.h"
#include "SkImageEncoder.h"
#include <stdio.h>
-void GrSurface::asImageInfo(SkImageInfo* info) const {
- if (!GrPixelConfig2ColorType(this->config(), &info->fColorType)) {
- sk_throw();
- }
- info->fWidth = this->width();
- info->fHeight = this->height();
- info->fAlphaType = kPremul_SkAlphaType;
-}
-
bool GrSurface::savePixels(const char* filename) {
SkBitmap bm;
bm.setConfig(SkBitmap::kARGB_8888_Config, this->width(), this->height());
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index a413d04..ce02f2c 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -214,10 +214,7 @@
if (NULL == surface) {
surface = fRenderTarget;
}
-
- SkImageInfo info;
- surface->asImageInfo(&info);
- SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, cached));
+ SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
this->setPixelRef(pr, 0)->unref();
}
@@ -227,8 +224,8 @@
int width,
int height,
int sampleCount)
- : SkBitmapDevice(make_bitmap(config, width, height, false /*isOpaque*/))
-{
+ : SkBitmapDevice(make_bitmap(config, width, height, false /*isOpaque*/)) {
+
fDrawProcs = NULL;
fContext = context;
@@ -248,14 +245,6 @@
desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
desc.fSampleCnt = sampleCount;
- SkImageInfo info;
- if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
- sk_throw();
- }
- info.fWidth = width;
- info.fHeight = height;
- info.fAlphaType = kPremul_SkAlphaType;
-
SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
if (NULL != texture) {
@@ -265,7 +254,7 @@
SkASSERT(NULL != fRenderTarget);
// wrap the bitmap with a pixelref to expose our texture
- SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, texture));
+ SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
this->setPixelRef(pr, 0)->unref();
} else {
GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
@@ -851,12 +840,11 @@
}
SkBitmap wrap_texture(GrTexture* texture) {
- SkImageInfo info;
- texture->asImageInfo(&info);
-
SkBitmap result;
- result.setConfig(info);
- result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
+ bool dummy;
+ SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
+ result.setConfig(config, texture->width(), texture->height());
+ result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
return result;
}
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index a3f0eef..c7ae0c8 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -258,33 +258,3 @@
return kUnknown_GrPixelConfig;
}
}
-
-bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) {
- SkColorType ct;
- switch (config) {
- case kAlpha_8_GrPixelConfig:
- ct = kAlpha_8_SkColorType;
- break;
- case kIndex_8_GrPixelConfig:
- ct = kIndex_8_SkColorType;
- break;
- case kRGB_565_GrPixelConfig:
- ct = kRGB_565_SkColorType;
- break;
- case kRGBA_4444_GrPixelConfig:
- ct = kARGB_4444_SkColorType;
- break;
- case kRGBA_8888_GrPixelConfig:
- ct = kRGBA_8888_SkColorType;
- break;
- case kBGRA_8888_GrPixelConfig:
- ct = kBGRA_8888_SkColorType;
- break;
- default:
- return false;
- }
- if (ctOut) {
- *ctOut = ct;
- }
- return true;
-}
diff --git a/src/gpu/SkGrPixelRef.cpp b/src/gpu/SkGrPixelRef.cpp
index 74b1a4f..0129426 100644
--- a/src/gpu/SkGrPixelRef.cpp
+++ b/src/gpu/SkGrPixelRef.cpp
@@ -18,29 +18,24 @@
// to avoid deadlock with the default one provided by SkPixelRef.
SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex);
-SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info)
- : INHERITED(info, &gROLockPixelsPixelRefMutex) {
+SkROLockPixelsPixelRef::SkROLockPixelsPixelRef() : INHERITED(&gROLockPixelsPixelRefMutex) {
}
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() {
@@ -81,14 +76,6 @@
desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
desc.fConfig = SkBitmapConfig2GrPixelConfig(dstConfig);
- SkImageInfo info;
- if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
- return NULL;
- }
- info.fWidth = desc.fWidth;
- info.fHeight = desc.fHeight;
- info.fAlphaType = kPremul_SkAlphaType;
-
GrTexture* dst = context->createUncachedTexture(desc, NULL, 0);
if (NULL == dst) {
return NULL;
@@ -106,17 +93,31 @@
dst->releaseRenderTarget();
#endif
- SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst));
+ SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (dst));
SkSafeUnref(dst);
return pixelRef;
}
///////////////////////////////////////////////////////////////////////////////
-SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface,
- bool transferCacheLock)
- : INHERITED(info)
-{
+SkGrPixelRef::SkGrPixelRef(GrSurface* surface, bool transferCacheLock) {
+ // TODO: figure out if this is responsible for Chrome canvas errors
+#if 0
+ // The GrTexture has a ref to the GrRenderTarget but not vice versa.
+ // If the GrTexture exists take a ref to that (rather than the render
+ // target)
+ fSurface = surface->asTexture();
+#else
+ fSurface = NULL;
+#endif
+ if (NULL == fSurface) {
+ fSurface = surface;
+ }
+ fUnlock = transferCacheLock;
+ SkSafeRef(surface);
+}
+
+SkGrPixelRef::SkGrPixelRef(const SkImageInfo&, GrSurface* surface, bool transferCacheLock) {
// TODO: figure out if this is responsible for Chrome canvas errors
#if 0
// The GrTexture has a ref to the GrRenderTarget but not vice versa.
diff --git a/src/image/SkDataPixelRef.cpp b/src/image/SkDataPixelRef.cpp
index 875f933..7897bf9 100644
--- a/src/image/SkDataPixelRef.cpp
+++ b/src/image/SkDataPixelRef.cpp
@@ -9,25 +9,18 @@
#include "SkData.h"
#include "SkFlattenableBuffers.h"
-SkDataPixelRef::SkDataPixelRef(const SkImageInfo& info,
- SkData* data, size_t rowBytes)
- : INHERITED(info)
- , fData(data)
- , fRB(rowBytes)
-{
+SkDataPixelRef::SkDataPixelRef(SkData* data) : fData(data) {
fData->ref();
- this->setPreLocked(const_cast<void*>(fData->data()), rowBytes, NULL);
+ this->setPreLocked(const_cast<void*>(fData->data()), NULL);
}
SkDataPixelRef::~SkDataPixelRef() {
fData->unref();
}
-bool SkDataPixelRef::onNewLockPixels(LockRec* rec) {
- rec->fPixels = const_cast<void*>(fData->data());
- rec->fColorTable = NULL;
- rec->fRowBytes = fRB;
- return true;
+void* SkDataPixelRef::onLockPixels(SkColorTable** ct) {
+ *ct = NULL;
+ return const_cast<void*>(fData->data());
}
void SkDataPixelRef::onUnlockPixels() {
@@ -40,15 +33,11 @@
void SkDataPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
-
buffer.writeDataAsByteArray(fData);
- buffer.write32(fRB);
}
SkDataPixelRef::SkDataPixelRef(SkFlattenableReadBuffer& buffer)
- : INHERITED(buffer, NULL)
-{
+ : INHERITED(buffer, NULL) {
fData = buffer.readByteArrayAsData();
- fRB = buffer.read32();
- this->setPreLocked(const_cast<void*>(fData->data()), fRB, NULL);
+ this->setPreLocked(const_cast<void*>(fData->data()), NULL);
}
diff --git a/src/image/SkDataPixelRef.h b/src/image/SkDataPixelRef.h
index c2e52cd..50c8857 100644
--- a/src/image/SkDataPixelRef.h
+++ b/src/image/SkDataPixelRef.h
@@ -14,13 +14,13 @@
class SkDataPixelRef : public SkPixelRef {
public:
- SkDataPixelRef(const SkImageInfo&, SkData* data, size_t rowBytes);
+ SkDataPixelRef(SkData* data);
virtual ~SkDataPixelRef();
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDataPixelRef)
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;
@@ -28,8 +28,7 @@
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
private:
- SkData* fData;
- size_t fRB;
+ SkData* fData;
typedef SkPixelRef INHERITED;
};
diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp
index 93de5ea..a872ae3 100644
--- a/src/image/SkImage_Raster.cpp
+++ b/src/image/SkImage_Raster.cpp
@@ -84,8 +84,10 @@
SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes)
: INHERITED(info.fWidth, info.fHeight) {
- fBitmap.setConfig(info, rowBytes);
- fBitmap.setPixelRef(SkNEW_ARGS(SkDataPixelRef, (info, data, rowBytes)))->unref();
+ SkBitmap::Config config = SkImageInfoToBitmapConfig(info);
+
+ fBitmap.setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlphaType);
+ fBitmap.setPixelRef(SkNEW_ARGS(SkDataPixelRef, (data)))->unref();
fBitmap.setImmutable();
}
diff --git a/src/image/SkSurface_Raster.cpp b/src/image/SkSurface_Raster.cpp
index 61ade6f..27db504 100644
--- a/src/image/SkSurface_Raster.cpp
+++ b/src/image/SkSurface_Raster.cpp
@@ -155,9 +155,19 @@
return NULL;
}
- SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL));
- if (NULL == pr.get()) {
+ static const size_t kMaxTotalSize = SK_MaxS32;
+ size_t rowBytes = SkImageMinRowBytes(info);
+ uint64_t size64 = (uint64_t)info.fHeight * rowBytes;
+ if (size64 > kMaxTotalSize) {
return NULL;
}
- return SkNEW_ARGS(SkSurface_Raster, (info, pr, info.minRowBytes()));
+
+ size_t size = (size_t)size64;
+ void* pixels = sk_malloc_throw(size);
+ if (NULL == pixels) {
+ return NULL;
+ }
+
+ SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL, true)));
+ return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes));
}
diff --git a/src/images/SkImageRef.cpp b/src/images/SkImageRef.cpp
index 99782c4..1a8284b 100644
--- a/src/images/SkImageRef.cpp
+++ b/src/images/SkImageRef.cpp
@@ -15,14 +15,16 @@
//#define DUMP_IMAGEREF_LIFECYCLE
+
///////////////////////////////////////////////////////////////////////////////
-SkImageRef::SkImageRef(const SkImageInfo& info, SkStreamRewindable* stream,
+SkImageRef::SkImageRef(SkStreamRewindable* stream, SkBitmap::Config config,
int sampleSize, SkBaseMutex* mutex)
- : INHERITED(info, mutex), fErrorInDecoding(false) {
+ : SkPixelRef(mutex), fErrorInDecoding(false) {
SkASSERT(stream);
stream->ref();
fStream = stream;
+ fConfig = config;
fSampleSize = sampleSize;
fDoDither = true;
fPrev = fNext = NULL;
@@ -38,7 +40,7 @@
#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();
@@ -90,6 +92,14 @@
return false;
}
+ /* As soon as we really know our config, we record it, so that on
+ subsequent calls to the codec, we are sure we will always get the same
+ result.
+ */
+ if (SkBitmap::kNo_Config != fBitmap.config()) {
+ fConfig = fBitmap.config();
+ }
+
if (NULL != fBitmap.getPixels() ||
(SkBitmap::kNo_Config != fBitmap.config() &&
SkImageDecoder::kDecodeBounds_Mode == mode)) {
@@ -115,7 +125,7 @@
codec->setSampleSize(fSampleSize);
codec->setDitherImage(fDoDither);
- if (this->onDecode(codec, fStream, &fBitmap, fBitmap.config(), mode)) {
+ if (this->onDecode(codec, fStream, &fBitmap, fConfig, mode)) {
return true;
}
}
@@ -133,18 +143,15 @@
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 {
@@ -163,6 +170,7 @@
SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
: INHERITED(buffer, mutex), fErrorInDecoding(false) {
+ fConfig = (SkBitmap::Config)buffer.readUInt();
fSampleSize = buffer.readInt();
fDoDither = buffer.readBool();
@@ -177,6 +185,7 @@
void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
+ buffer.writeUInt(fConfig);
buffer.writeInt(fSampleSize);
buffer.writeBool(fDoDither);
// FIXME: Consider moving this logic should go into writeStream itself.
diff --git a/src/images/SkImageRef_GlobalPool.cpp b/src/images/SkImageRef_GlobalPool.cpp
index f91ceba..352dd42 100644
--- a/src/images/SkImageRef_GlobalPool.cpp
+++ b/src/images/SkImageRef_GlobalPool.cpp
@@ -24,10 +24,10 @@
return gPool;
}
-SkImageRef_GlobalPool::SkImageRef_GlobalPool(const SkImageInfo& info,
- SkStreamRewindable* stream,
+SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStreamRewindable* stream,
+ SkBitmap::Config config,
int sampleSize)
- : SkImageRef(info, stream, sampleSize, &gGlobalPoolMutex) {
+ : SkImageRef(stream, config, sampleSize, &gGlobalPoolMutex) {
SkASSERT(&gGlobalPoolMutex == this->mutex());
SkAutoMutexAcquire ac(gGlobalPoolMutex);
GetGlobalPool()->addToHead(this);
diff --git a/src/images/SkImageRef_ashmem.cpp b/src/images/SkImageRef_ashmem.cpp
index eaa875d..9933ca9 100644
--- a/src/images/SkImageRef_ashmem.cpp
+++ b/src/images/SkImageRef_ashmem.cpp
@@ -31,10 +31,10 @@
return newsize;
}
-SkImageRef_ashmem::SkImageRef_ashmem(const SkImageInfo& info,
- SkStreamRewindable* stream,
- int sampleSize)
- : SkImageRef(info, stream, sampleSize) {
+SkImageRef_ashmem::SkImageRef_ashmem(SkStreamRewindable* stream,
+ SkBitmap::Config config,
+ int sampleSize)
+ : SkImageRef(stream, config, sampleSize) {
fRec.fFD = -1;
fRec.fAddr = NULL;
diff --git a/src/images/SkImageRef_ashmem.h b/src/images/SkImageRef_ashmem.h
index a2652fb..efee5e7 100644
--- a/src/images/SkImageRef_ashmem.h
+++ b/src/images/SkImageRef_ashmem.h
@@ -19,7 +19,7 @@
class SkImageRef_ashmem : public SkImageRef {
public:
- SkImageRef_ashmem(const SkImageInfo&, SkStreamRewindable*, int sampleSize = 1);
+ SkImageRef_ashmem(SkStreamRewindable*, SkBitmap::Config, int sampleSize = 1);
virtual ~SkImageRef_ashmem();
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkImageRef_ashmem)
diff --git a/src/lazy/SkCachingPixelRef.cpp b/src/lazy/SkCachingPixelRef.cpp
index 2419e37..b7eaf57 100644
--- a/src/lazy/SkCachingPixelRef.cpp
+++ b/src/lazy/SkCachingPixelRef.cpp
@@ -8,6 +8,7 @@
#include "SkCachingPixelRef.h"
#include "SkScaledImageCache.h"
+
bool SkCachingPixelRef::Install(SkImageGenerator* generator,
SkBitmap* dst) {
SkImageInfo info;
@@ -30,10 +31,10 @@
SkCachingPixelRef::SkCachingPixelRef(SkImageGenerator* generator,
const SkImageInfo& info,
size_t rowBytes)
- : INHERITED(info)
- , fImageGenerator(generator)
+ : fImageGenerator(generator)
, fErrorInDecoding(false)
, fScaledCacheId(NULL)
+ , fInfo(info)
, fRowBytes(rowBytes) {
SkASSERT(fImageGenerator != NULL);
}
@@ -43,32 +44,31 @@
// Assert always unlock before unref.
}
-bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) {
+void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) {
+ (void)colorTable;
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(),
- info.fWidth,
- info.fHeight,
+ fInfo.fWidth,
+ fInfo.fHeight,
&bitmap);
if (NULL == fScaledCacheId) {
// Cache has been purged, must re-decode.
- if ((!bitmap.setConfig(info, fRowBytes)) || !bitmap.allocPixels()) {
+ if ((!bitmap.setConfig(fInfo, fRowBytes)) || !bitmap.allocPixels()) {
fErrorInDecoding = true;
- return false;
+ return NULL;
}
SkAutoLockPixels autoLockPixels(bitmap);
- if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) {
+ if (!fImageGenerator->getPixels(fInfo, bitmap.getPixels(), fRowBytes)) {
fErrorInDecoding = true;
- return false;
+ return NULL;
}
fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
- info.fWidth,
- info.fHeight,
+ fInfo.fWidth,
+ fInfo.fHeight,
bitmap);
SkASSERT(fScaledCacheId != NULL);
}
@@ -78,7 +78,6 @@
SkAutoLockPixels autoLockPixels(bitmap);
void* pixels = bitmap.getPixels();
SkASSERT(pixels != NULL);
-
// At this point, the autoLockPixels will unlockPixels()
// to remove bitmap's lock on the pixels. We will then
// destroy bitmap. The *only* guarantee that this pointer
@@ -87,10 +86,7 @@
// 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() {
diff --git a/src/lazy/SkCachingPixelRef.h b/src/lazy/SkCachingPixelRef.h
index 75710d8..4a0387d 100644
--- a/src/lazy/SkCachingPixelRef.h
+++ b/src/lazy/SkCachingPixelRef.h
@@ -40,7 +40,7 @@
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; }
@@ -58,12 +58,12 @@
SkImageGenerator* const fImageGenerator;
bool fErrorInDecoding;
void* fScaledCacheId;
+ const SkImageInfo fInfo;
const size_t fRowBytes;
SkCachingPixelRef(SkImageGenerator* imageGenerator,
const SkImageInfo& info,
size_t rowBytes);
-
typedef SkPixelRef INHERITED;
};
diff --git a/src/lazy/SkDiscardablePixelRef.cpp b/src/lazy/SkDiscardablePixelRef.cpp
index 77ac825..f551436 100644
--- a/src/lazy/SkDiscardablePixelRef.cpp
+++ b/src/lazy/SkDiscardablePixelRef.cpp
@@ -11,15 +11,17 @@
SkDiscardablePixelRef::SkDiscardablePixelRef(SkImageGenerator* generator,
const SkImageInfo& info,
+ size_t size,
size_t rowBytes,
SkDiscardableMemory::Factory* fact)
- : INHERITED(info)
- , fGenerator(generator)
+ : fGenerator(generator)
, fDMFactory(fact)
+ , fInfo(info)
+ , fSize(size)
, fRowBytes(rowBytes)
- , fDiscardableMemory(NULL)
-{
+ , fDiscardableMemory(NULL) {
SkASSERT(fGenerator != NULL);
+ SkASSERT(fSize > 0);
SkASSERT(fRowBytes > 0);
// The SkImageGenerator contract requires fGenerator to always
// decode the same image on each call to getPixels().
@@ -36,42 +38,31 @@
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;
}
-
- const size_t size = this->info().getSafeSize(fRowBytes);
if (fDMFactory != NULL) {
- fDiscardableMemory = fDMFactory->create(size);
+ fDiscardableMemory = fDMFactory->create(fSize);
} else {
- fDiscardableMemory = SkDiscardableMemory::Create(size);
+ fDiscardableMemory = SkDiscardableMemory::Create(fSize);
}
if (NULL == fDiscardableMemory) {
- return false; // Memory allocation failed.
+ return NULL; // Memory allocation failed.
}
-
void* pixels = fDiscardableMemory->data();
- if (!fGenerator->getPixels(this->info(), pixels, fRowBytes)) {
+ if (!fGenerator->getPixels(fInfo, 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();
}
@@ -94,6 +85,7 @@
}
SkAutoTUnref<SkDiscardablePixelRef> ref(SkNEW_ARGS(SkDiscardablePixelRef,
(generator, info,
+ dst->getSize(),
dst->rowBytes(),
factory)));
dst->setPixelRef(ref);
diff --git a/src/lazy/SkDiscardablePixelRef.h b/src/lazy/SkDiscardablePixelRef.h
index 0a727b6..44c6df9 100644
--- a/src/lazy/SkDiscardablePixelRef.h
+++ b/src/lazy/SkDiscardablePixelRef.h
@@ -19,8 +19,7 @@
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; }
@@ -31,6 +30,8 @@
private:
SkImageGenerator* const fGenerator;
SkDiscardableMemory::Factory* const fDMFactory;
+ const SkImageInfo fInfo;
+ const size_t fSize; // size of memory to be allocated
const size_t fRowBytes;
// These const members should not change over the life of the
// PixelRef, since the SkBitmap doesn't expect them to change.
@@ -40,14 +41,12 @@
/* Takes ownership of SkImageGenerator. */
SkDiscardablePixelRef(SkImageGenerator* generator,
const SkImageInfo& info,
+ size_t size,
size_t rowBytes,
SkDiscardableMemory::Factory* factory);
-
friend bool SkInstallDiscardablePixelRef(SkImageGenerator*,
SkBitmap*,
SkDiscardableMemory::Factory*);
-
typedef SkPixelRef INHERITED;
};
-
#endif // SkDiscardablePixelRef_DEFINED
diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp
index 447ce4e..4971797 100644
--- a/tests/PictureTest.cpp
+++ b/tests/PictureTest.cpp
@@ -11,7 +11,6 @@
#include "SkCanvas.h"
#include "SkColorPriv.h"
#include "SkData.h"
-#include "SkDecodingImageGenerator.h"
#include "SkError.h"
#include "SkPaint.h"
#include "SkPicture.h"
@@ -338,6 +337,32 @@
}
#endif
+#include "SkData.h"
+#include "SkImageRef_GlobalPool.h"
+// Class to test SkPixelRef::onRefEncodedData, since there are currently no implementations in skia.
+class SkDataImageRef : public SkImageRef_GlobalPool {
+
+public:
+ SkDataImageRef(SkMemoryStream* stream)
+ : SkImageRef_GlobalPool(stream, SkBitmap::kNo_Config) {
+ SkASSERT(stream != NULL);
+ fData = stream->copyToData();
+ this->setImmutable();
+ }
+
+ ~SkDataImageRef() {
+ fData->unref();
+ }
+
+ virtual SkData* onRefEncodedData() SK_OVERRIDE {
+ fData->ref();
+ return fData;
+ }
+
+private:
+ SkData* fData;
+};
+
#include "SkImageEncoder.h"
static SkData* encode_bitmap_to_data(size_t* offset, const SkBitmap& bm) {
@@ -379,10 +404,14 @@
return;
}
SkAutoDataUnref data(wStream.copyToData());
+ SkMemoryStream memStream;
+ memStream.setData(data);
+ // Use the encoded bitmap as the data for an image ref.
SkBitmap bm;
- bool installSuccess = SkDecodingImageGenerator::Install(data, &bm);
- REPORTER_ASSERT(reporter, installSuccess);
+ SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream)));
+ imageRef->getInfo(&bm);
+ bm.setPixelRef(imageRef);
// Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
// Flattening original will follow the old path of performing an encode, while flattening bm
diff --git a/tests/PixelRefTest.cpp b/tests/PixelRefTest.cpp
index e0ffd7a..470221c 100644
--- a/tests/PixelRefTest.cpp
+++ b/tests/PixelRefTest.cpp
@@ -51,37 +51,35 @@
} // namespace
DEF_TEST(PixelRef_GenIDChange, r) {
- SkImageInfo info = { 10, 10, kPMColor_SkColorType, kPremul_SkAlphaType };
-
- SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL));
+ SkMallocPixelRef pixelRef(NULL, 0, NULL); // We don't really care about the pixels here.
// Register a listener.
int count = 0;
- pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
+ pixelRef.addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
REPORTER_ASSERT(r, 0 == count);
// No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense.
// (An SkPixelRef tree falls in the forest but there's nobody around to hear it. Do we care?)
- pixelRef->notifyPixelsChanged();
+ pixelRef.notifyPixelsChanged();
REPORTER_ASSERT(r, 0 == count);
// Force the generation ID to be calculated.
- REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
+ REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
// Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
- pixelRef->notifyPixelsChanged();
+ pixelRef.notifyPixelsChanged();
REPORTER_ASSERT(r, 0 == count);
// Force the generation ID to be recalculated, then add a listener.
- REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
- pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
- pixelRef->notifyPixelsChanged();
+ REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
+ pixelRef.addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
+ pixelRef.notifyPixelsChanged();
REPORTER_ASSERT(r, 1 == count);
// Quick check that NULL is safe.
- REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
- pixelRef->addGenIDChangeListener(NULL);
- pixelRef->notifyPixelsChanged();
+ REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
+ pixelRef.addGenIDChangeListener(NULL);
+ pixelRef.notifyPixelsChanged();
test_info(r);
}
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp
index 448c18f..bb05432 100644
--- a/tests/SerializationTest.cpp
+++ b/tests/SerializationTest.cpp
@@ -149,7 +149,6 @@
REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
unsigned char dataWritten[1024];
- SkASSERT(bytesWritten <= sizeof(dataWritten));
writer.writeToMemory(dataWritten);
// Make sure this fails when it should (test with smaller size, but still multiple of 4)
@@ -309,22 +308,10 @@
TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
// Create a bitmap with a pixel ref too small
- SkImageInfo info;
- info.fWidth = 256;
- info.fHeight = 256;
- info.fColorType = kPMColor_SkColorType;
- info.fAlphaType = kPremul_SkAlphaType;
-
SkBitmap invalidBitmap2;
- invalidBitmap2.setConfig(info);
-
- // Hack to force invalid, by making the pixelref smaller than its
- // owning bitmap.
- info.fWidth = 32;
- info.fHeight = 1;
-
- invalidBitmap2.setPixelRef(SkMallocPixelRef::NewAllocate(
- info, invalidBitmap2.rowBytes(), NULL))->unref();
+ invalidBitmap2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
+ invalidBitmap2.setPixelRef(SkNEW_ARGS(SkMallocPixelRef,
+ (NULL, 256, NULL)))->unref();
// The deserialization should detect the pixel ref being too small and fail
TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter);