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" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 10 | #include "SkImageGenerator.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 11 | #include "SkImagePriv.h" |
| 12 | #include "SkImage_Base.h" |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 13 | #include "SkReadPixelsRec.h" |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 14 | #include "SkString.h" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 15 | #include "SkSurface.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 16 | |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 17 | uint32_t SkImage::NextUniqueID() { |
| 18 | static int32_t gUniqueID; |
| 19 | |
| 20 | // never return 0; |
| 21 | uint32_t id; |
| 22 | do { |
| 23 | id = sk_atomic_inc(&gUniqueID) + 1; |
| 24 | } while (0 == id); |
| 25 | return id; |
| 26 | } |
| 27 | |
reed | 8572fc0 | 2014-08-11 13:03:55 -0700 | [diff] [blame] | 28 | 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] | 29 | as_IB(this)->onDraw(canvas, x, y, paint); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 30 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 31 | |
piotaixr | 5ceff91 | 2014-09-26 07:36:26 -0700 | [diff] [blame] | 32 | void SkImage::drawRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst, |
reed | 8572fc0 | 2014-08-11 13:03:55 -0700 | [diff] [blame] | 33 | const SkPaint* paint) const { |
piotaixr | 5ceff91 | 2014-09-26 07:36:26 -0700 | [diff] [blame] | 34 | as_IB(this)->onDrawRect(canvas, src, dst, paint); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 35 | } |
| 36 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 37 | const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const { |
| 38 | SkImageInfo infoStorage; |
| 39 | size_t rowBytesStorage; |
| 40 | if (NULL == info) { |
| 41 | info = &infoStorage; |
| 42 | } |
| 43 | if (NULL == rowBytes) { |
| 44 | rowBytes = &rowBytesStorage; |
| 45 | } |
| 46 | return as_IB(this)->onPeekPixels(info, rowBytes); |
| 47 | } |
| 48 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 49 | bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 50 | int srcX, int srcY) const { |
| 51 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRowBytes, srcX, srcY); |
| 52 | if (!rec.trim(this->width(), this->height())) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 55 | return as_IB(this)->onReadPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 56 | } |
| 57 | |
kkinnunen | cd6ca9e | 2015-02-18 10:50:52 -0800 | [diff] [blame] | 58 | GrTexture* SkImage::getTexture() const { |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 59 | return as_IB(this)->onGetTexture(); |
| 60 | } |
| 61 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 62 | SkShader* SkImage::newShader(SkShader::TileMode tileX, |
| 63 | SkShader::TileMode tileY, |
| 64 | const SkMatrix* localMatrix) const { |
| 65 | return as_IB(this)->onNewShader(tileX, tileY, localMatrix); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 66 | } |
| 67 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 68 | SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { |
| 69 | SkBitmap bm; |
| 70 | if (as_IB(this)->getROPixels(&bm)) { |
| 71 | return SkImageEncoder::EncodeData(bm, type, quality); |
| 72 | } |
| 73 | return NULL; |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 74 | } |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 75 | |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 76 | SkImage* SkImage::NewFromData(SkData* data) { |
| 77 | if (NULL == data) { |
| 78 | return NULL; |
| 79 | } |
| 80 | SkImageGenerator* generator = SkImageGenerator::NewFromData(data); |
| 81 | return generator ? SkImage::NewFromGenerator(generator) : NULL; |
| 82 | } |
| 83 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 84 | SkSurface* SkImage::newSurface(const SkImageInfo& info, const SkSurfaceProps* props) const { |
| 85 | if (NULL == props) { |
| 86 | props = &as_IB(this)->props(); |
| 87 | } |
| 88 | return as_IB(this)->onNewSurface(info, *props); |
| 89 | } |
| 90 | |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 91 | const char* SkImage::toString(SkString* str) const { |
| 92 | str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(), |
| 93 | this->isOpaque() ? "opaque" : ""); |
| 94 | return str->c_str(); |
| 95 | } |
| 96 | |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 97 | SkImage* SkImage::newImage(int newWidth, int newHeight, const SkIRect* subset, |
| 98 | SkFilterQuality quality) const { |
| 99 | if (newWidth <= 0 || newHeight <= 0) { |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 104 | |
| 105 | if (subset) { |
| 106 | if (!bounds.contains(*subset)) { |
| 107 | return NULL; |
| 108 | } |
| 109 | if (bounds == *subset) { |
| 110 | subset = NULL; // and fall through to check below |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (NULL == subset && this->width() == newWidth && this->height() == newHeight) { |
| 115 | return SkRef(const_cast<SkImage*>(this)); |
| 116 | } |
| 117 | |
| 118 | return as_IB(this)->onNewImage(newWidth, newHeight, subset, quality); |
| 119 | } |
| 120 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | |
| 123 | static bool raster_canvas_supports(const SkImageInfo& info) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 124 | switch (info.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 125 | case kN32_SkColorType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 126 | return kUnpremul_SkAlphaType != info.alphaType(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 127 | case kRGB_565_SkColorType: |
| 128 | return true; |
| 129 | case kAlpha_8_SkColorType: |
| 130 | return true; |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 137 | bool SkImage_Base::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 138 | int srcX, int srcY) const { |
| 139 | if (!raster_canvas_supports(dstInfo)) { |
| 140 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 141 | } |
| 142 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 143 | SkBitmap bm; |
| 144 | bm.installPixels(dstInfo, dstPixels, dstRowBytes); |
| 145 | SkCanvas canvas(bm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 146 | |
| 147 | SkPaint paint; |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 148 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 149 | canvas.drawImage(this, -SkIntToScalar(srcX), -SkIntToScalar(srcY), &paint); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 150 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 151 | return true; |
| 152 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 153 | |
| 154 | SkImage* SkImage_Base::onNewImage(int newWidth, int newHeight, const SkIRect* subset, |
| 155 | SkFilterQuality quality) const { |
| 156 | const bool opaque = this->isOpaque(); |
| 157 | const SkImageInfo info = SkImageInfo::Make(newWidth, newHeight, kN32_SkColorType, |
| 158 | opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 159 | SkAutoTUnref<SkSurface> surface(this->newSurface(info, NULL)); |
| 160 | if (!surface.get()) { |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | SkRect src; |
| 165 | if (subset) { |
| 166 | src.set(*subset); |
| 167 | } else { |
| 168 | src = SkRect::MakeIWH(this->width(), this->height()); |
| 169 | } |
| 170 | |
| 171 | surface->getCanvas()->scale(newWidth / src.width(), newHeight / src.height()); |
| 172 | surface->getCanvas()->translate(-src.x(), -src.y()); |
| 173 | |
| 174 | SkPaint paint; |
| 175 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 176 | paint.setFilterQuality(quality); |
| 177 | surface->getCanvas()->drawImage(this, 0, 0, &paint); |
| 178 | return surface->newImageSnapshot(); |
| 179 | } |
| 180 | |
| 181 | |