diff options
author | jcgregorio <jcgregorio@google.com> | 2014-07-11 07:48:20 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-07-11 07:48:20 -0700 |
commit | d08cb905a7cc80d8fb868bbd14fffe1cd68adcce (patch) | |
tree | 6f562c3fe22e3eb8eb45569fbfca025f57fc5fdb /src/core | |
parent | d081cf661ab418cb0d516a28e571282102691923 (diff) |
Revert of add readPixels() to SkBitmap (https://codereview.chromium.org/377303002/)
Reason for revert:
Maybe causing crashes in bench.
http://108.170.220.120:10117/builders/Test-ChromeOS-Daisy-MaliT604-Arm7-Debug/builds/2608/steps/RunBench/logs/stdio
Original issue's description:
> add readPixels() to SkBitmap
>
> BUG=chromium:390782
>
> Committed: https://skia.googlesource.com/skia/+/c4f216151b6ade70c35fade09a353052f40236b1
R=scroggo@google.com, bsalomon@google.com, reed@google.com
TBR=bsalomon@google.com, reed@google.com, scroggo@google.com
NOTREECHECKS=true
NOTRY=true
BUG=chromium:390782
Author: jcgregorio@google.com
Review URL: https://codereview.chromium.org/382243003
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/SkBitmap.cpp | 127 | ||||
-rw-r--r-- | src/core/SkBitmapDevice.cpp | 76 | ||||
-rw-r--r-- | src/core/SkConfig8888.cpp | 142 | ||||
-rw-r--r-- | src/core/SkConfig8888.h | 4 |
4 files changed, 130 insertions, 219 deletions
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index 9816711225..651152e703 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -821,13 +821,11 @@ bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { #include "SkPaint.h" bool SkBitmap::canCopyTo(SkColorType dstColorType) const { - const SkColorType srcCT = this->colorType(); - - if (srcCT == kUnknown_SkColorType) { + if (this->colorType() == kUnknown_SkColorType) { return false; } - bool sameConfigs = (srcCT == dstColorType); + bool sameConfigs = (this->colorType() == dstColorType); switch (dstColorType) { case kAlpha_8_SkColorType: case kRGB_565_SkColorType: @@ -840,66 +838,15 @@ bool SkBitmap::canCopyTo(SkColorType dstColorType) const { } break; case kARGB_4444_SkColorType: - return sameConfigs || kN32_SkColorType == srcCT || kIndex_8_SkColorType == srcCT; + return sameConfigs || kN32_SkColorType == this->colorType(); default: return false; } return true; } -#include "SkConfig8888.h" - -bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB, - int x, int y) const { - if (kUnknown_SkColorType == requestedDstInfo.colorType()) { - return false; - } - if (NULL == dstPixels || dstRB < requestedDstInfo.minRowBytes()) { - return false; - } - if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) { - return false; - } - - SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height()); - if (!srcR.intersect(0, 0, this->width(), this->height())) { - return false; - } - - SkImageInfo dstInfo = requestedDstInfo; - // the intersect may have shrunk info's logical size - dstInfo.fWidth = srcR.width(); - dstInfo.fHeight = srcR.height(); - - // if x or y are negative, then we have to adjust pixels - if (x > 0) { - x = 0; - } - if (y > 0) { - y = 0; - } - // here x,y are either 0 or negative - dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel()); - - ////////////// - - SkAutoLockPixels alp(*this); - - // since we don't stop creating un-pixeled devices yet, check for no pixels here - if (NULL == this->getPixels()) { - return false; - } - - SkImageInfo srcInfo = this->info(); - srcInfo.fWidth = dstInfo.width(); - srcInfo.fHeight = dstInfo.height(); - - const void* srcPixels = this->getAddr(srcR.x(), srcR.y()); - return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(), - this->getColorTable()); -} - -bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) const { +bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, + Allocator* alloc) const { if (!this->canCopyTo(dstColorType)) { return false; } @@ -972,21 +919,59 @@ bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) // returned false. SkASSERT(tmpDst.pixelRef() != NULL); - if (!src->readPixels(tmpDst.info(), tmpDst.getPixels(), tmpDst.rowBytes(), 0, 0)) { - return false; - } + /* do memcpy for the same configs cases, else use drawing + */ + if (src->colorType() == dstColorType) { + if (tmpDst.getSize() == src->getSize()) { + memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize()); - // (for BitmapHeap) Clone the pixelref genID even though we have a new pixelref. - // The old copyTo impl did this, so we continue it for now. - // - // TODO: should we ignore rowbytes (i.e. getSize)? Then it could just be - // if (src_pixelref->info == dst_pixelref->info) - // - if (src->colorType() == dstColorType && tmpDst.getSize() == src->getSize()) { - SkPixelRef* dstPixelRef = tmpDst.pixelRef(); - if (dstPixelRef->info() == fPixelRef->info()) { - dstPixelRef->cloneGenID(*fPixelRef); + SkPixelRef* dstPixelRef = tmpDst.pixelRef(); + if (dstPixelRef->info() == fPixelRef->info()) { + dstPixelRef->cloneGenID(*fPixelRef); + } + } else { + const char* srcP = reinterpret_cast<const char*>(src->getPixels()); + char* dstP = reinterpret_cast<char*>(tmpDst.getPixels()); + // to be sure we don't read too much, only copy our logical pixels + size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel(); + for (int y = 0; y < tmpDst.height(); y++) { + memcpy(dstP, srcP, bytesToCopy); + srcP += src->rowBytes(); + dstP += tmpDst.rowBytes(); + } } + } else if (kARGB_4444_SkColorType == dstColorType + && kN32_SkColorType == src->colorType()) { + if (src->alphaType() == kUnpremul_SkAlphaType) { + // Our method for converting to 4444 assumes premultiplied. + return false; + } + SkASSERT(src->height() == tmpDst.height()); + SkASSERT(src->width() == tmpDst.width()); + for (int y = 0; y < src->height(); ++y) { + SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y); + SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y); + DITHER_4444_SCAN(y); + for (int x = 0; x < src->width(); ++x) { + dstRow[x] = SkDitherARGB32To4444(srcRow[x], + DITHER_VALUE(x)); + } + } + } else { + if (tmpDst.alphaType() == kUnpremul_SkAlphaType) { + // We do not support drawing to unpremultiplied bitmaps. + return false; + } + + // Always clear the dest in case one of the blitters accesses it + // TODO: switch the allocation of tmpDst to call sk_calloc_throw + tmpDst.eraseColor(SK_ColorTRANSPARENT); + + SkCanvas canvas(tmpDst); + SkPaint paint; + + paint.setDither(true); + canvas.drawBitmap(*src, 0, 0, &paint); } dst->swap(tmpDst); diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp index 09b3b605f8..86c5702f03 100644 --- a/src/core/SkBitmapDevice.cpp +++ b/src/core/SkBitmapDevice.cpp @@ -141,8 +141,59 @@ void* SkBitmapDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) { return NULL; } +static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB, size_t bytesPerRow, + int rowCount) { + SkASSERT(bytesPerRow <= srcRB); + SkASSERT(bytesPerRow <= dstRB); + for (int i = 0; i < rowCount; ++i) { + memcpy(dst, src, bytesPerRow); + dst = (char*)dst + dstRB; + src = (const char*)src + srcRB; + } +} + #include "SkConfig8888.h" +static bool copy_pixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, + const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRowBytes) { + if (srcInfo.dimensions() != dstInfo.dimensions()) { + return false; + } + if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel()) { + SkDstPixelInfo dstPI; + dstPI.fColorType = dstInfo.colorType(); + dstPI.fAlphaType = dstInfo.alphaType(); + dstPI.fPixels = dstPixels; + dstPI.fRowBytes = dstRowBytes; + + SkSrcPixelInfo srcPI; + srcPI.fColorType = srcInfo.colorType(); + srcPI.fAlphaType = srcInfo.alphaType(); + srcPI.fPixels = srcPixels; + srcPI.fRowBytes = srcRowBytes; + + return srcPI.convertPixelsTo(&dstPI, srcInfo.width(), srcInfo.height()); + } + if (srcInfo.colorType() == dstInfo.colorType()) { + switch (srcInfo.colorType()) { + case kRGB_565_SkColorType: + case kAlpha_8_SkColorType: + break; + case kARGB_4444_SkColorType: + if (srcInfo.alphaType() != dstInfo.alphaType()) { + return false; + } + break; + default: + return false; + } + rect_memcpy(dstPixels, dstRowBytes, srcPixels, srcRowBytes, + srcInfo.width() * srcInfo.bytesPerPixel(), srcInfo.height()); + } + // TODO: add support for more conversions as needed + return false; +} + bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRowBytes, int x, int y) { // since we don't stop creating un-pixeled devices yet, check for no pixels here @@ -157,7 +208,7 @@ bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPi void* dstPixels = fBitmap.getAddr(x, y); size_t dstRowBytes = fBitmap.rowBytes(); - if (SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes)) { + if (copy_pixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes)) { fBitmap.notifyPixelsChanged(); return true; } @@ -166,7 +217,28 @@ bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPi bool SkBitmapDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int x, int y) { - return fBitmap.readPixels(dstInfo, dstPixels, dstRowBytes, x, y); + // since we don't stop creating un-pixeled devices yet, check for no pixels here + if (NULL == fBitmap.getPixels()) { + return false; + } + + SkImageInfo srcInfo = fBitmap.info(); + + // perhaps can relax these in the future + if (4 != dstInfo.bytesPerPixel()) { + return false; + } + if (4 != srcInfo.bytesPerPixel()) { + return false; + } + + srcInfo.fWidth = dstInfo.width(); + srcInfo.fHeight = dstInfo.height(); + + const void* srcPixels = fBitmap.getAddr(x, y); + const size_t srcRowBytes = fBitmap.rowBytes(); + + return copy_pixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes); } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/SkConfig8888.cpp b/src/core/SkConfig8888.cpp index b0572c0544..189309dae6 100644 --- a/src/core/SkConfig8888.cpp +++ b/src/core/SkConfig8888.cpp @@ -1,15 +1,5 @@ -/* - * Copyright 2014 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "SkBitmap.h" -#include "SkCanvas.h" #include "SkConfig8888.h" #include "SkColorPriv.h" -#include "SkDither.h" #include "SkMathPriv.h" #include "SkUnPreMultiply.h" @@ -125,135 +115,3 @@ bool SkSrcPixelInfo::convertPixelsTo(SkDstPixelInfo* dst, int width, int height) } return true; } - -static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB, size_t bytesPerRow, - int rowCount) { - SkASSERT(bytesPerRow <= srcRB); - SkASSERT(bytesPerRow <= dstRB); - for (int i = 0; i < rowCount; ++i) { - memcpy(dst, src, bytesPerRow); - dst = (char*)dst + dstRB; - src = (const char*)src + srcRB; - } -} - -bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, - const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB, - SkColorTable* ctable) { - if (srcInfo.dimensions() != dstInfo.dimensions()) { - return false; - } - - const int width = srcInfo.width(); - const int height = srcInfo.height(); - - // Handle fancy alpha swizzling if both are ARGB32 - if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel()) { - SkDstPixelInfo dstPI; - dstPI.fColorType = dstInfo.colorType(); - dstPI.fAlphaType = dstInfo.alphaType(); - dstPI.fPixels = dstPixels; - dstPI.fRowBytes = dstRB; - - SkSrcPixelInfo srcPI; - srcPI.fColorType = srcInfo.colorType(); - srcPI.fAlphaType = srcInfo.alphaType(); - srcPI.fPixels = srcPixels; - srcPI.fRowBytes = srcRB; - - return srcPI.convertPixelsTo(&dstPI, width, height); - } - - // If they agree on colorType and the alphaTypes are compatible, then we just memcpy. - // Note: we've already taken care of 32bit colortypes above. - if (srcInfo.colorType() == dstInfo.colorType()) { - switch (srcInfo.colorType()) { - case kRGB_565_SkColorType: - case kAlpha_8_SkColorType: - break; - case kIndex_8_SkColorType: - case kARGB_4444_SkColorType: - if (srcInfo.alphaType() != dstInfo.alphaType()) { - return false; - } - break; - default: - return false; - } - rect_memcpy(dstPixels, dstRB, srcPixels, srcRB, width * srcInfo.bytesPerPixel(), height); - return true; - } - - /* - * Begin section where we try to change colorTypes along the way. Not all combinations - * are supported. - */ - - // Can no longer draw directly into 4444, but we can manually whack it for a few combinations - if (kARGB_4444_SkColorType == dstInfo.colorType() && - (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcInfo.colorType())) { - if (srcInfo.alphaType() == kUnpremul_SkAlphaType) { - // Our method for converting to 4444 assumes premultiplied. - return false; - } - - const SkPMColor* table = NULL; - if (kIndex_8_SkColorType == srcInfo.colorType()) { - if (NULL == ctable) { - return false; - } - table = ctable->lockColors(); - } - - for (int y = 0; y < height; ++y) { - DITHER_4444_SCAN(y); - SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*)dstPixels; - if (table) { - const uint8_t* SK_RESTRICT srcRow = (const uint8_t*)srcPixels; - for (int x = 0; x < width; ++x) { - dstRow[x] = SkDitherARGB32To4444(table[srcRow[x]], DITHER_VALUE(x)); - } - } else { - const SkPMColor* SK_RESTRICT srcRow = (const SkPMColor*)srcPixels; - for (int x = 0; x < width; ++x) { - dstRow[x] = SkDitherARGB32To4444(srcRow[x], DITHER_VALUE(x)); - } - } - dstPixels = (char*)dstPixels + dstRB; - srcPixels = (const char*)srcPixels + srcRB; - } - - if (table) { - ctable->unlockColors(); - } - return true; - } - - if (dstInfo.alphaType() == kUnpremul_SkAlphaType) { - // We do not support drawing to unpremultiplied bitmaps. - return false; - } - - // Final fall-back, draw with a canvas - // - // Always clear the dest in case one of the blitters accesses it - // TODO: switch the allocation of tmpDst to call sk_calloc_throw - { - SkBitmap bm; - if (!bm.installPixels(srcInfo, const_cast<void*>(srcPixels), srcRB, ctable, NULL, NULL)) { - return false; - } - SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterDirect(dstInfo, dstPixels, dstRB)); - if (NULL == canvas.get()) { - return false; - } - - SkPaint paint; - paint.setDither(true); - - canvas->clear(0); - canvas->drawBitmap(bm, 0, 0, &paint); - return true; - } -} - diff --git a/src/core/SkConfig8888.h b/src/core/SkConfig8888.h index da0cbc6688..97a3433ad2 100644 --- a/src/core/SkConfig8888.h +++ b/src/core/SkConfig8888.h @@ -14,10 +14,6 @@ struct SkPixelInfo { SkColorType fColorType; SkAlphaType fAlphaType; size_t fRowBytes; - - static bool CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, - const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRowBytes, - SkColorTable* srcCTable = NULL); }; struct SkDstPixelInfo : SkPixelInfo { |