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