blob: 822d2141cb93223cfd50bdeec09f421d7a45817b [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);
Brian Osmane8e54582016-11-28 10:06:27 -050048 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040049 if (original) {
50 origOrigin = original->origin();
51 } else {
52 origOrigin = kTopLeft_GrSurfaceOrigin;
53 }
Greg Danielcd871402017-09-26 12:49:26 -040054 sk_sp<GrTextureProxy> result(fContext->resourceProvider()->findOrCreateProxyByUniqueKey(
Robert Phillips066f0202017-07-25 10:16:35 -040055 copyKey, origOrigin));
Brian Osmane8e54582016-11-28 10:06:27 -050056 if (result) {
57 return result;
58 }
59 }
60
Stan Ilievba81af22017-06-08 15:16:53 -040061 sk_sp<GrTextureProxy> result;
62 if (original) {
Greg Daniele1da1d92017-10-06 15:59:27 -040063 result = CopyOnGpu(fContext, std::move(original), nullptr, copyParams, willBeMipped);
Stan Ilievba81af22017-06-08 15:16:53 -040064 } else {
65 result = this->generateTextureProxyForParams(copyParams, willBeMipped, dstColorSpace);
66 }
67
Brian Osmane8e54582016-11-28 10:06:27 -050068 if (!result) {
69 return nullptr;
70 }
71
72 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040073 SkASSERT(result->origin() == origOrigin);
Robert Phillips3798c862017-03-27 11:08:16 -040074 fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get());
Brian Osmane8e54582016-11-28 10:06:27 -050075 this->didCacheCopy(copyKey);
76 }
77 return result;
78}
79
Brian Salomonaff329b2017-08-11 09:40:37 -040080std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
81 const SkMatrix& textureMatrix,
82 const SkRect& constraintRect,
83 FilterConstraint filterConstraint,
84 bool coordsLimitedToConstraintRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040085 const GrSamplerState::Filter* filterOrNullForBicubic,
Brian Salomonaff329b2017-08-11 09:40:37 -040086 SkColorSpace* dstColorSpace) {
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) {
90 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
91 // 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 Salomon2bbdcc42017-09-07 12:36:34 -040099 GrSamplerState samplerState;
Brian Osmane8e54582016-11-28 10:06:27 -0500100 if (filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400101 samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500102 } else {
103 // Bicubic doesn't use filtering for it's texture accesses.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400104 samplerState = GrSamplerState::ClampNearest();
Brian Osmane8e54582016-11-28 10:06:27 -0500105 }
106 sk_sp<SkColorSpace> texColorSpace;
Robert Phillips67c18d62017-01-20 12:44:06 -0500107 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400108 sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(samplerState, dstColorSpace,
109 &texColorSpace, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400110 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500111 return nullptr;
112 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500113 SkMatrix adjustedMatrix = textureMatrix;
114 adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500115 SkRect domain;
116 DomainMode domainMode =
Brian Salomon4df00922017-09-07 16:34:11 +0000117 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
118 proxy.get(),
119 nullptr, fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500120 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Osman5e341672017-10-18 10:23:18 -0400121 auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), adjustedMatrix,
122 domainMode, domain, filterOrNullForBicubic);
123 return GrColorSpaceXformEffect::Make(std::move(fp), texColorSpace.get(), dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -0500124}
125
Robert Phillips3798c862017-03-27 11:08:16 -0400126sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams,
127 bool willBeMipped,
128 SkColorSpace* dstColorSpace) {
Stan Ilievba81af22017-06-08 15:16:53 -0400129 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
130 AllowedTexGenType::kAny));
Brian Osmane8e54582016-11-28 10:06:27 -0500131 if (!original) {
132 return nullptr;
133 }
Robert Phillips4f358be2017-03-23 08:21:00 -0400134
Greg Daniele1da1d92017-10-06 15:59:27 -0400135 return CopyOnGpu(fContext, std::move(original), nullptr, copyParams, willBeMipped);
Brian Osmane8e54582016-11-28 10:06:27 -0500136}