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