blob: 22751792d439f3d5aaa6351467c978d25678c132 [file] [log] [blame]
Brian Osmane8e54582016-11-28 10:06:27 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrTextureMaker.h"
Brian Osmane8e54582016-11-28 10:06:27 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
11#include "src/gpu/GrColorSpaceXform.h"
12#include "src/gpu/GrGpu.h"
13#include "src/gpu/GrProxyProvider.h"
14#include "src/gpu/GrRecordingContextPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050015
Greg Danielcc104db2020-02-03 14:17:08 -050016GrSurfaceProxyView GrTextureMaker::onRefTextureProxyViewForParams(GrSamplerState params,
17 bool willBeMipped,
18 SkScalar scaleAdjust[2]) {
Robert Phillips9338c602019-02-19 12:52:29 -050019 if (this->width() > this->context()->priv().caps()->maxTextureSize() ||
20 this->height() > this->context()->priv().caps()->maxTextureSize()) {
Greg Danielcc104db2020-02-03 14:17:08 -050021 return {};
Brian Osman875f7852018-04-12 13:29:08 -040022 }
23
Brian Osmane8e54582016-11-28 10:06:27 -050024 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050025
Greg Danielcc104db2020-02-03 14:17:08 -050026 GrSurfaceProxyView original = this->refOriginalTextureProxyView(willBeMipped,
27 AllowedTexGenType::kCheap);
Greg Daniel8f5bbda2018-06-08 17:22:23 -040028 bool needsCopyForMipsOnly = false;
Greg Danielcc104db2020-02-03 14:17:08 -050029 if (original.proxy()) {
30 GrTextureProxy* texProxy = original.asTextureProxy();
31 SkASSERT(texProxy);
Greg Daniel8f5bbda2018-06-08 17:22:23 -040032 if (!params.isRepeated() ||
Greg Danielcc104db2020-02-03 14:17:08 -050033 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), texProxy,
34 texProxy->dimensions(), params.filter(),
Brian Salomon1a217eb2019-10-24 10:50:36 -040035 &copyParams, scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -050036 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Danielcc104db2020-02-03 14:17:08 -050037 texProxy, params.filter(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -040038 &copyParams);
39 if (!needsCopyForMipsOnly) {
40 return original;
41 }
Stan Ilievba81af22017-06-08 15:16:53 -040042 }
43 } else {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040044 if (!params.isRepeated() ||
Robert Phillips9338c602019-02-19 12:52:29 -050045 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), nullptr,
Brian Salomon1a217eb2019-10-24 10:50:36 -040046 this->dimensions(), params.filter(), &copyParams,
47 scaleAdjust)) {
Greg Danielcc104db2020-02-03 14:17:08 -050048 return this->refOriginalTextureProxyView(willBeMipped, AllowedTexGenType::kAny);
Stan Ilievba81af22017-06-08 15:16:53 -040049 }
Brian Osmane8e54582016-11-28 10:06:27 -050050 }
Stan Ilievba81af22017-06-08 15:16:53 -040051
Robert Phillips9338c602019-02-19 12:52:29 -050052 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050053
Greg Danielcc104db2020-02-03 14:17:08 -050054 GrSurfaceOrigin origOrigin = original.proxy() ? original.origin() : kTopLeft_GrSurfaceOrigin;
Brian Osmane8e54582016-11-28 10:06:27 -050055 GrUniqueKey copyKey;
Brian Osmanb3f38302018-09-07 15:24:44 -040056 this->makeCopyKey(copyParams, &copyKey);
Greg Danielcc104db2020-02-03 14:17:08 -050057 GrSurfaceProxyView cachedView;
Brian Osmane8e54582016-11-28 10:06:27 -050058 if (copyKey.isValid()) {
Greg Danielcc104db2020-02-03 14:17:08 -050059 auto cachedProxy =
Brian Salomon2af3e702019-08-11 19:10:31 -040060 proxyProvider->findOrCreateProxyByUniqueKey(copyKey, this->colorType(), origOrigin);
Greg Danielcc104db2020-02-03 14:17:08 -050061 if (cachedProxy) {
62 GrMipMapped mipped = cachedProxy->mipMapped();
63 // TODO: Once we no longer use CopyOnGpu which can fallback to arbitrary formats and
64 // colorTypes, we can use the swizzle of the originalView.
Greg Daniel87506ab2020-02-12 13:05:42 -050065 GrSwizzle swizzle = cachedProxy->textureSwizzleDoNotUse();
Greg Danielcc104db2020-02-03 14:17:08 -050066 cachedView = GrSurfaceProxyView(std::move(cachedProxy), origOrigin, swizzle);
67 if (!willBeMipped || GrMipMapped::kYes == mipped) {
68 return cachedView;
69 }
Brian Osmane8e54582016-11-28 10:06:27 -050070 }
71 }
72
Greg Danielcc104db2020-02-03 14:17:08 -050073 GrSurfaceProxyView source;
74 if (original.proxy()) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040075 source = std::move(original);
Greg Danielcc104db2020-02-03 14:17:08 -050076 } else if (cachedView.proxy()) {
77 source = cachedView;
Stan Ilievba81af22017-06-08 15:16:53 -040078 } else {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040079 // Since we will be copying this texture there is no reason to make it mipped
Greg Danielcc104db2020-02-03 14:17:08 -050080 source = this->refOriginalTextureProxyView(false, AllowedTexGenType::kAny);
Stan Ilievba81af22017-06-08 15:16:53 -040081 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -040082
Greg Danielcc104db2020-02-03 14:17:08 -050083 if (!source.proxy()) {
84 return {};
Greg Daniel2d59d2f2017-10-31 15:25:14 -040085 }
Greg Danielcc104db2020-02-03 14:17:08 -050086 SkASSERT(source.asTextureProxy());
Greg Daniel2d59d2f2017-10-31 15:25:14 -040087
Greg Danielcc104db2020-02-03 14:17:08 -050088 GrSurfaceProxyView result =
Brian Salomond6287472019-06-24 15:50:07 -040089 CopyOnGpu(this->context(), source, this->colorType(), copyParams, willBeMipped);
Stan Ilievba81af22017-06-08 15:16:53 -040090
Greg Danielcc104db2020-02-03 14:17:08 -050091 if (!result.proxy()) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040092 // If we were unable to make a copy and we only needed a copy for mips, then we will return
93 // the source texture here and require that the GPU backend is able to fall back to using
94 // bilerp if mips are required.
95 if (needsCopyForMipsOnly) {
96 return source;
97 }
Greg Danielcc104db2020-02-03 14:17:08 -050098 return {};
Brian Osmane8e54582016-11-28 10:06:27 -050099 }
100
101 if (copyKey.isValid()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500102 SkASSERT(result.origin() == origOrigin);
103 if (cachedView.proxy()) {
104 SkASSERT(GrMipMapped::kYes == result.asTextureProxy()->mipMapped() &&
105 GrMipMapped::kNo == cachedView.asTextureProxy()->mipMapped());
Greg Daniel2d59d2f2017-10-31 15:25:14 -0400106 // If we had a cachedProxy, that means there already is a proxy in the cache which
107 // matches the key, but it does not have mip levels and we require them. Thus we must
108 // remove the unique key from that proxy.
Greg Danielcc104db2020-02-03 14:17:08 -0500109 SkASSERT(cachedView.asTextureProxy()->getUniqueKey() == copyKey);
110 proxyProvider->removeUniqueKeyFromProxy(cachedView.asTextureProxy());
Greg Daniel2d59d2f2017-10-31 15:25:14 -0400111 }
Greg Danielcc104db2020-02-03 14:17:08 -0500112 proxyProvider->assignUniqueKeyToProxy(copyKey, result.asTextureProxy());
Robert Phillipsa41c6852019-02-07 10:44:10 -0500113 this->didCacheCopy(copyKey, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -0500114 }
115 return result;
116}
117
Brian Salomonaff329b2017-08-11 09:40:37 -0400118std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
119 const SkMatrix& textureMatrix,
120 const SkRect& constraintRect,
121 FilterConstraint filterConstraint,
122 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400123 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400124 const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic;
125 if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic &&
Brian Osmane8e54582016-11-28 10:06:27 -0500126 kYes_FilterConstraint == filterConstraint) {
Michael Ludwigddeed372019-02-20 16:50:10 -0500127 // TODO: Here we should force a copy restricted to the constraintRect since MIP maps will
Brian Osmane8e54582016-11-28 10:06:27 -0500128 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
129 // doing that.
130 // We instead we compute the domain as though were bilerping which is only correct if we
131 // only sample level 0.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400132 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500133 fmForDetermineDomain = &kBilerp;
134 }
135
Robert Phillips67c18d62017-01-20 12:44:06 -0500136 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Greg Danielc61d7e32020-02-04 14:27:45 -0500137 GrSurfaceProxyView view = this->viewForParams(filterOrNullForBicubic, scaleAdjust);
Greg Danielcc104db2020-02-03 14:17:08 -0500138 if (!view.proxy()) {
Brian Osmane8e54582016-11-28 10:06:27 -0500139 return nullptr;
140 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500141 SkMatrix adjustedMatrix = textureMatrix;
142 adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Michael Ludwigddeed372019-02-20 16:50:10 -0500143
Brian Osmane8e54582016-11-28 10:06:27 -0500144 SkRect domain;
145 DomainMode domainMode =
Brian Salomon4df00922017-09-07 16:34:11 +0000146 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielcc104db2020-02-03 14:17:08 -0500147 view.proxy(), fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500148 SkASSERT(kTightCopy_DomainMode != domainMode);
Michael Ludwigddeed372019-02-20 16:50:10 -0500149 return this->createFragmentProcessorForDomainAndFilter(
Greg Danielcc104db2020-02-03 14:17:08 -0500150 std::move(view), adjustedMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500151}