• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/image/SkImage_Lazy.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImageGenerator.h"
13 #include "src/core/SkBitmapCache.h"
14 #include "src/core/SkCachedData.h"
15 #include "src/core/SkImagePriv.h"
16 #include "src/core/SkNextID.h"
17 
18 #if SK_SUPPORT_GPU
19 #include "include/private/GrRecordingContext.h"
20 #include "include/private/GrResourceKey.h"
21 #include "src/gpu/GrBitmapTextureMaker.h"
22 #include "src/gpu/GrCaps.h"
23 #include "src/gpu/GrGpuResourcePriv.h"
24 #include "src/gpu/GrImageTextureMaker.h"
25 #include "src/gpu/GrProxyProvider.h"
26 #include "src/gpu/GrRecordingContextPriv.h"
27 #include "src/gpu/GrSamplerState.h"
28 #include "src/gpu/GrYUVProvider.h"
29 #include "src/gpu/SkGr.h"
30 #endif
31 
32 // Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images
33 class SharedGenerator final : public SkNVRefCnt<SharedGenerator> {
34 public:
Make(std::unique_ptr<SkImageGenerator> gen)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     // This is thread safe.  It is a const field set in the constructor.
getInfo()40     const SkImageInfo& getInfo() { return fGenerator->getInfo(); }
41 
42 private:
SharedGenerator(std::unique_ptr<SkImageGenerator> gen)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 ///////////////////////////////////////////////////////////////////////////////
56 
Validator(sk_sp<SharedGenerator> gen,const SkIRect * subset,const SkColorType * colorType,sk_sp<SkColorSpace> colorSpace)57 SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset,
58                                    const SkColorType* colorType, sk_sp<SkColorSpace> colorSpace)
59         : fSharedGenerator(std::move(gen)) {
60     if (!fSharedGenerator) {
61         return;
62     }
63 
64     // The following generator accessors are safe without acquiring the mutex (const getters).
65     // TODO: refactor to use a ScopedGenerator instead, for clarity.
66     const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo();
67     if (info.isEmpty()) {
68         fSharedGenerator.reset();
69         return;
70     }
71 
72     fUniqueID = fSharedGenerator->fGenerator->uniqueID();
73     const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
74     if (subset) {
75         if (!bounds.contains(*subset)) {
76             fSharedGenerator.reset();
77             return;
78         }
79         if (*subset != bounds) {
80             // we need a different uniqueID since we really are a subset of the raw generator
81             fUniqueID = SkNextID::ImageID();
82         }
83     } else {
84         subset = &bounds;
85     }
86 
87     fInfo   = info.makeDimensions(subset->size());
88     fOrigin = SkIPoint::Make(subset->x(), subset->y());
89     if (colorType || colorSpace) {
90         if (colorType) {
91             fInfo = fInfo.makeColorType(*colorType);
92         }
93         if (colorSpace) {
94             fInfo = fInfo.makeColorSpace(colorSpace);
95         }
96         fUniqueID = SkNextID::ImageID();
97     }
98 }
99 
100 ///////////////////////////////////////////////////////////////////////////////
101 
102 // Helper for exclusive access to a shared generator.
103 class SkImage_Lazy::ScopedGenerator {
104 public:
ScopedGenerator(const sk_sp<SharedGenerator> & gen)105     ScopedGenerator(const sk_sp<SharedGenerator>& gen)
106       : fSharedGenerator(gen)
107       , fAutoAquire(gen->fMutex) {}
108 
operator ->() const109     SkImageGenerator* operator->() const {
110         fSharedGenerator->fMutex.assertHeld();
111         return fSharedGenerator->fGenerator.get();
112     }
113 
operator SkImageGenerator*() const114     operator SkImageGenerator*() const {
115         fSharedGenerator->fMutex.assertHeld();
116         return fSharedGenerator->fGenerator.get();
117     }
118 
119 private:
120     const sk_sp<SharedGenerator>& fSharedGenerator;
121     SkAutoMutexExclusive          fAutoAquire;
122 };
123 
124 ///////////////////////////////////////////////////////////////////////////////
125 
SkImage_Lazy(Validator * validator)126 SkImage_Lazy::SkImage_Lazy(Validator* validator)
127         : INHERITED(validator->fInfo, validator->fUniqueID)
128         , fSharedGenerator(std::move(validator->fSharedGenerator))
129         , fOrigin(validator->fOrigin) {
130     SkASSERT(fSharedGenerator);
131     fUniqueID = validator->fUniqueID;
132 }
133 
~SkImage_Lazy()134 SkImage_Lazy::~SkImage_Lazy() {
135 #if SK_SUPPORT_GPU
136     for (int i = 0; i < fUniqueKeyInvalidatedMessages.count(); ++i) {
137         SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(*fUniqueKeyInvalidatedMessages[i]);
138     }
139     fUniqueKeyInvalidatedMessages.deleteAll();
140 #endif
141 }
142 
143 //////////////////////////////////////////////////////////////////////////////////////////////////
144 
generate_pixels(SkImageGenerator * gen,const SkPixmap & pmap,int originX,int originY)145 static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY) {
146     const int genW = gen->getInfo().width();
147     const int genH = gen->getInfo().height();
148     const SkIRect srcR = SkIRect::MakeWH(genW, genH);
149     const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
150     if (!srcR.contains(dstR)) {
151         return false;
152     }
153 
154     // If they are requesting a subset, we have to have a temp allocation for full image, and
155     // then copy the subset into their allocation
156     SkBitmap full;
157     SkPixmap fullPM;
158     const SkPixmap* dstPM = &pmap;
159     if (srcR != dstR) {
160         if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
161             return false;
162         }
163         if (!full.peekPixels(&fullPM)) {
164             return false;
165         }
166         dstPM = &fullPM;
167     }
168 
169     if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes())) {
170         return false;
171     }
172 
173     if (srcR != dstR) {
174         if (!full.readPixels(pmap, originX, originY)) {
175             return false;
176         }
177     }
178     return true;
179 }
180 
getROPixels(SkBitmap * bitmap,SkImage::CachingHint chint) const181 bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkImage::CachingHint chint) const {
182     auto check_output_bitmap = [bitmap]() {
183         SkASSERT(bitmap->isImmutable());
184         SkASSERT(bitmap->getPixels());
185         (void)bitmap;
186     };
187 
188     auto desc = SkBitmapCacheDesc::Make(this);
189     if (SkBitmapCache::Find(desc, bitmap)) {
190         check_output_bitmap();
191         return true;
192     }
193 
194     if (SkImage::kAllow_CachingHint == chint) {
195         SkPixmap pmap;
196         SkBitmapCache::RecPtr cacheRec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
197         if (!cacheRec ||
198             !generate_pixels(ScopedGenerator(fSharedGenerator), pmap,
199                              fOrigin.x(), fOrigin.y())) {
200             return false;
201         }
202         SkBitmapCache::Add(std::move(cacheRec), bitmap);
203         this->notifyAddedToRasterCache();
204     } else {
205         if (!bitmap->tryAllocPixels(this->imageInfo()) ||
206             !generate_pixels(ScopedGenerator(fSharedGenerator), bitmap->pixmap(), fOrigin.x(),
207                              fOrigin.y())) {
208             return false;
209         }
210         bitmap->setImmutable();
211     }
212 
213     check_output_bitmap();
214     return true;
215 }
216 
217 //////////////////////////////////////////////////////////////////////////////////////////////////
218 
onReadPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,int srcX,int srcY,CachingHint chint) const219 bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
220                                 int srcX, int srcY, CachingHint chint) const {
221     SkBitmap bm;
222     if (this->getROPixels(&bm, chint)) {
223         return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
224     }
225     return false;
226 }
227 
onRefEncoded() const228 sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
229     ScopedGenerator generator(fSharedGenerator);
230     return generator->refEncodedData();
231 }
232 
onIsValid(GrContext * context) const233 bool SkImage_Lazy::onIsValid(GrContext* context) const {
234     ScopedGenerator generator(fSharedGenerator);
235     return generator->isValid(context);
236 }
237 
238 ///////////////////////////////////////////////////////////////////////////////////////////////////
239 
240 #if SK_SUPPORT_GPU
refView(GrRecordingContext * context,GrSamplerState params,SkScalar scaleAdjust[2]) const241 GrSurfaceProxyView SkImage_Lazy::refView(GrRecordingContext* context, GrSamplerState params,
242                                          SkScalar scaleAdjust[2]) const {
243     if (!context) {
244         return {};
245     }
246 
247     GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
248     return textureMaker.viewForParams(params, scaleAdjust);
249 }
250 #endif
251 
onMakeSubset(GrRecordingContext * context,const SkIRect & subset) const252 sk_sp<SkImage> SkImage_Lazy::onMakeSubset(GrRecordingContext* context,
253                                           const SkIRect& subset) const {
254     SkASSERT(this->bounds().contains(subset));
255     SkASSERT(this->bounds() != subset);
256 
257     const SkIRect generatorSubset = subset.makeOffset(fOrigin);
258     const SkColorType colorType = this->colorType();
259     Validator validator(fSharedGenerator, &generatorSubset, &colorType, this->refColorSpace());
260     return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
261 }
262 
onMakeColorTypeAndColorSpace(GrRecordingContext *,SkColorType targetCT,sk_sp<SkColorSpace> targetCS) const263 sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(GrRecordingContext*,
264                                                           SkColorType targetCT,
265                                                           sk_sp<SkColorSpace> targetCS) const {
266     SkAutoMutexExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
267     if (fOnMakeColorTypeAndSpaceResult &&
268         targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
269         SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
270         return fOnMakeColorTypeAndSpaceResult;
271     }
272     const SkIRect generatorSubset =
273             SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), this->width(), this->height());
274     Validator validator(fSharedGenerator, &generatorSubset, &targetCT, targetCS);
275     sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
276     if (result) {
277         fOnMakeColorTypeAndSpaceResult = result;
278     }
279     return result;
280 }
281 
onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const282 sk_sp<SkImage> SkImage_Lazy::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
283     // TODO: The correct thing is to clone the generator, and modify its color space. That's hard,
284     // because we don't have a clone method, and generator is public (and derived-from by clients).
285     // So do the simple/inefficient thing here, and fallback to raster when this is called.
286 
287     // We allocate the bitmap with the new color space, then generate the image using the original.
288     SkBitmap bitmap;
289     if (bitmap.tryAllocPixels(this->imageInfo().makeColorSpace(std::move(newCS)))) {
290         SkPixmap pixmap = bitmap.pixmap();
291         pixmap.setColorSpace(this->refColorSpace());
292         if (generate_pixels(ScopedGenerator(fSharedGenerator), pixmap, fOrigin.x(), fOrigin.y())) {
293             bitmap.setImmutable();
294             return SkImage::MakeFromBitmap(bitmap);
295         }
296     }
297     return nullptr;
298 }
299 
MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,const SkIRect * subset)300 sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
301                                           const SkIRect* subset) {
302     SkImage_Lazy::Validator
303             validator(SharedGenerator::Make(std::move(generator)), subset, nullptr, nullptr);
304 
305     return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
306 }
307 
DecodeToRaster(const void * encoded,size_t length,const SkIRect * subset)308 sk_sp<SkImage> SkImage::DecodeToRaster(const void* encoded, size_t length, const SkIRect* subset) {
309     // The generator will not outlive this function, so we can wrap the encoded data without copy
310     auto gen = SkImageGenerator::MakeFromEncoded(SkData::MakeWithoutCopy(encoded, length));
311     if (!gen) {
312         return nullptr;
313     }
314     SkImageInfo info = gen->getInfo();
315     if (info.isEmpty()) {
316         return nullptr;
317     }
318 
319     SkIPoint origin = {0, 0};
320     if (subset) {
321         if (!SkIRect::MakeWH(info.width(), info.height()).contains(*subset)) {
322             return nullptr;
323         }
324         info = info.makeDimensions(subset->size());
325         origin = {subset->x(), subset->y()};
326     }
327 
328     size_t rb = info.minRowBytes();
329     if (rb == 0) {
330         return nullptr; // rb was too big
331     }
332     size_t size = info.computeByteSize(rb);
333     if (size == SIZE_MAX) {
334         return nullptr;
335     }
336     auto data = SkData::MakeUninitialized(size);
337 
338     SkPixmap pmap(info, data->writable_data(), rb);
339     if (!generate_pixels(gen.get(), pmap, origin.x(), origin.y())) {
340         return nullptr;
341     }
342 
343     return SkImage::MakeRasterData(info, data, rb);
344 }
345 
346 //////////////////////////////////////////////////////////////////////////////////////////////////
347 
348 #if SK_SUPPORT_GPU
349 
makeCacheKeyFromOrigKey(const GrUniqueKey & origKey,GrUniqueKey * cacheKey) const350 void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey,
351                                            GrUniqueKey* cacheKey) const {
352     SkASSERT(!cacheKey->isValid());
353     if (origKey.isValid()) {
354         static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
355         GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image");
356     }
357 }
358 
359 class Generator_GrYUVProvider : public GrYUVProvider {
360 public:
Generator_GrYUVProvider(SkImageGenerator * gen)361     Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
362 
363 private:
onGetID() const364     uint32_t onGetID() const override { return fGen->uniqueID(); }
onQueryYUVA8(SkYUVASizeInfo * sizeInfo,SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],SkYUVColorSpace * colorSpace) const365     bool onQueryYUVA8(SkYUVASizeInfo* sizeInfo,
366                       SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
367                       SkYUVColorSpace* colorSpace) const override {
368         return fGen->queryYUVA8(sizeInfo, yuvaIndices, colorSpace);
369     }
onGetYUVA8Planes(const SkYUVASizeInfo & sizeInfo,const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],void * planes[])370     bool onGetYUVA8Planes(const SkYUVASizeInfo& sizeInfo,
371                           const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
372                           void* planes[]) override {
373         return fGen->getYUVA8Planes(sizeInfo, yuvaIndices, planes);
374     }
375 
376     SkImageGenerator* fGen;
377 
378     typedef GrYUVProvider INHERITED;
379 };
380 
set_key_on_proxy(GrProxyProvider * proxyProvider,GrTextureProxy * proxy,GrTextureProxy * originalProxy,const GrUniqueKey & key)381 static void set_key_on_proxy(GrProxyProvider* proxyProvider,
382                              GrTextureProxy* proxy, GrTextureProxy* originalProxy,
383                              const GrUniqueKey& key) {
384     if (key.isValid()) {
385         if (originalProxy && originalProxy->getUniqueKey().isValid()) {
386             SkASSERT(originalProxy->getUniqueKey() == key);
387             SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
388                      GrMipMapped::kNo == originalProxy->mipMapped());
389             // If we had an originalProxy with a valid key, that means there already is a proxy in
390             // the cache which matches the key, but it does not have mip levels and we require them.
391             // Thus we must remove the unique key from that proxy.
392             SkASSERT(originalProxy->getUniqueKey() == key);
393             proxyProvider->removeUniqueKeyFromProxy(originalProxy);
394         }
395         proxyProvider->assignUniqueKeyToProxy(key, proxy);
396     }
397 }
398 
getPlanes(SkYUVASizeInfo * yuvaSizeInfo,SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],SkYUVColorSpace * yuvColorSpace,const void * planes[SkYUVASizeInfo::kMaxCount])399 sk_sp<SkCachedData> SkImage_Lazy::getPlanes(SkYUVASizeInfo* yuvaSizeInfo,
400                                             SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
401                                             SkYUVColorSpace* yuvColorSpace,
402                                             const void* planes[SkYUVASizeInfo::kMaxCount]) {
403     ScopedGenerator generator(fSharedGenerator);
404     Generator_GrYUVProvider provider(generator);
405 
406     sk_sp<SkCachedData> data = provider.getPlanes(yuvaSizeInfo, yuvaIndices, yuvColorSpace, planes);
407     if (!data) {
408         return nullptr;
409     }
410 
411     return data;
412 }
413 
414 
415 /*
416  *  We have 4 ways to try to return a texture (in sorted order)
417  *
418  *  1. Check the cache for a pre-existing one
419  *  2. Ask the generator to natively create one
420  *  3. Ask the generator to return YUV planes, which the GPU can convert
421  *  4. Ask the generator to return RGB(A) data, which the GPU can convert
422  */
lockTextureProxyView(GrRecordingContext * ctx,const GrUniqueKey & origKey,SkImage::CachingHint chint,bool willBeMipped,GrTextureMaker::AllowedTexGenType genType) const423 GrSurfaceProxyView SkImage_Lazy::lockTextureProxyView(
424         GrRecordingContext* ctx,
425         const GrUniqueKey& origKey,
426         SkImage::CachingHint chint,
427         bool willBeMipped,
428         GrTextureMaker::AllowedTexGenType genType) const {
429     // Values representing the various texture lock paths we can take. Used for logging the path
430     // taken to a histogram.
431     enum LockTexturePath {
432         kFailure_LockTexturePath,
433         kPreExisting_LockTexturePath,
434         kNative_LockTexturePath,
435         kCompressed_LockTexturePath, // Deprecated
436         kYUV_LockTexturePath,
437         kRGBA_LockTexturePath,
438     };
439 
440     enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
441 
442     // Build our texture key.
443     // Even though some proxies created here may have a specific origin and use that origin, we do
444     // not include that in the key. Since SkImages are meant to be immutable, a given SkImage will
445     // always have an associated proxy that is always one origin or the other. It never can change
446     // origins. Thus we don't need to include that info in the key iteself.
447     GrUniqueKey key;
448     this->makeCacheKeyFromOrigKey(origKey, &key);
449 
450     const GrCaps* caps = ctx->priv().caps();
451     GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
452     GrSurfaceProxyView view;
453 
454     auto ct = this->colorTypeOfLockTextureProxy(caps);
455 
456     // 1. Check the cache for a pre-existing one
457     if (key.isValid()) {
458         auto proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, ct);
459         if (proxy) {
460             SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
461                                      kLockTexturePathCount);
462             GrSwizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct);
463             view = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
464             if (!willBeMipped || GrMipMapped::kYes == view.asTextureProxy()->mipMapped()) {
465                 return view;
466             }
467         }
468     }
469 
470     // 2. Ask the generator to natively create one
471     if (!view.proxy()) {
472         ScopedGenerator generator(fSharedGenerator);
473         if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
474                 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
475             return {};
476         }
477         view = generator->generateTexture(ctx, this->imageInfo(), fOrigin, willBeMipped);
478         if (view.proxy()) {
479             SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
480                                      kLockTexturePathCount);
481             GrTextureProxy* proxy = view.asTextureProxy();
482             SkASSERT(proxy);
483             set_key_on_proxy(proxyProvider, proxy, nullptr, key);
484             if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
485                 if (generator->texturesAreCacheable()) {
486                     *fUniqueKeyInvalidatedMessages.append() =
487                         new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
488                 }
489                 return view;
490             }
491         }
492     }
493 
494     // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
495     //    the texture we fall through here and have the CPU generate the mip maps for us.
496     if (!view.proxy() && !willBeMipped && !ctx->priv().options().fDisableGpuYUVConversion) {
497         SkColorType colorType = this->colorType();
498 
499         ScopedGenerator generator(fSharedGenerator);
500         Generator_GrYUVProvider provider(generator);
501 
502         // The pixels in the texture will be in the generator's color space.
503         // If onMakeColorTypeAndColorSpace has been called then this will not match this image's
504         // color space. To correct this, apply a color space conversion from the generator's color
505         // space to this image's color space.
506         SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
507         SkColorSpace* thisColorSpace = this->colorSpace();
508 
509         // TODO: Update to create the mipped surface in the YUV generator and draw the base
510         // layer directly into the mipped surface.
511         view = provider.refAsTextureProxyView(ctx, this->imageInfo().dimensions(),
512                                               SkColorTypeToGrColorType(colorType),
513                                               generatorColorSpace, thisColorSpace);
514         if (view.proxy()) {
515             SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
516                                      kLockTexturePathCount);
517             GrTextureProxy* proxy = view.asTextureProxy();
518             SkASSERT(proxy);
519             set_key_on_proxy(proxyProvider, proxy, nullptr, key);
520             *fUniqueKeyInvalidatedMessages.append() =
521                     new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
522             return view;
523         }
524     }
525 
526     // 4. Ask the generator to return RGB(A) data, which the GPU can convert
527     SkBitmap bitmap;
528     if (!view.proxy() && this->getROPixels(&bitmap, chint)) {
529         GrBitmapTextureMaker bitmapMaker(ctx, bitmap);
530         std::tie(view, std::ignore) =
531                 bitmapMaker.view(willBeMipped ? GrMipMapped::kYes : GrMipMapped::kNo);
532         GrTextureProxy* proxy = view.asTextureProxy();
533         if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
534             SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
535                                      kLockTexturePathCount);
536             SkASSERT(proxy);
537             set_key_on_proxy(proxyProvider, proxy, nullptr, key);
538             *fUniqueKeyInvalidatedMessages.append() =
539                     new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
540             return view;
541         }
542     }
543 
544     if (view.proxy()) {
545         // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
546         // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
547         // mipped surface and copy the original proxy into the base layer. We will then let the gpu
548         // generate the rest of the mips.
549         SkASSERT(willBeMipped);
550         SkASSERT(GrMipMapped::kNo == view.asTextureProxy()->mipMapped());
551         *fUniqueKeyInvalidatedMessages.append() =
552                 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
553         GrColorType srcColorType = SkColorTypeToGrColorType(this->colorType());
554         GrSurfaceProxyView mippedView = GrCopyBaseMipMapToTextureProxy(
555                 ctx, view.proxy(), kTopLeft_GrSurfaceOrigin, srcColorType);
556         auto mippedProxy = mippedView.asTextureProxy();
557         if (mippedProxy) {
558             set_key_on_proxy(proxyProvider, mippedProxy, view.asTextureProxy(), key);
559             return mippedView;
560         }
561         // We failed to make a mipped proxy with the base copied into it. This could have
562         // been from failure to make the proxy or failure to do the copy. Thus we will fall
563         // back to just using the non mipped proxy; See skbug.com/7094.
564         return view;
565     }
566 
567     SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
568                              kLockTexturePathCount);
569     return {};
570 }
571 
colorTypeOfLockTextureProxy(const GrCaps * caps) const572 GrColorType SkImage_Lazy::colorTypeOfLockTextureProxy(const GrCaps* caps) const {
573     GrColorType ct = SkColorTypeToGrColorType(this->colorType());
574     GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kNo);
575     if (!format.isValid()) {
576         ct = GrColorType::kRGBA_8888;
577     }
578     return ct;
579 }
580 
581 ///////////////////////////////////////////////////////////////////////////////////////////////////
582 
DecodeToTexture(GrContext * ctx,const void * encoded,size_t length,const SkIRect * subset)583 sk_sp<SkImage> SkImage::DecodeToTexture(GrContext* ctx, const void* encoded, size_t length,
584                                         const SkIRect* subset) {
585     // img will not survive this function, so we don't need to copy/own the encoded data,
586     auto img = MakeFromEncoded(SkData::MakeWithoutCopy(encoded, length), subset);
587     if (!img) {
588         return nullptr;
589     }
590     return img->makeTextureImage(ctx);
591 }
592 
593 #endif
594