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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkData.h" |
| 11 | #include "include/core/SkImageEncoder.h" |
| 12 | #include "include/core/SkImageFilter.h" |
| 13 | #include "include/core/SkImageGenerator.h" |
| 14 | #include "include/core/SkPicture.h" |
| 15 | #include "include/core/SkString.h" |
| 16 | #include "include/core/SkSurface.h" |
| 17 | #include "src/core/SkBitmapCache.h" |
| 18 | #include "src/core/SkCachedData.h" |
| 19 | #include "src/core/SkColorSpacePriv.h" |
| 20 | #include "src/core/SkImageFilterCache.h" |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 21 | #include "src/core/SkImageFilter_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkImagePriv.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 23 | #include "src/core/SkMipmap.h" |
Mike Reed | 6affa2c | 2020-11-30 13:09:24 -0500 | [diff] [blame] | 24 | #include "src/core/SkMipmapBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/core/SkNextID.h" |
| 26 | #include "src/core/SkSpecialImage.h" |
| 27 | #include "src/image/SkImage_Base.h" |
| 28 | #include "src/image/SkReadPixelsRec.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 29 | #include "src/image/SkRescaleAndReadPixels.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/shaders/SkImageShader.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 31 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 32 | #if SK_SUPPORT_GPU |
Robert Phillips | 4a3ebc2 | 2020-07-10 11:27:43 -0400 | [diff] [blame] | 33 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 34 | #include "src/gpu/GrDirectContextPriv.h" |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 35 | #include "src/gpu/GrImageContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/image/SkImage_Gpu.h" |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 37 | #endif |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 38 | #include "include/gpu/GrBackendSurface.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 39 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 40 | SkImage::SkImage(const SkImageInfo& info, uint32_t uniqueID) |
| 41 | : fInfo(info) |
| 42 | , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) { |
| 43 | SkASSERT(info.width() > 0); |
| 44 | SkASSERT(info.height() > 0); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 45 | } |
| 46 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 47 | bool SkImage::peekPixels(SkPixmap* pm) const { |
| 48 | SkPixmap tmp; |
| 49 | if (!pm) { |
| 50 | pm = &tmp; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 51 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 52 | return as_IB(this)->onPeekPixels(pm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 55 | bool SkImage::readPixels(GrDirectContext* dContext, const SkImageInfo& dstInfo, void* dstPixels, |
| 56 | size_t dstRowBytes, int srcX, int srcY, CachingHint chint) const { |
| 57 | return as_IB(this)->onReadPixels(dContext, dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 60 | #ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API |
| 61 | bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, |
| 62 | size_t dstRowBytes, int srcX, int srcY, CachingHint chint) const { |
| 63 | auto dContext = as_IB(this)->directContext(); |
| 64 | return this->readPixels(dContext, dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint); |
| 65 | } |
| 66 | #endif |
| 67 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 68 | void SkImage::asyncRescaleAndReadPixels(const SkImageInfo& info, |
| 69 | const SkIRect& srcRect, |
| 70 | RescaleGamma rescaleGamma, |
| 71 | SkFilterQuality rescaleQuality, |
| 72 | ReadPixelsCallback callback, |
| 73 | ReadPixelsContext context) { |
| 74 | if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || |
| 75 | !SkImageInfoIsValid(info)) { |
| 76 | callback(context, nullptr); |
| 77 | return; |
| 78 | } |
| 79 | as_IB(this)->onAsyncRescaleAndReadPixels( |
| 80 | info, srcRect, rescaleGamma, rescaleQuality, callback, context); |
| 81 | } |
| 82 | |
| 83 | void SkImage::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, |
| 84 | sk_sp<SkColorSpace> dstColorSpace, |
| 85 | const SkIRect& srcRect, |
| 86 | const SkISize& dstSize, |
| 87 | RescaleGamma rescaleGamma, |
| 88 | SkFilterQuality rescaleQuality, |
| 89 | ReadPixelsCallback callback, |
| 90 | ReadPixelsContext context) { |
| 91 | if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() || |
| 92 | (dstSize.width() & 0b1) || (dstSize.height() & 0b1)) { |
| 93 | callback(context, nullptr); |
| 94 | return; |
| 95 | } |
| 96 | as_IB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace, |
| 97 | std::move(dstColorSpace), |
| 98 | srcRect, |
| 99 | dstSize, |
| 100 | rescaleGamma, |
| 101 | rescaleQuality, |
| 102 | callback, |
| 103 | context); |
| 104 | } |
| 105 | |
Mike Reed | ce0c876 | 2020-11-23 17:41:35 -0500 | [diff] [blame] | 106 | bool SkImage::scalePixels(const SkPixmap& dst, const SkSamplingOptions& sampling, |
| 107 | CachingHint chint) const { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 108 | // Context TODO: Elevate GrDirectContext requirement to public API. |
| 109 | auto dContext = as_IB(this)->directContext(); |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 110 | if (this->width() == dst.width() && this->height() == dst.height()) { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 111 | return this->readPixels(dContext, dst, 0, 0, chint); |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 112 | } |
| 113 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 114 | // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself |
| 115 | // can scale more efficiently) we should take advantage of it here. |
| 116 | // |
| 117 | SkBitmap bm; |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 118 | if (as_IB(this)->getROPixels(dContext, &bm, chint)) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 119 | SkPixmap pmap; |
| 120 | // Note: By calling the pixmap scaler, we never cache the final result, so the chint |
| 121 | // is (currently) only being applied to the getROPixels. If we get a request to |
| 122 | // also attempt to cache the final (scaled) result, we would add that logic here. |
| 123 | // |
Mike Reed | ce0c876 | 2020-11-23 17:41:35 -0500 | [diff] [blame] | 124 | return bm.peekPixels(&pmap) && pmap.scalePixels(dst, sampling); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 125 | } |
| 126 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 127 | } |
| 128 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 130 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 131 | SkColorType SkImage::colorType() const { return fInfo.colorType(); } |
Greg Daniel | 56008aa | 2018-03-14 15:33:42 -0400 | [diff] [blame] | 132 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 133 | SkAlphaType SkImage::alphaType() const { return fInfo.alphaType(); } |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 134 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 135 | SkColorSpace* SkImage::colorSpace() const { return fInfo.colorSpace(); } |
Matt Sarett | 5b4599f | 2017-03-02 12:42:35 -0500 | [diff] [blame] | 136 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 137 | sk_sp<SkColorSpace> SkImage::refColorSpace() const { return fInfo.refColorSpace(); } |
Matt Sarett | 5b4599f | 2017-03-02 12:42:35 -0500 | [diff] [blame] | 138 | |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 139 | #ifdef SK_SUPPORT_LEGACY_IMPLICIT_FILTERQUALITY |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 140 | sk_sp<SkShader> SkImage::makeShader(SkTileMode tmx, SkTileMode tmy, |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 141 | const SkMatrix* localMatrix) const { |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 142 | const SkSamplingOptions* inherit_from_paint = nullptr; |
| 143 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy, inherit_from_paint, |
| 144 | localMatrix); |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 145 | } |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 146 | #endif |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 147 | |
Mike Reed | b86cd3d | 2020-12-10 14:55:43 -0500 | [diff] [blame] | 148 | sk_sp<SkShader> SkImage_makeShaderImplicitFilterQuality(const SkImage* image, |
| 149 | SkTileMode tmx, SkTileMode tmy, |
| 150 | const SkMatrix* localMatrix) { |
| 151 | const SkSamplingOptions* inherit_from_paint = nullptr; |
| 152 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(image)), tmx, tmy, inherit_from_paint, |
| 153 | localMatrix); |
| 154 | } |
| 155 | |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 156 | sk_sp<SkShader> SkImage::makeShader(SkTileMode tmx, SkTileMode tmy, |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 157 | const SkSamplingOptions& sampling, |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 158 | const SkMatrix* localMatrix) const { |
| 159 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy, |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 160 | &sampling, localMatrix); |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 163 | sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 164 | // Context TODO: Elevate GrDirectContext requirement to public API. |
| 165 | auto dContext = as_IB(this)->directContext(); |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 166 | SkBitmap bm; |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 167 | if (as_IB(this)->getROPixels(dContext, &bm)) { |
Mike Reed | 25eef6b | 2017-12-08 16:20:58 -0500 | [diff] [blame] | 168 | return SkEncodeBitmap(bm, type, quality); |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 169 | } |
| 170 | return nullptr; |
| 171 | } |
| 172 | |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 173 | sk_sp<SkData> SkImage::encodeToData() const { |
| 174 | if (auto encoded = this->refEncodedData()) { |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 175 | return encoded; |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Brian Osman | e50cdf0 | 2018-10-19 13:02:14 -0400 | [diff] [blame] | 178 | return this->encodeToData(SkEncodedImageFormat::kPNG, 100); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 181 | sk_sp<SkData> SkImage::refEncodedData() const { |
| 182 | return sk_sp<SkData>(as_IB(this)->onRefEncoded()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Mike Reed | 564d49e | 2020-07-28 12:52:31 -0400 | [diff] [blame] | 185 | sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 186 | if (nullptr == encoded || 0 == encoded->size()) { |
| 187 | return nullptr; |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 188 | } |
Mike Reed | 564d49e | 2020-07-28 12:52:31 -0400 | [diff] [blame] | 189 | return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(std::move(encoded))); |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 192 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 193 | |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 194 | sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset, GrDirectContext* direct) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 195 | if (subset.isEmpty()) { |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
| 199 | const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 200 | if (!bounds.contains(subset)) { |
| 201 | return nullptr; |
| 202 | } |
| 203 | |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 204 | #if SK_SUPPORT_GPU |
| 205 | auto myContext = as_IB(this)->context(); |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 206 | // This check is also performed in the subclass, but we do it here for the short-circuit below. |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 207 | if (myContext && !myContext->priv().matches(direct)) { |
| 208 | return nullptr; |
| 209 | } |
| 210 | #endif |
| 211 | |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 212 | // optimization : return self if the subset == our bounds |
| 213 | if (bounds == subset) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 214 | return sk_ref_sp(const_cast<SkImage*>(this)); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 215 | } |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 216 | |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 217 | return as_IB(this)->onMakeSubset(subset, direct); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 218 | } |
| 219 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 220 | #if SK_SUPPORT_GPU |
| 221 | |
Jim Van Verth | 21bd60d | 2018-10-12 15:00:20 -0400 | [diff] [blame] | 222 | bool SkImage::isTextureBacked() const { return as_IB(this)->onIsTextureBacked(); } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 223 | |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 224 | GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO, |
| 225 | GrSurfaceOrigin* origin) const { |
| 226 | return as_IB(this)->onGetBackendTexture(flushPendingGrContextIO, origin); |
| 227 | } |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 228 | |
Adlai Holler | 0137355 | 2020-07-21 13:52:41 -0400 | [diff] [blame] | 229 | bool SkImage::isValid(GrRecordingContext* rContext) const { |
| 230 | if (rContext && rContext->abandoned()) { |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 231 | return false; |
| 232 | } |
Adlai Holler | 0137355 | 2020-07-21 13:52:41 -0400 | [diff] [blame] | 233 | return as_IB(this)->onIsValid(rContext); |
Robert Phillips | 4a3ebc2 | 2020-07-10 11:27:43 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Adlai Holler | 40ad5fd | 2020-07-22 10:22:12 -0400 | [diff] [blame] | 236 | GrSemaphoresSubmitted SkImage::flush(GrDirectContext* dContext, const GrFlushInfo& flushInfo) { |
| 237 | return as_IB(this)->onFlush(dContext, flushInfo); |
| 238 | } |
| 239 | |
| 240 | void SkImage::flushAndSubmit(GrDirectContext* dContext) { |
| 241 | this->flush(dContext, {}); |
| 242 | dContext->submit(); |
| 243 | } |
| 244 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 245 | #else |
| 246 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 247 | bool SkImage::isTextureBacked() const { return false; } |
| 248 | |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 249 | GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO, |
| 250 | GrSurfaceOrigin* origin) const { |
| 251 | return GrBackendTexture(); // invalid |
| 252 | } |
| 253 | |
Adlai Holler | 0137355 | 2020-07-21 13:52:41 -0400 | [diff] [blame] | 254 | bool SkImage::isValid(GrRecordingContext* rContext) const { |
| 255 | if (rContext) { |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 256 | return false; |
| 257 | } |
Robert Phillips | 4a3ebc2 | 2020-07-10 11:27:43 -0400 | [diff] [blame] | 258 | return as_IB(this)->onIsValid(nullptr); |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Adlai Holler | 40ad5fd | 2020-07-22 10:22:12 -0400 | [diff] [blame] | 261 | GrSemaphoresSubmitted SkImage::flush(GrDirectContext*, const GrFlushInfo&) { |
| 262 | return GrSemaphoresSubmitted::kNo; |
| 263 | } |
| 264 | |
| 265 | void SkImage::flushAndSubmit(GrDirectContext*) {} |
| 266 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 267 | #endif |
| 268 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 269 | /////////////////////////////////////////////////////////////////////////////// |
| 270 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 271 | SkImage_Base::SkImage_Base(const SkImageInfo& info, uint32_t uniqueID) |
| 272 | : INHERITED(info, uniqueID), fAddedToRasterCache(false) {} |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 273 | |
| 274 | SkImage_Base::~SkImage_Base() { |
Mike Reed | 30301c4 | 2018-07-19 09:39:21 -0400 | [diff] [blame] | 275 | if (fAddedToRasterCache.load()) { |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 276 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 277 | } |
| 278 | } |
| 279 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 280 | void SkImage_Base::onAsyncRescaleAndReadPixels(const SkImageInfo& info, |
| 281 | const SkIRect& origSrcRect, |
| 282 | RescaleGamma rescaleGamma, |
| 283 | SkFilterQuality rescaleQuality, |
| 284 | ReadPixelsCallback callback, |
| 285 | ReadPixelsContext context) { |
| 286 | SkBitmap src; |
| 287 | SkPixmap peek; |
| 288 | SkIRect srcRect; |
| 289 | if (this->peekPixels(&peek)) { |
| 290 | src.installPixels(peek); |
| 291 | srcRect = origSrcRect; |
| 292 | } else { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 293 | // Context TODO: Elevate GrDirectContext requirement to public API. |
| 294 | auto dContext = as_IB(this)->directContext(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 295 | src.setInfo(this->imageInfo().makeDimensions(origSrcRect.size())); |
| 296 | src.allocPixels(); |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 297 | if (!this->readPixels(dContext, src.pixmap(), origSrcRect.x(), origSrcRect.y())) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 298 | callback(context, nullptr); |
| 299 | return; |
| 300 | } |
| 301 | srcRect = SkIRect::MakeSize(src.dimensions()); |
| 302 | } |
| 303 | return SkRescaleAndReadPixels( |
| 304 | src, info, srcRect, rescaleGamma, rescaleQuality, callback, context); |
| 305 | } |
| 306 | |
| 307 | void SkImage_Base::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, |
| 308 | sk_sp<SkColorSpace> dstColorSpace, |
| 309 | const SkIRect& srcRect, |
| 310 | const SkISize& dstSize, |
| 311 | RescaleGamma, |
| 312 | SkFilterQuality, |
| 313 | ReadPixelsCallback callback, |
| 314 | ReadPixelsContext context) { |
| 315 | // TODO: Call non-YUV asyncRescaleAndReadPixels and then make our callback convert to YUV and |
| 316 | // call client's callback. |
| 317 | callback(context, nullptr); |
| 318 | } |
| 319 | |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 320 | GrBackendTexture SkImage_Base::onGetBackendTexture(bool flushPendingGrContextIO, |
| 321 | GrSurfaceOrigin* origin) const { |
| 322 | return GrBackendTexture(); // invalid |
| 323 | } |
| 324 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 325 | GrDirectContext* SkImage_Base::directContext() const { |
| 326 | #if SK_SUPPORT_GPU |
| 327 | return GrAsDirectContext(this->context()); |
| 328 | #else |
| 329 | return nullptr; |
| 330 | #endif |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 333 | bool SkImage::readPixels(GrDirectContext* dContext, const SkPixmap& pmap, int srcX, int srcY, |
| 334 | CachingHint chint) const { |
| 335 | return this->readPixels(dContext, pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, |
| 336 | srcY, chint); |
| 337 | } |
| 338 | |
| 339 | #ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API |
| 340 | bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const { |
| 341 | auto dContext = as_IB(this)->directContext(); |
| 342 | return this->readPixels(dContext, pmap, srcX, srcY, chint); |
| 343 | } |
| 344 | #endif |
| 345 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 346 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 347 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 348 | sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) { |
Brian Osman | 41ba826 | 2018-10-19 14:56:26 -0400 | [diff] [blame] | 349 | if (!bm.pixelRef()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 350 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 351 | } |
| 352 | |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 353 | return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 356 | bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode ) const { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 357 | // Context TODO: Elevate GrDirectContext requirement to public API. |
| 358 | auto dContext = as_IB(this)->directContext(); |
| 359 | return as_IB(this)->onAsLegacyBitmap(dContext, bitmap); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 362 | bool SkImage_Base::onAsLegacyBitmap(GrDirectContext* dContext, SkBitmap* bitmap) const { |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 363 | // As the base-class, all we can do is make a copy (regardless of mode). |
| 364 | // Subclasses that want to be more optimal should override. |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 365 | SkImageInfo info = fInfo.makeColorType(kN32_SkColorType).makeColorSpace(nullptr); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 366 | if (!bitmap->tryAllocPixels(info)) { |
| 367 | return false; |
| 368 | } |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 369 | |
| 370 | if (!this->readPixels(dContext, bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), |
| 371 | 0, 0)) { |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 372 | bitmap->reset(); |
| 373 | return false; |
| 374 | } |
| 375 | |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 376 | bitmap->setImmutable(); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 380 | sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 381 | const SkMatrix* matrix, const SkPaint* paint, |
| 382 | BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 383 | return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture), |
| 384 | matrix, paint, bitDepth, |
| 385 | std::move(colorSpace))); |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 386 | } |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 387 | |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 388 | sk_sp<SkImage> SkImage::makeWithFilter(GrRecordingContext* rContext, const SkImageFilter* filter, |
| 389 | const SkIRect& subset, const SkIRect& clipBounds, |
| 390 | SkIRect* outSubset, SkIPoint* offset) const { |
Robert Phillips | 2746765 | 2019-01-10 16:34:22 -0500 | [diff] [blame] | 391 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 392 | if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) { |
| 393 | return nullptr; |
| 394 | } |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 395 | sk_sp<SkSpecialImage> srcSpecialImage; |
Robert Phillips | bc42944 | 2019-02-20 08:26:03 -0500 | [diff] [blame] | 396 | #if SK_SUPPORT_GPU |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 397 | auto myContext = as_IB(this)->context(); |
| 398 | if (myContext && !myContext->priv().matches(rContext)) { |
| 399 | return nullptr; |
| 400 | } |
| 401 | srcSpecialImage = SkSpecialImage::MakeFromImage(rContext, subset, |
| 402 | sk_ref_sp(const_cast<SkImage*>(this))); |
Robert Phillips | bc42944 | 2019-02-20 08:26:03 -0500 | [diff] [blame] | 403 | #else |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 404 | srcSpecialImage = SkSpecialImage::MakeFromImage(nullptr, subset, |
| 405 | sk_ref_sp(const_cast<SkImage*>(this))); |
Robert Phillips | bc42944 | 2019-02-20 08:26:03 -0500 | [diff] [blame] | 406 | #endif |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 407 | if (!srcSpecialImage) { |
| 408 | return nullptr; |
| 409 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 410 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 411 | sk_sp<SkImageFilterCache> cache( |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 412 | SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 413 | |
| 414 | // The filters operate in the local space of the src image, where (0,0) corresponds to the |
| 415 | // subset's top left corner. But the clip bounds and any crop rects on the filters are in the |
| 416 | // original coordinate system, so configure the CTM to correct crop rects and explicitly adjust |
| 417 | // the clip bounds (since it is assumed to already be in image space). |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 418 | SkImageFilter_Base::Context context(SkMatrix::Translate(-subset.x(), -subset.y()), |
Brian Salomon | 9bd947d | 2019-10-03 14:57:07 -0400 | [diff] [blame] | 419 | clipBounds.makeOffset(-subset.topLeft()), |
Michael Ludwig | e30a485 | 2019-08-14 14:35:42 -0400 | [diff] [blame] | 420 | cache.get(), fInfo.colorType(), fInfo.colorSpace(), |
| 421 | srcSpecialImage.get()); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 422 | |
Michael Ludwig | ea07123 | 2019-08-26 10:52:15 -0400 | [diff] [blame] | 423 | sk_sp<SkSpecialImage> result = as_IFB(filter)->filterImage(context).imageAndOffset(offset); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 424 | if (!result) { |
| 425 | return nullptr; |
| 426 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 427 | |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 428 | // The output image and offset are relative to the subset rectangle, so the offset needs to |
| 429 | // be shifted to put it in the correct spot with respect to the original coordinate system |
| 430 | offset->fX += subset.x(); |
| 431 | offset->fY += subset.y(); |
| 432 | |
| 433 | // Final clip against the exact clipBounds (the clip provided in the context gets adjusted |
| 434 | // to account for pixel-moving filters so doesn't always exactly match when finished). The |
| 435 | // clipBounds are translated into the clippedDstRect coordinate space, including the |
| 436 | // result->subset() ensures that the result's image pixel origin does not affect results. |
| 437 | SkIRect dstRect = result->subset(); |
| 438 | SkIRect clippedDstRect = dstRect; |
Brian Salomon | 9bd947d | 2019-10-03 14:57:07 -0400 | [diff] [blame] | 439 | if (!clippedDstRect.intersect(clipBounds.makeOffset(result->subset().topLeft() - *offset))) { |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 440 | return nullptr; |
| 441 | } |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 442 | |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 443 | // Adjust the geometric offset if the top-left corner moved as well |
| 444 | offset->fX += (clippedDstRect.x() - dstRect.x()); |
| 445 | offset->fY += (clippedDstRect.y() - dstRect.y()); |
| 446 | *outSubset = clippedDstRect; |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 447 | return result->asImage(); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 448 | } |
| 449 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 450 | bool SkImage::isLazyGenerated() const { |
| 451 | return as_IB(this)->onIsLazyGenerated(); |
| 452 | } |
| 453 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 454 | bool SkImage::isAlphaOnly() const { return SkColorTypeIsAlphaOnly(fInfo.colorType()); } |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 455 | |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 456 | sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target, GrDirectContext* direct) const { |
| 457 | return this->makeColorTypeAndColorSpace(this->colorType(), std::move(target), direct); |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | sk_sp<SkImage> SkImage::makeColorTypeAndColorSpace(SkColorType targetColorType, |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 461 | sk_sp<SkColorSpace> targetColorSpace, |
Adlai Holler | 356bcb6 | 2020-08-14 11:09:30 -0400 | [diff] [blame] | 462 | GrDirectContext* dContext) const { |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 463 | if (kUnknown_SkColorType == targetColorType || !targetColorSpace) { |
| 464 | return nullptr; |
| 465 | } |
| 466 | |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 467 | #if SK_SUPPORT_GPU |
| 468 | auto myContext = as_IB(this)->context(); |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 469 | // This check is also performed in the subclass, but we do it here for the short-circuit below. |
Adlai Holler | 356bcb6 | 2020-08-14 11:09:30 -0400 | [diff] [blame] | 470 | if (myContext && !myContext->priv().matches(dContext)) { |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 471 | return nullptr; |
| 472 | } |
| 473 | #endif |
| 474 | |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 475 | SkColorType colorType = this->colorType(); |
| 476 | SkColorSpace* colorSpace = this->colorSpace(); |
| 477 | if (!colorSpace) { |
| 478 | colorSpace = sk_srgb_singleton(); |
| 479 | } |
| 480 | if (colorType == targetColorType && |
| 481 | (SkColorSpace::Equals(colorSpace, targetColorSpace.get()) || this->isAlphaOnly())) { |
| 482 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 483 | } |
| 484 | |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 485 | return as_IB(this)->onMakeColorTypeAndColorSpace(targetColorType, |
Adlai Holler | 356bcb6 | 2020-08-14 11:09:30 -0400 | [diff] [blame] | 486 | std::move(targetColorSpace), dContext); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 487 | } |
| 488 | |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 489 | sk_sp<SkImage> SkImage::reinterpretColorSpace(sk_sp<SkColorSpace> target) const { |
| 490 | if (!target) { |
| 491 | return nullptr; |
| 492 | } |
| 493 | |
| 494 | // No need to create a new image if: |
| 495 | // (1) The color spaces are equal. |
| 496 | // (2) The color type is kAlpha8. |
| 497 | SkColorSpace* colorSpace = this->colorSpace(); |
| 498 | if (!colorSpace) { |
| 499 | colorSpace = sk_srgb_singleton(); |
| 500 | } |
| 501 | if (SkColorSpace::Equals(colorSpace, target.get()) || this->isAlphaOnly()) { |
| 502 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 503 | } |
| 504 | |
| 505 | return as_IB(this)->onReinterpretColorSpace(std::move(target)); |
| 506 | } |
| 507 | |
Mike Reed | b950b59 | 2018-01-02 10:16:12 -0500 | [diff] [blame] | 508 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
| 509 | if (!this->isTextureBacked()) { |
| 510 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 511 | } |
| 512 | return this->makeRasterImage(); |
| 513 | } |
| 514 | |
Brian Osman | a88cab1 | 2020-01-09 10:21:44 -0500 | [diff] [blame] | 515 | sk_sp<SkImage> SkImage::makeRasterImage(CachingHint chint) const { |
Mike Reed | f7ee95c | 2017-12-04 14:27:01 -0500 | [diff] [blame] | 516 | SkPixmap pm; |
| 517 | if (this->peekPixels(&pm)) { |
| 518 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 519 | } |
| 520 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 521 | const size_t rowBytes = fInfo.minRowBytes(); |
| 522 | size_t size = fInfo.computeByteSize(rowBytes); |
Mike Reed | f7ee95c | 2017-12-04 14:27:01 -0500 | [diff] [blame] | 523 | if (SkImageInfo::ByteSizeOverflowed(size)) { |
| 524 | return nullptr; |
| 525 | } |
| 526 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 527 | // Context TODO: Elevate GrDirectContext requirement to public API. |
| 528 | auto dContext = as_IB(this)->directContext(); |
Mike Reed | f7ee95c | 2017-12-04 14:27:01 -0500 | [diff] [blame] | 529 | sk_sp<SkData> data = SkData::MakeUninitialized(size); |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 530 | pm = {fInfo.makeColorSpace(nullptr), data->writable_data(), fInfo.minRowBytes()}; |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 531 | if (!this->readPixels(dContext, pm, 0, 0, chint)) { |
Mike Reed | f7ee95c | 2017-12-04 14:27:01 -0500 | [diff] [blame] | 532 | return nullptr; |
| 533 | } |
| 534 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 535 | return SkImage::MakeRasterData(fInfo, std::move(data), rowBytes); |
Mike Reed | f7ee95c | 2017-12-04 14:27:01 -0500 | [diff] [blame] | 536 | } |
| 537 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 538 | ////////////////////////////////////////////////////////////////////////////////////// |
| 539 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 540 | #if !SK_SUPPORT_GPU |
| 541 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 542 | sk_sp<SkImage> SkImage::MakeFromTexture(GrRecordingContext*, |
| 543 | const GrBackendTexture&, GrSurfaceOrigin, |
| 544 | SkColorType, SkAlphaType, sk_sp<SkColorSpace>, |
| 545 | TextureReleaseProc, ReleaseContext) { |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 546 | return nullptr; |
| 547 | } |
| 548 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 549 | sk_sp<SkImage> SkImage::MakeFromCompressedTexture(GrRecordingContext*, |
| 550 | const GrBackendTexture&, |
| 551 | GrSurfaceOrigin, |
| 552 | SkAlphaType, |
| 553 | sk_sp<SkColorSpace>, |
| 554 | TextureReleaseProc, |
| 555 | ReleaseContext) { |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 556 | return nullptr; |
| 557 | } |
| 558 | |
Adlai Holler | b270568 | 2020-10-20 10:11:53 -0400 | [diff] [blame] | 559 | bool SkImage::MakeBackendTextureFromSkImage(GrDirectContext*, |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 560 | sk_sp<SkImage>, |
| 561 | GrBackendTexture*, |
| 562 | BackendTextureReleaseProc*) { |
| 563 | return false; |
| 564 | } |
| 565 | |
Adlai Holler | f7129fc | 2020-08-04 14:07:40 -0400 | [diff] [blame] | 566 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrRecordingContext*, |
Adlai Holler | e34b282 | 2020-07-29 12:50:56 -0400 | [diff] [blame] | 567 | const GrBackendTexture&, GrSurfaceOrigin, |
| 568 | SkColorType, SkAlphaType, |
| 569 | sk_sp<SkColorSpace>) { |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 570 | return nullptr; |
| 571 | } |
| 572 | |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 573 | sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps(GrRecordingContext* context, |
| 574 | const SkYUVAPixmaps& pixmaps, |
| 575 | GrMipMapped buildMips, |
| 576 | bool limitToMaxTextureSize, |
| 577 | sk_sp<SkColorSpace> imageColorSpace) { |
| 578 | return nullptr; |
| 579 | } |
| 580 | |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 581 | sk_sp<SkImage> SkImage::makeTextureImage(GrDirectContext*, GrMipmapped, SkBudgeted) const { |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 582 | return nullptr; |
| 583 | } |
Adlai Holler | c0ca856 | 2020-07-28 10:23:33 -0400 | [diff] [blame] | 584 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 585 | #endif |
| 586 | |
| 587 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 588 | |
Adlai Holler | 2940538 | 2020-07-20 16:02:05 -0400 | [diff] [blame] | 589 | bool SkImage_pinAsTexture(const SkImage* image, GrRecordingContext* rContext) { |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 590 | SkASSERT(image); |
Adlai Holler | 2940538 | 2020-07-20 16:02:05 -0400 | [diff] [blame] | 591 | SkASSERT(rContext); |
| 592 | return as_IB(image)->onPinAsTexture(rContext); |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Adlai Holler | 2940538 | 2020-07-20 16:02:05 -0400 | [diff] [blame] | 595 | void SkImage_unpinAsTexture(const SkImage* image, GrRecordingContext* rContext) { |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 596 | SkASSERT(image); |
Adlai Holler | 2940538 | 2020-07-20 16:02:05 -0400 | [diff] [blame] | 597 | SkASSERT(rContext); |
| 598 | as_IB(image)->onUnpinAsTexture(rContext); |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 599 | } |
Brian Osman | 0d4ff6c | 2017-01-17 16:10:07 -0500 | [diff] [blame] | 600 | |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 601 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 602 | |
| 603 | SkMipmapBuilder::SkMipmapBuilder(const SkImageInfo& info) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 604 | fMM = sk_sp<SkMipmap>(SkMipmap::Build({info, nullptr, 0}, nullptr, false)); |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | SkMipmapBuilder::~SkMipmapBuilder() {} |
| 608 | |
| 609 | int SkMipmapBuilder::countLevels() const { |
| 610 | return fMM ? fMM->countLevels() : 0; |
| 611 | } |
| 612 | |
| 613 | SkPixmap SkMipmapBuilder::level(int index) const { |
| 614 | SkPixmap pm; |
| 615 | |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 616 | SkMipmap::Level level; |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 617 | if (fMM && fMM->getLevel(index, &level)) { |
| 618 | pm = level.fPixmap; |
| 619 | } |
| 620 | return pm; |
| 621 | } |
| 622 | |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 623 | bool SkImage::hasMipmaps() const { |
| 624 | return as_IB(this)->onPeekMips() != nullptr; |
| 625 | } |
| 626 | |
Mike Reed | 1529295 | 2020-07-25 12:47:14 -0400 | [diff] [blame] | 627 | sk_sp<SkImage> SkImage::withMipmaps(sk_sp<SkMipmap> mips) const { |
| 628 | if (mips == nullptr || mips->validForRootLevel(this->imageInfo())) { |
| 629 | if (auto result = as_IB(this)->onMakeWithMipmaps(std::move(mips))) { |
| 630 | return result; |
| 631 | } |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 632 | } |
Mike Reed | 1529295 | 2020-07-25 12:47:14 -0400 | [diff] [blame] | 633 | return sk_ref_sp((const_cast<SkImage*>(this))); |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 634 | } |
Mike Reed | 186866c | 2020-09-12 10:38:32 -0400 | [diff] [blame] | 635 | |
| 636 | sk_sp<SkImage> SkImage::withDefaultMipmaps() const { |
| 637 | return this->withMipmaps(nullptr); |
| 638 | } |
Mike Reed | b4aa639 | 2020-09-17 13:58:26 -0400 | [diff] [blame] | 639 | |
| 640 | sk_sp<SkImage> SkMipmapBuilder::attachTo(const SkImage* src) { |
| 641 | return src->withMipmaps(fMM); |
| 642 | } |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 643 | |
Mike Reed | 9a27566 | 2020-12-26 21:48:57 -0500 | [diff] [blame^] | 644 | SkSamplingOptions::SkSamplingOptions(SkFilterQuality fq, MediumBehavior behavior) { |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 645 | switch (fq) { |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 646 | case SkFilterQuality::kHigh_SkFilterQuality: |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 647 | *this = SkSamplingOptions(SkCubicResampler{1/3.0f, 1/3.0f}); |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 648 | break; |
| 649 | case SkFilterQuality::kMedium_SkFilterQuality: |
Mike Reed | 9a27566 | 2020-12-26 21:48:57 -0500 | [diff] [blame^] | 650 | *this = SkSamplingOptions(SkFilterMode::kLinear, |
| 651 | behavior == kMedium_asMipmapNearest ? SkMipmapMode::kNearest |
| 652 | : SkMipmapMode::kLinear); |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 653 | break; |
| 654 | case SkFilterQuality::kLow_SkFilterQuality: |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 655 | *this = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone); |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 656 | break; |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 657 | case SkFilterQuality::kNone_SkFilterQuality: |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 658 | *this = SkSamplingOptions(SkFilterMode::kNearest, SkMipmapMode::kNone); |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 659 | break; |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 660 | } |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 661 | } |