Brian Osman | e8e5458 | 2016-11-28 10:06:27 -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 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 8 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrColorSpaceXform.h" |
| 10 | #include "src/gpu/GrGpu.h" |
| 11 | #include "src/gpu/GrProxyProvider.h" |
| 12 | #include "src/gpu/GrRecordingContextPriv.h" |
| 13 | #include "src/gpu/GrTextureAdjuster.h" |
| 14 | #include "src/gpu/SkGr.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 15 | |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 16 | GrTextureAdjuster::GrTextureAdjuster(GrRecordingContext* context, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 17 | GrSurfaceProxyView original, |
Greg Daniel | a4828a1 | 2019-10-11 13:51:02 -0400 | [diff] [blame] | 18 | const GrColorInfo& colorInfo, |
Brian Salomon | 777e146 | 2020-02-28 21:10:31 -0500 | [diff] [blame] | 19 | uint32_t uniqueID) |
| 20 | : INHERITED(context, {colorInfo, original.proxy()->dimensions()}) |
Greg Daniel | a4828a1 | 2019-10-11 13:51:02 -0400 | [diff] [blame] | 21 | , fOriginal(std::move(original)) |
| 22 | , fUniqueID(uniqueID) {} |
| 23 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 24 | GrSurfaceProxyView GrTextureAdjuster::makeMippedCopy() { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 25 | GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 26 | |
Brian Salomon | b62cee3 | 2020-02-28 13:34:16 -0500 | [diff] [blame] | 27 | GrUniqueKey baseKey, mipMappedKey; |
| 28 | GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeSize(this->dimensions())); |
| 29 | if (baseKey.isValid()) { |
| 30 | static const GrUniqueKey::Domain kMipMappedDomain = GrUniqueKey::GenerateDomain(); |
| 31 | GrUniqueKey::Builder builder(&mipMappedKey, baseKey, kMipMappedDomain, 0); |
| 32 | } |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 33 | sk_sp<GrTextureProxy> cachedCopy; |
Brian Salomon | b62cee3 | 2020-02-28 13:34:16 -0500 | [diff] [blame] | 34 | if (mipMappedKey.isValid()) { |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 35 | cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(mipMappedKey); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 36 | if (cachedCopy) { |
Brian Salomon | b62cee3 | 2020-02-28 13:34:16 -0500 | [diff] [blame] | 37 | return {std::move(cachedCopy), fOriginal.origin(), fOriginal.swizzle()}; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 41 | auto copy = GrCopyBaseMipMapToView(this->context(), fOriginal); |
| 42 | if (!copy) { |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 43 | return {}; |
Greg Daniel | cce6500 | 2020-01-23 11:16:49 -0500 | [diff] [blame] | 44 | } |
Brian Salomon | b62cee3 | 2020-02-28 13:34:16 -0500 | [diff] [blame] | 45 | if (mipMappedKey.isValid()) { |
Brian Salomon | b62cee3 | 2020-02-28 13:34:16 -0500 | [diff] [blame] | 46 | // TODO: If we move listeners up from SkImage_Lazy to SkImage_Base then add one here. |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 47 | proxyProvider->assignUniqueKeyToProxy(mipMappedKey, copy.asTextureProxy()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 48 | } |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 49 | return copy; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 52 | GrSurfaceProxyView GrTextureAdjuster::onView(GrMipMapped mipMapped) { |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 53 | if (this->context()->abandoned()) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 54 | // The texture was abandoned. |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 55 | return {}; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 56 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 57 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 58 | SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() && |
| 59 | this->height() <= this->context()->priv().caps()->maxTextureSize()); |
Brian Osman | 875f785 | 2018-04-12 13:29:08 -0400 | [diff] [blame] | 60 | |
Brian Salomon | b62cee3 | 2020-02-28 13:34:16 -0500 | [diff] [blame] | 61 | GrTextureProxy* texProxy = fOriginal.asTextureProxy(); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 62 | SkASSERT(texProxy); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 63 | if (mipMapped == GrMipMapped::kNo || texProxy->mipMapped() == GrMipMapped::kYes) { |
Brian Salomon | b62cee3 | 2020-02-28 13:34:16 -0500 | [diff] [blame] | 64 | return fOriginal; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 67 | GrSurfaceProxyView copy = this->makeMippedCopy(); |
| 68 | if (!copy) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 69 | // If we were unable to make a copy and we only needed a copy for mips, then we will return |
| 70 | // the source texture here and require that the GPU backend is able to fall back to using |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 71 | // linear filtering if mips are required. |
Brian Salomon | b62cee3 | 2020-02-28 13:34:16 -0500 | [diff] [blame] | 72 | return fOriginal; |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 73 | } |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 74 | SkASSERT(copy.asTextureProxy()); |
| 75 | return copy; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 78 | std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor( |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 79 | const SkMatrix& textureMatrix, |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 80 | const SkRect* subset, |
| 81 | const SkRect* domain, |
| 82 | GrSamplerState samplerState) { |
| 83 | return this->createFragmentProcessorForView( |
| 84 | this->view(samplerState.filter()), textureMatrix, subset, domain, samplerState); |
| 85 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 86 | |
Brian Salomon | 0ea3307 | 2020-07-14 10:43:42 -0400 | [diff] [blame] | 87 | std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createBicubicFragmentProcessor( |
| 88 | const SkMatrix& textureMatrix, |
| 89 | const SkRect* subset, |
| 90 | const SkRect* domain, |
| 91 | GrSamplerState::WrapMode wrapX, |
| 92 | GrSamplerState::WrapMode wrapY) { |
| 93 | return this->createBicubicFragmentProcessorForView( |
| 94 | this->view(GrMipMapped::kNo), textureMatrix, subset, domain, wrapX, wrapY); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 95 | } |