blob: d4d1c028beb5981c6330dbf592f3976cf5628c91 [file] [log] [blame]
reed85d91782015-09-10 14:33:38 -07001/*
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"
reed85d91782015-09-10 14:33:38 -07009#include "SkImageCacherator.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040010
11#include "SkBitmap.h"
12#include "SkBitmapCache.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040013#include "SkData.h"
14#include "SkImageGenerator.h"
reed85d91782015-09-10 14:33:38 -070015#include "SkImagePriv.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040016#include "SkNextID.h"
reed85d91782015-09-10 14:33:38 -070017#include "SkPixelRef.h"
reed85d91782015-09-10 14:33:38 -070018
Brian Osmandf7e0752017-04-26 16:20:28 -040019#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 Phillips1afd4cd2018-01-08 13:40:32 -050025#include "GrProxyProvider.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040026#include "GrSamplerState.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040027#include "GrYUVProvider.h"
28#include "SkGr.h"
29#endif
reed85d91782015-09-10 14:33:38 -070030
Brian Osmandf7e0752017-04-26 16:20:28 -040031// Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images
32class SharedGenerator final : public SkNVRefCnt<SharedGenerator> {
33public:
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 Sarettb2004f72017-05-18 09:26:50 -040038 // This is thread safe. It is a const field set in the constructor.
39 const SkImageInfo& getInfo() { return fGenerator->getInfo(); }
40
Brian Osmandf7e0752017-04-26 16:20:28 -040041private:
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
54class SkImage_Lazy : public SkImage_Base, public SkImageCacherator {
55public:
56 struct Validator {
Christopher Cameron77e96662017-07-08 01:47:47 -070057 Validator(sk_sp<SharedGenerator>, const SkIRect* subset, sk_sp<SkColorSpace> colorSpace);
Brian Osmandf7e0752017-04-26 16:20:28 -040058
59 operator bool() const { return fSharedGenerator.get(); }
60
61 sk_sp<SharedGenerator> fSharedGenerator;
62 SkImageInfo fInfo;
63 SkIPoint fOrigin;
Christopher Cameron77e96662017-07-08 01:47:47 -070064 sk_sp<SkColorSpace> fColorSpace;
Brian Osmandf7e0752017-04-26 16:20:28 -040065 uint32_t fUniqueID;
66 };
67
68 SkImage_Lazy(Validator* validator);
69
70 SkImageInfo onImageInfo() const override {
71 return fInfo;
herba7c9d632016-04-19 12:30:22 -070072 }
Greg Daniel56008aa2018-03-14 15:33:42 -040073 SkColorType onColorType() const override {
74 return kUnknown_SkColorType;
75 }
brianosman69c166d2016-08-17 14:01:05 -070076 SkAlphaType onAlphaType() const override {
Brian Osmandf7e0752017-04-26 16:20:28 -040077 return fInfo.alphaType();
brianosman69c166d2016-08-17 14:01:05 -070078 }
herba7c9d632016-04-19 12:30:22 -070079
Mike Reedf2c73642018-05-29 15:41:27 -040080 SkIRect onGetSubset() const override {
81 return SkIRect::MakeXYWH(fOrigin.fX, fOrigin.fY, fInfo.width(), fInfo.height());
82 }
83
Robert Phillipsb726d582017-03-09 16:36:32 -050084 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY,
85 CachingHint) const override;
86#if SK_SUPPORT_GPU
Brian Salomon2bbdcc42017-09-07 12:36:34 -040087 sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*,
88 const GrSamplerState&, SkColorSpace*,
89 sk_sp<SkColorSpace>*,
Robert Phillipsb726d582017-03-09 16:36:32 -050090 SkScalar scaleAdjust[2]) const override;
91#endif
Ben Wagnerbdf54332018-05-15 14:12:14 -040092 sk_sp<SkData> onRefEncoded() const override;
reed7fb4f8b2016-03-11 04:33:52 -080093 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
Brian Osman61624f02016-12-09 14:51:59 -050094 bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override;
reed85d91782015-09-10 14:33:38 -070095 bool onIsLazyGenerated() const override { return true; }
Mike Reed7f1d0202017-05-08 16:13:39 -040096 bool onCanLazyGenerateOnGPU() const override;
Matt Sarett9f3dcb32017-05-04 08:53:32 -040097 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType,
98 SkTransferFunctionBehavior) const override;
reed85d91782015-09-10 14:33:38 -070099
Brian Osman5bbd0762017-05-08 11:07:42 -0400100 bool onIsValid(GrContext*) const override;
101
Brian Osmandf7e0752017-04-26 16:20:28 -0400102 SkImageCacherator* peekCacherator() const override {
103 return const_cast<SkImage_Lazy*>(this);
104 }
105
106 // Only return true if the generate has already been cached.
107 bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*, CachedFormat) const;
108 // Call the underlying generator directly
109 bool directGeneratePixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
110 int srcX, int srcY, SkTransferFunctionBehavior behavior) const;
111
112 // SkImageCacherator interface
113#if SK_SUPPORT_GPU
114 // Returns the texture proxy. If the cacherator is generating the texture and wants to cache it,
115 // it should use the passed in key (if the key is valid).
116 sk_sp<GrTextureProxy> lockTextureProxy(GrContext*,
117 const GrUniqueKey& key,
118 SkImage::CachingHint,
119 bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -0400120 SkColorSpace* dstColorSpace,
121 GrTextureMaker::AllowedTexGenType genType) override;
Brian Osmandf7e0752017-04-26 16:20:28 -0400122
123 // Returns the color space of the texture that would be returned if you called lockTexture.
124 // Separate code path to allow querying of the color space for textures that cached (even
125 // externally).
126 sk_sp<SkColorSpace> getColorSpace(GrContext*, SkColorSpace* dstColorSpace) override;
127 void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, CachedFormat,
128 GrUniqueKey* cacheKey) override;
129#endif
130
131 CachedFormat chooseCacheFormat(SkColorSpace* dstColorSpace,
132 const GrCaps* = nullptr) const override;
133 SkImageInfo buildCacheInfo(CachedFormat) const override;
134
reed85d91782015-09-10 14:33:38 -0700135private:
Brian Osmandf7e0752017-04-26 16:20:28 -0400136 class ScopedGenerator;
137
138 /**
139 * On success (true), bitmap will point to the pixels for this generator. If this returns
140 * false, the bitmap will be reset to empty.
141 */
Christopher Cameron77e96662017-07-08 01:47:47 -0700142 bool lockAsBitmap(SkBitmap*, SkImage::CachingHint, CachedFormat, const SkImageInfo&,
143 SkTransferFunctionBehavior) const;
144
145 /**
146 * Populates parameters to pass to the generator for reading pixels or generating a texture.
147 * For image generators, legacy versus true color blending is indicated using a
148 * SkTransferFunctionBehavior, and the target color space is specified on the SkImageInfo.
149 * If generatorImageInfo has no color space set, set its color space to this SkImage's color
150 * space, and return "ignore" behavior, indicating legacy mode. If generatorImageInfo has a
151 * color space set, return "respect" behavior, indicating linear blending mode.
152 */
153 SkTransferFunctionBehavior getGeneratorBehaviorAndInfo(SkImageInfo* generatorImageInfo) const;
Brian Osmandf7e0752017-04-26 16:20:28 -0400154
155 sk_sp<SharedGenerator> fSharedGenerator;
Christopher Cameron77e96662017-07-08 01:47:47 -0700156 // Note that fInfo is not necessarily the info from the generator. It may be cropped by
157 // onMakeSubset and its color space may be changed by onMakeColorSpace.
Brian Osmandf7e0752017-04-26 16:20:28 -0400158 const SkImageInfo fInfo;
159 const SkIPoint fOrigin;
160
161 struct IDRec {
162 SkOnce fOnce;
163 uint32_t fUniqueID;
164 };
165 mutable IDRec fIDRecs[kNumCachedFormats];
166
167 uint32_t getUniqueID(CachedFormat) const;
reed85d91782015-09-10 14:33:38 -0700168
Christopher Camerond4b67872017-07-13 15:18:08 -0700169 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and
170 // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call.
171 mutable SkMutex fOnMakeColorSpaceMutex;
172 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget;
173 mutable sk_sp<SkImage> fOnMakeColorSpaceResult;
174
reed85d91782015-09-10 14:33:38 -0700175 typedef SkImage_Base INHERITED;
176};
177
178///////////////////////////////////////////////////////////////////////////////
179
Christopher Cameron77e96662017-07-08 01:47:47 -0700180SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset,
181 sk_sp<SkColorSpace> colorSpace)
Brian Osmandf7e0752017-04-26 16:20:28 -0400182 : fSharedGenerator(std::move(gen)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400183 if (!fSharedGenerator) {
184 return;
185 }
186
187 // The following generator accessors are safe without acquiring the mutex (const getters).
188 // TODO: refactor to use a ScopedGenerator instead, for clarity.
189 const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo();
190 if (info.isEmpty()) {
191 fSharedGenerator.reset();
192 return;
193 }
194
195 fUniqueID = fSharedGenerator->fGenerator->uniqueID();
196 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
197 if (subset) {
198 if (!bounds.contains(*subset)) {
199 fSharedGenerator.reset();
200 return;
201 }
202 if (*subset != bounds) {
203 // we need a different uniqueID since we really are a subset of the raw generator
204 fUniqueID = SkNextID::ImageID();
205 }
206 } else {
207 subset = &bounds;
208 }
209
210 fInfo = info.makeWH(subset->width(), subset->height());
211 fOrigin = SkIPoint::Make(subset->x(), subset->y());
Christopher Cameron77e96662017-07-08 01:47:47 -0700212 if (colorSpace) {
213 fInfo = fInfo.makeColorSpace(colorSpace);
214 fUniqueID = SkNextID::ImageID();
215 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400216}
217
218///////////////////////////////////////////////////////////////////////////////
219
220// Helper for exclusive access to a shared generator.
221class SkImage_Lazy::ScopedGenerator {
222public:
223 ScopedGenerator(const sk_sp<SharedGenerator>& gen)
224 : fSharedGenerator(gen)
225 , fAutoAquire(gen->fMutex) {}
226
227 SkImageGenerator* operator->() const {
228 fSharedGenerator->fMutex.assertHeld();
229 return fSharedGenerator->fGenerator.get();
230 }
231
232 operator SkImageGenerator*() const {
233 fSharedGenerator->fMutex.assertHeld();
234 return fSharedGenerator->fGenerator.get();
235 }
236
237private:
238 const sk_sp<SharedGenerator>& fSharedGenerator;
239 SkAutoExclusive fAutoAquire;
240};
241
242///////////////////////////////////////////////////////////////////////////////
243
244SkImage_Lazy::SkImage_Lazy(Validator* validator)
245 : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID)
246 , fSharedGenerator(std::move(validator->fSharedGenerator))
247 , fInfo(validator->fInfo)
248 , fOrigin(validator->fOrigin) {
249 SkASSERT(fSharedGenerator);
Brian Osmandf7e0752017-04-26 16:20:28 -0400250 // We explicit set the legacy format slot, but leave the others uninitialized (via SkOnce)
251 // and only resolove them to IDs as needed (by calling getUniqueID()).
252 fIDRecs[kLegacy_CachedFormat].fOnce([this, validator] {
253 fIDRecs[kLegacy_CachedFormat].fUniqueID = validator->fUniqueID;
254 });
255}
256
257uint32_t SkImage_Lazy::getUniqueID(CachedFormat format) const {
258 IDRec* rec = &fIDRecs[format];
259 rec->fOnce([rec] {
260 rec->fUniqueID = SkNextID::ImageID();
261 });
262 return rec->fUniqueID;
263}
264
265//////////////////////////////////////////////////////////////////////////////////////////////////
266
267// Abstraction of GrCaps that handles the cases where we don't have a caps pointer (because
268// we're in raster mode), or where GPU support is entirely missing. In theory, we only need the
269// chosen format to be texturable, but that lets us choose F16 on GLES implemenations where we
270// won't be able to read the texture back. We'd like to ensure that SkImake::makeNonTextureImage
271// works, so we require that the formats we choose are renderable (as a proxy for being readable).
272struct CacheCaps {
273 CacheCaps(const GrCaps* caps) : fCaps(caps) {}
274
275#if SK_SUPPORT_GPU
276 bool supportsHalfFloat() const {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500277 return !fCaps || (fCaps->isConfigTexturable(kRGBA_half_GrPixelConfig) &&
278 fCaps->isConfigRenderable(kRGBA_half_GrPixelConfig));
Brian Osmandf7e0752017-04-26 16:20:28 -0400279 }
280
281 bool supportsSRGB() const {
282 return !fCaps ||
283 (fCaps->srgbSupport() && fCaps->isConfigTexturable(kSRGBA_8888_GrPixelConfig));
284 }
285
286 bool supportsSBGR() const {
287 return !fCaps || fCaps->srgbSupport();
288 }
289#else
290 bool supportsHalfFloat() const { return true; }
291 bool supportsSRGB() const { return true; }
292 bool supportsSBGR() const { return true; }
293#endif
294
295 const GrCaps* fCaps;
296};
297
298SkImageCacherator::CachedFormat SkImage_Lazy::chooseCacheFormat(SkColorSpace* dstColorSpace,
299 const GrCaps* grCaps) const {
Brian Osmane2693102018-06-26 19:19:49 +0000300 SkColorSpace* cs = fInfo.colorSpace();
301 if (!cs || !dstColorSpace) {
302 return kLegacy_CachedFormat;
303 }
304
305 CacheCaps caps(grCaps);
306 switch (fInfo.colorType()) {
307 case kUnknown_SkColorType:
308 case kAlpha_8_SkColorType:
309 case kRGB_565_SkColorType:
310 case kARGB_4444_SkColorType:
311 case kRGB_888x_SkColorType:
312 case kRGBA_1010102_SkColorType:
313 case kRGB_101010x_SkColorType:
314 // We don't support color space on these formats, so always decode in legacy mode:
315 // TODO: Ask the codec to decode these to something else (at least sRGB 8888)?
316 return kLegacy_CachedFormat;
317
318 case kGray_8_SkColorType:
319 // TODO: What do we do with grayscale sources that have strange color spaces attached?
320 // The codecs and color space xform don't handle this correctly (yet), so drop it on
321 // the floor. (Also, inflating by a factor of 8 is going to be unfortunate).
322 // As it is, we don't directly support sRGB grayscale, so ask the codec to convert
323 // it for us. This bypasses some really sketchy code GrUploadPixmapToTexture.
324 if (cs->gammaCloseToSRGB() && caps.supportsSRGB()) {
325 return kSRGB8888_CachedFormat;
326 } else {
327 return kLegacy_CachedFormat;
328 }
329
330 case kRGBA_8888_SkColorType:
331 if (cs->gammaCloseToSRGB()) {
332 if (caps.supportsSRGB()) {
333 return kSRGB8888_CachedFormat;
334 } else if (caps.supportsHalfFloat()) {
335 return kLinearF16_CachedFormat;
336 } else {
337 return kLegacy_CachedFormat;
338 }
339 } else {
340 if (caps.supportsHalfFloat()) {
341 return kLinearF16_CachedFormat;
342 } else if (caps.supportsSRGB()) {
343 return kSRGB8888_CachedFormat;
344 } else {
345 return kLegacy_CachedFormat;
346 }
347 }
348
349 case kBGRA_8888_SkColorType:
350 // Odd case. sBGRA isn't a real thing, so we may not have this texturable.
351 if (caps.supportsSBGR()) {
352 if (cs->gammaCloseToSRGB()) {
353 return kSBGR8888_CachedFormat;
354 } else if (caps.supportsHalfFloat()) {
355 return kLinearF16_CachedFormat;
356 } else if (caps.supportsSRGB()) {
357 return kSRGB8888_CachedFormat;
358 } else {
359 // sBGRA support without sRGBA is highly unlikely (impossible?) Nevertheless.
360 return kLegacy_CachedFormat;
361 }
362 } else {
363 if (cs->gammaCloseToSRGB()) {
364 if (caps.supportsSRGB()) {
365 return kSRGB8888_CachedFormat;
366 } else if (caps.supportsHalfFloat()) {
367 return kLinearF16_CachedFormat;
368 } else {
369 return kLegacy_CachedFormat;
370 }
371 } else {
372 if (caps.supportsHalfFloat()) {
373 return kLinearF16_CachedFormat;
374 } else if (caps.supportsSRGB()) {
375 return kSRGB8888_CachedFormat;
376 } else {
377 return kLegacy_CachedFormat;
378 }
379 }
380 }
381
382 case kRGBA_F16_SkColorType:
Brian Osmand47fe092018-06-26 15:27:10 -0400383 case kRGBA_F32_SkColorType:
Brian Osmane2693102018-06-26 19:19:49 +0000384 if (caps.supportsHalfFloat()) {
385 return kLinearF16_CachedFormat;
386 } else if (caps.supportsSRGB()) {
387 return kSRGB8888_CachedFormat;
388 } else {
389 return kLegacy_CachedFormat;
390 }
391 }
392 SkDEBUGFAIL("Unreachable");
Brian Osmandf7e0752017-04-26 16:20:28 -0400393 return kLegacy_CachedFormat;
394}
395
396SkImageInfo SkImage_Lazy::buildCacheInfo(CachedFormat format) const {
Brian Osmane2693102018-06-26 19:19:49 +0000397 switch (format) {
398 case kLegacy_CachedFormat:
399 return fInfo.makeColorSpace(nullptr);
400 case kLinearF16_CachedFormat:
401 return fInfo.makeColorType(kRGBA_F16_SkColorType)
402 .makeColorSpace(fInfo.colorSpace()->makeLinearGamma());
403 case kSRGB8888_CachedFormat:
404 // If the transfer function is nearly (but not exactly) sRGB, we don't want the codec
405 // to bother trans-coding. It would be slow, and do more harm than good visually,
406 // so we make sure to leave the colorspace as-is.
407 if (fInfo.colorSpace()->gammaCloseToSRGB()) {
408 return fInfo.makeColorType(kRGBA_8888_SkColorType);
409 } else {
410 return fInfo.makeColorType(kRGBA_8888_SkColorType)
411 .makeColorSpace(fInfo.colorSpace()->makeSRGBGamma());
412 }
413 case kSBGR8888_CachedFormat:
414 // See note above about not-quite-sRGB transfer functions.
415 if (fInfo.colorSpace()->gammaCloseToSRGB()) {
416 return fInfo.makeColorType(kBGRA_8888_SkColorType);
417 } else {
418 return fInfo.makeColorType(kBGRA_8888_SkColorType)
419 .makeColorSpace(fInfo.colorSpace()->makeSRGBGamma());
420 }
421 default:
422 SkDEBUGFAIL("Invalid cached format");
423 return fInfo;
Brian Osmandf7e0752017-04-26 16:20:28 -0400424 }
425}
426
427//////////////////////////////////////////////////////////////////////////////////////////////////
428
429static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) {
430 SkASSERT(bitmap.getGenerationID() == expectedID);
431 SkASSERT(bitmap.isImmutable());
432 SkASSERT(bitmap.getPixels());
433 return true;
434}
435
436bool SkImage_Lazy::directGeneratePixels(const SkImageInfo& info, void* pixels, size_t rb,
437 int srcX, int srcY,
438 SkTransferFunctionBehavior behavior) const {
439 ScopedGenerator generator(fSharedGenerator);
440 const SkImageInfo& genInfo = generator->getInfo();
441 // Currently generators do not natively handle subsets, so check that first.
442 if (srcX || srcY || genInfo.width() != info.width() || genInfo.height() != info.height()) {
443 return false;
444 }
445
446 SkImageGenerator::Options opts;
Christopher Cameron77e96662017-07-08 01:47:47 -0700447 // TODO: This should respect the behavior argument.
448 opts.fBehavior = SkTransferFunctionBehavior::kIgnore;
Brian Osmandf7e0752017-04-26 16:20:28 -0400449 return generator->getPixels(info, pixels, rb, &opts);
450}
451
452//////////////////////////////////////////////////////////////////////////////////////////////////
453
454bool SkImage_Lazy::lockAsBitmapOnlyIfAlreadyCached(SkBitmap* bitmap, CachedFormat format) const {
455 uint32_t uniqueID = this->getUniqueID(format);
456 return SkBitmapCache::Find(SkBitmapCacheDesc::Make(uniqueID,
457 fInfo.width(), fInfo.height()), bitmap) &&
458 check_output_bitmap(*bitmap, uniqueID);
459}
460
Christopher Cameron77e96662017-07-08 01:47:47 -0700461static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY,
462 SkTransferFunctionBehavior behavior) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400463 const int genW = gen->getInfo().width();
464 const int genH = gen->getInfo().height();
465 const SkIRect srcR = SkIRect::MakeWH(genW, genH);
466 const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
467 if (!srcR.contains(dstR)) {
468 return false;
469 }
470
471 // If they are requesting a subset, we have to have a temp allocation for full image, and
472 // then copy the subset into their allocation
473 SkBitmap full;
474 SkPixmap fullPM;
475 const SkPixmap* dstPM = &pmap;
476 if (srcR != dstR) {
477 if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
478 return false;
479 }
480 if (!full.peekPixels(&fullPM)) {
481 return false;
482 }
483 dstPM = &fullPM;
484 }
485
Christopher Cameron77e96662017-07-08 01:47:47 -0700486 SkImageGenerator::Options opts;
487 opts.fBehavior = behavior;
488 if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes(), &opts)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400489 return false;
490 }
491
492 if (srcR != dstR) {
493 if (!full.readPixels(pmap, originX, originY)) {
494 return false;
495 }
496 }
497 return true;
498}
499
Christopher Cameron77e96662017-07-08 01:47:47 -0700500bool SkImage_Lazy::lockAsBitmap(SkBitmap* bitmap, SkImage::CachingHint chint, CachedFormat format,
501 const SkImageInfo& info,
502 SkTransferFunctionBehavior behavior) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400503 if (this->lockAsBitmapOnlyIfAlreadyCached(bitmap, format)) {
504 return true;
505 }
506
507 uint32_t uniqueID = this->getUniqueID(format);
508
509 SkBitmap tmpBitmap;
510 SkBitmapCache::RecPtr cacheRec;
511 SkPixmap pmap;
512 if (SkImage::kAllow_CachingHint == chint) {
513 auto desc = SkBitmapCacheDesc::Make(uniqueID, info.width(), info.height());
514 cacheRec = SkBitmapCache::Alloc(desc, info, &pmap);
515 if (!cacheRec) {
516 return false;
517 }
518 } else {
519 if (!tmpBitmap.tryAllocPixels(info)) {
520 return false;
521 }
522 if (!tmpBitmap.peekPixels(&pmap)) {
523 return false;
524 }
525 }
526
527 ScopedGenerator generator(fSharedGenerator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700528 if (!generate_pixels(generator, pmap, fOrigin.x(), fOrigin.y(), behavior)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400529 return false;
530 }
531
532 if (cacheRec) {
533 SkBitmapCache::Add(std::move(cacheRec), bitmap);
534 SkASSERT(bitmap->getPixels()); // we're locked
535 SkASSERT(bitmap->isImmutable());
536 SkASSERT(bitmap->getGenerationID() == uniqueID);
537 this->notifyAddedToCache();
538 } else {
539 *bitmap = tmpBitmap;
540 bitmap->pixelRef()->setImmutableWithID(uniqueID);
541 }
542
543 check_output_bitmap(*bitmap, uniqueID);
544 return true;
545}
546
547//////////////////////////////////////////////////////////////////////////////////////////////////
548
Brian Osmanf1b43822017-04-20 13:43:23 -0400549bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
550 int srcX, int srcY, CachingHint chint) const {
Brian Osman61624f02016-12-09 14:51:59 -0500551 SkColorSpace* dstColorSpace = dstInfo.colorSpace();
reed85d91782015-09-10 14:33:38 -0700552 SkBitmap bm;
reed6868c3f2015-11-24 11:44:47 -0800553 if (kDisallow_CachingHint == chint) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400554 CachedFormat cacheFormat = this->chooseCacheFormat(dstColorSpace);
Christopher Cameron77e96662017-07-08 01:47:47 -0700555 SkImageInfo genPixelsInfo = dstInfo;
556 SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo);
Brian Osmandf7e0752017-04-26 16:20:28 -0400557 if (this->lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) {
reed6868c3f2015-11-24 11:44:47 -0800558 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
559 } else {
560 // Try passing the caller's buffer directly down to the generator. If this fails we
561 // may still succeed in the general case, as the generator may prefer some other
562 // config, which we could then convert via SkBitmap::readPixels.
Christopher Cameron77e96662017-07-08 01:47:47 -0700563 if (this->directGeneratePixels(genPixelsInfo, dstPixels, dstRB, srcX, srcY, behavior)) {
reed6868c3f2015-11-24 11:44:47 -0800564 return true;
565 }
566 // else fall through
567 }
568 }
569
Brian Osman61624f02016-12-09 14:51:59 -0500570 if (this->getROPixels(&bm, dstColorSpace, chint)) {
reed85d91782015-09-10 14:33:38 -0700571 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
572 }
573 return false;
574}
575
Ben Wagnerbdf54332018-05-15 14:12:14 -0400576sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
577 ScopedGenerator generator(fSharedGenerator);
578 return generator->refEncodedData();
579}
reed85d91782015-09-10 14:33:38 -0700580
Brian Osmanf1b43822017-04-20 13:43:23 -0400581bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace,
582 CachingHint chint) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400583 CachedFormat cacheFormat = this->chooseCacheFormat(dstColorSpace);
Christopher Cameron77e96662017-07-08 01:47:47 -0700584 const SkImageInfo cacheInfo = this->buildCacheInfo(cacheFormat);
585 SkImageInfo genPixelsInfo = cacheInfo;
586 SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo);
587 return this->lockAsBitmap(bitmap, chint, cacheFormat, genPixelsInfo, behavior);
reed85d91782015-09-10 14:33:38 -0700588}
589
Brian Osman5bbd0762017-05-08 11:07:42 -0400590bool SkImage_Lazy::onIsValid(GrContext* context) const {
591 ScopedGenerator generator(fSharedGenerator);
592 return generator->isValid(context);
593}
594
Mike Reed7f1d0202017-05-08 16:13:39 -0400595bool SkImage_Lazy::onCanLazyGenerateOnGPU() const {
596#if SK_SUPPORT_GPU
597 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400598 return SkImageGenerator::TexGenType::kNone != generator->onCanGenerateTexture();
Mike Reed7f1d0202017-05-08 16:13:39 -0400599#else
600 return false;
601#endif
602}
603
Christopher Cameron77e96662017-07-08 01:47:47 -0700604SkTransferFunctionBehavior SkImage_Lazy::getGeneratorBehaviorAndInfo(SkImageInfo* generatorImageInfo) const {
Brian Osmane2693102018-06-26 19:19:49 +0000605 if (generatorImageInfo->colorSpace()) {
606 return SkTransferFunctionBehavior::kRespect;
607 }
608 // Only specify an output color space if color conversion can be done on the color type.
609 switch (generatorImageInfo->colorType()) {
610 case kRGBA_8888_SkColorType:
611 case kBGRA_8888_SkColorType:
612 case kRGBA_F16_SkColorType:
613 case kRGB_565_SkColorType:
614 *generatorImageInfo = generatorImageInfo->makeColorSpace(fInfo.refColorSpace());
615 break;
616 default:
617 break;
618 }
Christopher Cameron77e96662017-07-08 01:47:47 -0700619 return SkTransferFunctionBehavior::kIgnore;
620}
621
Brian Osmandf7e0752017-04-26 16:20:28 -0400622///////////////////////////////////////////////////////////////////////////////////////////////////
623
Robert Phillipsb726d582017-03-09 16:36:32 -0500624#if SK_SUPPORT_GPU
Brian Osmanf1b43822017-04-20 13:43:23 -0400625sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrContext* context,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400626 const GrSamplerState& params,
Brian Osmanf1b43822017-04-20 13:43:23 -0400627 SkColorSpace* dstColorSpace,
628 sk_sp<SkColorSpace>* texColorSpace,
629 SkScalar scaleAdjust[2]) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400630 if (!context) {
631 return nullptr;
632 }
633
634 GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
635 return textureMaker.refTextureProxyForParams(params, dstColorSpace, texColorSpace, scaleAdjust);
Robert Phillipsb726d582017-03-09 16:36:32 -0500636}
637#endif
638
Brian Osmanf1b43822017-04-20 13:43:23 -0400639sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400640 SkASSERT(fInfo.bounds().contains(subset));
641 SkASSERT(fInfo.bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700642
Brian Osmandf7e0752017-04-26 16:20:28 -0400643 const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y());
Christopher Cameron77e96662017-07-08 01:47:47 -0700644 Validator validator(fSharedGenerator, &generatorSubset, fInfo.refColorSpace());
Brian Osmanf1b43822017-04-20 13:43:23 -0400645 return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
reed7b6945b2015-09-24 00:50:58 -0700646}
647
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400648sk_sp<SkImage> SkImage_Lazy::onMakeColorSpace(sk_sp<SkColorSpace> target,
649 SkColorType targetColorType,
650 SkTransferFunctionBehavior premulBehavior) const {
Christopher Camerond4b67872017-07-13 15:18:08 -0700651 SkAutoExclusive autoAquire(fOnMakeColorSpaceMutex);
652 if (target && fOnMakeColorSpaceTarget &&
653 SkColorSpace::Equals(target.get(), fOnMakeColorSpaceTarget.get())) {
654 return fOnMakeColorSpaceResult;
655 }
Christopher Cameron77e96662017-07-08 01:47:47 -0700656 const SkIRect generatorSubset =
657 SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
658 Validator validator(fSharedGenerator, &generatorSubset, target);
Christopher Camerond4b67872017-07-13 15:18:08 -0700659 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
660 if (result) {
661 fOnMakeColorSpaceTarget = target;
662 fOnMakeColorSpaceResult = result;
663 }
664 return result;
Matt Sarett6de13102017-03-14 14:10:48 -0400665}
666
Mike Reed185130c2017-02-15 15:14:16 -0500667sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
668 const SkIRect* subset) {
Christopher Cameron77e96662017-07-08 01:47:47 -0700669 SkImage_Lazy::Validator validator(SharedGenerator::Make(std::move(generator)), subset, nullptr);
fmalita7929e3a2016-10-27 08:15:44 -0700670
Brian Osmanf1b43822017-04-20 13:43:23 -0400671 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700672}
Brian Osmandf7e0752017-04-26 16:20:28 -0400673
674//////////////////////////////////////////////////////////////////////////////////////////////////
675
676/**
677 * Implementation of SkImageCacherator interface, as needed by GrImageTextureMaker
678 */
679
680#if SK_SUPPORT_GPU
681
682void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, CachedFormat format,
683 GrUniqueKey* cacheKey) {
684 SkASSERT(!cacheKey->isValid());
685 if (origKey.isValid()) {
686 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Derek Sollenbergere1c60d62018-04-04 11:53:35 -0400687 GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 1, "Image");
Brian Osmandf7e0752017-04-26 16:20:28 -0400688 builder[0] = format;
689 }
690}
691
692class Generator_GrYUVProvider : public GrYUVProvider {
693 SkImageGenerator* fGen;
694
695public:
696 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
697
698 uint32_t onGetID() override { return fGen->uniqueID(); }
699 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
700 return fGen->queryYUV8(sizeInfo, colorSpace);
701 }
702 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
703 return fGen->getYUV8Planes(sizeInfo, planes);
704 }
705};
706
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500707static void set_key_on_proxy(GrProxyProvider* proxyProvider,
Greg Danielfc5060d2017-10-04 18:36:15 +0000708 GrTextureProxy* proxy, GrTextureProxy* originalProxy,
709 const GrUniqueKey& key) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400710 if (key.isValid()) {
Robert Phillips8a90f502017-07-24 15:09:56 -0400711 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Greg Danielf6f7b672018-02-15 13:06:26 -0500712 if (originalProxy && originalProxy->getUniqueKey().isValid()) {
713 SkASSERT(originalProxy->getUniqueKey() == key);
Greg Daniele252f082017-10-23 16:05:23 -0400714 SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
715 GrMipMapped::kNo == originalProxy->mipMapped());
Greg Danielf6f7b672018-02-15 13:06:26 -0500716 // If we had an originalProxy with a valid key, that means there already is a proxy in
717 // the cache which matches the key, but it does not have mip levels and we require them.
718 // Thus we must remove the unique key from that proxy.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500719 proxyProvider->removeUniqueKeyFromProxy(key, originalProxy);
Greg Danielfc5060d2017-10-04 18:36:15 +0000720 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500721 proxyProvider->assignUniqueKeyToProxy(key, proxy);
Brian Osmandf7e0752017-04-26 16:20:28 -0400722 }
723}
724
725sk_sp<SkColorSpace> SkImage_Lazy::getColorSpace(GrContext* ctx, SkColorSpace* dstColorSpace) {
Brian Osmana8ac9242017-09-07 10:19:08 -0400726 if (!dstColorSpace) {
727 // In legacy mode, we do no modification to the image's color space or encoding.
728 // Subsequent legacy drawing is likely to ignore the color space, but some clients
729 // may want to know what space the image data is in, so return it.
730 return fInfo.refColorSpace();
731 } else {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400732 CachedFormat format = this->chooseCacheFormat(dstColorSpace, ctx->contextPriv().caps());
Brian Osmana8ac9242017-09-07 10:19:08 -0400733 SkImageInfo cacheInfo = this->buildCacheInfo(format);
734 return cacheInfo.refColorSpace();
735 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400736}
737
738/*
739 * We have 4 ways to try to return a texture (in sorted order)
740 *
741 * 1. Check the cache for a pre-existing one
742 * 2. Ask the generator to natively create one
743 * 3. Ask the generator to return YUV planes, which the GPU can convert
744 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
745 */
746sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(GrContext* ctx,
747 const GrUniqueKey& origKey,
748 SkImage::CachingHint chint,
749 bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -0400750 SkColorSpace* dstColorSpace,
751 GrTextureMaker::AllowedTexGenType genType) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400752 // Values representing the various texture lock paths we can take. Used for logging the path
753 // taken to a histogram.
754 enum LockTexturePath {
755 kFailure_LockTexturePath,
756 kPreExisting_LockTexturePath,
757 kNative_LockTexturePath,
758 kCompressed_LockTexturePath, // Deprecated
759 kYUV_LockTexturePath,
760 kRGBA_LockTexturePath,
761 };
762
763 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
764
765 // Determine which cached format we're going to use (which may involve decoding to a different
766 // info than the generator provides).
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400767 CachedFormat format = this->chooseCacheFormat(dstColorSpace, ctx->contextPriv().caps());
Brian Osmandf7e0752017-04-26 16:20:28 -0400768
769 // Fold the cache format into our texture key
770 GrUniqueKey key;
771 this->makeCacheKeyFromOrigKey(origKey, format, &key);
772
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500773 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +0000774 sk_sp<GrTextureProxy> proxy;
775
Brian Osmandf7e0752017-04-26 16:20:28 -0400776 // 1. Check the cache for a pre-existing one
777 if (key.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500778 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
Greg Danielfc5060d2017-10-04 18:36:15 +0000779 if (proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400780 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
781 kLockTexturePathCount);
Greg Daniele252f082017-10-23 16:05:23 -0400782 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000783 return proxy;
784 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400785 }
786 }
787
788 // The CachedFormat is both an index for which cache "slot" we'll use to store this particular
789 // decoded variant of the encoded data, and also a recipe for how to transform the original
790 // info to get the one that we're going to decode to.
Christopher Cameron77e96662017-07-08 01:47:47 -0700791 const SkImageInfo cacheInfo = this->buildCacheInfo(format);
792 SkImageInfo genPixelsInfo = cacheInfo;
793 SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo);
Brian Osmandf7e0752017-04-26 16:20:28 -0400794
795 // 2. Ask the generator to natively create one
Greg Danielfc5060d2017-10-04 18:36:15 +0000796 if (!proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400797 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400798 if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
799 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
800 return nullptr;
801 }
Greg Danielf88c12e2017-10-09 09:57:35 -0400802 if ((proxy = generator->generateTexture(ctx, genPixelsInfo, fOrigin, behavior,
803 willBeMipped))) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400804 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
805 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500806 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniele252f082017-10-23 16:05:23 -0400807 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000808 return proxy;
809 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400810 }
811 }
812
Greg Daniel3e70fa32017-10-05 16:27:06 -0400813 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
814 // the texture we fall through here and have the CPU generate the mip maps for us.
815 if (!proxy && !willBeMipped && !ctx->contextPriv().disableGpuYUVConversion()) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400816 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(cacheInfo);
Brian Osmandf7e0752017-04-26 16:20:28 -0400817 ScopedGenerator generator(fSharedGenerator);
818 Generator_GrYUVProvider provider(generator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700819
820 // The pixels in the texture will be in the generator's color space. If onMakeColorSpace
821 // has been called then this will not match this image's color space. To correct this, apply
822 // a color space conversion from the generator's color space to this image's color space.
Brian Osman56893cd2018-06-08 14:11:37 -0400823 // Note that we can only do this conversion (on the GPU) if both color spaces are XYZ type.
Brian Osman861ea5b2018-06-14 09:14:03 -0400824 SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
825 SkColorSpace* thisColorSpace = fInfo.colorSpace();
Christopher Cameron77e96662017-07-08 01:47:47 -0700826
Brian Osman56893cd2018-06-08 14:11:37 -0400827 if ((!generatorColorSpace || generatorColorSpace->toXYZD50()) &&
828 (!thisColorSpace || thisColorSpace->toXYZD50())) {
829 // TODO: Update to create the mipped surface in the YUV generator and draw the base
830 // layer directly into the mipped surface.
831 proxy = provider.refAsTextureProxy(ctx, desc, generatorColorSpace, thisColorSpace);
832 if (proxy) {
833 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
834 kLockTexturePathCount);
835 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
836 return proxy;
837 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400838 }
839 }
840
841 // 4. Ask the generator to return RGB(A) data, which the GPU can convert
842 SkBitmap bitmap;
Greg Danielfc5060d2017-10-04 18:36:15 +0000843 if (!proxy && this->lockAsBitmap(&bitmap, chint, format, genPixelsInfo, behavior)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400844 if (willBeMipped) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400845 proxy = proxyProvider->createMipMapProxyFromBitmap(bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400846 }
847 if (!proxy) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400848 proxy = GrUploadBitmapToTextureProxy(proxyProvider, bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400849 }
Greg Daniele252f082017-10-23 16:05:23 -0400850 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400851 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
852 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500853 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400854 return proxy;
855 }
856 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000857
858 if (proxy) {
859 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
860 // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
861 // mipped surface and copy the original proxy into the base layer. We will then let the gpu
862 // generate the rest of the mips.
863 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -0400864 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Daniele1da1d92017-10-06 15:59:27 -0400865 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500866 set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key);
Greg Danielfc5060d2017-10-04 18:36:15 +0000867 return mippedProxy;
868 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400869 // We failed to make a mipped proxy with the base copied into it. This could have
870 // been from failure to make the proxy or failure to do the copy. Thus we will fall
871 // back to just using the non mipped proxy; See skbug.com/7094.
872 return proxy;
Greg Danielfc5060d2017-10-04 18:36:15 +0000873 }
874
Brian Osmandf7e0752017-04-26 16:20:28 -0400875 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
876 kLockTexturePathCount);
877 return nullptr;
878}
879
880///////////////////////////////////////////////////////////////////////////////////////////////////
881
882#endif