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