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