blob: 213a1a89d7ccc7a2516ac521d9f32fd78fd378e5 [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,
Brian Salomon2a943df2018-05-04 13:43:19 -040017 sk_sp<SkColorSpace>* texColorSpace,
Greg Daniel8e9b4c42018-07-20 10:30:48 -040018 bool willBeMipped,
Brian Salomon2a943df2018-05-04 13:43:19 -040019 SkScalar scaleAdjust[2]) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -040020 if (this->width() > fContext->contextPriv().caps()->maxTextureSize() ||
21 this->height() > fContext->contextPriv().caps()->maxTextureSize()) {
Brian Osman875f7852018-04-12 13:29:08 -040022 return nullptr;
23 }
24
Brian Osmane8e54582016-11-28 10:06:27 -050025 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050026
27 if (texColorSpace) {
Brian Osmane7fd8c32018-10-19 13:30:39 -040028 *texColorSpace = this->getColorSpace();
Brian Osmane8e54582016-11-28 10:06:27 -050029 }
30
Brian Osmane7fd8c32018-10-19 13:30:39 -040031 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040032 AllowedTexGenType::kCheap));
Greg Daniel8f5bbda2018-06-08 17:22:23 -040033 bool needsCopyForMipsOnly = false;
Stan Ilievba81af22017-06-08 15:16:53 -040034 if (original) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040035 if (!params.isRepeated() ||
36 !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), original.get(),
37 original->width(), original->height(),
38 params.filter(), &copyParams, scaleAdjust)) {
39 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(fContext->contextPriv().caps(),
40 original.get(), params.filter(),
41 &copyParams);
42 if (!needsCopyForMipsOnly) {
43 return original;
44 }
Stan Ilievba81af22017-06-08 15:16:53 -040045 }
46 } else {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040047 if (!params.isRepeated() ||
48 !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), nullptr,
49 this->width(), this->height(),
50 params.filter(), &copyParams, scaleAdjust)) {
Brian Osmane7fd8c32018-10-19 13:30:39 -040051 return this->refOriginalTextureProxy(willBeMipped, AllowedTexGenType::kAny);
Stan Ilievba81af22017-06-08 15:16:53 -040052 }
Brian Osmane8e54582016-11-28 10:06:27 -050053 }
Stan Ilievba81af22017-06-08 15:16:53 -040054
Robert Phillips1afd4cd2018-01-08 13:40:32 -050055 GrProxyProvider* proxyProvider = fContext->contextPriv().proxyProvider();
56
57 GrSurfaceOrigin origOrigin = original ? original->origin() : kTopLeft_GrSurfaceOrigin;
Brian Osmane8e54582016-11-28 10:06:27 -050058 GrUniqueKey copyKey;
Brian Osmanb3f38302018-09-07 15:24:44 -040059 this->makeCopyKey(copyParams, &copyKey);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040060 sk_sp<GrTextureProxy> cachedProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050061 if (copyKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050062 cachedProxy = proxyProvider->findOrCreateProxyByUniqueKey(copyKey, origOrigin);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040063 if (cachedProxy && (!willBeMipped || GrMipMapped::kYes == cachedProxy->mipMapped())) {
64 return cachedProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050065 }
66 }
67
Greg Daniel8f5bbda2018-06-08 17:22:23 -040068 sk_sp<GrTextureProxy> source;
Stan Ilievba81af22017-06-08 15:16:53 -040069 if (original) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040070 source = std::move(original);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040071 } else if (cachedProxy) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040072 source = cachedProxy;
Stan Ilievba81af22017-06-08 15:16:53 -040073 } else {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040074 // Since we will be copying this texture there is no reason to make it mipped
Brian Osmane7fd8c32018-10-19 13:30:39 -040075 source = this->refOriginalTextureProxy(false, AllowedTexGenType::kAny);
Stan Ilievba81af22017-06-08 15:16:53 -040076 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -040077
78 if (!source) {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040079 return nullptr;
80 }
81
Greg Daniel8f5bbda2018-06-08 17:22:23 -040082 sk_sp<GrTextureProxy> result = CopyOnGpu(fContext, source, copyParams, willBeMipped);
Stan Ilievba81af22017-06-08 15:16:53 -040083
Brian Osmane8e54582016-11-28 10:06:27 -050084 if (!result) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040085 // If we were unable to make a copy and we only needed a copy for mips, then we will return
86 // the source texture here and require that the GPU backend is able to fall back to using
87 // bilerp if mips are required.
88 if (needsCopyForMipsOnly) {
89 return source;
90 }
Brian Osmane8e54582016-11-28 10:06:27 -050091 return nullptr;
92 }
93
94 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040095 SkASSERT(result->origin() == origOrigin);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040096 if (cachedProxy) {
97 SkASSERT(GrMipMapped::kYes == result->mipMapped() &&
98 GrMipMapped::kNo == cachedProxy->mipMapped());
99 // If we had a cachedProxy, that means there already is a proxy in the cache which
100 // matches the key, but it does not have mip levels and we require them. Thus we must
101 // remove the unique key from that proxy.
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500102 proxyProvider->removeUniqueKeyFromProxy(copyKey, cachedProxy.get());
Greg Daniel2d59d2f2017-10-31 15:25:14 -0400103 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500104 proxyProvider->assignUniqueKeyToProxy(copyKey, result.get());
Brian Salomon238069b2018-07-11 15:58:57 -0400105 this->didCacheCopy(copyKey, proxyProvider->contextUniqueID());
Brian Osmane8e54582016-11-28 10:06:27 -0500106 }
107 return result;
108}
109
Brian Salomonaff329b2017-08-11 09:40:37 -0400110std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
111 const SkMatrix& textureMatrix,
112 const SkRect& constraintRect,
113 FilterConstraint filterConstraint,
114 bool coordsLimitedToConstraintRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400115 const GrSamplerState::Filter* filterOrNullForBicubic,
Brian Salomonaff329b2017-08-11 09:40:37 -0400116 SkColorSpace* dstColorSpace) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400117 const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic;
118 if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic &&
Brian Osmane8e54582016-11-28 10:06:27 -0500119 kYes_FilterConstraint == filterConstraint) {
120 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
121 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
122 // doing that.
123 // We instead we compute the domain as though were bilerping which is only correct if we
124 // only sample level 0.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400125 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500126 fmForDetermineDomain = &kBilerp;
127 }
128
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400129 GrSamplerState samplerState;
Brian Osmane8e54582016-11-28 10:06:27 -0500130 if (filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400131 samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500132 } else {
133 // Bicubic doesn't use filtering for it's texture accesses.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400134 samplerState = GrSamplerState::ClampNearest();
Brian Osmane8e54582016-11-28 10:06:27 -0500135 }
136 sk_sp<SkColorSpace> texColorSpace;
Robert Phillips67c18d62017-01-20 12:44:06 -0500137 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Osmane7fd8c32018-10-19 13:30:39 -0400138 sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(samplerState, &texColorSpace,
139 scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400140 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500141 return nullptr;
142 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500143 SkMatrix adjustedMatrix = textureMatrix;
144 adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500145 SkRect domain;
146 DomainMode domainMode =
Brian Salomon4df00922017-09-07 16:34:11 +0000147 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielc77085d2017-11-01 16:38:48 -0400148 proxy.get(), fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500149 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Osman5e341672017-10-18 10:23:18 -0400150 auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), adjustedMatrix,
151 domainMode, domain, filterOrNullForBicubic);
Brian Osman21fc5ce2018-08-27 20:36:19 +0000152 return GrColorSpaceXformEffect::Make(std::move(fp), texColorSpace.get(), this->alphaType(),
153 dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -0500154}