blob: b3a16f5d8cbfcf6d09fb60c63cf0866bbd2901b6 [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
Robert Phillips066f0202017-07-25 10:16:35 -040045 GrSurfaceOrigin origOrigin;
Brian Osmane8e54582016-11-28 10:06:27 -050046 GrUniqueKey copyKey;
Brian Osman61624f02016-12-09 14:51:59 -050047 this->makeCopyKey(copyParams, &copyKey, dstColorSpace);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040048 sk_sp<GrTextureProxy> cachedProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050049 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040050 if (original) {
51 origOrigin = original->origin();
52 } else {
53 origOrigin = kTopLeft_GrSurfaceOrigin;
54 }
Greg Daniel2d59d2f2017-10-31 15:25:14 -040055 cachedProxy = fContext->resourceProvider()->findOrCreateProxyByUniqueKey(copyKey,
56 origOrigin);
57 if (cachedProxy && (!willBeMipped || GrMipMapped::kYes == cachedProxy->mipMapped())) {
58 return cachedProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050059 }
60 }
61
Stan Ilievba81af22017-06-08 15:16:53 -040062 sk_sp<GrTextureProxy> result;
63 if (original) {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040064 result = std::move(original);
65 } else if (cachedProxy) {
66 result = cachedProxy;
Stan Ilievba81af22017-06-08 15:16:53 -040067 } else {
Greg Daniel2d59d2f2017-10-31 15:25:14 -040068 // Since we will be copying this texture there is no reason to make it mipped
69 result = this->refOriginalTextureProxy(false, dstColorSpace,
70 AllowedTexGenType::kAny);
Stan Ilievba81af22017-06-08 15:16:53 -040071 }
Greg Daniel2d59d2f2017-10-31 15:25:14 -040072 if (!result) {
73 return nullptr;
74 }
75
Greg Danielc77085d2017-11-01 16:38:48 -040076 result = CopyOnGpu(fContext, std::move(result), copyParams, willBeMipped);
Stan Ilievba81af22017-06-08 15:16:53 -040077
Brian Osmane8e54582016-11-28 10:06:27 -050078 if (!result) {
79 return nullptr;
80 }
81
82 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040083 SkASSERT(result->origin() == origOrigin);
Greg Daniel2d59d2f2017-10-31 15:25:14 -040084 if (cachedProxy) {
85 SkASSERT(GrMipMapped::kYes == result->mipMapped() &&
86 GrMipMapped::kNo == cachedProxy->mipMapped());
87 // If we had a cachedProxy, that means there already is a proxy in the cache which
88 // matches the key, but it does not have mip levels and we require them. Thus we must
89 // remove the unique key from that proxy.
90 fContext->resourceProvider()->removeUniqueKeyFromProxy(copyKey, cachedProxy.get());
91 }
Robert Phillips3798c862017-03-27 11:08:16 -040092 fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get());
Brian Osmane8e54582016-11-28 10:06:27 -050093 this->didCacheCopy(copyKey);
94 }
95 return result;
96}
97
Brian Salomonaff329b2017-08-11 09:40:37 -040098std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
99 const SkMatrix& textureMatrix,
100 const SkRect& constraintRect,
101 FilterConstraint filterConstraint,
102 bool coordsLimitedToConstraintRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400103 const GrSamplerState::Filter* filterOrNullForBicubic,
Brian Salomonaff329b2017-08-11 09:40:37 -0400104 SkColorSpace* dstColorSpace) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400105 const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic;
106 if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic &&
Brian Osmane8e54582016-11-28 10:06:27 -0500107 kYes_FilterConstraint == filterConstraint) {
108 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
109 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
110 // doing that.
111 // We instead we compute the domain as though were bilerping which is only correct if we
112 // only sample level 0.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400113 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500114 fmForDetermineDomain = &kBilerp;
115 }
116
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400117 GrSamplerState samplerState;
Brian Osmane8e54582016-11-28 10:06:27 -0500118 if (filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400119 samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500120 } else {
121 // Bicubic doesn't use filtering for it's texture accesses.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400122 samplerState = GrSamplerState::ClampNearest();
Brian Osmane8e54582016-11-28 10:06:27 -0500123 }
124 sk_sp<SkColorSpace> texColorSpace;
Robert Phillips67c18d62017-01-20 12:44:06 -0500125 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400126 sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(samplerState, dstColorSpace,
127 &texColorSpace, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400128 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500129 return nullptr;
130 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500131 SkMatrix adjustedMatrix = textureMatrix;
132 adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500133 SkRect domain;
134 DomainMode domainMode =
Brian Salomon4df00922017-09-07 16:34:11 +0000135 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielc77085d2017-11-01 16:38:48 -0400136 proxy.get(), fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500137 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Osmanf06ead92017-10-30 13:47:41 -0400138 GrPixelConfig config = proxy->config();
Brian Osman5e341672017-10-18 10:23:18 -0400139 auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), adjustedMatrix,
140 domainMode, domain, filterOrNullForBicubic);
Brian Osmanf06ead92017-10-30 13:47:41 -0400141 return GrColorSpaceXformEffect::Make(std::move(fp), texColorSpace.get(), config, dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -0500142}
143
Robert Phillips3798c862017-03-27 11:08:16 -0400144sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams,
145 bool willBeMipped,
146 SkColorSpace* dstColorSpace) {
Stan Ilievba81af22017-06-08 15:16:53 -0400147 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
148 AllowedTexGenType::kAny));
Brian Osmane8e54582016-11-28 10:06:27 -0500149 if (!original) {
150 return nullptr;
151 }
Robert Phillips4f358be2017-03-23 08:21:00 -0400152
Greg Danielc77085d2017-11-01 16:38:48 -0400153 return CopyOnGpu(fContext, std::move(original), copyParams, willBeMipped);
Brian Osmane8e54582016-11-28 10:06:27 -0500154}