blob: d303dcf895af845d2bc0622feffd5bde6893f838 [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"
12
13GrTexture* GrTextureMaker::refTextureForParams(const GrSamplerParams& params,
14 SkDestinationSurfaceColorMode colorMode,
15 sk_sp<SkColorSpace>* texColorSpace) {
16 CopyParams copyParams;
17 bool willBeMipped = params.filterMode() == GrSamplerParams::kMipMap_FilterMode;
18
19 if (!fContext->caps()->mipMapSupport()) {
20 willBeMipped = false;
21 }
22
23 if (texColorSpace) {
24 *texColorSpace = this->getColorSpace(colorMode);
25 }
26
27 if (!fContext->getGpu()->makeCopyForTextureParams(this->width(), this->height(), params,
28 &copyParams)) {
29 return this->refOriginalTexture(willBeMipped, colorMode);
30 }
31 GrUniqueKey copyKey;
32 this->makeCopyKey(copyParams, &copyKey, colorMode);
33 if (copyKey.isValid()) {
34 GrTexture* result = fContext->textureProvider()->findAndRefTextureByUniqueKey(copyKey);
35 if (result) {
36 return result;
37 }
38 }
39
40 GrTexture* result = this->generateTextureForParams(copyParams, willBeMipped, colorMode);
41 if (!result) {
42 return nullptr;
43 }
44
45 if (copyKey.isValid()) {
46 fContext->textureProvider()->assignUniqueKeyToTexture(copyKey, result);
47 this->didCacheCopy(copyKey);
48 }
49 return result;
50}
51
52sk_sp<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor(
53 const SkMatrix& textureMatrix,
54 const SkRect& constraintRect,
55 FilterConstraint filterConstraint,
56 bool coordsLimitedToConstraintRect,
57 const GrSamplerParams::FilterMode* filterOrNullForBicubic,
58 SkColorSpace* dstColorSpace,
59 SkDestinationSurfaceColorMode colorMode) {
60
61 const GrSamplerParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic;
62 if (filterOrNullForBicubic && GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic &&
63 kYes_FilterConstraint == filterConstraint) {
64 // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will
65 // read outside the constraint rect. However, as in the adjuster case, we aren't currently
66 // doing that.
67 // We instead we compute the domain as though were bilerping which is only correct if we
68 // only sample level 0.
69 static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode;
70 fmForDetermineDomain = &kBilerp;
71 }
72
73 GrSamplerParams params;
74 if (filterOrNullForBicubic) {
75 params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic);
76 } else {
77 // Bicubic doesn't use filtering for it's texture accesses.
78 params.reset(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
79 }
80 sk_sp<SkColorSpace> texColorSpace;
81 sk_sp<GrTexture> texture(this->refTextureForParams(params, colorMode, &texColorSpace));
82 if (!texture) {
83 return nullptr;
84 }
85 SkRect domain;
86 DomainMode domainMode =
87 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
88 texture->width(), texture->height(), nullptr, fmForDetermineDomain,
89 &domain);
90 SkASSERT(kTightCopy_DomainMode != domainMode);
91 SkMatrix normalizedTextureMatrix = textureMatrix;
92 normalizedTextureMatrix.postIDiv(texture->width(), texture->height());
93 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(texColorSpace.get(),
94 dstColorSpace);
95 return CreateFragmentProcessorForDomainAndFilter(texture.get(), std::move(colorSpaceXform),
96 normalizedTextureMatrix, domainMode, domain,
97 filterOrNullForBicubic);
98}
99
100GrTexture* GrTextureMaker::generateTextureForParams(const CopyParams& copyParams, bool willBeMipped,
101 SkDestinationSurfaceColorMode colorMode) {
102 sk_sp<GrTexture> original(this->refOriginalTexture(willBeMipped, colorMode));
103 if (!original) {
104 return nullptr;
105 }
106 return CopyOnGpu(original.get(), nullptr, copyParams);
107}