Chromium Code Reviews| Index: src/image/SkImage.cpp |
| diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp |
| index 620922f64509ee15dd893de9c44e10d810f4b414..2b32126c74f2a8e049ab6bf3afd9535615dea357 100644 |
| --- a/src/image/SkImage.cpp |
| +++ b/src/image/SkImage.cpp |
| @@ -39,6 +39,41 @@ void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst, |
| as_IB(this)->onDrawRectToRect(canvas, src, dst, paint); |
| } |
| +const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const { |
| + SkImageInfo infoStorage; |
| + size_t rowBytesStorage; |
| + if (NULL == info) { |
| + info = &infoStorage; |
| + } |
| + if (NULL == rowBytes) { |
| + rowBytes = &rowBytesStorage; |
| + } |
| + return as_IB(this)->onPeekPixels(info, rowBytes); |
| +} |
| + |
| +bool SkImage::readPixels(SkBitmap* bitmap, const SkIRect* subset) const { |
| + if (NULL == bitmap) { |
| + return false; |
| + } |
| + |
| + SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| + |
| + // trim against the bitmap, if its already been allocated |
| + if (bitmap->pixelRef()) { |
| + bounds.fRight = SkMin32(bounds.fRight, bitmap->width()); |
|
bsalomon
2014/02/05 21:36:20
does this mean that for a preallocated bitmap the
|
| + bounds.fBottom = SkMin32(bounds.fBottom, bitmap->height()); |
| + if (bounds.isEmpty()) { |
| + return false; |
| + } |
| + } |
| + |
| + if (subset && !bounds.intersect(*subset)) { |
| + // perhaps we could return true + empty-bitmap? |
| + return false; |
| + } |
| + return as_IB(this)->onReadPixels(bitmap, bounds); |
| +} |
| + |
| GrTexture* SkImage::getTexture() { |
| return as_IB(this)->onGetTexture(); |
| } |
| @@ -50,3 +85,55 @@ SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { |
| } |
| return NULL; |
| } |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| +static bool canvas_supports(const SkImageInfo& info) { |
| + switch (info.fColorType) { |
| + case kPMColor_SkColorType: |
| + return kUnpremul_SkAlphaType != info.fAlphaType; |
| + case kRGB_565_SkColorType: |
| + return true; |
| + case kAlpha_8_SkColorType: |
| + return true; |
| + default: |
| + break; |
| + } |
| + return false; |
| +} |
| + |
| +bool SkImage_Base::onReadPixels(SkBitmap* bitmap, const SkIRect& subset) const { |
| + SkImageInfo info; |
| + |
| + if (bitmap->pixelRef()) { |
| + if (!bitmap->asImageInfo(&info)) { |
| + return false; |
| + } |
| + if (canvas_supports(info)) { |
|
scroggo
2014/02/05 21:55:28
Should this be
if(!canvas_supports(info))
?
|
| + return false; |
| + } |
| + } else { |
| + SkImageInfo info = SkImageInfo::MakeN32Premul(subset.width(), |
|
scroggo
2014/02/05 21:55:28
Are we okay with the side effect that readPixels r
|
| + subset.height()); |
| + SkBitmap tmp; |
| + if (!tmp.allocPixels(info)) { |
| + return false; |
| + } |
| + *bitmap = tmp; |
| + } |
| + |
| + SkRect srcR, dstR; |
| + srcR.set(subset); |
| + dstR = srcR; |
| + dstR.offset(-dstR.left(), -dstR.top()); |
| + |
| + SkCanvas canvas(*bitmap); |
| + |
| + SkPaint paint; |
| + paint.setXfermodeMode(SkXfermode::kClear_Mode); |
| + canvas.drawRect(dstR, paint); |
| + |
| + const_cast<SkImage_Base*>(this)->onDrawRectToRect(&canvas, &srcR, dstR, NULL); |
| + return true; |
| +} |
| + |