blob: 4fb0c0db41e865a5ba3d262b69441bdffaaa1bfa [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
10#include "GrContext.h"
11#include "GrGpu.h"
Brian Osman32342f02017-03-04 08:12:46 -050012#include "GrResourceProvider.h"
Brian Osmane8e54582016-11-28 10:06:27 -050013
Robert Phillips3798c862017-03-27 11:08:16 -040014sk_sp<GrTextureProxy> GrTextureMaker::refTextureProxyForParams(const GrSamplerParams& params,
15 SkColorSpace* dstColorSpace,
16 sk_sp<SkColorSpace>* texColorSpace,
17 SkScalar scaleAdjust[2]) {
Brian Osmane8e54582016-11-28 10:06:27 -050018 CopyParams copyParams;
19 bool willBeMipped = params.filterMode() == GrSamplerParams::kMipMap_FilterMode;
20
21 if (!fContext->caps()->mipMapSupport()) {
22 willBeMipped = false;
23 }
24
25 if (texColorSpace) {
Brian Osman61624f02016-12-09 14:51:59 -050026 *texColorSpace = this->getColorSpace(dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -050027 }
28
Robert Phillips81444fb2017-03-21 09:14:35 -040029 if (!fContext->getGpu()->isACopyNeededForTextureParams(this->width(), this->height(), params,
30 &copyParams, scaleAdjust)) {
Robert Phillips3798c862017-03-27 11:08:16 -040031 return this->refOriginalTextureProxy(willBeMipped, dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -050032 }
33 GrUniqueKey copyKey;
Brian Osman61624f02016-12-09 14:51:59 -050034 this->makeCopyKey(copyParams, &copyKey, dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -050035 if (copyKey.isValid()) {
Robert Phillips3798c862017-03-27 11:08:16 -040036 sk_sp<GrTextureProxy> result(fContext->resourceProvider()->findProxyByUniqueKey(copyKey));
Brian Osmane8e54582016-11-28 10:06:27 -050037 if (result) {
38 return result;
39 }
40 }
41
Robert Phillips3798c862017-03-27 11:08:16 -040042 sk_sp<GrTextureProxy> result(this->generateTextureProxyForParams(copyParams, willBeMipped,
43 dstColorSpace));
Brian Osmane8e54582016-11-28 10:06:27 -050044 if (!result) {
45 return nullptr;
46 }
47
48 if (copyKey.isValid()) {
Robert Phillips3798c862017-03-27 11:08:16 -040049 fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get());
Brian Osmane8e54582016-11-28 10:06:27 -050050 this->didCacheCopy(copyKey);
51 }
52 return result;
53}
54
55sk_sp<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
56 const SkMatrix& textureMatrix,
57 const SkRect& constraintRect,
58 FilterConstraint filterConstraint,
59 bool coordsLimitedToConstraintRect,
60 const GrSamplerParams::FilterMode* filterOrNullForBicubic,
Brian Osman61624f02016-12-09 14:51:59 -050061 SkColorSpace* dstColorSpace) {
Brian Osmane8e54582016-11-28 10:06:27 -050062
63 const GrSamplerParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic;
64 if (filterOrNullForBicubic && GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic &&
65 kYes_FilterConstraint == filterConstraint) {
66 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
67 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
68 // doing that.
69 // We instead we compute the domain as though were bilerping which is only correct if we
70 // only sample level 0.
71 static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode;
72 fmForDetermineDomain = &kBilerp;
73 }
74
75 GrSamplerParams params;
76 if (filterOrNullForBicubic) {
77 params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic);
78 } else {
79 // Bicubic doesn't use filtering for it's texture accesses.
80 params.reset(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
81 }
82 sk_sp<SkColorSpace> texColorSpace;
Robert Phillips67c18d62017-01-20 12:44:06 -050083 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Robert Phillips3798c862017-03-27 11:08:16 -040084 sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(params, dstColorSpace,
85 &texColorSpace,
86 scaleAdjust));
87 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -050088 return nullptr;
89 }
Robert Phillips67c18d62017-01-20 12:44:06 -050090 SkMatrix adjustedMatrix = textureMatrix;
91 adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -050092 SkRect domain;
93 DomainMode domainMode =
94 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Robert Phillips3798c862017-03-27 11:08:16 -040095 proxy.get(),
Robert Phillipse98234f2017-01-09 14:23:59 -050096 nullptr, fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -050097 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Osmane8e54582016-11-28 10:06:27 -050098 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(texColorSpace.get(),
99 dstColorSpace);
Robert Phillips3798c862017-03-27 11:08:16 -0400100 return CreateFragmentProcessorForDomainAndFilter(fContext->resourceProvider(), std::move(proxy),
101 std::move(colorSpaceXform),
Robert Phillips67c18d62017-01-20 12:44:06 -0500102 adjustedMatrix, domainMode, domain,
Brian Osmane8e54582016-11-28 10:06:27 -0500103 filterOrNullForBicubic);
104}
105
Robert Phillips3798c862017-03-27 11:08:16 -0400106sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams,
107 bool willBeMipped,
108 SkColorSpace* dstColorSpace) {
Robert Phillips4f358be2017-03-23 08:21:00 -0400109 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace));
Brian Osmane8e54582016-11-28 10:06:27 -0500110 if (!original) {
111 return nullptr;
112 }
Robert Phillips4f358be2017-03-23 08:21:00 -0400113
Robert Phillips3798c862017-03-27 11:08:16 -0400114 return CopyOnGpu(fContext, std::move(original), nullptr, copyParams);
Brian Osmane8e54582016-11-28 10:06:27 -0500115}