diff options
author | 2017-04-03 14:41:44 -0400 | |
---|---|---|
committer | 2017-04-03 19:29:38 +0000 | |
commit | 6b3155c4be0476bc53541b0431c368a44e69f0a7 (patch) | |
tree | 2de5a87716b38a587c475731df815e5c8178e133 /src | |
parent | 2db3232c88cbaec5585f263111f334ca7272fe10 (diff) |
Revert[4] "clean up (partially) colortable api""""
Fixes:
- create temp api for android to pass nullptr
- don't release and access sk_sp<SkData> at the same time in parameters
This reverts commit b14131c1851eea6acbd34cc42a8f860daed36b21.
Bug: skia:
Change-Id: Ic0e4f62520ba9f35455499ed30d306ad19d998a8
Reviewed-on: https://skia-review.googlesource.com/11129
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Matt Sarett <msarett@google.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/core/SkBitmap.cpp | 35 | ||||
-rw-r--r-- | src/core/SkBitmapDevice.cpp | 5 | ||||
-rw-r--r-- | src/core/SkColorTable.cpp | 22 | ||||
-rw-r--r-- | src/core/SkImageCacherator.cpp | 2 | ||||
-rw-r--r-- | src/core/SkMallocPixelRef.cpp | 128 | ||||
-rw-r--r-- | src/core/SkSpecialSurface.cpp | 4 | ||||
-rw-r--r-- | src/effects/gradients/SkGradientShader.cpp | 10 | ||||
-rw-r--r-- | src/effects/gradients/SkGradientShaderPriv.h | 5 | ||||
-rw-r--r-- | src/image/SkSurface_Raster.cpp | 2 |
9 files changed, 101 insertions, 112 deletions
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index 0a999cee42..df3b24ed11 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -300,8 +300,7 @@ void SkBitmap::setPixels(void* p, SkColorTable* ctable) { return; } - sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable)); - this->setPixelRef(std::move(pr), 0, 0); + this->setPixelRef(SkMallocPixelRef::MakeDirect(fInfo, p, fRowBytes, sk_ref_sp(ctable)), 0, 0); if (!fPixelRef) { return; } @@ -334,9 +333,7 @@ bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) // setInfo may have computed a valid rowbytes if 0 were passed in rowBytes = this->rowBytes(); - SkMallocPixelRef::PRFactory defaultFactory; - - sk_sp<SkPixelRef> pr(defaultFactory.create(correctedInfo, rowBytes, nullptr)); + sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(correctedInfo, rowBytes, nullptr); if (!pr) { return reset_return_false(this); } @@ -350,8 +347,8 @@ bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) return true; } -bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory, - SkColorTable* ctable) { +bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, sk_sp<SkColorTable> ctable, + uint32_t allocFlags) { if (kIndex_8_SkColorType == requestedInfo.colorType() && nullptr == ctable) { return reset_return_false(this); } @@ -362,18 +359,14 @@ bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactor // setInfo may have corrected info (e.g. 565 is always opaque). const SkImageInfo& correctedInfo = this->info(); - SkMallocPixelRef::PRFactory defaultFactory; - if (nullptr == factory) { - factory = &defaultFactory; - } - - sk_sp<SkPixelRef> pr(factory->create(correctedInfo, correctedInfo.minRowBytes(), ctable)); + sk_sp<SkPixelRef> pr = (allocFlags & kZeroPixels_AllocFlag) ? + SkMallocPixelRef::MakeZeroed(correctedInfo, correctedInfo.minRowBytes(), ctable) : + SkMallocPixelRef::MakeAllocate(correctedInfo, correctedInfo.minRowBytes(), ctable); if (!pr) { return reset_return_false(this); } this->setPixelRef(std::move(pr), 0, 0); - // TODO: lockPixels could/should return bool or void*/nullptr this->lockPixels(); if (nullptr == this->getPixels()) { return reset_return_false(this); @@ -403,8 +396,8 @@ bool SkBitmap::installPixels(const SkImageInfo& requestedInfo, void* pixels, siz // setInfo may have corrected info (e.g. 565 is always opaque). const SkImageInfo& correctedInfo = this->info(); - sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewWithProc(correctedInfo, rb, ct, pixels, releaseProc, - context)); + sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithProc(correctedInfo, rb, sk_ref_sp(ct), + pixels, releaseProc, context); if (!pr) { this->reset(); return false; @@ -473,7 +466,7 @@ bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, return false; } - sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable)); + sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, dst->rowBytes(), sk_ref_sp(ctable)); if (!pr) { return false; } @@ -1014,7 +1007,7 @@ bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { sk_sp<SkColorTable> ctable; if (buffer->readBool()) { - ctable.reset(SkColorTable::Create(*buffer)); + ctable = SkColorTable::Create(*buffer); if (!ctable) { return false; } @@ -1038,9 +1031,9 @@ bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { } } - sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(), - ctable.get(), data.get())); - if (!pr.get()) { + sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, info.minRowBytes(), + std::move(ctable), std::move(data)); + if (!pr) { return false; } bitmap->setInfo(pr->info()); diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp index 3be63ba66b..95ea45c29d 100644 --- a/src/core/SkBitmapDevice.cpp +++ b/src/core/SkBitmapDevice.cpp @@ -121,9 +121,8 @@ SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo, } } else { // This bitmap has transparency, so we'll zero the pixels (to transparent). - // We use a ZeroedPRFactory as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT). - SkMallocPixelRef::ZeroedPRFactory factory; - if (!bitmap.tryAllocPixels(info, &factory, nullptr/*color table*/)) { + // We use the flag as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT). + if (!bitmap.tryAllocPixels(info, nullptr/*colortable*/, SkBitmap::kZeroPixels_AllocFlag)) { return nullptr; } } diff --git a/src/core/SkColorTable.cpp b/src/core/SkColorTable.cpp index 97103463db..928f5158d4 100644 --- a/src/core/SkColorTable.cpp +++ b/src/core/SkColorTable.cpp @@ -23,11 +23,7 @@ void SkColorTable::init(const SkPMColor colors[], int count) { SkColorTable::SkColorTable(const SkPMColor colors[], int count) { SkASSERT(0 == count || colors); - if (count < 0) { - count = 0; - } else if (count > 256) { - count = 256; - } + SkASSERT(count >= 0 && count <= 256); this->init(colors, count); } @@ -56,6 +52,16 @@ const uint16_t* SkColorTable::read16BitCache() const { return f16BitCache; } +sk_sp<SkColorTable> SkColorTable::Make(const SkPMColor colors[], int count) { + if (count < 0 || count > 256) { + return nullptr; + } + if (count && !colors) { + return nullptr; + } + return sk_make_sp<SkColorTable>(colors, count); +} + /////////////////////////////////////////////////////////////////////////////// #if 0 @@ -85,14 +91,14 @@ void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const { buffer.writeColorArray(fColors, fCount); } -SkColorTable* SkColorTable::Create(SkReadBuffer& buffer) { +sk_sp<SkColorTable> SkColorTable::Create(SkReadBuffer& buffer) { if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) { /*fAlphaType = */buffer.readUInt(); } const int count = buffer.getArrayCount(); if (0 == count) { - return new SkColorTable(nullptr, 0); + return sk_sp<SkColorTable>(new SkColorTable(nullptr, 0)); } if (count < 0 || count > 256) { @@ -106,5 +112,5 @@ SkColorTable* SkColorTable::Create(SkReadBuffer& buffer) { return nullptr; } - return new SkColorTable(colors.release(), count, kAllocatedWithMalloc); + return sk_sp<SkColorTable>(new SkColorTable(colors.release(), count, kAllocatedWithMalloc)); } diff --git a/src/core/SkImageCacherator.cpp b/src/core/SkImageCacherator.cpp index 1a37f3b217..98a813ffd9 100644 --- a/src/core/SkImageCacherator.cpp +++ b/src/core/SkImageCacherator.cpp @@ -157,7 +157,7 @@ bool SkImageCacherator::generateBitmap(SkBitmap* bitmap, const SkImageInfo& deco allocator)) { return false; } - if (!bitmap->tryAllocPixels(decodeInfo, nullptr, full.getColorTable())) { + if (!bitmap->tryAllocPixels(decodeInfo, sk_ref_sp(full.getColorTable()))) { return false; } return full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), diff --git a/src/core/SkMallocPixelRef.cpp b/src/core/SkMallocPixelRef.cpp index 0d758fcb3d..e42a15db21 100644 --- a/src/core/SkMallocPixelRef.cpp +++ b/src/core/SkMallocPixelRef.cpp @@ -37,22 +37,23 @@ static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) { return true; } -SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info, - void* addr, - size_t rowBytes, - SkColorTable* ctable) { - if (!is_valid(info, ctable)) { +sk_sp<SkPixelRef> SkMallocPixelRef::MakeDirect(const SkImageInfo& info, + void* addr, + size_t rowBytes, + sk_sp<SkColorTable> ctable) { + if (!is_valid(info, ctable.get())) { return nullptr; } - return new SkMallocPixelRef(info, addr, rowBytes, ctable, nullptr, nullptr); + return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, std::move(ctable), + nullptr, nullptr)); } - SkMallocPixelRef* SkMallocPixelRef::NewUsing(void*(*alloc)(size_t), - const SkImageInfo& info, - size_t requestedRowBytes, - SkColorTable* ctable) { - if (!is_valid(info, ctable)) { + sk_sp<SkPixelRef> SkMallocPixelRef::MakeUsing(void*(*alloc)(size_t), + const SkImageInfo& info, + size_t requestedRowBytes, + sk_sp<SkColorTable> ctable) { + if (!is_valid(info, ctable.get())) { return nullptr; } @@ -84,62 +85,61 @@ SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info, return nullptr; } - return new SkMallocPixelRef(info, addr, rowBytes, ctable, sk_free_releaseproc, nullptr); + return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, std::move(ctable), + sk_free_releaseproc, nullptr)); } -SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info, +sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info, size_t rowBytes, - SkColorTable* ctable) { + sk_sp<SkColorTable> ctable) { auto sk_malloc_nothrow = [](size_t size) { return sk_malloc_flags(size, 0); }; - return NewUsing(sk_malloc_nothrow, info, rowBytes, ctable); + return MakeUsing(sk_malloc_nothrow, info, rowBytes, std::move(ctable)); } -SkMallocPixelRef* SkMallocPixelRef::NewZeroed(const SkImageInfo& info, - size_t rowBytes, - SkColorTable* ctable) { - return NewUsing(sk_calloc, info, rowBytes, ctable); +sk_sp<SkPixelRef> SkMallocPixelRef::MakeZeroed(const SkImageInfo& info, + size_t rowBytes, + sk_sp<SkColorTable> ctable) { + return MakeUsing(sk_calloc, info, rowBytes, std::move(ctable)); } -SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info, - size_t rowBytes, - SkColorTable* ctable, - void* addr, - SkMallocPixelRef::ReleaseProc proc, - void* context) { - if (!is_valid(info, ctable)) { +static void sk_data_releaseproc(void*, void* dataPtr) { + (static_cast<SkData*>(dataPtr))->unref(); +} + +sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithProc(const SkImageInfo& info, + size_t rowBytes, + sk_sp<SkColorTable> ctable, + void* addr, + SkMallocPixelRef::ReleaseProc proc, + void* context) { + if (!is_valid(info, ctable.get())) { if (proc) { proc(addr, context); } return nullptr; } - return new SkMallocPixelRef(info, addr, rowBytes, ctable, proc, context); + return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, std::move(ctable), + proc, context)); } -static void sk_data_releaseproc(void*, void* dataPtr) { - (static_cast<SkData*>(dataPtr))->unref(); -} - -SkMallocPixelRef* SkMallocPixelRef::NewWithData(const SkImageInfo& info, +sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithData(const SkImageInfo& info, size_t rowBytes, - SkColorTable* ctable, - SkData* data) { + sk_sp<SkColorTable> ctable, + sk_sp<SkData> data) { SkASSERT(data != nullptr); - if (!is_valid(info, ctable)) { + if (!is_valid(info, ctable.get())) { return nullptr; } if ((rowBytes < info.minRowBytes()) || (data->size() < info.getSafeSize(rowBytes))) { return nullptr; } - data->ref(); - SkMallocPixelRef* pr = - new SkMallocPixelRef(info, const_cast<void*>(data->data()), rowBytes, ctable, - sk_data_releaseproc, static_cast<void*>(data)); - SkASSERT(pr != nullptr); - // We rely on the immutability of the pixels to make the - // const_cast okay. - pr->setImmutable(); - return pr; + // must get this address before we call release + void* pixels = const_cast<void*>(data->data()); + SkPixelRef* pr = new SkMallocPixelRef(info, pixels, rowBytes, std::move(ctable), + sk_data_releaseproc, data.release()); + pr->setImmutable(); // since we were created with (immutable) data + return sk_sp<SkPixelRef>(pr); } /////////////////////////////////////////////////////////////////////////////// @@ -148,50 +148,47 @@ SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, size_t rowBytes, SkColorTable* ctable, bool ownsPixels) : INHERITED(info) + , fCTable(sk_ref_sp(ctable)) , fReleaseProc(ownsPixels ? sk_free_releaseproc : nullptr) , fReleaseProcContext(nullptr) { // This constructor is now DEPRICATED. - SkASSERT(is_valid(info, ctable)); + SkASSERT(is_valid(info, fCTable.get())); SkASSERT(rowBytes >= info.minRowBytes()); if (kIndex_8_SkColorType != info.colorType()) { - ctable = nullptr; + fCTable = nullptr; } fStorage = storage; - fCTable = ctable; fRB = rowBytes; - SkSafeRef(ctable); - this->setPreLocked(fStorage, rowBytes, fCTable); + this->setPreLocked(fStorage, rowBytes, fCTable.get()); } SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, - size_t rowBytes, SkColorTable* ctable, + size_t rowBytes, sk_sp<SkColorTable> ctable, SkMallocPixelRef::ReleaseProc proc, void* context) : INHERITED(info) , fReleaseProc(proc) , fReleaseProcContext(context) { - SkASSERT(is_valid(info, ctable)); + SkASSERT(is_valid(info, ctable.get())); SkASSERT(rowBytes >= info.minRowBytes()); if (kIndex_8_SkColorType != info.colorType()) { - ctable = nullptr; + ctable.reset(nullptr); } fStorage = storage; - fCTable = ctable; + fCTable = std::move(ctable); fRB = rowBytes; - SkSafeRef(ctable); - this->setPreLocked(fStorage, rowBytes, fCTable); + this->setPreLocked(fStorage, rowBytes, fCTable.get()); } SkMallocPixelRef::~SkMallocPixelRef() { - SkSafeUnref(fCTable); if (fReleaseProc != nullptr) { fReleaseProc(fStorage, fReleaseProcContext); } @@ -200,7 +197,7 @@ SkMallocPixelRef::~SkMallocPixelRef() { bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) { rec->fPixels = fStorage; rec->fRowBytes = fRB; - rec->fColorTable = fCTable; + rec->fColorTable = fCTable.get(); return true; } @@ -212,14 +209,11 @@ size_t SkMallocPixelRef::getAllocatedSizeInBytes() const { return this->info().getSafeSize(fRB); } -/////////////////////////////////////////////////////////////////////////////// - -SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info, size_t rowBytes, - SkColorTable* ctable) { - return SkMallocPixelRef::NewAllocate(info, rowBytes, ctable); -} - -SkPixelRef* SkMallocPixelRef::ZeroedPRFactory::create(const SkImageInfo& info, size_t rowBytes, - SkColorTable* ctable) { - return SkMallocPixelRef::NewZeroed(info, rowBytes, ctable); +#ifdef SK_SUPPORT_LEGACY_PIXELREFFACTORY +SkMallocPixelRef* SkMallocPixelRef::NewWithData(const SkImageInfo& info, + size_t rowBytes, + SkColorTable* ctable, + SkData* data) { + return (SkMallocPixelRef*)MakeWithData(info, rowBytes, sk_ref_sp(ctable), sk_ref_sp(data)).release(); } +#endif diff --git a/src/core/SkSpecialSurface.cpp b/src/core/SkSpecialSurface.cpp index 1f396a7ac2..40afb57cec 100644 --- a/src/core/SkSpecialSurface.cpp +++ b/src/core/SkSpecialSurface.cpp @@ -98,8 +98,8 @@ sk_sp<SkSpecialSurface> SkSpecialSurface::MakeFromBitmap(const SkIRect& subset, sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRaster(const SkImageInfo& info, const SkSurfaceProps* props) { - sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, 0, nullptr)); - if (nullptr == pr.get()) { + sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeZeroed(info, 0, nullptr); + if (!pr) { return nullptr; } diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp index 0840d7c55e..be60dee15e 100644 --- a/src/effects/gradients/SkGradientShader.cpp +++ b/src/effects/gradients/SkGradientShader.cpp @@ -10,6 +10,7 @@ #include "SkGradientShaderPriv.h" #include "SkHalf.h" #include "SkLinearGradient.h" +#include "SkMallocPixelRef.h" #include "SkRadialGradient.h" #include "SkTwoPointConicalGradient.h" #include "SkSweepGradient.h" @@ -410,12 +411,9 @@ SkGradientShaderBase::GradientShaderCache::GradientShaderCache( { // Only initialize the cache in getCache32. fCache32 = nullptr; - fCache32PixelRef = nullptr; } -SkGradientShaderBase::GradientShaderCache::~GradientShaderCache() { - SkSafeUnref(fCache32PixelRef); -} +SkGradientShaderBase::GradientShaderCache::~GradientShaderCache() {} /* * r,g,b used to be SkFixed, but on gcc (4.2.1 mac and 4.6.3 goobuntu) in @@ -584,8 +582,8 @@ void SkGradientShaderBase::GradientShaderCache::initCache32(GradientShaderCache* const SkImageInfo info = SkImageInfo::MakeN32Premul(kCache32Count, kNumberOfDitherRows); SkASSERT(nullptr == cache->fCache32PixelRef); - cache->fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, nullptr); - cache->fCache32 = (SkPMColor*)cache->fCache32PixelRef->getAddr(); + cache->fCache32PixelRef = SkMallocPixelRef::MakeAllocate(info, 0, nullptr); + cache->fCache32 = (SkPMColor*)cache->fCache32PixelRef->pixels(); if (cache->fShader.fColorCount == 2) { Build32bitCache(cache->fCache32, cache->fShader.fOrigColors[0], cache->fShader.fOrigColors[1], kCache32Count, cache->fCacheAlpha, diff --git a/src/effects/gradients/SkGradientShaderPriv.h b/src/effects/gradients/SkGradientShaderPriv.h index f64b4396c2..ec75bdc05a 100644 --- a/src/effects/gradients/SkGradientShaderPriv.h +++ b/src/effects/gradients/SkGradientShaderPriv.h @@ -16,7 +16,6 @@ #include "SkClampRange.h" #include "SkColorPriv.h" #include "SkColorSpace.h" -#include "SkMallocPixelRef.h" #include "SkOnce.h" #include "SkReadBuffer.h" #include "SkShader.h" @@ -129,7 +128,7 @@ public: const SkPMColor* getCache32(); - SkMallocPixelRef* getCache32PixelRef() const { return fCache32PixelRef; } + SkPixelRef* getCache32PixelRef() const { return fCache32PixelRef.get(); } unsigned getAlpha() const { return fCacheAlpha; } bool getDither() const { return fCacheDither; } @@ -138,7 +137,7 @@ public: // Working pointer. If it's nullptr, we need to recompute the cache values. SkPMColor* fCache32; - SkMallocPixelRef* fCache32PixelRef; + sk_sp<SkPixelRef> fCache32PixelRef; const unsigned fCacheAlpha; // The alpha value we used when we computed the cache. // Larger than 8bits so we can store uninitialized // value. diff --git a/src/image/SkSurface_Raster.cpp b/src/image/SkSurface_Raster.cpp index 4fae0f03c0..92f5301061 100644 --- a/src/image/SkSurface_Raster.cpp +++ b/src/image/SkSurface_Raster.cpp @@ -209,7 +209,7 @@ sk_sp<SkSurface> SkSurface::MakeRaster(const SkImageInfo& info, size_t rowBytes, return nullptr; } - sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, rowBytes, nullptr)); + sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeZeroed(info, rowBytes, nullptr); if (!pr) { return nullptr; } |