diff options
author | 2017-04-01 12:14:57 -0400 | |
---|---|---|
committer | 2017-04-01 16:46:11 +0000 | |
commit | 9920b10f5292838f00600f676c4578cd11705e60 (patch) | |
tree | 9217d23bf701b4ccfd15d818bfea44b766ad4fc3 /src/core | |
parent | ec61785bbb989a1901b063923da30c04ed41332f (diff) |
Revert[2] "clean up (partially) colortable api""
This reverts commit 1d1165ca6575e082b892c5460492c411618783ad.
Bug: skia:
Change-Id: Idbc0634ae3cec2e79f592d252de8751b077e6408
Reviewed-on: https://skia-review.googlesource.com/11024
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
Diffstat (limited to 'src/core')
-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 | 126 | ||||
-rw-r--r-- | src/core/SkSpecialSurface.cpp | 4 |
6 files changed, 92 insertions, 102 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 76da0e762e..e3c97d390b 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..7197aebae5 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,59 @@ 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; + SkPixelRef* pr = new SkMallocPixelRef(info, const_cast<void*>(data->data()), 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 +146,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 +195,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 +207,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; } |