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