Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 8 | #include "GrColorSpaceXform.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 9 | #include "GrImageTextureMaker.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 10 | #include "SkGr.h" |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 11 | #include "SkImage_GpuYUVA.h" |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 12 | #include "SkImage_Lazy.h" |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 13 | #include "effects/GrYUVtoRGBEffect.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 14 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 15 | GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context, const SkImage* client, |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 16 | SkImage::CachingHint chint, bool useDecal) |
| 17 | : INHERITED(context, client->width(), client->height(), client->isAlphaOnly(), useDecal) |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 18 | , fImage(static_cast<const SkImage_Lazy*>(client)) |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 19 | , fCachingHint(chint) { |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 20 | SkASSERT(client->isLazyGenerated()); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 21 | GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), |
| 22 | SkIRect::MakeWH(this->width(), this->height())); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 23 | } |
| 24 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 25 | sk_sp<GrTextureProxy> GrImageTextureMaker::refOriginalTextureProxy(bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 26 | AllowedTexGenType onlyIfFast) { |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 27 | return fImage->lockTextureProxy(this->context(), fOriginalKey, fCachingHint, |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 28 | willBeMipped, onlyIfFast); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 31 | void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) { |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 32 | if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) { |
Brian Osman | 10494e3 | 2018-09-10 12:45:18 -0400 | [diff] [blame] | 33 | GrUniqueKey cacheKey; |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 34 | fImage->makeCacheKeyFromOrigKey(fOriginalKey, &cacheKey); |
Brian Osman | 10494e3 | 2018-09-10 12:45:18 -0400 | [diff] [blame] | 35 | MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 39 | SkAlphaType GrImageTextureMaker::alphaType() const { |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 40 | return fImage->alphaType(); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 41 | } |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 42 | SkColorSpace* GrImageTextureMaker::colorSpace() const { |
| 43 | return fImage->colorSpace(); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 44 | } |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 45 | |
| 46 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 47 | |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 48 | GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrContext* context, const SkImage* client, |
| 49 | bool useDecal) |
| 50 | : INHERITED(context, client->width(), client->height(), client->isAlphaOnly(), useDecal) |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 51 | , fImage(static_cast<const SkImage_GpuYUVA*>(client)) { |
| 52 | SkASSERT(as_IB(client)->isYUVA()); |
| 53 | GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), |
| 54 | SkIRect::MakeWH(this->width(), this->height())); |
| 55 | } |
| 56 | |
| 57 | sk_sp<GrTextureProxy> GrYUVAImageTextureMaker::refOriginalTextureProxy(bool willBeMipped, |
| 58 | AllowedTexGenType onlyIfFast) { |
Jim Van Verth | 7da4676 | 2018-11-05 14:24:23 -0500 | [diff] [blame] | 59 | if (AllowedTexGenType::kCheap == onlyIfFast) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 63 | if (willBeMipped) { |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame^] | 64 | return fImage->asMippedTextureProxyRef(this->context()); |
Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 65 | } else { |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame^] | 66 | return fImage->asTextureProxyRef(this->context()); |
Jim Van Verth | 7da4676 | 2018-11-05 14:24:23 -0500 | [diff] [blame] | 67 | } |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void GrYUVAImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) { |
| 71 | // TODO: Do we ever want to disable caching? |
| 72 | if (fOriginalKey.isValid()) { |
| 73 | GrUniqueKey cacheKey; |
| 74 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 75 | GrUniqueKey::Builder builder(&cacheKey, fOriginalKey, kDomain, 0, "Image"); |
| 76 | MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | SkAlphaType GrYUVAImageTextureMaker::alphaType() const { |
| 81 | return fImage->alphaType(); |
| 82 | } |
| 83 | SkColorSpace* GrYUVAImageTextureMaker::colorSpace() const { |
| 84 | return fImage->colorSpace(); |
| 85 | } |
| 86 | |
| 87 | std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor( |
| 88 | const SkMatrix& textureMatrix, |
| 89 | const SkRect& constraintRect, |
| 90 | FilterConstraint filterConstraint, |
| 91 | bool coordsLimitedToConstraintRect, |
| 92 | const GrSamplerState::Filter* filterOrNullForBicubic) { |
| 93 | |
| 94 | // Check simple cases to see if we need to fall back to flattening the image |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 95 | // TODO: See if we can relax this -- for example, if filterConstraint |
| 96 | // is kYes_FilterConstraint we still may not need a TextureDomain |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 97 | // in some cases. Or allow YUVtoRGBEffect to take a wrap mode to |
| 98 | // handle ClampToBorder when a decal is needed. |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 99 | if (!textureMatrix.isIdentity() || kNo_FilterConstraint != filterConstraint || |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 100 | !coordsLimitedToConstraintRect || !filterOrNullForBicubic || |
| 101 | this->domainNeedsDecal()) { |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 102 | return this->INHERITED::createFragmentProcessor(textureMatrix, constraintRect, |
| 103 | filterConstraint, |
| 104 | coordsLimitedToConstraintRect, |
| 105 | filterOrNullForBicubic); |
| 106 | } |
| 107 | |
Jim Van Verth | f542cab | 2018-11-07 12:08:21 -0500 | [diff] [blame] | 108 | // Check to see if the client has given us pre-mipped textures or we can generate them |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 109 | // If not, fall back to bilerp |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 110 | GrSamplerState::Filter filter = *filterOrNullForBicubic; |
Jim Van Verth | f542cab | 2018-11-07 12:08:21 -0500 | [diff] [blame] | 111 | if (GrSamplerState::Filter::kMipMap == filter && !fImage->setupMipmapsForPlanes()) { |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 112 | filter = GrSamplerState::Filter::kBilerp; |
| 113 | } |
| 114 | |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 115 | auto fp = GrYUVtoRGBEffect::Make(fImage->fProxies, fImage->fYUVAIndices, |
| 116 | fImage->fYUVColorSpace, filter); |
| 117 | if (fImage->fTargetColorSpace) { |
| 118 | fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fColorSpace.get(), |
| 119 | fImage->alphaType(), fImage->fTargetColorSpace.get()); |
| 120 | } |
| 121 | return fp; |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 122 | } |