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" |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 11 | #include "SkColorSpace_Base.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 12 | #include "SkData.h" |
halcanary | f2848b6 | 2015-12-10 12:40:23 -0800 | [diff] [blame] | 13 | #include "SkImageEncoder.h" |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 14 | #include "SkImageFilter.h" |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 15 | #include "SkImageFilterCache.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 16 | #include "SkImageGenerator.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 17 | #include "SkImagePriv.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 18 | #include "SkImageShader.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 19 | #include "SkImage_Base.h" |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 20 | #include "SkNextID.h" |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 21 | #include "SkPicture.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 22 | #include "SkPixelRef.h" |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 23 | #include "SkPixelSerializer.h" |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 24 | #include "SkReadPixelsRec.h" |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 25 | #include "SkSpecialImage.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 26 | #include "SkStream.h" |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 27 | #include "SkString.h" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 28 | #include "SkSurface.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 29 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 30 | #if SK_SUPPORT_GPU |
| 31 | #include "GrTexture.h" |
| 32 | #include "GrContext.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 33 | #include "SkImage_Gpu.h" |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 34 | #endif |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 35 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 36 | SkImage::SkImage(int width, int height, uint32_t uniqueID) |
| 37 | : fWidth(width) |
| 38 | , fHeight(height) |
| 39 | , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) |
| 40 | { |
| 41 | SkASSERT(width > 0); |
| 42 | SkASSERT(height > 0); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 43 | } |
| 44 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 45 | bool SkImage::peekPixels(SkPixmap* pm) const { |
| 46 | SkPixmap tmp; |
| 47 | if (!pm) { |
| 48 | pm = &tmp; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 49 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 50 | return as_IB(this)->onPeekPixels(pm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 51 | } |
| 52 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 53 | bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 54 | int srcX, int srcY, CachingHint chint) const { |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 55 | return as_IB(this)->onReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool SkImage::scalePixels(const SkPixmap& dst, SkFilterQuality quality, CachingHint chint) const { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 59 | if (this->width() == dst.width() && this->height() == dst.height()) { |
| 60 | return this->readPixels(dst, 0, 0, chint); |
| 61 | } |
| 62 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 63 | // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself |
| 64 | // can scale more efficiently) we should take advantage of it here. |
| 65 | // |
| 66 | SkBitmap bm; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 67 | if (as_IB(this)->getROPixels(&bm, dst.info().colorSpace(), chint)) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 68 | SkPixmap pmap; |
| 69 | // Note: By calling the pixmap scaler, we never cache the final result, so the chint |
| 70 | // is (currently) only being applied to the getROPixels. If we get a request to |
| 71 | // also attempt to cache the final (scaled) result, we would add that logic here. |
| 72 | // |
| 73 | return bm.peekPixels(&pmap) && pmap.scalePixels(dst, quality); |
| 74 | } |
| 75 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 76 | } |
| 77 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 78 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 79 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 80 | SkAlphaType SkImage::alphaType() const { |
| 81 | return as_IB(this)->onAlphaType(); |
| 82 | } |
| 83 | |
Matt Sarett | 5b4599f | 2017-03-02 12:42:35 -0500 | [diff] [blame] | 84 | SkColorSpace* SkImage::colorSpace() const { |
| 85 | return as_IB(this)->onImageInfo().colorSpace(); |
| 86 | } |
| 87 | |
| 88 | sk_sp<SkColorSpace> SkImage::refColorSpace() const { |
| 89 | return as_IB(this)->onImageInfo().refColorSpace(); |
| 90 | } |
| 91 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 92 | sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 93 | const SkMatrix* localMatrix) const { |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 94 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame^] | 97 | sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const { |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 98 | SkBitmap bm; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 99 | SkColorSpace* legacyColorSpace = nullptr; |
| 100 | if (as_IB(this)->getROPixels(&bm, legacyColorSpace)) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 101 | SkDynamicMemoryWStream buf; |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame^] | 102 | return SkEncodeImage(&buf, bm, type, quality) ? buf.detachAsData() : nullptr; |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 103 | } |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame^] | 107 | sk_sp<SkData> SkImage::encodeToData(SkPixelSerializer* serializer) const { |
| 108 | sk_sp<SkData> encoded(this->refEncodedData()); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 109 | if (encoded && |
| 110 | (!serializer || serializer->useEncodedData(encoded->data(), encoded->size()))) { |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame^] | 111 | return encoded; |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | SkBitmap bm; |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 115 | SkPixmap pmap; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 116 | SkColorSpace* legacyColorSpace = nullptr; |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 117 | if (as_IB(this)->getROPixels(&bm, legacyColorSpace) && bm.peekPixels(&pmap)) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 118 | if (serializer) { |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame^] | 119 | return serializer->encodeToData(pmap); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 120 | } else { |
| 121 | SkDynamicMemoryWStream buf; |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 122 | return SkEncodeImage(&buf, pmap, SkEncodedImageFormat::kPNG, 100) |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame^] | 123 | ? buf.detachAsData() : nullptr; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 124 | } |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame^] | 130 | sk_sp<SkData> SkImage::refEncodedData() const { |
| 131 | return sk_sp<SkData>(as_IB(this)->onRefEncoded()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 132 | } |
| 133 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 134 | sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | if (nullptr == encoded || 0 == encoded->size()) { |
| 136 | return nullptr; |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 137 | } |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 138 | return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(encoded), subset); |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame^] | 141 | #ifdef SK_SUPPORT_LEGACY_IMAGE_ENCODE_API |
| 142 | SkData* SkImage::encode(SkEncodedImageFormat format, int quality) const { |
| 143 | return this->encodeToData(format, quality).release(); |
| 144 | } |
| 145 | SkData* SkImage::encode(SkPixelSerializer* serial) const { |
| 146 | return this->encodeToData(serial).release(); |
| 147 | } |
| 148 | SkData* SkImage::refEncoded() const { |
| 149 | return this->refEncodedData().release(); |
| 150 | } |
| 151 | SkData* SkPixelSerializer::encode(const SkPixmap& pixmap) { |
| 152 | return this->encodeToData(pixmap).release(); |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 157 | |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 158 | const char* SkImage::toString(SkString* str) const { |
| 159 | str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(), |
| 160 | this->isOpaque() ? "opaque" : ""); |
| 161 | return str->c_str(); |
| 162 | } |
| 163 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 164 | sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 165 | if (subset.isEmpty()) { |
| 166 | return nullptr; |
| 167 | } |
| 168 | |
| 169 | const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 170 | if (!bounds.contains(subset)) { |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | // optimization : return self if the subset == our bounds |
| 175 | if (bounds == subset) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 176 | return sk_ref_sp(const_cast<SkImage*>(this)); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 177 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 178 | return as_IB(this)->onMakeSubset(subset); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 179 | } |
| 180 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 181 | #if SK_SUPPORT_GPU |
| 182 | |
| 183 | GrTexture* SkImage::getTexture() const { |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 184 | return as_IB(this)->onGetTexture(); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 187 | bool SkImage::isTextureBacked() const { return SkToBool(as_IB(this)->peekProxy()); } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 188 | |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 189 | GrBackendObject SkImage::getTextureHandle(bool flushPendingGrContextIO, |
| 190 | GrSurfaceOrigin* origin) const { |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 191 | return as_IB(this)->onGetTextureHandle(flushPendingGrContextIO, origin); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 194 | bool SkImage::isValid(GrContext* context) const { |
| 195 | if (context && context->abandoned()) { |
| 196 | return false; |
| 197 | } |
| 198 | return as_IB(this)->onIsValid(context); |
| 199 | } |
| 200 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 201 | #else |
| 202 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 203 | GrTexture* SkImage::getTexture() const { return nullptr; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 204 | |
| 205 | bool SkImage::isTextureBacked() const { return false; } |
| 206 | |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 207 | GrBackendObject SkImage::getTextureHandle(bool, GrSurfaceOrigin*) const { return 0; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 208 | |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 209 | bool SkImage::isValid(GrContext* context) const { |
| 210 | if (context) { |
| 211 | return false; |
| 212 | } |
| 213 | return as_IB(this)->onIsValid(context); |
| 214 | } |
| 215 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 216 | #endif |
| 217 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 218 | /////////////////////////////////////////////////////////////////////////////// |
| 219 | |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 220 | SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 221 | : INHERITED(width, height, uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 222 | , fAddedToCache(false) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 223 | {} |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 224 | |
| 225 | SkImage_Base::~SkImage_Base() { |
| 226 | if (fAddedToCache.load()) { |
| 227 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 228 | } |
| 229 | } |
| 230 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 231 | bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const { |
| 232 | return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 236 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 237 | sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 238 | SkPixelRef* pr = bm.pixelRef(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 239 | if (nullptr == pr) { |
| 240 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 241 | } |
| 242 | |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 243 | return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 244 | } |
| 245 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 246 | bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 247 | return as_IB(this)->onAsLegacyBitmap(bitmap, mode); |
| 248 | } |
| 249 | |
| 250 | bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 251 | // As the base-class, all we can do is make a copy (regardless of mode). |
| 252 | // Subclasses that want to be more optimal should override. |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 253 | SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType).makeColorSpace(nullptr); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 254 | if (!bitmap->tryAllocPixels(info)) { |
| 255 | return false; |
| 256 | } |
| 257 | if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
| 258 | bitmap->reset(); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | if (kRO_LegacyBitmapMode == mode) { |
| 263 | bitmap->setImmutable(); |
| 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 268 | sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 269 | const SkMatrix* matrix, const SkPaint* paint, |
| 270 | BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 271 | return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture), |
| 272 | matrix, paint, bitDepth, |
| 273 | std::move(colorSpace))); |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 274 | } |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 275 | |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 276 | sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset, |
| 277 | const SkIRect& clipBounds, SkIRect* outSubset, |
| 278 | SkIPoint* offset) const { |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 279 | if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) { |
| 280 | return nullptr; |
| 281 | } |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 282 | SkColorSpace* colorSpace = as_IB(this)->onImageInfo().colorSpace(); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 283 | sk_sp<SkSpecialImage> srcSpecialImage = SkSpecialImage::MakeFromImage( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 284 | subset, sk_ref_sp(const_cast<SkImage*>(this)), colorSpace); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 285 | if (!srcSpecialImage) { |
| 286 | return nullptr; |
| 287 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 288 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 289 | sk_sp<SkImageFilterCache> cache( |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 290 | SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize)); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 291 | SkImageFilter::OutputProperties outputProperties(colorSpace); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 292 | SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties); |
| 293 | |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 294 | sk_sp<SkSpecialImage> result = filter->filterImage(srcSpecialImage.get(), context, offset); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 295 | if (!result) { |
| 296 | return nullptr; |
| 297 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 298 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 299 | *outSubset = SkIRect::MakeWH(result->width(), result->height()); |
| 300 | if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) { |
| 301 | return nullptr; |
| 302 | } |
| 303 | offset->fX += outSubset->x(); |
| 304 | offset->fY += outSubset->y(); |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 305 | |
| 306 | // Note that here we're returning the special image's entire backing store, loose padding |
| 307 | // and all! |
| 308 | return result->asImage(); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 309 | } |
| 310 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 311 | bool SkImage::isLazyGenerated() const { |
| 312 | return as_IB(this)->onIsLazyGenerated(); |
| 313 | } |
| 314 | |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 315 | bool SkImage::isAlphaOnly() const { |
| 316 | return as_IB(this)->onImageInfo().colorType() == kAlpha_8_SkColorType; |
| 317 | } |
| 318 | |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 319 | sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target, |
| 320 | SkTransferFunctionBehavior premulBehavior) const { |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 321 | SkColorSpaceTransferFn fn; |
| 322 | if (!target || !target->isNumericalTransferFn(&fn)) { |
| 323 | return nullptr; |
| 324 | } |
| 325 | |
Matt Sarett | 73879eb | 2017-03-22 10:07:50 -0400 | [diff] [blame] | 326 | // No need to create a new image if: |
Matt Sarett | d3df9ec | 2017-06-05 10:45:30 -0400 | [diff] [blame] | 327 | // (1) The color spaces are equal. |
Matt Sarett | 73879eb | 2017-03-22 10:07:50 -0400 | [diff] [blame] | 328 | // (2) The color type is kAlpha8. |
Matt Sarett | d3df9ec | 2017-06-05 10:45:30 -0400 | [diff] [blame] | 329 | if (SkColorSpace::Equals(this->colorSpace(), target.get()) || |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 330 | kAlpha_8_SkColorType == as_IB(this)->onImageInfo().colorType()) { |
| 331 | return sk_ref_sp(const_cast<SkImage*>(this)); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 332 | } |
| 333 | |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 334 | SkColorType targetColorType = kN32_SkColorType; |
| 335 | if (SkTransferFunctionBehavior::kRespect == premulBehavior && target->gammaIsLinear()) { |
| 336 | targetColorType = kRGBA_F16_SkColorType; |
| 337 | } |
| 338 | |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 339 | // TODO: We might consider making this a deferred conversion? |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 340 | return as_IB(this)->onMakeColorSpace(std::move(target), targetColorType, premulBehavior); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 341 | } |
| 342 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 343 | ////////////////////////////////////////////////////////////////////////////////////// |
| 344 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 345 | #if !SK_SUPPORT_GPU |
| 346 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 347 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel texels[], |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 348 | int mipLevelCount, SkBudgeted, SkDestinationSurfaceColorMode) { |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 349 | return nullptr; |
| 350 | } |
| 351 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 352 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 353 | sk_sp<SkColorSpace>, TextureReleaseProc, ReleaseContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 354 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 357 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, |
| 358 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
| 359 | SkAlphaType at, sk_sp<SkColorSpace> cs, |
| 360 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
| 361 | return nullptr; |
| 362 | } |
| 363 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 364 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy&, |
| 365 | const DeferredTextureImageUsageParams[], |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 366 | int paramCnt, void* buffer, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 367 | SkColorSpace* dstColorSpace, |
| 368 | SkColorType dstColorType) const { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void*, |
| 373 | SkBudgeted) { |
| 374 | return nullptr; |
| 375 | } |
| 376 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 377 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 378 | SkAlphaType, sk_sp<SkColorSpace>) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 379 | return nullptr; |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 382 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, |
| 383 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
| 384 | SkAlphaType at, sk_sp<SkColorSpace> cs) { |
| 385 | return nullptr; |
| 386 | } |
| 387 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 388 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space, |
| 389 | const GrBackendObject yuvTextureHandles[3], |
| 390 | const SkISize yuvSizes[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 391 | GrSurfaceOrigin origin, |
| 392 | sk_sp<SkColorSpace> imageColorSpace) { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 393 | return nullptr; |
| 394 | } |
| 395 | |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 396 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext*, SkColorSpace* dstColorSpace) const { |
| 397 | return nullptr; |
| 398 | } |
| 399 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 400 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
| 401 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 402 | } |
| 403 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 404 | #endif |
| 405 | |
| 406 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 407 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 408 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel texels[], |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 409 | int mipLevelCount, SkBudgeted) { |
| 410 | return nullptr; |
| 411 | } |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 412 | |
| 413 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 414 | #include "SkImageDeserializer.h" |
| 415 | |
| 416 | sk_sp<SkImage> SkImageDeserializer::makeFromData(SkData* data, const SkIRect* subset) { |
| 417 | return SkImage::MakeFromEncoded(sk_ref_sp(data), subset); |
| 418 | } |
| 419 | sk_sp<SkImage> SkImageDeserializer::makeFromMemory(const void* data, size_t length, |
| 420 | const SkIRect* subset) { |
| 421 | return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length), subset); |
| 422 | } |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 423 | |
| 424 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 425 | |
Derek Sollenberger | d3ea9b7 | 2016-11-09 11:25:13 -0500 | [diff] [blame] | 426 | bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) { |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 427 | SkASSERT(image); |
| 428 | SkASSERT(ctx); |
Derek Sollenberger | d3ea9b7 | 2016-11-09 11:25:13 -0500 | [diff] [blame] | 429 | return as_IB(image)->onPinAsTexture(ctx); |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) { |
| 433 | SkASSERT(image); |
| 434 | SkASSERT(ctx); |
| 435 | as_IB(image)->onUnpinAsTexture(ctx); |
| 436 | } |
Brian Osman | 0d4ff6c | 2017-01-17 16:10:07 -0500 | [diff] [blame] | 437 | |
| 438 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 439 | |
| 440 | sk_sp<SkImage> SkImageMakeRasterCopyAndAssignColorSpace(const SkImage* src, |
| 441 | SkColorSpace* colorSpace) { |
| 442 | // Read the pixels out of the source image, with no conversion |
| 443 | SkImageInfo info = as_IB(src)->onImageInfo(); |
| 444 | if (kUnknown_SkColorType == info.colorType()) { |
| 445 | SkDEBUGFAIL("Unexpected color type"); |
| 446 | return nullptr; |
| 447 | } |
| 448 | |
| 449 | size_t rowBytes = info.minRowBytes(); |
| 450 | size_t size = info.getSafeSize(rowBytes); |
| 451 | auto data = SkData::MakeUninitialized(size); |
| 452 | if (!data) { |
| 453 | return nullptr; |
| 454 | } |
| 455 | |
| 456 | SkPixmap pm(info, data->writable_data(), rowBytes); |
| 457 | if (!src->readPixels(pm, 0, 0, SkImage::kDisallow_CachingHint)) { |
| 458 | return nullptr; |
| 459 | } |
| 460 | |
| 461 | // Wrap them in a new image with a different color space |
| 462 | return SkImage::MakeRasterData(info.makeColorSpace(sk_ref_sp(colorSpace)), data, rowBytes); |
| 463 | } |