blob: d9e89c2113590a56596e89286b3d268d0eab28d5 [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,
Brian Salomonc8d092a2020-02-24 10:14:21 -050017 bool willBeMipped) {
Robert Phillips9338c602019-02-19 12:52:29 -050018 if (this->width() > this->context()->priv().caps()->maxTextureSize() ||
19 this->height() > this->context()->priv().caps()->maxTextureSize()) {
Greg Danielcc104db2020-02-03 14:17:08 -050020 return {};
Brian Osman875f7852018-04-12 13:29:08 -040021 }
22
Greg Danielcc104db2020-02-03 14:17:08 -050023 GrSurfaceProxyView original = this->refOriginalTextureProxyView(willBeMipped,
24 AllowedTexGenType::kCheap);
Brian Salomonc8d092a2020-02-24 10:14:21 -050025 if (!original) {
26 return this->refOriginalTextureProxyView(willBeMipped, AllowedTexGenType::kAny);
27 }
28
29 GrTextureProxy* texProxy = original.asTextureProxy();
30 if (!GrGpu::IsACopyNeededForMips(this->context()->priv().caps(), texProxy, params.filter())) {
31 return original;
Brian Osmane8e54582016-11-28 10:06:27 -050032 }
Stan Ilievba81af22017-06-08 15:16:53 -040033
Robert Phillips9338c602019-02-19 12:52:29 -050034 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050035
Greg Danielcc104db2020-02-03 14:17:08 -050036 GrSurfaceOrigin origOrigin = original.proxy() ? original.origin() : kTopLeft_GrSurfaceOrigin;
Brian Salomonc8d092a2020-02-24 10:14:21 -050037 GrUniqueKey mipMappedKey;
38 this->makeMipMappedKey(&mipMappedKey);
39 if (mipMappedKey.isValid()) {
40 auto cachedProxy =
41 proxyProvider->findOrCreateProxyByUniqueKey(mipMappedKey, this->colorType());
Greg Danielcc104db2020-02-03 14:17:08 -050042 if (cachedProxy) {
Brian Salomonc8d092a2020-02-24 10:14:21 -050043 SkASSERT(cachedProxy->mipMapped() == GrMipMapped::kYes);
44 // TODO: Once we no longer use MakeMipMappedCopy which can fallback to arbitrary formats
45 // and colorTypes, we can use the swizzle of the originalView.
Greg Daniel87506ab2020-02-12 13:05:42 -050046 GrSwizzle swizzle = cachedProxy->textureSwizzleDoNotUse();
Brian Salomonc8d092a2020-02-24 10:14:21 -050047 return GrSurfaceProxyView(std::move(cachedProxy), origOrigin, swizzle);
Brian Osmane8e54582016-11-28 10:06:27 -050048 }
49 }
50
Greg Danielcc104db2020-02-03 14:17:08 -050051 GrSurfaceProxyView source;
Brian Salomonc8d092a2020-02-24 10:14:21 -050052 if (original) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040053 source = std::move(original);
Stan Ilievba81af22017-06-08 15:16:53 -040054 } else {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040055 // Since we will be copying this texture there is no reason to make it mipped
Greg Danielcc104db2020-02-03 14:17:08 -050056 source = this->refOriginalTextureProxyView(false, AllowedTexGenType::kAny);
Brian Salomonc8d092a2020-02-24 10:14:21 -050057 if (!source) {
58 return {};
59 }
Stan Ilievba81af22017-06-08 15:16:53 -040060 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -040061
Greg Danielcc104db2020-02-03 14:17:08 -050062 SkASSERT(source.asTextureProxy());
Greg Daniel2d59d2f2017-10-31 15:25:14 -040063
Brian Salomonc8d092a2020-02-24 10:14:21 -050064 GrSurfaceProxyView result = MakeMipMappedCopy(this->context(), source, this->colorType());
Stan Ilievba81af22017-06-08 15:16:53 -040065
Brian Salomonc8d092a2020-02-24 10:14:21 -050066 if (!result) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040067 // If we were unable to make a copy and we only needed a copy for mips, then we will return
68 // the source texture here and require that the GPU backend is able to fall back to using
69 // bilerp if mips are required.
Brian Salomonc8d092a2020-02-24 10:14:21 -050070 return source;
Brian Osmane8e54582016-11-28 10:06:27 -050071 }
72
Brian Salomonc8d092a2020-02-24 10:14:21 -050073 if (mipMappedKey.isValid()) {
Greg Danielcc104db2020-02-03 14:17:08 -050074 SkASSERT(result.origin() == origOrigin);
Brian Salomonc8d092a2020-02-24 10:14:21 -050075 proxyProvider->assignUniqueKeyToProxy(mipMappedKey, result.asTextureProxy());
76 this->didCacheMipMappedCopy(mipMappedKey, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050077 }
78 return result;
79}
80
Brian Salomonaff329b2017-08-11 09:40:37 -040081std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
82 const SkMatrix& textureMatrix,
83 const SkRect& constraintRect,
84 FilterConstraint filterConstraint,
85 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -040086 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040087 const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic;
88 if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic &&
Brian Osmane8e54582016-11-28 10:06:27 -050089 kYes_FilterConstraint == filterConstraint) {
Michael Ludwigddeed372019-02-20 16:50:10 -050090 // TODO: Here we should force a copy restricted to the constraintRect since MIP maps will
Brian Osmane8e54582016-11-28 10:06:27 -050091 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
92 // doing that.
93 // We instead we compute the domain as though were bilerping which is only correct if we
94 // only sample level 0.
Brian Salomon2bbdcc42017-09-07 12:36:34 -040095 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -050096 fmForDetermineDomain = &kBilerp;
97 }
98
Brian Salomonc8d092a2020-02-24 10:14:21 -050099 GrSurfaceProxyView view = this->viewForParams(filterOrNullForBicubic);
100 if (!view) {
Brian Osmane8e54582016-11-28 10:06:27 -0500101 return nullptr;
102 }
Michael Ludwigddeed372019-02-20 16:50:10 -0500103
Brian Osmane8e54582016-11-28 10:06:27 -0500104 SkRect domain;
105 DomainMode domainMode =
Brian Salomon4df00922017-09-07 16:34:11 +0000106 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielcc104db2020-02-03 14:17:08 -0500107 view.proxy(), fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500108 SkASSERT(kTightCopy_DomainMode != domainMode);
Michael Ludwigddeed372019-02-20 16:50:10 -0500109 return this->createFragmentProcessorForDomainAndFilter(
Brian Salomonc8d092a2020-02-24 10:14:21 -0500110 std::move(view), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500111}