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" |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 36 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "src/image/SkImage_Gpu.h" |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 38 | #endif |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 39 | #include "include/gpu/GrBackendSurface.h" |
Adlai Holler | c44e21f | 2021-02-22 14:08:07 -0500 | [diff] [blame] | 40 | #include "include/gpu/GrContextThreadSafeProxy.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 41 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 42 | SkImage::SkImage(const SkImageInfo& info, uint32_t uniqueID) |
| 43 | : fInfo(info) |
| 44 | , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) { |
| 45 | SkASSERT(info.width() > 0); |
| 46 | SkASSERT(info.height() > 0); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 47 | } |
| 48 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 49 | bool SkImage::peekPixels(SkPixmap* pm) const { |
| 50 | SkPixmap tmp; |
| 51 | if (!pm) { |
| 52 | pm = &tmp; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 53 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 54 | return as_IB(this)->onPeekPixels(pm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 57 | bool SkImage::readPixels(GrDirectContext* dContext, const SkImageInfo& dstInfo, void* dstPixels, |
| 58 | size_t dstRowBytes, int srcX, int srcY, CachingHint chint) const { |
| 59 | return as_IB(this)->onReadPixels(dContext, dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 62 | #ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API |
| 63 | bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, |
| 64 | size_t dstRowBytes, int srcX, int srcY, CachingHint chint) const { |
| 65 | auto dContext = as_IB(this)->directContext(); |
| 66 | return this->readPixels(dContext, dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint); |
| 67 | } |
| 68 | #endif |
| 69 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 70 | void SkImage::asyncRescaleAndReadPixels(const SkImageInfo& info, |
| 71 | const SkIRect& srcRect, |
| 72 | RescaleGamma rescaleGamma, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame] | 73 | RescaleMode rescaleMode, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 74 | ReadPixelsCallback callback, |
| 75 | ReadPixelsContext context) { |
| 76 | if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || |
| 77 | !SkImageInfoIsValid(info)) { |
| 78 | callback(context, nullptr); |
| 79 | return; |
| 80 | } |
| 81 | as_IB(this)->onAsyncRescaleAndReadPixels( |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame] | 82 | info, srcRect, rescaleGamma, rescaleMode, callback, context); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void SkImage::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, |
| 86 | sk_sp<SkColorSpace> dstColorSpace, |
| 87 | const SkIRect& srcRect, |
| 88 | const SkISize& dstSize, |
| 89 | RescaleGamma rescaleGamma, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame] | 90 | RescaleMode rescaleMode, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 91 | ReadPixelsCallback callback, |
| 92 | ReadPixelsContext context) { |
| 93 | if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() || |
| 94 | (dstSize.width() & 0b1) || (dstSize.height() & 0b1)) { |
| 95 | callback(context, nullptr); |
| 96 | return; |
| 97 | } |
| 98 | as_IB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace, |
| 99 | std::move(dstColorSpace), |
| 100 | srcRect, |
| 101 | dstSize, |
| 102 | rescaleGamma, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame] | 103 | rescaleMode, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 104 | callback, |
| 105 | context); |
| 106 | } |
| 107 | |
Mike Reed | ce0c876 | 2020-11-23 17:41:35 -0500 | [diff] [blame] | 108 | bool SkImage::scalePixels(const SkPixmap& dst, const SkSamplingOptions& sampling, |
| 109 | CachingHint chint) const { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 110 | // Context TODO: Elevate GrDirectContext requirement to public API. |
| 111 | auto dContext = as_IB(this)->directContext(); |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 112 | if (this->width() == dst.width() && this->height() == dst.height()) { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 113 | return this->readPixels(dContext, dst, 0, 0, chint); |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 114 | } |
| 115 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 116 | // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself |
| 117 | // can scale more efficiently) we should take advantage of it here. |
| 118 | // |
| 119 | SkBitmap bm; |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 120 | if (as_IB(this)->getROPixels(dContext, &bm, chint)) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 121 | SkPixmap pmap; |
| 122 | // Note: By calling the pixmap scaler, we never cache the final result, so the chint |
| 123 | // is (currently) only being applied to the getROPixels. If we get a request to |
| 124 | // also attempt to cache the final (scaled) result, we would add that logic here. |
| 125 | // |
Mike Reed | ce0c876 | 2020-11-23 17:41:35 -0500 | [diff] [blame] | 126 | return bm.peekPixels(&pmap) && pmap.scalePixels(dst, sampling); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 127 | } |
| 128 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 129 | } |
| 130 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 131 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 132 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 133 | SkColorType SkImage::colorType() const { return fInfo.colorType(); } |
Greg Daniel | 56008aa | 2018-03-14 15:33:42 -0400 | [diff] [blame] | 134 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 135 | SkAlphaType SkImage::alphaType() const { return fInfo.alphaType(); } |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 136 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 137 | SkColorSpace* SkImage::colorSpace() const { return fInfo.colorSpace(); } |
Matt Sarett | 5b4599f | 2017-03-02 12:42:35 -0500 | [diff] [blame] | 138 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 139 | sk_sp<SkColorSpace> SkImage::refColorSpace() const { return fInfo.refColorSpace(); } |
Matt Sarett | 5b4599f | 2017-03-02 12:42:35 -0500 | [diff] [blame] | 140 | |
Mike Reed | 9290d01 | 2020-06-11 16:56:06 -0400 | [diff] [blame] | 141 | sk_sp<SkShader> SkImage::makeShader(SkTileMode tmx, SkTileMode tmy, |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 142 | const SkSamplingOptions& sampling, |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 143 | const SkMatrix* localMatrix) const { |
| 144 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy, |
Mike Reed | 225cdea | 2021-03-18 22:19:44 -0400 | [diff] [blame] | 145 | sampling, localMatrix); |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 148 | sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 149 | // Context TODO: Elevate GrDirectContext requirement to public API. |
| 150 | auto dContext = as_IB(this)->directContext(); |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 151 | SkBitmap bm; |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 152 | if (as_IB(this)->getROPixels(dContext, &bm)) { |
Mike Reed | 25eef6b | 2017-12-08 16:20:58 -0500 | [diff] [blame] | 153 | return SkEncodeBitmap(bm, type, quality); |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 154 | } |
| 155 | return nullptr; |
| 156 | } |
| 157 | |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 158 | sk_sp<SkData> SkImage::encodeToData() const { |
| 159 | if (auto encoded = this->refEncodedData()) { |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 160 | return encoded; |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Brian Osman | e50cdf0 | 2018-10-19 13:02:14 -0400 | [diff] [blame] | 163 | return this->encodeToData(SkEncodedImageFormat::kPNG, 100); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 166 | sk_sp<SkData> SkImage::refEncodedData() const { |
| 167 | return sk_sp<SkData>(as_IB(this)->onRefEncoded()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Mike Reed | 564d49e | 2020-07-28 12:52:31 -0400 | [diff] [blame] | 170 | sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 171 | if (nullptr == encoded || 0 == encoded->size()) { |
| 172 | return nullptr; |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 173 | } |
Mike Reed | 564d49e | 2020-07-28 12:52:31 -0400 | [diff] [blame] | 174 | return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(std::move(encoded))); |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 177 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 178 | |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 179 | sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset, GrDirectContext* direct) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 180 | if (subset.isEmpty()) { |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
| 184 | const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 185 | if (!bounds.contains(subset)) { |
| 186 | return nullptr; |
| 187 | } |
| 188 | |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 189 | #if SK_SUPPORT_GPU |
| 190 | auto myContext = as_IB(this)->context(); |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 191 | // 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] | 192 | if (myContext && !myContext->priv().matches(direct)) { |
| 193 | return nullptr; |
| 194 | } |
| 195 | #endif |
| 196 | |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 197 | // optimization : return self if the subset == our bounds |
| 198 | if (bounds == subset) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 199 | return sk_ref_sp(const_cast<SkImage*>(this)); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 200 | } |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 201 | |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 202 | return as_IB(this)->onMakeSubset(subset, direct); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 203 | } |
| 204 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 205 | #if SK_SUPPORT_GPU |
| 206 | |
Jim Van Verth | 21bd60d | 2018-10-12 15:00:20 -0400 | [diff] [blame] | 207 | bool SkImage::isTextureBacked() const { return as_IB(this)->onIsTextureBacked(); } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 208 | |
Brian Salomon | 650dd08 | 2021-02-11 08:59:03 -0500 | [diff] [blame] | 209 | size_t SkImage::textureSize() const { return as_IB(this)->onTextureSize(); } |
| 210 | |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 211 | GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO, |
| 212 | GrSurfaceOrigin* origin) const { |
| 213 | return as_IB(this)->onGetBackendTexture(flushPendingGrContextIO, origin); |
| 214 | } |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 215 | |
Adlai Holler | 0137355 | 2020-07-21 13:52:41 -0400 | [diff] [blame] | 216 | bool SkImage::isValid(GrRecordingContext* rContext) const { |
| 217 | if (rContext && rContext->abandoned()) { |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 218 | return false; |
| 219 | } |
Adlai Holler | 0137355 | 2020-07-21 13:52:41 -0400 | [diff] [blame] | 220 | return as_IB(this)->onIsValid(rContext); |
Robert Phillips | 4a3ebc2 | 2020-07-10 11:27:43 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Adlai Holler | 40ad5fd | 2020-07-22 10:22:12 -0400 | [diff] [blame] | 223 | GrSemaphoresSubmitted SkImage::flush(GrDirectContext* dContext, const GrFlushInfo& flushInfo) { |
| 224 | return as_IB(this)->onFlush(dContext, flushInfo); |
| 225 | } |
| 226 | |
| 227 | void SkImage::flushAndSubmit(GrDirectContext* dContext) { |
| 228 | this->flush(dContext, {}); |
| 229 | dContext->submit(); |
| 230 | } |
| 231 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 232 | #else |
| 233 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 234 | bool SkImage::isTextureBacked() const { return false; } |
| 235 | |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 236 | GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO, |
| 237 | GrSurfaceOrigin* origin) const { |
| 238 | return GrBackendTexture(); // invalid |
| 239 | } |
| 240 | |
Adlai Holler | 0137355 | 2020-07-21 13:52:41 -0400 | [diff] [blame] | 241 | bool SkImage::isValid(GrRecordingContext* rContext) const { |
| 242 | if (rContext) { |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 243 | return false; |
| 244 | } |
Robert Phillips | 4a3ebc2 | 2020-07-10 11:27:43 -0400 | [diff] [blame] | 245 | return as_IB(this)->onIsValid(nullptr); |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Adlai Holler | 40ad5fd | 2020-07-22 10:22:12 -0400 | [diff] [blame] | 248 | GrSemaphoresSubmitted SkImage::flush(GrDirectContext*, const GrFlushInfo&) { |
| 249 | return GrSemaphoresSubmitted::kNo; |
| 250 | } |
| 251 | |
| 252 | void SkImage::flushAndSubmit(GrDirectContext*) {} |
| 253 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 254 | #endif |
| 255 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 256 | /////////////////////////////////////////////////////////////////////////////// |
| 257 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 258 | SkImage_Base::SkImage_Base(const SkImageInfo& info, uint32_t uniqueID) |
| 259 | : INHERITED(info, uniqueID), fAddedToRasterCache(false) {} |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 260 | |
| 261 | SkImage_Base::~SkImage_Base() { |
Mike Reed | 30301c4 | 2018-07-19 09:39:21 -0400 | [diff] [blame] | 262 | if (fAddedToRasterCache.load()) { |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 263 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 264 | } |
| 265 | } |
| 266 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 267 | void SkImage_Base::onAsyncRescaleAndReadPixels(const SkImageInfo& info, |
| 268 | const SkIRect& origSrcRect, |
| 269 | RescaleGamma rescaleGamma, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame] | 270 | RescaleMode rescaleMode, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 271 | ReadPixelsCallback callback, |
| 272 | ReadPixelsContext context) { |
| 273 | SkBitmap src; |
| 274 | SkPixmap peek; |
| 275 | SkIRect srcRect; |
| 276 | if (this->peekPixels(&peek)) { |
| 277 | src.installPixels(peek); |
| 278 | srcRect = origSrcRect; |
| 279 | } else { |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 280 | // Context TODO: Elevate GrDirectContext requirement to public API. |
| 281 | auto dContext = as_IB(this)->directContext(); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 282 | src.setInfo(this->imageInfo().makeDimensions(origSrcRect.size())); |
| 283 | src.allocPixels(); |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 284 | if (!this->readPixels(dContext, src.pixmap(), origSrcRect.x(), origSrcRect.y())) { |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 285 | callback(context, nullptr); |
| 286 | return; |
| 287 | } |
| 288 | srcRect = SkIRect::MakeSize(src.dimensions()); |
| 289 | } |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame] | 290 | return SkRescaleAndReadPixels(src, info, srcRect, rescaleGamma, rescaleMode, callback, context); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void SkImage_Base::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, |
| 294 | sk_sp<SkColorSpace> dstColorSpace, |
| 295 | const SkIRect& srcRect, |
| 296 | const SkISize& dstSize, |
| 297 | RescaleGamma, |
Mike Reed | 1efa14d | 2021-01-02 21:44:59 -0500 | [diff] [blame] | 298 | RescaleMode, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 299 | ReadPixelsCallback callback, |
| 300 | ReadPixelsContext context) { |
| 301 | // TODO: Call non-YUV asyncRescaleAndReadPixels and then make our callback convert to YUV and |
| 302 | // call client's callback. |
| 303 | callback(context, nullptr); |
| 304 | } |
| 305 | |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 306 | #if SK_SUPPORT_GPU |
| 307 | std::tuple<GrSurfaceProxyView, GrColorType> SkImage_Base::asView(GrRecordingContext* context, |
| 308 | GrMipmapped mipmapped, |
| 309 | GrImageTexGenPolicy policy) const { |
| 310 | if (!context) { |
| 311 | return {}; |
| 312 | } |
| 313 | if (!context->priv().caps()->mipmapSupport() || this->dimensions().area() <= 1) { |
| 314 | mipmapped = GrMipmapped::kNo; |
| 315 | } |
| 316 | return this->onAsView(context, mipmapped, policy); |
| 317 | } |
| 318 | #endif |
| 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 | |
Adlai Holler | c44e21f | 2021-02-22 14:08:07 -0500 | [diff] [blame] | 585 | sk_sp<SkImage> SkImage::MakePromiseTexture(sk_sp<GrContextThreadSafeProxy>, |
| 586 | const GrBackendFormat&, |
| 587 | SkISize, |
| 588 | GrMipmapped, |
| 589 | GrSurfaceOrigin, |
| 590 | SkColorType, |
| 591 | SkAlphaType, |
| 592 | sk_sp<SkColorSpace>, |
| 593 | PromiseImageTextureFulfillProc, |
| 594 | PromiseImageTextureReleaseProc, |
| 595 | PromiseImageTextureContext) { |
| 596 | return nullptr; |
| 597 | } |
| 598 | |
| 599 | sk_sp<SkImage> SkImage::MakePromiseYUVATexture(sk_sp<GrContextThreadSafeProxy>, |
| 600 | const GrYUVABackendTextureInfo&, |
| 601 | sk_sp<SkColorSpace>, |
| 602 | PromiseImageTextureFulfillProc, |
| 603 | PromiseImageTextureReleaseProc, |
| 604 | PromiseImageTextureContext[]) { |
| 605 | return nullptr; |
| 606 | } |
| 607 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 608 | #endif |
| 609 | |
| 610 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 611 | |
Adlai Holler | 2940538 | 2020-07-20 16:02:05 -0400 | [diff] [blame] | 612 | bool SkImage_pinAsTexture(const SkImage* image, GrRecordingContext* rContext) { |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 613 | SkASSERT(image); |
Adlai Holler | 2940538 | 2020-07-20 16:02:05 -0400 | [diff] [blame] | 614 | SkASSERT(rContext); |
| 615 | return as_IB(image)->onPinAsTexture(rContext); |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Adlai Holler | 2940538 | 2020-07-20 16:02:05 -0400 | [diff] [blame] | 618 | void SkImage_unpinAsTexture(const SkImage* image, GrRecordingContext* rContext) { |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 619 | SkASSERT(image); |
Adlai Holler | 2940538 | 2020-07-20 16:02:05 -0400 | [diff] [blame] | 620 | SkASSERT(rContext); |
| 621 | as_IB(image)->onUnpinAsTexture(rContext); |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 622 | } |
Brian Osman | 0d4ff6c | 2017-01-17 16:10:07 -0500 | [diff] [blame] | 623 | |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 624 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 625 | |
| 626 | SkMipmapBuilder::SkMipmapBuilder(const SkImageInfo& info) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 627 | fMM = sk_sp<SkMipmap>(SkMipmap::Build({info, nullptr, 0}, nullptr, false)); |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | SkMipmapBuilder::~SkMipmapBuilder() {} |
| 631 | |
| 632 | int SkMipmapBuilder::countLevels() const { |
| 633 | return fMM ? fMM->countLevels() : 0; |
| 634 | } |
| 635 | |
| 636 | SkPixmap SkMipmapBuilder::level(int index) const { |
| 637 | SkPixmap pm; |
| 638 | |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 639 | SkMipmap::Level level; |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 640 | if (fMM && fMM->getLevel(index, &level)) { |
| 641 | pm = level.fPixmap; |
| 642 | } |
| 643 | return pm; |
| 644 | } |
| 645 | |
Brian Salomon | 7d2757f | 2021-01-26 17:46:50 -0500 | [diff] [blame] | 646 | bool SkImage::hasMipmaps() const { return as_IB(this)->onHasMipmaps(); } |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 647 | |
Mike Reed | 1529295 | 2020-07-25 12:47:14 -0400 | [diff] [blame] | 648 | sk_sp<SkImage> SkImage::withMipmaps(sk_sp<SkMipmap> mips) const { |
| 649 | if (mips == nullptr || mips->validForRootLevel(this->imageInfo())) { |
| 650 | if (auto result = as_IB(this)->onMakeWithMipmaps(std::move(mips))) { |
| 651 | return result; |
| 652 | } |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 653 | } |
Mike Reed | 1529295 | 2020-07-25 12:47:14 -0400 | [diff] [blame] | 654 | return sk_ref_sp((const_cast<SkImage*>(this))); |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 655 | } |
Mike Reed | 186866c | 2020-09-12 10:38:32 -0400 | [diff] [blame] | 656 | |
| 657 | sk_sp<SkImage> SkImage::withDefaultMipmaps() const { |
| 658 | return this->withMipmaps(nullptr); |
| 659 | } |
Mike Reed | b4aa639 | 2020-09-17 13:58:26 -0400 | [diff] [blame] | 660 | |
| 661 | sk_sp<SkImage> SkMipmapBuilder::attachTo(const SkImage* src) { |
| 662 | return src->withMipmaps(fMM); |
| 663 | } |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 664 | |
Mike Reed | 9223665 | 2021-02-01 13:07:32 -0500 | [diff] [blame] | 665 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 666 | |
| 667 | #include "src/core/SkReadBuffer.h" |
| 668 | #include "src/core/SkSamplingPriv.h" |
| 669 | #include "src/core/SkWriteBuffer.h" |
| 670 | |
Mike Reed | 9a27566 | 2020-12-26 21:48:57 -0500 | [diff] [blame] | 671 | SkSamplingOptions::SkSamplingOptions(SkFilterQuality fq, MediumBehavior behavior) { |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 672 | switch (fq) { |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 673 | case SkFilterQuality::kHigh_SkFilterQuality: |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 674 | *this = SkSamplingOptions(SkCubicResampler{1/3.0f, 1/3.0f}); |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 675 | break; |
| 676 | case SkFilterQuality::kMedium_SkFilterQuality: |
Mike Reed | 9a27566 | 2020-12-26 21:48:57 -0500 | [diff] [blame] | 677 | *this = SkSamplingOptions(SkFilterMode::kLinear, |
| 678 | behavior == kMedium_asMipmapNearest ? SkMipmapMode::kNearest |
| 679 | : SkMipmapMode::kLinear); |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 680 | break; |
| 681 | case SkFilterQuality::kLow_SkFilterQuality: |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 682 | *this = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone); |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 683 | break; |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 684 | case SkFilterQuality::kNone_SkFilterQuality: |
Mike Klein | dcc8960 | 2020-12-02 15:06:15 -0600 | [diff] [blame] | 685 | *this = SkSamplingOptions(SkFilterMode::kNearest, SkMipmapMode::kNone); |
Mike Reed | a03f8bf | 2020-11-20 18:45:36 -0500 | [diff] [blame] | 686 | break; |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 687 | } |
Mike Reed | cae335d | 2020-11-13 14:32:04 -0500 | [diff] [blame] | 688 | } |
Mike Reed | 9223665 | 2021-02-01 13:07:32 -0500 | [diff] [blame] | 689 | |
| 690 | SkSamplingOptions SkSamplingPriv::Read(SkReadBuffer& buffer) { |
| 691 | if (buffer.readBool()) { |
| 692 | SkScalar B = buffer.readScalar(), |
| 693 | C = buffer.readScalar(); |
| 694 | return SkSamplingOptions({B,C}); |
| 695 | } else { |
| 696 | auto filter = buffer.read32LE<SkFilterMode>(SkFilterMode::kLinear); |
| 697 | auto mipmap = buffer.read32LE<SkMipmapMode>(SkMipmapMode::kLinear); |
| 698 | return SkSamplingOptions(filter, mipmap); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | void SkSamplingPriv::Write(SkWriteBuffer& buffer, const SkSamplingOptions& sampling) { |
| 703 | buffer.writeBool(sampling.useCubic); |
| 704 | if (sampling.useCubic) { |
| 705 | buffer.writeScalar(sampling.cubic.B); |
| 706 | buffer.writeScalar(sampling.cubic.C); |
| 707 | } else { |
| 708 | buffer.writeUInt((unsigned)sampling.filter); |
| 709 | buffer.writeUInt((unsigned)sampling.mipmap); |
| 710 | } |
| 711 | } |