blob: 3fe023d900660c9910dc8750cfede9a48ec71f4a [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;
Brian Osmanb62f50c2018-07-12 14:44:27 -040097 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType) const override;
reed85d91782015-09-10 14:33:38 -070098
Brian Osman5bbd0762017-05-08 11:07:42 -040099 bool onIsValid(GrContext*) const override;
100
Brian Osmandf7e0752017-04-26 16:20:28 -0400101 SkImageCacherator* peekCacherator() const override {
102 return const_cast<SkImage_Lazy*>(this);
103 }
104
105 // Only return true if the generate has already been cached.
Brian Osmaneb7e5292018-08-08 14:32:06 -0400106 bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*) const;
Brian Osmandf7e0752017-04-26 16:20:28 -0400107 // Call the underlying generator directly
108 bool directGeneratePixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
Brian Osmanc87cfb62018-07-11 09:08:46 -0400109 int srcX, int srcY) const;
Brian Osmandf7e0752017-04-26 16:20:28 -0400110
111 // SkImageCacherator interface
112#if SK_SUPPORT_GPU
113 // Returns the texture proxy. If the cacherator is generating the texture and wants to cache it,
114 // it should use the passed in key (if the key is valid).
115 sk_sp<GrTextureProxy> lockTextureProxy(GrContext*,
116 const GrUniqueKey& key,
117 SkImage::CachingHint,
118 bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -0400119 SkColorSpace* dstColorSpace,
120 GrTextureMaker::AllowedTexGenType genType) override;
Brian Osmandf7e0752017-04-26 16:20:28 -0400121
122 // Returns the color space of the texture that would be returned if you called lockTexture.
123 // Separate code path to allow querying of the color space for textures that cached (even
124 // externally).
125 sk_sp<SkColorSpace> getColorSpace(GrContext*, SkColorSpace* dstColorSpace) override;
Brian Osmaneb7e5292018-08-08 14:32:06 -0400126
127 // TODO: Need to pass in dstColorSpace to fold into key here?
128 void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) override;
Brian Osmandf7e0752017-04-26 16:20:28 -0400129#endif
130
Brian Osmaneb7e5292018-08-08 14:32:06 -0400131 SkImageInfo buildCacheInfo() const override;
Brian Osmandf7e0752017-04-26 16:20:28 -0400132
reed85d91782015-09-10 14:33:38 -0700133private:
Brian Osmandf7e0752017-04-26 16:20:28 -0400134 class ScopedGenerator;
135
136 /**
137 * On success (true), bitmap will point to the pixels for this generator. If this returns
138 * false, the bitmap will be reset to empty.
Brian Osmaneb7e5292018-08-08 14:32:06 -0400139 * TODO: Pass in dstColorSpace to ensure bitmap is compatible?
Brian Osmandf7e0752017-04-26 16:20:28 -0400140 */
Brian Osmaneb7e5292018-08-08 14:32:06 -0400141 bool lockAsBitmap(SkBitmap*, SkImage::CachingHint, const SkImageInfo&) const;
Brian Osmandf7e0752017-04-26 16:20:28 -0400142
143 sk_sp<SharedGenerator> fSharedGenerator;
Christopher Cameron77e96662017-07-08 01:47:47 -0700144 // Note that fInfo is not necessarily the info from the generator. It may be cropped by
145 // onMakeSubset and its color space may be changed by onMakeColorSpace.
Brian Osmandf7e0752017-04-26 16:20:28 -0400146 const SkImageInfo fInfo;
147 const SkIPoint fOrigin;
148
Brian Osmaneb7e5292018-08-08 14:32:06 -0400149 uint32_t fUniqueID;
reed85d91782015-09-10 14:33:38 -0700150
Christopher Camerond4b67872017-07-13 15:18:08 -0700151 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and
152 // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call.
153 mutable SkMutex fOnMakeColorSpaceMutex;
154 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget;
155 mutable sk_sp<SkImage> fOnMakeColorSpaceResult;
156
reed85d91782015-09-10 14:33:38 -0700157 typedef SkImage_Base INHERITED;
158};
159
160///////////////////////////////////////////////////////////////////////////////
161
Christopher Cameron77e96662017-07-08 01:47:47 -0700162SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset,
163 sk_sp<SkColorSpace> colorSpace)
Brian Osmandf7e0752017-04-26 16:20:28 -0400164 : fSharedGenerator(std::move(gen)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400165 if (!fSharedGenerator) {
166 return;
167 }
168
169 // The following generator accessors are safe without acquiring the mutex (const getters).
170 // TODO: refactor to use a ScopedGenerator instead, for clarity.
171 const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo();
172 if (info.isEmpty()) {
173 fSharedGenerator.reset();
174 return;
175 }
176
177 fUniqueID = fSharedGenerator->fGenerator->uniqueID();
178 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
179 if (subset) {
180 if (!bounds.contains(*subset)) {
181 fSharedGenerator.reset();
182 return;
183 }
184 if (*subset != bounds) {
185 // we need a different uniqueID since we really are a subset of the raw generator
186 fUniqueID = SkNextID::ImageID();
187 }
188 } else {
189 subset = &bounds;
190 }
191
192 fInfo = info.makeWH(subset->width(), subset->height());
193 fOrigin = SkIPoint::Make(subset->x(), subset->y());
Christopher Cameron77e96662017-07-08 01:47:47 -0700194 if (colorSpace) {
195 fInfo = fInfo.makeColorSpace(colorSpace);
196 fUniqueID = SkNextID::ImageID();
197 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400198}
199
200///////////////////////////////////////////////////////////////////////////////
201
202// Helper for exclusive access to a shared generator.
203class SkImage_Lazy::ScopedGenerator {
204public:
205 ScopedGenerator(const sk_sp<SharedGenerator>& gen)
206 : fSharedGenerator(gen)
207 , fAutoAquire(gen->fMutex) {}
208
209 SkImageGenerator* operator->() const {
210 fSharedGenerator->fMutex.assertHeld();
211 return fSharedGenerator->fGenerator.get();
212 }
213
214 operator SkImageGenerator*() const {
215 fSharedGenerator->fMutex.assertHeld();
216 return fSharedGenerator->fGenerator.get();
217 }
218
219private:
220 const sk_sp<SharedGenerator>& fSharedGenerator;
221 SkAutoExclusive fAutoAquire;
222};
223
224///////////////////////////////////////////////////////////////////////////////
225
226SkImage_Lazy::SkImage_Lazy(Validator* validator)
227 : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID)
228 , fSharedGenerator(std::move(validator->fSharedGenerator))
229 , fInfo(validator->fInfo)
230 , fOrigin(validator->fOrigin) {
231 SkASSERT(fSharedGenerator);
Brian Osmaneb7e5292018-08-08 14:32:06 -0400232 fUniqueID = validator->fUniqueID;
Brian Osmandf7e0752017-04-26 16:20:28 -0400233}
234
235//////////////////////////////////////////////////////////////////////////////////////////////////
236
Brian Osmaneb7e5292018-08-08 14:32:06 -0400237SkImageInfo SkImage_Lazy::buildCacheInfo() const {
Brian Osmanbfc33e52018-06-27 14:24:11 -0400238 if (kGray_8_SkColorType == fInfo.colorType()) {
239 return fInfo.makeColorSpace(nullptr);
240 } else {
241 return fInfo;
242 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400243}
244
245//////////////////////////////////////////////////////////////////////////////////////////////////
246
247static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) {
248 SkASSERT(bitmap.getGenerationID() == expectedID);
249 SkASSERT(bitmap.isImmutable());
250 SkASSERT(bitmap.getPixels());
251 return true;
252}
253
254bool SkImage_Lazy::directGeneratePixels(const SkImageInfo& info, void* pixels, size_t rb,
Brian Osmanc87cfb62018-07-11 09:08:46 -0400255 int srcX, int srcY) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400256 ScopedGenerator generator(fSharedGenerator);
257 const SkImageInfo& genInfo = generator->getInfo();
258 // Currently generators do not natively handle subsets, so check that first.
259 if (srcX || srcY || genInfo.width() != info.width() || genInfo.height() != info.height()) {
260 return false;
261 }
262
Brian Osmanc87cfb62018-07-11 09:08:46 -0400263 return generator->getPixels(info, pixels, rb);
Brian Osmandf7e0752017-04-26 16:20:28 -0400264}
265
266//////////////////////////////////////////////////////////////////////////////////////////////////
267
Brian Osmaneb7e5292018-08-08 14:32:06 -0400268bool SkImage_Lazy::lockAsBitmapOnlyIfAlreadyCached(SkBitmap* bitmap) const {
269 return SkBitmapCache::Find(SkBitmapCacheDesc::Make(fUniqueID,
Brian Osmandf7e0752017-04-26 16:20:28 -0400270 fInfo.width(), fInfo.height()), bitmap) &&
Brian Osmaneb7e5292018-08-08 14:32:06 -0400271 check_output_bitmap(*bitmap, fUniqueID);
Brian Osmandf7e0752017-04-26 16:20:28 -0400272}
273
Brian Osmanc87cfb62018-07-11 09:08:46 -0400274static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400275 const int genW = gen->getInfo().width();
276 const int genH = gen->getInfo().height();
277 const SkIRect srcR = SkIRect::MakeWH(genW, genH);
278 const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
279 if (!srcR.contains(dstR)) {
280 return false;
281 }
282
283 // If they are requesting a subset, we have to have a temp allocation for full image, and
284 // then copy the subset into their allocation
285 SkBitmap full;
286 SkPixmap fullPM;
287 const SkPixmap* dstPM = &pmap;
288 if (srcR != dstR) {
289 if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
290 return false;
291 }
292 if (!full.peekPixels(&fullPM)) {
293 return false;
294 }
295 dstPM = &fullPM;
296 }
297
Brian Osmanc87cfb62018-07-11 09:08:46 -0400298 if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400299 return false;
300 }
301
302 if (srcR != dstR) {
303 if (!full.readPixels(pmap, originX, originY)) {
304 return false;
305 }
306 }
307 return true;
308}
309
Brian Osmaneb7e5292018-08-08 14:32:06 -0400310bool SkImage_Lazy::lockAsBitmap(SkBitmap* bitmap, SkImage::CachingHint chint,
Brian Osmanc87cfb62018-07-11 09:08:46 -0400311 const SkImageInfo& info) const {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400312 // TODO: Verify dstColorSpace here
313 if (this->lockAsBitmapOnlyIfAlreadyCached(bitmap)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400314 return true;
315 }
316
Brian Osmandf7e0752017-04-26 16:20:28 -0400317 SkBitmap tmpBitmap;
318 SkBitmapCache::RecPtr cacheRec;
319 SkPixmap pmap;
320 if (SkImage::kAllow_CachingHint == chint) {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400321 auto desc = SkBitmapCacheDesc::Make(fUniqueID, info.width(), info.height());
Brian Osmandf7e0752017-04-26 16:20:28 -0400322 cacheRec = SkBitmapCache::Alloc(desc, info, &pmap);
323 if (!cacheRec) {
324 return false;
325 }
326 } else {
327 if (!tmpBitmap.tryAllocPixels(info)) {
328 return false;
329 }
330 if (!tmpBitmap.peekPixels(&pmap)) {
331 return false;
332 }
333 }
334
335 ScopedGenerator generator(fSharedGenerator);
Brian Osmanc87cfb62018-07-11 09:08:46 -0400336 if (!generate_pixels(generator, pmap, fOrigin.x(), fOrigin.y())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400337 return false;
338 }
339
340 if (cacheRec) {
341 SkBitmapCache::Add(std::move(cacheRec), bitmap);
342 SkASSERT(bitmap->getPixels()); // we're locked
343 SkASSERT(bitmap->isImmutable());
Brian Osmaneb7e5292018-08-08 14:32:06 -0400344 SkASSERT(bitmap->getGenerationID() == fUniqueID);
Mike Reed30301c42018-07-19 09:39:21 -0400345 this->notifyAddedToRasterCache();
Brian Osmandf7e0752017-04-26 16:20:28 -0400346 } else {
347 *bitmap = tmpBitmap;
Brian Osmaneb7e5292018-08-08 14:32:06 -0400348 bitmap->pixelRef()->setImmutableWithID(fUniqueID);
Brian Osmandf7e0752017-04-26 16:20:28 -0400349 }
350
Brian Osmaneb7e5292018-08-08 14:32:06 -0400351 check_output_bitmap(*bitmap, fUniqueID);
Brian Osmandf7e0752017-04-26 16:20:28 -0400352 return true;
353}
354
355//////////////////////////////////////////////////////////////////////////////////////////////////
356
Brian Osmanf1b43822017-04-20 13:43:23 -0400357bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
358 int srcX, int srcY, CachingHint chint) const {
Brian Osman61624f02016-12-09 14:51:59 -0500359 SkColorSpace* dstColorSpace = dstInfo.colorSpace();
reed85d91782015-09-10 14:33:38 -0700360 SkBitmap bm;
reed6868c3f2015-11-24 11:44:47 -0800361 if (kDisallow_CachingHint == chint) {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400362 if (this->lockAsBitmapOnlyIfAlreadyCached(&bm)) {
reed6868c3f2015-11-24 11:44:47 -0800363 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
364 } else {
365 // Try passing the caller's buffer directly down to the generator. If this fails we
366 // may still succeed in the general case, as the generator may prefer some other
367 // config, which we could then convert via SkBitmap::readPixels.
Brian Osmanc87cfb62018-07-11 09:08:46 -0400368 if (this->directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) {
reed6868c3f2015-11-24 11:44:47 -0800369 return true;
370 }
371 // else fall through
372 }
373 }
374
Brian Osman61624f02016-12-09 14:51:59 -0500375 if (this->getROPixels(&bm, dstColorSpace, chint)) {
reed85d91782015-09-10 14:33:38 -0700376 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
377 }
378 return false;
379}
380
Ben Wagnerbdf54332018-05-15 14:12:14 -0400381sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
382 ScopedGenerator generator(fSharedGenerator);
383 return generator->refEncodedData();
384}
reed85d91782015-09-10 14:33:38 -0700385
Brian Osmanf1b43822017-04-20 13:43:23 -0400386bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace,
387 CachingHint chint) const {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400388 const SkImageInfo cacheInfo = this->buildCacheInfo();
389 return this->lockAsBitmap(bitmap, chint, cacheInfo);
reed85d91782015-09-10 14:33:38 -0700390}
391
Brian Osman5bbd0762017-05-08 11:07:42 -0400392bool SkImage_Lazy::onIsValid(GrContext* context) const {
393 ScopedGenerator generator(fSharedGenerator);
394 return generator->isValid(context);
395}
396
Mike Reed7f1d0202017-05-08 16:13:39 -0400397bool SkImage_Lazy::onCanLazyGenerateOnGPU() const {
398#if SK_SUPPORT_GPU
399 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400400 return SkImageGenerator::TexGenType::kNone != generator->onCanGenerateTexture();
Mike Reed7f1d0202017-05-08 16:13:39 -0400401#else
402 return false;
403#endif
404}
405
Brian Osmandf7e0752017-04-26 16:20:28 -0400406///////////////////////////////////////////////////////////////////////////////////////////////////
407
Robert Phillipsb726d582017-03-09 16:36:32 -0500408#if SK_SUPPORT_GPU
Brian Osmanf1b43822017-04-20 13:43:23 -0400409sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrContext* context,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400410 const GrSamplerState& params,
Brian Osmanf1b43822017-04-20 13:43:23 -0400411 SkColorSpace* dstColorSpace,
412 sk_sp<SkColorSpace>* texColorSpace,
413 SkScalar scaleAdjust[2]) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400414 if (!context) {
415 return nullptr;
416 }
417
418 GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
419 return textureMaker.refTextureProxyForParams(params, dstColorSpace, texColorSpace, scaleAdjust);
Robert Phillipsb726d582017-03-09 16:36:32 -0500420}
421#endif
422
Brian Osmanf1b43822017-04-20 13:43:23 -0400423sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400424 SkASSERT(fInfo.bounds().contains(subset));
425 SkASSERT(fInfo.bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700426
Brian Osmandf7e0752017-04-26 16:20:28 -0400427 const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y());
Christopher Cameron77e96662017-07-08 01:47:47 -0700428 Validator validator(fSharedGenerator, &generatorSubset, fInfo.refColorSpace());
Brian Osmanf1b43822017-04-20 13:43:23 -0400429 return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
reed7b6945b2015-09-24 00:50:58 -0700430}
431
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400432sk_sp<SkImage> SkImage_Lazy::onMakeColorSpace(sk_sp<SkColorSpace> target,
Brian Osmanb62f50c2018-07-12 14:44:27 -0400433 SkColorType targetColorType) const {
Christopher Camerond4b67872017-07-13 15:18:08 -0700434 SkAutoExclusive autoAquire(fOnMakeColorSpaceMutex);
435 if (target && fOnMakeColorSpaceTarget &&
436 SkColorSpace::Equals(target.get(), fOnMakeColorSpaceTarget.get())) {
437 return fOnMakeColorSpaceResult;
438 }
Christopher Cameron77e96662017-07-08 01:47:47 -0700439 const SkIRect generatorSubset =
440 SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
441 Validator validator(fSharedGenerator, &generatorSubset, target);
Christopher Camerond4b67872017-07-13 15:18:08 -0700442 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
443 if (result) {
444 fOnMakeColorSpaceTarget = target;
445 fOnMakeColorSpaceResult = result;
446 }
447 return result;
Matt Sarett6de13102017-03-14 14:10:48 -0400448}
449
Mike Reed185130c2017-02-15 15:14:16 -0500450sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
451 const SkIRect* subset) {
Christopher Cameron77e96662017-07-08 01:47:47 -0700452 SkImage_Lazy::Validator validator(SharedGenerator::Make(std::move(generator)), subset, nullptr);
fmalita7929e3a2016-10-27 08:15:44 -0700453
Brian Osmanf1b43822017-04-20 13:43:23 -0400454 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700455}
Brian Osmandf7e0752017-04-26 16:20:28 -0400456
457//////////////////////////////////////////////////////////////////////////////////////////////////
458
459/**
460 * Implementation of SkImageCacherator interface, as needed by GrImageTextureMaker
461 */
462
463#if SK_SUPPORT_GPU
464
Brian Osmaneb7e5292018-08-08 14:32:06 -0400465void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) {
466 // TODO: Take dstColorSpace, include hash in key
Brian Osmandf7e0752017-04-26 16:20:28 -0400467 SkASSERT(!cacheKey->isValid());
468 if (origKey.isValid()) {
469 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Osmaneb7e5292018-08-08 14:32:06 -0400470 GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image");
Brian Osmandf7e0752017-04-26 16:20:28 -0400471 }
472}
473
474class Generator_GrYUVProvider : public GrYUVProvider {
475 SkImageGenerator* fGen;
476
477public:
478 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
479
480 uint32_t onGetID() override { return fGen->uniqueID(); }
481 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
482 return fGen->queryYUV8(sizeInfo, colorSpace);
483 }
484 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
485 return fGen->getYUV8Planes(sizeInfo, planes);
486 }
487};
488
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500489static void set_key_on_proxy(GrProxyProvider* proxyProvider,
Greg Danielfc5060d2017-10-04 18:36:15 +0000490 GrTextureProxy* proxy, GrTextureProxy* originalProxy,
491 const GrUniqueKey& key) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400492 if (key.isValid()) {
Robert Phillips8a90f502017-07-24 15:09:56 -0400493 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Greg Danielf6f7b672018-02-15 13:06:26 -0500494 if (originalProxy && originalProxy->getUniqueKey().isValid()) {
495 SkASSERT(originalProxy->getUniqueKey() == key);
Greg Daniele252f082017-10-23 16:05:23 -0400496 SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
497 GrMipMapped::kNo == originalProxy->mipMapped());
Greg Danielf6f7b672018-02-15 13:06:26 -0500498 // If we had an originalProxy with a valid key, that means there already is a proxy in
499 // the cache which matches the key, but it does not have mip levels and we require them.
500 // Thus we must remove the unique key from that proxy.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500501 proxyProvider->removeUniqueKeyFromProxy(key, originalProxy);
Greg Danielfc5060d2017-10-04 18:36:15 +0000502 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500503 proxyProvider->assignUniqueKeyToProxy(key, proxy);
Brian Osmandf7e0752017-04-26 16:20:28 -0400504 }
505}
506
507sk_sp<SkColorSpace> SkImage_Lazy::getColorSpace(GrContext* ctx, SkColorSpace* dstColorSpace) {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400508 // TODO: Is this ever needed? Is the output of this function going to be:
509 // return dstColorSpace ? fInfo.refColorSpace() : dstColorSpace;
510 // Yes, except for gray?
Brian Osmana8ac9242017-09-07 10:19:08 -0400511 if (!dstColorSpace) {
512 // In legacy mode, we do no modification to the image's color space or encoding.
513 // Subsequent legacy drawing is likely to ignore the color space, but some clients
514 // may want to know what space the image data is in, so return it.
515 return fInfo.refColorSpace();
516 } else {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400517 SkImageInfo cacheInfo = this->buildCacheInfo();
Brian Osmana8ac9242017-09-07 10:19:08 -0400518 return cacheInfo.refColorSpace();
519 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400520}
521
522/*
523 * We have 4 ways to try to return a texture (in sorted order)
524 *
525 * 1. Check the cache for a pre-existing one
526 * 2. Ask the generator to natively create one
527 * 3. Ask the generator to return YUV planes, which the GPU can convert
528 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
529 */
530sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(GrContext* ctx,
531 const GrUniqueKey& origKey,
532 SkImage::CachingHint chint,
533 bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -0400534 SkColorSpace* dstColorSpace,
535 GrTextureMaker::AllowedTexGenType genType) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400536 // Values representing the various texture lock paths we can take. Used for logging the path
537 // taken to a histogram.
538 enum LockTexturePath {
539 kFailure_LockTexturePath,
540 kPreExisting_LockTexturePath,
541 kNative_LockTexturePath,
542 kCompressed_LockTexturePath, // Deprecated
543 kYUV_LockTexturePath,
544 kRGBA_LockTexturePath,
545 };
546
547 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
548
Brian Osmaneb7e5292018-08-08 14:32:06 -0400549 // Build our texture key.
550 // TODO: This needs to include the dstColorSpace.
Brian Osmandf7e0752017-04-26 16:20:28 -0400551 GrUniqueKey key;
Brian Osmaneb7e5292018-08-08 14:32:06 -0400552 this->makeCacheKeyFromOrigKey(origKey, &key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400553
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500554 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +0000555 sk_sp<GrTextureProxy> proxy;
556
Brian Osmandf7e0752017-04-26 16:20:28 -0400557 // 1. Check the cache for a pre-existing one
558 if (key.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500559 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
Greg Danielfc5060d2017-10-04 18:36:15 +0000560 if (proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400561 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
562 kLockTexturePathCount);
Greg Daniele252f082017-10-23 16:05:23 -0400563 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000564 return proxy;
565 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400566 }
567 }
568
Brian Osmaneb7e5292018-08-08 14:32:06 -0400569 // What format are we going to ask the generator to create?
570 // TODO: Based on the dstColorSpace?
571 const SkImageInfo cacheInfo = this->buildCacheInfo();
Brian Osmandf7e0752017-04-26 16:20:28 -0400572
573 // 2. Ask the generator to natively create one
Greg Danielfc5060d2017-10-04 18:36:15 +0000574 if (!proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400575 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400576 if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
577 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
578 return nullptr;
579 }
Brian Osmanc87cfb62018-07-11 09:08:46 -0400580 if ((proxy = generator->generateTexture(ctx, cacheInfo, fOrigin, willBeMipped))) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400581 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
582 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500583 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniele252f082017-10-23 16:05:23 -0400584 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000585 return proxy;
586 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400587 }
588 }
589
Greg Daniel3e70fa32017-10-05 16:27:06 -0400590 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
591 // the texture we fall through here and have the CPU generate the mip maps for us.
592 if (!proxy && !willBeMipped && !ctx->contextPriv().disableGpuYUVConversion()) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400593 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(cacheInfo);
Brian Osmandf7e0752017-04-26 16:20:28 -0400594 ScopedGenerator generator(fSharedGenerator);
595 Generator_GrYUVProvider provider(generator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700596
597 // The pixels in the texture will be in the generator's color space. If onMakeColorSpace
598 // has been called then this will not match this image's color space. To correct this, apply
599 // a color space conversion from the generator's color space to this image's color space.
Brian Osman56893cd2018-06-08 14:11:37 -0400600 // 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 -0400601 SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
602 SkColorSpace* thisColorSpace = fInfo.colorSpace();
Christopher Cameron77e96662017-07-08 01:47:47 -0700603
Brian Osman56893cd2018-06-08 14:11:37 -0400604 if ((!generatorColorSpace || generatorColorSpace->toXYZD50()) &&
605 (!thisColorSpace || thisColorSpace->toXYZD50())) {
606 // TODO: Update to create the mipped surface in the YUV generator and draw the base
607 // layer directly into the mipped surface.
608 proxy = provider.refAsTextureProxy(ctx, desc, generatorColorSpace, thisColorSpace);
609 if (proxy) {
610 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
611 kLockTexturePathCount);
612 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
613 return proxy;
614 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400615 }
616 }
617
618 // 4. Ask the generator to return RGB(A) data, which the GPU can convert
619 SkBitmap bitmap;
Brian Osmaneb7e5292018-08-08 14:32:06 -0400620 if (!proxy && this->lockAsBitmap(&bitmap, chint, cacheInfo)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400621 if (willBeMipped) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400622 proxy = proxyProvider->createMipMapProxyFromBitmap(bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400623 }
624 if (!proxy) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400625 proxy = GrUploadBitmapToTextureProxy(proxyProvider, bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400626 }
Greg Daniele252f082017-10-23 16:05:23 -0400627 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400628 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
629 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500630 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400631 return proxy;
632 }
633 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000634
635 if (proxy) {
636 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
637 // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
638 // mipped surface and copy the original proxy into the base layer. We will then let the gpu
639 // generate the rest of the mips.
640 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -0400641 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Daniele1da1d92017-10-06 15:59:27 -0400642 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500643 set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key);
Greg Danielfc5060d2017-10-04 18:36:15 +0000644 return mippedProxy;
645 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400646 // We failed to make a mipped proxy with the base copied into it. This could have
647 // been from failure to make the proxy or failure to do the copy. Thus we will fall
648 // back to just using the non mipped proxy; See skbug.com/7094.
649 return proxy;
Greg Danielfc5060d2017-10-04 18:36:15 +0000650 }
651
Brian Osmandf7e0752017-04-26 16:20:28 -0400652 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
653 kLockTexturePathCount);
654 return nullptr;
655}
656
657///////////////////////////////////////////////////////////////////////////////////////////////////
658
659#endif