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