blob: 7e257d8a78494b01aeac0729598bc4ba6f9f2374 [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
Robert Phillips066f0202017-07-25 10:16:35 -040044 GrSurfaceOrigin origOrigin;
Brian Osmane8e54582016-11-28 10:06:27 -050045 GrUniqueKey copyKey;
Brian Osman61624f02016-12-09 14:51:59 -050046 this->makeCopyKey(copyParams, &copyKey, dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -050047 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040048 if (original) {
49 origOrigin = original->origin();
50 } else {
51 origOrigin = kTopLeft_GrSurfaceOrigin;
52 }
Robert Phillips066f0202017-07-25 10:16:35 -040053 sk_sp<GrTextureProxy> result(fContext->resourceProvider()->findProxyByUniqueKey(
54 copyKey, origOrigin));
Brian Osmane8e54582016-11-28 10:06:27 -050055 if (result) {
56 return result;
57 }
58 }
59
Stan Ilievba81af22017-06-08 15:16:53 -040060 sk_sp<GrTextureProxy> result;
61 if (original) {
62 result = CopyOnGpu(fContext, std::move(original), nullptr, copyParams);
63 } else {
64 result = this->generateTextureProxyForParams(copyParams, willBeMipped, dstColorSpace);
65 }
66
Brian Osmane8e54582016-11-28 10:06:27 -050067 if (!result) {
68 return nullptr;
69 }
70
71 if (copyKey.isValid()) {
Robert Phillipsd3e247f2017-07-25 08:00:17 -040072 SkASSERT(result->origin() == origOrigin);
Robert Phillips3798c862017-03-27 11:08:16 -040073 fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get());
Brian Osmane8e54582016-11-28 10:06:27 -050074 this->didCacheCopy(copyKey);
75 }
76 return result;
77}
78
Brian Salomonaff329b2017-08-11 09:40:37 -040079std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
80 const SkMatrix& textureMatrix,
81 const SkRect& constraintRect,
82 FilterConstraint filterConstraint,
83 bool coordsLimitedToConstraintRect,
84 const GrSamplerParams::FilterMode* filterOrNullForBicubic,
85 SkColorSpace* dstColorSpace) {
Brian Osmane8e54582016-11-28 10:06:27 -050086 const GrSamplerParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic;
87 if (filterOrNullForBicubic && GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic &&
88 kYes_FilterConstraint == filterConstraint) {
89 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
90 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
91 // doing that.
92 // We instead we compute the domain as though were bilerping which is only correct if we
93 // only sample level 0.
94 static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode;
95 fmForDetermineDomain = &kBilerp;
96 }
97
98 GrSamplerParams params;
99 if (filterOrNullForBicubic) {
100 params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic);
101 } else {
102 // Bicubic doesn't use filtering for it's texture accesses.
103 params.reset(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
104 }
105 sk_sp<SkColorSpace> texColorSpace;
Robert Phillips67c18d62017-01-20 12:44:06 -0500106 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Robert Phillips3798c862017-03-27 11:08:16 -0400107 sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(params, dstColorSpace,
108 &texColorSpace,
109 scaleAdjust));
110 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 =
117 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Robert Phillips3798c862017-03-27 11:08:16 -0400118 proxy.get(),
Robert Phillipse98234f2017-01-09 14:23:59 -0500119 nullptr, fmForDetermineDomain, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500120 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Osmane8e54582016-11-28 10:06:27 -0500121 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(texColorSpace.get(),
122 dstColorSpace);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400123 return CreateFragmentProcessorForDomainAndFilter(std::move(proxy),
Robert Phillips3798c862017-03-27 11:08:16 -0400124 std::move(colorSpaceXform),
Robert Phillips67c18d62017-01-20 12:44:06 -0500125 adjustedMatrix, domainMode, domain,
Brian Osmane8e54582016-11-28 10:06:27 -0500126 filterOrNullForBicubic);
127}
128
Robert Phillips3798c862017-03-27 11:08:16 -0400129sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams,
130 bool willBeMipped,
131 SkColorSpace* dstColorSpace) {
Stan Ilievba81af22017-06-08 15:16:53 -0400132 sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace,
133 AllowedTexGenType::kAny));
Brian Osmane8e54582016-11-28 10:06:27 -0500134 if (!original) {
135 return nullptr;
136 }
Robert Phillips4f358be2017-03-23 08:21:00 -0400137
Robert Phillips3798c862017-03-27 11:08:16 -0400138 return CopyOnGpu(fContext, std::move(original), nullptr, copyParams);
Brian Osmane8e54582016-11-28 10:06:27 -0500139}