blob: 9b3a4c56e25f2aac706c58d8b73d870731e64935 [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 Salomonadc9bbb2020-02-20 20:10:31 -050015#include "src/gpu/SkGr.h"
Brian Osmane8e54582016-11-28 10:06:27 -050016
Greg Danielcc104db2020-02-03 14:17:08 -050017GrSurfaceProxyView GrTextureMaker::onRefTextureProxyViewForParams(GrSamplerState params,
Brian Salomonc8d092a2020-02-24 10:14:21 -050018 bool willBeMipped) {
Robert Phillips9338c602019-02-19 12:52:29 -050019 if (this->width() > this->context()->priv().caps()->maxTextureSize() ||
20 this->height() > this->context()->priv().caps()->maxTextureSize()) {
Greg Danielcc104db2020-02-03 14:17:08 -050021 return {};
Brian Osman875f7852018-04-12 13:29:08 -040022 }
23
Greg Danielcc104db2020-02-03 14:17:08 -050024 GrSurfaceProxyView original = this->refOriginalTextureProxyView(willBeMipped,
25 AllowedTexGenType::kCheap);
Brian Salomonc8d092a2020-02-24 10:14:21 -050026 if (!original) {
27 return this->refOriginalTextureProxyView(willBeMipped, AllowedTexGenType::kAny);
28 }
29
30 GrTextureProxy* texProxy = original.asTextureProxy();
31 if (!GrGpu::IsACopyNeededForMips(this->context()->priv().caps(), texProxy, params.filter())) {
32 return original;
Brian Osmane8e54582016-11-28 10:06:27 -050033 }
Stan Ilievba81af22017-06-08 15:16:53 -040034
Robert Phillips9338c602019-02-19 12:52:29 -050035 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050036
Greg Danielcc104db2020-02-03 14:17:08 -050037 GrSurfaceOrigin origOrigin = original.proxy() ? original.origin() : kTopLeft_GrSurfaceOrigin;
Brian Salomonc8d092a2020-02-24 10:14:21 -050038 GrUniqueKey mipMappedKey;
39 this->makeMipMappedKey(&mipMappedKey);
40 if (mipMappedKey.isValid()) {
41 auto cachedProxy =
42 proxyProvider->findOrCreateProxyByUniqueKey(mipMappedKey, this->colorType());
Greg Danielcc104db2020-02-03 14:17:08 -050043 if (cachedProxy) {
Brian Salomonc8d092a2020-02-24 10:14:21 -050044 SkASSERT(cachedProxy->mipMapped() == GrMipMapped::kYes);
Brian Salomonadc9bbb2020-02-20 20:10:31 -050045 return GrSurfaceProxyView(std::move(cachedProxy), origOrigin, original.swizzle());
Brian Osmane8e54582016-11-28 10:06:27 -050046 }
47 }
48
Greg Danielcc104db2020-02-03 14:17:08 -050049 GrSurfaceProxyView source;
Brian Salomonc8d092a2020-02-24 10:14:21 -050050 if (original) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040051 source = std::move(original);
Stan Ilievba81af22017-06-08 15:16:53 -040052 } else {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040053 // Since we will be copying this texture there is no reason to make it mipped
Greg Danielcc104db2020-02-03 14:17:08 -050054 source = this->refOriginalTextureProxyView(false, AllowedTexGenType::kAny);
Brian Salomonc8d092a2020-02-24 10:14:21 -050055 if (!source) {
56 return {};
57 }
Stan Ilievba81af22017-06-08 15:16:53 -040058 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -040059
Greg Danielcc104db2020-02-03 14:17:08 -050060 SkASSERT(source.asTextureProxy());
Greg Daniel2d59d2f2017-10-31 15:25:14 -040061
Brian Salomonadc9bbb2020-02-20 20:10:31 -050062 GrSurfaceProxyView result = GrCopyBaseMipMapToTextureProxy(this->context(), source.proxy(),
63 source.origin(), this->colorType());
Brian Salomonc8d092a2020-02-24 10:14:21 -050064 if (!result) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040065 // If we were unable to make a copy and we only needed a copy for mips, then we will return
66 // the source texture here and require that the GPU backend is able to fall back to using
67 // bilerp if mips are required.
Brian Salomonc8d092a2020-02-24 10:14:21 -050068 return source;
Brian Osmane8e54582016-11-28 10:06:27 -050069 }
70
Brian Salomonc8d092a2020-02-24 10:14:21 -050071 if (mipMappedKey.isValid()) {
Greg Danielcc104db2020-02-03 14:17:08 -050072 SkASSERT(result.origin() == origOrigin);
Brian Salomonc8d092a2020-02-24 10:14:21 -050073 proxyProvider->assignUniqueKeyToProxy(mipMappedKey, result.asTextureProxy());
74 this->didCacheMipMappedCopy(mipMappedKey, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050075 }
76 return result;
77}
78
Brian Salomonaff329b2017-08-11 09:40:37 -040079std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
80 const SkMatrix& textureMatrix,
81 const SkRect& constraintRect,
82 FilterConstraint filterConstraint,
83 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -040084 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040085 const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic;
86 if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic &&
Brian Osmane8e54582016-11-28 10:06:27 -050087 kYes_FilterConstraint == filterConstraint) {
Michael Ludwigddeed372019-02-20 16:50:10 -050088 // TODO: Here we should force a copy restricted to the constraintRect since MIP maps will
Brian Osmane8e54582016-11-28 10:06:27 -050089 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
90 // doing that.
91 // We instead we compute the domain as though were bilerping which is only correct if we
92 // only sample level 0.
Brian Salomon2bbdcc42017-09-07 12:36:34 -040093 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -050094 fmForDetermineDomain = &kBilerp;
95 }
96
Brian Salomonc8d092a2020-02-24 10:14:21 -050097 GrSurfaceProxyView view = this->viewForParams(filterOrNullForBicubic);
98 if (!view) {
Brian Osmane8e54582016-11-28 10:06:27 -050099 return nullptr;
100 }
Michael Ludwigddeed372019-02-20 16:50:10 -0500101
Brian Osmane8e54582016-11-28 10:06:27 -0500102 SkRect domain;
103 DomainMode domainMode =
Brian Salomon4df00922017-09-07 16:34:11 +0000104 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielcc104db2020-02-03 14:17:08 -0500105 view.proxy(), fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500106 SkASSERT(kTightCopy_DomainMode != domainMode);
Michael Ludwigddeed372019-02-20 16:50:10 -0500107 return this->createFragmentProcessorForDomainAndFilter(
Brian Salomonc8d092a2020-02-24 10:14:21 -0500108 std::move(view), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500109}