blob: fa4d98646613e426b55066f9a34afa37ad92d1d5 [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
8#include "GrTextureMaker.h"
9
Brian Osman1cb41712017-10-19 12:54:52 -040010#include "GrColorSpaceXform.h"
Brian Osmane8e54582016-11-28 10:06:27 -050011#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050012#include "GrContextPriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050013#include "GrGpu.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050014#include "GrProxyProvider.h"
Brian Osmane8e54582016-11-28 10:06:27 -050015
Brian Salomon2a943df2018-05-04 13:43:19 -040016sk_sp<GrTextureProxy> GrTextureMaker::onRefTextureProxyForParams(const GrSamplerState& params,
Greg Daniel8e9b4c42018-07-20 10:30:48 -040017 bool willBeMipped,
Brian Salomon2a943df2018-05-04 13:43:19 -040018 SkScalar scaleAdjust[2]) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -040019 if (this->width() > fContext->contextPriv().caps()->maxTextureSize() ||
20 this->height() > fContext->contextPriv().caps()->maxTextureSize()) {
Brian Osman875f7852018-04-12 13:29:08 -040021 return nullptr;
22 }
23
Brian Osmane8e54582016-11-28 10:06:27 -050024 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050025
Brian Osmane7fd8c32018-10-19 13:30:39 -040026 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040027 AllowedTexGenType::kCheap));
Greg Daniel8f5bbda2018-06-08 17:22:23 -040028 bool needsCopyForMipsOnly = false;
Stan Ilievba81af22017-06-08 15:16:53 -040029 if (original) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040030 if (!params.isRepeated() ||
31 !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), original.get(),
32 original->width(), original->height(),
33 params.filter(), &copyParams, scaleAdjust)) {
34 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(fContext->contextPriv().caps(),
35 original.get(), params.filter(),
36 &copyParams);
37 if (!needsCopyForMipsOnly) {
38 return original;
39 }
Stan Ilievba81af22017-06-08 15:16:53 -040040 }
41 } else {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040042 if (!params.isRepeated() ||
43 !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), nullptr,
44 this->width(), this->height(),
45 params.filter(), &copyParams, scaleAdjust)) {
Brian Osmane7fd8c32018-10-19 13:30:39 -040046 return this->refOriginalTextureProxy(willBeMipped, AllowedTexGenType::kAny);
Stan Ilievba81af22017-06-08 15:16:53 -040047 }
Brian Osmane8e54582016-11-28 10:06:27 -050048 }
Stan Ilievba81af22017-06-08 15:16:53 -040049
Robert Phillips1afd4cd2018-01-08 13:40:32 -050050 GrProxyProvider* proxyProvider = fContext->contextPriv().proxyProvider();
51
52 GrSurfaceOrigin origOrigin = original ? original->origin() : kTopLeft_GrSurfaceOrigin;
Brian Osmane8e54582016-11-28 10:06:27 -050053 GrUniqueKey copyKey;
Brian Osmanb3f38302018-09-07 15:24:44 -040054 this->makeCopyKey(copyParams, &copyKey);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040055 sk_sp<GrTextureProxy> cachedProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050056 if (copyKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050057 cachedProxy = proxyProvider->findOrCreateProxyByUniqueKey(copyKey, origOrigin);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040058 if (cachedProxy && (!willBeMipped || GrMipMapped::kYes == cachedProxy->mipMapped())) {
59 return cachedProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050060 }
61 }
62
Greg Daniel8f5bbda2018-06-08 17:22:23 -040063 sk_sp<GrTextureProxy> source;
Stan Ilievba81af22017-06-08 15:16:53 -040064 if (original) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040065 source = std::move(original);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040066 } else if (cachedProxy) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040067 source = cachedProxy;
Stan Ilievba81af22017-06-08 15:16:53 -040068 } else {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040069 // Since we will be copying this texture there is no reason to make it mipped
Brian Osmane7fd8c32018-10-19 13:30:39 -040070 source = this->refOriginalTextureProxy(false, AllowedTexGenType::kAny);
Stan Ilievba81af22017-06-08 15:16:53 -040071 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -040072
73 if (!source) {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040074 return nullptr;
75 }
76
Greg Daniel8f5bbda2018-06-08 17:22:23 -040077 sk_sp<GrTextureProxy> result = CopyOnGpu(fContext, source, copyParams, willBeMipped);
Stan Ilievba81af22017-06-08 15:16:53 -040078
Brian Osmane8e54582016-11-28 10:06:27 -050079 if (!result) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040080 // If we were unable to make a copy and we only needed a copy for mips, then we will return
81 // the source texture here and require that the GPU backend is able to fall back to using
82 // bilerp if mips are required.
83 if (needsCopyForMipsOnly) {
84 return source;
85 }
Brian Osmane8e54582016-11-28 10:06:27 -050086 return nullptr;
87 }
88
89 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040090 SkASSERT(result->origin() == origOrigin);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040091 if (cachedProxy) {
92 SkASSERT(GrMipMapped::kYes == result->mipMapped() &&
93 GrMipMapped::kNo == cachedProxy->mipMapped());
94 // If we had a cachedProxy, that means there already is a proxy in the cache which
95 // matches the key, but it does not have mip levels and we require them. Thus we must
96 // remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070097 SkASSERT(cachedProxy->getUniqueKey() == copyKey);
98 proxyProvider->removeUniqueKeyFromProxy(cachedProxy.get());
Greg Daniel2d59d2f2017-10-31 15:25:14 -040099 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500100 proxyProvider->assignUniqueKeyToProxy(copyKey, result.get());
Brian Salomon238069b2018-07-11 15:58:57 -0400101 this->didCacheCopy(copyKey, proxyProvider->contextUniqueID());
Brian Osmane8e54582016-11-28 10:06:27 -0500102 }
103 return result;
104}
105
Brian Salomonaff329b2017-08-11 09:40:37 -0400106std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
107 const SkMatrix& textureMatrix,
108 const SkRect& constraintRect,
109 FilterConstraint filterConstraint,
110 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400111 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400112 const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic;
113 if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic &&
Brian Osmane8e54582016-11-28 10:06:27 -0500114 kYes_FilterConstraint == filterConstraint) {
115 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
116 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
117 // doing that.
118 // We instead we compute the domain as though were bilerping which is only correct if we
119 // only sample level 0.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400120 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500121 fmForDetermineDomain = &kBilerp;
122 }
123
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400124 GrSamplerState samplerState;
Brian Osmane8e54582016-11-28 10:06:27 -0500125 if (filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400126 samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500127 } else {
128 // Bicubic doesn't use filtering for it's texture accesses.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400129 samplerState = GrSamplerState::ClampNearest();
Brian Osmane8e54582016-11-28 10:06:27 -0500130 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500131 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Osman6064e1c2018-10-19 14:27:54 -0400132 sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(samplerState, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400133 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500134 return nullptr;
135 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500136 SkMatrix adjustedMatrix = textureMatrix;
137 adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500138 SkRect domain;
139 DomainMode domainMode =
Brian Salomon4df00922017-09-07 16:34:11 +0000140 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielc77085d2017-11-01 16:38:48 -0400141 proxy.get(), fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500142 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Osman05c8f462018-10-22 17:13:36 -0400143 return CreateFragmentProcessorForDomainAndFilter(std::move(proxy), adjustedMatrix, domainMode,
144 domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500145}