blob: 66f626660780a697a01ed0890260a44df9864c7a [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 "include/private/GrRecordingContext.h"
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 Osmane8e54582016-11-28 10:06:27 -050015
Brian Salomone7499c72019-06-24 12:12:36 -040016GrTextureAdjuster::GrTextureAdjuster(GrRecordingContext* context,
17 sk_sp<GrTextureProxy> original,
Greg Daniela4828a12019-10-11 13:51:02 -040018 const GrColorInfo& colorInfo,
19 uint32_t uniqueID,
20 bool useDecal)
Brian Salomon1a217eb2019-10-24 10:50:36 -040021 : INHERITED(context, {colorInfo, original->dimensions()}, useDecal)
Greg Daniela4828a12019-10-11 13:51:02 -040022 , fOriginal(std::move(original))
23 , fUniqueID(uniqueID) {}
24
Brian Osmanb3f38302018-09-07 15:24:44 -040025void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -050026 // Destination color space is irrelevant - we already have a texture so we're just sub-setting
Brian Osmane8e54582016-11-28 10:06:27 -050027 GrUniqueKey baseKey;
Brian Salomon1a217eb2019-10-24 10:50:36 -040028 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeSize(this->dimensions()));
Brian Osmane8e54582016-11-28 10:06:27 -050029 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
30}
31
Brian Salomon238069b2018-07-11 15:58:57 -040032void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
Brian Osmane8e54582016-11-28 10:06:27 -050033 // We don't currently have a mechanism for notifications on Images!
34}
35
Greg Daniele1da1d92017-10-06 15:59:27 -040036sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams,
Greg Danielcce65002020-01-23 11:16:49 -050037 bool willBeMipped,
38 bool copyForMipsOnly) {
Robert Phillips9338c602019-02-19 12:52:29 -050039 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040
Robert Phillips0c984a02017-03-16 07:51:56 -040041 GrUniqueKey key;
Brian Osmanb3f38302018-09-07 15:24:44 -040042 this->makeCopyKey(copyParams, &key);
Greg Daniel09c94002018-06-08 22:11:51 +000043 sk_sp<GrTextureProxy> cachedCopy;
Robert Phillips0c984a02017-03-16 07:51:56 -040044 if (key.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040045 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key, this->colorType(),
Greg Daniel09c94002018-06-08 22:11:51 +000046 this->originalProxy()->origin());
47 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Robert Phillips0c984a02017-03-16 07:51:56 -040048 return cachedCopy;
49 }
50 }
51
52 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Robert Phillips0c984a02017-03-16 07:51:56 -040053
Greg Danielcce65002020-01-23 11:16:49 -050054 sk_sp<GrTextureProxy> copy;
55 if (copyForMipsOnly) {
56 copy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(), this->colorType());
57 } else {
58 copy = CopyOnGpu(this->context(), std::move(proxy), this->colorType(),
59 copyParams, willBeMipped);
60 }
Robert Phillips0c984a02017-03-16 07:51:56 -040061 if (copy) {
62 if (key.isValid()) {
Robert Phillips78814092017-07-24 15:26:17 -040063 SkASSERT(copy->origin() == this->originalProxy()->origin());
Greg Daniel09c94002018-06-08 22:11:51 +000064 if (cachedCopy) {
65 SkASSERT(GrMipMapped::kYes == copy->mipMapped() &&
66 GrMipMapped::kNo == cachedCopy->mipMapped());
67 // If we had a cachedProxy, that means there already is a proxy in the cache which
68 // matches the key, but it does not have mip levels and we require them. Thus we
69 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070070 SkASSERT(cachedCopy->getUniqueKey() == key);
71 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000072 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050073 proxyProvider->assignUniqueKeyToProxy(key, copy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -050074 this->didCacheCopy(key, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050075 }
76 }
77 return copy;
78}
79
Brian Salomonccb61422020-01-09 10:46:36 -050080sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams(GrSamplerState params,
81 bool willBeMipped,
82 SkScalar scaleAdjust[2]) {
Robert Phillips3798c862017-03-27 11:08:16 -040083 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Brian Osmane8e54582016-11-28 10:06:27 -050084 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050085
Robert Phillips9338c602019-02-19 12:52:29 -050086 if (this->context()->priv().abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050087 // The texture was abandoned.
88 return nullptr;
89 }
Brian Salomon4df00922017-09-07 16:34:11 +000090
Robert Phillips9338c602019-02-19 12:52:29 -050091 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
92 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040093
Greg Daniel8f5bbda2018-06-08 17:22:23 -040094 bool needsCopyForMipsOnly = false;
95 if (!params.isRepeated() ||
Robert Phillips9338c602019-02-19 12:52:29 -050096 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), proxy.get(),
Brian Salomon1a217eb2019-10-24 10:50:36 -040097 proxy->dimensions(), params.filter(), &copyParams,
98 scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -050099 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400100 proxy.get(), params.filter(),
101 &copyParams);
102 if (!needsCopyForMipsOnly) {
103 return proxy;
104 }
Brian Osmane8e54582016-11-28 10:06:27 -0500105 }
106
Greg Danielcce65002020-01-23 11:16:49 -0500107 sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped,
108 needsCopyForMipsOnly);
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400109 if (!result && needsCopyForMipsOnly) {
110 // If we were unable to make a copy and we only needed a copy for mips, then we will return
111 // the source texture here and require that the GPU backend is able to fall back to using
112 // bilerp if mips are required.
113 return this->originalProxyRef();
114 }
115 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500116}
117
Brian Salomonaff329b2017-08-11 09:40:37 -0400118std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
119 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400120 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400121 FilterConstraint filterConstraint,
122 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400123 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500124 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500125
Robert Phillips67c18d62017-01-20 12:44:06 -0500126 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400127 sk_sp<GrTextureProxy> proxy(
Michael Ludwigddeed372019-02-20 16:50:10 -0500128 this->refTextureProxyForParams(filterOrNullForBicubic, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400129 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500130 return nullptr;
131 }
132 // If we made a copy then we only copied the contentArea, in which case the new texture is all
133 // content.
Robert Phillips3798c862017-03-27 11:08:16 -0400134 if (proxy.get() != this->originalProxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500135 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500136 }
137
Michael Ludwigddeed372019-02-20 16:50:10 -0500138 SkRect domain;
Brian Osmane8e54582016-11-28 10:06:27 -0500139 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400140 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
141 proxy.get(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500142 if (kTightCopy_DomainMode == domainMode) {
143 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
144 // non-int constraint rect)
145 // For now: treat as bilerp and ignore what goes on above level 0.
146
147 // We only expect MIP maps to require a tight copy.
148 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400149 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
150 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500151 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400152 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
153 proxy.get(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500154 SkASSERT(kTightCopy_DomainMode != domainMode);
155 }
156 SkASSERT(kNoDomain_DomainMode == domainMode ||
157 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Michael Ludwigddeed372019-02-20 16:50:10 -0500158 return this->createFragmentProcessorForDomainAndFilter(
159 std::move(proxy), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500160}