blob: e06d94f806080dce09f99bc91346cca833c25fd2 [file] [log] [blame]
Brian Salomon34169692017-08-28 15:32:01 -04001/*
2 * Copyright 2017 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 "GrTextureOp.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Brian Salomon34169692017-08-28 15:32:01 -040010#include "GrAppliedClip.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040011#include "GrCaps.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040012#include "GrContext.h"
13#include "GrContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -040014#include "GrDrawOpTest.h"
15#include "GrGeometryProcessor.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040016#include "GrMemoryPool.h"
Brian Salomon34169692017-08-28 15:32:01 -040017#include "GrMeshDrawOp.h"
18#include "GrOpFlushState.h"
19#include "GrQuad.h"
20#include "GrResourceProvider.h"
21#include "GrShaderCaps.h"
22#include "GrTexture.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040023#include "GrTexturePriv.h"
Brian Salomon34169692017-08-28 15:32:01 -040024#include "GrTextureProxy.h"
25#include "SkGr.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040026#include "SkMathPriv.h"
Brian Salomona33b67c2018-05-17 10:42:14 -040027#include "SkMatrixPriv.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050028#include "SkPoint.h"
29#include "SkPoint3.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040030#include "SkTo.h"
Brian Salomon34169692017-08-28 15:32:01 -040031#include "glsl/GrGLSLColorSpaceXformHelper.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050032#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon34169692017-08-28 15:32:01 -040033#include "glsl/GrGLSLGeometryProcessor.h"
34#include "glsl/GrGLSLVarying.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050035#include "glsl/GrGLSLVertexGeoBuilder.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040036#include <new>
Brian Salomon34169692017-08-28 15:32:01 -040037
38namespace {
39
Brian Salomon17031a72018-05-22 14:14:07 -040040enum class MultiTexture : bool { kNo = false, kYes = true };
41
Brian Salomonb80ffee2018-05-23 16:39:39 -040042enum class Domain : bool { kNo = false, kYes = true };
43
Brian Salomon34169692017-08-28 15:32:01 -040044/**
45 * Geometry Processor that draws a texture modulated by a vertex color (though, this is meant to be
46 * the same value across all vertices of a quad and uses flat interpolation when available). This is
47 * used by TextureOp below.
48 */
49class TextureGeometryProcessor : public GrGeometryProcessor {
50public:
Brian Salomon17031a72018-05-22 14:14:07 -040051 template <typename Pos> struct VertexCommon {
52 using Position = Pos;
53 Position fPosition;
Brian Salomon34169692017-08-28 15:32:01 -040054 GrColor fColor;
Brian Salomon17031a72018-05-22 14:14:07 -040055 SkPoint fTextureCoords;
Brian Salomon34169692017-08-28 15:32:01 -040056 };
Brian Salomon17031a72018-05-22 14:14:07 -040057
58 template <typename Pos, MultiTexture MT> struct OptionalMultiTextureVertex;
59 template <typename Pos>
60 struct OptionalMultiTextureVertex<Pos, MultiTexture::kNo> : VertexCommon<Pos> {
61 static constexpr MultiTexture kMultiTexture = MultiTexture::kNo;
Brian Salomonb5ef1f92018-01-11 11:46:21 -050062 };
Brian Salomon17031a72018-05-22 14:14:07 -040063 template <typename Pos>
64 struct OptionalMultiTextureVertex<Pos, MultiTexture::kYes> : VertexCommon<Pos> {
65 static constexpr MultiTexture kMultiTexture = MultiTexture::kYes;
Brian Salomon336ce7b2017-09-08 08:23:58 -040066 int fTextureIdx;
Brian Salomon336ce7b2017-09-08 08:23:58 -040067 };
Brian Salomon17031a72018-05-22 14:14:07 -040068
Brian Salomonb80ffee2018-05-23 16:39:39 -040069 template <typename Pos, MultiTexture MT, Domain D> struct OptionalDomainVertex;
Brian Salomon17031a72018-05-22 14:14:07 -040070 template <typename Pos, MultiTexture MT>
Brian Salomonb80ffee2018-05-23 16:39:39 -040071 struct OptionalDomainVertex<Pos, MT, Domain::kNo> : OptionalMultiTextureVertex<Pos, MT> {
72 static constexpr Domain kDomain = Domain::kNo;
Brian Salomona0047bc2018-05-23 16:39:39 -040073 };
Stephen White633f20e2018-05-26 18:07:27 +000074 template <typename Pos, MultiTexture MT>
Brian Salomonb80ffee2018-05-23 16:39:39 -040075 struct OptionalDomainVertex<Pos, MT, Domain::kYes> : OptionalMultiTextureVertex<Pos, MT> {
76 static constexpr Domain kDomain = Domain::kYes;
77 SkRect fTextureDomain;
78 };
79
80 template <typename Pos, MultiTexture MT, Domain D, GrAA> struct OptionalAAVertex;
81 template <typename Pos, MultiTexture MT, Domain D>
82 struct OptionalAAVertex<Pos, MT, D, GrAA::kNo> : OptionalDomainVertex<Pos, MT, D> {
83 static constexpr GrAA kAA = GrAA::kNo;
84 };
85 template <typename Pos, MultiTexture MT, Domain D>
86 struct OptionalAAVertex<Pos, MT, D, GrAA::kYes> : OptionalDomainVertex<Pos, MT, D> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -040087 static constexpr GrAA kAA = GrAA::kYes;
Brian Salomonb5ef1f92018-01-11 11:46:21 -050088 SkPoint3 fEdges[4];
Brian Salomonb5ef1f92018-01-11 11:46:21 -050089 };
Brian Salomon336ce7b2017-09-08 08:23:58 -040090
Brian Salomonb80ffee2018-05-23 16:39:39 -040091 template <typename Pos, MultiTexture MT, Domain D, GrAA AA>
92 using Vertex = OptionalAAVertex<Pos, MT, D, AA>;
Brian Salomon17031a72018-05-22 14:14:07 -040093
Brian Salomon336ce7b2017-09-08 08:23:58 -040094 // Maximum number of textures supported by this op. Must also be checked against the caps
95 // limit. These numbers were based on some limited experiments on a HP Z840 and Pixel XL 2016
96 // and could probably use more tuning.
97#ifdef SK_BUILD_FOR_ANDROID
98 static constexpr int kMaxTextures = 4;
99#else
100 static constexpr int kMaxTextures = 8;
101#endif
102
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400103 static int SupportsMultitexture(const GrShaderCaps& caps) {
Brian Salomon762d5e72017-12-01 10:25:08 -0500104 return caps.integerSupport() && caps.maxFragmentSamplers() > 1;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400105 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400106
107 static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxies[], int proxyCnt,
Brian Salomon485b8c62018-01-12 15:11:06 -0500108 sk_sp<GrColorSpaceXform> csxf, bool coverageAA,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400109 bool perspective, Domain domain,
110 const GrSamplerState::Filter filters[],
Brian Salomon336ce7b2017-09-08 08:23:58 -0400111 const GrShaderCaps& caps) {
112 // We use placement new to avoid always allocating space for kMaxTextures TextureSampler
113 // instances.
114 int samplerCnt = NumSamplersToUse(proxyCnt, caps);
115 size_t size = sizeof(TextureGeometryProcessor) + sizeof(TextureSampler) * (samplerCnt - 1);
116 void* mem = GrGeometryProcessor::operator new(size);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400117 return sk_sp<TextureGeometryProcessor>(
118 new (mem) TextureGeometryProcessor(proxies, proxyCnt, samplerCnt, std::move(csxf),
Brian Salomonb80ffee2018-05-23 16:39:39 -0400119 coverageAA, perspective, domain, filters, caps));
Brian Salomon336ce7b2017-09-08 08:23:58 -0400120 }
121
122 ~TextureGeometryProcessor() override {
123 int cnt = this->numTextureSamplers();
124 for (int i = 1; i < cnt; ++i) {
125 fSamplers[i].~TextureSampler();
126 }
Brian Salomon34169692017-08-28 15:32:01 -0400127 }
128
129 const char* name() const override { return "TextureGeometryProcessor"; }
130
131 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
132 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400133 uint32_t x = this->usesCoverageEdgeAA() ? 0 : 1;
Brian Salomon70132d02018-05-29 15:33:06 -0400134 x |= kFloat3_GrVertexAttribType == fPositions.type() ? 0 : 2;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400135 x |= fDomain.isInitialized() ? 4 : 0;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400136 b->add32(x);
Brian Salomon34169692017-08-28 15:32:01 -0400137 }
138
139 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
140 class GLSLProcessor : public GrGLSLGeometryProcessor {
141 public:
142 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
143 FPCoordTransformIter&& transformIter) override {
144 const auto& textureGP = proc.cast<TextureGeometryProcessor>();
145 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
Brian Osmanc891b102018-06-14 14:50:17 -0400146 fColorSpaceXformHelper.setData(pdman, textureGP.fColorSpaceXform.get());
Brian Salomon34169692017-08-28 15:32:01 -0400147 }
148
149 private:
150 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Chris Dalton7b046312018-02-02 11:06:30 -0700151 using Interpolation = GrGLSLVaryingHandler::Interpolation;
Brian Salomon34169692017-08-28 15:32:01 -0400152 const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>();
153 fColorSpaceXformHelper.emitCode(
154 args.fUniformHandler, textureGP.fColorSpaceXform.get());
Brian Salomon70132d02018-05-29 15:33:06 -0400155 if (kFloat2_GrVertexAttribType == textureGP.fPositions.type()) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400156 args.fVaryingHandler->setNoPerspective();
157 }
Brian Salomon34169692017-08-28 15:32:01 -0400158 args.fVaryingHandler->emitAttributes(textureGP);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400159 gpArgs->fPositionVar = textureGP.fPositions.asShaderVar();
160
Brian Salomon34169692017-08-28 15:32:01 -0400161 this->emitTransforms(args.fVertBuilder,
162 args.fVaryingHandler,
163 args.fUniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -0500164 textureGP.fTextureCoords.asShaderVar(),
Brian Salomon34169692017-08-28 15:32:01 -0400165 args.fFPCoordTransformHandler);
Mike Klein5045e502018-06-19 01:40:57 +0000166 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors,
167 args.fOutputColor,
168 Interpolation::kCanBeFlat);
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400169 args.fFragBuilder->codeAppend("float2 texCoord;");
Mike Klein5045e502018-06-19 01:40:57 +0000170 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords,
171 "texCoord");
Brian Salomonb80ffee2018-05-23 16:39:39 -0400172 if (textureGP.fDomain.isInitialized()) {
173 args.fFragBuilder->codeAppend("float4 domain;");
174 args.fVaryingHandler->addPassThroughAttribute(
Mike Klein5045e502018-06-19 01:40:57 +0000175 &textureGP.fDomain, "domain",
Brian Salomonb80ffee2018-05-23 16:39:39 -0400176 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
177 args.fFragBuilder->codeAppend(
178 "texCoord = clamp(texCoord, domain.xy, domain.zw);");
179 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400180 if (textureGP.numTextureSamplers() > 1) {
Chris Dalton7b046312018-02-02 11:06:30 -0700181 // If this changes to float, reconsider Interpolation::kMustBeFlat.
Brian Salomon70132d02018-05-29 15:33:06 -0400182 SkASSERT(kInt_GrVertexAttribType == textureGP.fTextureIdx.type());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400183 SkASSERT(args.fShaderCaps->integerSupport());
184 args.fFragBuilder->codeAppend("int texIdx;");
Mike Klein5045e502018-06-19 01:40:57 +0000185 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx, "texIdx",
Chris Dalton7b046312018-02-02 11:06:30 -0700186 Interpolation::kMustBeFlat);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400187 args.fFragBuilder->codeAppend("switch (texIdx) {");
188 for (int i = 0; i < textureGP.numTextureSamplers(); ++i) {
189 args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor);
190 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
191 args.fTexSamplers[i],
192 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400193 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400194 &fColorSpaceXformHelper);
195 args.fFragBuilder->codeAppend("; break;");
196 }
197 args.fFragBuilder->codeAppend("}");
198 } else {
199 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
200 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
201 args.fTexSamplers[0],
202 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400203 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400204 &fColorSpaceXformHelper);
205 }
Brian Salomon34169692017-08-28 15:32:01 -0400206 args.fFragBuilder->codeAppend(";");
Brian Salomon485b8c62018-01-12 15:11:06 -0500207 if (textureGP.usesCoverageEdgeAA()) {
Brian Salomondba65f92018-01-22 08:43:38 -0500208 const char* aaDistName = nullptr;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400209 bool mulByFragCoordW = false;
210 // When interpolation is inaccurate we perform the evaluation of the edge
Brian Salomondba65f92018-01-22 08:43:38 -0500211 // equations in the fragment shader rather than interpolating values computed
212 // in the vertex shader.
213 if (!args.fShaderCaps->interpolantsAreInaccurate()) {
214 GrGLSLVarying aaDistVarying(kFloat4_GrSLType,
215 GrGLSLVarying::Scope::kVertToFrag);
Brian Salomon70132d02018-05-29 15:33:06 -0400216 if (kFloat3_GrVertexAttribType == textureGP.fPositions.type()) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400217 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
218 // The distance from edge equation e to homogenous point p=sk_Position
219 // is e.x*p.x/p.wx + e.y*p.y/p.w + e.z. However, we want screen space
220 // interpolation of this distance. We can do this by multiplying the
221 // varying in the VS by p.w and then multiplying by sk_FragCoord.w in
222 // the FS. So we output e.x*p.x + e.y*p.y + e.z * p.w
223 args.fVertBuilder->codeAppendf(
224 R"(%s = float4(dot(aaEdge0, %s), dot(aaEdge1, %s),
225 dot(aaEdge2, %s), dot(aaEdge3, %s));)",
Brian Salomon70132d02018-05-29 15:33:06 -0400226 aaDistVarying.vsOut(), textureGP.fPositions.name(),
227 textureGP.fPositions.name(), textureGP.fPositions.name(),
228 textureGP.fPositions.name());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400229 mulByFragCoordW = true;
230 } else {
231 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
232 args.fVertBuilder->codeAppendf(
233 R"(%s = float4(dot(aaEdge0.xy, %s.xy) + aaEdge0.z,
234 dot(aaEdge1.xy, %s.xy) + aaEdge1.z,
235 dot(aaEdge2.xy, %s.xy) + aaEdge2.z,
236 dot(aaEdge3.xy, %s.xy) + aaEdge3.z);)",
Brian Salomon70132d02018-05-29 15:33:06 -0400237 aaDistVarying.vsOut(), textureGP.fPositions.name(),
238 textureGP.fPositions.name(), textureGP.fPositions.name(),
239 textureGP.fPositions.name());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400240 }
Brian Salomondba65f92018-01-22 08:43:38 -0500241 aaDistName = aaDistVarying.fsIn();
242 } else {
243 GrGLSLVarying aaEdgeVarying[4]{
244 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
245 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
246 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
247 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag}
248 };
249 for (int i = 0; i < 4; ++i) {
250 SkString name;
251 name.printf("aaEdge%d", i);
Brian Salomon7d982c62018-02-05 16:20:47 -0500252 args.fVaryingHandler->addVarying(name.c_str(), &aaEdgeVarying[i],
253 Interpolation::kCanBeFlat);
Brian Salomondba65f92018-01-22 08:43:38 -0500254 args.fVertBuilder->codeAppendf(
255 "%s = aaEdge%d;", aaEdgeVarying[i].vsOut(), i);
256 }
257 args.fFragBuilder->codeAppendf(
258 R"(float4 aaDists = float4(dot(%s.xy, sk_FragCoord.xy) + %s.z,
259 dot(%s.xy, sk_FragCoord.xy) + %s.z,
260 dot(%s.xy, sk_FragCoord.xy) + %s.z,
261 dot(%s.xy, sk_FragCoord.xy) + %s.z);)",
262 aaEdgeVarying[0].fsIn(), aaEdgeVarying[0].fsIn(),
263 aaEdgeVarying[1].fsIn(), aaEdgeVarying[1].fsIn(),
264 aaEdgeVarying[2].fsIn(), aaEdgeVarying[2].fsIn(),
265 aaEdgeVarying[3].fsIn(), aaEdgeVarying[3].fsIn());
266 aaDistName = "aaDists";
267 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500268 args.fFragBuilder->codeAppendf(
269 "float mindist = min(min(%s.x, %s.y), min(%s.z, %s.w));",
Brian Salomondba65f92018-01-22 08:43:38 -0500270 aaDistName, aaDistName, aaDistName, aaDistName);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400271 if (mulByFragCoordW) {
272 args.fFragBuilder->codeAppend("mindist *= sk_FragCoord.w;");
273 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500274 args.fFragBuilder->codeAppendf("%s = float4(clamp(mindist, 0, 1));",
275 args.fOutputCoverage);
276 } else {
277 args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
278 }
Brian Salomon34169692017-08-28 15:32:01 -0400279 }
280 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
281 };
282 return new GLSLProcessor;
283 }
284
Brian Salomon485b8c62018-01-12 15:11:06 -0500285 bool usesCoverageEdgeAA() const { return SkToBool(fAAEdges[0].isInitialized()); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500286
Brian Salomon34169692017-08-28 15:32:01 -0400287private:
Brian Salomon336ce7b2017-09-08 08:23:58 -0400288 // This exists to reduce the number of shaders generated. It does some rounding of sampler
289 // counts.
290 static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) {
291 SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures &&
292 numRealProxies <= caps.maxFragmentSamplers());
293 if (1 == numRealProxies) {
294 return 1;
295 }
296 if (numRealProxies <= 4) {
297 return 4;
298 }
299 // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps.
300 return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers()));
301 }
302
303 TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400304 sk_sp<GrColorSpaceXform> csxf, bool coverageAA, bool perspective,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400305 Domain domain, const GrSamplerState::Filter filters[],
306 const GrShaderCaps& caps)
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500307 : INHERITED(kTextureGeometryProcessor_ClassID), fColorSpaceXform(std::move(csxf)) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400308 SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400309 fSamplers[0].reset(std::move(proxies[0]), filters[0]);
310 this->addTextureSampler(&fSamplers[0]);
311 for (int i = 1; i < proxyCnt; ++i) {
312 // This class has one sampler built in, the rest come from memory this processor was
313 // placement-newed into and so haven't been constructed.
314 new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]);
315 this->addTextureSampler(&fSamplers[i]);
316 }
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400317
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400318 if (perspective) {
Mike Klein5045e502018-06-19 01:40:57 +0000319 fPositions = this->addVertexAttrib("position", kFloat3_GrVertexAttribType);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400320 } else {
Mike Klein5045e502018-06-19 01:40:57 +0000321 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400322 }
Mike Klein5045e502018-06-19 01:40:57 +0000323 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
324 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400325
Brian Salomon336ce7b2017-09-08 08:23:58 -0400326 if (samplerCnt > 1) {
327 // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt
328 // times.
329 GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy();
330 for (int i = proxyCnt; i < samplerCnt; ++i) {
331 new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]);
332 this->addTextureSampler(&fSamplers[i]);
333 }
334 SkASSERT(caps.integerSupport());
Mike Klein5045e502018-06-19 01:40:57 +0000335 fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400336 }
Brian Salomonb80ffee2018-05-23 16:39:39 -0400337 if (domain == Domain::kYes) {
Mike Klein5045e502018-06-19 01:40:57 +0000338 fDomain = this->addVertexAttrib("domain", kFloat4_GrVertexAttribType);
Brian Salomonb80ffee2018-05-23 16:39:39 -0400339 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500340 if (coverageAA) {
Mike Klein5045e502018-06-19 01:40:57 +0000341 fAAEdges[0] = this->addVertexAttrib("aaEdge0", kFloat3_GrVertexAttribType);
342 fAAEdges[1] = this->addVertexAttrib("aaEdge1", kFloat3_GrVertexAttribType);
343 fAAEdges[2] = this->addVertexAttrib("aaEdge2", kFloat3_GrVertexAttribType);
344 fAAEdges[3] = this->addVertexAttrib("aaEdge3", kFloat3_GrVertexAttribType);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500345 }
Brian Salomon34169692017-08-28 15:32:01 -0400346 }
347
348 Attribute fPositions;
Brian Salomon34169692017-08-28 15:32:01 -0400349 Attribute fColors;
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400350 Attribute fTextureCoords;
351 Attribute fTextureIdx;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400352 Attribute fDomain;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500353 Attribute fAAEdges[4];
Brian Salomon34169692017-08-28 15:32:01 -0400354 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400355 TextureSampler fSamplers[1];
Ethan Nicholasabff9562017-10-09 10:54:08 -0400356
357 typedef GrGeometryProcessor INHERITED;
Brian Salomon34169692017-08-28 15:32:01 -0400358};
359
Brian Salomon6872e942018-05-18 10:29:54 -0400360// This computes the four edge equations for a quad, then outsets them and computes a new quad
361// as the intersection points of the outset edges. 'x' and 'y' contain the original points as input
362// and the outset points as output. 'a', 'b', and 'c' are the edge equation coefficients on output.
363static void compute_quad_edges_and_outset_vertices(Sk4f* x, Sk4f* y, Sk4f* a, Sk4f* b, Sk4f* c) {
364 static constexpr auto fma = SkNx_fma<4, float>;
365 // These rotate the points/edge values either clockwise or counterclockwise assuming tri strip
366 // order.
367 auto nextCW = [](const Sk4f& v) { return SkNx_shuffle<2, 0, 3, 1>(v); };
368 auto nextCCW = [](const Sk4f& v) { return SkNx_shuffle<1, 3, 0, 2>(v); };
369
370 auto xnext = nextCCW(*x);
371 auto ynext = nextCCW(*y);
372 *a = ynext - *y;
373 *b = *x - xnext;
374 *c = fma(xnext, *y, -ynext * *x);
375 Sk4f invNormLengths = (*a * *a + *b * *b).rsqrt();
376 // Make sure the edge equations have their normals facing into the quad in device space.
377 auto test = fma(*a, nextCW(*x), fma(*b, nextCW(*y), *c));
378 if ((test < Sk4f(0)).anyTrue()) {
379 invNormLengths = -invNormLengths;
380 }
381 *a *= invNormLengths;
382 *b *= invNormLengths;
383 *c *= invNormLengths;
384
385 // Here is the outset. This makes our edge equations compute coverage without requiring a
386 // half pixel offset and is also used to compute the bloated quad that will cover all
387 // pixels.
388 *c += Sk4f(0.5f);
389
390 // Reverse the process to compute the points of the bloated quad from the edge equations.
391 // This time the inputs don't have 1s as their third coord and we want to homogenize rather
392 // than normalize.
393 auto anext = nextCW(*a);
394 auto bnext = nextCW(*b);
395 auto cnext = nextCW(*c);
396 *x = fma(bnext, *c, -*b * cnext);
397 *y = fma(*a, cnext, -anext * *c);
398 auto ic = (fma(anext, *b, -bnext * *a)).invert();
399 *x *= ic;
400 *y *= ic;
401}
402
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500403namespace {
404// This is a class soley so it can be partially specialized (functions cannot be).
Brian Salomon86c40012018-05-22 10:48:49 -0400405template <typename V, GrAA AA = V::kAA, typename Position = typename V::Position>
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400406class VertexAAHandler;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500407
Brian Salomon86c40012018-05-22 10:48:49 -0400408template<typename V> class VertexAAHandler<V, GrAA::kNo, SkPoint> {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500409public:
Brian Salomon86c40012018-05-22 10:48:49 -0400410 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500411 const SkRect& texRect) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400412 SkASSERT((quad.w4f() == Sk4f(1.f)).allTrue());
Brian Salomon86c40012018-05-22 10:48:49 -0400413 SkPointPriv::SetRectTriStrip(&vertices[0].fTextureCoords, texRect, sizeof(V));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400414 for (int i = 0; i < 4; ++i) {
415 vertices[i].fPosition = {quad.x(i), quad.y(i)};
416 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500417 }
418};
419
Brian Salomon86c40012018-05-22 10:48:49 -0400420template<typename V> class VertexAAHandler<V, GrAA::kNo, SkPoint3> {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500421public:
Brian Salomon86c40012018-05-22 10:48:49 -0400422 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500423 const SkRect& texRect) {
Brian Salomon86c40012018-05-22 10:48:49 -0400424 SkPointPriv::SetRectTriStrip(&vertices[0].fTextureCoords, texRect, sizeof(V));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400425 for (int i = 0; i < 4; ++i) {
426 vertices[i].fPosition = quad.point(i);
427 }
428 }
429};
430
Brian Salomon86c40012018-05-22 10:48:49 -0400431template<typename V> class VertexAAHandler<V, GrAA::kYes, SkPoint> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400432public:
Brian Salomon86c40012018-05-22 10:48:49 -0400433 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400434 const SkRect& texRect) {
435 SkASSERT((quad.w4f() == Sk4f(1.f)).allTrue());
Brian Salomon6872e942018-05-18 10:29:54 -0400436 auto x = quad.x4f();
437 auto y = quad.y4f();
438 Sk4f a, b, c;
439 compute_quad_edges_and_outset_vertices(&x, &y, &a, &b, &c);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500440
441 for (int i = 0; i < 4; ++i) {
Brian Salomon6872e942018-05-18 10:29:54 -0400442 vertices[i].fPosition = {x[i], y[i]};
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500443 for (int j = 0; j < 4; ++j) {
Brian Salomon6872e942018-05-18 10:29:54 -0400444 vertices[i].fEdges[j] = {a[j], b[j], c[j]};
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500445 }
446 }
447
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500448 AssignTexCoords(vertices, quad, texRect);
449 }
450
451private:
Brian Salomon86c40012018-05-22 10:48:49 -0400452 static void AssignTexCoords(V* vertices, const GrPerspQuad& quad, const SkRect& tex) {
Brian Salomona33b67c2018-05-17 10:42:14 -0400453 SkMatrix q = SkMatrix::MakeAll(quad.x(0), quad.x(1), quad.x(2),
454 quad.y(0), quad.y(1), quad.y(2),
455 1.f, 1.f, 1.f);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500456 SkMatrix qinv;
457 if (!q.invert(&qinv)) {
458 return;
459 }
460 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
461 tex.fTop, tex.fBottom, tex.fTop,
462 1.f, 1.f, 1.f);
463 SkMatrix map;
464 map.setConcat(t, qinv);
Brian Salomon86c40012018-05-22 10:48:49 -0400465 SkMatrixPriv::MapPointsWithStride(map, &vertices[0].fTextureCoords, sizeof(V),
466 &vertices[0].fPosition, sizeof(V), 4);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500467 }
468};
469
Brian Salomon86c40012018-05-22 10:48:49 -0400470template<typename V> class VertexAAHandler<V, GrAA::kYes, SkPoint3> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400471public:
Brian Salomon86c40012018-05-22 10:48:49 -0400472 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400473 const SkRect& texRect) {
474 auto x = quad.x4f();
475 auto y = quad.y4f();
476 auto iw = quad.iw4f();
477 x *= iw;
478 y *= iw;
479
480 // Get an equation for w from device space coords.
481 SkMatrix P;
482 P.setAll(x[0], y[0], 1, x[1], y[1], 1, x[2], y[2], 1);
483 SkAssertResult(P.invert(&P));
484 SkPoint3 weq{quad.w(0), quad.w(1), quad.w(2)};
485 P.mapHomogeneousPoints(&weq, &weq, 1);
486
487 Sk4f a, b, c;
488 compute_quad_edges_and_outset_vertices(&x, &y, &a, &b, &c);
489
490 // Compute new w values for the output vertices;
491 auto w = Sk4f(weq.fX) * x + Sk4f(weq.fY) * y + Sk4f(weq.fZ);
492 x *= w;
493 y *= w;
494
495 for (int i = 0; i < 4; ++i) {
496 vertices[i].fPosition = {x[i], y[i], w[i]};
497 for (int j = 0; j < 4; ++j) {
498 vertices[i].fEdges[j] = {a[j], b[j], c[j]};
499 }
500 }
501
502 AssignTexCoords(vertices, quad, texRect);
503 }
504
505private:
Brian Salomon86c40012018-05-22 10:48:49 -0400506 static void AssignTexCoords(V* vertices, const GrPerspQuad& quad, const SkRect& tex) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400507 SkMatrix q = SkMatrix::MakeAll(quad.x(0), quad.x(1), quad.x(2),
508 quad.y(0), quad.y(1), quad.y(2),
509 quad.w(0), quad.w(1), quad.w(2));
510 SkMatrix qinv;
511 if (!q.invert(&qinv)) {
512 return;
513 }
514 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
515 tex.fTop, tex.fBottom, tex.fTop,
516 1.f, 1.f, 1.f);
517 SkMatrix map;
518 map.setConcat(t, qinv);
519 SkPoint3 tempTexCoords[4];
520 SkMatrixPriv::MapHomogeneousPointsWithStride(map, tempTexCoords, sizeof(SkPoint3),
Brian Salomon86c40012018-05-22 10:48:49 -0400521 &vertices[0].fPosition, sizeof(V), 4);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400522 for (int i = 0; i < 4; ++i) {
523 auto invW = 1.f / tempTexCoords[i].fZ;
524 vertices[i].fTextureCoords.fX = tempTexCoords[i].fX * invW;
525 vertices[i].fTextureCoords.fY = tempTexCoords[i].fY * invW;
526 }
527 }
528};
529
Brian Salomon17031a72018-05-22 14:14:07 -0400530template <typename V, MultiTexture MT = V::kMultiTexture> struct TexIdAssigner;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500531
Brian Salomon17031a72018-05-22 14:14:07 -0400532template <typename V> struct TexIdAssigner<V, MultiTexture::kYes> {
Brian Salomon86c40012018-05-22 10:48:49 -0400533 static void Assign(V* vertices, int textureIdx) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400534 for (int i = 0; i < 4; ++i) {
535 vertices[i].fTextureIdx = textureIdx;
536 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500537 }
538};
539
Brian Salomon17031a72018-05-22 14:14:07 -0400540template <typename V> struct TexIdAssigner<V, MultiTexture::kNo> {
Brian Salomon86c40012018-05-22 10:48:49 -0400541 static void Assign(V* vertices, int textureIdx) {}
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500542};
Brian Salomonb80ffee2018-05-23 16:39:39 -0400543
544template <typename V, Domain D = V::kDomain> struct DomainAssigner;
545
546template <typename V> struct DomainAssigner<V, Domain::kYes> {
547 static void Assign(V* vertices, Domain domain, GrSamplerState::Filter filter,
548 const SkRect& srcRect, GrSurfaceOrigin origin, float iw, float ih) {
549 static constexpr SkRect kLargeRect = {-2, -2, 2, 2};
550 SkRect domainRect;
551 if (domain == Domain::kYes) {
552 auto ltrb = Sk4f::Load(&srcRect);
553 if (filter == GrSamplerState::Filter::kBilerp) {
554 auto rblt = SkNx_shuffle<2, 3, 0, 1>(ltrb);
555 auto whwh = (rblt - ltrb).abs();
556 auto c = (rblt + ltrb) * 0.5f;
557 static const Sk4f kOffsets = {0.5f, 0.5f, -0.5f, -0.5f};
558 ltrb = (whwh < 1.f).thenElse(c, ltrb + kOffsets);
559 }
560 ltrb *= Sk4f(iw, ih, iw, ih);
561 if (origin == kBottomLeft_GrSurfaceOrigin) {
562 static const Sk4f kMul = {1.f, -1.f, 1.f, -1.f};
563 static const Sk4f kAdd = {0.f, 1.f, 0.f, 1.f};
564 ltrb = SkNx_shuffle<0, 3, 2, 1>(kMul * ltrb + kAdd);
565 }
566 ltrb.store(&domainRect);
567 } else {
568 domainRect = kLargeRect;
569 }
570 for (int i = 0; i < 4; ++i) {
571 vertices[i].fTextureDomain = domainRect;
572 }
573 }
574};
575
576template <typename V> struct DomainAssigner<V, Domain::kNo> {
577 static void Assign(V*, Domain domain, GrSamplerState::Filter, const SkRect&, GrSurfaceOrigin,
578 float iw, float ih) {
579 SkASSERT(domain == Domain::kNo);
580 }
581};
582
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500583} // anonymous namespace
584
Brian Salomon86c40012018-05-22 10:48:49 -0400585template <typename V>
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400586static void tessellate_quad(const GrPerspQuad& devQuad, const SkRect& srcRect, GrColor color,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400587 GrSurfaceOrigin origin, GrSamplerState::Filter filter, V* vertices,
588 SkScalar iw, SkScalar ih, int textureIdx, Domain domain) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500589 SkRect texRect = {
590 iw * srcRect.fLeft,
591 ih * srcRect.fTop,
592 iw * srcRect.fRight,
593 ih * srcRect.fBottom
594 };
595 if (origin == kBottomLeft_GrSurfaceOrigin) {
596 texRect.fTop = 1.f - texRect.fTop;
597 texRect.fBottom = 1.f - texRect.fBottom;
598 }
Brian Salomon86c40012018-05-22 10:48:49 -0400599 VertexAAHandler<V>::AssignPositionsAndTexCoords(vertices, devQuad, texRect);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500600 vertices[0].fColor = color;
601 vertices[1].fColor = color;
602 vertices[2].fColor = color;
603 vertices[3].fColor = color;
Brian Salomon86c40012018-05-22 10:48:49 -0400604 TexIdAssigner<V>::Assign(vertices, textureIdx);
Brian Salomonb80ffee2018-05-23 16:39:39 -0400605 DomainAssigner<V>::Assign(vertices, domain, filter, srcRect, origin, iw, ih);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500606}
Brian Salomon17031a72018-05-22 14:14:07 -0400607
Brian Salomon34169692017-08-28 15:32:01 -0400608/**
609 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
610 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
611 */
612class TextureOp final : public GrMeshDrawOp {
613public:
Robert Phillips7c525e62018-06-12 10:11:12 -0400614 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
615 sk_sp<GrTextureProxy> proxy,
616 GrSamplerState::Filter filter,
617 GrColor color,
618 const SkRect& srcRect,
619 const SkRect& dstRect,
620 GrAAType aaType,
621 SkCanvas::SrcRectConstraint constraint,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400622 const SkMatrix& viewMatrix,
623 sk_sp<GrColorSpaceXform> csxf) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400624 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
625
626 return pool->allocate<TextureOp>(std::move(proxy), filter, color,
627 srcRect, dstRect, aaType, constraint,
628 viewMatrix, std::move(csxf));
Brian Salomon34169692017-08-28 15:32:01 -0400629 }
630
Brian Salomon336ce7b2017-09-08 08:23:58 -0400631 ~TextureOp() override {
632 if (fFinalized) {
633 auto proxies = this->proxies();
634 for (int i = 0; i < fProxyCnt; ++i) {
635 proxies[i]->completedRead();
636 }
637 if (fProxyCnt > 1) {
638 delete[] reinterpret_cast<const char*>(proxies);
639 }
640 } else {
641 SkASSERT(1 == fProxyCnt);
642 fProxy0->unref();
643 }
644 }
Brian Salomon34169692017-08-28 15:32:01 -0400645
646 const char* name() const override { return "TextureOp"; }
647
Robert Phillipsf1748f52017-09-14 14:11:24 -0400648 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400649 auto proxies = this->proxies();
650 for (int i = 0; i < fProxyCnt; ++i) {
651 func(proxies[i]);
652 }
653 }
654
Brian Salomon34169692017-08-28 15:32:01 -0400655 SkString dumpInfo() const override {
656 SkString str;
Brian Salomon34169692017-08-28 15:32:01 -0400657 str.appendf("# draws: %d\n", fDraws.count());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400658 auto proxies = this->proxies();
659 for (int i = 0; i < fProxyCnt; ++i) {
660 str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(),
661 static_cast<int>(this->filters()[i]));
662 }
Brian Salomon34169692017-08-28 15:32:01 -0400663 for (int i = 0; i < fDraws.count(); ++i) {
664 const Draw& draw = fDraws[i];
665 str.appendf(
Brian Salomon336ce7b2017-09-08 08:23:58 -0400666 "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
667 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
Brian Salomonb80ffee2018-05-23 16:39:39 -0400668 i, draw.color(), draw.textureIdx(), draw.srcRect().fLeft, draw.srcRect().fTop,
669 draw.srcRect().fRight, draw.srcRect().fBottom, draw.quad().point(0).fX,
670 draw.quad().point(0).fY, draw.quad().point(1).fX, draw.quad().point(1).fY,
671 draw.quad().point(2).fX, draw.quad().point(2).fY, draw.quad().point(3).fX,
672 draw.quad().point(3).fY);
Brian Salomon34169692017-08-28 15:32:01 -0400673 }
674 str += INHERITED::dumpInfo();
675 return str;
676 }
677
Brian Osman9a725dd2017-09-20 09:53:22 -0400678 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
679 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon34169692017-08-28 15:32:01 -0400680 SkASSERT(!fFinalized);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400681 SkASSERT(1 == fProxyCnt);
Brian Salomon34169692017-08-28 15:32:01 -0400682 fFinalized = true;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400683 fProxy0->addPendingRead();
684 fProxy0->unref();
Brian Salomon34169692017-08-28 15:32:01 -0400685 return RequiresDstTexture::kNo;
686 }
687
Brian Salomon485b8c62018-01-12 15:11:06 -0500688 FixedFunctionFlags fixedFunctionFlags() const override {
689 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
690 : FixedFunctionFlags::kNone;
691 }
Brian Salomon34169692017-08-28 15:32:01 -0400692
693 DEFINE_OP_CLASS_ID
694
695private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400696 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500697
698 // This is used in a heursitic for choosing a code path. We don't care what happens with
699 // really large rects, infs, nans, etc.
700#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
701__attribute__((no_sanitize("float-cast-overflow")))
702#endif
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500703 size_t RectSizeAsSizeT(const SkRect& rect) {;
Brian Salomon762d5e72017-12-01 10:25:08 -0500704 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
705 }
706
Brian Salomon336ce7b2017-09-08 08:23:58 -0400707 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
708
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400709 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500710 const SkRect& srcRect, const SkRect& dstRect, GrAAType aaType,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400711 SkCanvas::SrcRectConstraint constraint, const SkMatrix& viewMatrix,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400712 sk_sp<GrColorSpaceXform> csxf)
Brian Salomon34169692017-08-28 15:32:01 -0400713 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400714 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400715 , fProxy0(proxy.release())
716 , fFilter0(filter)
717 , fProxyCnt(1)
Brian Salomon485b8c62018-01-12 15:11:06 -0500718 , fAAType(static_cast<unsigned>(aaType))
Brian Osman2b23c4b2018-06-01 12:25:08 -0400719 , fFinalized(0) {
Brian Salomon485b8c62018-01-12 15:11:06 -0500720 SkASSERT(aaType != GrAAType::kMixedSamples);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400721 fPerspective = viewMatrix.hasPerspective();
Brian Salomon594b64c2018-05-29 12:47:57 -0400722 auto quad = GrPerspQuad(dstRect, viewMatrix);
723 auto bounds = quad.bounds();
724#ifndef SK_DONT_DROP_UNNECESSARY_AA_IN_TEXTURE_OP
725 if (GrAAType::kCoverage == this->aaType() && viewMatrix.rectStaysRect()) {
726 // Disable coverage AA when rect falls on integers in device space.
727 auto is_int = [](float f) { return f == sk_float_floor(f); };
728 if (is_int(bounds.fLeft) && is_int(bounds.fTop) && is_int(bounds.fRight) &&
729 is_int(bounds.fBottom)) {
730 fAAType = static_cast<unsigned>(GrAAType::kNone);
731 // We may have had a strict constraint with nearest filter soley due to possible AA
732 // bloat. In that case it's no longer necessary.
733 if (constraint == SkCanvas::kStrict_SrcRectConstraint &&
734 filter == GrSamplerState::Filter::kNearest) {
735 constraint = SkCanvas::kFast_SrcRectConstraint;
736 }
737 }
738 }
739#endif
740 const auto& draw = fDraws.emplace_back(srcRect, 0, quad, constraint, color);
Brian Salomon34169692017-08-28 15:32:01 -0400741 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon594b64c2018-05-29 12:47:57 -0400742 fDomain = static_cast<bool>(draw.domain());
Brian Salomon762d5e72017-12-01 10:25:08 -0500743 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400744 }
745
Brian Salomonb80ffee2018-05-23 16:39:39 -0400746 template <typename Pos, MultiTexture MT, Domain D, GrAA AA>
Brian Salomon17031a72018-05-22 14:14:07 -0400747 void tess(void* v, const float iw[], const float ih[], const GrGeometryProcessor* gp) {
Brian Salomonb80ffee2018-05-23 16:39:39 -0400748 using Vertex = TextureGeometryProcessor::Vertex<Pos, MT, D, AA>;
Mike Klein5045e502018-06-19 01:40:57 +0000749 SkASSERT(gp->getVertexStride() == sizeof(Vertex));
Brian Salomon17031a72018-05-22 14:14:07 -0400750 auto vertices = static_cast<Vertex*>(v);
751 auto proxies = this->proxies();
Brian Salomonb80ffee2018-05-23 16:39:39 -0400752 auto filters = this->filters();
Brian Salomon17031a72018-05-22 14:14:07 -0400753 for (const auto& draw : fDraws) {
Brian Salomonb80ffee2018-05-23 16:39:39 -0400754 auto textureIdx = draw.textureIdx();
755 auto origin = proxies[textureIdx]->origin();
756 tessellate_quad<Vertex>(draw.quad(), draw.srcRect(), draw.color(), origin,
757 filters[textureIdx], vertices, iw[textureIdx], ih[textureIdx],
758 textureIdx, draw.domain());
Brian Salomon17031a72018-05-22 14:14:07 -0400759 vertices += 4;
760 }
761 }
762
Brian Salomon34169692017-08-28 15:32:01 -0400763 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400764 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
765 auto proxies = this->proxies();
766 auto filters = this->filters();
767 for (int i = 0; i < fProxyCnt; ++i) {
768 if (!proxies[i]->instantiate(target->resourceProvider())) {
769 return;
770 }
771 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400772 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400773
Brian Salomonb80ffee2018-05-23 16:39:39 -0400774 Domain domain = fDomain ? Domain::kYes : Domain::kNo;
Brian Salomon485b8c62018-01-12 15:11:06 -0500775 bool coverageAA = GrAAType::kCoverage == this->aaType();
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400776 sk_sp<GrGeometryProcessor> gp = TextureGeometryProcessor::Make(
777 proxiesSPs, fProxyCnt, std::move(fColorSpaceXform), coverageAA, fPerspective,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400778 domain, filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400779 GrPipeline::InitArgs args;
780 args.fProxy = target->proxy();
781 args.fCaps = &target->caps();
782 args.fResourceProvider = target->resourceProvider();
Brian Salomon485b8c62018-01-12 15:11:06 -0500783 args.fFlags = 0;
Brian Salomon485b8c62018-01-12 15:11:06 -0500784 if (GrAAType::kMSAA == this->aaType()) {
785 args.fFlags |= GrPipeline::kHWAntialias_Flag;
786 }
787
Brian Salomon34169692017-08-28 15:32:01 -0400788 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
789 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400790 int vstart;
791 const GrBuffer* vbuffer;
Mike Klein5045e502018-06-19 01:40:57 +0000792 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
793 &vstart);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400794 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400795 SkDebugf("Could not allocate vertices\n");
796 return;
797 }
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400798
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400799 float iw[kMaxTextures];
800 float ih[kMaxTextures];
801 for (int t = 0; t < fProxyCnt; ++t) {
802 const auto* texture = proxies[t]->priv().peekTexture();
803 iw[t] = 1.f / texture->width();
804 ih[t] = 1.f / texture->height();
805 }
806
Mike Klein5045e502018-06-19 01:40:57 +0000807 using TessFn =
808 decltype(&TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kNo, GrAA::kNo>);
809 static constexpr TessFn kTessFns[] = {
810 &TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kNo, GrAA::kNo>,
811 &TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kNo, GrAA::kYes>,
812 &TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kYes, GrAA::kNo>,
813 &TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kYes, GrAA::kYes>,
814 &TextureOp::tess<SkPoint, MultiTexture::kYes, Domain::kNo, GrAA::kNo>,
815 &TextureOp::tess<SkPoint, MultiTexture::kYes, Domain::kNo, GrAA::kYes>,
816 &TextureOp::tess<SkPoint, MultiTexture::kYes, Domain::kYes, GrAA::kNo>,
817 &TextureOp::tess<SkPoint, MultiTexture::kYes, Domain::kYes, GrAA::kYes>,
818 &TextureOp::tess<SkPoint3, MultiTexture::kNo, Domain::kNo, GrAA::kNo>,
819 &TextureOp::tess<SkPoint3, MultiTexture::kNo, Domain::kNo, GrAA::kYes>,
820 &TextureOp::tess<SkPoint3, MultiTexture::kNo, Domain::kYes, GrAA::kNo>,
821 &TextureOp::tess<SkPoint3, MultiTexture::kNo, Domain::kYes, GrAA::kYes>,
822 &TextureOp::tess<SkPoint3, MultiTexture::kYes, Domain::kNo, GrAA::kNo>,
823 &TextureOp::tess<SkPoint3, MultiTexture::kYes, Domain::kNo, GrAA::kYes>,
824 &TextureOp::tess<SkPoint3, MultiTexture::kYes, Domain::kYes, GrAA::kNo>,
825 &TextureOp::tess<SkPoint3, MultiTexture::kYes, Domain::kYes, GrAA::kYes>,
826 };
827 int tessFnIdx = 0;
828 tessFnIdx |= coverageAA ? 0x1 : 0x0;
829 tessFnIdx |= fDomain ? 0x2 : 0x0;
830 tessFnIdx |= (fProxyCnt > 1) ? 0x4 : 0x0;
831 tessFnIdx |= fPerspective ? 0x8 : 0x0;
832 (this->*(kTessFns[tessFnIdx]))(vdata, iw, ih, gp.get());
Brian Salomonb80ffee2018-05-23 16:39:39 -0400833
Brian Salomon57caa662017-10-18 12:21:05 +0000834 GrPrimitiveType primitiveType =
835 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
836 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400837 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000838 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400839 if (!ibuffer) {
840 SkDebugf("Could not allocate quad indices\n");
841 return;
842 }
Brian Salomon34169692017-08-28 15:32:01 -0400843 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
844 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400845 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400846 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400847 }
Brian Salomon57caa662017-10-18 12:21:05 +0000848 mesh.setVertexData(vbuffer, vstart);
849 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400850 }
851
852 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
853 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500854 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400855 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400856 return false;
857 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500858 if (this->aaType() != that->aaType()) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500859 return false;
860 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400861 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
862 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500863 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
864 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
865 that->fMaxApproxDstPixelArea <=
866 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400867 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500868 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400869 if (numNewProxies < 0) {
870 return false;
871 }
872 if (1 == fProxyCnt && numNewProxies) {
873 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
874 kMaxTextures];
875 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
876 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
877 proxies[0] = fProxy0;
878 filters[0] = fFilter0;
879 fProxyArray = proxies;
880 }
881 fProxyCnt += numNewProxies;
882 auto thisProxies = fProxyArray;
883 auto thatProxies = that->proxies();
884 auto thatFilters = that->filters();
885 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
886 kMaxTextures);
887 for (int i = 0; i < that->fProxyCnt; ++i) {
888 if (map[i] < 0) {
889 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400890
Brian Salomon336ce7b2017-09-08 08:23:58 -0400891 thisProxies[-map[i]] = thatProxies[i];
892 thisFilters[-map[i]] = thatFilters[i];
893 map[i] = -map[i];
894 }
895 }
896 int firstNewDraw = fDraws.count();
897 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
898 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
Brian Salomonb80ffee2018-05-23 16:39:39 -0400899 fDraws[i].setTextureIdx(map[fDraws[i].textureIdx()]);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400900 }
901 } else {
Brian Salomonbbf05752017-11-30 11:30:48 -0500902 // We can get here when one of the ops is already multitextured but the other cannot
903 // be because of the dst rect size.
904 if (fProxyCnt > 1 || that->fProxyCnt > 1) {
905 return false;
906 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400907 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
908 return false;
909 }
910 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
911 }
Brian Salomon34169692017-08-28 15:32:01 -0400912 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500913 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400914 fPerspective |= that->fPerspective;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400915 fDomain |= that->fDomain;
Brian Salomon34169692017-08-28 15:32:01 -0400916 return true;
917 }
918
Brian Salomon336ce7b2017-09-08 08:23:58 -0400919 /**
920 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
921 * value means that's proxy should be added to this's proxy array at the absolute value of
922 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
923 * negative value is returned. Otherwise, return value indicates the number of proxies that have
924 * to be added to this op or, equivalently, the number of negative entries in map.
925 */
926 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
927 std::fill_n(map, kMaxTextures, -kMaxTextures);
928 int sharedProxyCnt = 0;
929 auto thisProxies = this->proxies();
930 auto thisFilters = this->filters();
931 auto thatProxies = that->proxies();
932 auto thatFilters = that->filters();
933 for (int i = 0; i < fProxyCnt; ++i) {
934 for (int j = 0; j < that->fProxyCnt; ++j) {
935 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
936 if (thisFilters[i] != thatFilters[j]) {
937 // In GL we don't currently support using the same texture with different
938 // samplers. If we added support for sampler objects and a cap bit to know
939 // it's ok to use different filter modes then we could support this.
940 // Otherwise, we could also only allow a single filter mode for each op
941 // instance.
942 return -1;
943 }
944 map[j] = i;
945 ++sharedProxyCnt;
946 break;
947 }
948 }
949 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500950 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400951 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
952 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
953 return -1;
954 }
955 GrPixelConfig config = thisProxies[0]->config();
956 int nextSlot = fProxyCnt;
957 for (int j = 0; j < that->fProxyCnt; ++j) {
958 // We want to avoid making many shaders because of different permutations of shader
959 // based swizzle and sampler types. The approach taken here is to require the configs to
960 // be the same and to only allow already instantiated proxies that have the most
961 // common sampler type. Otherwise we don't merge.
962 if (thatProxies[j]->config() != config) {
963 return -1;
964 }
965 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
966 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
967 return -1;
968 }
969 }
970 if (map[j] < 0) {
971 map[j] = -(nextSlot++);
972 }
973 }
974 return newProxyCnt;
975 }
976
Brian Salomon485b8c62018-01-12 15:11:06 -0500977 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500978
Brian Salomon336ce7b2017-09-08 08:23:58 -0400979 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
980
981 const GrSamplerState::Filter* filters() const {
982 if (fProxyCnt > 1) {
983 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
984 }
985 return &fFilter0;
986 }
987
Brian Salomonb80ffee2018-05-23 16:39:39 -0400988 class Draw {
989 public:
990 Draw(const SkRect& srcRect, int textureIdx, const GrPerspQuad& quad,
991 SkCanvas::SrcRectConstraint constraint, GrColor color)
992 : fSrcRect(srcRect)
993 , fHasDomain(constraint == SkCanvas::kStrict_SrcRectConstraint)
994 , fTextureIdx(SkToUInt(textureIdx))
995 , fQuad(quad)
996 , fColor(color) {}
997 const GrPerspQuad& quad() const { return fQuad; }
998 int textureIdx() const { return SkToInt(fTextureIdx); }
999 const SkRect& srcRect() const { return fSrcRect; }
1000 GrColor color() const { return fColor; }
1001 Domain domain() const { return Domain(fHasDomain); }
1002 void setTextureIdx(int i) { fTextureIdx = SkToUInt(i); }
1003
1004 private:
Brian Salomon34169692017-08-28 15:32:01 -04001005 SkRect fSrcRect;
Brian Salomonb80ffee2018-05-23 16:39:39 -04001006 unsigned fHasDomain : 1;
1007 unsigned fTextureIdx : 31;
Brian Salomonbe3c1d22018-05-21 12:54:39 -04001008 GrPerspQuad fQuad;
Brian Salomon34169692017-08-28 15:32:01 -04001009 GrColor fColor;
1010 };
1011 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -04001012 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001013 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
1014 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
1015 // followed by kMaxTextures filters.
1016 union {
1017 GrTextureProxy* fProxy0;
1018 GrTextureProxy** fProxyArray;
1019 };
Brian Salomonbbf05752017-11-30 11:30:48 -05001020 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001021 GrSamplerState::Filter fFilter0;
1022 uint8_t fProxyCnt;
Brian Salomon485b8c62018-01-12 15:11:06 -05001023 unsigned fAAType : 2;
Brian Salomonbe3c1d22018-05-21 12:54:39 -04001024 unsigned fPerspective : 1;
Brian Salomonb80ffee2018-05-23 16:39:39 -04001025 unsigned fDomain : 1;
Brian Salomon34169692017-08-28 15:32:01 -04001026 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -05001027 unsigned fFinalized : 1;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001028
Brian Salomon34169692017-08-28 15:32:01 -04001029 typedef GrMeshDrawOp INHERITED;
1030};
1031
Brian Salomon336ce7b2017-09-08 08:23:58 -04001032constexpr int TextureGeometryProcessor::kMaxTextures;
1033constexpr int TextureOp::kMaxTextures;
1034
Brian Salomon34169692017-08-28 15:32:01 -04001035} // anonymous namespace
1036
1037namespace GrTextureOp {
1038
Robert Phillips7c525e62018-06-12 10:11:12 -04001039std::unique_ptr<GrDrawOp> Make(GrContext* context,
1040 sk_sp<GrTextureProxy> proxy,
1041 GrSamplerState::Filter filter,
1042 GrColor color,
1043 const SkRect& srcRect,
1044 const SkRect& dstRect,
1045 GrAAType aaType,
1046 SkCanvas::SrcRectConstraint constraint,
1047 const SkMatrix& viewMatrix,
1048 sk_sp<GrColorSpaceXform> csxf) {
1049 return TextureOp::Make(context, std::move(proxy), filter, color, srcRect, dstRect, aaType,
1050 constraint, viewMatrix, std::move(csxf));
Brian Salomon34169692017-08-28 15:32:01 -04001051}
1052
1053} // namespace GrTextureOp
1054
1055#if GR_TEST_UTILS
1056#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001057#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001058#include "GrProxyProvider.h"
Brian Salomon34169692017-08-28 15:32:01 -04001059
1060GR_DRAW_OP_TEST_DEFINE(TextureOp) {
1061 GrSurfaceDesc desc;
1062 desc.fConfig = kRGBA_8888_GrPixelConfig;
1063 desc.fHeight = random->nextULessThan(90) + 10;
1064 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001065 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel09c94002018-06-08 22:11:51 +00001066 GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
1067 SkBackingFit fit = SkBackingFit::kExact;
1068 if (mipMapped == GrMipMapped::kNo) {
1069 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1070 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001071
1072 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Greg Daniel09c94002018-06-08 22:11:51 +00001073 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(desc, origin, mipMapped, fit,
1074 SkBudgeted::kNo,
1075 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001076
Brian Salomon34169692017-08-28 15:32:01 -04001077 SkRect rect = GrTest::TestRect(random);
1078 SkRect srcRect;
1079 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1080 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1081 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1082 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1083 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
1084 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001085 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
1086 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +00001087 while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
1088 filter = (GrSamplerState::Filter)random->nextULessThan(
1089 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
1090 }
Brian Salomon34169692017-08-28 15:32:01 -04001091 auto csxf = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001092 GrAAType aaType = GrAAType::kNone;
1093 if (random->nextBool()) {
1094 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
1095 }
Brian Salomonb80ffee2018-05-23 16:39:39 -04001096 auto constraint = random->nextBool() ? SkCanvas::kStrict_SrcRectConstraint
1097 : SkCanvas::kFast_SrcRectConstraint;
Robert Phillips7c525e62018-06-12 10:11:12 -04001098 return GrTextureOp::Make(context, std::move(proxy), filter, color, srcRect, rect, aaType,
1099 constraint, viewMatrix, std::move(csxf));
Brian Salomon34169692017-08-28 15:32:01 -04001100}
1101
1102#endif