reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 8 | #include "SkImage_Base.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 9 | #include "SkImageCacherator.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 10 | |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkBitmapCache.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 13 | #include "SkData.h" |
| 14 | #include "SkImageGenerator.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 15 | #include "SkImagePriv.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 16 | #include "SkNextID.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 17 | #include "SkPixelRef.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 18 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
| 20 | #include "GrContext.h" |
| 21 | #include "GrContextPriv.h" |
| 22 | #include "GrGpuResourcePriv.h" |
| 23 | #include "GrImageTextureMaker.h" |
| 24 | #include "GrResourceKey.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 25 | #include "GrProxyProvider.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 26 | #include "GrSamplerState.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 27 | #include "GrYUVProvider.h" |
| 28 | #include "SkGr.h" |
| 29 | #endif |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 30 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 31 | // Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images |
| 32 | class SharedGenerator final : public SkNVRefCnt<SharedGenerator> { |
| 33 | public: |
| 34 | static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen) { |
| 35 | return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr; |
| 36 | } |
| 37 | |
Matt Sarett | b2004f7 | 2017-05-18 09:26:50 -0400 | [diff] [blame] | 38 | // This is thread safe. It is a const field set in the constructor. |
| 39 | const SkImageInfo& getInfo() { return fGenerator->getInfo(); } |
| 40 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 41 | private: |
| 42 | explicit SharedGenerator(std::unique_ptr<SkImageGenerator> gen) |
| 43 | : fGenerator(std::move(gen)) { |
| 44 | SkASSERT(fGenerator); |
| 45 | } |
| 46 | |
| 47 | friend class ScopedGenerator; |
| 48 | friend class SkImage_Lazy; |
| 49 | |
| 50 | std::unique_ptr<SkImageGenerator> fGenerator; |
| 51 | SkMutex fMutex; |
| 52 | }; |
| 53 | |
| 54 | class SkImage_Lazy : public SkImage_Base, public SkImageCacherator { |
| 55 | public: |
| 56 | struct Validator { |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 57 | Validator(sk_sp<SharedGenerator>, const SkIRect* subset, sk_sp<SkColorSpace> colorSpace); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 58 | |
| 59 | operator bool() const { return fSharedGenerator.get(); } |
| 60 | |
| 61 | sk_sp<SharedGenerator> fSharedGenerator; |
| 62 | SkImageInfo fInfo; |
| 63 | SkIPoint fOrigin; |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 64 | sk_sp<SkColorSpace> fColorSpace; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 65 | uint32_t fUniqueID; |
| 66 | }; |
| 67 | |
| 68 | SkImage_Lazy(Validator* validator); |
| 69 | |
| 70 | SkImageInfo onImageInfo() const override { |
| 71 | return fInfo; |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 72 | } |
Greg Daniel | 56008aa | 2018-03-14 15:33:42 -0400 | [diff] [blame] | 73 | SkColorType onColorType() const override { |
| 74 | return kUnknown_SkColorType; |
| 75 | } |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 76 | SkAlphaType onAlphaType() const override { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 77 | return fInfo.alphaType(); |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 78 | } |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 79 | |
Mike Reed | f2c7364 | 2018-05-29 15:41:27 -0400 | [diff] [blame] | 80 | SkIRect onGetSubset() const override { |
| 81 | return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, fInfo.width(), fInfo.height()); |
| 82 | } |
| 83 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 84 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, |
| 85 | CachingHint) const override; |
| 86 | #if SK_SUPPORT_GPU |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 87 | sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, |
| 88 | const GrSamplerState&, SkColorSpace*, |
| 89 | sk_sp<SkColorSpace>*, |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 90 | SkScalar scaleAdjust[2]) const override; |
| 91 | #endif |
Ben Wagner | bdf5433 | 2018-05-15 14:12:14 -0400 | [diff] [blame] | 92 | sk_sp<SkData> onRefEncoded() const override; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 93 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 94 | bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 95 | bool onIsLazyGenerated() const override { return true; } |
Mike Reed | 7f1d020 | 2017-05-08 16:13:39 -0400 | [diff] [blame] | 96 | bool onCanLazyGenerateOnGPU() const override; |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 97 | sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType, |
| 98 | SkTransferFunctionBehavior) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 99 | |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 100 | bool onIsValid(GrContext*) const override; |
| 101 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 102 | SkImageCacherator* peekCacherator() const override { |
| 103 | return const_cast<SkImage_Lazy*>(this); |
| 104 | } |
| 105 | |
| 106 | // Only return true if the generate has already been cached. |
| 107 | bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*, CachedFormat) const; |
| 108 | // Call the underlying generator directly |
| 109 | bool directGeneratePixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 110 | int srcX, int srcY, SkTransferFunctionBehavior behavior) const; |
| 111 | |
| 112 | // SkImageCacherator interface |
| 113 | #if SK_SUPPORT_GPU |
| 114 | // Returns the texture proxy. If the cacherator is generating the texture and wants to cache it, |
| 115 | // it should use the passed in key (if the key is valid). |
| 116 | sk_sp<GrTextureProxy> lockTextureProxy(GrContext*, |
| 117 | const GrUniqueKey& key, |
| 118 | SkImage::CachingHint, |
| 119 | bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 120 | SkColorSpace* dstColorSpace, |
| 121 | GrTextureMaker::AllowedTexGenType genType) override; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 122 | |
| 123 | // Returns the color space of the texture that would be returned if you called lockTexture. |
| 124 | // Separate code path to allow querying of the color space for textures that cached (even |
| 125 | // externally). |
| 126 | sk_sp<SkColorSpace> getColorSpace(GrContext*, SkColorSpace* dstColorSpace) override; |
| 127 | void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, CachedFormat, |
| 128 | GrUniqueKey* cacheKey) override; |
| 129 | #endif |
| 130 | |
| 131 | CachedFormat chooseCacheFormat(SkColorSpace* dstColorSpace, |
| 132 | const GrCaps* = nullptr) const override; |
| 133 | SkImageInfo buildCacheInfo(CachedFormat) const override; |
| 134 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 135 | private: |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 136 | class ScopedGenerator; |
| 137 | |
| 138 | /** |
| 139 | * On success (true), bitmap will point to the pixels for this generator. If this returns |
| 140 | * false, the bitmap will be reset to empty. |
| 141 | */ |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 142 | bool lockAsBitmap(SkBitmap*, SkImage::CachingHint, CachedFormat, const SkImageInfo&, |
| 143 | SkTransferFunctionBehavior) const; |
| 144 | |
| 145 | /** |
| 146 | * Populates parameters to pass to the generator for reading pixels or generating a texture. |
| 147 | * For image generators, legacy versus true color blending is indicated using a |
| 148 | * SkTransferFunctionBehavior, and the target color space is specified on the SkImageInfo. |
| 149 | * If generatorImageInfo has no color space set, set its color space to this SkImage's color |
| 150 | * space, and return "ignore" behavior, indicating legacy mode. If generatorImageInfo has a |
| 151 | * color space set, return "respect" behavior, indicating linear blending mode. |
| 152 | */ |
| 153 | SkTransferFunctionBehavior getGeneratorBehaviorAndInfo(SkImageInfo* generatorImageInfo) const; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 154 | |
| 155 | sk_sp<SharedGenerator> fSharedGenerator; |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 156 | // Note that fInfo is not necessarily the info from the generator. It may be cropped by |
| 157 | // onMakeSubset and its color space may be changed by onMakeColorSpace. |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 158 | const SkImageInfo fInfo; |
| 159 | const SkIPoint fOrigin; |
| 160 | |
| 161 | struct IDRec { |
| 162 | SkOnce fOnce; |
| 163 | uint32_t fUniqueID; |
| 164 | }; |
| 165 | mutable IDRec fIDRecs[kNumCachedFormats]; |
| 166 | |
| 167 | uint32_t getUniqueID(CachedFormat) const; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 168 | |
Christopher Cameron | d4b6787 | 2017-07-13 15:18:08 -0700 | [diff] [blame] | 169 | // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and |
| 170 | // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call. |
| 171 | mutable SkMutex fOnMakeColorSpaceMutex; |
| 172 | mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget; |
| 173 | mutable sk_sp<SkImage> fOnMakeColorSpaceResult; |
| 174 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 175 | typedef SkImage_Base INHERITED; |
| 176 | }; |
| 177 | |
| 178 | /////////////////////////////////////////////////////////////////////////////// |
| 179 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 180 | SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset, |
| 181 | sk_sp<SkColorSpace> colorSpace) |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 182 | : fSharedGenerator(std::move(gen)) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 183 | if (!fSharedGenerator) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | // The following generator accessors are safe without acquiring the mutex (const getters). |
| 188 | // TODO: refactor to use a ScopedGenerator instead, for clarity. |
| 189 | const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo(); |
| 190 | if (info.isEmpty()) { |
| 191 | fSharedGenerator.reset(); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | fUniqueID = fSharedGenerator->fGenerator->uniqueID(); |
| 196 | const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height()); |
| 197 | if (subset) { |
| 198 | if (!bounds.contains(*subset)) { |
| 199 | fSharedGenerator.reset(); |
| 200 | return; |
| 201 | } |
| 202 | if (*subset != bounds) { |
| 203 | // we need a different uniqueID since we really are a subset of the raw generator |
| 204 | fUniqueID = SkNextID::ImageID(); |
| 205 | } |
| 206 | } else { |
| 207 | subset = &bounds; |
| 208 | } |
| 209 | |
| 210 | fInfo = info.makeWH(subset->width(), subset->height()); |
| 211 | fOrigin = SkIPoint::Make(subset->x(), subset->y()); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 212 | if (colorSpace) { |
| 213 | fInfo = fInfo.makeColorSpace(colorSpace); |
| 214 | fUniqueID = SkNextID::ImageID(); |
| 215 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /////////////////////////////////////////////////////////////////////////////// |
| 219 | |
| 220 | // Helper for exclusive access to a shared generator. |
| 221 | class SkImage_Lazy::ScopedGenerator { |
| 222 | public: |
| 223 | ScopedGenerator(const sk_sp<SharedGenerator>& gen) |
| 224 | : fSharedGenerator(gen) |
| 225 | , fAutoAquire(gen->fMutex) {} |
| 226 | |
| 227 | SkImageGenerator* operator->() const { |
| 228 | fSharedGenerator->fMutex.assertHeld(); |
| 229 | return fSharedGenerator->fGenerator.get(); |
| 230 | } |
| 231 | |
| 232 | operator SkImageGenerator*() const { |
| 233 | fSharedGenerator->fMutex.assertHeld(); |
| 234 | return fSharedGenerator->fGenerator.get(); |
| 235 | } |
| 236 | |
| 237 | private: |
| 238 | const sk_sp<SharedGenerator>& fSharedGenerator; |
| 239 | SkAutoExclusive fAutoAquire; |
| 240 | }; |
| 241 | |
| 242 | /////////////////////////////////////////////////////////////////////////////// |
| 243 | |
| 244 | SkImage_Lazy::SkImage_Lazy(Validator* validator) |
| 245 | : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID) |
| 246 | , fSharedGenerator(std::move(validator->fSharedGenerator)) |
| 247 | , fInfo(validator->fInfo) |
| 248 | , fOrigin(validator->fOrigin) { |
| 249 | SkASSERT(fSharedGenerator); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 250 | // We explicit set the legacy format slot, but leave the others uninitialized (via SkOnce) |
| 251 | // and only resolove them to IDs as needed (by calling getUniqueID()). |
| 252 | fIDRecs[kLegacy_CachedFormat].fOnce([this, validator] { |
| 253 | fIDRecs[kLegacy_CachedFormat].fUniqueID = validator->fUniqueID; |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | uint32_t SkImage_Lazy::getUniqueID(CachedFormat format) const { |
| 258 | IDRec* rec = &fIDRecs[format]; |
| 259 | rec->fOnce([rec] { |
| 260 | rec->fUniqueID = SkNextID::ImageID(); |
| 261 | }); |
| 262 | return rec->fUniqueID; |
| 263 | } |
| 264 | |
| 265 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 266 | |
| 267 | // Abstraction of GrCaps that handles the cases where we don't have a caps pointer (because |
| 268 | // we're in raster mode), or where GPU support is entirely missing. In theory, we only need the |
| 269 | // chosen format to be texturable, but that lets us choose F16 on GLES implemenations where we |
| 270 | // won't be able to read the texture back. We'd like to ensure that SkImake::makeNonTextureImage |
| 271 | // works, so we require that the formats we choose are renderable (as a proxy for being readable). |
| 272 | struct CacheCaps { |
| 273 | CacheCaps(const GrCaps* caps) : fCaps(caps) {} |
| 274 | |
| 275 | #if SK_SUPPORT_GPU |
| 276 | bool supportsHalfFloat() const { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 277 | return !fCaps || (fCaps->isConfigTexturable(kRGBA_half_GrPixelConfig) && |
| 278 | fCaps->isConfigRenderable(kRGBA_half_GrPixelConfig)); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bool supportsSRGB() const { |
| 282 | return !fCaps || |
| 283 | (fCaps->srgbSupport() && fCaps->isConfigTexturable(kSRGBA_8888_GrPixelConfig)); |
| 284 | } |
| 285 | |
| 286 | bool supportsSBGR() const { |
| 287 | return !fCaps || fCaps->srgbSupport(); |
| 288 | } |
| 289 | #else |
| 290 | bool supportsHalfFloat() const { return true; } |
| 291 | bool supportsSRGB() const { return true; } |
| 292 | bool supportsSBGR() const { return true; } |
| 293 | #endif |
| 294 | |
| 295 | const GrCaps* fCaps; |
| 296 | }; |
| 297 | |
| 298 | SkImageCacherator::CachedFormat SkImage_Lazy::chooseCacheFormat(SkColorSpace* dstColorSpace, |
| 299 | const GrCaps* grCaps) const { |
Brian Osman | bfc33e5 | 2018-06-27 14:24:11 -0400 | [diff] [blame^] | 300 | #ifdef SK_SUPPORT_LEGACY_LAZY_IMAGE_DECODE_BEHAVIOR |
Brian Osman | e269310 | 2018-06-26 19:19:49 +0000 | [diff] [blame] | 301 | SkColorSpace* cs = fInfo.colorSpace(); |
| 302 | if (!cs || !dstColorSpace) { |
| 303 | return kLegacy_CachedFormat; |
| 304 | } |
| 305 | |
| 306 | CacheCaps caps(grCaps); |
| 307 | switch (fInfo.colorType()) { |
| 308 | case kUnknown_SkColorType: |
| 309 | case kAlpha_8_SkColorType: |
| 310 | case kRGB_565_SkColorType: |
| 311 | case kARGB_4444_SkColorType: |
| 312 | case kRGB_888x_SkColorType: |
| 313 | case kRGBA_1010102_SkColorType: |
| 314 | case kRGB_101010x_SkColorType: |
| 315 | // We don't support color space on these formats, so always decode in legacy mode: |
| 316 | // TODO: Ask the codec to decode these to something else (at least sRGB 8888)? |
| 317 | return kLegacy_CachedFormat; |
| 318 | |
| 319 | case kGray_8_SkColorType: |
| 320 | // TODO: What do we do with grayscale sources that have strange color spaces attached? |
| 321 | // The codecs and color space xform don't handle this correctly (yet), so drop it on |
| 322 | // the floor. (Also, inflating by a factor of 8 is going to be unfortunate). |
| 323 | // As it is, we don't directly support sRGB grayscale, so ask the codec to convert |
| 324 | // it for us. This bypasses some really sketchy code GrUploadPixmapToTexture. |
| 325 | if (cs->gammaCloseToSRGB() && caps.supportsSRGB()) { |
| 326 | return kSRGB8888_CachedFormat; |
| 327 | } else { |
| 328 | return kLegacy_CachedFormat; |
| 329 | } |
| 330 | |
| 331 | case kRGBA_8888_SkColorType: |
| 332 | if (cs->gammaCloseToSRGB()) { |
| 333 | if (caps.supportsSRGB()) { |
| 334 | return kSRGB8888_CachedFormat; |
| 335 | } else if (caps.supportsHalfFloat()) { |
| 336 | return kLinearF16_CachedFormat; |
| 337 | } else { |
| 338 | return kLegacy_CachedFormat; |
| 339 | } |
| 340 | } else { |
| 341 | if (caps.supportsHalfFloat()) { |
| 342 | return kLinearF16_CachedFormat; |
| 343 | } else if (caps.supportsSRGB()) { |
| 344 | return kSRGB8888_CachedFormat; |
| 345 | } else { |
| 346 | return kLegacy_CachedFormat; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | case kBGRA_8888_SkColorType: |
| 351 | // Odd case. sBGRA isn't a real thing, so we may not have this texturable. |
| 352 | if (caps.supportsSBGR()) { |
| 353 | if (cs->gammaCloseToSRGB()) { |
| 354 | return kSBGR8888_CachedFormat; |
| 355 | } else if (caps.supportsHalfFloat()) { |
| 356 | return kLinearF16_CachedFormat; |
| 357 | } else if (caps.supportsSRGB()) { |
| 358 | return kSRGB8888_CachedFormat; |
| 359 | } else { |
| 360 | // sBGRA support without sRGBA is highly unlikely (impossible?) Nevertheless. |
| 361 | return kLegacy_CachedFormat; |
| 362 | } |
| 363 | } else { |
| 364 | if (cs->gammaCloseToSRGB()) { |
| 365 | if (caps.supportsSRGB()) { |
| 366 | return kSRGB8888_CachedFormat; |
| 367 | } else if (caps.supportsHalfFloat()) { |
| 368 | return kLinearF16_CachedFormat; |
| 369 | } else { |
| 370 | return kLegacy_CachedFormat; |
| 371 | } |
| 372 | } else { |
| 373 | if (caps.supportsHalfFloat()) { |
| 374 | return kLinearF16_CachedFormat; |
| 375 | } else if (caps.supportsSRGB()) { |
| 376 | return kSRGB8888_CachedFormat; |
| 377 | } else { |
| 378 | return kLegacy_CachedFormat; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | case kRGBA_F16_SkColorType: |
Brian Osman | d47fe09 | 2018-06-26 15:27:10 -0400 | [diff] [blame] | 384 | case kRGBA_F32_SkColorType: |
Brian Osman | e269310 | 2018-06-26 19:19:49 +0000 | [diff] [blame] | 385 | if (caps.supportsHalfFloat()) { |
| 386 | return kLinearF16_CachedFormat; |
| 387 | } else if (caps.supportsSRGB()) { |
| 388 | return kSRGB8888_CachedFormat; |
| 389 | } else { |
| 390 | return kLegacy_CachedFormat; |
| 391 | } |
| 392 | } |
| 393 | SkDEBUGFAIL("Unreachable"); |
Brian Osman | bfc33e5 | 2018-06-27 14:24:11 -0400 | [diff] [blame^] | 394 | #endif |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 395 | return kLegacy_CachedFormat; |
| 396 | } |
| 397 | |
| 398 | SkImageInfo SkImage_Lazy::buildCacheInfo(CachedFormat format) const { |
Brian Osman | bfc33e5 | 2018-06-27 14:24:11 -0400 | [diff] [blame^] | 399 | #ifdef SK_SUPPORT_LEGACY_LAZY_IMAGE_DECODE_BEHAVIOR |
Brian Osman | e269310 | 2018-06-26 19:19:49 +0000 | [diff] [blame] | 400 | switch (format) { |
| 401 | case kLegacy_CachedFormat: |
| 402 | return fInfo.makeColorSpace(nullptr); |
| 403 | case kLinearF16_CachedFormat: |
| 404 | return fInfo.makeColorType(kRGBA_F16_SkColorType) |
| 405 | .makeColorSpace(fInfo.colorSpace()->makeLinearGamma()); |
| 406 | case kSRGB8888_CachedFormat: |
| 407 | // If the transfer function is nearly (but not exactly) sRGB, we don't want the codec |
| 408 | // to bother trans-coding. It would be slow, and do more harm than good visually, |
| 409 | // so we make sure to leave the colorspace as-is. |
| 410 | if (fInfo.colorSpace()->gammaCloseToSRGB()) { |
| 411 | return fInfo.makeColorType(kRGBA_8888_SkColorType); |
| 412 | } else { |
| 413 | return fInfo.makeColorType(kRGBA_8888_SkColorType) |
| 414 | .makeColorSpace(fInfo.colorSpace()->makeSRGBGamma()); |
| 415 | } |
| 416 | case kSBGR8888_CachedFormat: |
| 417 | // See note above about not-quite-sRGB transfer functions. |
| 418 | if (fInfo.colorSpace()->gammaCloseToSRGB()) { |
| 419 | return fInfo.makeColorType(kBGRA_8888_SkColorType); |
| 420 | } else { |
| 421 | return fInfo.makeColorType(kBGRA_8888_SkColorType) |
| 422 | .makeColorSpace(fInfo.colorSpace()->makeSRGBGamma()); |
| 423 | } |
| 424 | default: |
| 425 | SkDEBUGFAIL("Invalid cached format"); |
| 426 | return fInfo; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 427 | } |
Brian Osman | bfc33e5 | 2018-06-27 14:24:11 -0400 | [diff] [blame^] | 428 | #else |
| 429 | if (kGray_8_SkColorType == fInfo.colorType()) { |
| 430 | return fInfo.makeColorSpace(nullptr); |
| 431 | } else { |
| 432 | return fInfo; |
| 433 | } |
| 434 | #endif |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 438 | |
| 439 | static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { |
| 440 | SkASSERT(bitmap.getGenerationID() == expectedID); |
| 441 | SkASSERT(bitmap.isImmutable()); |
| 442 | SkASSERT(bitmap.getPixels()); |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | bool SkImage_Lazy::directGeneratePixels(const SkImageInfo& info, void* pixels, size_t rb, |
| 447 | int srcX, int srcY, |
| 448 | SkTransferFunctionBehavior behavior) const { |
| 449 | ScopedGenerator generator(fSharedGenerator); |
| 450 | const SkImageInfo& genInfo = generator->getInfo(); |
| 451 | // Currently generators do not natively handle subsets, so check that first. |
| 452 | if (srcX || srcY || genInfo.width() != info.width() || genInfo.height() != info.height()) { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | SkImageGenerator::Options opts; |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 457 | // TODO: This should respect the behavior argument. |
| 458 | opts.fBehavior = SkTransferFunctionBehavior::kIgnore; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 459 | return generator->getPixels(info, pixels, rb, &opts); |
| 460 | } |
| 461 | |
| 462 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 463 | |
| 464 | bool SkImage_Lazy::lockAsBitmapOnlyIfAlreadyCached(SkBitmap* bitmap, CachedFormat format) const { |
| 465 | uint32_t uniqueID = this->getUniqueID(format); |
| 466 | return SkBitmapCache::Find(SkBitmapCacheDesc::Make(uniqueID, |
| 467 | fInfo.width(), fInfo.height()), bitmap) && |
| 468 | check_output_bitmap(*bitmap, uniqueID); |
| 469 | } |
| 470 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 471 | static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY, |
| 472 | SkTransferFunctionBehavior behavior) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 473 | const int genW = gen->getInfo().width(); |
| 474 | const int genH = gen->getInfo().height(); |
| 475 | const SkIRect srcR = SkIRect::MakeWH(genW, genH); |
| 476 | const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height()); |
| 477 | if (!srcR.contains(dstR)) { |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | // If they are requesting a subset, we have to have a temp allocation for full image, and |
| 482 | // then copy the subset into their allocation |
| 483 | SkBitmap full; |
| 484 | SkPixmap fullPM; |
| 485 | const SkPixmap* dstPM = &pmap; |
| 486 | if (srcR != dstR) { |
| 487 | if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) { |
| 488 | return false; |
| 489 | } |
| 490 | if (!full.peekPixels(&fullPM)) { |
| 491 | return false; |
| 492 | } |
| 493 | dstPM = &fullPM; |
| 494 | } |
| 495 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 496 | SkImageGenerator::Options opts; |
| 497 | opts.fBehavior = behavior; |
| 498 | if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes(), &opts)) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 499 | return false; |
| 500 | } |
| 501 | |
| 502 | if (srcR != dstR) { |
| 503 | if (!full.readPixels(pmap, originX, originY)) { |
| 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 510 | bool SkImage_Lazy::lockAsBitmap(SkBitmap* bitmap, SkImage::CachingHint chint, CachedFormat format, |
| 511 | const SkImageInfo& info, |
| 512 | SkTransferFunctionBehavior behavior) const { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 513 | if (this->lockAsBitmapOnlyIfAlreadyCached(bitmap, format)) { |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | uint32_t uniqueID = this->getUniqueID(format); |
| 518 | |
| 519 | SkBitmap tmpBitmap; |
| 520 | SkBitmapCache::RecPtr cacheRec; |
| 521 | SkPixmap pmap; |
| 522 | if (SkImage::kAllow_CachingHint == chint) { |
| 523 | auto desc = SkBitmapCacheDesc::Make(uniqueID, info.width(), info.height()); |
| 524 | cacheRec = SkBitmapCache::Alloc(desc, info, &pmap); |
| 525 | if (!cacheRec) { |
| 526 | return false; |
| 527 | } |
| 528 | } else { |
| 529 | if (!tmpBitmap.tryAllocPixels(info)) { |
| 530 | return false; |
| 531 | } |
| 532 | if (!tmpBitmap.peekPixels(&pmap)) { |
| 533 | return false; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | ScopedGenerator generator(fSharedGenerator); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 538 | if (!generate_pixels(generator, pmap, fOrigin.x(), fOrigin.y(), behavior)) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | |
| 542 | if (cacheRec) { |
| 543 | SkBitmapCache::Add(std::move(cacheRec), bitmap); |
| 544 | SkASSERT(bitmap->getPixels()); // we're locked |
| 545 | SkASSERT(bitmap->isImmutable()); |
| 546 | SkASSERT(bitmap->getGenerationID() == uniqueID); |
| 547 | this->notifyAddedToCache(); |
| 548 | } else { |
| 549 | *bitmap = tmpBitmap; |
| 550 | bitmap->pixelRef()->setImmutableWithID(uniqueID); |
| 551 | } |
| 552 | |
| 553 | check_output_bitmap(*bitmap, uniqueID); |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 558 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 559 | bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 560 | int srcX, int srcY, CachingHint chint) const { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 561 | SkColorSpace* dstColorSpace = dstInfo.colorSpace(); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 562 | SkBitmap bm; |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 563 | if (kDisallow_CachingHint == chint) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 564 | CachedFormat cacheFormat = this->chooseCacheFormat(dstColorSpace); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 565 | SkImageInfo genPixelsInfo = dstInfo; |
| 566 | SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 567 | if (this->lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 568 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 569 | } else { |
| 570 | // Try passing the caller's buffer directly down to the generator. If this fails we |
| 571 | // may still succeed in the general case, as the generator may prefer some other |
| 572 | // config, which we could then convert via SkBitmap::readPixels. |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 573 | if (this->directGeneratePixels(genPixelsInfo, dstPixels, dstRB, srcX, srcY, behavior)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 574 | return true; |
| 575 | } |
| 576 | // else fall through |
| 577 | } |
| 578 | } |
| 579 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 580 | if (this->getROPixels(&bm, dstColorSpace, chint)) { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 581 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 582 | } |
| 583 | return false; |
| 584 | } |
| 585 | |
Ben Wagner | bdf5433 | 2018-05-15 14:12:14 -0400 | [diff] [blame] | 586 | sk_sp<SkData> SkImage_Lazy::onRefEncoded() const { |
| 587 | ScopedGenerator generator(fSharedGenerator); |
| 588 | return generator->refEncodedData(); |
| 589 | } |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 590 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 591 | bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace, |
| 592 | CachingHint chint) const { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 593 | CachedFormat cacheFormat = this->chooseCacheFormat(dstColorSpace); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 594 | const SkImageInfo cacheInfo = this->buildCacheInfo(cacheFormat); |
| 595 | SkImageInfo genPixelsInfo = cacheInfo; |
| 596 | SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo); |
| 597 | return this->lockAsBitmap(bitmap, chint, cacheFormat, genPixelsInfo, behavior); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 600 | bool SkImage_Lazy::onIsValid(GrContext* context) const { |
| 601 | ScopedGenerator generator(fSharedGenerator); |
| 602 | return generator->isValid(context); |
| 603 | } |
| 604 | |
Mike Reed | 7f1d020 | 2017-05-08 16:13:39 -0400 | [diff] [blame] | 605 | bool SkImage_Lazy::onCanLazyGenerateOnGPU() const { |
| 606 | #if SK_SUPPORT_GPU |
| 607 | ScopedGenerator generator(fSharedGenerator); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 608 | return SkImageGenerator::TexGenType::kNone != generator->onCanGenerateTexture(); |
Mike Reed | 7f1d020 | 2017-05-08 16:13:39 -0400 | [diff] [blame] | 609 | #else |
| 610 | return false; |
| 611 | #endif |
| 612 | } |
| 613 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 614 | SkTransferFunctionBehavior SkImage_Lazy::getGeneratorBehaviorAndInfo(SkImageInfo* generatorImageInfo) const { |
Brian Osman | bfc33e5 | 2018-06-27 14:24:11 -0400 | [diff] [blame^] | 615 | #ifdef SK_SUPPORT_LEGACY_LAZY_IMAGE_DECODE_BEHAVIOR |
Brian Osman | e269310 | 2018-06-26 19:19:49 +0000 | [diff] [blame] | 616 | if (generatorImageInfo->colorSpace()) { |
| 617 | return SkTransferFunctionBehavior::kRespect; |
| 618 | } |
| 619 | // Only specify an output color space if color conversion can be done on the color type. |
| 620 | switch (generatorImageInfo->colorType()) { |
| 621 | case kRGBA_8888_SkColorType: |
| 622 | case kBGRA_8888_SkColorType: |
| 623 | case kRGBA_F16_SkColorType: |
| 624 | case kRGB_565_SkColorType: |
| 625 | *generatorImageInfo = generatorImageInfo->makeColorSpace(fInfo.refColorSpace()); |
| 626 | break; |
| 627 | default: |
| 628 | break; |
| 629 | } |
Brian Osman | bfc33e5 | 2018-06-27 14:24:11 -0400 | [diff] [blame^] | 630 | #endif |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 631 | return SkTransferFunctionBehavior::kIgnore; |
| 632 | } |
| 633 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 634 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 635 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 636 | #if SK_SUPPORT_GPU |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 637 | sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrContext* context, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 638 | const GrSamplerState& params, |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 639 | SkColorSpace* dstColorSpace, |
| 640 | sk_sp<SkColorSpace>* texColorSpace, |
| 641 | SkScalar scaleAdjust[2]) const { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 642 | if (!context) { |
| 643 | return nullptr; |
| 644 | } |
| 645 | |
| 646 | GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint); |
| 647 | return textureMaker.refTextureProxyForParams(params, dstColorSpace, texColorSpace, scaleAdjust); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 648 | } |
| 649 | #endif |
| 650 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 651 | sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset) const { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 652 | SkASSERT(fInfo.bounds().contains(subset)); |
| 653 | SkASSERT(fInfo.bounds() != subset); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 654 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 655 | const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y()); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 656 | Validator validator(fSharedGenerator, &generatorSubset, fInfo.refColorSpace()); |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 657 | return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 660 | sk_sp<SkImage> SkImage_Lazy::onMakeColorSpace(sk_sp<SkColorSpace> target, |
| 661 | SkColorType targetColorType, |
| 662 | SkTransferFunctionBehavior premulBehavior) const { |
Christopher Cameron | d4b6787 | 2017-07-13 15:18:08 -0700 | [diff] [blame] | 663 | SkAutoExclusive autoAquire(fOnMakeColorSpaceMutex); |
| 664 | if (target && fOnMakeColorSpaceTarget && |
| 665 | SkColorSpace::Equals(target.get(), fOnMakeColorSpaceTarget.get())) { |
| 666 | return fOnMakeColorSpaceResult; |
| 667 | } |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 668 | const SkIRect generatorSubset = |
| 669 | SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height()); |
| 670 | Validator validator(fSharedGenerator, &generatorSubset, target); |
Christopher Cameron | d4b6787 | 2017-07-13 15:18:08 -0700 | [diff] [blame] | 671 | sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr; |
| 672 | if (result) { |
| 673 | fOnMakeColorSpaceTarget = target; |
| 674 | fOnMakeColorSpaceResult = result; |
| 675 | } |
| 676 | return result; |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 677 | } |
| 678 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 679 | sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator, |
| 680 | const SkIRect* subset) { |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 681 | SkImage_Lazy::Validator validator(SharedGenerator::Make(std::move(generator)), subset, nullptr); |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 682 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 683 | return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 684 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 685 | |
| 686 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 687 | |
| 688 | /** |
| 689 | * Implementation of SkImageCacherator interface, as needed by GrImageTextureMaker |
| 690 | */ |
| 691 | |
| 692 | #if SK_SUPPORT_GPU |
| 693 | |
| 694 | void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, CachedFormat format, |
| 695 | GrUniqueKey* cacheKey) { |
| 696 | SkASSERT(!cacheKey->isValid()); |
| 697 | if (origKey.isValid()) { |
| 698 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
Derek Sollenberger | e1c60d6 | 2018-04-04 11:53:35 -0400 | [diff] [blame] | 699 | GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 1, "Image"); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 700 | builder[0] = format; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | class Generator_GrYUVProvider : public GrYUVProvider { |
| 705 | SkImageGenerator* fGen; |
| 706 | |
| 707 | public: |
| 708 | Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {} |
| 709 | |
| 710 | uint32_t onGetID() override { return fGen->uniqueID(); } |
| 711 | bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override { |
| 712 | return fGen->queryYUV8(sizeInfo, colorSpace); |
| 713 | } |
| 714 | bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override { |
| 715 | return fGen->getYUV8Planes(sizeInfo, planes); |
| 716 | } |
| 717 | }; |
| 718 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 719 | static void set_key_on_proxy(GrProxyProvider* proxyProvider, |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 720 | GrTextureProxy* proxy, GrTextureProxy* originalProxy, |
| 721 | const GrUniqueKey& key) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 722 | if (key.isValid()) { |
Robert Phillips | 8a90f50 | 2017-07-24 15:09:56 -0400 | [diff] [blame] | 723 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 724 | if (originalProxy && originalProxy->getUniqueKey().isValid()) { |
| 725 | SkASSERT(originalProxy->getUniqueKey() == key); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 726 | SkASSERT(GrMipMapped::kYes == proxy->mipMapped() && |
| 727 | GrMipMapped::kNo == originalProxy->mipMapped()); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 728 | // If we had an originalProxy with a valid key, that means there already is a proxy in |
| 729 | // the cache which matches the key, but it does not have mip levels and we require them. |
| 730 | // Thus we must remove the unique key from that proxy. |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 731 | proxyProvider->removeUniqueKeyFromProxy(key, originalProxy); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 732 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 733 | proxyProvider->assignUniqueKeyToProxy(key, proxy); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | sk_sp<SkColorSpace> SkImage_Lazy::getColorSpace(GrContext* ctx, SkColorSpace* dstColorSpace) { |
Brian Osman | a8ac924 | 2017-09-07 10:19:08 -0400 | [diff] [blame] | 738 | if (!dstColorSpace) { |
| 739 | // In legacy mode, we do no modification to the image's color space or encoding. |
| 740 | // Subsequent legacy drawing is likely to ignore the color space, but some clients |
| 741 | // may want to know what space the image data is in, so return it. |
| 742 | return fInfo.refColorSpace(); |
| 743 | } else { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 744 | CachedFormat format = this->chooseCacheFormat(dstColorSpace, ctx->contextPriv().caps()); |
Brian Osman | a8ac924 | 2017-09-07 10:19:08 -0400 | [diff] [blame] | 745 | SkImageInfo cacheInfo = this->buildCacheInfo(format); |
| 746 | return cacheInfo.refColorSpace(); |
| 747 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /* |
| 751 | * We have 4 ways to try to return a texture (in sorted order) |
| 752 | * |
| 753 | * 1. Check the cache for a pre-existing one |
| 754 | * 2. Ask the generator to natively create one |
| 755 | * 3. Ask the generator to return YUV planes, which the GPU can convert |
| 756 | * 4. Ask the generator to return RGB(A) data, which the GPU can convert |
| 757 | */ |
| 758 | sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(GrContext* ctx, |
| 759 | const GrUniqueKey& origKey, |
| 760 | SkImage::CachingHint chint, |
| 761 | bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 762 | SkColorSpace* dstColorSpace, |
| 763 | GrTextureMaker::AllowedTexGenType genType) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 764 | // Values representing the various texture lock paths we can take. Used for logging the path |
| 765 | // taken to a histogram. |
| 766 | enum LockTexturePath { |
| 767 | kFailure_LockTexturePath, |
| 768 | kPreExisting_LockTexturePath, |
| 769 | kNative_LockTexturePath, |
| 770 | kCompressed_LockTexturePath, // Deprecated |
| 771 | kYUV_LockTexturePath, |
| 772 | kRGBA_LockTexturePath, |
| 773 | }; |
| 774 | |
| 775 | enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 }; |
| 776 | |
| 777 | // Determine which cached format we're going to use (which may involve decoding to a different |
| 778 | // info than the generator provides). |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 779 | CachedFormat format = this->chooseCacheFormat(dstColorSpace, ctx->contextPriv().caps()); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 780 | |
| 781 | // Fold the cache format into our texture key |
| 782 | GrUniqueKey key; |
| 783 | this->makeCacheKeyFromOrigKey(origKey, format, &key); |
| 784 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 785 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 786 | sk_sp<GrTextureProxy> proxy; |
| 787 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 788 | // 1. Check the cache for a pre-existing one |
| 789 | if (key.isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 790 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 791 | if (proxy) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 792 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath, |
| 793 | kLockTexturePathCount); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 794 | if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 795 | return proxy; |
| 796 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
| 800 | // The CachedFormat is both an index for which cache "slot" we'll use to store this particular |
| 801 | // decoded variant of the encoded data, and also a recipe for how to transform the original |
| 802 | // info to get the one that we're going to decode to. |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 803 | const SkImageInfo cacheInfo = this->buildCacheInfo(format); |
| 804 | SkImageInfo genPixelsInfo = cacheInfo; |
| 805 | SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 806 | |
| 807 | // 2. Ask the generator to natively create one |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 808 | if (!proxy) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 809 | ScopedGenerator generator(fSharedGenerator); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 810 | if (GrTextureMaker::AllowedTexGenType::kCheap == genType && |
| 811 | SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) { |
| 812 | return nullptr; |
| 813 | } |
Greg Daniel | f88c12e | 2017-10-09 09:57:35 -0400 | [diff] [blame] | 814 | if ((proxy = generator->generateTexture(ctx, genPixelsInfo, fOrigin, behavior, |
| 815 | willBeMipped))) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 816 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath, |
| 817 | kLockTexturePathCount); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 818 | set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 819 | if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 820 | return proxy; |
| 821 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Greg Daniel | 3e70fa3 | 2017-10-05 16:27:06 -0400 | [diff] [blame] | 825 | // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping |
| 826 | // the texture we fall through here and have the CPU generate the mip maps for us. |
| 827 | if (!proxy && !willBeMipped && !ctx->contextPriv().disableGpuYUVConversion()) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 828 | const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(cacheInfo); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 829 | ScopedGenerator generator(fSharedGenerator); |
| 830 | Generator_GrYUVProvider provider(generator); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 831 | |
| 832 | // The pixels in the texture will be in the generator's color space. If onMakeColorSpace |
| 833 | // has been called then this will not match this image's color space. To correct this, apply |
| 834 | // a color space conversion from the generator's color space to this image's color space. |
Brian Osman | 56893cd | 2018-06-08 14:11:37 -0400 | [diff] [blame] | 835 | // Note that we can only do this conversion (on the GPU) if both color spaces are XYZ type. |
Brian Osman | 861ea5b | 2018-06-14 09:14:03 -0400 | [diff] [blame] | 836 | SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace(); |
| 837 | SkColorSpace* thisColorSpace = fInfo.colorSpace(); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 838 | |
Brian Osman | 56893cd | 2018-06-08 14:11:37 -0400 | [diff] [blame] | 839 | if ((!generatorColorSpace || generatorColorSpace->toXYZD50()) && |
| 840 | (!thisColorSpace || thisColorSpace->toXYZD50())) { |
| 841 | // TODO: Update to create the mipped surface in the YUV generator and draw the base |
| 842 | // layer directly into the mipped surface. |
| 843 | proxy = provider.refAsTextureProxy(ctx, desc, generatorColorSpace, thisColorSpace); |
| 844 | if (proxy) { |
| 845 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath, |
| 846 | kLockTexturePathCount); |
| 847 | set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key); |
| 848 | return proxy; |
| 849 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | // 4. Ask the generator to return RGB(A) data, which the GPU can convert |
| 854 | SkBitmap bitmap; |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 855 | if (!proxy && this->lockAsBitmap(&bitmap, chint, format, genPixelsInfo, behavior)) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 856 | if (willBeMipped) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 857 | proxy = proxyProvider->createMipMapProxyFromBitmap(bitmap); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 858 | } |
| 859 | if (!proxy) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 860 | proxy = GrUploadBitmapToTextureProxy(proxyProvider, bitmap); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 861 | } |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 862 | if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 863 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath, |
| 864 | kLockTexturePathCount); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 865 | set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 866 | return proxy; |
| 867 | } |
| 868 | } |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 869 | |
| 870 | if (proxy) { |
| 871 | // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated |
| 872 | // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new |
| 873 | // mipped surface and copy the original proxy into the base layer. We will then let the gpu |
| 874 | // generate the rest of the mips. |
| 875 | SkASSERT(willBeMipped); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 876 | SkASSERT(GrMipMapped::kNo == proxy->mipMapped()); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 877 | if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 878 | set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 879 | return mippedProxy; |
| 880 | } |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 881 | // We failed to make a mipped proxy with the base copied into it. This could have |
| 882 | // been from failure to make the proxy or failure to do the copy. Thus we will fall |
| 883 | // back to just using the non mipped proxy; See skbug.com/7094. |
| 884 | return proxy; |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 887 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath, |
| 888 | kLockTexturePathCount); |
| 889 | return nullptr; |
| 890 | } |
| 891 | |
| 892 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 893 | |
| 894 | #endif |