michael@0: michael@0: /* michael@0: * Copyright 2008 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkBitmap.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkDither.h" michael@0: #include "SkFlattenable.h" michael@0: #include "SkImagePriv.h" michael@0: #include "SkMallocPixelRef.h" michael@0: #include "SkMask.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkPixelRef.h" michael@0: #include "SkThread.h" michael@0: #include "SkUnPreMultiply.h" michael@0: #include "SkUtils.h" michael@0: #include "SkValidationUtils.h" michael@0: #include "SkPackBits.h" michael@0: #include michael@0: michael@0: static bool reset_return_false(SkBitmap* bm) { michael@0: bm->reset(); michael@0: return false; michael@0: } michael@0: michael@0: struct MipLevel { michael@0: void* fPixels; michael@0: uint32_t fRowBytes; michael@0: uint32_t fWidth, fHeight; michael@0: }; michael@0: michael@0: struct SkBitmap::MipMap : SkNoncopyable { michael@0: int32_t fRefCnt; michael@0: int fLevelCount; michael@0: // MipLevel fLevel[fLevelCount]; michael@0: // Pixels[] michael@0: michael@0: static MipMap* Alloc(int levelCount, size_t pixelSize) { michael@0: if (levelCount < 0) { michael@0: return NULL; michael@0: } michael@0: int64_t size = (levelCount + 1) * sizeof(MipLevel); michael@0: size += sizeof(MipMap) + pixelSize; michael@0: if (!sk_64_isS32(size)) { michael@0: return NULL; michael@0: } michael@0: MipMap* mm = (MipMap*)sk_malloc_throw(sk_64_asS32(size)); michael@0: mm->fRefCnt = 1; michael@0: mm->fLevelCount = levelCount; michael@0: return mm; michael@0: } michael@0: michael@0: const MipLevel* levels() const { return (const MipLevel*)(this + 1); } michael@0: MipLevel* levels() { return (MipLevel*)(this + 1); } michael@0: michael@0: const void* pixels() const { return levels() + fLevelCount; } michael@0: void* pixels() { return levels() + fLevelCount; } michael@0: michael@0: void ref() { michael@0: if (SK_MaxS32 == sk_atomic_inc(&fRefCnt)) { michael@0: sk_throw(); michael@0: } michael@0: } michael@0: void unref() { michael@0: SkASSERT(fRefCnt > 0); michael@0: if (sk_atomic_dec(&fRefCnt) == 1) { michael@0: sk_free(this); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkBitmap::SkBitmap() { michael@0: sk_bzero(this, sizeof(*this)); michael@0: } michael@0: michael@0: SkBitmap::SkBitmap(const SkBitmap& src) { michael@0: SkDEBUGCODE(src.validate();) michael@0: sk_bzero(this, sizeof(*this)); michael@0: *this = src; michael@0: SkDEBUGCODE(this->validate();) michael@0: } michael@0: michael@0: SkBitmap::~SkBitmap() { michael@0: SkDEBUGCODE(this->validate();) michael@0: this->freePixels(); michael@0: } michael@0: michael@0: SkBitmap& SkBitmap::operator=(const SkBitmap& src) { michael@0: if (this != &src) { michael@0: this->freePixels(); michael@0: memcpy(this, &src, sizeof(src)); michael@0: michael@0: // inc src reference counts michael@0: SkSafeRef(src.fPixelRef); michael@0: SkSafeRef(src.fMipMap); michael@0: michael@0: // we reset our locks if we get blown away michael@0: fPixelLockCount = 0; michael@0: michael@0: if (fPixelRef) { michael@0: // ignore the values from the memcpy michael@0: fPixels = NULL; michael@0: fColorTable = NULL; michael@0: // Note that what to for genID is somewhat arbitrary. We have no michael@0: // way to track changes to raw pixels across multiple SkBitmaps. michael@0: // Would benefit from an SkRawPixelRef type created by michael@0: // setPixels. michael@0: // Just leave the memcpy'ed one but they'll get out of sync michael@0: // as soon either is modified. michael@0: } michael@0: } michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: return *this; michael@0: } michael@0: michael@0: void SkBitmap::swap(SkBitmap& other) { michael@0: SkTSwap(fColorTable, other.fColorTable); michael@0: SkTSwap(fPixelRef, other.fPixelRef); michael@0: SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin); michael@0: SkTSwap(fPixelLockCount, other.fPixelLockCount); michael@0: SkTSwap(fMipMap, other.fMipMap); michael@0: SkTSwap(fPixels, other.fPixels); michael@0: SkTSwap(fInfo, other.fInfo); michael@0: SkTSwap(fRowBytes, other.fRowBytes); michael@0: SkTSwap(fFlags, other.fFlags); michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: } michael@0: michael@0: void SkBitmap::reset() { michael@0: this->freePixels(); michael@0: sk_bzero(this, sizeof(*this)); michael@0: } michael@0: michael@0: SkBitmap::Config SkBitmap::config() const { michael@0: return SkColorTypeToBitmapConfig(fInfo.colorType()); michael@0: } michael@0: michael@0: int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) { michael@0: int bpp; michael@0: switch (config) { michael@0: case kNo_Config: michael@0: bpp = 0; // not applicable michael@0: break; michael@0: case kA8_Config: michael@0: case kIndex8_Config: michael@0: bpp = 1; michael@0: break; michael@0: case kRGB_565_Config: michael@0: case kARGB_4444_Config: michael@0: bpp = 2; michael@0: break; michael@0: case kARGB_8888_Config: michael@0: bpp = 4; michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("unknown config"); michael@0: bpp = 0; // error michael@0: break; michael@0: } michael@0: return bpp; michael@0: } michael@0: michael@0: size_t SkBitmap::ComputeRowBytes(Config c, int width) { michael@0: return SkColorTypeMinRowBytes(SkBitmapConfigToColorType(c), width); michael@0: } michael@0: michael@0: int64_t SkBitmap::ComputeSize64(Config config, int width, int height) { michael@0: SkColorType ct = SkBitmapConfigToColorType(config); michael@0: int64_t rowBytes = sk_64_mul(SkColorTypeBytesPerPixel(ct), width); michael@0: return rowBytes * height; michael@0: } michael@0: michael@0: size_t SkBitmap::ComputeSize(Config c, int width, int height) { michael@0: int64_t size = SkBitmap::ComputeSize64(c, width, height); michael@0: return sk_64_isS32(size) ? sk_64_asS32(size) : 0; michael@0: } michael@0: michael@0: int64_t SkBitmap::ComputeSafeSize64(Config config, michael@0: uint32_t width, michael@0: uint32_t height, michael@0: size_t rowBytes) { michael@0: SkImageInfo info = SkImageInfo::Make(width, height, michael@0: SkBitmapConfigToColorType(config), michael@0: kPremul_SkAlphaType); michael@0: return info.getSafeSize64(rowBytes); michael@0: } michael@0: michael@0: size_t SkBitmap::ComputeSafeSize(Config config, michael@0: uint32_t width, michael@0: uint32_t height, michael@0: size_t rowBytes) { michael@0: int64_t safeSize = ComputeSafeSize64(config, width, height, rowBytes); michael@0: int32_t safeSize32 = (int32_t)safeSize; michael@0: michael@0: if (safeSize32 != safeSize) { michael@0: safeSize32 = 0; michael@0: } michael@0: return safeSize32; michael@0: } michael@0: michael@0: void SkBitmap::getBounds(SkRect* bounds) const { michael@0: SkASSERT(bounds); michael@0: bounds->set(0, 0, michael@0: SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight)); michael@0: } michael@0: michael@0: void SkBitmap::getBounds(SkIRect* bounds) const { michael@0: SkASSERT(bounds); michael@0: bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static bool validate_alphaType(SkColorType colorType, SkAlphaType alphaType, michael@0: SkAlphaType* canonical = NULL) { michael@0: switch (colorType) { michael@0: case kUnknown_SkColorType: michael@0: alphaType = kIgnore_SkAlphaType; michael@0: break; michael@0: case kAlpha_8_SkColorType: michael@0: if (kUnpremul_SkAlphaType == alphaType) { michael@0: alphaType = kPremul_SkAlphaType; michael@0: } michael@0: // fall-through michael@0: case kIndex_8_SkColorType: michael@0: case kARGB_4444_SkColorType: michael@0: case kRGBA_8888_SkColorType: michael@0: case kBGRA_8888_SkColorType: michael@0: if (kIgnore_SkAlphaType == alphaType) { michael@0: return false; michael@0: } michael@0: break; michael@0: case kRGB_565_SkColorType: michael@0: alphaType = kOpaque_SkAlphaType; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: if (canonical) { michael@0: *canonical = alphaType; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkBitmap::setConfig(const SkImageInfo& origInfo, size_t rowBytes) { michael@0: SkImageInfo info = origInfo; michael@0: michael@0: if (!validate_alphaType(info.fColorType, info.fAlphaType, michael@0: &info.fAlphaType)) { michael@0: return reset_return_false(this); michael@0: } michael@0: michael@0: // require that rowBytes fit in 31bits michael@0: int64_t mrb = info.minRowBytes64(); michael@0: if ((int32_t)mrb != mrb) { michael@0: return reset_return_false(this); michael@0: } michael@0: if ((int64_t)rowBytes != (int32_t)rowBytes) { michael@0: return reset_return_false(this); michael@0: } michael@0: michael@0: if (info.width() < 0 || info.height() < 0) { michael@0: return reset_return_false(this); michael@0: } michael@0: michael@0: if (kUnknown_SkColorType == info.colorType()) { michael@0: rowBytes = 0; michael@0: } else if (0 == rowBytes) { michael@0: rowBytes = (size_t)mrb; michael@0: } else if (rowBytes < info.minRowBytes()) { michael@0: return reset_return_false(this); michael@0: } michael@0: michael@0: this->freePixels(); michael@0: michael@0: fInfo = info; michael@0: fRowBytes = SkToU32(rowBytes); michael@0: return true; michael@0: } michael@0: michael@0: bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes, michael@0: SkAlphaType alphaType) { michael@0: SkColorType ct = SkBitmapConfigToColorType(config); michael@0: return this->setConfig(SkImageInfo::Make(width, height, ct, alphaType), michael@0: rowBytes); michael@0: } michael@0: michael@0: bool SkBitmap::setAlphaType(SkAlphaType alphaType) { michael@0: if (!validate_alphaType(fInfo.fColorType, alphaType, &alphaType)) { michael@0: return false; michael@0: } michael@0: if (fInfo.fAlphaType != alphaType) { michael@0: fInfo.fAlphaType = alphaType; michael@0: if (fPixelRef) { michael@0: fPixelRef->changeAlphaType(alphaType); michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void SkBitmap::updatePixelsFromRef() const { michael@0: if (NULL != fPixelRef) { michael@0: if (fPixelLockCount > 0) { michael@0: SkASSERT(fPixelRef->isLocked()); michael@0: michael@0: void* p = fPixelRef->pixels(); michael@0: if (NULL != p) { michael@0: p = (char*)p michael@0: + fPixelRefOrigin.fY * fRowBytes michael@0: + fPixelRefOrigin.fX * fInfo.bytesPerPixel(); michael@0: } michael@0: fPixels = p; michael@0: fColorTable = fPixelRef->colorTable(); michael@0: } else { michael@0: SkASSERT(0 == fPixelLockCount); michael@0: fPixels = NULL; michael@0: fColorTable = NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static bool config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) { michael@0: SkColorType ct; michael@0: switch (config) { michael@0: case SkBitmap::kA8_Config: michael@0: ct = kAlpha_8_SkColorType; michael@0: break; michael@0: case SkBitmap::kIndex8_Config: michael@0: ct = kIndex_8_SkColorType; michael@0: break; michael@0: case SkBitmap::kRGB_565_Config: michael@0: ct = kRGB_565_SkColorType; michael@0: break; michael@0: case SkBitmap::kARGB_4444_Config: michael@0: ct = kARGB_4444_SkColorType; michael@0: break; michael@0: case SkBitmap::kARGB_8888_Config: michael@0: ct = kPMColor_SkColorType; michael@0: break; michael@0: case SkBitmap::kNo_Config: michael@0: default: michael@0: return false; michael@0: } michael@0: if (ctOut) { michael@0: *ctOut = ct; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) { michael@0: #ifdef SK_DEBUG michael@0: if (pr) { michael@0: SkImageInfo info; michael@0: if (this->asImageInfo(&info)) { michael@0: const SkImageInfo& prInfo = pr->info(); michael@0: SkASSERT(info.fWidth <= prInfo.fWidth); michael@0: SkASSERT(info.fHeight <= prInfo.fHeight); michael@0: SkASSERT(info.fColorType == prInfo.fColorType); michael@0: switch (prInfo.fAlphaType) { michael@0: case kIgnore_SkAlphaType: michael@0: SkASSERT(fInfo.fAlphaType == kIgnore_SkAlphaType); michael@0: break; michael@0: case kOpaque_SkAlphaType: michael@0: case kPremul_SkAlphaType: michael@0: SkASSERT(info.fAlphaType == kOpaque_SkAlphaType || michael@0: info.fAlphaType == kPremul_SkAlphaType); michael@0: break; michael@0: case kUnpremul_SkAlphaType: michael@0: SkASSERT(info.fAlphaType == kOpaque_SkAlphaType || michael@0: info.fAlphaType == kUnpremul_SkAlphaType); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (pr) { michael@0: const SkImageInfo& info = pr->info(); michael@0: fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth), michael@0: SkPin32(dy, 0, info.fHeight)); michael@0: } else { michael@0: // ignore dx,dy if there is no pixelref michael@0: fPixelRefOrigin.setZero(); michael@0: } michael@0: michael@0: if (fPixelRef != pr) { michael@0: if (fPixelRef != pr) { michael@0: this->freePixels(); michael@0: SkASSERT(NULL == fPixelRef); michael@0: michael@0: SkSafeRef(pr); michael@0: fPixelRef = pr; michael@0: } michael@0: this->updatePixelsFromRef(); michael@0: } michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: return pr; michael@0: } michael@0: michael@0: void SkBitmap::lockPixels() const { michael@0: if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) { michael@0: fPixelRef->lockPixels(); michael@0: this->updatePixelsFromRef(); michael@0: } michael@0: SkDEBUGCODE(this->validate();) michael@0: } michael@0: michael@0: void SkBitmap::unlockPixels() const { michael@0: SkASSERT(NULL == fPixelRef || fPixelLockCount > 0); michael@0: michael@0: if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) { michael@0: fPixelRef->unlockPixels(); michael@0: this->updatePixelsFromRef(); michael@0: } michael@0: SkDEBUGCODE(this->validate();) michael@0: } michael@0: michael@0: bool SkBitmap::lockPixelsAreWritable() const { michael@0: return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false; michael@0: } michael@0: michael@0: void SkBitmap::setPixels(void* p, SkColorTable* ctable) { michael@0: if (NULL == p) { michael@0: this->setPixelRef(NULL); michael@0: return; michael@0: } michael@0: michael@0: SkImageInfo info; michael@0: if (!this->asImageInfo(&info)) { michael@0: this->setPixelRef(NULL); michael@0: return; michael@0: } michael@0: michael@0: SkPixelRef* pr = SkMallocPixelRef::NewDirect(info, p, fRowBytes, ctable); michael@0: if (NULL == pr) { michael@0: this->setPixelRef(NULL); michael@0: return; michael@0: } michael@0: michael@0: this->setPixelRef(pr)->unref(); michael@0: michael@0: // since we're already allocated, we lockPixels right away michael@0: this->lockPixels(); michael@0: SkDEBUGCODE(this->validate();) michael@0: } michael@0: michael@0: bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { michael@0: HeapAllocator stdalloc; michael@0: michael@0: if (NULL == allocator) { michael@0: allocator = &stdalloc; michael@0: } michael@0: return allocator->allocPixelRef(this, ctable); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkBitmap::allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory, michael@0: SkColorTable* ctable) { michael@0: if (kIndex_8_SkColorType == info.fColorType && NULL == ctable) { michael@0: return reset_return_false(this); michael@0: } michael@0: if (!this->setConfig(info)) { michael@0: return reset_return_false(this); michael@0: } michael@0: michael@0: SkMallocPixelRef::PRFactory defaultFactory; michael@0: if (NULL == factory) { michael@0: factory = &defaultFactory; michael@0: } michael@0: michael@0: SkPixelRef* pr = factory->create(info, ctable); michael@0: if (NULL == pr) { michael@0: return reset_return_false(this); michael@0: } michael@0: this->setPixelRef(pr)->unref(); michael@0: michael@0: // TODO: lockPixels could/should return bool or void*/NULL michael@0: this->lockPixels(); michael@0: if (NULL == this->getPixels()) { michael@0: return reset_return_false(this); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkBitmap::installPixels(const SkImageInfo& info, void* pixels, size_t rb, michael@0: void (*releaseProc)(void* addr, void* context), michael@0: void* context) { michael@0: if (!this->setConfig(info, rb)) { michael@0: this->reset(); michael@0: return false; michael@0: } michael@0: michael@0: SkPixelRef* pr = SkMallocPixelRef::NewWithProc(info, rb, NULL, pixels, michael@0: releaseProc, context); michael@0: if (!pr) { michael@0: this->reset(); michael@0: return false; michael@0: } michael@0: michael@0: this->setPixelRef(pr)->unref(); michael@0: michael@0: // since we're already allocated, we lockPixels right away michael@0: this->lockPixels(); michael@0: SkDEBUGCODE(this->validate();) michael@0: return true; michael@0: } michael@0: michael@0: bool SkBitmap::installMaskPixels(const SkMask& mask) { michael@0: if (SkMask::kA8_Format != mask.fFormat) { michael@0: this->reset(); michael@0: return false; michael@0: } michael@0: return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), michael@0: mask.fBounds.height()), michael@0: mask.fImage, mask.fRowBytes); michael@0: } michael@0: michael@0: bool SkBitmap::allocConfigPixels(Config config, int width, int height, michael@0: bool isOpaque) { michael@0: SkColorType ct; michael@0: if (!config_to_colorType(config, &ct)) { michael@0: return false; michael@0: } michael@0: michael@0: SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; michael@0: return this->allocPixels(SkImageInfo::Make(width, height, ct, at)); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkBitmap::freePixels() { michael@0: // if we're gonna free the pixels, we certainly need to free the mipmap michael@0: this->freeMipMap(); michael@0: michael@0: if (NULL != fPixelRef) { michael@0: if (fPixelLockCount > 0) { michael@0: fPixelRef->unlockPixels(); michael@0: } michael@0: fPixelRef->unref(); michael@0: fPixelRef = NULL; michael@0: fPixelRefOrigin.setZero(); michael@0: } michael@0: fPixelLockCount = 0; michael@0: fPixels = NULL; michael@0: fColorTable = NULL; michael@0: } michael@0: michael@0: void SkBitmap::freeMipMap() { michael@0: if (fMipMap) { michael@0: fMipMap->unref(); michael@0: fMipMap = NULL; michael@0: } michael@0: } michael@0: michael@0: uint32_t SkBitmap::getGenerationID() const { michael@0: return (fPixelRef) ? fPixelRef->getGenerationID() : 0; michael@0: } michael@0: michael@0: void SkBitmap::notifyPixelsChanged() const { michael@0: SkASSERT(!this->isImmutable()); michael@0: if (fPixelRef) { michael@0: fPixelRef->notifyPixelsChanged(); michael@0: } michael@0: } michael@0: michael@0: GrTexture* SkBitmap::getTexture() const { michael@0: return fPixelRef ? fPixelRef->getTexture() : NULL; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** We explicitly use the same allocator for our pixels that SkMask does, michael@0: so that we can freely assign memory allocated by one class to the other. michael@0: */ michael@0: bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst, michael@0: SkColorTable* ctable) { michael@0: SkImageInfo info; michael@0: if (!dst->asImageInfo(&info)) { michael@0: // SkDebugf("unsupported config for info %d\n", dst->config()); michael@0: return false; michael@0: } michael@0: michael@0: SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), michael@0: ctable); michael@0: if (NULL == pr) { michael@0: return false; michael@0: } michael@0: michael@0: dst->setPixelRef(pr)->unref(); michael@0: // since we're already allocated, we lockPixels right away michael@0: dst->lockPixels(); michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize, michael@0: size_t dstRowBytes, bool preserveDstPad) const { michael@0: michael@0: if (0 == dstRowBytes) { michael@0: dstRowBytes = fRowBytes; michael@0: } michael@0: michael@0: if (dstRowBytes < fInfo.minRowBytes() || michael@0: dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) { michael@0: return false; michael@0: } michael@0: michael@0: if (!preserveDstPad && static_cast(dstRowBytes) == fRowBytes) { michael@0: size_t safeSize = this->getSafeSize(); michael@0: if (safeSize > dstSize || safeSize == 0) michael@0: return false; michael@0: else { michael@0: SkAutoLockPixels lock(*this); michael@0: // This implementation will write bytes beyond the end of each row, michael@0: // excluding the last row, if the bitmap's stride is greater than michael@0: // strictly required by the current config. michael@0: memcpy(dst, getPixels(), safeSize); michael@0: michael@0: return true; michael@0: } michael@0: } else { michael@0: // If destination has different stride than us, then copy line by line. michael@0: if (fInfo.getSafeSize(dstRowBytes) > dstSize) { michael@0: return false; michael@0: } else { michael@0: // Just copy what we need on each line. michael@0: size_t rowBytes = fInfo.minRowBytes(); michael@0: SkAutoLockPixels lock(*this); michael@0: const uint8_t* srcP = reinterpret_cast(getPixels()); michael@0: uint8_t* dstP = reinterpret_cast(dst); michael@0: for (int row = 0; row < fInfo.fHeight; michael@0: row++, srcP += fRowBytes, dstP += dstRowBytes) { michael@0: memcpy(dstP, srcP, rowBytes); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkBitmap::isImmutable() const { michael@0: return fPixelRef ? fPixelRef->isImmutable() : michael@0: fFlags & kImageIsImmutable_Flag; michael@0: } michael@0: michael@0: void SkBitmap::setImmutable() { michael@0: if (fPixelRef) { michael@0: fPixelRef->setImmutable(); michael@0: } else { michael@0: fFlags |= kImageIsImmutable_Flag; michael@0: } michael@0: } michael@0: michael@0: bool SkBitmap::isVolatile() const { michael@0: return (fFlags & kImageIsVolatile_Flag) != 0; michael@0: } michael@0: michael@0: void SkBitmap::setIsVolatile(bool isVolatile) { michael@0: if (isVolatile) { michael@0: fFlags |= kImageIsVolatile_Flag; michael@0: } else { michael@0: fFlags &= ~kImageIsVolatile_Flag; michael@0: } michael@0: } michael@0: michael@0: void* SkBitmap::getAddr(int x, int y) const { michael@0: SkASSERT((unsigned)x < (unsigned)this->width()); michael@0: SkASSERT((unsigned)y < (unsigned)this->height()); michael@0: michael@0: char* base = (char*)this->getPixels(); michael@0: if (base) { michael@0: base += y * this->rowBytes(); michael@0: switch (this->colorType()) { michael@0: case kRGBA_8888_SkColorType: michael@0: case kBGRA_8888_SkColorType: michael@0: base += x << 2; michael@0: break; michael@0: case kARGB_4444_SkColorType: michael@0: case kRGB_565_SkColorType: michael@0: base += x << 1; michael@0: break; michael@0: case kAlpha_8_SkColorType: michael@0: case kIndex_8_SkColorType: michael@0: base += x; michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("Can't return addr for config"); michael@0: base = NULL; michael@0: break; michael@0: } michael@0: } michael@0: return base; michael@0: } michael@0: michael@0: SkColor SkBitmap::getColor(int x, int y) const { michael@0: SkASSERT((unsigned)x < (unsigned)this->width()); michael@0: SkASSERT((unsigned)y < (unsigned)this->height()); michael@0: michael@0: switch (this->config()) { michael@0: case SkBitmap::kA8_Config: { michael@0: uint8_t* addr = this->getAddr8(x, y); michael@0: return SkColorSetA(0, addr[0]); michael@0: } michael@0: case SkBitmap::kIndex8_Config: { michael@0: SkPMColor c = this->getIndex8Color(x, y); michael@0: return SkUnPreMultiply::PMColorToColor(c); michael@0: } michael@0: case SkBitmap::kRGB_565_Config: { michael@0: uint16_t* addr = this->getAddr16(x, y); michael@0: return SkPixel16ToColor(addr[0]); michael@0: } michael@0: case SkBitmap::kARGB_4444_Config: { michael@0: uint16_t* addr = this->getAddr16(x, y); michael@0: SkPMColor c = SkPixel4444ToPixel32(addr[0]); michael@0: return SkUnPreMultiply::PMColorToColor(c); michael@0: } michael@0: case SkBitmap::kARGB_8888_Config: { michael@0: uint32_t* addr = this->getAddr32(x, y); michael@0: return SkUnPreMultiply::PMColorToColor(addr[0]); michael@0: } michael@0: case kNo_Config: michael@0: default: michael@0: SkASSERT(false); michael@0: return 0; michael@0: } michael@0: SkASSERT(false); // Not reached. michael@0: return 0; michael@0: } michael@0: michael@0: bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) { michael@0: SkAutoLockPixels alp(bm); michael@0: if (!bm.getPixels()) { michael@0: return false; michael@0: } michael@0: michael@0: const int height = bm.height(); michael@0: const int width = bm.width(); michael@0: michael@0: switch (bm.config()) { michael@0: case SkBitmap::kA8_Config: { michael@0: unsigned a = 0xFF; michael@0: for (int y = 0; y < height; ++y) { michael@0: const uint8_t* row = bm.getAddr8(0, y); michael@0: for (int x = 0; x < width; ++x) { michael@0: a &= row[x]; michael@0: } michael@0: if (0xFF != a) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } break; michael@0: case SkBitmap::kIndex8_Config: { michael@0: SkAutoLockColors alc(bm); michael@0: const SkPMColor* table = alc.colors(); michael@0: if (!table) { michael@0: return false; michael@0: } michael@0: SkPMColor c = (SkPMColor)~0; michael@0: for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) { michael@0: c &= table[i]; michael@0: } michael@0: return 0xFF == SkGetPackedA32(c); michael@0: } break; michael@0: case SkBitmap::kRGB_565_Config: michael@0: return true; michael@0: break; michael@0: case SkBitmap::kARGB_4444_Config: { michael@0: unsigned c = 0xFFFF; michael@0: for (int y = 0; y < height; ++y) { michael@0: const SkPMColor16* row = bm.getAddr16(0, y); michael@0: for (int x = 0; x < width; ++x) { michael@0: c &= row[x]; michael@0: } michael@0: if (0xF != SkGetPackedA4444(c)) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } break; michael@0: case SkBitmap::kARGB_8888_Config: { michael@0: SkPMColor c = (SkPMColor)~0; michael@0: for (int y = 0; y < height; ++y) { michael@0: const SkPMColor* row = bm.getAddr32(0, y); michael@0: for (int x = 0; x < width; ++x) { michael@0: c &= row[x]; michael@0: } michael@0: if (0xFF != SkGetPackedA32(c)) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: default: michael@0: break; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) { michael@0: unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) | michael@0: (SkR32To4444(r) << SK_R4444_SHIFT) | michael@0: (SkG32To4444(g) << SK_G4444_SHIFT) | michael@0: (SkB32To4444(b) << SK_B4444_SHIFT); michael@0: return SkToU16(pixel); michael@0: } michael@0: michael@0: void SkBitmap::internalErase(const SkIRect& area, michael@0: U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { michael@0: #ifdef SK_DEBUG michael@0: SkDEBUGCODE(this->validate();) michael@0: SkASSERT(!area.isEmpty()); michael@0: { michael@0: SkIRect total = { 0, 0, this->width(), this->height() }; michael@0: SkASSERT(total.contains(area)); michael@0: } michael@0: #endif michael@0: michael@0: switch (fInfo.colorType()) { michael@0: case kUnknown_SkColorType: michael@0: case kIndex_8_SkColorType: michael@0: return; // can't erase michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: SkAutoLockPixels alp(*this); michael@0: // perform this check after the lock call michael@0: if (!this->readyToDraw()) { michael@0: return; michael@0: } michael@0: michael@0: int height = area.height(); michael@0: const int width = area.width(); michael@0: const int rowBytes = fRowBytes; michael@0: michael@0: // make rgb premultiplied michael@0: if (255 != a) { michael@0: r = SkAlphaMul(r, a); michael@0: g = SkAlphaMul(g, a); michael@0: b = SkAlphaMul(b, a); michael@0: } michael@0: michael@0: switch (this->colorType()) { michael@0: case kAlpha_8_SkColorType: { michael@0: uint8_t* p = this->getAddr8(area.fLeft, area.fTop); michael@0: while (--height >= 0) { michael@0: memset(p, a, width); michael@0: p += rowBytes; michael@0: } michael@0: break; michael@0: } michael@0: case kARGB_4444_SkColorType: michael@0: case kRGB_565_SkColorType: { michael@0: uint16_t* p = this->getAddr16(area.fLeft, area.fTop);; michael@0: uint16_t v; michael@0: michael@0: if (kARGB_4444_SkColorType == this->colorType()) { michael@0: v = pack_8888_to_4444(a, r, g, b); michael@0: } else { michael@0: v = SkPackRGB16(r >> (8 - SK_R16_BITS), michael@0: g >> (8 - SK_G16_BITS), michael@0: b >> (8 - SK_B16_BITS)); michael@0: } michael@0: while (--height >= 0) { michael@0: sk_memset16(p, v, width); michael@0: p = (uint16_t*)((char*)p + rowBytes); michael@0: } michael@0: break; michael@0: } michael@0: case kPMColor_SkColorType: { michael@0: // what to do about BGRA or RGBA (which ever is != PMColor ? michael@0: // for now we don't support them. michael@0: uint32_t* p = this->getAddr32(area.fLeft, area.fTop); michael@0: uint32_t v = SkPackARGB32(a, r, g, b); michael@0: michael@0: while (--height >= 0) { michael@0: sk_memset32(p, v, width); michael@0: p = (uint32_t*)((char*)p + rowBytes); michael@0: } michael@0: break; michael@0: } michael@0: default: michael@0: return; // no change, so don't call notifyPixelsChanged() michael@0: } michael@0: michael@0: this->notifyPixelsChanged(); michael@0: } michael@0: michael@0: void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { michael@0: SkIRect area = { 0, 0, this->width(), this->height() }; michael@0: if (!area.isEmpty()) { michael@0: this->internalErase(area, a, r, g, b); michael@0: } michael@0: } michael@0: michael@0: void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const { michael@0: SkIRect area = { 0, 0, this->width(), this->height() }; michael@0: if (area.intersect(rect)) { michael@0: this->internalErase(area, SkColorGetA(c), SkColorGetR(c), michael@0: SkColorGetG(c), SkColorGetB(c)); michael@0: } michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////////////// michael@0: ////////////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: if (NULL == result || NULL == fPixelRef) { michael@0: return false; // no src pixels michael@0: } michael@0: michael@0: SkIRect srcRect, r; michael@0: srcRect.set(0, 0, this->width(), this->height()); michael@0: if (!r.intersect(srcRect, subset)) { michael@0: return false; // r is empty (i.e. no intersection) michael@0: } michael@0: michael@0: if (fPixelRef->getTexture() != NULL) { michael@0: // Do a deep copy michael@0: SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset); michael@0: if (pixelRef != NULL) { michael@0: SkBitmap dst; michael@0: dst.setConfig(this->config(), subset.width(), subset.height(), 0, michael@0: this->alphaType()); michael@0: dst.setIsVolatile(this->isVolatile()); michael@0: dst.setPixelRef(pixelRef)->unref(); michael@0: SkDEBUGCODE(dst.validate()); michael@0: result->swap(dst); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have michael@0: // exited above. michael@0: SkASSERT(static_cast(r.fLeft) < static_cast(this->width())); michael@0: SkASSERT(static_cast(r.fTop) < static_cast(this->height())); michael@0: michael@0: SkBitmap dst; michael@0: dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(), michael@0: this->alphaType()); michael@0: dst.setIsVolatile(this->isVolatile()); michael@0: michael@0: if (fPixelRef) { michael@0: SkIPoint origin = fPixelRefOrigin; michael@0: origin.fX += r.fLeft; michael@0: origin.fY += r.fTop; michael@0: // share the pixelref with a custom offset michael@0: dst.setPixelRef(fPixelRef, origin); michael@0: } michael@0: SkDEBUGCODE(dst.validate();) michael@0: michael@0: // we know we're good, so commit to result michael@0: result->swap(dst); michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkCanvas.h" michael@0: #include "SkPaint.h" michael@0: michael@0: bool SkBitmap::canCopyTo(SkColorType dstColorType) const { michael@0: if (this->colorType() == kUnknown_SkColorType) { michael@0: return false; michael@0: } michael@0: michael@0: bool sameConfigs = (this->colorType() == dstColorType); michael@0: switch (dstColorType) { michael@0: case kAlpha_8_SkColorType: michael@0: case kRGB_565_SkColorType: michael@0: case kPMColor_SkColorType: michael@0: break; michael@0: case kIndex_8_SkColorType: michael@0: if (!sameConfigs) { michael@0: return false; michael@0: } michael@0: break; michael@0: case kARGB_4444_SkColorType: michael@0: return sameConfigs || kPMColor_SkColorType == this->colorType(); michael@0: default: michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, michael@0: Allocator* alloc) const { michael@0: if (!this->canCopyTo(dstColorType)) { michael@0: return false; michael@0: } michael@0: michael@0: // if we have a texture, first get those pixels michael@0: SkBitmap tmpSrc; michael@0: const SkBitmap* src = this; michael@0: michael@0: if (fPixelRef) { michael@0: SkIRect subset; michael@0: subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY, michael@0: fInfo.width(), fInfo.height()); michael@0: if (fPixelRef->readPixels(&tmpSrc, &subset)) { michael@0: SkASSERT(tmpSrc.width() == this->width()); michael@0: SkASSERT(tmpSrc.height() == this->height()); michael@0: michael@0: // did we get lucky and we can just return tmpSrc? michael@0: if (tmpSrc.colorType() == dstColorType && NULL == alloc) { michael@0: dst->swap(tmpSrc); michael@0: // If the result is an exact copy, clone the gen ID. michael@0: if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) { michael@0: dst->pixelRef()->cloneGenID(*fPixelRef); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // fall through to the raster case michael@0: src = &tmpSrc; michael@0: } michael@0: } michael@0: michael@0: // we lock this now, since we may need its colortable michael@0: SkAutoLockPixels srclock(*src); michael@0: if (!src->readyToDraw()) { michael@0: return false; michael@0: } michael@0: michael@0: // The only way to be readyToDraw is if fPixelRef is non NULL. michael@0: SkASSERT(fPixelRef != NULL); michael@0: michael@0: SkImageInfo dstInfo = src->info(); michael@0: dstInfo.fColorType = dstColorType; michael@0: michael@0: SkBitmap tmpDst; michael@0: if (!tmpDst.setConfig(dstInfo)) { michael@0: return false; michael@0: } michael@0: michael@0: // allocate colortable if srcConfig == kIndex8_Config michael@0: SkAutoTUnref ctable; michael@0: if (dstColorType == kIndex_8_SkColorType) { michael@0: // TODO: can we just ref() the src colortable? Is it reentrant-safe? michael@0: ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable()))); michael@0: } michael@0: if (!tmpDst.allocPixels(alloc, ctable)) { michael@0: return false; michael@0: } michael@0: michael@0: if (!tmpDst.readyToDraw()) { michael@0: // allocator/lock failed michael@0: return false; michael@0: } michael@0: michael@0: // pixelRef must be non NULL or tmpDst.readyToDraw() would have michael@0: // returned false. michael@0: SkASSERT(tmpDst.pixelRef() != NULL); michael@0: michael@0: /* do memcpy for the same configs cases, else use drawing michael@0: */ michael@0: if (src->colorType() == dstColorType) { michael@0: if (tmpDst.getSize() == src->getSize()) { michael@0: memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize()); michael@0: SkPixelRef* pixelRef = tmpDst.pixelRef(); michael@0: michael@0: // In order to reach this point, we know that the width, config and michael@0: // rowbytes of the SkPixelRefs are the same, but it is possible for michael@0: // the heights to differ, if this SkBitmap's height is a subset of michael@0: // fPixelRef. Only if the SkPixelRefs' heights match are we michael@0: // guaranteed that this is an exact copy, meaning we should clone michael@0: // the genID. michael@0: if (pixelRef->info().fHeight == fPixelRef->info().fHeight) { michael@0: // TODO: what to do if the two infos match, BUT michael@0: // fPixelRef is premul and pixelRef is opaque? michael@0: // skipping assert for now michael@0: // https://code.google.com/p/skia/issues/detail?id=2012 michael@0: // SkASSERT(pixelRef->info() == fPixelRef->info()); michael@0: SkASSERT(pixelRef->info().fWidth == fPixelRef->info().fWidth); michael@0: SkASSERT(pixelRef->info().fColorType == fPixelRef->info().fColorType); michael@0: pixelRef->cloneGenID(*fPixelRef); michael@0: } michael@0: } else { michael@0: const char* srcP = reinterpret_cast(src->getPixels()); michael@0: char* dstP = reinterpret_cast(tmpDst.getPixels()); michael@0: // to be sure we don't read too much, only copy our logical pixels michael@0: size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel(); michael@0: for (int y = 0; y < tmpDst.height(); y++) { michael@0: memcpy(dstP, srcP, bytesToCopy); michael@0: srcP += src->rowBytes(); michael@0: dstP += tmpDst.rowBytes(); michael@0: } michael@0: } michael@0: } else if (kARGB_4444_SkColorType == dstColorType michael@0: && kPMColor_SkColorType == src->colorType()) { michael@0: SkASSERT(src->height() == tmpDst.height()); michael@0: SkASSERT(src->width() == tmpDst.width()); michael@0: for (int y = 0; y < src->height(); ++y) { michael@0: SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y); michael@0: SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y); michael@0: DITHER_4444_SCAN(y); michael@0: for (int x = 0; x < src->width(); ++x) { michael@0: dstRow[x] = SkDitherARGB32To4444(srcRow[x], michael@0: DITHER_VALUE(x)); michael@0: } michael@0: } michael@0: } else { michael@0: // Always clear the dest in case one of the blitters accesses it michael@0: // TODO: switch the allocation of tmpDst to call sk_calloc_throw michael@0: tmpDst.eraseColor(SK_ColorTRANSPARENT); michael@0: michael@0: SkCanvas canvas(tmpDst); michael@0: SkPaint paint; michael@0: michael@0: paint.setDither(true); michael@0: canvas.drawBitmap(*src, 0, 0, &paint); michael@0: } michael@0: michael@0: dst->swap(tmpDst); michael@0: return true; michael@0: } michael@0: michael@0: bool SkBitmap::deepCopyTo(SkBitmap* dst) const { michael@0: const SkBitmap::Config dstConfig = this->config(); michael@0: const SkColorType dstCT = SkBitmapConfigToColorType(dstConfig); michael@0: michael@0: if (!this->canCopyTo(dstCT)) { michael@0: return false; michael@0: } michael@0: michael@0: // If we have a PixelRef, and it supports deep copy, use it. michael@0: // Currently supported only by texture-backed bitmaps. michael@0: if (fPixelRef) { michael@0: SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig); michael@0: if (pixelRef) { michael@0: uint32_t rowBytes; michael@0: if (this->colorType() == dstCT) { michael@0: // Since there is no subset to pass to deepCopy, and deepCopy michael@0: // succeeded, the new pixel ref must be identical. michael@0: SkASSERT(fPixelRef->info() == pixelRef->info()); michael@0: pixelRef->cloneGenID(*fPixelRef); michael@0: // Use the same rowBytes as the original. michael@0: rowBytes = fRowBytes; michael@0: } else { michael@0: // With the new config, an appropriate fRowBytes will be computed by setConfig. michael@0: rowBytes = 0; michael@0: } michael@0: michael@0: SkImageInfo info = fInfo; michael@0: info.fColorType = dstCT; michael@0: if (!dst->setConfig(info, rowBytes)) { michael@0: return false; michael@0: } michael@0: dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref(); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: if (this->getTexture()) { michael@0: return false; michael@0: } else { michael@0: return this->copyTo(dst, dstCT, NULL); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static void downsampleby2_proc32(SkBitmap* dst, int x, int y, michael@0: const SkBitmap& src) { michael@0: x <<= 1; michael@0: y <<= 1; michael@0: const SkPMColor* p = src.getAddr32(x, y); michael@0: const SkPMColor* baseP = p; michael@0: SkPMColor c, ag, rb; michael@0: michael@0: c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF; michael@0: if (x < src.width() - 1) { michael@0: p += 1; michael@0: } michael@0: c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; michael@0: michael@0: p = baseP; michael@0: if (y < src.height() - 1) { michael@0: p += src.rowBytes() >> 2; michael@0: } michael@0: c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; michael@0: if (x < src.width() - 1) { michael@0: p += 1; michael@0: } michael@0: c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; michael@0: michael@0: *dst->getAddr32(x >> 1, y >> 1) = michael@0: ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); michael@0: } michael@0: michael@0: static inline uint32_t expand16(U16CPU c) { michael@0: return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16); michael@0: } michael@0: michael@0: // returns dirt in the top 16bits, but we don't care, since we only michael@0: // store the low 16bits. michael@0: static inline U16CPU pack16(uint32_t c) { michael@0: return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE); michael@0: } michael@0: michael@0: static void downsampleby2_proc16(SkBitmap* dst, int x, int y, michael@0: const SkBitmap& src) { michael@0: x <<= 1; michael@0: y <<= 1; michael@0: const uint16_t* p = src.getAddr16(x, y); michael@0: const uint16_t* baseP = p; michael@0: SkPMColor c; michael@0: michael@0: c = expand16(*p); michael@0: if (x < src.width() - 1) { michael@0: p += 1; michael@0: } michael@0: c += expand16(*p); michael@0: michael@0: p = baseP; michael@0: if (y < src.height() - 1) { michael@0: p += src.rowBytes() >> 1; michael@0: } michael@0: c += expand16(*p); michael@0: if (x < src.width() - 1) { michael@0: p += 1; michael@0: } michael@0: c += expand16(*p); michael@0: michael@0: *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2); michael@0: } michael@0: michael@0: static uint32_t expand4444(U16CPU c) { michael@0: return (c & 0xF0F) | ((c & ~0xF0F) << 12); michael@0: } michael@0: michael@0: static U16CPU collaps4444(uint32_t c) { michael@0: return (c & 0xF0F) | ((c >> 12) & ~0xF0F); michael@0: } michael@0: michael@0: static void downsampleby2_proc4444(SkBitmap* dst, int x, int y, michael@0: const SkBitmap& src) { michael@0: x <<= 1; michael@0: y <<= 1; michael@0: const uint16_t* p = src.getAddr16(x, y); michael@0: const uint16_t* baseP = p; michael@0: uint32_t c; michael@0: michael@0: c = expand4444(*p); michael@0: if (x < src.width() - 1) { michael@0: p += 1; michael@0: } michael@0: c += expand4444(*p); michael@0: michael@0: p = baseP; michael@0: if (y < src.height() - 1) { michael@0: p += src.rowBytes() >> 1; michael@0: } michael@0: c += expand4444(*p); michael@0: if (x < src.width() - 1) { michael@0: p += 1; michael@0: } michael@0: c += expand4444(*p); michael@0: michael@0: *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2); michael@0: } michael@0: michael@0: void SkBitmap::buildMipMap(bool forceRebuild) { michael@0: if (forceRebuild) michael@0: this->freeMipMap(); michael@0: else if (fMipMap) michael@0: return; // we're already built michael@0: michael@0: SkASSERT(NULL == fMipMap); michael@0: michael@0: void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); michael@0: michael@0: const SkBitmap::Config config = this->config(); michael@0: michael@0: switch (config) { michael@0: case kARGB_8888_Config: michael@0: proc = downsampleby2_proc32; michael@0: break; michael@0: case kRGB_565_Config: michael@0: proc = downsampleby2_proc16; michael@0: break; michael@0: case kARGB_4444_Config: michael@0: proc = downsampleby2_proc4444; michael@0: break; michael@0: case kIndex8_Config: michael@0: case kA8_Config: michael@0: default: michael@0: return; // don't build mipmaps for these configs michael@0: } michael@0: michael@0: SkAutoLockPixels alp(*this); michael@0: if (!this->readyToDraw()) { michael@0: return; michael@0: } michael@0: michael@0: // whip through our loop to compute the exact size needed michael@0: size_t size = 0; michael@0: int maxLevels = 0; michael@0: { michael@0: int width = this->width(); michael@0: int height = this->height(); michael@0: for (;;) { michael@0: width >>= 1; michael@0: height >>= 1; michael@0: if (0 == width || 0 == height) { michael@0: break; michael@0: } michael@0: size += ComputeRowBytes(config, width) * height; michael@0: maxLevels += 1; michael@0: } michael@0: } michael@0: michael@0: // nothing to build michael@0: if (0 == maxLevels) { michael@0: return; michael@0: } michael@0: michael@0: SkBitmap srcBM(*this); michael@0: srcBM.lockPixels(); michael@0: if (!srcBM.readyToDraw()) { michael@0: return; michael@0: } michael@0: michael@0: MipMap* mm = MipMap::Alloc(maxLevels, size); michael@0: if (NULL == mm) { michael@0: return; michael@0: } michael@0: michael@0: MipLevel* level = mm->levels(); michael@0: uint8_t* addr = (uint8_t*)mm->pixels(); michael@0: int width = this->width(); michael@0: int height = this->height(); michael@0: uint32_t rowBytes; michael@0: SkBitmap dstBM; michael@0: michael@0: for (int i = 0; i < maxLevels; i++) { michael@0: width >>= 1; michael@0: height >>= 1; michael@0: rowBytes = SkToU32(ComputeRowBytes(config, width)); michael@0: michael@0: level[i].fPixels = addr; michael@0: level[i].fWidth = width; michael@0: level[i].fHeight = height; michael@0: level[i].fRowBytes = rowBytes; michael@0: michael@0: dstBM.setConfig(config, width, height, rowBytes); michael@0: dstBM.setPixels(addr); michael@0: michael@0: srcBM.lockPixels(); michael@0: for (int y = 0; y < height; y++) { michael@0: for (int x = 0; x < width; x++) { michael@0: proc(&dstBM, x, y, srcBM); michael@0: } michael@0: } michael@0: srcBM.unlockPixels(); michael@0: michael@0: srcBM = dstBM; michael@0: addr += height * rowBytes; michael@0: } michael@0: SkASSERT(addr == (uint8_t*)mm->pixels() + size); michael@0: fMipMap = mm; michael@0: } michael@0: michael@0: bool SkBitmap::hasMipMap() const { michael@0: return fMipMap != NULL; michael@0: } michael@0: michael@0: int SkBitmap::extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy) { michael@0: if (NULL == fMipMap) { michael@0: return 0; michael@0: } michael@0: michael@0: int level = ComputeMipLevel(sx, sy) >> 16; michael@0: SkASSERT(level >= 0); michael@0: if (level <= 0) { michael@0: return 0; michael@0: } michael@0: michael@0: if (level >= fMipMap->fLevelCount) { michael@0: level = fMipMap->fLevelCount - 1; michael@0: } michael@0: if (dst) { michael@0: const MipLevel& mip = fMipMap->levels()[level - 1]; michael@0: dst->setConfig((SkBitmap::Config)this->config(), michael@0: mip.fWidth, mip.fHeight, mip.fRowBytes); michael@0: dst->setPixels(mip.fPixels); michael@0: } michael@0: return level; michael@0: } michael@0: michael@0: SkFixed SkBitmap::ComputeMipLevel(SkFixed sx, SkFixed sy) { michael@0: sx = SkAbs32(sx); michael@0: sy = SkAbs32(sy); michael@0: if (sx < sy) { michael@0: sx = sy; michael@0: } michael@0: if (sx < SK_Fixed1) { michael@0: return 0; michael@0: } michael@0: int clz = SkCLZ(sx); michael@0: SkASSERT(clz >= 1 && clz <= 15); michael@0: return SkIntToFixed(15 - clz) + ((unsigned)(sx << (clz + 1)) >> 16); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, michael@0: int alphaRowBytes) { michael@0: SkASSERT(alpha != NULL); michael@0: SkASSERT(alphaRowBytes >= src.width()); michael@0: michael@0: SkBitmap::Config config = src.config(); michael@0: int w = src.width(); michael@0: int h = src.height(); michael@0: size_t rb = src.rowBytes(); michael@0: michael@0: SkAutoLockPixels alp(src); michael@0: if (!src.readyToDraw()) { michael@0: // zero out the alpha buffer and return michael@0: while (--h >= 0) { michael@0: memset(alpha, 0, w); michael@0: alpha += alphaRowBytes; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: if (SkBitmap::kA8_Config == config && !src.isOpaque()) { michael@0: const uint8_t* s = src.getAddr8(0, 0); michael@0: while (--h >= 0) { michael@0: memcpy(alpha, s, w); michael@0: s += rb; michael@0: alpha += alphaRowBytes; michael@0: } michael@0: } else if (SkBitmap::kARGB_8888_Config == config && !src.isOpaque()) { michael@0: const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0); michael@0: while (--h >= 0) { michael@0: for (int x = 0; x < w; x++) { michael@0: alpha[x] = SkGetPackedA32(s[x]); michael@0: } michael@0: s = (const SkPMColor*)((const char*)s + rb); michael@0: alpha += alphaRowBytes; michael@0: } michael@0: } else if (SkBitmap::kARGB_4444_Config == config && !src.isOpaque()) { michael@0: const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0); michael@0: while (--h >= 0) { michael@0: for (int x = 0; x < w; x++) { michael@0: alpha[x] = SkPacked4444ToA32(s[x]); michael@0: } michael@0: s = (const SkPMColor16*)((const char*)s + rb); michael@0: alpha += alphaRowBytes; michael@0: } michael@0: } else if (SkBitmap::kIndex8_Config == config && !src.isOpaque()) { michael@0: SkColorTable* ct = src.getColorTable(); michael@0: if (ct) { michael@0: const SkPMColor* SK_RESTRICT table = ct->lockColors(); michael@0: const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0); michael@0: while (--h >= 0) { michael@0: for (int x = 0; x < w; x++) { michael@0: alpha[x] = SkGetPackedA32(table[s[x]]); michael@0: } michael@0: s += rb; michael@0: alpha += alphaRowBytes; michael@0: } michael@0: ct->unlockColors(); michael@0: } michael@0: } else { // src is opaque, so just fill alpha[] with 0xFF michael@0: memset(alpha, 0xFF, h * alphaRowBytes); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: #include "SkPaint.h" michael@0: #include "SkMaskFilter.h" michael@0: #include "SkMatrix.h" michael@0: michael@0: bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, michael@0: Allocator *allocator, SkIPoint* offset) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: SkBitmap tmpBitmap; michael@0: SkMatrix identity; michael@0: SkMask srcM, dstM; michael@0: michael@0: srcM.fBounds.set(0, 0, this->width(), this->height()); michael@0: srcM.fRowBytes = SkAlign4(this->width()); michael@0: srcM.fFormat = SkMask::kA8_Format; michael@0: michael@0: SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL; michael@0: michael@0: // compute our (larger?) dst bounds if we have a filter michael@0: if (NULL != filter) { michael@0: identity.reset(); michael@0: srcM.fImage = NULL; michael@0: if (!filter->filterMask(&dstM, srcM, identity, NULL)) { michael@0: goto NO_FILTER_CASE; michael@0: } michael@0: dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); michael@0: } else { michael@0: NO_FILTER_CASE: michael@0: tmpBitmap.setConfig(SkBitmap::kA8_Config, this->width(), this->height(), michael@0: srcM.fRowBytes); michael@0: if (!tmpBitmap.allocPixels(allocator, NULL)) { michael@0: // Allocation of pixels for alpha bitmap failed. michael@0: SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", michael@0: tmpBitmap.width(), tmpBitmap.height()); michael@0: return false; michael@0: } michael@0: GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); michael@0: if (offset) { michael@0: offset->set(0, 0); michael@0: } michael@0: tmpBitmap.swap(*dst); michael@0: return true; michael@0: } michael@0: srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); michael@0: SkAutoMaskFreeImage srcCleanup(srcM.fImage); michael@0: michael@0: GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); michael@0: if (!filter->filterMask(&dstM, srcM, identity, NULL)) { michael@0: goto NO_FILTER_CASE; michael@0: } michael@0: SkAutoMaskFreeImage dstCleanup(dstM.fImage); michael@0: michael@0: tmpBitmap.setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(), michael@0: dstM.fBounds.height(), dstM.fRowBytes); michael@0: if (!tmpBitmap.allocPixels(allocator, NULL)) { michael@0: // Allocation of pixels for alpha bitmap failed. michael@0: SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", michael@0: tmpBitmap.width(), tmpBitmap.height()); michael@0: return false; michael@0: } michael@0: memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); michael@0: if (offset) { michael@0: offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); michael@0: } michael@0: SkDEBUGCODE(tmpBitmap.validate();) michael@0: michael@0: tmpBitmap.swap(*dst); michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: enum { michael@0: SERIALIZE_PIXELTYPE_NONE, michael@0: SERIALIZE_PIXELTYPE_REF_DATA michael@0: }; michael@0: michael@0: void SkBitmap::flatten(SkWriteBuffer& buffer) const { michael@0: fInfo.flatten(buffer); michael@0: buffer.writeInt(fRowBytes); michael@0: michael@0: if (fPixelRef) { michael@0: if (fPixelRef->getFactory()) { michael@0: buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA); michael@0: buffer.writeInt(fPixelRefOrigin.fX); michael@0: buffer.writeInt(fPixelRefOrigin.fY); michael@0: buffer.writeFlattenable(fPixelRef); michael@0: return; michael@0: } michael@0: // if we get here, we can't record the pixels michael@0: buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); michael@0: } else { michael@0: buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); michael@0: } michael@0: } michael@0: michael@0: void SkBitmap::unflatten(SkReadBuffer& buffer) { michael@0: this->reset(); michael@0: michael@0: SkImageInfo info; michael@0: info.unflatten(buffer); michael@0: size_t rowBytes = buffer.readInt(); michael@0: if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && michael@0: SkColorTypeIsValid(info.fColorType) && michael@0: SkAlphaTypeIsValid(info.fAlphaType) && michael@0: validate_alphaType(info.fColorType, info.fAlphaType) && michael@0: info.validRowBytes(rowBytes))) { michael@0: return; michael@0: } michael@0: michael@0: bool configIsValid = this->setConfig(info, rowBytes); michael@0: buffer.validate(configIsValid); michael@0: michael@0: int reftype = buffer.readInt(); michael@0: if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) || michael@0: (SERIALIZE_PIXELTYPE_NONE == reftype))) { michael@0: switch (reftype) { michael@0: case SERIALIZE_PIXELTYPE_REF_DATA: { michael@0: SkIPoint origin; michael@0: origin.fX = buffer.readInt(); michael@0: origin.fY = buffer.readInt(); michael@0: size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel(); michael@0: SkPixelRef* pr = buffer.readPixelRef(); michael@0: if (!buffer.validate((NULL == pr) || michael@0: (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) { michael@0: origin.setZero(); michael@0: } michael@0: SkSafeUnref(this->setPixelRef(pr, origin)); michael@0: break; michael@0: } michael@0: case SERIALIZE_PIXELTYPE_NONE: michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("unrecognized pixeltype in serialized data"); michael@0: sk_throw(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkBitmap::RLEPixels::RLEPixels(int width, int height) { michael@0: fHeight = height; michael@0: fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*)); michael@0: } michael@0: michael@0: SkBitmap::RLEPixels::~RLEPixels() { michael@0: sk_free(fYPtrs); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkBitmap::validate() const { michael@0: fInfo.validate(); michael@0: michael@0: // ImageInfo may not require this, but Bitmap ensures that opaque-only michael@0: // colorTypes report opaque for their alphatype michael@0: if (kRGB_565_SkColorType == fInfo.colorType()) { michael@0: SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType()); michael@0: } michael@0: michael@0: SkASSERT(fInfo.validRowBytes(fRowBytes)); michael@0: uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag; michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: allFlags |= kHasHardwareMipMap_Flag; michael@0: #endif michael@0: SkASSERT(fFlags <= allFlags); michael@0: SkASSERT(fPixelLockCount >= 0); michael@0: michael@0: if (fPixels) { michael@0: SkASSERT(fPixelRef); michael@0: SkASSERT(fPixelLockCount > 0); michael@0: SkASSERT(fPixelRef->isLocked()); michael@0: SkASSERT(fPixelRef->rowBytes() == fRowBytes); michael@0: SkASSERT(fPixelRefOrigin.fX >= 0); michael@0: SkASSERT(fPixelRefOrigin.fY >= 0); michael@0: SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX); michael@0: SkASSERT(fPixelRef->info().fHeight >= (int)this->height() + fPixelRefOrigin.fY); michael@0: SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes()); michael@0: } else { michael@0: SkASSERT(NULL == fColorTable); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkBitmap::toString(SkString* str) const { michael@0: michael@0: static const char* gConfigNames[kConfigCount] = { michael@0: "NONE", "A8", "INDEX8", "565", "4444", "8888" michael@0: }; michael@0: michael@0: str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(), michael@0: gConfigNames[this->config()]); michael@0: michael@0: str->append(" ("); michael@0: if (this->isOpaque()) { michael@0: str->append("opaque"); michael@0: } else { michael@0: str->append("transparent"); michael@0: } michael@0: if (this->isImmutable()) { michael@0: str->append(", immutable"); michael@0: } else { michael@0: str->append(", not-immutable"); michael@0: } michael@0: str->append(")"); michael@0: michael@0: SkPixelRef* pr = this->pixelRef(); michael@0: if (NULL == pr) { michael@0: // show null or the explicit pixel address (rare) michael@0: str->appendf(" pixels:%p", this->getPixels()); michael@0: } else { michael@0: const char* uri = pr->getURI(); michael@0: if (NULL != uri) { michael@0: str->appendf(" uri:\"%s\"", uri); michael@0: } else { michael@0: str->appendf(" pixelref:%p", pr); michael@0: } michael@0: } michael@0: michael@0: str->append(")"); michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkImageInfo::validate() const { michael@0: SkASSERT(fWidth >= 0); michael@0: SkASSERT(fHeight >= 0); michael@0: SkASSERT(SkColorTypeIsValid(fColorType)); michael@0: SkASSERT(SkAlphaTypeIsValid(fAlphaType)); michael@0: } michael@0: #endif