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 | |
reed | 8572fc0 | 2014-08-11 13:03:55 -0700 | [diff] [blame] | 32 | void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const { |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 33 | as_IB(this)->onDraw(canvas, x, y, paint); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 34 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 36 | void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst, |
reed | 8572fc0 | 2014-08-11 13:03:55 -0700 | [diff] [blame] | 37 | const SkPaint* paint) const { |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 38 | as_IB(this)->onDrawRectToRect(canvas, src, dst, paint); |
| 39 | } |
| 40 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 41 | const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const { |
| 42 | SkImageInfo infoStorage; |
| 43 | size_t rowBytesStorage; |
| 44 | if (NULL == info) { |
| 45 | info = &infoStorage; |
| 46 | } |
| 47 | if (NULL == rowBytes) { |
| 48 | rowBytes = &rowBytesStorage; |
| 49 | } |
| 50 | return as_IB(this)->onPeekPixels(info, rowBytes); |
| 51 | } |
| 52 | |
| 53 | bool SkImage::readPixels(SkBitmap* bitmap, const SkIRect* subset) const { |
| 54 | if (NULL == bitmap) { |
| 55 | return false; |
| 56 | } |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 57 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 58 | SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 59 | |
| 60 | // trim against the bitmap, if its already been allocated |
| 61 | if (bitmap->pixelRef()) { |
| 62 | bounds.fRight = SkMin32(bounds.fRight, bitmap->width()); |
| 63 | bounds.fBottom = SkMin32(bounds.fBottom, bitmap->height()); |
| 64 | if (bounds.isEmpty()) { |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (subset && !bounds.intersect(*subset)) { |
| 70 | // perhaps we could return true + empty-bitmap? |
| 71 | return false; |
| 72 | } |
| 73 | return as_IB(this)->onReadPixels(bitmap, bounds); |
| 74 | } |
| 75 | |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 76 | GrTexture* SkImage::getTexture() { |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 77 | return as_IB(this)->onGetTexture(); |
| 78 | } |
| 79 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 80 | SkShader* SkImage::newShader(SkShader::TileMode tileX, |
| 81 | SkShader::TileMode tileY, |
| 82 | const SkMatrix* localMatrix) const { |
| 83 | return as_IB(this)->onNewShader(tileX, tileY, localMatrix); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 84 | } |
| 85 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 86 | SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { |
| 87 | SkBitmap bm; |
| 88 | if (as_IB(this)->getROPixels(&bm)) { |
| 89 | return SkImageEncoder::EncodeData(bm, type, quality); |
| 90 | } |
| 91 | return NULL; |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 92 | } |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 93 | |
| 94 | /////////////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | static bool raster_canvas_supports(const SkImageInfo& info) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame^] | 97 | switch (info.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 98 | case kN32_SkColorType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame^] | 99 | return kUnpremul_SkAlphaType != info.alphaType(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 100 | case kRGB_565_SkColorType: |
| 101 | return true; |
| 102 | case kAlpha_8_SkColorType: |
| 103 | return true; |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | bool SkImage_Base::onReadPixels(SkBitmap* bitmap, const SkIRect& subset) const { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 111 | if (bitmap->pixelRef()) { |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 112 | const SkImageInfo info = bitmap->info(); |
| 113 | if (kUnknown_SkColorType == info.colorType()) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | if (!raster_canvas_supports(info)) { |
| 117 | return false; |
| 118 | } |
| 119 | } else { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 120 | SkBitmap tmp; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 121 | if (!tmp.tryAllocN32Pixels(subset.width(), subset.height())) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | *bitmap = tmp; |
| 125 | } |
| 126 | |
| 127 | SkRect srcR, dstR; |
| 128 | srcR.set(subset); |
| 129 | dstR = srcR; |
| 130 | dstR.offset(-dstR.left(), -dstR.top()); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 131 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 132 | SkCanvas canvas(*bitmap); |
| 133 | |
| 134 | SkPaint paint; |
| 135 | paint.setXfermodeMode(SkXfermode::kClear_Mode); |
| 136 | canvas.drawRect(dstR, paint); |
| 137 | |
| 138 | const_cast<SkImage_Base*>(this)->onDrawRectToRect(&canvas, &srcR, dstR, NULL); |
| 139 | return true; |
| 140 | } |