blob: c299fed9f923652d389a3e3bfa1bb1644f268118 [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 Salomond6287472019-06-24 15:50:07 -040018 GrColorType colorType,
Brian Salomon4df00922017-09-07 16:34:11 +000019 SkAlphaType alphaType,
Greg Danielc77085d2017-11-01 16:38:48 -040020 uint32_t uniqueID,
Michael Ludwigddeed372019-02-20 16:50:10 -050021 SkColorSpace* cs,
22 bool useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040023 : INHERITED(context, original->width(), original->height(),
Brian Salomond6287472019-06-24 15:50:07 -040024 GrColorSpaceInfo(colorType,
25 alphaType,
26 sk_ref_sp(cs),
27 original->config()),
28 useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040029 , fOriginal(std::move(original))
30 , fUniqueID(uniqueID) {}
Brian Osmane8e54582016-11-28 10:06:27 -050031
Brian Osmanb3f38302018-09-07 15:24:44 -040032void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -050033 // Destination color space is irrelevant - we already have a texture so we're just sub-setting
Brian Osmane8e54582016-11-28 10:06:27 -050034 GrUniqueKey baseKey;
35 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height()));
36 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
37}
38
Brian Salomon238069b2018-07-11 15:58:57 -040039void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
Brian Osmane8e54582016-11-28 10:06:27 -050040 // We don't currently have a mechanism for notifications on Images!
41}
42
Greg Daniele1da1d92017-10-06 15:59:27 -040043sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams,
44 bool willBeMipped) {
Robert Phillips9338c602019-02-19 12:52:29 -050045 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050046
Robert Phillips0c984a02017-03-16 07:51:56 -040047 GrUniqueKey key;
Brian Osmanb3f38302018-09-07 15:24:44 -040048 this->makeCopyKey(copyParams, &key);
Greg Daniel09c94002018-06-08 22:11:51 +000049 sk_sp<GrTextureProxy> cachedCopy;
Robert Phillips0c984a02017-03-16 07:51:56 -040050 if (key.isValid()) {
Greg Daniel09c94002018-06-08 22:11:51 +000051 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key,
52 this->originalProxy()->origin());
53 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Robert Phillips0c984a02017-03-16 07:51:56 -040054 return cachedCopy;
55 }
56 }
57
58 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Robert Phillips0c984a02017-03-16 07:51:56 -040059
Brian Salomond6287472019-06-24 15:50:07 -040060 sk_sp<GrTextureProxy> copy = CopyOnGpu(this->context(), std::move(proxy), this->colorType(),
Robert Phillips9338c602019-02-19 12:52:29 -050061 copyParams, willBeMipped);
Robert Phillips0c984a02017-03-16 07:51:56 -040062 if (copy) {
63 if (key.isValid()) {
Robert Phillips78814092017-07-24 15:26:17 -040064 SkASSERT(copy->origin() == this->originalProxy()->origin());
Greg Daniel09c94002018-06-08 22:11:51 +000065 if (cachedCopy) {
66 SkASSERT(GrMipMapped::kYes == copy->mipMapped() &&
67 GrMipMapped::kNo == cachedCopy->mipMapped());
68 // If we had a cachedProxy, that means there already is a proxy in the cache which
69 // matches the key, but it does not have mip levels and we require them. Thus we
70 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070071 SkASSERT(cachedCopy->getUniqueKey() == key);
72 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000073 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074 proxyProvider->assignUniqueKeyToProxy(key, copy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -050075 this->didCacheCopy(key, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050076 }
77 }
78 return copy;
79}
80
Brian Salomon2a943df2018-05-04 13:43:19 -040081sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams(
82 const GrSamplerState& params,
Greg Daniel8e9b4c42018-07-20 10:30:48 -040083 bool willBeMipped,
Brian Salomon2a943df2018-05-04 13:43:19 -040084 SkScalar scaleAdjust[2]) {
Robert Phillips3798c862017-03-27 11:08:16 -040085 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Brian Osmane8e54582016-11-28 10:06:27 -050086 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050087
Robert Phillips9338c602019-02-19 12:52:29 -050088 if (this->context()->priv().abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050089 // The texture was abandoned.
90 return nullptr;
91 }
Brian Salomon4df00922017-09-07 16:34:11 +000092
Robert Phillips9338c602019-02-19 12:52:29 -050093 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
94 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040095
Greg Daniel8f5bbda2018-06-08 17:22:23 -040096 bool needsCopyForMipsOnly = false;
97 if (!params.isRepeated() ||
Robert Phillips9338c602019-02-19 12:52:29 -050098 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), proxy.get(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -040099 proxy->width(), proxy->height(), params.filter(),
100 &copyParams, scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -0500101 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400102 proxy.get(), params.filter(),
103 &copyParams);
104 if (!needsCopyForMipsOnly) {
105 return proxy;
106 }
Brian Osmane8e54582016-11-28 10:06:27 -0500107 }
108
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400109 sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped);
110 if (!result && needsCopyForMipsOnly) {
111 // If we were unable to make a copy and we only needed a copy for mips, then we will return
112 // the source texture here and require that the GPU backend is able to fall back to using
113 // bilerp if mips are required.
114 return this->originalProxyRef();
115 }
116 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500117}
118
Brian Salomonaff329b2017-08-11 09:40:37 -0400119std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
120 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400121 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400122 FilterConstraint filterConstraint,
123 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400124 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500125 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500126
Robert Phillips67c18d62017-01-20 12:44:06 -0500127 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400128 sk_sp<GrTextureProxy> proxy(
Michael Ludwigddeed372019-02-20 16:50:10 -0500129 this->refTextureProxyForParams(filterOrNullForBicubic, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400130 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500131 return nullptr;
132 }
133 // If we made a copy then we only copied the contentArea, in which case the new texture is all
134 // content.
Robert Phillips3798c862017-03-27 11:08:16 -0400135 if (proxy.get() != this->originalProxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500136 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500137 }
138
Michael Ludwigddeed372019-02-20 16:50:10 -0500139 SkRect domain;
Brian Osmane8e54582016-11-28 10:06:27 -0500140 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400141 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
142 proxy.get(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500143 if (kTightCopy_DomainMode == domainMode) {
144 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
145 // non-int constraint rect)
146 // For now: treat as bilerp and ignore what goes on above level 0.
147
148 // We only expect MIP maps to require a tight copy.
149 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400150 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
151 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500152 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400153 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
154 proxy.get(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500155 SkASSERT(kTightCopy_DomainMode != domainMode);
156 }
157 SkASSERT(kNoDomain_DomainMode == domainMode ||
158 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Michael Ludwigddeed372019-02-20 16:50:10 -0500159 return this->createFragmentProcessorForDomainAndFilter(
160 std::move(proxy), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500161}