diff options
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, 102 insertions, 92 deletions
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index df3b24ed11..0a999cee42 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -300,7 +300,8 @@ void SkBitmap::setPixels(void* p, SkColorTable* ctable) { return; } - this->setPixelRef(SkMallocPixelRef::MakeDirect(fInfo, p, fRowBytes, sk_ref_sp(ctable)), 0, 0); + sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable)); + this->setPixelRef(std::move(pr), 0, 0); if (!fPixelRef) { return; } @@ -333,7 +334,9 @@ bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) // setInfo may have computed a valid rowbytes if 0 were passed in rowBytes = this->rowBytes(); - sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(correctedInfo, rowBytes, nullptr); + SkMallocPixelRef::PRFactory defaultFactory; + + sk_sp<SkPixelRef> pr(defaultFactory.create(correctedInfo, rowBytes, nullptr)); if (!pr) { return reset_return_false(this); } @@ -347,8 +350,8 @@ bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) return true; } -bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, sk_sp<SkColorTable> ctable, - uint32_t allocFlags) { +bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory, + SkColorTable* ctable) { if (kIndex_8_SkColorType == requestedInfo.colorType() && nullptr == ctable) { return reset_return_false(this); } @@ -359,14 +362,18 @@ bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, sk_sp<SkColorTab // setInfo may have corrected info (e.g. 565 is always opaque). const SkImageInfo& correctedInfo = this->info(); - sk_sp<SkPixelRef> pr = (allocFlags & kZeroPixels_AllocFlag) ? - SkMallocPixelRef::MakeZeroed(correctedInfo, correctedInfo.minRowBytes(), ctable) : - SkMallocPixelRef::MakeAllocate(correctedInfo, correctedInfo.minRowBytes(), ctable); + SkMallocPixelRef::PRFactory defaultFactory; + if (nullptr == factory) { + factory = &defaultFactory; + } + + sk_sp<SkPixelRef> pr(factory->create(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); @@ -396,8 +403,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::MakeWithProc(correctedInfo, rb, sk_ref_sp(ct), - pixels, releaseProc, context); + sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewWithProc(correctedInfo, rb, ct, pixels, releaseProc, + context)); if (!pr) { this->reset(); return false; @@ -466,7 +473,7 @@ bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, return false; } - sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, dst->rowBytes(), sk_ref_sp(ctable)); + sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable)); if (!pr) { return false; } @@ -1007,7 +1014,7 @@ bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { sk_sp<SkColorTable> ctable; if (buffer->readBool()) { - ctable = SkColorTable::Create(*buffer); + ctable.reset(SkColorTable::Create(*buffer)); if (!ctable) { return false; } @@ -1031,9 +1038,9 @@ bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { } } - sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, info.minRowBytes(), - std::move(ctable), std::move(data)); - if (!pr) { + sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(), + ctable.get(), data.get())); + if (!pr.get()) { return false; } bitmap->setInfo(pr->info()); diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp index 95ea45c29d..3be63ba66b 100644 --- a/src/core/SkBitmapDevice.cpp +++ b/src/core/SkBitmapDevice.cpp @@ -121,8 +121,9 @@ SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo, } } else { // This bitmap has transparency, so we'll zero the pixels (to transparent). - // We use the flag as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT). - if (!bitmap.tryAllocPixels(info, nullptr/*colortable*/, SkBitmap::kZeroPixels_AllocFlag)) { + // We use a ZeroedPRFactory as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT). + SkMallocPixelRef::ZeroedPRFactory factory; + if (!bitmap.tryAllocPixels(info, &factory, nullptr/*color table*/)) { return nullptr; } } diff --git a/src/core/SkColorTable.cpp b/src/core/SkColorTable.cpp index 928f5158d4..97103463db 100644 --- a/src/core/SkColorTable.cpp +++ b/src/core/SkColorTable.cpp @@ -23,7 +23,11 @@ void SkColorTable::init(const SkPMColor colors[], int count) { SkColorTable::SkColorTable(const SkPMColor colors[], int count) { SkASSERT(0 == count || colors); - SkASSERT(count >= 0 && count <= 256); + if (count < 0) { + count = 0; + } else if (count > 256) { + count = 256; + } this->init(colors, count); } @@ -52,16 +56,6 @@ 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 @@ -91,14 +85,14 @@ void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const { buffer.writeColorArray(fColors, fCount); } -sk_sp<SkColorTable> SkColorTable::Create(SkReadBuffer& buffer) { +SkColorTable* SkColorTable::Create(SkReadBuffer& buffer) { if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) { /*fAlphaType = */buffer.readUInt(); } const int count = buffer.getArrayCount(); if (0 == count) { - return sk_sp<SkColorTable>(new SkColorTable(nullptr, 0)); + return new SkColorTable(nullptr, 0); } if (count < 0 || count > 256) { @@ -112,5 +106,5 @@ sk_sp<SkColorTable> SkColorTable::Create(SkReadBuffer& buffer) { return nullptr; } - return sk_sp<SkColorTable>(new SkColorTable(colors.release(), count, kAllocatedWithMalloc)); + return new SkColorTable(colors.release(), count, kAllocatedWithMalloc); } diff --git a/src/core/SkImageCacherator.cpp b/src/core/SkImageCacherator.cpp index e3c97d390b..76da0e762e 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, sk_ref_sp(full.getColorTable()))) { + if (!bitmap->tryAllocPixels(decodeInfo, nullptr, 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 7197aebae5..0d758fcb3d 100644 --- a/src/core/SkMallocPixelRef.cpp +++ b/src/core/SkMallocPixelRef.cpp @@ -37,23 +37,22 @@ static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) { return true; } -sk_sp<SkPixelRef> SkMallocPixelRef::MakeDirect(const SkImageInfo& info, - void* addr, - size_t rowBytes, - sk_sp<SkColorTable> ctable) { - if (!is_valid(info, ctable.get())) { +SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info, + void* addr, + size_t rowBytes, + SkColorTable* ctable) { + if (!is_valid(info, ctable)) { return nullptr; } - return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, std::move(ctable), - nullptr, nullptr)); + return new SkMallocPixelRef(info, addr, rowBytes, ctable, nullptr, nullptr); } - 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())) { + SkMallocPixelRef* SkMallocPixelRef::NewUsing(void*(*alloc)(size_t), + const SkImageInfo& info, + size_t requestedRowBytes, + SkColorTable* ctable) { + if (!is_valid(info, ctable)) { return nullptr; } @@ -85,59 +84,62 @@ sk_sp<SkPixelRef> SkMallocPixelRef::MakeDirect(const SkImageInfo& info, return nullptr; } - return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, std::move(ctable), - sk_free_releaseproc, nullptr)); + return new SkMallocPixelRef(info, addr, rowBytes, ctable, sk_free_releaseproc, nullptr); } -sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info, +SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info, size_t rowBytes, - sk_sp<SkColorTable> ctable) { + SkColorTable* ctable) { auto sk_malloc_nothrow = [](size_t size) { return sk_malloc_flags(size, 0); }; - return MakeUsing(sk_malloc_nothrow, info, rowBytes, std::move(ctable)); + return NewUsing(sk_malloc_nothrow, 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::NewZeroed(const SkImageInfo& info, + size_t rowBytes, + SkColorTable* ctable) { + return NewUsing(sk_calloc, info, rowBytes, 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())) { +SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info, + size_t rowBytes, + SkColorTable* ctable, + void* addr, + SkMallocPixelRef::ReleaseProc proc, + void* context) { + if (!is_valid(info, ctable)) { if (proc) { proc(addr, context); } return nullptr; } - return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, std::move(ctable), - proc, context)); + return new SkMallocPixelRef(info, addr, rowBytes, ctable, proc, context); } -sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithData(const SkImageInfo& info, +static void sk_data_releaseproc(void*, void* dataPtr) { + (static_cast<SkData*>(dataPtr))->unref(); +} + +SkMallocPixelRef* SkMallocPixelRef::NewWithData(const SkImageInfo& info, size_t rowBytes, - sk_sp<SkColorTable> ctable, - sk_sp<SkData> data) { + SkColorTable* ctable, + SkData* data) { SkASSERT(data != nullptr); - if (!is_valid(info, ctable.get())) { + if (!is_valid(info, ctable)) { return nullptr; } if ((rowBytes < info.minRowBytes()) || (data->size() < info.getSafeSize(rowBytes))) { return nullptr; } - 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); + 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; } /////////////////////////////////////////////////////////////////////////////// @@ -146,47 +148,50 @@ 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, fCTable.get())); + SkASSERT(is_valid(info, ctable)); SkASSERT(rowBytes >= info.minRowBytes()); if (kIndex_8_SkColorType != info.colorType()) { - fCTable = nullptr; + ctable = nullptr; } fStorage = storage; + fCTable = ctable; fRB = rowBytes; + SkSafeRef(ctable); - this->setPreLocked(fStorage, rowBytes, fCTable.get()); + this->setPreLocked(fStorage, rowBytes, fCTable); } SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, - size_t rowBytes, sk_sp<SkColorTable> ctable, + size_t rowBytes, SkColorTable* ctable, SkMallocPixelRef::ReleaseProc proc, void* context) : INHERITED(info) , fReleaseProc(proc) , fReleaseProcContext(context) { - SkASSERT(is_valid(info, ctable.get())); + SkASSERT(is_valid(info, ctable)); SkASSERT(rowBytes >= info.minRowBytes()); if (kIndex_8_SkColorType != info.colorType()) { - ctable.reset(nullptr); + ctable = nullptr; } fStorage = storage; - fCTable = std::move(ctable); + fCTable = ctable; fRB = rowBytes; + SkSafeRef(ctable); - this->setPreLocked(fStorage, rowBytes, fCTable.get()); + this->setPreLocked(fStorage, rowBytes, fCTable); } SkMallocPixelRef::~SkMallocPixelRef() { + SkSafeUnref(fCTable); if (fReleaseProc != nullptr) { fReleaseProc(fStorage, fReleaseProcContext); } @@ -195,7 +200,7 @@ SkMallocPixelRef::~SkMallocPixelRef() { bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) { rec->fPixels = fStorage; rec->fRowBytes = fRB; - rec->fColorTable = fCTable.get(); + rec->fColorTable = fCTable; return true; } @@ -207,11 +212,14 @@ size_t SkMallocPixelRef::getAllocatedSizeInBytes() const { return this->info().getSafeSize(fRB); } -#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(); +/////////////////////////////////////////////////////////////////////////////// + +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); } -#endif diff --git a/src/core/SkSpecialSurface.cpp b/src/core/SkSpecialSurface.cpp index 40afb57cec..1f396a7ac2 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::MakeZeroed(info, 0, nullptr); - if (!pr) { + sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, 0, nullptr)); + if (nullptr == pr.get()) { return nullptr; } |