1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkImage_Lazy.h"
9
10 #include "SkBitmap.h"
11 #include "SkBitmapCache.h"
12 #include "SkCachedData.h"
13 #include "SkData.h"
14 #include "SkImageGenerator.h"
15 #include "SkImagePriv.h"
16 #include "SkNextID.h"
17
18 #if SK_SUPPORT_GPU
19 #include "GrContext.h"
20 #include "GrContextPriv.h"
21 #include "GrGpuResourcePriv.h"
22 #include "GrImageTextureMaker.h"
23 #include "GrResourceKey.h"
24 #include "GrProxyProvider.h"
25 #include "GrSamplerState.h"
26 #include "GrYUVProvider.h"
27 #include "SkGr.h"
28 #endif
29
30 // Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images
31 class SharedGenerator final : public SkNVRefCnt<SharedGenerator> {
32 public:
Make(std::unique_ptr<SkImageGenerator> gen)33 static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen) {
34 return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr;
35 }
36
37 // This is thread safe. It is a const field set in the constructor.
getInfo()38 const SkImageInfo& getInfo() { return fGenerator->getInfo(); }
39
40 private:
SharedGenerator(std::unique_ptr<SkImageGenerator> gen)41 explicit SharedGenerator(std::unique_ptr<SkImageGenerator> gen)
42 : fGenerator(std::move(gen)) {
43 SkASSERT(fGenerator);
44 }
45
46 friend class ScopedGenerator;
47 friend class SkImage_Lazy;
48
49 std::unique_ptr<SkImageGenerator> fGenerator;
50 SkMutex fMutex;
51 };
52
53 ///////////////////////////////////////////////////////////////////////////////
54
Validator(sk_sp<SharedGenerator> gen,const SkIRect * subset,const SkColorType * colorType,sk_sp<SkColorSpace> colorSpace)55 SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset,
56 const SkColorType* colorType, sk_sp<SkColorSpace> colorSpace)
57 : fSharedGenerator(std::move(gen)) {
58 if (!fSharedGenerator) {
59 return;
60 }
61
62 // The following generator accessors are safe without acquiring the mutex (const getters).
63 // TODO: refactor to use a ScopedGenerator instead, for clarity.
64 const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo();
65 if (info.isEmpty()) {
66 fSharedGenerator.reset();
67 return;
68 }
69
70 fUniqueID = fSharedGenerator->fGenerator->uniqueID();
71 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
72 if (subset) {
73 if (!bounds.contains(*subset)) {
74 fSharedGenerator.reset();
75 return;
76 }
77 if (*subset != bounds) {
78 // we need a different uniqueID since we really are a subset of the raw generator
79 fUniqueID = SkNextID::ImageID();
80 }
81 } else {
82 subset = &bounds;
83 }
84
85 fInfo = info.makeWH(subset->width(), subset->height());
86 fOrigin = SkIPoint::Make(subset->x(), subset->y());
87 if (colorType || colorSpace) {
88 if (colorType) {
89 fInfo = fInfo.makeColorType(*colorType);
90 }
91 if (colorSpace) {
92 fInfo = fInfo.makeColorSpace(colorSpace);
93 }
94 fUniqueID = SkNextID::ImageID();
95 }
96 }
97
98 ///////////////////////////////////////////////////////////////////////////////
99
100 // Helper for exclusive access to a shared generator.
101 class SkImage_Lazy::ScopedGenerator {
102 public:
ScopedGenerator(const sk_sp<SharedGenerator> & gen)103 ScopedGenerator(const sk_sp<SharedGenerator>& gen)
104 : fSharedGenerator(gen)
105 , fAutoAquire(gen->fMutex) {}
106
operator ->() const107 SkImageGenerator* operator->() const {
108 fSharedGenerator->fMutex.assertHeld();
109 return fSharedGenerator->fGenerator.get();
110 }
111
operator SkImageGenerator*() const112 operator SkImageGenerator*() const {
113 fSharedGenerator->fMutex.assertHeld();
114 return fSharedGenerator->fGenerator.get();
115 }
116
117 private:
118 const sk_sp<SharedGenerator>& fSharedGenerator;
119 SkAutoExclusive fAutoAquire;
120 };
121
122 ///////////////////////////////////////////////////////////////////////////////
123
SkImage_Lazy(Validator * validator)124 SkImage_Lazy::SkImage_Lazy(Validator* validator)
125 : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID)
126 , fSharedGenerator(std::move(validator->fSharedGenerator))
127 , fInfo(validator->fInfo)
128 , fOrigin(validator->fOrigin) {
129 SkASSERT(fSharedGenerator);
130 fUniqueID = validator->fUniqueID;
131 }
132
~SkImage_Lazy()133 SkImage_Lazy::~SkImage_Lazy() {
134 #if SK_SUPPORT_GPU
135 for (int i = 0; i < fUniqueKeyInvalidatedMessages.count(); ++i) {
136 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(*fUniqueKeyInvalidatedMessages[i]);
137 }
138 fUniqueKeyInvalidatedMessages.deleteAll();
139 #endif
140 }
141
142 //////////////////////////////////////////////////////////////////////////////////////////////////
143
generate_pixels(SkImageGenerator * gen,const SkPixmap & pmap,int originX,int originY)144 static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY) {
145 const int genW = gen->getInfo().width();
146 const int genH = gen->getInfo().height();
147 const SkIRect srcR = SkIRect::MakeWH(genW, genH);
148 const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
149 if (!srcR.contains(dstR)) {
150 return false;
151 }
152
153 // If they are requesting a subset, we have to have a temp allocation for full image, and
154 // then copy the subset into their allocation
155 SkBitmap full;
156 SkPixmap fullPM;
157 const SkPixmap* dstPM = &pmap;
158 if (srcR != dstR) {
159 if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
160 return false;
161 }
162 if (!full.peekPixels(&fullPM)) {
163 return false;
164 }
165 dstPM = &fullPM;
166 }
167
168 if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes())) {
169 return false;
170 }
171
172 if (srcR != dstR) {
173 if (!full.readPixels(pmap, originX, originY)) {
174 return false;
175 }
176 }
177 return true;
178 }
179
getROPixels(SkBitmap * bitmap,SkImage::CachingHint chint) const180 bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkImage::CachingHint chint) const {
181 auto check_output_bitmap = [bitmap]() {
182 SkASSERT(bitmap->isImmutable());
183 SkASSERT(bitmap->getPixels());
184 (void)bitmap;
185 };
186
187 auto desc = SkBitmapCacheDesc::Make(this);
188 if (SkBitmapCache::Find(desc, bitmap)) {
189 check_output_bitmap();
190 return true;
191 }
192
193 if (SkImage::kAllow_CachingHint == chint) {
194 SkPixmap pmap;
195 SkBitmapCache::RecPtr cacheRec = SkBitmapCache::Alloc(desc, fInfo, &pmap);
196 if (!cacheRec ||
197 !generate_pixels(ScopedGenerator(fSharedGenerator), pmap,
198 fOrigin.x(), fOrigin.y())) {
199 return false;
200 }
201 SkBitmapCache::Add(std::move(cacheRec), bitmap);
202 this->notifyAddedToRasterCache();
203 } else {
204 if (!bitmap->tryAllocPixels(fInfo) ||
205 !generate_pixels(ScopedGenerator(fSharedGenerator), bitmap->pixmap(),
206 fOrigin.x(), fOrigin.y())) {
207 return false;
208 }
209 bitmap->setImmutable();
210 }
211
212 check_output_bitmap();
213 return true;
214 }
215
216 //////////////////////////////////////////////////////////////////////////////////////////////////
217
onReadPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,int srcX,int srcY,CachingHint chint) const218 bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
219 int srcX, int srcY, CachingHint chint) const {
220 SkBitmap bm;
221 if (this->getROPixels(&bm, chint)) {
222 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
223 }
224 return false;
225 }
226
onRefEncoded() const227 sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
228 ScopedGenerator generator(fSharedGenerator);
229 return generator->refEncodedData();
230 }
231
onIsValid(GrContext * context) const232 bool SkImage_Lazy::onIsValid(GrContext* context) const {
233 ScopedGenerator generator(fSharedGenerator);
234 return generator->isValid(context);
235 }
236
237 ///////////////////////////////////////////////////////////////////////////////////////////////////
238
239 #if SK_SUPPORT_GPU
asTextureProxyRef(GrContext * context,const GrSamplerState & params,SkScalar scaleAdjust[2]) const240 sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrContext* context,
241 const GrSamplerState& params,
242 SkScalar scaleAdjust[2]) const {
243 if (!context) {
244 return nullptr;
245 }
246
247 GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
248 return textureMaker.refTextureProxyForParams(params, scaleAdjust);
249 }
250 #endif
251
onMakeSubset(const SkIRect & subset) const252 sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset) const {
253 SkASSERT(fInfo.bounds().contains(subset));
254 SkASSERT(fInfo.bounds() != subset);
255
256 const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y());
257 const SkColorType colorType = fInfo.colorType();
258 Validator validator(fSharedGenerator, &generatorSubset, &colorType, fInfo.refColorSpace());
259 return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
260 }
261
onMakeColorTypeAndColorSpace(SkColorType targetCT,sk_sp<SkColorSpace> targetCS) const262 sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(SkColorType targetCT,
263 sk_sp<SkColorSpace> targetCS) const {
264 SkAutoExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
265 if (fOnMakeColorTypeAndSpaceResult &&
266 targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
267 SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
268 return fOnMakeColorTypeAndSpaceResult;
269 }
270 const SkIRect generatorSubset =
271 SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
272 Validator validator(fSharedGenerator, &generatorSubset, &targetCT, targetCS);
273 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
274 if (result) {
275 fOnMakeColorTypeAndSpaceResult = result;
276 }
277 return result;
278 }
279
MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,const SkIRect * subset)280 sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
281 const SkIRect* subset) {
282 SkImage_Lazy::Validator
283 validator(SharedGenerator::Make(std::move(generator)), subset, nullptr, nullptr);
284
285 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
286 }
287
288 //////////////////////////////////////////////////////////////////////////////////////////////////
289
290 #if SK_SUPPORT_GPU
291
makeCacheKeyFromOrigKey(const GrUniqueKey & origKey,GrUniqueKey * cacheKey) const292 void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey,
293 GrUniqueKey* cacheKey) const {
294 SkASSERT(!cacheKey->isValid());
295 if (origKey.isValid()) {
296 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
297 GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image");
298 }
299 }
300
301 class Generator_GrYUVProvider : public GrYUVProvider {
302 public:
Generator_GrYUVProvider(SkImageGenerator * gen)303 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
304
305 private:
onGetID() const306 uint32_t onGetID() const override { return fGen->uniqueID(); }
onQueryYUVA8(SkYUVASizeInfo * sizeInfo,SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],SkYUVColorSpace * colorSpace) const307 bool onQueryYUVA8(SkYUVASizeInfo* sizeInfo,
308 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
309 SkYUVColorSpace* colorSpace) const override {
310 return fGen->queryYUVA8(sizeInfo, yuvaIndices, colorSpace);
311 }
onGetYUVA8Planes(const SkYUVASizeInfo & sizeInfo,const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],void * planes[])312 bool onGetYUVA8Planes(const SkYUVASizeInfo& sizeInfo,
313 const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
314 void* planes[]) override {
315 return fGen->getYUVA8Planes(sizeInfo, yuvaIndices, planes);
316 }
317
318 SkImageGenerator* fGen;
319
320 typedef GrYUVProvider INHERITED;
321 };
322
set_key_on_proxy(GrProxyProvider * proxyProvider,GrTextureProxy * proxy,GrTextureProxy * originalProxy,const GrUniqueKey & key)323 static void set_key_on_proxy(GrProxyProvider* proxyProvider,
324 GrTextureProxy* proxy, GrTextureProxy* originalProxy,
325 const GrUniqueKey& key) {
326 if (key.isValid()) {
327 if (originalProxy && originalProxy->getUniqueKey().isValid()) {
328 SkASSERT(originalProxy->getUniqueKey() == key);
329 SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
330 GrMipMapped::kNo == originalProxy->mipMapped());
331 // If we had an originalProxy with a valid key, that means there already is a proxy in
332 // the cache which matches the key, but it does not have mip levels and we require them.
333 // Thus we must remove the unique key from that proxy.
334 SkASSERT(originalProxy->getUniqueKey() == key);
335 proxyProvider->removeUniqueKeyFromProxy(originalProxy);
336 }
337 proxyProvider->assignUniqueKeyToProxy(key, proxy);
338 }
339 }
340
getPlanes(SkYUVASizeInfo * yuvaSizeInfo,SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],SkYUVColorSpace * yuvColorSpace,const void * planes[SkYUVASizeInfo::kMaxCount])341 sk_sp<SkCachedData> SkImage_Lazy::getPlanes(SkYUVASizeInfo* yuvaSizeInfo,
342 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
343 SkYUVColorSpace* yuvColorSpace,
344 const void* planes[SkYUVASizeInfo::kMaxCount]) {
345 ScopedGenerator generator(fSharedGenerator);
346 Generator_GrYUVProvider provider(generator);
347
348 sk_sp<SkCachedData> data = provider.getPlanes(yuvaSizeInfo, yuvaIndices, yuvColorSpace, planes);
349 if (!data) {
350 return nullptr;
351 }
352
353 return data;
354 }
355
356
357 /*
358 * We have 4 ways to try to return a texture (in sorted order)
359 *
360 * 1. Check the cache for a pre-existing one
361 * 2. Ask the generator to natively create one
362 * 3. Ask the generator to return YUV planes, which the GPU can convert
363 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
364 */
lockTextureProxy(GrContext * ctx,const GrUniqueKey & origKey,SkImage::CachingHint chint,bool willBeMipped,GrTextureMaker::AllowedTexGenType genType) const365 sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(
366 GrContext* ctx,
367 const GrUniqueKey& origKey,
368 SkImage::CachingHint chint,
369 bool willBeMipped,
370 GrTextureMaker::AllowedTexGenType genType) const {
371 // Values representing the various texture lock paths we can take. Used for logging the path
372 // taken to a histogram.
373 enum LockTexturePath {
374 kFailure_LockTexturePath,
375 kPreExisting_LockTexturePath,
376 kNative_LockTexturePath,
377 kCompressed_LockTexturePath, // Deprecated
378 kYUV_LockTexturePath,
379 kRGBA_LockTexturePath,
380 };
381
382 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
383
384 // Build our texture key.
385 // Even though some proxies created here may have a specific origin and use that origin, we do
386 // not include that in the key. Since SkImages are meant to be immutable, a given SkImage will
387 // always have an associated proxy that is always one origin or the other. It never can change
388 // origins. Thus we don't need to include that info in the key iteself.
389 GrUniqueKey key;
390 this->makeCacheKeyFromOrigKey(origKey, &key);
391
392 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
393 sk_sp<GrTextureProxy> proxy;
394
395 // 1. Check the cache for a pre-existing one
396 if (key.isValid()) {
397 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
398 if (proxy) {
399 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
400 kLockTexturePathCount);
401 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
402 return proxy;
403 }
404 }
405 }
406
407 // 2. Ask the generator to natively create one
408 if (!proxy) {
409 ScopedGenerator generator(fSharedGenerator);
410 if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
411 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
412 return nullptr;
413 }
414 if ((proxy = generator->generateTexture(ctx, fInfo, fOrigin, willBeMipped))) {
415 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
416 kLockTexturePathCount);
417 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
418 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
419 *fUniqueKeyInvalidatedMessages.append() =
420 new GrUniqueKeyInvalidatedMessage(key, ctx->contextPriv().contextID());
421 return proxy;
422 }
423 }
424 }
425
426 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
427 // the texture we fall through here and have the CPU generate the mip maps for us.
428 if (!proxy && !willBeMipped && !ctx->contextPriv().disableGpuYUVConversion()) {
429 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo);
430
431 SkColorType colorType = fInfo.colorType();
432 GrBackendFormat format =
433 ctx->contextPriv().caps()->getBackendFormatFromColorType(colorType);
434
435 ScopedGenerator generator(fSharedGenerator);
436 Generator_GrYUVProvider provider(generator);
437
438 // The pixels in the texture will be in the generator's color space.
439 // If onMakeColorTypeAndColorSpace has been called then this will not match this image's
440 // color space. To correct this, apply a color space conversion from the generator's color
441 // space to this image's color space.
442 SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
443 SkColorSpace* thisColorSpace = fInfo.colorSpace();
444
445 // TODO: Update to create the mipped surface in the YUV generator and draw the base
446 // layer directly into the mipped surface.
447 proxy = provider.refAsTextureProxy(ctx, format, desc, generatorColorSpace, thisColorSpace);
448 if (proxy) {
449 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
450 kLockTexturePathCount);
451 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
452 *fUniqueKeyInvalidatedMessages.append() =
453 new GrUniqueKeyInvalidatedMessage(key, ctx->contextPriv().contextID());
454 return proxy;
455 }
456 }
457
458 // 4. Ask the generator to return RGB(A) data, which the GPU can convert
459 SkBitmap bitmap;
460 if (!proxy && this->getROPixels(&bitmap, chint)) {
461 if (willBeMipped) {
462 proxy = proxyProvider->createMipMapProxyFromBitmap(bitmap);
463 }
464 if (!proxy) {
465 proxy = GrUploadBitmapToTextureProxy(proxyProvider, bitmap);
466 }
467 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
468 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
469 kLockTexturePathCount);
470 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
471 *fUniqueKeyInvalidatedMessages.append() =
472 new GrUniqueKeyInvalidatedMessage(key, ctx->contextPriv().contextID());
473 return proxy;
474 }
475 }
476
477 if (proxy) {
478 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
479 // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
480 // mipped surface and copy the original proxy into the base layer. We will then let the gpu
481 // generate the rest of the mips.
482 SkASSERT(willBeMipped);
483 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
484 *fUniqueKeyInvalidatedMessages.append() =
485 new GrUniqueKeyInvalidatedMessage(key, ctx->contextPriv().contextID());
486 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
487 set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key);
488 return mippedProxy;
489 }
490 // We failed to make a mipped proxy with the base copied into it. This could have
491 // been from failure to make the proxy or failure to do the copy. Thus we will fall
492 // back to just using the non mipped proxy; See skbug.com/7094.
493 return proxy;
494 }
495
496 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
497 kLockTexturePathCount);
498 return nullptr;
499 }
500
501 ///////////////////////////////////////////////////////////////////////////////////////////////////
502
503 #endif
504