blob: 6cb9832b59fb4eb302dd5dec910d6aebf57a4997 [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"
12#include "GrGpu.h"
Brian Osman32342f02017-03-04 08:12:46 -050013#include "GrResourceProvider.h"
Brian Osmane8e54582016-11-28 10:06:27 -050014
Brian Salomon2bbdcc42017-09-07 12:36:34 -040015sk_sp<GrTextureProxy> GrTextureMaker::refTextureProxyForParams(const GrSamplerState& params,
Robert Phillips3798c862017-03-27 11:08:16 -040016 SkColorSpace* dstColorSpace,
17 sk_sp<SkColorSpace>* texColorSpace,
18 SkScalar scaleAdjust[2]) {
Brian Osmane8e54582016-11-28 10:06:27 -050019 CopyParams copyParams;
Brian Salomon2bbdcc42017-09-07 12:36:34 -040020 bool willBeMipped = params.filter() == GrSamplerState::Filter::kMipMap;
Brian Osmane8e54582016-11-28 10:06:27 -050021
22 if (!fContext->caps()->mipMapSupport()) {
23 willBeMipped = false;
24 }
25
26 if (texColorSpace) {
Brian Osman61624f02016-12-09 14:51:59 -050027 *texColorSpace = this->getColorSpace(dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -050028 }
29
Stan Ilievba81af22017-06-08 15:16:53 -040030 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
31 AllowedTexGenType::kCheap));
32 if (original) {
33 if (!fContext->getGpu()->isACopyNeededForTextureParams(original.get(), params, &copyParams,
34 scaleAdjust)) {
35 return original;
36 }
37 } else {
38 if (!fContext->getGpu()->isACopyNeededForTextureParams(this->width(), this->height(),
39 params, &copyParams, scaleAdjust)) {
40 return this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
41 AllowedTexGenType::kAny);
42 }
Brian Osmane8e54582016-11-28 10:06:27 -050043 }
Stan Ilievba81af22017-06-08 15:16:53 -040044
Kevin Lubick42846132018-01-05 10:11:11 -050045 GrSurfaceOrigin origOrigin = original ? original->origin()
46 : kTopLeft_GrSurfaceOrigin;
Brian Osmane8e54582016-11-28 10:06:27 -050047 GrUniqueKey copyKey;
Brian Osman61624f02016-12-09 14:51:59 -050048 this->makeCopyKey(copyParams, &copyKey, dstColorSpace);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040049 sk_sp<GrTextureProxy> cachedProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050050 if (copyKey.isValid()) {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040051 cachedProxy = fContext->resourceProvider()->findOrCreateProxyByUniqueKey(copyKey,
52 origOrigin);
53 if (cachedProxy && (!willBeMipped || GrMipMapped::kYes == cachedProxy->mipMapped())) {
54 return cachedProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050055 }
56 }
57
Stan Ilievba81af22017-06-08 15:16:53 -040058 sk_sp<GrTextureProxy> result;
59 if (original) {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040060 result = std::move(original);
61 } else if (cachedProxy) {
62 result = cachedProxy;
Stan Ilievba81af22017-06-08 15:16:53 -040063 } else {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040064 // Since we will be copying this texture there is no reason to make it mipped
65 result = this->refOriginalTextureProxy(false, dstColorSpace,
66 AllowedTexGenType::kAny);
Stan Ilievba81af22017-06-08 15:16:53 -040067 }
Greg Daniel2d59d2f2017-10-31 15:25:14 -040068 if (!result) {
69 return nullptr;
70 }
71
Greg Danielc77085d2017-11-01 16:38:48 -040072 result = CopyOnGpu(fContext, std::move(result), copyParams, willBeMipped);
Stan Ilievba81af22017-06-08 15:16:53 -040073
Brian Osmane8e54582016-11-28 10:06:27 -050074 if (!result) {
75 return nullptr;
76 }
77
78 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040079 SkASSERT(result->origin() == origOrigin);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040080 if (cachedProxy) {
81 SkASSERT(GrMipMapped::kYes == result->mipMapped() &&
82 GrMipMapped::kNo == cachedProxy->mipMapped());
83 // If we had a cachedProxy, that means there already is a proxy in the cache which
84 // matches the key, but it does not have mip levels and we require them. Thus we must
85 // remove the unique key from that proxy.
86 fContext->resourceProvider()->removeUniqueKeyFromProxy(copyKey, cachedProxy.get());
87 }
Robert Phillips3798c862017-03-27 11:08:16 -040088 fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get());
Brian Osmane8e54582016-11-28 10:06:27 -050089 this->didCacheCopy(copyKey);
90 }
91 return result;
92}
93
Brian Salomonaff329b2017-08-11 09:40:37 -040094std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
95 const SkMatrix& textureMatrix,
96 const SkRect& constraintRect,
97 FilterConstraint filterConstraint,
98 bool coordsLimitedToConstraintRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040099 const GrSamplerState::Filter* filterOrNullForBicubic,
Brian Salomonaff329b2017-08-11 09:40:37 -0400100 SkColorSpace* dstColorSpace) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400101 const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic;
102 if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic &&
Brian Osmane8e54582016-11-28 10:06:27 -0500103 kYes_FilterConstraint == filterConstraint) {
104 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
105 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
106 // doing that.
107 // We instead we compute the domain as though were bilerping which is only correct if we
108 // only sample level 0.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400109 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500110 fmForDetermineDomain = &kBilerp;
111 }
112
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400113 GrSamplerState samplerState;
Brian Osmane8e54582016-11-28 10:06:27 -0500114 if (filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400115 samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500116 } else {
117 // Bicubic doesn't use filtering for it's texture accesses.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400118 samplerState = GrSamplerState::ClampNearest();
Brian Osmane8e54582016-11-28 10:06:27 -0500119 }
120 sk_sp<SkColorSpace> texColorSpace;
Robert Phillips67c18d62017-01-20 12:44:06 -0500121 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400122 sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(samplerState, dstColorSpace,
123 &texColorSpace, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400124 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500125 return nullptr;
126 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500127 SkMatrix adjustedMatrix = textureMatrix;
128 adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500129 SkRect domain;
130 DomainMode domainMode =
Brian Salomon4df00922017-09-07 16:34:11 +0000131 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielc77085d2017-11-01 16:38:48 -0400132 proxy.get(), fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500133 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Osmanf06ead92017-10-30 13:47:41 -0400134 GrPixelConfig config = proxy->config();
Brian Osman5e341672017-10-18 10:23:18 -0400135 auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), adjustedMatrix,
136 domainMode, domain, filterOrNullForBicubic);
Brian Osmanf06ead92017-10-30 13:47:41 -0400137 return GrColorSpaceXformEffect::Make(std::move(fp), texColorSpace.get(), config, dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -0500138}
139
Robert Phillips3798c862017-03-27 11:08:16 -0400140sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams,
141 bool willBeMipped,
142 SkColorSpace* dstColorSpace) {
Stan Ilievba81af22017-06-08 15:16:53 -0400143 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
144 AllowedTexGenType::kAny));
Brian Osmane8e54582016-11-28 10:06:27 -0500145 if (!original) {
146 return nullptr;
147 }
Robert Phillips4f358be2017-03-23 08:21:00 -0400148
Greg Danielc77085d2017-11-01 16:38:48 -0400149 return CopyOnGpu(fContext, std::move(original), copyParams, willBeMipped);
Brian Osmane8e54582016-11-28 10:06:27 -0500150}