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