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