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" |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 9 | #include "SkBitmapCache.h" |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 11 | #include "SkData.h" |
halcanary | f2848b6 | 2015-12-10 12:40:23 -0800 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 13 | #include "SkImageFilter.h" |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 14 | #include "SkImageFilterCache.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 15 | #include "SkImageGenerator.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 16 | #include "SkImagePriv.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 17 | #include "SkImageShader.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 18 | #include "SkImage_Base.h" |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 19 | #include "SkNextID.h" |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 20 | #include "SkPicture.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 21 | #include "SkPixelRef.h" |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 22 | #include "SkPixelSerializer.h" |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 23 | #include "SkReadPixelsRec.h" |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 24 | #include "SkSpecialImage.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 25 | #include "SkStream.h" |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 26 | #include "SkString.h" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 27 | #include "SkSurface.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 28 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 29 | #if SK_SUPPORT_GPU |
| 30 | #include "GrTexture.h" |
| 31 | #include "GrContext.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 32 | #include "SkImage_Gpu.h" |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 33 | #endif |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 34 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 35 | SkImage::SkImage(int width, int height, uint32_t uniqueID) |
| 36 | : fWidth(width) |
| 37 | , fHeight(height) |
| 38 | , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) |
| 39 | { |
| 40 | SkASSERT(width > 0); |
| 41 | SkASSERT(height > 0); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 42 | } |
| 43 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 44 | bool SkImage::peekPixels(SkPixmap* pm) const { |
| 45 | SkPixmap tmp; |
| 46 | if (!pm) { |
| 47 | pm = &tmp; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 48 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 49 | return as_IB(this)->onPeekPixels(pm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 50 | } |
| 51 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 52 | bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 53 | int srcX, int srcY, CachingHint chint) const { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 54 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRowBytes, srcX, srcY); |
| 55 | if (!rec.trim(this->width(), this->height())) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 56 | return false; |
| 57 | } |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 58 | return as_IB(this)->onReadPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, chint); |
| 59 | } |
| 60 | |
| 61 | bool SkImage::scalePixels(const SkPixmap& dst, SkFilterQuality quality, CachingHint chint) const { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 62 | if (this->width() == dst.width() && this->height() == dst.height()) { |
| 63 | return this->readPixels(dst, 0, 0, chint); |
| 64 | } |
| 65 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 66 | // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself |
| 67 | // can scale more efficiently) we should take advantage of it here. |
| 68 | // |
| 69 | SkBitmap bm; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 70 | if (as_IB(this)->getROPixels(&bm, dst.info().colorSpace(), chint)) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 71 | bm.lockPixels(); |
| 72 | SkPixmap pmap; |
| 73 | // Note: By calling the pixmap scaler, we never cache the final result, so the chint |
| 74 | // is (currently) only being applied to the getROPixels. If we get a request to |
| 75 | // also attempt to cache the final (scaled) result, we would add that logic here. |
| 76 | // |
| 77 | return bm.peekPixels(&pmap) && pmap.scalePixels(dst, quality); |
| 78 | } |
| 79 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 80 | } |
| 81 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 82 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 83 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 84 | SkAlphaType SkImage::alphaType() const { |
| 85 | return as_IB(this)->onAlphaType(); |
| 86 | } |
| 87 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 88 | sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 89 | const SkMatrix* localMatrix) const { |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 90 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 93 | SkData* SkImage::encode(SkEncodedImageFormat type, int quality) const { |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 94 | SkBitmap bm; |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 95 | // TODO: Right now, the encoders don't handle F16 or linearly premultiplied data. Once they do, |
| 96 | // we should decode in "color space aware" mode, then re-encode that. For now, work around this |
| 97 | // by asking for a legacy decode (which gives us the raw data in N32). |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 98 | SkColorSpace* legacyColorSpace = nullptr; |
| 99 | if (as_IB(this)->getROPixels(&bm, legacyColorSpace)) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 100 | SkDynamicMemoryWStream buf; |
| 101 | return SkEncodeImage(&buf, bm, type, quality) ? buf.detachAsData().release() : nullptr; |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 102 | } |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
halcanary | a2e0567 | 2015-12-09 03:56:02 -0800 | [diff] [blame] | 106 | SkData* SkImage::encode(SkPixelSerializer* serializer) const { |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 107 | sk_sp<SkData> encoded(this->refEncoded()); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 108 | if (encoded && |
| 109 | (!serializer || serializer->useEncodedData(encoded->data(), encoded->size()))) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 110 | return encoded.release(); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SkBitmap bm; |
| 114 | SkAutoPixmapUnlock apu; |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 115 | // TODO: Right now, the encoders don't handle F16 or linearly premultiplied data. Once they do, |
| 116 | // we should decode in "color space aware" mode, then re-encode that. For now, work around this |
| 117 | // by asking for a legacy decode (which gives us the raw data in N32). |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 118 | SkColorSpace* legacyColorSpace = nullptr; |
| 119 | if (as_IB(this)->getROPixels(&bm, legacyColorSpace) && |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 120 | bm.requestLock(&apu)) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 121 | if (serializer) { |
| 122 | return serializer->encode(apu.pixmap()); |
| 123 | } else { |
| 124 | SkDynamicMemoryWStream buf; |
| 125 | return SkEncodeImage(&buf, apu.pixmap(), SkEncodedImageFormat::kPNG, 100) |
| 126 | ? buf.detachAsData().release() : nullptr; |
| 127 | } |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 133 | SkData* SkImage::refEncoded() const { |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 134 | GrContext* ctx = nullptr; // should we allow the caller to pass in a ctx? |
| 135 | return as_IB(this)->onRefEncoded(ctx); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 136 | } |
| 137 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 138 | sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | if (nullptr == encoded || 0 == encoded->size()) { |
| 140 | return nullptr; |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 141 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 142 | SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded.get()); |
| 143 | return SkImage::MakeFromGenerator(generator, subset); |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 144 | } |
| 145 | |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 146 | const char* SkImage::toString(SkString* str) const { |
| 147 | str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(), |
| 148 | this->isOpaque() ? "opaque" : ""); |
| 149 | return str->c_str(); |
| 150 | } |
| 151 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 152 | sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 153 | if (subset.isEmpty()) { |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
| 157 | const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 158 | if (!bounds.contains(subset)) { |
| 159 | return nullptr; |
| 160 | } |
| 161 | |
| 162 | // optimization : return self if the subset == our bounds |
| 163 | if (bounds == subset) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 164 | return sk_ref_sp(const_cast<SkImage*>(this)); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 165 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 166 | return as_IB(this)->onMakeSubset(subset); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 167 | } |
| 168 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 169 | #if SK_SUPPORT_GPU |
| 170 | |
| 171 | GrTexture* SkImage::getTexture() const { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 172 | return as_IB(this)->peekTexture(); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 175 | bool SkImage::isTextureBacked() const { return SkToBool(as_IB(this)->peekTexture()); } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 176 | |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 177 | GrBackendObject SkImage::getTextureHandle(bool flushPendingGrContextIO) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 178 | GrTexture* texture = as_IB(this)->peekTexture(); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 179 | if (texture) { |
| 180 | GrContext* context = texture->getContext(); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 181 | if (context) { |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 182 | if (flushPendingGrContextIO) { |
| 183 | context->prepareSurfaceForExternalIO(texture); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | return texture->getTextureHandle(); |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | #else |
| 192 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 193 | GrTexture* SkImage::getTexture() const { return nullptr; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 194 | |
| 195 | bool SkImage::isTextureBacked() const { return false; } |
| 196 | |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 197 | GrBackendObject SkImage::getTextureHandle(bool) const { return 0; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 198 | |
| 199 | #endif |
| 200 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 201 | /////////////////////////////////////////////////////////////////////////////// |
| 202 | |
| 203 | static bool raster_canvas_supports(const SkImageInfo& info) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 204 | switch (info.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 205 | case kN32_SkColorType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 206 | return kUnpremul_SkAlphaType != info.alphaType(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 207 | case kRGB_565_SkColorType: |
| 208 | return true; |
| 209 | case kAlpha_8_SkColorType: |
| 210 | return true; |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 217 | SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 218 | : INHERITED(width, height, uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 219 | , fAddedToCache(false) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 220 | {} |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 221 | |
| 222 | SkImage_Base::~SkImage_Base() { |
| 223 | if (fAddedToCache.load()) { |
| 224 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 225 | } |
| 226 | } |
| 227 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 228 | bool SkImage_Base::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 229 | int srcX, int srcY, CachingHint) const { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 230 | if (!raster_canvas_supports(dstInfo)) { |
| 231 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 232 | } |
| 233 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 234 | SkBitmap bm; |
| 235 | bm.installPixels(dstInfo, dstPixels, dstRowBytes); |
| 236 | SkCanvas canvas(bm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 237 | |
| 238 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 239 | paint.setBlendMode(SkBlendMode::kSrc); |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 240 | canvas.drawImage(this, -SkIntToScalar(srcX), -SkIntToScalar(srcY), &paint); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 241 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 242 | return true; |
| 243 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 244 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 245 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 246 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 247 | bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const { |
| 248 | return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 249 | } |
| 250 | |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 251 | #if SK_SUPPORT_GPU |
| 252 | #include "GrTextureToYUVPlanes.h" |
| 253 | #endif |
| 254 | |
| 255 | #include "SkRGBAToYUV.h" |
| 256 | |
| 257 | bool SkImage::readYUV8Planes(const SkISize sizes[3], void* const planes[3], |
Brian Salomon | de3a726 | 2016-02-03 17:09:00 -0500 | [diff] [blame] | 258 | const size_t rowBytes[3], SkYUVColorSpace colorSpace) const { |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 259 | #if SK_SUPPORT_GPU |
| 260 | if (GrTexture* texture = as_IB(this)->peekTexture()) { |
| 261 | if (GrTextureToYUVPlanes(texture, sizes, planes, rowBytes, colorSpace)) { |
| 262 | return true; |
| 263 | } |
| 264 | } |
| 265 | #endif |
| 266 | return SkRGBAToYUV(this, sizes, planes, rowBytes, colorSpace); |
| 267 | } |
| 268 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 269 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 270 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 271 | sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 272 | SkPixelRef* pr = bm.pixelRef(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 273 | if (nullptr == pr) { |
| 274 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 275 | } |
| 276 | |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 277 | return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 278 | } |
| 279 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 280 | bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 281 | return as_IB(this)->onAsLegacyBitmap(bitmap, mode); |
| 282 | } |
| 283 | |
| 284 | bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 285 | // As the base-class, all we can do is make a copy (regardless of mode). |
| 286 | // Subclasses that want to be more optimal should override. |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 287 | SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType).makeColorSpace(nullptr); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 288 | if (!bitmap->tryAllocPixels(info)) { |
| 289 | return false; |
| 290 | } |
| 291 | if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
| 292 | bitmap->reset(); |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | if (kRO_LegacyBitmapMode == mode) { |
| 297 | bitmap->setImmutable(); |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 302 | sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, |
| 303 | const SkMatrix* matrix, const SkPaint* paint) { |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 304 | return SkImage::MakeFromPicture(std::move(picture), dimensions, matrix, paint, BitDepth::kU8, |
Matt Sarett | f9436b8 | 2017-01-09 15:59:17 -0500 | [diff] [blame^] | 305 | nullptr); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 308 | sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, |
| 309 | const SkMatrix* matrix, const SkPaint* paint, |
| 310 | BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) { |
| 311 | return MakeFromGenerator(SkImageGenerator::NewFromPicture(dimensions, picture.get(), matrix, |
| 312 | paint, bitDepth, |
| 313 | std::move(colorSpace))); |
| 314 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 315 | sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset, |
| 316 | const SkIRect& clipBounds, SkIRect* outSubset, |
| 317 | SkIPoint* offset) const { |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 318 | if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) { |
| 319 | return nullptr; |
| 320 | } |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 321 | SkColorSpace* colorSpace = as_IB(this)->onImageInfo().colorSpace(); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 322 | sk_sp<SkSpecialImage> srcSpecialImage = SkSpecialImage::MakeFromImage( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 323 | subset, sk_ref_sp(const_cast<SkImage*>(this)), colorSpace); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 324 | if (!srcSpecialImage) { |
| 325 | return nullptr; |
| 326 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 327 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 328 | sk_sp<SkImageFilterCache> cache( |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 329 | SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize)); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 330 | SkImageFilter::OutputProperties outputProperties(colorSpace); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 331 | SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties); |
| 332 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 333 | sk_sp<SkSpecialImage> result = |
| 334 | filter->filterImage(srcSpecialImage.get(), context, offset); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 335 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 336 | if (!result) { |
| 337 | return nullptr; |
| 338 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 339 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 340 | SkIRect fullSize = SkIRect::MakeWH(result->width(), result->height()); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 341 | #if SK_SUPPORT_GPU |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 342 | if (result->isTextureBacked()) { |
| 343 | GrContext* context = result->getContext(); |
| 344 | sk_sp<GrTexture> texture = result->asTextureRef(context); |
brianosman | 818d8a9 | 2016-11-22 12:03:53 -0800 | [diff] [blame] | 345 | if (!texture) { |
| 346 | return nullptr; |
| 347 | } |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 348 | fullSize = SkIRect::MakeWH(texture->width(), texture->height()); |
| 349 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 350 | #endif |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 351 | *outSubset = SkIRect::MakeWH(result->width(), result->height()); |
| 352 | if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) { |
| 353 | return nullptr; |
| 354 | } |
| 355 | offset->fX += outSubset->x(); |
| 356 | offset->fY += outSubset->y(); |
| 357 | // This isn't really a "tight" subset, but includes any texture padding. |
| 358 | return result->makeTightSubset(fullSize); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 359 | } |
| 360 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 361 | bool SkImage::isLazyGenerated() const { |
| 362 | return as_IB(this)->onIsLazyGenerated(); |
| 363 | } |
| 364 | |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 365 | bool SkImage::isAlphaOnly() const { |
| 366 | return as_IB(this)->onImageInfo().colorType() == kAlpha_8_SkColorType; |
| 367 | } |
| 368 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 369 | ////////////////////////////////////////////////////////////////////////////////////// |
| 370 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 371 | #if !SK_SUPPORT_GPU |
| 372 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 373 | sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext*, const SkPixmap&, SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 374 | return nullptr; |
| 375 | } |
| 376 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 377 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 378 | int mipLevelCount, SkBudgeted, SkDestinationSurfaceColorMode) { |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 379 | return nullptr; |
| 380 | } |
| 381 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 382 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 383 | sk_sp<SkColorSpace>, TextureReleaseProc, ReleaseContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 384 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 385 | } |
| 386 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 387 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy&, |
| 388 | const DeferredTextureImageUsageParams[], |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 389 | int paramCnt, void* buffer, |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 390 | SkColorSpace* dstColorSpace) const { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void*, |
| 395 | SkBudgeted) { |
| 396 | return nullptr; |
| 397 | } |
| 398 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 399 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 400 | SkAlphaType, sk_sp<SkColorSpace>) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 401 | return nullptr; |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 402 | } |
| 403 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 404 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space, |
| 405 | const GrBackendObject yuvTextureHandles[3], |
| 406 | const SkISize yuvSizes[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 407 | GrSurfaceOrigin origin, |
| 408 | sk_sp<SkColorSpace> imageColorSpace) { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 409 | return nullptr; |
| 410 | } |
| 411 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 412 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
| 413 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 414 | } |
| 415 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 416 | #endif |
| 417 | |
| 418 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 419 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 420 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
| 421 | int mipLevelCount, SkBudgeted) { |
| 422 | return nullptr; |
| 423 | } |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 424 | |
| 425 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 426 | #include "SkImageDeserializer.h" |
| 427 | |
| 428 | sk_sp<SkImage> SkImageDeserializer::makeFromData(SkData* data, const SkIRect* subset) { |
| 429 | return SkImage::MakeFromEncoded(sk_ref_sp(data), subset); |
| 430 | } |
| 431 | sk_sp<SkImage> SkImageDeserializer::makeFromMemory(const void* data, size_t length, |
| 432 | const SkIRect* subset) { |
| 433 | return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length), subset); |
| 434 | } |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 435 | |
| 436 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 437 | |
Derek Sollenberger | d3ea9b7 | 2016-11-09 11:25:13 -0500 | [diff] [blame] | 438 | bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) { |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 439 | SkASSERT(image); |
| 440 | SkASSERT(ctx); |
Derek Sollenberger | d3ea9b7 | 2016-11-09 11:25:13 -0500 | [diff] [blame] | 441 | return as_IB(image)->onPinAsTexture(ctx); |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) { |
| 445 | SkASSERT(image); |
| 446 | SkASSERT(ctx); |
| 447 | as_IB(image)->onUnpinAsTexture(ctx); |
| 448 | } |