blob: 34aac309dce495ca035d4542d7c326b0e91028a7 [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,
Brian Salomon4df00922017-09-07 16:34:11 +000018 SkAlphaType alphaType,
Greg Danielc77085d2017-11-01 16:38:48 -040019 uint32_t uniqueID,
Michael Ludwigddeed372019-02-20 16:50:10 -050020 SkColorSpace* cs,
21 bool useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040022 : INHERITED(context, original->width(), original->height(),
23 GrColorSpaceInfo(alphaType, sk_ref_sp(cs), original->config()), useDecal)
24 , fOriginal(std::move(original))
25 , fUniqueID(uniqueID) {}
Brian Osmane8e54582016-11-28 10:06:27 -050026
Brian Osmanb3f38302018-09-07 15:24:44 -040027void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -050028 // Destination color space is irrelevant - we already have a texture so we're just sub-setting
Brian Osmane8e54582016-11-28 10:06:27 -050029 GrUniqueKey baseKey;
30 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height()));
31 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
32}
33
Brian Salomon238069b2018-07-11 15:58:57 -040034void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
Brian Osmane8e54582016-11-28 10:06:27 -050035 // We don't currently have a mechanism for notifications on Images!
36}
37
Greg Daniele1da1d92017-10-06 15:59:27 -040038sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams,
39 bool willBeMipped) {
Robert Phillips9338c602019-02-19 12:52:29 -050040 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050041
Robert Phillips0c984a02017-03-16 07:51:56 -040042 GrUniqueKey key;
Brian Osmanb3f38302018-09-07 15:24:44 -040043 this->makeCopyKey(copyParams, &key);
Greg Daniel09c94002018-06-08 22:11:51 +000044 sk_sp<GrTextureProxy> cachedCopy;
Robert Phillips0c984a02017-03-16 07:51:56 -040045 if (key.isValid()) {
Greg Daniel09c94002018-06-08 22:11:51 +000046 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key,
47 this->originalProxy()->origin());
48 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Robert Phillips0c984a02017-03-16 07:51:56 -040049 return cachedCopy;
50 }
51 }
52
53 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Robert Phillips0c984a02017-03-16 07:51:56 -040054
Robert Phillips9338c602019-02-19 12:52:29 -050055 sk_sp<GrTextureProxy> copy = CopyOnGpu(this->context(), std::move(proxy),
56 copyParams, willBeMipped);
Robert Phillips0c984a02017-03-16 07:51:56 -040057 if (copy) {
58 if (key.isValid()) {
Robert Phillips78814092017-07-24 15:26:17 -040059 SkASSERT(copy->origin() == this->originalProxy()->origin());
Greg Daniel09c94002018-06-08 22:11:51 +000060 if (cachedCopy) {
61 SkASSERT(GrMipMapped::kYes == copy->mipMapped() &&
62 GrMipMapped::kNo == cachedCopy->mipMapped());
63 // If we had a cachedProxy, that means there already is a proxy in the cache which
64 // matches the key, but it does not have mip levels and we require them. Thus we
65 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070066 SkASSERT(cachedCopy->getUniqueKey() == key);
67 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000068 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050069 proxyProvider->assignUniqueKeyToProxy(key, copy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -050070 this->didCacheCopy(key, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050071 }
72 }
73 return copy;
74}
75
Brian Salomon2a943df2018-05-04 13:43:19 -040076sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams(
77 const GrSamplerState& params,
Greg Daniel8e9b4c42018-07-20 10:30:48 -040078 bool willBeMipped,
Brian Salomon2a943df2018-05-04 13:43:19 -040079 SkScalar scaleAdjust[2]) {
Robert Phillips3798c862017-03-27 11:08:16 -040080 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Brian Osmane8e54582016-11-28 10:06:27 -050081 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050082
Robert Phillips9338c602019-02-19 12:52:29 -050083 if (this->context()->priv().abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050084 // The texture was abandoned.
85 return nullptr;
86 }
Brian Salomon4df00922017-09-07 16:34:11 +000087
Robert Phillips9338c602019-02-19 12:52:29 -050088 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
89 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040090
Greg Daniel8f5bbda2018-06-08 17:22:23 -040091 bool needsCopyForMipsOnly = false;
92 if (!params.isRepeated() ||
Robert Phillips9338c602019-02-19 12:52:29 -050093 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), proxy.get(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -040094 proxy->width(), proxy->height(), params.filter(),
95 &copyParams, scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -050096 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -040097 proxy.get(), params.filter(),
98 &copyParams);
99 if (!needsCopyForMipsOnly) {
100 return proxy;
101 }
Brian Osmane8e54582016-11-28 10:06:27 -0500102 }
103
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400104 sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped);
105 if (!result && needsCopyForMipsOnly) {
106 // If we were unable to make a copy and we only needed a copy for mips, then we will return
107 // the source texture here and require that the GPU backend is able to fall back to using
108 // bilerp if mips are required.
109 return this->originalProxyRef();
110 }
111 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500112}
113
Brian Salomonaff329b2017-08-11 09:40:37 -0400114std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
115 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400116 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400117 FilterConstraint filterConstraint,
118 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400119 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500120 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500121
Robert Phillips67c18d62017-01-20 12:44:06 -0500122 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400123 sk_sp<GrTextureProxy> proxy(
Michael Ludwigddeed372019-02-20 16:50:10 -0500124 this->refTextureProxyForParams(filterOrNullForBicubic, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400125 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500126 return nullptr;
127 }
128 // If we made a copy then we only copied the contentArea, in which case the new texture is all
129 // content.
Robert Phillips3798c862017-03-27 11:08:16 -0400130 if (proxy.get() != this->originalProxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500131 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500132 }
133
Michael Ludwigddeed372019-02-20 16:50:10 -0500134 SkRect domain;
Brian Osmane8e54582016-11-28 10:06:27 -0500135 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400136 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
137 proxy.get(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500138 if (kTightCopy_DomainMode == domainMode) {
139 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
140 // non-int constraint rect)
141 // For now: treat as bilerp and ignore what goes on above level 0.
142
143 // We only expect MIP maps to require a tight copy.
144 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400145 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
146 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500147 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400148 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
149 proxy.get(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500150 SkASSERT(kTightCopy_DomainMode != domainMode);
151 }
152 SkASSERT(kNoDomain_DomainMode == domainMode ||
153 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Michael Ludwigddeed372019-02-20 16:50:10 -0500154 return this->createFragmentProcessorForDomainAndFilter(
155 std::move(proxy), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500156}