blob: 8e055f395e446276e2d1a2f828192b540ce3f23a [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"
13#include "SkColorSpace_Base.h"
14#include "SkData.h"
15#include "SkImageGenerator.h"
reed85d91782015-09-10 14:33:38 -070016#include "SkImagePriv.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040017#include "SkNextID.h"
reed85d91782015-09-10 14:33:38 -070018#include "SkPixelRef.h"
reed85d91782015-09-10 14:33:38 -070019
Brian Osmandf7e0752017-04-26 16:20:28 -040020#if SK_SUPPORT_GPU
21#include "GrContext.h"
22#include "GrContextPriv.h"
23#include "GrGpuResourcePriv.h"
24#include "GrImageTextureMaker.h"
25#include "GrResourceKey.h"
26#include "GrResourceProvider.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040027#include "GrSamplerState.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040028#include "GrYUVProvider.h"
29#include "SkGr.h"
30#endif
reed85d91782015-09-10 14:33:38 -070031
Brian Osmandf7e0752017-04-26 16:20:28 -040032// Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images
33class SharedGenerator final : public SkNVRefCnt<SharedGenerator> {
34public:
35 static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen) {
36 return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr;
37 }
38
Matt Sarettb2004f72017-05-18 09:26:50 -040039 // This is thread safe. It is a const field set in the constructor.
40 const SkImageInfo& getInfo() { return fGenerator->getInfo(); }
41
Brian Osmandf7e0752017-04-26 16:20:28 -040042private:
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
55class SkImage_Lazy : public SkImage_Base, public SkImageCacherator {
56public:
57 struct Validator {
Christopher Cameron77e96662017-07-08 01:47:47 -070058 Validator(sk_sp<SharedGenerator>, const SkIRect* subset, sk_sp<SkColorSpace> colorSpace);
Brian Osmandf7e0752017-04-26 16:20:28 -040059
60 operator bool() const { return fSharedGenerator.get(); }
61
62 sk_sp<SharedGenerator> fSharedGenerator;
63 SkImageInfo fInfo;
64 SkIPoint fOrigin;
Christopher Cameron77e96662017-07-08 01:47:47 -070065 sk_sp<SkColorSpace> fColorSpace;
Brian Osmandf7e0752017-04-26 16:20:28 -040066 uint32_t fUniqueID;
67 };
68
69 SkImage_Lazy(Validator* validator);
70
71 SkImageInfo onImageInfo() const override {
72 return fInfo;
herba7c9d632016-04-19 12:30:22 -070073 }
brianosman69c166d2016-08-17 14:01:05 -070074 SkAlphaType onAlphaType() const override {
Brian Osmandf7e0752017-04-26 16:20:28 -040075 return fInfo.alphaType();
brianosman69c166d2016-08-17 14:01:05 -070076 }
herba7c9d632016-04-19 12:30:22 -070077
Robert Phillipsb726d582017-03-09 16:36:32 -050078 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY,
79 CachingHint) const override;
80#if SK_SUPPORT_GPU
Brian Salomon2bbdcc42017-09-07 12:36:34 -040081 sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*,
82 const GrSamplerState&, SkColorSpace*,
83 sk_sp<SkColorSpace>*,
Robert Phillipsb726d582017-03-09 16:36:32 -050084 SkScalar scaleAdjust[2]) const override;
85#endif
Brian Osman47858972017-04-25 10:02:12 -040086 SkData* onRefEncoded() const override;
reed7fb4f8b2016-03-11 04:33:52 -080087 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
Brian Osman61624f02016-12-09 14:51:59 -050088 bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override;
reed85d91782015-09-10 14:33:38 -070089 bool onIsLazyGenerated() const override { return true; }
Mike Reed7f1d0202017-05-08 16:13:39 -040090 bool onCanLazyGenerateOnGPU() const override;
Matt Sarett9f3dcb32017-05-04 08:53:32 -040091 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType,
92 SkTransferFunctionBehavior) const override;
reed85d91782015-09-10 14:33:38 -070093
Brian Osman5bbd0762017-05-08 11:07:42 -040094 bool onIsValid(GrContext*) const override;
95
Brian Osmandf7e0752017-04-26 16:20:28 -040096 SkImageCacherator* peekCacherator() const override {
97 return const_cast<SkImage_Lazy*>(this);
98 }
99
100 // Only return true if the generate has already been cached.
101 bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*, CachedFormat) const;
102 // Call the underlying generator directly
103 bool directGeneratePixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
104 int srcX, int srcY, SkTransferFunctionBehavior behavior) const;
105
106 // SkImageCacherator interface
107#if SK_SUPPORT_GPU
108 // Returns the texture proxy. If the cacherator is generating the texture and wants to cache it,
109 // it should use the passed in key (if the key is valid).
110 sk_sp<GrTextureProxy> lockTextureProxy(GrContext*,
111 const GrUniqueKey& key,
112 SkImage::CachingHint,
113 bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -0400114 SkColorSpace* dstColorSpace,
115 GrTextureMaker::AllowedTexGenType genType) override;
Brian Osmandf7e0752017-04-26 16:20:28 -0400116
117 // Returns the color space of the texture that would be returned if you called lockTexture.
118 // Separate code path to allow querying of the color space for textures that cached (even
119 // externally).
120 sk_sp<SkColorSpace> getColorSpace(GrContext*, SkColorSpace* dstColorSpace) override;
121 void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, CachedFormat,
122 GrUniqueKey* cacheKey) override;
123#endif
124
125 CachedFormat chooseCacheFormat(SkColorSpace* dstColorSpace,
126 const GrCaps* = nullptr) const override;
127 SkImageInfo buildCacheInfo(CachedFormat) const override;
128
reed85d91782015-09-10 14:33:38 -0700129private:
Brian Osmandf7e0752017-04-26 16:20:28 -0400130 class ScopedGenerator;
131
132 /**
133 * On success (true), bitmap will point to the pixels for this generator. If this returns
134 * false, the bitmap will be reset to empty.
135 */
Christopher Cameron77e96662017-07-08 01:47:47 -0700136 bool lockAsBitmap(SkBitmap*, SkImage::CachingHint, CachedFormat, const SkImageInfo&,
137 SkTransferFunctionBehavior) const;
138
139 /**
140 * Populates parameters to pass to the generator for reading pixels or generating a texture.
141 * For image generators, legacy versus true color blending is indicated using a
142 * SkTransferFunctionBehavior, and the target color space is specified on the SkImageInfo.
143 * If generatorImageInfo has no color space set, set its color space to this SkImage's color
144 * space, and return "ignore" behavior, indicating legacy mode. If generatorImageInfo has a
145 * color space set, return "respect" behavior, indicating linear blending mode.
146 */
147 SkTransferFunctionBehavior getGeneratorBehaviorAndInfo(SkImageInfo* generatorImageInfo) const;
Brian Osmandf7e0752017-04-26 16:20:28 -0400148
149 sk_sp<SharedGenerator> fSharedGenerator;
Christopher Cameron77e96662017-07-08 01:47:47 -0700150 // Note that fInfo is not necessarily the info from the generator. It may be cropped by
151 // onMakeSubset and its color space may be changed by onMakeColorSpace.
Brian Osmandf7e0752017-04-26 16:20:28 -0400152 const SkImageInfo fInfo;
153 const SkIPoint fOrigin;
154
155 struct IDRec {
156 SkOnce fOnce;
157 uint32_t fUniqueID;
158 };
159 mutable IDRec fIDRecs[kNumCachedFormats];
160
161 uint32_t getUniqueID(CachedFormat) const;
reed85d91782015-09-10 14:33:38 -0700162
Christopher Camerond4b67872017-07-13 15:18:08 -0700163 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and
164 // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call.
165 mutable SkMutex fOnMakeColorSpaceMutex;
166 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget;
167 mutable sk_sp<SkImage> fOnMakeColorSpaceResult;
168
reed85d91782015-09-10 14:33:38 -0700169 typedef SkImage_Base INHERITED;
170};
171
172///////////////////////////////////////////////////////////////////////////////
173
Christopher Cameron77e96662017-07-08 01:47:47 -0700174SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset,
175 sk_sp<SkColorSpace> colorSpace)
Brian Osmandf7e0752017-04-26 16:20:28 -0400176 : fSharedGenerator(std::move(gen)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400177 if (!fSharedGenerator) {
178 return;
179 }
180
181 // The following generator accessors are safe without acquiring the mutex (const getters).
182 // TODO: refactor to use a ScopedGenerator instead, for clarity.
183 const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo();
184 if (info.isEmpty()) {
185 fSharedGenerator.reset();
186 return;
187 }
188
189 fUniqueID = fSharedGenerator->fGenerator->uniqueID();
190 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
191 if (subset) {
192 if (!bounds.contains(*subset)) {
193 fSharedGenerator.reset();
194 return;
195 }
196 if (*subset != bounds) {
197 // we need a different uniqueID since we really are a subset of the raw generator
198 fUniqueID = SkNextID::ImageID();
199 }
200 } else {
201 subset = &bounds;
202 }
203
204 fInfo = info.makeWH(subset->width(), subset->height());
205 fOrigin = SkIPoint::Make(subset->x(), subset->y());
Christopher Cameron77e96662017-07-08 01:47:47 -0700206 if (colorSpace) {
207 fInfo = fInfo.makeColorSpace(colorSpace);
208 fUniqueID = SkNextID::ImageID();
209 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400210}
211
212///////////////////////////////////////////////////////////////////////////////
213
214// Helper for exclusive access to a shared generator.
215class SkImage_Lazy::ScopedGenerator {
216public:
217 ScopedGenerator(const sk_sp<SharedGenerator>& gen)
218 : fSharedGenerator(gen)
219 , fAutoAquire(gen->fMutex) {}
220
221 SkImageGenerator* operator->() const {
222 fSharedGenerator->fMutex.assertHeld();
223 return fSharedGenerator->fGenerator.get();
224 }
225
226 operator SkImageGenerator*() const {
227 fSharedGenerator->fMutex.assertHeld();
228 return fSharedGenerator->fGenerator.get();
229 }
230
231private:
232 const sk_sp<SharedGenerator>& fSharedGenerator;
233 SkAutoExclusive fAutoAquire;
234};
235
236///////////////////////////////////////////////////////////////////////////////
237
238SkImage_Lazy::SkImage_Lazy(Validator* validator)
239 : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID)
240 , fSharedGenerator(std::move(validator->fSharedGenerator))
241 , fInfo(validator->fInfo)
242 , fOrigin(validator->fOrigin) {
243 SkASSERT(fSharedGenerator);
Brian Osmandf7e0752017-04-26 16:20:28 -0400244 // We explicit set the legacy format slot, but leave the others uninitialized (via SkOnce)
245 // and only resolove them to IDs as needed (by calling getUniqueID()).
246 fIDRecs[kLegacy_CachedFormat].fOnce([this, validator] {
247 fIDRecs[kLegacy_CachedFormat].fUniqueID = validator->fUniqueID;
248 });
249}
250
251uint32_t SkImage_Lazy::getUniqueID(CachedFormat format) const {
252 IDRec* rec = &fIDRecs[format];
253 rec->fOnce([rec] {
254 rec->fUniqueID = SkNextID::ImageID();
255 });
256 return rec->fUniqueID;
257}
258
259//////////////////////////////////////////////////////////////////////////////////////////////////
260
261// Abstraction of GrCaps that handles the cases where we don't have a caps pointer (because
262// we're in raster mode), or where GPU support is entirely missing. In theory, we only need the
263// chosen format to be texturable, but that lets us choose F16 on GLES implemenations where we
264// won't be able to read the texture back. We'd like to ensure that SkImake::makeNonTextureImage
265// works, so we require that the formats we choose are renderable (as a proxy for being readable).
266struct CacheCaps {
267 CacheCaps(const GrCaps* caps) : fCaps(caps) {}
268
269#if SK_SUPPORT_GPU
270 bool supportsHalfFloat() const {
271 return !fCaps ||
272 (fCaps->isConfigTexturable(kRGBA_half_GrPixelConfig) &&
273 fCaps->isConfigRenderable(kRGBA_half_GrPixelConfig, false));
274 }
275
276 bool supportsSRGB() const {
277 return !fCaps ||
278 (fCaps->srgbSupport() && fCaps->isConfigTexturable(kSRGBA_8888_GrPixelConfig));
279 }
280
281 bool supportsSBGR() const {
282 return !fCaps || fCaps->srgbSupport();
283 }
284#else
285 bool supportsHalfFloat() const { return true; }
286 bool supportsSRGB() const { return true; }
287 bool supportsSBGR() const { return true; }
288#endif
289
290 const GrCaps* fCaps;
291};
292
293SkImageCacherator::CachedFormat SkImage_Lazy::chooseCacheFormat(SkColorSpace* dstColorSpace,
294 const GrCaps* grCaps) const {
295 SkColorSpace* cs = fInfo.colorSpace();
296 if (!cs || !dstColorSpace) {
297 return kLegacy_CachedFormat;
298 }
299
300 CacheCaps caps(grCaps);
301 switch (fInfo.colorType()) {
302 case kUnknown_SkColorType:
303 case kAlpha_8_SkColorType:
304 case kRGB_565_SkColorType:
305 case kARGB_4444_SkColorType:
306 // We don't support color space on these formats, so always decode in legacy mode:
307 // TODO: Ask the codec to decode these to something else (at least sRGB 8888)?
308 return kLegacy_CachedFormat;
309
Brian Osmandf7e0752017-04-26 16:20:28 -0400310 case kGray_8_SkColorType:
311 // TODO: What do we do with grayscale sources that have strange color spaces attached?
312 // The codecs and color space xform don't handle this correctly (yet), so drop it on
313 // the floor. (Also, inflating by a factor of 8 is going to be unfortunate).
314 // As it is, we don't directly support sRGB grayscale, so ask the codec to convert
315 // it for us. This bypasses some really sketchy code GrUploadPixmapToTexture.
316 if (cs->gammaCloseToSRGB() && caps.supportsSRGB()) {
317 return kSRGB8888_CachedFormat;
318 } else {
319 return kLegacy_CachedFormat;
320 }
321
322 case kRGBA_8888_SkColorType:
323 if (cs->gammaCloseToSRGB()) {
324 if (caps.supportsSRGB()) {
325 return kSRGB8888_CachedFormat;
326 } else if (caps.supportsHalfFloat()) {
327 return kLinearF16_CachedFormat;
328 } else {
329 return kLegacy_CachedFormat;
330 }
331 } else {
332 if (caps.supportsHalfFloat()) {
333 return kLinearF16_CachedFormat;
334 } else if (caps.supportsSRGB()) {
335 return kSRGB8888_CachedFormat;
336 } else {
337 return kLegacy_CachedFormat;
338 }
339 }
340
341 case kBGRA_8888_SkColorType:
342 // Odd case. sBGRA isn't a real thing, so we may not have this texturable.
343 if (caps.supportsSBGR()) {
344 if (cs->gammaCloseToSRGB()) {
345 return kSBGR8888_CachedFormat;
346 } else if (caps.supportsHalfFloat()) {
347 return kLinearF16_CachedFormat;
348 } else if (caps.supportsSRGB()) {
349 return kSRGB8888_CachedFormat;
350 } else {
351 // sBGRA support without sRGBA is highly unlikely (impossible?) Nevertheless.
352 return kLegacy_CachedFormat;
353 }
354 } else {
355 if (cs->gammaCloseToSRGB()) {
356 if (caps.supportsSRGB()) {
357 return kSRGB8888_CachedFormat;
358 } else if (caps.supportsHalfFloat()) {
359 return kLinearF16_CachedFormat;
360 } else {
361 return kLegacy_CachedFormat;
362 }
363 } else {
364 if (caps.supportsHalfFloat()) {
365 return kLinearF16_CachedFormat;
366 } else if (caps.supportsSRGB()) {
367 return kSRGB8888_CachedFormat;
368 } else {
369 return kLegacy_CachedFormat;
370 }
371 }
372 }
373
374 case kRGBA_F16_SkColorType:
375 if (caps.supportsHalfFloat()) {
376 return kLinearF16_CachedFormat;
377 } else if (caps.supportsSRGB()) {
378 return kSRGB8888_CachedFormat;
379 } else {
380 return kLegacy_CachedFormat;
381 }
382 }
383 SkDEBUGFAIL("Unreachable");
384 return kLegacy_CachedFormat;
385}
386
387SkImageInfo SkImage_Lazy::buildCacheInfo(CachedFormat format) const {
388 switch (format) {
389 case kLegacy_CachedFormat:
390 return fInfo.makeColorSpace(nullptr);
391 case kLinearF16_CachedFormat:
392 return fInfo.makeColorType(kRGBA_F16_SkColorType)
393 .makeColorSpace(as_CSB(fInfo.colorSpace())->makeLinearGamma());
394 case kSRGB8888_CachedFormat:
395 // If the transfer function is nearly (but not exactly) sRGB, we don't want the codec
396 // to bother trans-coding. It would be slow, and do more harm than good visually,
397 // so we make sure to leave the colorspace as-is.
398 if (fInfo.colorSpace()->gammaCloseToSRGB()) {
399 return fInfo.makeColorType(kRGBA_8888_SkColorType);
400 } else {
401 return fInfo.makeColorType(kRGBA_8888_SkColorType)
402 .makeColorSpace(as_CSB(fInfo.colorSpace())->makeSRGBGamma());
403 }
404 case kSBGR8888_CachedFormat:
405 // See note above about not-quite-sRGB transfer functions.
406 if (fInfo.colorSpace()->gammaCloseToSRGB()) {
407 return fInfo.makeColorType(kBGRA_8888_SkColorType);
408 } else {
409 return fInfo.makeColorType(kBGRA_8888_SkColorType)
410 .makeColorSpace(as_CSB(fInfo.colorSpace())->makeSRGBGamma());
411 }
412 default:
413 SkDEBUGFAIL("Invalid cached format");
414 return fInfo;
415 }
416}
417
418//////////////////////////////////////////////////////////////////////////////////////////////////
419
420static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) {
421 SkASSERT(bitmap.getGenerationID() == expectedID);
422 SkASSERT(bitmap.isImmutable());
423 SkASSERT(bitmap.getPixels());
424 return true;
425}
426
427bool SkImage_Lazy::directGeneratePixels(const SkImageInfo& info, void* pixels, size_t rb,
428 int srcX, int srcY,
429 SkTransferFunctionBehavior behavior) const {
430 ScopedGenerator generator(fSharedGenerator);
431 const SkImageInfo& genInfo = generator->getInfo();
432 // Currently generators do not natively handle subsets, so check that first.
433 if (srcX || srcY || genInfo.width() != info.width() || genInfo.height() != info.height()) {
434 return false;
435 }
436
437 SkImageGenerator::Options opts;
Christopher Cameron77e96662017-07-08 01:47:47 -0700438 // TODO: This should respect the behavior argument.
439 opts.fBehavior = SkTransferFunctionBehavior::kIgnore;
Brian Osmandf7e0752017-04-26 16:20:28 -0400440 return generator->getPixels(info, pixels, rb, &opts);
441}
442
443//////////////////////////////////////////////////////////////////////////////////////////////////
444
445bool SkImage_Lazy::lockAsBitmapOnlyIfAlreadyCached(SkBitmap* bitmap, CachedFormat format) const {
446 uint32_t uniqueID = this->getUniqueID(format);
447 return SkBitmapCache::Find(SkBitmapCacheDesc::Make(uniqueID,
448 fInfo.width(), fInfo.height()), bitmap) &&
449 check_output_bitmap(*bitmap, uniqueID);
450}
451
Christopher Cameron77e96662017-07-08 01:47:47 -0700452static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY,
453 SkTransferFunctionBehavior behavior) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400454 const int genW = gen->getInfo().width();
455 const int genH = gen->getInfo().height();
456 const SkIRect srcR = SkIRect::MakeWH(genW, genH);
457 const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
458 if (!srcR.contains(dstR)) {
459 return false;
460 }
461
462 // If they are requesting a subset, we have to have a temp allocation for full image, and
463 // then copy the subset into their allocation
464 SkBitmap full;
465 SkPixmap fullPM;
466 const SkPixmap* dstPM = &pmap;
467 if (srcR != dstR) {
468 if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
469 return false;
470 }
471 if (!full.peekPixels(&fullPM)) {
472 return false;
473 }
474 dstPM = &fullPM;
475 }
476
Christopher Cameron77e96662017-07-08 01:47:47 -0700477 SkImageGenerator::Options opts;
478 opts.fBehavior = behavior;
479 if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes(), &opts)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400480 return false;
481 }
482
483 if (srcR != dstR) {
484 if (!full.readPixels(pmap, originX, originY)) {
485 return false;
486 }
487 }
488 return true;
489}
490
Christopher Cameron77e96662017-07-08 01:47:47 -0700491bool SkImage_Lazy::lockAsBitmap(SkBitmap* bitmap, SkImage::CachingHint chint, CachedFormat format,
492 const SkImageInfo& info,
493 SkTransferFunctionBehavior behavior) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400494 if (this->lockAsBitmapOnlyIfAlreadyCached(bitmap, format)) {
495 return true;
496 }
497
498 uint32_t uniqueID = this->getUniqueID(format);
499
500 SkBitmap tmpBitmap;
501 SkBitmapCache::RecPtr cacheRec;
502 SkPixmap pmap;
503 if (SkImage::kAllow_CachingHint == chint) {
504 auto desc = SkBitmapCacheDesc::Make(uniqueID, info.width(), info.height());
505 cacheRec = SkBitmapCache::Alloc(desc, info, &pmap);
506 if (!cacheRec) {
507 return false;
508 }
509 } else {
510 if (!tmpBitmap.tryAllocPixels(info)) {
511 return false;
512 }
513 if (!tmpBitmap.peekPixels(&pmap)) {
514 return false;
515 }
516 }
517
518 ScopedGenerator generator(fSharedGenerator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700519 if (!generate_pixels(generator, pmap, fOrigin.x(), fOrigin.y(), behavior)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400520 return false;
521 }
522
523 if (cacheRec) {
524 SkBitmapCache::Add(std::move(cacheRec), bitmap);
525 SkASSERT(bitmap->getPixels()); // we're locked
526 SkASSERT(bitmap->isImmutable());
527 SkASSERT(bitmap->getGenerationID() == uniqueID);
528 this->notifyAddedToCache();
529 } else {
530 *bitmap = tmpBitmap;
531 bitmap->pixelRef()->setImmutableWithID(uniqueID);
532 }
533
534 check_output_bitmap(*bitmap, uniqueID);
535 return true;
536}
537
538//////////////////////////////////////////////////////////////////////////////////////////////////
539
Brian Osmanf1b43822017-04-20 13:43:23 -0400540bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
541 int srcX, int srcY, CachingHint chint) const {
Brian Osman61624f02016-12-09 14:51:59 -0500542 SkColorSpace* dstColorSpace = dstInfo.colorSpace();
reed85d91782015-09-10 14:33:38 -0700543 SkBitmap bm;
reed6868c3f2015-11-24 11:44:47 -0800544 if (kDisallow_CachingHint == chint) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400545 CachedFormat cacheFormat = this->chooseCacheFormat(dstColorSpace);
Christopher Cameron77e96662017-07-08 01:47:47 -0700546 SkImageInfo genPixelsInfo = dstInfo;
547 SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo);
Brian Osmandf7e0752017-04-26 16:20:28 -0400548 if (this->lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) {
reed6868c3f2015-11-24 11:44:47 -0800549 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
550 } else {
551 // Try passing the caller's buffer directly down to the generator. If this fails we
552 // may still succeed in the general case, as the generator may prefer some other
553 // config, which we could then convert via SkBitmap::readPixels.
Christopher Cameron77e96662017-07-08 01:47:47 -0700554 if (this->directGeneratePixels(genPixelsInfo, dstPixels, dstRB, srcX, srcY, behavior)) {
reed6868c3f2015-11-24 11:44:47 -0800555 return true;
556 }
557 // else fall through
558 }
559 }
560
Brian Osman61624f02016-12-09 14:51:59 -0500561 if (this->getROPixels(&bm, dstColorSpace, chint)) {
reed85d91782015-09-10 14:33:38 -0700562 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
563 }
564 return false;
565}
566
Brian Osman47858972017-04-25 10:02:12 -0400567SkData* SkImage_Lazy::onRefEncoded() const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400568 ScopedGenerator generator(fSharedGenerator);
569 return generator->refEncodedData();
reed85d91782015-09-10 14:33:38 -0700570}
571
Brian Osmanf1b43822017-04-20 13:43:23 -0400572bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace,
573 CachingHint chint) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400574 CachedFormat cacheFormat = this->chooseCacheFormat(dstColorSpace);
Christopher Cameron77e96662017-07-08 01:47:47 -0700575 const SkImageInfo cacheInfo = this->buildCacheInfo(cacheFormat);
576 SkImageInfo genPixelsInfo = cacheInfo;
577 SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo);
578 return this->lockAsBitmap(bitmap, chint, cacheFormat, genPixelsInfo, behavior);
reed85d91782015-09-10 14:33:38 -0700579}
580
Brian Osman5bbd0762017-05-08 11:07:42 -0400581bool SkImage_Lazy::onIsValid(GrContext* context) const {
582 ScopedGenerator generator(fSharedGenerator);
583 return generator->isValid(context);
584}
585
Mike Reed7f1d0202017-05-08 16:13:39 -0400586bool SkImage_Lazy::onCanLazyGenerateOnGPU() const {
587#if SK_SUPPORT_GPU
588 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400589 return SkImageGenerator::TexGenType::kNone != generator->onCanGenerateTexture();
Mike Reed7f1d0202017-05-08 16:13:39 -0400590#else
591 return false;
592#endif
593}
594
Christopher Cameron77e96662017-07-08 01:47:47 -0700595SkTransferFunctionBehavior SkImage_Lazy::getGeneratorBehaviorAndInfo(SkImageInfo* generatorImageInfo) const {
596 if (generatorImageInfo->colorSpace()) {
597 return SkTransferFunctionBehavior::kRespect;
598 }
599 // Only specify an output color space if color conversion can be done on the color type.
600 switch (generatorImageInfo->colorType()) {
601 case kRGBA_8888_SkColorType:
602 case kBGRA_8888_SkColorType:
603 case kRGBA_F16_SkColorType:
604 case kRGB_565_SkColorType:
605 *generatorImageInfo = generatorImageInfo->makeColorSpace(fInfo.refColorSpace());
606 break;
607 default:
608 break;
609 }
610 return SkTransferFunctionBehavior::kIgnore;
611}
612
Brian Osmandf7e0752017-04-26 16:20:28 -0400613///////////////////////////////////////////////////////////////////////////////////////////////////
614
Robert Phillipsb726d582017-03-09 16:36:32 -0500615#if SK_SUPPORT_GPU
Brian Osmanf1b43822017-04-20 13:43:23 -0400616sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrContext* context,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400617 const GrSamplerState& params,
Brian Osmanf1b43822017-04-20 13:43:23 -0400618 SkColorSpace* dstColorSpace,
619 sk_sp<SkColorSpace>* texColorSpace,
620 SkScalar scaleAdjust[2]) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400621 if (!context) {
622 return nullptr;
623 }
624
625 GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
626 return textureMaker.refTextureProxyForParams(params, dstColorSpace, texColorSpace, scaleAdjust);
Robert Phillipsb726d582017-03-09 16:36:32 -0500627}
628#endif
629
Brian Osmanf1b43822017-04-20 13:43:23 -0400630sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400631 SkASSERT(fInfo.bounds().contains(subset));
632 SkASSERT(fInfo.bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700633
Brian Osmandf7e0752017-04-26 16:20:28 -0400634 const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y());
Christopher Cameron77e96662017-07-08 01:47:47 -0700635 Validator validator(fSharedGenerator, &generatorSubset, fInfo.refColorSpace());
Brian Osmanf1b43822017-04-20 13:43:23 -0400636 return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
reed7b6945b2015-09-24 00:50:58 -0700637}
638
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400639sk_sp<SkImage> SkImage_Lazy::onMakeColorSpace(sk_sp<SkColorSpace> target,
640 SkColorType targetColorType,
641 SkTransferFunctionBehavior premulBehavior) const {
Christopher Camerond4b67872017-07-13 15:18:08 -0700642 SkAutoExclusive autoAquire(fOnMakeColorSpaceMutex);
643 if (target && fOnMakeColorSpaceTarget &&
644 SkColorSpace::Equals(target.get(), fOnMakeColorSpaceTarget.get())) {
645 return fOnMakeColorSpaceResult;
646 }
Christopher Cameron77e96662017-07-08 01:47:47 -0700647 const SkIRect generatorSubset =
648 SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
649 Validator validator(fSharedGenerator, &generatorSubset, target);
Christopher Camerond4b67872017-07-13 15:18:08 -0700650 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
651 if (result) {
652 fOnMakeColorSpaceTarget = target;
653 fOnMakeColorSpaceResult = result;
654 }
655 return result;
Matt Sarett6de13102017-03-14 14:10:48 -0400656}
657
Mike Reed185130c2017-02-15 15:14:16 -0500658sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
659 const SkIRect* subset) {
Christopher Cameron77e96662017-07-08 01:47:47 -0700660 SkImage_Lazy::Validator validator(SharedGenerator::Make(std::move(generator)), subset, nullptr);
fmalita7929e3a2016-10-27 08:15:44 -0700661
Brian Osmanf1b43822017-04-20 13:43:23 -0400662 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700663}
Brian Osmandf7e0752017-04-26 16:20:28 -0400664
665//////////////////////////////////////////////////////////////////////////////////////////////////
666
667/**
668 * Implementation of SkImageCacherator interface, as needed by GrImageTextureMaker
669 */
670
671#if SK_SUPPORT_GPU
672
673void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, CachedFormat format,
674 GrUniqueKey* cacheKey) {
675 SkASSERT(!cacheKey->isValid());
676 if (origKey.isValid()) {
677 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
678 GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 1);
679 builder[0] = format;
680 }
681}
682
683class Generator_GrYUVProvider : public GrYUVProvider {
684 SkImageGenerator* fGen;
685
686public:
687 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
688
689 uint32_t onGetID() override { return fGen->uniqueID(); }
690 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
691 return fGen->queryYUV8(sizeInfo, colorSpace);
692 }
693 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
694 return fGen->getYUV8Planes(sizeInfo, planes);
695 }
696};
697
698static void set_key_on_proxy(GrResourceProvider* resourceProvider,
Greg Danielfc5060d2017-10-04 18:36:15 +0000699 GrTextureProxy* proxy, GrTextureProxy* originalProxy,
700 const GrUniqueKey& key) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400701 if (key.isValid()) {
Robert Phillips8a90f502017-07-24 15:09:56 -0400702 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Greg Danielfc5060d2017-10-04 18:36:15 +0000703 if (originalProxy) {
704 SkASSERT(proxy->isMipMapped() && !originalProxy->isMipMapped());
705 // If we had an originalProxy, that means there already is a proxy in the cache which
706 // matches the key, but it does not have mip levels and we require them. Thus we must
707 // remove the unique key from that proxy.
708 resourceProvider->removeUniqueKeyFromProxy(key, originalProxy);
709 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400710 resourceProvider->assignUniqueKeyToProxy(key, proxy);
711 }
712}
713
714sk_sp<SkColorSpace> SkImage_Lazy::getColorSpace(GrContext* ctx, SkColorSpace* dstColorSpace) {
Brian Osmana8ac9242017-09-07 10:19:08 -0400715 if (!dstColorSpace) {
716 // In legacy mode, we do no modification to the image's color space or encoding.
717 // Subsequent legacy drawing is likely to ignore the color space, but some clients
718 // may want to know what space the image data is in, so return it.
719 return fInfo.refColorSpace();
720 } else {
721 CachedFormat format = this->chooseCacheFormat(dstColorSpace, ctx->caps());
722 SkImageInfo cacheInfo = this->buildCacheInfo(format);
723 return cacheInfo.refColorSpace();
724 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400725}
726
727/*
728 * We have 4 ways to try to return a texture (in sorted order)
729 *
730 * 1. Check the cache for a pre-existing one
731 * 2. Ask the generator to natively create one
732 * 3. Ask the generator to return YUV planes, which the GPU can convert
733 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
734 */
735sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(GrContext* ctx,
736 const GrUniqueKey& origKey,
737 SkImage::CachingHint chint,
738 bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -0400739 SkColorSpace* dstColorSpace,
740 GrTextureMaker::AllowedTexGenType genType) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400741 // Values representing the various texture lock paths we can take. Used for logging the path
742 // taken to a histogram.
743 enum LockTexturePath {
744 kFailure_LockTexturePath,
745 kPreExisting_LockTexturePath,
746 kNative_LockTexturePath,
747 kCompressed_LockTexturePath, // Deprecated
748 kYUV_LockTexturePath,
749 kRGBA_LockTexturePath,
750 };
751
752 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
753
754 // Determine which cached format we're going to use (which may involve decoding to a different
755 // info than the generator provides).
756 CachedFormat format = this->chooseCacheFormat(dstColorSpace, ctx->caps());
757
758 // Fold the cache format into our texture key
759 GrUniqueKey key;
760 this->makeCacheKeyFromOrigKey(origKey, format, &key);
761
Greg Danielfc5060d2017-10-04 18:36:15 +0000762 sk_sp<GrTextureProxy> proxy;
763
Brian Osmandf7e0752017-04-26 16:20:28 -0400764 // 1. Check the cache for a pre-existing one
765 if (key.isValid()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000766 proxy = ctx->resourceProvider()->findOrCreateProxyByUniqueKey(key,
767 kTopLeft_GrSurfaceOrigin);
768 if (proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400769 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
770 kLockTexturePathCount);
Greg Danielfc5060d2017-10-04 18:36:15 +0000771 if (!willBeMipped || proxy->isMipMapped()) {
772 return proxy;
773 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400774 }
775 }
776
777 // The CachedFormat is both an index for which cache "slot" we'll use to store this particular
778 // decoded variant of the encoded data, and also a recipe for how to transform the original
779 // info to get the one that we're going to decode to.
Christopher Cameron77e96662017-07-08 01:47:47 -0700780 const SkImageInfo cacheInfo = this->buildCacheInfo(format);
781 SkImageInfo genPixelsInfo = cacheInfo;
782 SkTransferFunctionBehavior behavior = getGeneratorBehaviorAndInfo(&genPixelsInfo);
Brian Osmandf7e0752017-04-26 16:20:28 -0400783
784 // 2. Ask the generator to natively create one
Greg Danielfc5060d2017-10-04 18:36:15 +0000785 if (!proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400786 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400787 if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
788 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
789 return nullptr;
790 }
Greg Danielf88c12e2017-10-09 09:57:35 -0400791 if ((proxy = generator->generateTexture(ctx, genPixelsInfo, fOrigin, behavior,
792 willBeMipped))) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400793 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
794 kLockTexturePathCount);
Greg Danielfc5060d2017-10-04 18:36:15 +0000795 set_key_on_proxy(ctx->resourceProvider(), proxy.get(), nullptr, key);
796 if (!willBeMipped || proxy->isMipMapped()) {
797 return proxy;
798 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400799 }
800 }
801
Greg Daniel3e70fa32017-10-05 16:27:06 -0400802 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
803 // the texture we fall through here and have the CPU generate the mip maps for us.
804 if (!proxy && !willBeMipped && !ctx->contextPriv().disableGpuYUVConversion()) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400805 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(cacheInfo, *ctx->caps());
806 ScopedGenerator generator(fSharedGenerator);
807 Generator_GrYUVProvider provider(generator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700808
809 // The pixels in the texture will be in the generator's color space. If onMakeColorSpace
810 // has been called then this will not match this image's color space. To correct this, apply
811 // a color space conversion from the generator's color space to this image's color space.
812 const SkColorSpace* generatorColorSpace =
813 fSharedGenerator->fGenerator->getInfo().colorSpace();
814 const SkColorSpace* thisColorSpace = fInfo.colorSpace();
815
Greg Danielfc5060d2017-10-04 18:36:15 +0000816 // TODO: Update to create the mipped surface in the YUV generator and draw the base layer
817 // directly into the mipped surface.
818 proxy = provider.refAsTextureProxy(ctx, desc, true, generatorColorSpace, thisColorSpace);
Christopher Cameron77e96662017-07-08 01:47:47 -0700819 if (proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400820 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
821 kLockTexturePathCount);
Greg Danielfc5060d2017-10-04 18:36:15 +0000822 set_key_on_proxy(ctx->resourceProvider(), proxy.get(), nullptr, key);
Greg Daniel3e70fa32017-10-05 16:27:06 -0400823 return proxy;
Brian Osmandf7e0752017-04-26 16:20:28 -0400824 }
825 }
826
827 // 4. Ask the generator to return RGB(A) data, which the GPU can convert
828 SkBitmap bitmap;
Greg Danielfc5060d2017-10-04 18:36:15 +0000829 if (!proxy && this->lockAsBitmap(&bitmap, chint, format, genPixelsInfo, behavior)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400830 if (willBeMipped) {
831 proxy = GrGenerateMipMapsAndUploadToTextureProxy(ctx, bitmap, dstColorSpace);
832 }
833 if (!proxy) {
Matt Sarettdedac852017-05-12 10:56:49 -0400834 proxy = GrUploadBitmapToTextureProxy(ctx->resourceProvider(), bitmap, dstColorSpace);
Brian Osmandf7e0752017-04-26 16:20:28 -0400835 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000836 if (proxy && (!willBeMipped || proxy->isMipMapped())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400837 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
838 kLockTexturePathCount);
Greg Danielfc5060d2017-10-04 18:36:15 +0000839 set_key_on_proxy(ctx->resourceProvider(), proxy.get(), nullptr, key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400840 return proxy;
841 }
842 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000843
844 if (proxy) {
845 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
846 // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
847 // mipped surface and copy the original proxy into the base layer. We will then let the gpu
848 // generate the rest of the mips.
849 SkASSERT(willBeMipped);
850 SkASSERT(!proxy->isMipMapped());
Greg Daniele1da1d92017-10-06 15:59:27 -0400851 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000852 set_key_on_proxy(ctx->resourceProvider(), mippedProxy.get(), proxy.get(), key);
853 return mippedProxy;
854 }
855 // We failed to make a mipped proxy with the base copied into it. This could have
856 // been from failure to make the proxy or failure to do the copy. Thus we will fall
857 // back to just using the non mipped proxy; See skbug.com/7094.
858 return proxy;
859 }
860
Brian Osmandf7e0752017-04-26 16:20:28 -0400861 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
862 kLockTexturePathCount);
863 return nullptr;
864}
865
866///////////////////////////////////////////////////////////////////////////////////////////////////
867
868#endif