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 "src/image/SkImage_Lazy.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkImageGenerator.h"
15 #include "include/core/SkPixmap.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkYUVAInfo.h"
19 #include "src/core/SkBitmapCache.h"
20 #include "src/core/SkCachedData.h"
21 #include "src/core/SkNextID.h"
22 #include "src/core/SkResourceCache.h"
23 #include "src/core/SkYUVPlanesCache.h"
24
25 #if defined(SK_GANESH)
26 #include "include/gpu/GpuTypes.h"
27 #include "include/gpu/GrBackendSurface.h"
28 #include "include/gpu/GrContextOptions.h"
29 #include "include/gpu/GrDirectContext.h"
30 #include "include/gpu/GrRecordingContext.h"
31 #include "include/gpu/GrTypes.h"
32 #include "include/private/gpu/ganesh/GrTypesPriv.h"
33 #include "src/gpu/ResourceKey.h"
34 #include "src/gpu/SkBackingFit.h"
35 #include "src/gpu/Swizzle.h"
36 #include "src/gpu/ganesh/GrCaps.h"
37 #include "src/gpu/ganesh/GrColorInfo.h"
38 #include "src/gpu/ganesh/GrColorSpaceXform.h"
39 #include "src/gpu/ganesh/GrDirectContextPriv.h"
40 #include "src/gpu/ganesh/GrFragmentProcessor.h"
41 #include "src/gpu/ganesh/GrImageInfo.h"
42 #include "src/gpu/ganesh/GrProxyProvider.h"
43 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
44 #include "src/gpu/ganesh/GrSamplerState.h"
45 #include "src/gpu/ganesh/GrSurfaceProxy.h"
46 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
47 #include "src/gpu/ganesh/GrTextureProxy.h"
48 #include "src/gpu/ganesh/GrYUVATextureProxies.h"
49 #include "src/gpu/ganesh/SkGr.h"
50 #include "src/gpu/ganesh/SurfaceContext.h"
51 #include "src/gpu/ganesh/SurfaceFillContext.h"
52 #include "src/gpu/ganesh/effects/GrYUVtoRGBEffect.h"
53 #endif
54
55 #if defined(SK_GRAPHITE)
56 #include "src/gpu/graphite/TextureUtils.h"
57 #endif
58
59 #include <utility>
60
61 class SkMatrix;
62 enum SkColorType : int;
63 enum class SkTileMode;
64
65 // Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images
66 class SharedGenerator final : public SkNVRefCnt<SharedGenerator> {
67 public:
Make(std::unique_ptr<SkImageGenerator> gen)68 static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen) {
69 return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr;
70 }
71
72 // This is thread safe. It is a const field set in the constructor.
getInfo()73 const SkImageInfo& getInfo() { return fGenerator->getInfo(); }
74
75 private:
SharedGenerator(std::unique_ptr<SkImageGenerator> gen)76 explicit SharedGenerator(std::unique_ptr<SkImageGenerator> gen)
77 : fGenerator(std::move(gen)) {
78 SkASSERT(fGenerator);
79 }
80
81 friend class ScopedGenerator;
82 friend class SkImage_Lazy;
83
84 std::unique_ptr<SkImageGenerator> fGenerator;
85 SkMutex fMutex;
86 };
87
88 ///////////////////////////////////////////////////////////////////////////////
89
Validator(sk_sp<SharedGenerator> gen,const SkColorType * colorType,sk_sp<SkColorSpace> colorSpace)90 SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkColorType* colorType,
91 sk_sp<SkColorSpace> colorSpace)
92 : fSharedGenerator(std::move(gen)) {
93 if (!fSharedGenerator) {
94 return;
95 }
96
97 // The following generator accessors are safe without acquiring the mutex (const getters).
98 // TODO: refactor to use a ScopedGenerator instead, for clarity.
99 fInfo = fSharedGenerator->fGenerator->getInfo();
100 if (fInfo.isEmpty()) {
101 fSharedGenerator.reset();
102 return;
103 }
104
105 fUniqueID = fSharedGenerator->fGenerator->uniqueID();
106
107 if (colorType && (*colorType == fInfo.colorType())) {
108 colorType = nullptr;
109 }
110
111 if (colorType || colorSpace) {
112 if (colorType) {
113 fInfo = fInfo.makeColorType(*colorType);
114 }
115 if (colorSpace) {
116 fInfo = fInfo.makeColorSpace(colorSpace);
117 }
118 fUniqueID = SkNextID::ImageID();
119 }
120 }
121
122 ///////////////////////////////////////////////////////////////////////////////
123
124 // Helper for exclusive access to a shared generator.
125 class SkImage_Lazy::ScopedGenerator {
126 public:
ScopedGenerator(const sk_sp<SharedGenerator> & gen)127 ScopedGenerator(const sk_sp<SharedGenerator>& gen)
128 : fSharedGenerator(gen)
129 , fAutoAquire(gen->fMutex) {}
130
operator ->() const131 SkImageGenerator* operator->() const {
132 fSharedGenerator->fMutex.assertHeld();
133 return fSharedGenerator->fGenerator.get();
134 }
135
operator SkImageGenerator*() const136 operator SkImageGenerator*() const {
137 fSharedGenerator->fMutex.assertHeld();
138 return fSharedGenerator->fGenerator.get();
139 }
140
141 private:
142 const sk_sp<SharedGenerator>& fSharedGenerator;
143 SkAutoMutexExclusive fAutoAquire;
144 };
145
146 ///////////////////////////////////////////////////////////////////////////////
147
SkImage_Lazy(Validator * validator)148 SkImage_Lazy::SkImage_Lazy(Validator* validator)
149 : SkImage_Base(validator->fInfo, validator->fUniqueID)
150 , fSharedGenerator(std::move(validator->fSharedGenerator))
151 {
152 SkASSERT(fSharedGenerator);
153 }
154
155
156 //////////////////////////////////////////////////////////////////////////////////////////////////
157
getROPixels(GrDirectContext * ctx,SkBitmap * bitmap,SkImage::CachingHint chint) const158 bool SkImage_Lazy::getROPixels(GrDirectContext* ctx, SkBitmap* bitmap,
159 SkImage::CachingHint chint) const {
160 auto check_output_bitmap = [bitmap]() {
161 SkASSERT(bitmap->isImmutable());
162 SkASSERT(bitmap->getPixels());
163 (void)bitmap;
164 };
165
166 auto desc = SkBitmapCacheDesc::Make(this);
167 if (SkBitmapCache::Find(desc, bitmap)) {
168 check_output_bitmap();
169 return true;
170 }
171
172 if (SkImage::kAllow_CachingHint == chint) {
173 SkPixmap pmap;
174 SkBitmapCache::RecPtr cacheRec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
175 if (!cacheRec) {
176 return false;
177 }
178 bool success = false;
179 { // make sure ScopedGenerator goes out of scope before we try readPixelsProxy
180 success = ScopedGenerator(fSharedGenerator)->getPixels(pmap);
181 }
182 if (!success && !this->readPixelsProxy(ctx, pmap)) {
183 return false;
184 }
185 SkBitmapCache::Add(std::move(cacheRec), bitmap);
186 this->notifyAddedToRasterCache();
187 } else {
188 if (!bitmap->tryAllocPixels(this->imageInfo())) {
189 return false;
190 }
191 bool success = false;
192 { // make sure ScopedGenerator goes out of scope before we try readPixelsProxy
193 success = ScopedGenerator(fSharedGenerator)->getPixels(bitmap->pixmap());
194 }
195 if (!success && !this->readPixelsProxy(ctx, bitmap->pixmap())) {
196 return false;
197 }
198 bitmap->setImmutable();
199 }
200 check_output_bitmap();
201 return true;
202 }
203
readPixelsProxy(GrDirectContext * ctx,const SkPixmap & pixmap) const204 bool SkImage_Lazy::readPixelsProxy(GrDirectContext* ctx, const SkPixmap& pixmap) const {
205 #if defined(SK_GANESH)
206 if (!ctx) {
207 return false;
208 }
209 GrSurfaceProxyView view = this->lockTextureProxyView(ctx,
210 GrImageTexGenPolicy::kDraw,
211 GrMipmapped::kNo);
212
213 if (!view) {
214 return false;
215 }
216
217 GrColorType ct = this->colorTypeOfLockTextureProxy(ctx->priv().caps());
218 GrColorInfo colorInfo(ct, this->alphaType(), this->refColorSpace());
219 auto sContext = ctx->priv().makeSC(std::move(view), colorInfo);
220 if (!sContext) {
221 return false;
222 }
223 size_t rowBytes = this->imageInfo().minRowBytes();
224 return sContext->readPixels(ctx, {this->imageInfo(), pixmap.writable_addr(), rowBytes}, {0, 0});
225 #else
226 return false;
227 #endif // defined(SK_GANESH)
228 }
229
230 //////////////////////////////////////////////////////////////////////////////////////////////////
231
onReadPixels(GrDirectContext * dContext,const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,int srcX,int srcY,CachingHint chint) const232 bool SkImage_Lazy::onReadPixels(GrDirectContext* dContext,
233 const SkImageInfo& dstInfo,
234 void* dstPixels,
235 size_t dstRB,
236 int srcX,
237 int srcY,
238 CachingHint chint) const {
239 SkBitmap bm;
240 if (this->getROPixels(dContext, &bm, chint)) {
241 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
242 }
243 return false;
244 }
245
onRefEncoded() const246 sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
247 // check that we aren't a subset or colortype/etc modification of the original
248 if (fSharedGenerator->fGenerator->uniqueID() == this->uniqueID()) {
249 ScopedGenerator generator(fSharedGenerator);
250 return generator->refEncodedData();
251 }
252 return nullptr;
253 }
254
onIsValid(GrRecordingContext * context) const255 bool SkImage_Lazy::onIsValid(GrRecordingContext* context) const {
256 ScopedGenerator generator(fSharedGenerator);
257 return generator->isValid(context);
258 }
259
260 ///////////////////////////////////////////////////////////////////////////////////////////////////
261
onMakeSubset(const SkIRect & subset,GrDirectContext * direct) const262 sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset, GrDirectContext* direct) const {
263 // TODO: can we do this more efficiently, by telling the generator we want to
264 // "realize" a subset?
265
266 #if defined(SK_GANESH)
267 auto pixels = direct ? this->makeTextureImage(direct)
268 : this->makeRasterImage();
269 #else
270 auto pixels = this->makeRasterImage();
271 #endif
272 return pixels ? pixels->makeSubset(subset, direct) : nullptr;
273 }
274
275 #if defined(SK_GRAPHITE)
276
onMakeSubset(const SkIRect & subset,skgpu::graphite::Recorder * recorder,RequiredImageProperties requiredProperties) const277 sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset,
278 skgpu::graphite::Recorder* recorder,
279 RequiredImageProperties requiredProperties) const {
280 // TODO: can we do this more efficiently, by telling the generator we want to
281 // "realize" a subset?
282
283 sk_sp<SkImage> nonLazyImg = recorder ? this->makeTextureImage(recorder, requiredProperties)
284 : this->makeRasterImage();
285
286 return nonLazyImg ? nonLazyImg->makeSubset(subset, recorder, requiredProperties) : nullptr;
287 }
288
289 #endif // SK_GRAPHITE
290
onMakeColorTypeAndColorSpace(SkColorType targetCT,sk_sp<SkColorSpace> targetCS,GrDirectContext *) const291 sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(SkColorType targetCT,
292 sk_sp<SkColorSpace> targetCS,
293 GrDirectContext*) const {
294 SkAutoMutexExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
295 if (fOnMakeColorTypeAndSpaceResult &&
296 targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
297 SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
298 return fOnMakeColorTypeAndSpaceResult;
299 }
300 Validator validator(fSharedGenerator, &targetCT, targetCS);
301 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
302 if (result) {
303 fOnMakeColorTypeAndSpaceResult = result;
304 }
305 return result;
306 }
307
onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const308 sk_sp<SkImage> SkImage_Lazy::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
309 // TODO: The correct thing is to clone the generator, and modify its color space. That's hard,
310 // because we don't have a clone method, and generator is public (and derived-from by clients).
311 // So do the simple/inefficient thing here, and fallback to raster when this is called.
312
313 // We allocate the bitmap with the new color space, then generate the image using the original.
314 SkBitmap bitmap;
315 if (bitmap.tryAllocPixels(this->imageInfo().makeColorSpace(std::move(newCS)))) {
316 SkPixmap pixmap = bitmap.pixmap();
317 pixmap.setColorSpace(this->refColorSpace());
318 if (ScopedGenerator(fSharedGenerator)->getPixels(pixmap)) {
319 bitmap.setImmutable();
320 return bitmap.asImage();
321 }
322 }
323 return nullptr;
324 }
325
MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator)326 sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator) {
327 SkImage_Lazy::Validator
328 validator(SharedGenerator::Make(std::move(generator)), nullptr, nullptr);
329
330 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
331 }
332
333 #if defined(SK_GANESH)
334
onAsView(GrRecordingContext * context,GrMipmapped mipmapped,GrImageTexGenPolicy policy) const335 std::tuple<GrSurfaceProxyView, GrColorType> SkImage_Lazy::onAsView(
336 GrRecordingContext* context,
337 GrMipmapped mipmapped,
338 GrImageTexGenPolicy policy) const {
339 GrColorType ct = this->colorTypeOfLockTextureProxy(context->priv().caps());
340 return {this->lockTextureProxyView(context, policy, mipmapped), ct};
341 }
342
onAsFragmentProcessor(GrRecordingContext * rContext,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain) const343 std::unique_ptr<GrFragmentProcessor> SkImage_Lazy::onAsFragmentProcessor(
344 GrRecordingContext* rContext,
345 SkSamplingOptions sampling,
346 const SkTileMode tileModes[2],
347 const SkMatrix& m,
348 const SkRect* subset,
349 const SkRect* domain) const {
350 // TODO: If the CPU data is extracted as planes return a FP that reconstructs the image from
351 // the planes.
352 auto mm = sampling.mipmap == SkMipmapMode::kNone ? GrMipmapped::kNo : GrMipmapped::kYes;
353 return MakeFragmentProcessorFromView(rContext,
354 std::get<0>(this->asView(rContext, mm)),
355 this->alphaType(),
356 sampling,
357 tileModes,
358 m,
359 subset,
360 domain);
361 }
362
textureProxyViewFromPlanes(GrRecordingContext * ctx,skgpu::Budgeted budgeted) const363 GrSurfaceProxyView SkImage_Lazy::textureProxyViewFromPlanes(GrRecordingContext* ctx,
364 skgpu::Budgeted budgeted) const {
365 SkYUVAPixmapInfo::SupportedDataTypes supportedDataTypes(*ctx);
366 SkYUVAPixmaps yuvaPixmaps;
367 sk_sp<SkCachedData> dataStorage = this->getPlanes(supportedDataTypes, &yuvaPixmaps);
368 if (!dataStorage) {
369 return {};
370 }
371
372 GrSurfaceProxyView views[SkYUVAInfo::kMaxPlanes];
373 GrColorType pixmapColorTypes[SkYUVAInfo::kMaxPlanes];
374 for (int i = 0; i < yuvaPixmaps.numPlanes(); ++i) {
375 // If the sizes of the components are not all the same we choose to create exact-match
376 // textures for the smaller ones rather than add a texture domain to the draw.
377 // TODO: revisit this decision to improve texture reuse?
378 SkBackingFit fit = yuvaPixmaps.plane(i).dimensions() == this->dimensions()
379 ? SkBackingFit::kApprox
380 : SkBackingFit::kExact;
381
382 // We grab a ref to cached yuv data. When the SkBitmap we create below goes away it will
383 // call releaseProc which will release this ref.
384 // DDL TODO: Currently we end up creating a lazy proxy that will hold onto a ref to the
385 // SkImage in its lambda. This means that we'll keep the ref on the YUV data around for the
386 // life time of the proxy and not just upload. For non-DDL draws we should look into
387 // releasing this SkImage after uploads (by deleting the lambda after instantiation).
388 auto releaseProc = [](void*, void* data) {
389 auto cachedData = static_cast<SkCachedData*>(data);
390 SkASSERT(cachedData);
391 cachedData->unref();
392 };
393 SkBitmap bitmap;
394 bitmap.installPixels(yuvaPixmaps.plane(i).info(),
395 yuvaPixmaps.plane(i).writable_addr(),
396 yuvaPixmaps.plane(i).rowBytes(),
397 releaseProc,
398 SkRef(dataStorage.get()));
399 bitmap.setImmutable();
400
401 std::tie(views[i], std::ignore) = GrMakeUncachedBitmapProxyView(ctx,
402 bitmap,
403 GrMipmapped::kNo,
404 fit);
405 if (!views[i]) {
406 return {};
407 }
408 pixmapColorTypes[i] = SkColorTypeToGrColorType(bitmap.colorType());
409 }
410
411 // TODO: investigate preallocating mip maps here
412 GrImageInfo info(SkColorTypeToGrColorType(this->colorType()),
413 kPremul_SkAlphaType,
414 /*color space*/ nullptr,
415 this->dimensions());
416
417 auto sfc = ctx->priv().makeSFC(info,
418 "ImageLazy_TextureProxyViewFromPlanes",
419 SkBackingFit::kExact,
420 1,
421 GrMipmapped::kNo,
422 GrProtected::kNo,
423 kTopLeft_GrSurfaceOrigin,
424 budgeted);
425 if (!sfc) {
426 return {};
427 }
428
429 GrYUVATextureProxies yuvaProxies(yuvaPixmaps.yuvaInfo(), views, pixmapColorTypes);
430 SkAssertResult(yuvaProxies.isValid());
431
432 std::unique_ptr<GrFragmentProcessor> fp = GrYUVtoRGBEffect::Make(
433 yuvaProxies,
434 GrSamplerState::Filter::kNearest,
435 *ctx->priv().caps());
436
437 // The pixels after yuv->rgb will be in the generator's color space.
438 // If onMakeColorTypeAndColorSpace has been called then this will not match this image's
439 // color space. To correct this, apply a color space conversion from the generator's color
440 // space to this image's color space.
441 SkColorSpace* srcColorSpace;
442 {
443 ScopedGenerator generator(fSharedGenerator);
444 srcColorSpace = generator->getInfo().colorSpace();
445 }
446 SkColorSpace* dstColorSpace = this->colorSpace();
447
448 // If the caller expects the pixels in a different color space than the one from the image,
449 // apply a color conversion to do this.
450 fp = GrColorSpaceXformEffect::Make(std::move(fp),
451 srcColorSpace, kOpaque_SkAlphaType,
452 dstColorSpace, kOpaque_SkAlphaType);
453 sfc->fillWithFP(std::move(fp));
454
455 return sfc->readSurfaceView();
456 }
457
getPlanes(const SkYUVAPixmapInfo::SupportedDataTypes & supportedDataTypes,SkYUVAPixmaps * yuvaPixmaps) const458 sk_sp<SkCachedData> SkImage_Lazy::getPlanes(
459 const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
460 SkYUVAPixmaps* yuvaPixmaps) const {
461 ScopedGenerator generator(fSharedGenerator);
462
463 sk_sp<SkCachedData> data(SkYUVPlanesCache::FindAndRef(generator->uniqueID(), yuvaPixmaps));
464
465 if (data) {
466 SkASSERT(yuvaPixmaps->isValid());
467 SkASSERT(yuvaPixmaps->yuvaInfo().dimensions() == this->dimensions());
468 return data;
469 }
470 SkYUVAPixmapInfo yuvaPixmapInfo;
471 if (!generator->queryYUVAInfo(supportedDataTypes, &yuvaPixmapInfo) ||
472 yuvaPixmapInfo.yuvaInfo().dimensions() != this->dimensions()) {
473 return nullptr;
474 }
475 data.reset(SkResourceCache::NewCachedData(yuvaPixmapInfo.computeTotalBytes()));
476 SkYUVAPixmaps tempPixmaps = SkYUVAPixmaps::FromExternalMemory(yuvaPixmapInfo,
477 data->writable_data());
478 SkASSERT(tempPixmaps.isValid());
479 if (!generator->getYUVAPlanes(tempPixmaps)) {
480 return nullptr;
481 }
482 // Decoding is done, cache the resulting YUV planes
483 *yuvaPixmaps = tempPixmaps;
484 SkYUVPlanesCache::Add(this->uniqueID(), data.get(), *yuvaPixmaps);
485 return data;
486 }
487
488 /*
489 * We have 4 ways to try to return a texture (in sorted order)
490 *
491 * 1. Check the cache for a pre-existing one
492 * 2. Ask the generator to natively create one
493 * 3. Ask the generator to return YUV planes, which the GPU can convert
494 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
495 */
lockTextureProxyView(GrRecordingContext * rContext,GrImageTexGenPolicy texGenPolicy,GrMipmapped mipmapped) const496 GrSurfaceProxyView SkImage_Lazy::lockTextureProxyView(GrRecordingContext* rContext,
497 GrImageTexGenPolicy texGenPolicy,
498 GrMipmapped mipmapped) const {
499 // Values representing the various texture lock paths we can take. Used for logging the path
500 // taken to a histogram.
501 enum LockTexturePath {
502 kFailure_LockTexturePath,
503 kPreExisting_LockTexturePath,
504 kNative_LockTexturePath,
505 kCompressed_LockTexturePath, // Deprecated
506 kYUV_LockTexturePath,
507 kRGBA_LockTexturePath,
508 };
509
510 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
511
512 skgpu::UniqueKey key;
513 if (texGenPolicy == GrImageTexGenPolicy::kDraw) {
514 GrMakeKeyFromImageID(&key, this->uniqueID(), SkIRect::MakeSize(this->dimensions()));
515 }
516
517 const GrCaps* caps = rContext->priv().caps();
518 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
519
520 auto installKey = [&](const GrSurfaceProxyView& view) {
521 SkASSERT(view && view.asTextureProxy());
522 if (key.isValid()) {
523 auto listener = GrMakeUniqueKeyInvalidationListener(&key, rContext->priv().contextID());
524 this->addUniqueIDListener(std::move(listener));
525 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
526 }
527 };
528
529 auto ct = this->colorTypeOfLockTextureProxy(caps);
530
531 // 1. Check the cache for a pre-existing one.
532 if (key.isValid()) {
533 auto proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
534 if (proxy) {
535 skgpu::Swizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct);
536 GrSurfaceOrigin origin = ScopedGenerator(fSharedGenerator)->origin();
537 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
538 if (mipmapped == GrMipmapped::kNo ||
539 view.asTextureProxy()->mipmapped() == GrMipmapped::kYes) {
540 return view;
541 } else {
542 // We need a mipped proxy, but we found a cached proxy that wasn't mipped. Thus we
543 // generate a new mipped surface and copy the original proxy into the base layer. We
544 // will then let the gpu generate the rest of the mips.
545 auto mippedView = GrCopyBaseMipMapToView(rContext, view);
546 if (!mippedView) {
547 // We failed to make a mipped proxy with the base copied into it. This could
548 // have been from failure to make the proxy or failure to do the copy. Thus we
549 // will fall back to just using the non mipped proxy; See skbug.com/7094.
550 return view;
551 }
552 proxyProvider->removeUniqueKeyFromProxy(view.asTextureProxy());
553 installKey(mippedView);
554 return mippedView;
555 }
556 }
557 }
558
559 // 2. Ask the generator to natively create one.
560 {
561 ScopedGenerator generator(fSharedGenerator);
562 if (auto view = generator->generateTexture(rContext,
563 this->imageInfo(),
564 mipmapped,
565 texGenPolicy)) {
566 installKey(view);
567 return view;
568 }
569 }
570
571 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
572 // the texture we skip this step so the CPU generate non-planar MIP maps for us.
573 if (mipmapped == GrMipmapped::kNo && !rContext->priv().options().fDisableGpuYUVConversion) {
574 // TODO: Update to create the mipped surface in the textureProxyViewFromPlanes generator and
575 // draw the base layer directly into the mipped surface.
576 skgpu::Budgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
577 ? skgpu::Budgeted::kNo
578 : skgpu::Budgeted::kYes;
579 auto view = this->textureProxyViewFromPlanes(rContext, budgeted);
580 if (view) {
581 installKey(view);
582 return view;
583 }
584 }
585
586 // 4. Ask the generator to return a bitmap, which the GPU can convert.
587 auto hint = texGenPolicy == GrImageTexGenPolicy::kDraw ? CachingHint::kAllow_CachingHint
588 : CachingHint::kDisallow_CachingHint;
589 if (SkBitmap bitmap; this->getROPixels(nullptr, &bitmap, hint)) {
590 // We always make an uncached bitmap here because we will cache it based on passed in policy
591 // with *our* key, not a key derived from bitmap. We're just making the proxy here.
592 auto budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
593 ? skgpu::Budgeted::kNo
594 : skgpu::Budgeted::kYes;
595 auto view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext,
596 bitmap,
597 mipmapped,
598 SkBackingFit::kExact,
599 budgeted));
600 if (view) {
601 installKey(view);
602 return view;
603 }
604 }
605
606 return {};
607 }
608
colorTypeOfLockTextureProxy(const GrCaps * caps) const609 GrColorType SkImage_Lazy::colorTypeOfLockTextureProxy(const GrCaps* caps) const {
610 GrColorType ct = SkColorTypeToGrColorType(this->colorType());
611 GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kNo);
612 if (!format.isValid()) {
613 ct = GrColorType::kRGBA_8888;
614 }
615 return ct;
616 }
617
addUniqueIDListener(sk_sp<SkIDChangeListener> listener) const618 void SkImage_Lazy::addUniqueIDListener(sk_sp<SkIDChangeListener> listener) const {
619 fUniqueIDListeners.add(std::move(listener));
620 }
621 #endif // defined(SK_GANESH)
622
623 #if defined(SK_GRAPHITE)
624
625 /*
626 * We only have 2 ways to create a Graphite-backed image.
627 *
628 * 1. Ask the generator to natively create one
629 * 2. Ask the generator to return RGB(A) data, which the GPU can convert
630 */
onMakeTextureImage(skgpu::graphite::Recorder * recorder,RequiredImageProperties requiredProps) const631 sk_sp<SkImage> SkImage_Lazy::onMakeTextureImage(skgpu::graphite::Recorder* recorder,
632 RequiredImageProperties requiredProps) const {
633 using namespace skgpu::graphite;
634
635 // 1. Ask the generator to natively create one.
636 {
637 // Disable mipmaps here bc Graphite doesn't currently support mipmap regeneration
638 // In this case, we would allocate the mipmaps and fill in the base layer but the mipmap
639 // levels would never be filled out - yielding incorrect draws. Please see: b/238754357.
640 requiredProps.fMipmapped = skgpu::Mipmapped::kNo;
641
642 ScopedGenerator generator(fSharedGenerator);
643 sk_sp<SkImage> newImage = generator->makeTextureImage(recorder,
644 this->imageInfo(),
645 requiredProps.fMipmapped);
646 if (newImage) {
647 SkASSERT(as_IB(newImage)->isGraphiteBacked());
648 return newImage;
649 }
650 }
651
652 // 2. Ask the generator to return a bitmap, which the GPU can convert.
653 if (SkBitmap bitmap; this->getROPixels(nullptr, &bitmap, CachingHint::kDisallow_CachingHint)) {
654 return skgpu::graphite::MakeFromBitmap(recorder,
655 this->imageInfo().colorInfo(),
656 bitmap,
657 nullptr,
658 skgpu::Budgeted::kNo,
659 requiredProps);
660 }
661
662 return nullptr;
663 }
664
onMakeColorTypeAndColorSpace(SkColorType targetCT,sk_sp<SkColorSpace> targetCS,skgpu::graphite::Recorder * recorder,RequiredImageProperties requiredProps) const665 sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(
666 SkColorType targetCT,
667 sk_sp<SkColorSpace> targetCS,
668 skgpu::graphite::Recorder* recorder,
669 RequiredImageProperties requiredProps) const {
670 SkAutoMutexExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
671 if (fOnMakeColorTypeAndSpaceResult &&
672 targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
673 SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
674 return fOnMakeColorTypeAndSpaceResult;
675 }
676 Validator validator(fSharedGenerator, &targetCT, targetCS);
677 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
678 if (result) {
679 fOnMakeColorTypeAndSpaceResult = result;
680 }
681
682 if (recorder) {
683 return result->makeTextureImage(recorder, requiredProps);
684 } else {
685 return result;
686 }
687 }
688
689 #endif
690