reed@google.com | 6997ebb | 2012-07-30 19:50:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 10 | #include "SkImagePriv.h" |
| 11 | #include "SkImage_Base.h" |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 12 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 13 | static SkImage_Base* as_IB(SkImage* image) { |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 14 | return static_cast<SkImage_Base*>(image); |
| 15 | } |
| 16 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 17 | static const SkImage_Base* as_IB(const SkImage* image) { |
| 18 | return static_cast<const SkImage_Base*>(image); |
| 19 | } |
| 20 | |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 21 | uint32_t SkImage::NextUniqueID() { |
| 22 | static int32_t gUniqueID; |
| 23 | |
| 24 | // never return 0; |
| 25 | uint32_t id; |
| 26 | do { |
| 27 | id = sk_atomic_inc(&gUniqueID) + 1; |
| 28 | } while (0 == id); |
| 29 | return id; |
| 30 | } |
| 31 | |
| 32 | void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 33 | const SkPaint* paint) { |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 34 | as_IB(this)->onDraw(canvas, x, y, paint); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 35 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 37 | void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst, |
| 38 | const SkPaint* paint) { |
| 39 | as_IB(this)->onDrawRectToRect(canvas, src, dst, paint); |
| 40 | } |
| 41 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 42 | const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const { |
| 43 | SkImageInfo infoStorage; |
| 44 | size_t rowBytesStorage; |
| 45 | if (NULL == info) { |
| 46 | info = &infoStorage; |
| 47 | } |
| 48 | if (NULL == rowBytes) { |
| 49 | rowBytes = &rowBytesStorage; |
| 50 | } |
| 51 | return as_IB(this)->onPeekPixels(info, rowBytes); |
| 52 | } |
| 53 | |
| 54 | bool SkImage::readPixels(SkBitmap* bitmap, const SkIRect* subset) const { |
| 55 | if (NULL == bitmap) { |
| 56 | return false; |
| 57 | } |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 58 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 59 | SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 60 | |
| 61 | // trim against the bitmap, if its already been allocated |
| 62 | if (bitmap->pixelRef()) { |
| 63 | bounds.fRight = SkMin32(bounds.fRight, bitmap->width()); |
| 64 | bounds.fBottom = SkMin32(bounds.fBottom, bitmap->height()); |
| 65 | if (bounds.isEmpty()) { |
| 66 | return false; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (subset && !bounds.intersect(*subset)) { |
| 71 | // perhaps we could return true + empty-bitmap? |
| 72 | return false; |
| 73 | } |
| 74 | return as_IB(this)->onReadPixels(bitmap, bounds); |
| 75 | } |
| 76 | |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 77 | GrTexture* SkImage::getTexture() { |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 78 | return as_IB(this)->onGetTexture(); |
| 79 | } |
| 80 | |
| 81 | SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { |
| 82 | SkBitmap bm; |
| 83 | if (as_IB(this)->getROPixels(&bm)) { |
| 84 | return SkImageEncoder::EncodeData(bm, type, quality); |
| 85 | } |
| 86 | return NULL; |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 87 | } |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | static bool raster_canvas_supports(const SkImageInfo& info) { |
| 92 | switch (info.fColorType) { |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame] | 93 | case kN32_SkColorType: |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 94 | return kUnpremul_SkAlphaType != info.fAlphaType; |
| 95 | case kRGB_565_SkColorType: |
| 96 | return true; |
| 97 | case kAlpha_8_SkColorType: |
| 98 | return true; |
| 99 | default: |
| 100 | break; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | bool SkImage_Base::onReadPixels(SkBitmap* bitmap, const SkIRect& subset) const { |
| 106 | SkImageInfo info; |
| 107 | |
| 108 | if (bitmap->pixelRef()) { |
| 109 | if (!bitmap->asImageInfo(&info)) { |
| 110 | return false; |
| 111 | } |
| 112 | if (!raster_canvas_supports(info)) { |
| 113 | return false; |
| 114 | } |
| 115 | } else { |
| 116 | SkImageInfo info = SkImageInfo::MakeN32Premul(subset.width(), |
| 117 | subset.height()); |
| 118 | SkBitmap tmp; |
| 119 | if (!tmp.allocPixels(info)) { |
| 120 | return false; |
| 121 | } |
| 122 | *bitmap = tmp; |
| 123 | } |
| 124 | |
| 125 | SkRect srcR, dstR; |
| 126 | srcR.set(subset); |
| 127 | dstR = srcR; |
| 128 | dstR.offset(-dstR.left(), -dstR.top()); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 129 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 130 | SkCanvas canvas(*bitmap); |
| 131 | |
| 132 | SkPaint paint; |
| 133 | paint.setXfermodeMode(SkXfermode::kClear_Mode); |
| 134 | canvas.drawRect(dstR, paint); |
| 135 | |
| 136 | const_cast<SkImage_Base*>(this)->onDrawRectToRect(&canvas, &srcR, dstR, NULL); |
| 137 | return true; |
| 138 | } |