reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkImage_Base.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 9 | #include "SkImageCacherator.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 10 | |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkBitmapCache.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 13 | #include "SkData.h" |
| 14 | #include "SkImageGenerator.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 15 | #include "SkImagePriv.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 16 | #include "SkNextID.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 17 | #include "SkPixelRef.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 18 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
| 20 | #include "GrContext.h" |
| 21 | #include "GrContextPriv.h" |
| 22 | #include "GrGpuResourcePriv.h" |
| 23 | #include "GrImageTextureMaker.h" |
| 24 | #include "GrResourceKey.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 25 | #include "GrProxyProvider.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 26 | #include "GrSamplerState.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 27 | #include "GrYUVProvider.h" |
| 28 | #include "SkGr.h" |
| 29 | #endif |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 30 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 31 | // Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images |
| 32 | class SharedGenerator final : public SkNVRefCnt<SharedGenerator> { |
| 33 | public: |
| 34 | static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen) { |
| 35 | return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr; |
| 36 | } |
| 37 | |
Matt Sarett | b2004f7 | 2017-05-18 09:26:50 -0400 | [diff] [blame] | 38 | // This is thread safe. It is a const field set in the constructor. |
| 39 | const SkImageInfo& getInfo() { return fGenerator->getInfo(); } |
| 40 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 41 | private: |
| 42 | explicit SharedGenerator(std::unique_ptr<SkImageGenerator> gen) |
| 43 | : fGenerator(std::move(gen)) { |
| 44 | SkASSERT(fGenerator); |
| 45 | } |
| 46 | |
| 47 | friend class ScopedGenerator; |
| 48 | friend class SkImage_Lazy; |
| 49 | |
| 50 | std::unique_ptr<SkImageGenerator> fGenerator; |
| 51 | SkMutex fMutex; |
| 52 | }; |
| 53 | |
| 54 | class SkImage_Lazy : public SkImage_Base, public SkImageCacherator { |
| 55 | public: |
| 56 | struct Validator { |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 57 | Validator(sk_sp<SharedGenerator>, const SkIRect* subset, sk_sp<SkColorSpace> colorSpace); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 58 | |
| 59 | operator bool() const { return fSharedGenerator.get(); } |
| 60 | |
| 61 | sk_sp<SharedGenerator> fSharedGenerator; |
| 62 | SkImageInfo fInfo; |
| 63 | SkIPoint fOrigin; |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 64 | sk_sp<SkColorSpace> fColorSpace; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 65 | uint32_t fUniqueID; |
| 66 | }; |
| 67 | |
| 68 | SkImage_Lazy(Validator* validator); |
| 69 | |
| 70 | SkImageInfo onImageInfo() const override { |
| 71 | return fInfo; |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 72 | } |
Greg Daniel | 56008aa | 2018-03-14 15:33:42 -0400 | [diff] [blame] | 73 | SkColorType onColorType() const override { |
| 74 | return kUnknown_SkColorType; |
| 75 | } |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 76 | SkAlphaType onAlphaType() const override { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 77 | return fInfo.alphaType(); |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 78 | } |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 79 | |
Mike Reed | f2c7364 | 2018-05-29 15:41:27 -0400 | [diff] [blame] | 80 | SkIRect onGetSubset() const override { |
| 81 | return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, fInfo.width(), fInfo.height()); |
| 82 | } |
| 83 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 84 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, |
| 85 | CachingHint) const override; |
| 86 | #if SK_SUPPORT_GPU |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 87 | sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, |
| 88 | const GrSamplerState&, SkColorSpace*, |
| 89 | sk_sp<SkColorSpace>*, |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 90 | SkScalar scaleAdjust[2]) const override; |
| 91 | #endif |
Ben Wagner | bdf5433 | 2018-05-15 14:12:14 -0400 | [diff] [blame] | 92 | sk_sp<SkData> onRefEncoded() const override; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 93 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 94 | bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 95 | bool onIsLazyGenerated() const override { return true; } |
Mike Reed | 7f1d020 | 2017-05-08 16:13:39 -0400 | [diff] [blame] | 96 | bool onCanLazyGenerateOnGPU() const override; |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 97 | sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 98 | |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 99 | bool onIsValid(GrContext*) const override; |
| 100 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 101 | SkImageCacherator* peekCacherator() const override { |
| 102 | return const_cast<SkImage_Lazy*>(this); |
| 103 | } |
| 104 | |
| 105 | // Only return true if the generate has already been cached. |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 106 | bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*) const; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 107 | // Call the underlying generator directly |
| 108 | bool directGeneratePixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 109 | int srcX, int srcY) const; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 110 | |
| 111 | // SkImageCacherator interface |
| 112 | #if SK_SUPPORT_GPU |
| 113 | // Returns the texture proxy. If the cacherator is generating the texture and wants to cache it, |
| 114 | // it should use the passed in key (if the key is valid). |
| 115 | sk_sp<GrTextureProxy> lockTextureProxy(GrContext*, |
| 116 | const GrUniqueKey& key, |
| 117 | SkImage::CachingHint, |
| 118 | bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 119 | SkColorSpace* dstColorSpace, |
| 120 | GrTextureMaker::AllowedTexGenType genType) override; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 121 | |
| 122 | // Returns the color space of the texture that would be returned if you called lockTexture. |
| 123 | // Separate code path to allow querying of the color space for textures that cached (even |
| 124 | // externally). |
| 125 | sk_sp<SkColorSpace> getColorSpace(GrContext*, SkColorSpace* dstColorSpace) override; |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 126 | |
| 127 | // TODO: Need to pass in dstColorSpace to fold into key here? |
| 128 | void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) override; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 129 | #endif |
| 130 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 131 | SkImageInfo buildCacheInfo() const override; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 132 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 133 | private: |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 134 | class ScopedGenerator; |
| 135 | |
| 136 | /** |
| 137 | * On success (true), bitmap will point to the pixels for this generator. If this returns |
| 138 | * false, the bitmap will be reset to empty. |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 139 | * TODO: Pass in dstColorSpace to ensure bitmap is compatible? |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 140 | */ |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 141 | bool lockAsBitmap(SkBitmap*, SkImage::CachingHint, const SkImageInfo&) const; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 142 | |
| 143 | sk_sp<SharedGenerator> fSharedGenerator; |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 144 | // Note that fInfo is not necessarily the info from the generator. It may be cropped by |
| 145 | // onMakeSubset and its color space may be changed by onMakeColorSpace. |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 146 | const SkImageInfo fInfo; |
| 147 | const SkIPoint fOrigin; |
| 148 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 149 | uint32_t fUniqueID; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 150 | |
Christopher Cameron | d4b6787 | 2017-07-13 15:18:08 -0700 | [diff] [blame] | 151 | // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and |
| 152 | // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call. |
| 153 | mutable SkMutex fOnMakeColorSpaceMutex; |
| 154 | mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget; |
| 155 | mutable sk_sp<SkImage> fOnMakeColorSpaceResult; |
| 156 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 157 | typedef SkImage_Base INHERITED; |
| 158 | }; |
| 159 | |
| 160 | /////////////////////////////////////////////////////////////////////////////// |
| 161 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 162 | SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset, |
| 163 | sk_sp<SkColorSpace> colorSpace) |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 164 | : fSharedGenerator(std::move(gen)) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 165 | if (!fSharedGenerator) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | // The following generator accessors are safe without acquiring the mutex (const getters). |
| 170 | // TODO: refactor to use a ScopedGenerator instead, for clarity. |
| 171 | const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo(); |
| 172 | if (info.isEmpty()) { |
| 173 | fSharedGenerator.reset(); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | fUniqueID = fSharedGenerator->fGenerator->uniqueID(); |
| 178 | const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height()); |
| 179 | if (subset) { |
| 180 | if (!bounds.contains(*subset)) { |
| 181 | fSharedGenerator.reset(); |
| 182 | return; |
| 183 | } |
| 184 | if (*subset != bounds) { |
| 185 | // we need a different uniqueID since we really are a subset of the raw generator |
| 186 | fUniqueID = SkNextID::ImageID(); |
| 187 | } |
| 188 | } else { |
| 189 | subset = &bounds; |
| 190 | } |
| 191 | |
| 192 | fInfo = info.makeWH(subset->width(), subset->height()); |
| 193 | fOrigin = SkIPoint::Make(subset->x(), subset->y()); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 194 | if (colorSpace) { |
| 195 | fInfo = fInfo.makeColorSpace(colorSpace); |
| 196 | fUniqueID = SkNextID::ImageID(); |
| 197 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /////////////////////////////////////////////////////////////////////////////// |
| 201 | |
| 202 | // Helper for exclusive access to a shared generator. |
| 203 | class SkImage_Lazy::ScopedGenerator { |
| 204 | public: |
| 205 | ScopedGenerator(const sk_sp<SharedGenerator>& gen) |
| 206 | : fSharedGenerator(gen) |
| 207 | , fAutoAquire(gen->fMutex) {} |
| 208 | |
| 209 | SkImageGenerator* operator->() const { |
| 210 | fSharedGenerator->fMutex.assertHeld(); |
| 211 | return fSharedGenerator->fGenerator.get(); |
| 212 | } |
| 213 | |
| 214 | operator SkImageGenerator*() const { |
| 215 | fSharedGenerator->fMutex.assertHeld(); |
| 216 | return fSharedGenerator->fGenerator.get(); |
| 217 | } |
| 218 | |
| 219 | private: |
| 220 | const sk_sp<SharedGenerator>& fSharedGenerator; |
| 221 | SkAutoExclusive fAutoAquire; |
| 222 | }; |
| 223 | |
| 224 | /////////////////////////////////////////////////////////////////////////////// |
| 225 | |
| 226 | SkImage_Lazy::SkImage_Lazy(Validator* validator) |
| 227 | : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID) |
| 228 | , fSharedGenerator(std::move(validator->fSharedGenerator)) |
| 229 | , fInfo(validator->fInfo) |
| 230 | , fOrigin(validator->fOrigin) { |
| 231 | SkASSERT(fSharedGenerator); |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 232 | fUniqueID = validator->fUniqueID; |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 236 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 237 | SkImageInfo SkImage_Lazy::buildCacheInfo() const { |
Brian Osman | bfc33e5 | 2018-06-27 14:24:11 -0400 | [diff] [blame] | 238 | if (kGray_8_SkColorType == fInfo.colorType()) { |
| 239 | return fInfo.makeColorSpace(nullptr); |
| 240 | } else { |
| 241 | return fInfo; |
| 242 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 246 | |
| 247 | static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { |
| 248 | SkASSERT(bitmap.getGenerationID() == expectedID); |
| 249 | SkASSERT(bitmap.isImmutable()); |
| 250 | SkASSERT(bitmap.getPixels()); |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | bool SkImage_Lazy::directGeneratePixels(const SkImageInfo& info, void* pixels, size_t rb, |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 255 | int srcX, int srcY) const { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 256 | ScopedGenerator generator(fSharedGenerator); |
| 257 | const SkImageInfo& genInfo = generator->getInfo(); |
| 258 | // Currently generators do not natively handle subsets, so check that first. |
| 259 | if (srcX || srcY || genInfo.width() != info.width() || genInfo.height() != info.height()) { |
| 260 | return false; |
| 261 | } |
| 262 | |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 263 | return generator->getPixels(info, pixels, rb); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 267 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 268 | bool SkImage_Lazy::lockAsBitmapOnlyIfAlreadyCached(SkBitmap* bitmap) const { |
| 269 | return SkBitmapCache::Find(SkBitmapCacheDesc::Make(fUniqueID, |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 270 | fInfo.width(), fInfo.height()), bitmap) && |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 271 | check_output_bitmap(*bitmap, fUniqueID); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 274 | static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 275 | const int genW = gen->getInfo().width(); |
| 276 | const int genH = gen->getInfo().height(); |
| 277 | const SkIRect srcR = SkIRect::MakeWH(genW, genH); |
| 278 | const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height()); |
| 279 | if (!srcR.contains(dstR)) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | // If they are requesting a subset, we have to have a temp allocation for full image, and |
| 284 | // then copy the subset into their allocation |
| 285 | SkBitmap full; |
| 286 | SkPixmap fullPM; |
| 287 | const SkPixmap* dstPM = &pmap; |
| 288 | if (srcR != dstR) { |
| 289 | if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) { |
| 290 | return false; |
| 291 | } |
| 292 | if (!full.peekPixels(&fullPM)) { |
| 293 | return false; |
| 294 | } |
| 295 | dstPM = &fullPM; |
| 296 | } |
| 297 | |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 298 | if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes())) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 299 | return false; |
| 300 | } |
| 301 | |
| 302 | if (srcR != dstR) { |
| 303 | if (!full.readPixels(pmap, originX, originY)) { |
| 304 | return false; |
| 305 | } |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 310 | bool SkImage_Lazy::lockAsBitmap(SkBitmap* bitmap, SkImage::CachingHint chint, |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 311 | const SkImageInfo& info) const { |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 312 | // TODO: Verify dstColorSpace here |
| 313 | if (this->lockAsBitmapOnlyIfAlreadyCached(bitmap)) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 317 | SkBitmap tmpBitmap; |
| 318 | SkBitmapCache::RecPtr cacheRec; |
| 319 | SkPixmap pmap; |
| 320 | if (SkImage::kAllow_CachingHint == chint) { |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 321 | auto desc = SkBitmapCacheDesc::Make(fUniqueID, info.width(), info.height()); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 322 | cacheRec = SkBitmapCache::Alloc(desc, info, &pmap); |
| 323 | if (!cacheRec) { |
| 324 | return false; |
| 325 | } |
| 326 | } else { |
| 327 | if (!tmpBitmap.tryAllocPixels(info)) { |
| 328 | return false; |
| 329 | } |
| 330 | if (!tmpBitmap.peekPixels(&pmap)) { |
| 331 | return false; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | ScopedGenerator generator(fSharedGenerator); |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 336 | if (!generate_pixels(generator, pmap, fOrigin.x(), fOrigin.y())) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | |
| 340 | if (cacheRec) { |
| 341 | SkBitmapCache::Add(std::move(cacheRec), bitmap); |
| 342 | SkASSERT(bitmap->getPixels()); // we're locked |
| 343 | SkASSERT(bitmap->isImmutable()); |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 344 | SkASSERT(bitmap->getGenerationID() == fUniqueID); |
Mike Reed | 30301c4 | 2018-07-19 09:39:21 -0400 | [diff] [blame] | 345 | this->notifyAddedToRasterCache(); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 346 | } else { |
| 347 | *bitmap = tmpBitmap; |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 348 | bitmap->pixelRef()->setImmutableWithID(fUniqueID); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 351 | check_output_bitmap(*bitmap, fUniqueID); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | |
| 355 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 356 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 357 | bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 358 | int srcX, int srcY, CachingHint chint) const { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 359 | SkColorSpace* dstColorSpace = dstInfo.colorSpace(); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 360 | SkBitmap bm; |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 361 | if (kDisallow_CachingHint == chint) { |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 362 | if (this->lockAsBitmapOnlyIfAlreadyCached(&bm)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 363 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 364 | } else { |
| 365 | // Try passing the caller's buffer directly down to the generator. If this fails we |
| 366 | // may still succeed in the general case, as the generator may prefer some other |
| 367 | // config, which we could then convert via SkBitmap::readPixels. |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 368 | if (this->directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 369 | return true; |
| 370 | } |
| 371 | // else fall through |
| 372 | } |
| 373 | } |
| 374 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 375 | if (this->getROPixels(&bm, dstColorSpace, chint)) { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 376 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 377 | } |
| 378 | return false; |
| 379 | } |
| 380 | |
Ben Wagner | bdf5433 | 2018-05-15 14:12:14 -0400 | [diff] [blame] | 381 | sk_sp<SkData> SkImage_Lazy::onRefEncoded() const { |
| 382 | ScopedGenerator generator(fSharedGenerator); |
| 383 | return generator->refEncodedData(); |
| 384 | } |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 385 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 386 | bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace, |
| 387 | CachingHint chint) const { |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 388 | const SkImageInfo cacheInfo = this->buildCacheInfo(); |
| 389 | return this->lockAsBitmap(bitmap, chint, cacheInfo); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Brian Osman | 5bbd076 | 2017-05-08 11:07:42 -0400 | [diff] [blame] | 392 | bool SkImage_Lazy::onIsValid(GrContext* context) const { |
| 393 | ScopedGenerator generator(fSharedGenerator); |
| 394 | return generator->isValid(context); |
| 395 | } |
| 396 | |
Mike Reed | 7f1d020 | 2017-05-08 16:13:39 -0400 | [diff] [blame] | 397 | bool SkImage_Lazy::onCanLazyGenerateOnGPU() const { |
| 398 | #if SK_SUPPORT_GPU |
| 399 | ScopedGenerator generator(fSharedGenerator); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 400 | return SkImageGenerator::TexGenType::kNone != generator->onCanGenerateTexture(); |
Mike Reed | 7f1d020 | 2017-05-08 16:13:39 -0400 | [diff] [blame] | 401 | #else |
| 402 | return false; |
| 403 | #endif |
| 404 | } |
| 405 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 406 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 407 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 408 | #if SK_SUPPORT_GPU |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 409 | sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrContext* context, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 410 | const GrSamplerState& params, |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 411 | SkColorSpace* dstColorSpace, |
| 412 | sk_sp<SkColorSpace>* texColorSpace, |
| 413 | SkScalar scaleAdjust[2]) const { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 414 | if (!context) { |
| 415 | return nullptr; |
| 416 | } |
| 417 | |
| 418 | GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint); |
| 419 | return textureMaker.refTextureProxyForParams(params, dstColorSpace, texColorSpace, scaleAdjust); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 420 | } |
| 421 | #endif |
| 422 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 423 | sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset) const { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 424 | SkASSERT(fInfo.bounds().contains(subset)); |
| 425 | SkASSERT(fInfo.bounds() != subset); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 426 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 427 | const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y()); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 428 | Validator validator(fSharedGenerator, &generatorSubset, fInfo.refColorSpace()); |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 429 | return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 432 | sk_sp<SkImage> SkImage_Lazy::onMakeColorSpace(sk_sp<SkColorSpace> target, |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 433 | SkColorType targetColorType) const { |
Christopher Cameron | d4b6787 | 2017-07-13 15:18:08 -0700 | [diff] [blame] | 434 | SkAutoExclusive autoAquire(fOnMakeColorSpaceMutex); |
| 435 | if (target && fOnMakeColorSpaceTarget && |
| 436 | SkColorSpace::Equals(target.get(), fOnMakeColorSpaceTarget.get())) { |
| 437 | return fOnMakeColorSpaceResult; |
| 438 | } |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 439 | const SkIRect generatorSubset = |
| 440 | SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height()); |
| 441 | Validator validator(fSharedGenerator, &generatorSubset, target); |
Christopher Cameron | d4b6787 | 2017-07-13 15:18:08 -0700 | [diff] [blame] | 442 | sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr; |
| 443 | if (result) { |
| 444 | fOnMakeColorSpaceTarget = target; |
| 445 | fOnMakeColorSpaceResult = result; |
| 446 | } |
| 447 | return result; |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 448 | } |
| 449 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 450 | sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator, |
| 451 | const SkIRect* subset) { |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 452 | SkImage_Lazy::Validator validator(SharedGenerator::Make(std::move(generator)), subset, nullptr); |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 453 | |
Brian Osman | f1b4382 | 2017-04-20 13:43:23 -0400 | [diff] [blame] | 454 | return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 455 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 456 | |
| 457 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 458 | |
| 459 | /** |
| 460 | * Implementation of SkImageCacherator interface, as needed by GrImageTextureMaker |
| 461 | */ |
| 462 | |
| 463 | #if SK_SUPPORT_GPU |
| 464 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 465 | void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) { |
| 466 | // TODO: Take dstColorSpace, include hash in key |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 467 | SkASSERT(!cacheKey->isValid()); |
| 468 | if (origKey.isValid()) { |
| 469 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 470 | GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image"); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | class Generator_GrYUVProvider : public GrYUVProvider { |
| 475 | SkImageGenerator* fGen; |
| 476 | |
| 477 | public: |
| 478 | Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {} |
| 479 | |
| 480 | uint32_t onGetID() override { return fGen->uniqueID(); } |
| 481 | bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override { |
| 482 | return fGen->queryYUV8(sizeInfo, colorSpace); |
| 483 | } |
| 484 | bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override { |
| 485 | return fGen->getYUV8Planes(sizeInfo, planes); |
| 486 | } |
| 487 | }; |
| 488 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 489 | static void set_key_on_proxy(GrProxyProvider* proxyProvider, |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 490 | GrTextureProxy* proxy, GrTextureProxy* originalProxy, |
| 491 | const GrUniqueKey& key) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 492 | if (key.isValid()) { |
Robert Phillips | 8a90f50 | 2017-07-24 15:09:56 -0400 | [diff] [blame] | 493 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 494 | if (originalProxy && originalProxy->getUniqueKey().isValid()) { |
| 495 | SkASSERT(originalProxy->getUniqueKey() == key); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 496 | SkASSERT(GrMipMapped::kYes == proxy->mipMapped() && |
| 497 | GrMipMapped::kNo == originalProxy->mipMapped()); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 498 | // If we had an originalProxy with a valid key, that means there already is a proxy in |
| 499 | // the cache which matches the key, but it does not have mip levels and we require them. |
| 500 | // Thus we must remove the unique key from that proxy. |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 501 | proxyProvider->removeUniqueKeyFromProxy(key, originalProxy); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 502 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 503 | proxyProvider->assignUniqueKeyToProxy(key, proxy); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | sk_sp<SkColorSpace> SkImage_Lazy::getColorSpace(GrContext* ctx, SkColorSpace* dstColorSpace) { |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 508 | // TODO: Is this ever needed? Is the output of this function going to be: |
| 509 | // return dstColorSpace ? fInfo.refColorSpace() : dstColorSpace; |
| 510 | // Yes, except for gray? |
Brian Osman | a8ac924 | 2017-09-07 10:19:08 -0400 | [diff] [blame] | 511 | if (!dstColorSpace) { |
| 512 | // In legacy mode, we do no modification to the image's color space or encoding. |
| 513 | // Subsequent legacy drawing is likely to ignore the color space, but some clients |
| 514 | // may want to know what space the image data is in, so return it. |
| 515 | return fInfo.refColorSpace(); |
| 516 | } else { |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 517 | SkImageInfo cacheInfo = this->buildCacheInfo(); |
Brian Osman | a8ac924 | 2017-09-07 10:19:08 -0400 | [diff] [blame] | 518 | return cacheInfo.refColorSpace(); |
| 519 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* |
| 523 | * We have 4 ways to try to return a texture (in sorted order) |
| 524 | * |
| 525 | * 1. Check the cache for a pre-existing one |
| 526 | * 2. Ask the generator to natively create one |
| 527 | * 3. Ask the generator to return YUV planes, which the GPU can convert |
| 528 | * 4. Ask the generator to return RGB(A) data, which the GPU can convert |
| 529 | */ |
| 530 | sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(GrContext* ctx, |
| 531 | const GrUniqueKey& origKey, |
| 532 | SkImage::CachingHint chint, |
| 533 | bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 534 | SkColorSpace* dstColorSpace, |
| 535 | GrTextureMaker::AllowedTexGenType genType) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 536 | // Values representing the various texture lock paths we can take. Used for logging the path |
| 537 | // taken to a histogram. |
| 538 | enum LockTexturePath { |
| 539 | kFailure_LockTexturePath, |
| 540 | kPreExisting_LockTexturePath, |
| 541 | kNative_LockTexturePath, |
| 542 | kCompressed_LockTexturePath, // Deprecated |
| 543 | kYUV_LockTexturePath, |
| 544 | kRGBA_LockTexturePath, |
| 545 | }; |
| 546 | |
| 547 | enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 }; |
| 548 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 549 | // Build our texture key. |
| 550 | // TODO: This needs to include the dstColorSpace. |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 551 | GrUniqueKey key; |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 552 | this->makeCacheKeyFromOrigKey(origKey, &key); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 553 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 554 | GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider(); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 555 | sk_sp<GrTextureProxy> proxy; |
| 556 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 557 | // 1. Check the cache for a pre-existing one |
| 558 | if (key.isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 559 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 560 | if (proxy) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 561 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath, |
| 562 | kLockTexturePathCount); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 563 | if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 564 | return proxy; |
| 565 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 569 | // What format are we going to ask the generator to create? |
| 570 | // TODO: Based on the dstColorSpace? |
| 571 | const SkImageInfo cacheInfo = this->buildCacheInfo(); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 572 | |
| 573 | // 2. Ask the generator to natively create one |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 574 | if (!proxy) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 575 | ScopedGenerator generator(fSharedGenerator); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 576 | if (GrTextureMaker::AllowedTexGenType::kCheap == genType && |
| 577 | SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) { |
| 578 | return nullptr; |
| 579 | } |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 580 | if ((proxy = generator->generateTexture(ctx, cacheInfo, fOrigin, willBeMipped))) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 581 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath, |
| 582 | kLockTexturePathCount); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 583 | set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 584 | if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 585 | return proxy; |
| 586 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Greg Daniel | 3e70fa3 | 2017-10-05 16:27:06 -0400 | [diff] [blame] | 590 | // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping |
| 591 | // the texture we fall through here and have the CPU generate the mip maps for us. |
| 592 | if (!proxy && !willBeMipped && !ctx->contextPriv().disableGpuYUVConversion()) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 593 | const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(cacheInfo); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 594 | ScopedGenerator generator(fSharedGenerator); |
| 595 | Generator_GrYUVProvider provider(generator); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 596 | |
| 597 | // The pixels in the texture will be in the generator's color space. If onMakeColorSpace |
| 598 | // has been called then this will not match this image's color space. To correct this, apply |
| 599 | // a color space conversion from the generator's color space to this image's color space. |
Brian Osman | 56893cd | 2018-06-08 14:11:37 -0400 | [diff] [blame] | 600 | // Note that we can only do this conversion (on the GPU) if both color spaces are XYZ type. |
Brian Osman | 861ea5b | 2018-06-14 09:14:03 -0400 | [diff] [blame] | 601 | SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace(); |
| 602 | SkColorSpace* thisColorSpace = fInfo.colorSpace(); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 603 | |
Brian Osman | 56893cd | 2018-06-08 14:11:37 -0400 | [diff] [blame] | 604 | if ((!generatorColorSpace || generatorColorSpace->toXYZD50()) && |
| 605 | (!thisColorSpace || thisColorSpace->toXYZD50())) { |
| 606 | // TODO: Update to create the mipped surface in the YUV generator and draw the base |
| 607 | // layer directly into the mipped surface. |
| 608 | proxy = provider.refAsTextureProxy(ctx, desc, generatorColorSpace, thisColorSpace); |
| 609 | if (proxy) { |
| 610 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath, |
| 611 | kLockTexturePathCount); |
| 612 | set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key); |
| 613 | return proxy; |
| 614 | } |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
| 618 | // 4. Ask the generator to return RGB(A) data, which the GPU can convert |
| 619 | SkBitmap bitmap; |
Brian Osman | eb7e529 | 2018-08-08 14:32:06 -0400 | [diff] [blame] | 620 | if (!proxy && this->lockAsBitmap(&bitmap, chint, cacheInfo)) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 621 | if (willBeMipped) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 622 | proxy = proxyProvider->createMipMapProxyFromBitmap(bitmap); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 623 | } |
| 624 | if (!proxy) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 625 | proxy = GrUploadBitmapToTextureProxy(proxyProvider, bitmap); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 626 | } |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 627 | if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) { |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 628 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath, |
| 629 | kLockTexturePathCount); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 630 | set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 631 | return proxy; |
| 632 | } |
| 633 | } |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 634 | |
| 635 | if (proxy) { |
| 636 | // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated |
| 637 | // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new |
| 638 | // mipped surface and copy the original proxy into the base layer. We will then let the gpu |
| 639 | // generate the rest of the mips. |
| 640 | SkASSERT(willBeMipped); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 641 | SkASSERT(GrMipMapped::kNo == proxy->mipMapped()); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 642 | if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 643 | set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 644 | return mippedProxy; |
| 645 | } |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 646 | // We failed to make a mipped proxy with the base copied into it. This could have |
| 647 | // been from failure to make the proxy or failure to do the copy. Thus we will fall |
| 648 | // back to just using the non mipped proxy; See skbug.com/7094. |
| 649 | return proxy; |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 652 | SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath, |
| 653 | kLockTexturePathCount); |
| 654 | return nullptr; |
| 655 | } |
| 656 | |
| 657 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 658 | |
| 659 | #endif |