blob: 69e3a6e060b40cf9327dc2a0c0d4e866e0d8a537 [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
Stan Ilievba81af22017-06-08 15:16:53 -040029 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
30 AllowedTexGenType::kCheap));
31 if (original) {
32 if (!fContext->getGpu()->isACopyNeededForTextureParams(original.get(), params, &copyParams,
33 scaleAdjust)) {
34 return original;
35 }
36 } else {
37 if (!fContext->getGpu()->isACopyNeededForTextureParams(this->width(), this->height(),
38 params, &copyParams, scaleAdjust)) {
39 return this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
40 AllowedTexGenType::kAny);
41 }
Brian Osmane8e54582016-11-28 10:06:27 -050042 }
Stan Ilievba81af22017-06-08 15:16:53 -040043
Brian Osmane8e54582016-11-28 10:06:27 -050044 GrUniqueKey copyKey;
Brian Osman61624f02016-12-09 14:51:59 -050045 this->makeCopyKey(copyParams, &copyKey, dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -050046 if (copyKey.isValid()) {
Robert Phillips3798c862017-03-27 11:08:16 -040047 sk_sp<GrTextureProxy> result(fContext->resourceProvider()->findProxyByUniqueKey(copyKey));
Brian Osmane8e54582016-11-28 10:06:27 -050048 if (result) {
49 return result;
50 }
51 }
52
Stan Ilievba81af22017-06-08 15:16:53 -040053 sk_sp<GrTextureProxy> result;
54 if (original) {
55 result = CopyOnGpu(fContext, std::move(original), nullptr, copyParams);
56 } else {
57 result = this->generateTextureProxyForParams(copyParams, willBeMipped, dstColorSpace);
58 }
59
Brian Osmane8e54582016-11-28 10:06:27 -050060 if (!result) {
61 return nullptr;
62 }
63
64 if (copyKey.isValid()) {
Robert Phillips3798c862017-03-27 11:08:16 -040065 fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get());
Brian Osmane8e54582016-11-28 10:06:27 -050066 this->didCacheCopy(copyKey);
67 }
68 return result;
69}
70
71sk_sp<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
72 const SkMatrix& textureMatrix,
73 const SkRect& constraintRect,
74 FilterConstraint filterConstraint,
75 bool coordsLimitedToConstraintRect,
76 const GrSamplerParams::FilterMode* filterOrNullForBicubic,
Brian Osman61624f02016-12-09 14:51:59 -050077 SkColorSpace* dstColorSpace) {
Brian Osmane8e54582016-11-28 10:06:27 -050078
79 const GrSamplerParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic;
80 if (filterOrNullForBicubic && GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic &&
81 kYes_FilterConstraint == filterConstraint) {
82 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
83 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
84 // doing that.
85 // We instead we compute the domain as though were bilerping which is only correct if we
86 // only sample level 0.
87 static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode;
88 fmForDetermineDomain = &kBilerp;
89 }
90
91 GrSamplerParams params;
92 if (filterOrNullForBicubic) {
93 params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic);
94 } else {
95 // Bicubic doesn't use filtering for it's texture accesses.
96 params.reset(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
97 }
98 sk_sp<SkColorSpace> texColorSpace;
Robert Phillips67c18d62017-01-20 12:44:06 -050099 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Robert Phillips3798c862017-03-27 11:08:16 -0400100 sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(params, dstColorSpace,
101 &texColorSpace,
102 scaleAdjust));
103 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500104 return nullptr;
105 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500106 SkMatrix adjustedMatrix = textureMatrix;
107 adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500108 SkRect domain;
109 DomainMode domainMode =
110 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Robert Phillips3798c862017-03-27 11:08:16 -0400111 proxy.get(),
Robert Phillipse98234f2017-01-09 14:23:59 -0500112 nullptr, fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500113 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Osmane8e54582016-11-28 10:06:27 -0500114 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(texColorSpace.get(),
115 dstColorSpace);
Robert Phillips3798c862017-03-27 11:08:16 -0400116 return CreateFragmentProcessorForDomainAndFilter(fContext->resourceProvider(), std::move(proxy),
117 std::move(colorSpaceXform),
Robert Phillips67c18d62017-01-20 12:44:06 -0500118 adjustedMatrix, domainMode, domain,
Brian Osmane8e54582016-11-28 10:06:27 -0500119 filterOrNullForBicubic);
120}
121
Robert Phillips3798c862017-03-27 11:08:16 -0400122sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams,
123 bool willBeMipped,
124 SkColorSpace* dstColorSpace) {
Stan Ilievba81af22017-06-08 15:16:53 -0400125 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
126 AllowedTexGenType::kAny));
Brian Osmane8e54582016-11-28 10:06:27 -0500127 if (!original) {
128 return nullptr;
129 }
Robert Phillips4f358be2017-03-23 08:21:00 -0400130
Robert Phillips3798c862017-03-27 11:08:16 -0400131 return CopyOnGpu(fContext, std::move(original), nullptr, copyParams);
Brian Osmane8e54582016-11-28 10:06:27 -0500132}