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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrImageTextureMaker.h" |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 9 | |
| 10 | #include "src/gpu/GrColorSpaceXform.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/SkGr.h" |
| 12 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
| 13 | #include "src/image/SkImage_GpuYUVA.h" |
| 14 | #include "src/image/SkImage_Lazy.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 15 | |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 16 | static GrColorSpaceInfo make_info(const SkImage*& image) { |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame^] | 17 | return GrColorSpaceInfo(SkColorTypeToGrColorType(image->colorType()), |
| 18 | image->alphaType(), |
| 19 | image->refColorSpace(), |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 20 | SkImageInfo2GrPixelConfig(image->imageInfo())); |
| 21 | } |
| 22 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 23 | GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context, const SkImage* client, |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 24 | SkImage::CachingHint chint, bool useDecal) |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 25 | : INHERITED(context, client->width(), client->height(), make_info(client), useDecal) |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 26 | , fImage(static_cast<const SkImage_Lazy*>(client)) |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 27 | , fCachingHint(chint) { |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 28 | SkASSERT(client->isLazyGenerated()); |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 29 | GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), |
| 30 | SkIRect::MakeWH(this->width(), this->height())); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 31 | } |
| 32 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 33 | sk_sp<GrTextureProxy> GrImageTextureMaker::refOriginalTextureProxy(bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 34 | AllowedTexGenType onlyIfFast) { |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 35 | return fImage->lockTextureProxy(this->context(), fOriginalKey, fCachingHint, |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 36 | willBeMipped, onlyIfFast); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 39 | void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) { |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 40 | if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) { |
Brian Osman | 10494e3 | 2018-09-10 12:45:18 -0400 | [diff] [blame] | 41 | GrUniqueKey cacheKey; |
Brian Osman | bd65955 | 2018-09-11 10:03:19 -0400 | [diff] [blame] | 42 | fImage->makeCacheKeyFromOrigKey(fOriginalKey, &cacheKey); |
Brian Osman | 10494e3 | 2018-09-10 12:45:18 -0400 | [diff] [blame] | 43 | MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 47 | |
| 48 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 49 | |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 50 | GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrContext* context, const SkImage* client, |
| 51 | bool useDecal) |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 52 | : INHERITED(context, client->width(), client->height(), make_info(client), useDecal) |
| 53 | , fImage(static_cast<const SkImage_GpuYUVA*>(client)) { |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 54 | SkASSERT(as_IB(client)->isYUVA()); |
| 55 | GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), |
| 56 | SkIRect::MakeWH(this->width(), this->height())); |
| 57 | } |
| 58 | |
| 59 | sk_sp<GrTextureProxy> GrYUVAImageTextureMaker::refOriginalTextureProxy(bool willBeMipped, |
| 60 | AllowedTexGenType onlyIfFast) { |
Jim Van Verth | 7da4676 | 2018-11-05 14:24:23 -0500 | [diff] [blame] | 61 | if (AllowedTexGenType::kCheap == onlyIfFast) { |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 65 | if (willBeMipped) { |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 66 | return fImage->asMippedTextureProxyRef(this->context()); |
Jim Van Verth | 803a502 | 2018-11-05 15:55:53 -0500 | [diff] [blame] | 67 | } else { |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 68 | return fImage->asTextureProxyRef(this->context()); |
Jim Van Verth | 7da4676 | 2018-11-05 14:24:23 -0500 | [diff] [blame] | 69 | } |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void GrYUVAImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) { |
| 73 | // TODO: Do we ever want to disable caching? |
| 74 | if (fOriginalKey.isValid()) { |
| 75 | GrUniqueKey cacheKey; |
| 76 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 77 | GrUniqueKey::Builder builder(&cacheKey, fOriginalKey, kDomain, 0, "Image"); |
| 78 | MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey); |
| 79 | } |
| 80 | } |
| 81 | |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 82 | std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor( |
| 83 | const SkMatrix& textureMatrix, |
| 84 | const SkRect& constraintRect, |
| 85 | FilterConstraint filterConstraint, |
| 86 | bool coordsLimitedToConstraintRect, |
| 87 | const GrSamplerState::Filter* filterOrNullForBicubic) { |
| 88 | |
Brian Salomon | ad8efda | 2019-05-10 09:22:49 -0400 | [diff] [blame] | 89 | // Check simple cases to see if we need to fall back to flattening the image (or whether it's |
| 90 | // already been flattened.) |
| 91 | if (!filterOrNullForBicubic || this->domainNeedsDecal() || fImage->fRGBProxy) { |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 92 | return this->INHERITED::createFragmentProcessor(textureMatrix, constraintRect, |
| 93 | filterConstraint, |
| 94 | coordsLimitedToConstraintRect, |
| 95 | filterOrNullForBicubic); |
| 96 | } |
| 97 | |
Jim Van Verth | f542cab | 2018-11-07 12:08:21 -0500 | [diff] [blame] | 98 | // Check to see if the client has given us pre-mipped textures or we can generate them |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 99 | // If not, fall back to bilerp. Also fall back to bilerp when a domain is requested |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 100 | GrSamplerState::Filter filter = *filterOrNullForBicubic; |
Robert Phillips | 8defcc1 | 2019-03-05 15:58:59 -0500 | [diff] [blame] | 101 | if (GrSamplerState::Filter::kMipMap == filter && |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 102 | (filterConstraint == GrTextureProducer::kYes_FilterConstraint || |
| 103 | !fImage->setupMipmapsForPlanes(this->context()))) { |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 104 | filter = GrSamplerState::Filter::kBilerp; |
| 105 | } |
| 106 | |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 107 | // Cannot rely on GrTextureProducer's domain infrastructure since we need to calculate domain's |
| 108 | // per plane, which may be different, so respect the filterConstraint without any additional |
| 109 | // analysis. |
| 110 | const SkRect* domain = nullptr; |
| 111 | if (filterConstraint == GrTextureProducer::kYes_FilterConstraint) { |
| 112 | domain = &constraintRect; |
| 113 | } |
| 114 | |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 115 | auto fp = GrYUVtoRGBEffect::Make(fImage->fProxies, fImage->fYUVAIndices, |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 116 | fImage->fYUVColorSpace, filter, textureMatrix, domain); |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 117 | if (fImage->fFromColorSpace) { |
| 118 | fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fFromColorSpace.get(), |
| 119 | fImage->alphaType(), fImage->colorSpace()); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 120 | } |
| 121 | return fp; |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 122 | } |