blob: 1090e3e7af3e29e7e5afb0a4feb8c0244eb4fe25 [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; }
Brian Osmanb62f50c2018-07-12 14:44:27 -040096 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType) const override;
reed85d91782015-09-10 14:33:38 -070097
Brian Osman5bbd0762017-05-08 11:07:42 -040098 bool onIsValid(GrContext*) const override;
99
Brian Osmandf7e0752017-04-26 16:20:28 -0400100 SkImageCacherator* peekCacherator() const override {
101 return const_cast<SkImage_Lazy*>(this);
102 }
103
104 // Only return true if the generate has already been cached.
Brian Osmaneb7e5292018-08-08 14:32:06 -0400105 bool lockAsBitmapOnlyIfAlreadyCached(SkBitmap*) const;
Brian Osmandf7e0752017-04-26 16:20:28 -0400106 // Call the underlying generator directly
107 bool directGeneratePixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
Brian Osmanc87cfb62018-07-11 09:08:46 -0400108 int srcX, int srcY) const;
Brian Osmandf7e0752017-04-26 16:20:28 -0400109
110 // SkImageCacherator interface
111#if SK_SUPPORT_GPU
112 // Returns the texture proxy. If the cacherator is generating the texture and wants to cache it,
113 // it should use the passed in key (if the key is valid).
114 sk_sp<GrTextureProxy> lockTextureProxy(GrContext*,
115 const GrUniqueKey& key,
116 SkImage::CachingHint,
117 bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -0400118 SkColorSpace* dstColorSpace,
119 GrTextureMaker::AllowedTexGenType genType) override;
Brian Osmandf7e0752017-04-26 16:20:28 -0400120
121 // Returns the color space of the texture that would be returned if you called lockTexture.
122 // Separate code path to allow querying of the color space for textures that cached (even
123 // externally).
124 sk_sp<SkColorSpace> getColorSpace(GrContext*, SkColorSpace* dstColorSpace) override;
Brian Osmaneb7e5292018-08-08 14:32:06 -0400125
126 // TODO: Need to pass in dstColorSpace to fold into key here?
127 void makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) override;
Brian Osmandf7e0752017-04-26 16:20:28 -0400128#endif
129
Brian Osman7ed0eae2018-08-30 13:46:36 +0000130 SkImageInfo buildCacheInfo() const override;
131
reed85d91782015-09-10 14:33:38 -0700132private:
Brian Osmandf7e0752017-04-26 16:20:28 -0400133 class ScopedGenerator;
134
135 /**
136 * On success (true), bitmap will point to the pixels for this generator. If this returns
137 * false, the bitmap will be reset to empty.
Brian Osmaneb7e5292018-08-08 14:32:06 -0400138 * TODO: Pass in dstColorSpace to ensure bitmap is compatible?
Brian Osmandf7e0752017-04-26 16:20:28 -0400139 */
Brian Osmaneb7e5292018-08-08 14:32:06 -0400140 bool lockAsBitmap(SkBitmap*, SkImage::CachingHint, const SkImageInfo&) const;
Brian Osmandf7e0752017-04-26 16:20:28 -0400141
142 sk_sp<SharedGenerator> fSharedGenerator;
Christopher Cameron77e96662017-07-08 01:47:47 -0700143 // Note that fInfo is not necessarily the info from the generator. It may be cropped by
144 // onMakeSubset and its color space may be changed by onMakeColorSpace.
Brian Osmandf7e0752017-04-26 16:20:28 -0400145 const SkImageInfo fInfo;
146 const SkIPoint fOrigin;
147
Brian Osmaneb7e5292018-08-08 14:32:06 -0400148 uint32_t fUniqueID;
reed85d91782015-09-10 14:33:38 -0700149
Christopher Camerond4b67872017-07-13 15:18:08 -0700150 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and
151 // SkImage_Lazy instances. Cache the result of the last successful onMakeColorSpace call.
152 mutable SkMutex fOnMakeColorSpaceMutex;
153 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget;
154 mutable sk_sp<SkImage> fOnMakeColorSpaceResult;
155
reed85d91782015-09-10 14:33:38 -0700156 typedef SkImage_Base INHERITED;
157};
158
159///////////////////////////////////////////////////////////////////////////////
160
Christopher Cameron77e96662017-07-08 01:47:47 -0700161SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset,
162 sk_sp<SkColorSpace> colorSpace)
Brian Osmandf7e0752017-04-26 16:20:28 -0400163 : fSharedGenerator(std::move(gen)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400164 if (!fSharedGenerator) {
165 return;
166 }
167
168 // The following generator accessors are safe without acquiring the mutex (const getters).
169 // TODO: refactor to use a ScopedGenerator instead, for clarity.
170 const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo();
171 if (info.isEmpty()) {
172 fSharedGenerator.reset();
173 return;
174 }
175
176 fUniqueID = fSharedGenerator->fGenerator->uniqueID();
177 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
178 if (subset) {
179 if (!bounds.contains(*subset)) {
180 fSharedGenerator.reset();
181 return;
182 }
183 if (*subset != bounds) {
184 // we need a different uniqueID since we really are a subset of the raw generator
185 fUniqueID = SkNextID::ImageID();
186 }
187 } else {
188 subset = &bounds;
189 }
190
191 fInfo = info.makeWH(subset->width(), subset->height());
192 fOrigin = SkIPoint::Make(subset->x(), subset->y());
Christopher Cameron77e96662017-07-08 01:47:47 -0700193 if (colorSpace) {
194 fInfo = fInfo.makeColorSpace(colorSpace);
195 fUniqueID = SkNextID::ImageID();
196 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400197}
198
199///////////////////////////////////////////////////////////////////////////////
200
201// Helper for exclusive access to a shared generator.
202class SkImage_Lazy::ScopedGenerator {
203public:
204 ScopedGenerator(const sk_sp<SharedGenerator>& gen)
205 : fSharedGenerator(gen)
206 , fAutoAquire(gen->fMutex) {}
207
208 SkImageGenerator* operator->() const {
209 fSharedGenerator->fMutex.assertHeld();
210 return fSharedGenerator->fGenerator.get();
211 }
212
213 operator SkImageGenerator*() const {
214 fSharedGenerator->fMutex.assertHeld();
215 return fSharedGenerator->fGenerator.get();
216 }
217
218private:
219 const sk_sp<SharedGenerator>& fSharedGenerator;
220 SkAutoExclusive fAutoAquire;
221};
222
223///////////////////////////////////////////////////////////////////////////////
224
225SkImage_Lazy::SkImage_Lazy(Validator* validator)
226 : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID)
227 , fSharedGenerator(std::move(validator->fSharedGenerator))
228 , fInfo(validator->fInfo)
229 , fOrigin(validator->fOrigin) {
230 SkASSERT(fSharedGenerator);
Brian Osmaneb7e5292018-08-08 14:32:06 -0400231 fUniqueID = validator->fUniqueID;
Brian Osmandf7e0752017-04-26 16:20:28 -0400232}
233
234//////////////////////////////////////////////////////////////////////////////////////////////////
235
Brian Osman7ed0eae2018-08-30 13:46:36 +0000236SkImageInfo SkImage_Lazy::buildCacheInfo() const {
237 if (kGray_8_SkColorType == fInfo.colorType()) {
238 return fInfo.makeColorSpace(nullptr);
239 } else {
240 return fInfo;
241 }
242}
243
244//////////////////////////////////////////////////////////////////////////////////////////////////
245
Brian Osmandf7e0752017-04-26 16:20:28 -0400246static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) {
247 SkASSERT(bitmap.getGenerationID() == expectedID);
248 SkASSERT(bitmap.isImmutable());
249 SkASSERT(bitmap.getPixels());
250 return true;
251}
252
253bool SkImage_Lazy::directGeneratePixels(const SkImageInfo& info, void* pixels, size_t rb,
Brian Osmanc87cfb62018-07-11 09:08:46 -0400254 int srcX, int srcY) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400255 ScopedGenerator generator(fSharedGenerator);
256 const SkImageInfo& genInfo = generator->getInfo();
257 // Currently generators do not natively handle subsets, so check that first.
258 if (srcX || srcY || genInfo.width() != info.width() || genInfo.height() != info.height()) {
259 return false;
260 }
261
Brian Osmanc87cfb62018-07-11 09:08:46 -0400262 return generator->getPixels(info, pixels, rb);
Brian Osmandf7e0752017-04-26 16:20:28 -0400263}
264
265//////////////////////////////////////////////////////////////////////////////////////////////////
266
Brian Osmaneb7e5292018-08-08 14:32:06 -0400267bool SkImage_Lazy::lockAsBitmapOnlyIfAlreadyCached(SkBitmap* bitmap) const {
268 return SkBitmapCache::Find(SkBitmapCacheDesc::Make(fUniqueID,
Brian Osmandf7e0752017-04-26 16:20:28 -0400269 fInfo.width(), fInfo.height()), bitmap) &&
Brian Osmaneb7e5292018-08-08 14:32:06 -0400270 check_output_bitmap(*bitmap, fUniqueID);
Brian Osmandf7e0752017-04-26 16:20:28 -0400271}
272
Brian Osmanc87cfb62018-07-11 09:08:46 -0400273static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400274 const int genW = gen->getInfo().width();
275 const int genH = gen->getInfo().height();
276 const SkIRect srcR = SkIRect::MakeWH(genW, genH);
277 const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
278 if (!srcR.contains(dstR)) {
279 return false;
280 }
281
282 // If they are requesting a subset, we have to have a temp allocation for full image, and
283 // then copy the subset into their allocation
284 SkBitmap full;
285 SkPixmap fullPM;
286 const SkPixmap* dstPM = &pmap;
287 if (srcR != dstR) {
288 if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
289 return false;
290 }
291 if (!full.peekPixels(&fullPM)) {
292 return false;
293 }
294 dstPM = &fullPM;
295 }
296
Brian Osmanc87cfb62018-07-11 09:08:46 -0400297 if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400298 return false;
299 }
300
301 if (srcR != dstR) {
302 if (!full.readPixels(pmap, originX, originY)) {
303 return false;
304 }
305 }
306 return true;
307}
308
Brian Osmaneb7e5292018-08-08 14:32:06 -0400309bool SkImage_Lazy::lockAsBitmap(SkBitmap* bitmap, SkImage::CachingHint chint,
Brian Osmanc87cfb62018-07-11 09:08:46 -0400310 const SkImageInfo& info) const {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400311 // TODO: Verify dstColorSpace here
312 if (this->lockAsBitmapOnlyIfAlreadyCached(bitmap)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400313 return true;
314 }
315
Brian Osmandf7e0752017-04-26 16:20:28 -0400316 SkBitmap tmpBitmap;
317 SkBitmapCache::RecPtr cacheRec;
318 SkPixmap pmap;
319 if (SkImage::kAllow_CachingHint == chint) {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400320 auto desc = SkBitmapCacheDesc::Make(fUniqueID, info.width(), info.height());
Brian Osmandf7e0752017-04-26 16:20:28 -0400321 cacheRec = SkBitmapCache::Alloc(desc, info, &pmap);
322 if (!cacheRec) {
323 return false;
324 }
325 } else {
326 if (!tmpBitmap.tryAllocPixels(info)) {
327 return false;
328 }
329 if (!tmpBitmap.peekPixels(&pmap)) {
330 return false;
331 }
332 }
333
334 ScopedGenerator generator(fSharedGenerator);
Brian Osmanc87cfb62018-07-11 09:08:46 -0400335 if (!generate_pixels(generator, pmap, fOrigin.x(), fOrigin.y())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400336 return false;
337 }
338
339 if (cacheRec) {
340 SkBitmapCache::Add(std::move(cacheRec), bitmap);
341 SkASSERT(bitmap->getPixels()); // we're locked
342 SkASSERT(bitmap->isImmutable());
Brian Osmaneb7e5292018-08-08 14:32:06 -0400343 SkASSERT(bitmap->getGenerationID() == fUniqueID);
Mike Reed30301c42018-07-19 09:39:21 -0400344 this->notifyAddedToRasterCache();
Brian Osmandf7e0752017-04-26 16:20:28 -0400345 } else {
346 *bitmap = tmpBitmap;
Brian Osmaneb7e5292018-08-08 14:32:06 -0400347 bitmap->pixelRef()->setImmutableWithID(fUniqueID);
Brian Osmandf7e0752017-04-26 16:20:28 -0400348 }
349
Brian Osmaneb7e5292018-08-08 14:32:06 -0400350 check_output_bitmap(*bitmap, fUniqueID);
Brian Osmandf7e0752017-04-26 16:20:28 -0400351 return true;
352}
353
354//////////////////////////////////////////////////////////////////////////////////////////////////
355
Brian Osmanf1b43822017-04-20 13:43:23 -0400356bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
357 int srcX, int srcY, CachingHint chint) const {
Brian Osman61624f02016-12-09 14:51:59 -0500358 SkColorSpace* dstColorSpace = dstInfo.colorSpace();
reed85d91782015-09-10 14:33:38 -0700359 SkBitmap bm;
reed6868c3f2015-11-24 11:44:47 -0800360 if (kDisallow_CachingHint == chint) {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400361 if (this->lockAsBitmapOnlyIfAlreadyCached(&bm)) {
reed6868c3f2015-11-24 11:44:47 -0800362 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
363 } else {
364 // Try passing the caller's buffer directly down to the generator. If this fails we
365 // may still succeed in the general case, as the generator may prefer some other
366 // config, which we could then convert via SkBitmap::readPixels.
Brian Osmanc87cfb62018-07-11 09:08:46 -0400367 if (this->directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) {
reed6868c3f2015-11-24 11:44:47 -0800368 return true;
369 }
370 // else fall through
371 }
372 }
373
Brian Osman61624f02016-12-09 14:51:59 -0500374 if (this->getROPixels(&bm, dstColorSpace, chint)) {
reed85d91782015-09-10 14:33:38 -0700375 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
376 }
377 return false;
378}
379
Ben Wagnerbdf54332018-05-15 14:12:14 -0400380sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
381 ScopedGenerator generator(fSharedGenerator);
382 return generator->refEncodedData();
383}
reed85d91782015-09-10 14:33:38 -0700384
Brian Osmanf1b43822017-04-20 13:43:23 -0400385bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace,
386 CachingHint chint) const {
Brian Osman7ed0eae2018-08-30 13:46:36 +0000387 const SkImageInfo cacheInfo = this->buildCacheInfo();
388 return this->lockAsBitmap(bitmap, chint, cacheInfo);
reed85d91782015-09-10 14:33:38 -0700389}
390
Brian Osman5bbd0762017-05-08 11:07:42 -0400391bool SkImage_Lazy::onIsValid(GrContext* context) const {
392 ScopedGenerator generator(fSharedGenerator);
393 return generator->isValid(context);
394}
395
Brian Osmandf7e0752017-04-26 16:20:28 -0400396///////////////////////////////////////////////////////////////////////////////////////////////////
397
Robert Phillipsb726d582017-03-09 16:36:32 -0500398#if SK_SUPPORT_GPU
Brian Osmanf1b43822017-04-20 13:43:23 -0400399sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrContext* context,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400400 const GrSamplerState& params,
Brian Osmanf1b43822017-04-20 13:43:23 -0400401 SkColorSpace* dstColorSpace,
402 sk_sp<SkColorSpace>* texColorSpace,
403 SkScalar scaleAdjust[2]) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400404 if (!context) {
405 return nullptr;
406 }
407
408 GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
409 return textureMaker.refTextureProxyForParams(params, dstColorSpace, texColorSpace, scaleAdjust);
Robert Phillipsb726d582017-03-09 16:36:32 -0500410}
411#endif
412
Brian Osmanf1b43822017-04-20 13:43:23 -0400413sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400414 SkASSERT(fInfo.bounds().contains(subset));
415 SkASSERT(fInfo.bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700416
Brian Osmandf7e0752017-04-26 16:20:28 -0400417 const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y());
Christopher Cameron77e96662017-07-08 01:47:47 -0700418 Validator validator(fSharedGenerator, &generatorSubset, fInfo.refColorSpace());
Brian Osmanf1b43822017-04-20 13:43:23 -0400419 return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
reed7b6945b2015-09-24 00:50:58 -0700420}
421
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400422sk_sp<SkImage> SkImage_Lazy::onMakeColorSpace(sk_sp<SkColorSpace> target,
Brian Osmanb62f50c2018-07-12 14:44:27 -0400423 SkColorType targetColorType) const {
Christopher Camerond4b67872017-07-13 15:18:08 -0700424 SkAutoExclusive autoAquire(fOnMakeColorSpaceMutex);
425 if (target && fOnMakeColorSpaceTarget &&
426 SkColorSpace::Equals(target.get(), fOnMakeColorSpaceTarget.get())) {
427 return fOnMakeColorSpaceResult;
428 }
Christopher Cameron77e96662017-07-08 01:47:47 -0700429 const SkIRect generatorSubset =
430 SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
431 Validator validator(fSharedGenerator, &generatorSubset, target);
Christopher Camerond4b67872017-07-13 15:18:08 -0700432 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
433 if (result) {
434 fOnMakeColorSpaceTarget = target;
435 fOnMakeColorSpaceResult = result;
436 }
437 return result;
Matt Sarett6de13102017-03-14 14:10:48 -0400438}
439
Mike Reed185130c2017-02-15 15:14:16 -0500440sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
441 const SkIRect* subset) {
Christopher Cameron77e96662017-07-08 01:47:47 -0700442 SkImage_Lazy::Validator validator(SharedGenerator::Make(std::move(generator)), subset, nullptr);
fmalita7929e3a2016-10-27 08:15:44 -0700443
Brian Osmanf1b43822017-04-20 13:43:23 -0400444 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700445}
Brian Osmandf7e0752017-04-26 16:20:28 -0400446
447//////////////////////////////////////////////////////////////////////////////////////////////////
448
449/**
450 * Implementation of SkImageCacherator interface, as needed by GrImageTextureMaker
451 */
452
453#if SK_SUPPORT_GPU
454
Brian Osmaneb7e5292018-08-08 14:32:06 -0400455void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey, GrUniqueKey* cacheKey) {
456 // TODO: Take dstColorSpace, include hash in key
Brian Osmandf7e0752017-04-26 16:20:28 -0400457 SkASSERT(!cacheKey->isValid());
458 if (origKey.isValid()) {
459 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Osmaneb7e5292018-08-08 14:32:06 -0400460 GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image");
Brian Osmandf7e0752017-04-26 16:20:28 -0400461 }
462}
463
464class Generator_GrYUVProvider : public GrYUVProvider {
465 SkImageGenerator* fGen;
466
467public:
468 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
469
470 uint32_t onGetID() override { return fGen->uniqueID(); }
471 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
472 return fGen->queryYUV8(sizeInfo, colorSpace);
473 }
474 bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
475 return fGen->getYUV8Planes(sizeInfo, planes);
476 }
477};
478
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500479static void set_key_on_proxy(GrProxyProvider* proxyProvider,
Greg Danielfc5060d2017-10-04 18:36:15 +0000480 GrTextureProxy* proxy, GrTextureProxy* originalProxy,
481 const GrUniqueKey& key) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400482 if (key.isValid()) {
Robert Phillips8a90f502017-07-24 15:09:56 -0400483 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Greg Danielf6f7b672018-02-15 13:06:26 -0500484 if (originalProxy && originalProxy->getUniqueKey().isValid()) {
485 SkASSERT(originalProxy->getUniqueKey() == key);
Greg Daniele252f082017-10-23 16:05:23 -0400486 SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
487 GrMipMapped::kNo == originalProxy->mipMapped());
Greg Danielf6f7b672018-02-15 13:06:26 -0500488 // If we had an originalProxy with a valid key, that means there already is a proxy in
489 // the cache which matches the key, but it does not have mip levels and we require them.
490 // Thus we must remove the unique key from that proxy.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500491 proxyProvider->removeUniqueKeyFromProxy(key, originalProxy);
Greg Danielfc5060d2017-10-04 18:36:15 +0000492 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500493 proxyProvider->assignUniqueKeyToProxy(key, proxy);
Brian Osmandf7e0752017-04-26 16:20:28 -0400494 }
495}
496
497sk_sp<SkColorSpace> SkImage_Lazy::getColorSpace(GrContext* ctx, SkColorSpace* dstColorSpace) {
Brian Osmaneb7e5292018-08-08 14:32:06 -0400498 // TODO: Is this ever needed? Is the output of this function going to be:
499 // return dstColorSpace ? fInfo.refColorSpace() : dstColorSpace;
Brian Osman7ed0eae2018-08-30 13:46:36 +0000500 // Yes, except for gray?
501 if (!dstColorSpace) {
502 // In legacy mode, we do no modification to the image's color space or encoding.
503 // Subsequent legacy drawing is likely to ignore the color space, but some clients
504 // may want to know what space the image data is in, so return it.
505 return fInfo.refColorSpace();
506 } else {
507 SkImageInfo cacheInfo = this->buildCacheInfo();
508 return cacheInfo.refColorSpace();
509 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400510}
511
512/*
513 * We have 4 ways to try to return a texture (in sorted order)
514 *
515 * 1. Check the cache for a pre-existing one
516 * 2. Ask the generator to natively create one
517 * 3. Ask the generator to return YUV planes, which the GPU can convert
518 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
519 */
520sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(GrContext* ctx,
521 const GrUniqueKey& origKey,
522 SkImage::CachingHint chint,
523 bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -0400524 SkColorSpace* dstColorSpace,
525 GrTextureMaker::AllowedTexGenType genType) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400526 // Values representing the various texture lock paths we can take. Used for logging the path
527 // taken to a histogram.
528 enum LockTexturePath {
529 kFailure_LockTexturePath,
530 kPreExisting_LockTexturePath,
531 kNative_LockTexturePath,
532 kCompressed_LockTexturePath, // Deprecated
533 kYUV_LockTexturePath,
534 kRGBA_LockTexturePath,
535 };
536
537 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
538
Brian Osmaneb7e5292018-08-08 14:32:06 -0400539 // Build our texture key.
540 // TODO: This needs to include the dstColorSpace.
Brian Osmandf7e0752017-04-26 16:20:28 -0400541 GrUniqueKey key;
Brian Osmaneb7e5292018-08-08 14:32:06 -0400542 this->makeCacheKeyFromOrigKey(origKey, &key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400543
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500544 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +0000545 sk_sp<GrTextureProxy> proxy;
546
Brian Osmandf7e0752017-04-26 16:20:28 -0400547 // 1. Check the cache for a pre-existing one
548 if (key.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500549 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
Greg Danielfc5060d2017-10-04 18:36:15 +0000550 if (proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400551 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
552 kLockTexturePathCount);
Greg Daniele252f082017-10-23 16:05:23 -0400553 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000554 return proxy;
555 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400556 }
557 }
558
Brian Osmaneb7e5292018-08-08 14:32:06 -0400559 // What format are we going to ask the generator to create?
560 // TODO: Based on the dstColorSpace?
Brian Osman7ed0eae2018-08-30 13:46:36 +0000561 const SkImageInfo cacheInfo = this->buildCacheInfo();
Brian Osmandf7e0752017-04-26 16:20:28 -0400562
563 // 2. Ask the generator to natively create one
Greg Danielfc5060d2017-10-04 18:36:15 +0000564 if (!proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400565 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400566 if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
567 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
568 return nullptr;
569 }
Brian Osmanc87cfb62018-07-11 09:08:46 -0400570 if ((proxy = generator->generateTexture(ctx, cacheInfo, fOrigin, willBeMipped))) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400571 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
572 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500573 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniele252f082017-10-23 16:05:23 -0400574 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000575 return proxy;
576 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400577 }
578 }
579
Greg Daniel3e70fa32017-10-05 16:27:06 -0400580 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
581 // the texture we fall through here and have the CPU generate the mip maps for us.
582 if (!proxy && !willBeMipped && !ctx->contextPriv().disableGpuYUVConversion()) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400583 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(cacheInfo);
Brian Osmandf7e0752017-04-26 16:20:28 -0400584 ScopedGenerator generator(fSharedGenerator);
585 Generator_GrYUVProvider provider(generator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700586
587 // The pixels in the texture will be in the generator's color space. If onMakeColorSpace
588 // has been called then this will not match this image's color space. To correct this, apply
589 // a color space conversion from the generator's color space to this image's color space.
Brian Osman56893cd2018-06-08 14:11:37 -0400590 // 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 -0400591 SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
592 SkColorSpace* thisColorSpace = fInfo.colorSpace();
Christopher Cameron77e96662017-07-08 01:47:47 -0700593
Brian Osman56893cd2018-06-08 14:11:37 -0400594 if ((!generatorColorSpace || generatorColorSpace->toXYZD50()) &&
595 (!thisColorSpace || thisColorSpace->toXYZD50())) {
596 // TODO: Update to create the mipped surface in the YUV generator and draw the base
597 // layer directly into the mipped surface.
598 proxy = provider.refAsTextureProxy(ctx, desc, generatorColorSpace, thisColorSpace);
599 if (proxy) {
600 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
601 kLockTexturePathCount);
602 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
603 return proxy;
604 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400605 }
606 }
607
608 // 4. Ask the generator to return RGB(A) data, which the GPU can convert
609 SkBitmap bitmap;
Brian Osmaneb7e5292018-08-08 14:32:06 -0400610 if (!proxy && this->lockAsBitmap(&bitmap, chint, cacheInfo)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400611 if (willBeMipped) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400612 proxy = proxyProvider->createMipMapProxyFromBitmap(bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400613 }
614 if (!proxy) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400615 proxy = GrUploadBitmapToTextureProxy(proxyProvider, bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400616 }
Greg Daniele252f082017-10-23 16:05:23 -0400617 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400618 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
619 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500620 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400621 return proxy;
622 }
623 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000624
625 if (proxy) {
626 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
627 // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
628 // mipped surface and copy the original proxy into the base layer. We will then let the gpu
629 // generate the rest of the mips.
630 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -0400631 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Daniele1da1d92017-10-06 15:59:27 -0400632 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500633 set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key);
Greg Danielfc5060d2017-10-04 18:36:15 +0000634 return mippedProxy;
635 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400636 // We failed to make a mipped proxy with the base copied into it. This could have
637 // been from failure to make the proxy or failure to do the copy. Thus we will fall
638 // back to just using the non mipped proxy; See skbug.com/7094.
639 return proxy;
Greg Danielfc5060d2017-10-04 18:36:15 +0000640 }
641
Brian Osmandf7e0752017-04-26 16:20:28 -0400642 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
643 kLockTexturePathCount);
644 return nullptr;
645}
646
647///////////////////////////////////////////////////////////////////////////////////////////////////
648
649#endif