diff options
33 files changed, 360 insertions, 221 deletions
diff --git a/include/core/SkMallocPixelRef.h b/include/core/SkMallocPixelRef.h index 100a15d90a..ba0b953cf6 100644 --- a/include/core/SkMallocPixelRef.h +++ b/include/core/SkMallocPixelRef.h @@ -17,33 +17,54 @@ */ class SkMallocPixelRef : public SkPixelRef { public: - /** 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. + /** + * 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 this pixelref will not try + * to delete it. + * + * The pixelref will ref() the colortable (if not NULL). + * + * Returns NULL on failure. */ - SkMallocPixelRef(void* addr, size_t size, SkColorTable* ctable, bool ownPixels = true); - virtual ~SkMallocPixelRef(); + static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr, + size_t rowBytes, SkColorTable*); + /** + * Return a new SkMallocPixelRef, automatically allocating storage for the + * 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). + * + * Returns NULL on failure. + */ + static SkMallocPixelRef* NewAllocate(const SkImageInfo& info, + size_t rowBytes, SkColorTable*); + void* getAddr() const { return fStorage; } SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef) protected: - // overrides from SkPixelRef - virtual void* onLockPixels(SkColorTable**); - virtual void onUnlockPixels(); - + SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, + bool ownPixels); SkMallocPixelRef(SkFlattenableReadBuffer& buffer); - virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; + virtual ~SkMallocPixelRef(); - // Returns the allocation size for the pixels - virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE { return fSize; } + 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; private: void* fStorage; - size_t fSize; SkColorTable* fCTable; - bool fOwnPixels; + size_t fRB; + const bool fOwnPixels; typedef SkPixelRef INHERITED; }; diff --git a/include/core/SkPixelRef.h b/include/core/SkPixelRef.h index d4c35323bb..4369e5d537 100644 --- a/include/core/SkPixelRef.h +++ b/include/core/SkPixelRef.h @@ -16,7 +16,7 @@ #include "SkFlattenable.h" #include "SkTDArray.h" -#define SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR +//#define SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR #ifdef SK_DEBUG /** diff --git a/include/gpu/GrSurface.h b/include/gpu/GrSurface.h index c401a905ff..15e44ab593 100644 --- a/include/gpu/GrSurface.h +++ b/include/gpu/GrSurface.h @@ -15,6 +15,7 @@ class GrTexture; class GrRenderTarget; +struct SkImageInfo; class GrSurface : public GrResource { public: @@ -58,6 +59,8 @@ public: */ 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 5e5ca4b72c..db08548f5d 100644 --- a/include/gpu/SkGr.h +++ b/include/gpu/SkGr.h @@ -50,6 +50,7 @@ GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff); * 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 c29c27fb3b..d8933724fb 100644 --- a/include/gpu/SkGrPixelRef.h +++ b/include/gpu/SkGrPixelRef.h @@ -1,4 +1,3 @@ - /* * Copyright 2010 Google Inc. * @@ -6,8 +5,6 @@ * found in the LICENSE file. */ - - #ifndef SkGrPixelRef_DEFINED #define SkGrPixelRef_DEFINED @@ -23,7 +20,7 @@ */ class SK_API SkROLockPixelsPixelRef : public SkPixelRef { public: - SkROLockPixelsPixelRef(); + SkROLockPixelsPixelRef(const SkImageInfo&); virtual ~SkROLockPixelsPixelRef(); protected: @@ -47,7 +44,6 @@ public: * 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 0599a8d963..30b1562c98 100644 --- a/include/images/SkImageRef.h +++ b/include/images/SkImageRef.h @@ -34,7 +34,7 @@ public: @param config The preferred config of the decoded bitmap. @param sampleSize Requested sampleSize for decoding. Defaults to 1. */ - SkImageRef(SkStreamRewindable*, SkBitmap::Config config, int sampleSize = 1, + SkImageRef(const SkImageInfo&, SkStreamRewindable*, int sampleSize = 1, SkBaseMutex* mutex = NULL); virtual ~SkImageRef(); @@ -89,7 +89,6 @@ private: 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 3adc0f6150..caaf2487de 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(SkStreamRewindable*, SkBitmap::Config, + SkImageRef_GlobalPool(const SkImageInfo&, SkStreamRewindable*, int sampleSize = 1); virtual ~SkImageRef_GlobalPool(); diff --git a/samplecode/SamplePicture.cpp b/samplecode/SamplePicture.cpp index 66289affe0..2f7d691d17 100644 --- a/samplecode/SamplePicture.cpp +++ b/samplecode/SamplePicture.cpp @@ -40,7 +40,9 @@ static SkBitmap load_bitmap() { if (SkImageDecoder::DecodeStream(stream, &bm, SkBitmap::kNo_Config, SkImageDecoder::kDecodeBounds_Mode)) { - SkPixelRef* pr = new SkImageRef_GlobalPool(stream, bm.config(), 1); + SkImageInfo info; + bm.asImageInfo(&info); + SkPixelRef* pr = new SkImageRef_GlobalPool(info, stream, 1); bm.setPixelRef(pr)->unref(); } } diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index f2b56290ea..b387795efe 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -453,10 +453,20 @@ void SkBitmap::setPixels(void* p, SkColorTable* ctable) { return; } - Sk64 size = this->getSize64(); - SkASSERT(!size.isNeg() && size.is32()); + SkImageInfo info; + if (!this->asImageInfo(&info)) { + this->setPixelRef(NULL, 0); + return; + } + + 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();) @@ -521,17 +531,19 @@ GrTexture* SkBitmap::getTexture() const { */ bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, SkColorTable* ctable) { - Sk64 size = dst->getSize64(); - if (size.isNeg() || !size.is32()) { + SkImageInfo info; + if (!dst->asImageInfo(&info)) { +// SkDebugf("unsupported config for info %d\n", dst->config()); return false; } - - void* addr = sk_malloc_flags(size.get32(), 0); // returns NULL on failure - if (NULL == addr) { + + SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), + ctable); + if (NULL == pr) { return false; } - dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref(); + dst->setPixelRef(pr, 0)->unref(); // since we're already allocated, we lockPixels right away dst->lockPixels(); return true; diff --git a/src/core/SkImageFilterUtils.cpp b/src/core/SkImageFilterUtils.cpp index 8385fb446a..a59cf7bbbd 100644 --- a/src/core/SkImageFilterUtils.cpp +++ b/src/core/SkImageFilterUtils.cpp @@ -15,8 +15,14 @@ #include "SkGr.h" bool SkImageFilterUtils::WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result) { - result->setConfig(SkBitmap::kARGB_8888_Config, width, height); - result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref(); + SkImageInfo info = { + width, + height, + kPMColor_SkColorType, + kPremul_SkAlphaType, + }; + result->setConfig(info); + result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); return true; } @@ -36,8 +42,12 @@ bool SkImageFilterUtils::GetInputResultGPU(SkImageFilter* filter, SkImageFilter: } 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(resultTex))->unref(); + result->setPixelRef(new SkGrPixelRef(info, resultTex))->unref(); GrUnlockAndUnrefCachedBitmapTexture(resultTex); } return true; diff --git a/src/core/SkMallocPixelRef.cpp b/src/core/SkMallocPixelRef.cpp index f229e9de34..25337e7b9d 100644 --- a/src/core/SkMallocPixelRef.cpp +++ b/src/core/SkMallocPixelRef.cpp @@ -1,26 +1,104 @@ - /* * 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" -SkMallocPixelRef::SkMallocPixelRef(void* storage, size_t size, - SkColorTable* ctable, bool ownPixels) { - if (NULL == storage) { - SkASSERT(ownPixels); - storage = sk_malloc_throw(size); +static bool is_valid(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; + } + + // 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 (!is_valid(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 (!is_valid(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) + : INHERITED(info) + , fOwnPixels(ownsPixels) +{ + SkASSERT(is_valid(info, ctable)); + SkASSERT(rowBytes >= info.minRowBytes()); + + if (kIndex_8_SkColorType != info.fColorType) { + ctable = NULL; + } + fStorage = storage; - fSize = size; fCTable = ctable; + fRB = rowBytes; SkSafeRef(ctable); - fOwnPixels = ownPixels; - + this->setPreLocked(fStorage, fCTable); } @@ -31,8 +109,8 @@ SkMallocPixelRef::~SkMallocPixelRef() { } } -void* SkMallocPixelRef::onLockPixels(SkColorTable** ct) { - *ct = fCTable; +void* SkMallocPixelRef::onLockPixels(SkColorTable** ctable) { + *ctable = fCTable; return fStorage; } @@ -40,10 +118,19 @@ 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.writeByteArray(fStorage, fSize); + 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.writeBool(fCTable != NULL); if (fCTable) { fCTable->writeToBuffer(buffer); @@ -51,16 +138,18 @@ void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { } SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer) - : INHERITED(buffer, NULL) { - fSize = buffer.getArrayCount(); - fStorage = sk_malloc_throw(fSize); - buffer.readByteArray(fStorage, fSize); + : INHERITED(buffer, NULL) + , fOwnPixels(true) +{ + fRB = buffer.read32(); + size_t size = this->info().getSafeSize(fRB); + fStorage = sk_malloc_throw(size); + buffer.readByteArray(fStorage, size); if (buffer.readBool()) { fCTable = SkNEW_ARGS(SkColorTable, (buffer)); } else { fCTable = NULL; } - fOwnPixels = true; this->setPreLocked(fStorage, fCTable); } diff --git a/src/core/SkMaskFilter.cpp b/src/core/SkMaskFilter.cpp index f062f135fd..1bc17bb47f 100644 --- a/src/core/SkMaskFilter.cpp +++ b/src/core/SkMaskFilter.cpp @@ -349,10 +349,14 @@ bool SkMaskFilter::filterMaskGPU(GrContext* context, if (!result) { return false; } + SkAutoUnref aur(dst); + SkImageInfo info; resultBM->setConfig(srcBM.config(), dst->width(), dst->height()); - resultBM->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (dst)))->unref(); - dst->unref(); + if (!resultBM->asImageInfo(&info)) { + return false; + } + resultBM->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, dst)))->unref(); return true; } diff --git a/src/core/SkPixelRef.cpp b/src/core/SkPixelRef.cpp index da88ca5c71..6cc67d89af 100644 --- a/src/core/SkPixelRef.cpp +++ b/src/core/SkPixelRef.cpp @@ -122,6 +122,7 @@ SkPixelRef::SkPixelRef(SkBaseMutex* mutex) { SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) : INHERITED(buffer) { this->setMutex(mutex); + fInfo.unflatten(buffer); fPixels = NULL; fColorTable = NULL; // we do not track ownership of this fLockCount = 0; @@ -160,6 +161,7 @@ void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { 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 diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp index 2776199346..5d200d18d3 100644 --- a/src/effects/gradients/SkGradientShader.cpp +++ b/src/effects/gradients/SkGradientShader.cpp @@ -513,13 +513,14 @@ const uint16_t* SkGradientShaderBase::getCache16() const { const SkPMColor* SkGradientShaderBase::getCache32() const { if (fCache32 == NULL) { - // double the count for dither entries - const int entryCount = kCache32Count * 4; - const size_t allocSize = sizeof(SkPMColor) * entryCount; + SkImageInfo info; + info.fWidth = kCache32Count; + info.fHeight = 4; // for our 4 dither rows + info.fAlphaType = kPremul_SkAlphaType; + info.fColorType = kPMColor_SkColorType; if (NULL == fCache32PixelRef) { - fCache32PixelRef = SkNEW_ARGS(SkMallocPixelRef, - (NULL, allocSize, NULL)); + fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL); } fCache32 = (SkPMColor*)fCache32PixelRef->getAddr(); if (fColorCount == 2) { @@ -541,8 +542,7 @@ const SkPMColor* SkGradientShaderBase::getCache32() const { } if (fMapper) { - SkMallocPixelRef* newPR = SkNEW_ARGS(SkMallocPixelRef, - (NULL, allocSize, NULL)); + SkMallocPixelRef* newPR = SkMallocPixelRef::NewAllocate(info, 0, 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 fed95f232f..1fcc4ff18b 100644 --- a/src/gpu/GrSurface.cpp +++ b/src/gpu/GrSurface.cpp @@ -8,9 +8,19 @@ #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 ce02f2c55f..a413d04204 100644 --- a/src/gpu/SkGpuDevice.cpp +++ b/src/gpu/SkGpuDevice.cpp @@ -214,7 +214,10 @@ void SkGpuDevice::initFromRenderTarget(GrContext* context, if (NULL == surface) { surface = fRenderTarget; } - SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached)); + + SkImageInfo info; + surface->asImageInfo(&info); + SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, cached)); this->setPixelRef(pr, 0)->unref(); } @@ -224,8 +227,8 @@ SkGpuDevice::SkGpuDevice(GrContext* context, 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; @@ -245,6 +248,14 @@ SkGpuDevice::SkGpuDevice(GrContext* context, 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) { @@ -254,7 +265,7 @@ SkGpuDevice::SkGpuDevice(GrContext* context, SkASSERT(NULL != fRenderTarget); // wrap the bitmap with a pixelref to expose our texture - SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture)); + SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, texture)); this->setPixelRef(pr, 0)->unref(); } else { GrPrintf("--- failed to create gpu-offscreen [%d %d]\n", @@ -840,11 +851,12 @@ bool create_mask_GPU(GrContext* context, } SkBitmap wrap_texture(GrTexture* texture) { + SkImageInfo info; + texture->asImageInfo(&info); + SkBitmap result; - bool dummy; - SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy); - result.setConfig(config, texture->width(), texture->height()); - result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref(); + result.setConfig(info); + result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); return result; } diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp index c7ae0c8f36..a3f0eefa91 100644 --- a/src/gpu/SkGr.cpp +++ b/src/gpu/SkGr.cpp @@ -258,3 +258,33 @@ GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) { 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 012942667c..ddd60ff864 100644 --- a/src/gpu/SkGrPixelRef.cpp +++ b/src/gpu/SkGrPixelRef.cpp @@ -18,11 +18,10 @@ // to avoid deadlock with the default one provided by SkPixelRef. SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); -SkROLockPixelsPixelRef::SkROLockPixelsPixelRef() : INHERITED(&gROLockPixelsPixelRefMutex) { -} +SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) + : INHERITED(info, &gROLockPixelsPixelRefMutex) {} -SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() { -} +SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {} void* SkROLockPixelsPixelRef::onLockPixels(SkColorTable** ctable) { if (ctable) { @@ -76,6 +75,14 @@ static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkBitmap::Config 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; @@ -93,31 +100,15 @@ static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkBitmap::Config dst->releaseRenderTarget(); #endif - SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (dst)); + SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst)); SkSafeUnref(dst); return pixelRef; } /////////////////////////////////////////////////////////////////////////////// -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) { +SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface, + bool transferCacheLock) : INHERITED(info) { // 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 7897bf9315..c8bfe8083d 100644 --- a/src/image/SkDataPixelRef.cpp +++ b/src/image/SkDataPixelRef.cpp @@ -9,7 +9,10 @@ #include "SkData.h" #include "SkFlattenableBuffers.h" -SkDataPixelRef::SkDataPixelRef(SkData* data) : fData(data) { +SkDataPixelRef::SkDataPixelRef(const SkImageInfo& info, SkData* data) + : INHERITED(info) + , fData(data) +{ fData->ref(); this->setPreLocked(const_cast<void*>(fData->data()), NULL); } diff --git a/src/image/SkDataPixelRef.h b/src/image/SkDataPixelRef.h index 50c885714b..0f8269c80f 100644 --- a/src/image/SkDataPixelRef.h +++ b/src/image/SkDataPixelRef.h @@ -14,7 +14,7 @@ class SkData; class SkDataPixelRef : public SkPixelRef { public: - SkDataPixelRef(SkData* data); + SkDataPixelRef(const SkImageInfo&, SkData* data); virtual ~SkDataPixelRef(); SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDataPixelRef) diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp index a872ae36e5..807c19e79d 100644 --- a/src/image/SkImage_Raster.cpp +++ b/src/image/SkImage_Raster.cpp @@ -84,10 +84,8 @@ SkImage* SkImage_Raster::NewEmpty() { SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes) : INHERITED(info.fWidth, info.fHeight) { - SkBitmap::Config config = SkImageInfoToBitmapConfig(info); - - fBitmap.setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlphaType); - fBitmap.setPixelRef(SkNEW_ARGS(SkDataPixelRef, (data)))->unref(); + fBitmap.setConfig(info, rowBytes); + fBitmap.setPixelRef(SkNEW_ARGS(SkDataPixelRef, (info, data)))->unref(); fBitmap.setImmutable(); } diff --git a/src/image/SkSurface_Raster.cpp b/src/image/SkSurface_Raster.cpp index 27db504df6..61ade6f46c 100644 --- a/src/image/SkSurface_Raster.cpp +++ b/src/image/SkSurface_Raster.cpp @@ -155,19 +155,9 @@ SkSurface* SkSurface::NewRaster(const SkImageInfo& info) { return NULL; } - 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; - } - - size_t size = (size_t)size64; - void* pixels = sk_malloc_throw(size); - if (NULL == pixels) { + SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); + if (NULL == pr.get()) { return NULL; } - - SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL, true))); - return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes)); + return SkNEW_ARGS(SkSurface_Raster, (info, pr, info.minRowBytes())); } diff --git a/src/images/SkImageRef.cpp b/src/images/SkImageRef.cpp index 1a8284bdd4..716519f080 100644 --- a/src/images/SkImageRef.cpp +++ b/src/images/SkImageRef.cpp @@ -18,13 +18,12 @@ /////////////////////////////////////////////////////////////////////////////// -SkImageRef::SkImageRef(SkStreamRewindable* stream, SkBitmap::Config config, +SkImageRef::SkImageRef(const SkImageInfo& info, SkStreamRewindable* stream, int sampleSize, SkBaseMutex* mutex) - : SkPixelRef(mutex), fErrorInDecoding(false) { + : SkPixelRef(info, mutex), fErrorInDecoding(false) { SkASSERT(stream); stream->ref(); fStream = stream; - fConfig = config; fSampleSize = sampleSize; fDoDither = true; fPrev = fNext = NULL; @@ -32,7 +31,7 @@ SkImageRef::SkImageRef(SkStreamRewindable* stream, SkBitmap::Config config, #ifdef DUMP_IMAGEREF_LIFECYCLE SkDebugf("add ImageRef %p [%d] data=%d\n", - this, config, (int)stream->getLength()); + this, this->info().fColorType, (int)stream->getLength()); #endif } @@ -92,14 +91,6 @@ bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) { 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)) { @@ -125,7 +116,7 @@ bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) { codec->setSampleSize(fSampleSize); codec->setDitherImage(fDoDither); - if (this->onDecode(codec, fStream, &fBitmap, fConfig, mode)) { + if (this->onDecode(codec, fStream, &fBitmap, fBitmap.config(), mode)) { return true; } } @@ -170,7 +161,6 @@ size_t SkImageRef::ramUsed() const { SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) : INHERITED(buffer, mutex), fErrorInDecoding(false) { - fConfig = (SkBitmap::Config)buffer.readUInt(); fSampleSize = buffer.readInt(); fDoDither = buffer.readBool(); @@ -185,7 +175,6 @@ SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) 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 352dd42d9f..f91cebabbf 100644 --- a/src/images/SkImageRef_GlobalPool.cpp +++ b/src/images/SkImageRef_GlobalPool.cpp @@ -24,10 +24,10 @@ static SkImageRefPool* GetGlobalPool() { return gPool; } -SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStreamRewindable* stream, - SkBitmap::Config config, +SkImageRef_GlobalPool::SkImageRef_GlobalPool(const SkImageInfo& info, + SkStreamRewindable* stream, int sampleSize) - : SkImageRef(stream, config, sampleSize, &gGlobalPoolMutex) { + : SkImageRef(info, stream, 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 9933ca9b4b..269199faf8 100644 --- a/src/images/SkImageRef_ashmem.cpp +++ b/src/images/SkImageRef_ashmem.cpp @@ -31,11 +31,11 @@ static size_t roundToPageSize(size_t size) { return newsize; } -SkImageRef_ashmem::SkImageRef_ashmem(SkStreamRewindable* stream, - SkBitmap::Config config, - int sampleSize) - : SkImageRef(stream, config, sampleSize) { - +SkImageRef_ashmem::SkImageRef_ashmem(const SkImageInfo& info, + SkStreamRewindable* stream, + int sampleSize) + : SkImageRef(info, stream, sampleSize) +{ fRec.fFD = -1; fRec.fAddr = NULL; fRec.fSize = 0; diff --git a/src/images/SkImageRef_ashmem.h b/src/images/SkImageRef_ashmem.h index efee5e759b..a2652fbc30 100644 --- a/src/images/SkImageRef_ashmem.h +++ b/src/images/SkImageRef_ashmem.h @@ -19,7 +19,7 @@ struct SkAshmemRec { class SkImageRef_ashmem : public SkImageRef { public: - SkImageRef_ashmem(SkStreamRewindable*, SkBitmap::Config, int sampleSize = 1); + SkImageRef_ashmem(const SkImageInfo&, SkStreamRewindable*, 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 b7eaf574aa..668f57ef30 100644 --- a/src/lazy/SkCachingPixelRef.cpp +++ b/src/lazy/SkCachingPixelRef.cpp @@ -21,20 +21,18 @@ bool SkCachingPixelRef::Install(SkImageGenerator* generator, return false; } SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef, - (generator, - info, - dst->rowBytes()))); + (info, generator, dst->rowBytes()))); dst->setPixelRef(ref); return true; } -SkCachingPixelRef::SkCachingPixelRef(SkImageGenerator* generator, - const SkImageInfo& info, +SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info, + SkImageGenerator* generator, size_t rowBytes) - : fImageGenerator(generator) + : INHERITED(info) + , fImageGenerator(generator) , fErrorInDecoding(false) , fScaledCacheId(NULL) - , fInfo(info) , fRowBytes(rowBytes) { SkASSERT(fImageGenerator != NULL); } @@ -44,31 +42,32 @@ SkCachingPixelRef::~SkCachingPixelRef() { // Assert always unlock before unref. } -void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { - (void)colorTable; +void* SkCachingPixelRef::onLockPixels(SkColorTable**) { + const SkImageInfo& info = this->info(); + if (fErrorInDecoding) { return NULL; // don't try again. } SkBitmap bitmap; SkASSERT(NULL == fScaledCacheId); fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), - fInfo.fWidth, - fInfo.fHeight, + info.fWidth, + info.fHeight, &bitmap); if (NULL == fScaledCacheId) { // Cache has been purged, must re-decode. - if ((!bitmap.setConfig(fInfo, fRowBytes)) || !bitmap.allocPixels()) { + if ((!bitmap.setConfig(info, fRowBytes)) || !bitmap.allocPixels()) { fErrorInDecoding = true; return NULL; } SkAutoLockPixels autoLockPixels(bitmap); - if (!fImageGenerator->getPixels(fInfo, bitmap.getPixels(), fRowBytes)) { + if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) { fErrorInDecoding = true; return NULL; } fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), - fInfo.fWidth, - fInfo.fHeight, + info.fWidth, + info.fHeight, bitmap); SkASSERT(fScaledCacheId != NULL); } diff --git a/src/lazy/SkCachingPixelRef.h b/src/lazy/SkCachingPixelRef.h index 4a0387ddf8..b1f2fcd669 100644 --- a/src/lazy/SkCachingPixelRef.h +++ b/src/lazy/SkCachingPixelRef.h @@ -58,12 +58,10 @@ private: SkImageGenerator* const fImageGenerator; bool fErrorInDecoding; void* fScaledCacheId; - const SkImageInfo fInfo; const size_t fRowBytes; - SkCachingPixelRef(SkImageGenerator* imageGenerator, - const SkImageInfo& info, - size_t rowBytes); + SkCachingPixelRef(const SkImageInfo&, SkImageGenerator*, size_t rowBytes); + typedef SkPixelRef INHERITED; }; diff --git a/src/lazy/SkDiscardablePixelRef.cpp b/src/lazy/SkDiscardablePixelRef.cpp index f551436b3b..160ca5b4fb 100644 --- a/src/lazy/SkDiscardablePixelRef.cpp +++ b/src/lazy/SkDiscardablePixelRef.cpp @@ -9,19 +9,17 @@ #include "SkDiscardableMemory.h" #include "SkImageGenerator.h" -SkDiscardablePixelRef::SkDiscardablePixelRef(SkImageGenerator* generator, - const SkImageInfo& info, - size_t size, +SkDiscardablePixelRef::SkDiscardablePixelRef(const SkImageInfo& info, + SkImageGenerator* generator, size_t rowBytes, SkDiscardableMemory::Factory* fact) - : fGenerator(generator) + : INHERITED(info) + , 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(). @@ -46,16 +44,19 @@ void* SkDiscardablePixelRef::onLockPixels(SkColorTable**) { SkDELETE(fDiscardableMemory); fDiscardableMemory = NULL; } + + const size_t size = this->info().getSafeSize(fRowBytes); + if (fDMFactory != NULL) { - fDiscardableMemory = fDMFactory->create(fSize); + fDiscardableMemory = fDMFactory->create(size); } else { - fDiscardableMemory = SkDiscardableMemory::Create(fSize); + fDiscardableMemory = SkDiscardableMemory::Create(size); } if (NULL == fDiscardableMemory) { return NULL; // Memory allocation failed. } void* pixels = fDiscardableMemory->data(); - if (!fGenerator->getPixels(fInfo, pixels, fRowBytes)) { + if (!fGenerator->getPixels(this->info(), pixels, fRowBytes)) { fDiscardableMemory->unlock(); SkDELETE(fDiscardableMemory); fDiscardableMemory = NULL; @@ -84,10 +85,7 @@ bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, return dst->allocPixels(NULL, NULL); } SkAutoTUnref<SkDiscardablePixelRef> ref(SkNEW_ARGS(SkDiscardablePixelRef, - (generator, info, - dst->getSize(), - dst->rowBytes(), - factory))); + (info, generator, dst->rowBytes(), factory))); dst->setPixelRef(ref); return true; } diff --git a/src/lazy/SkDiscardablePixelRef.h b/src/lazy/SkDiscardablePixelRef.h index 44c6df9637..4b6693837d 100644 --- a/src/lazy/SkDiscardablePixelRef.h +++ b/src/lazy/SkDiscardablePixelRef.h @@ -30,8 +30,6 @@ protected: 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. @@ -39,9 +37,7 @@ private: SkDiscardableMemory* fDiscardableMemory; /* Takes ownership of SkImageGenerator. */ - SkDiscardablePixelRef(SkImageGenerator* generator, - const SkImageInfo& info, - size_t size, + SkDiscardablePixelRef(const SkImageInfo&, SkImageGenerator*, size_t rowBytes, SkDiscardableMemory::Factory* factory); friend bool SkInstallDiscardablePixelRef(SkImageGenerator*, diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp index 49717972d4..447ce4eb4d 100644 --- a/tests/PictureTest.cpp +++ b/tests/PictureTest.cpp @@ -11,6 +11,7 @@ #include "SkCanvas.h" #include "SkColorPriv.h" #include "SkData.h" +#include "SkDecodingImageGenerator.h" #include "SkError.h" #include "SkPaint.h" #include "SkPicture.h" @@ -337,32 +338,6 @@ static void test_bad_bitmap() { } #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) { @@ -404,14 +379,10 @@ static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { return; } SkAutoDataUnref data(wStream.copyToData()); - SkMemoryStream memStream; - memStream.setData(data); - // Use the encoded bitmap as the data for an image ref. SkBitmap bm; - SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream))); - imageRef->getInfo(&bm); - bm.setPixelRef(imageRef); + bool installSuccess = SkDecodingImageGenerator::Install(data, &bm); + REPORTER_ASSERT(reporter, installSuccess); // 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 470221c0fe..e0ffd7a601 100644 --- a/tests/PixelRefTest.cpp +++ b/tests/PixelRefTest.cpp @@ -51,35 +51,37 @@ private: } // namespace DEF_TEST(PixelRef_GenIDChange, r) { - SkMallocPixelRef pixelRef(NULL, 0, NULL); // We don't really care about the pixels here. + SkImageInfo info = { 10, 10, kPMColor_SkColorType, kPremul_SkAlphaType }; + + SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL)); // 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 bb05432427..448c18f4b1 100644 --- a/tests/SerializationTest.cpp +++ b/tests/SerializationTest.cpp @@ -149,6 +149,7 @@ static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, 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) @@ -308,10 +309,22 @@ DEF_TEST(Serialization, reporter) { 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(SkBitmap::kARGB_8888_Config, 256, 256); - invalidBitmap2.setPixelRef(SkNEW_ARGS(SkMallocPixelRef, - (NULL, 256, NULL)))->unref(); + 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(); // The deserialization should detect the pixel ref being too small and fail TestBitmapSerialization(validBitmap, invalidBitmap2, false, reporter); |