blob: f2efb95a929698ae05fd5086395c40bdbbc4fd8a [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"
9#include "GrAppliedClip.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040010#include "GrCaps.h"
Brian Salomon34169692017-08-28 15:32:01 -040011#include "GrDrawOpTest.h"
12#include "GrGeometryProcessor.h"
13#include "GrMeshDrawOp.h"
14#include "GrOpFlushState.h"
15#include "GrQuad.h"
16#include "GrResourceProvider.h"
17#include "GrShaderCaps.h"
18#include "GrTexture.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040019#include "GrTexturePriv.h"
Brian Salomon34169692017-08-28 15:32:01 -040020#include "GrTextureProxy.h"
21#include "SkGr.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040022#include "SkMathPriv.h"
Brian Salomona33b67c2018-05-17 10:42:14 -040023#include "SkMatrixPriv.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050024#include "SkPoint.h"
25#include "SkPoint3.h"
Brian Salomon34169692017-08-28 15:32:01 -040026#include "glsl/GrGLSLColorSpaceXformHelper.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050027#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon34169692017-08-28 15:32:01 -040028#include "glsl/GrGLSLGeometryProcessor.h"
29#include "glsl/GrGLSLVarying.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050030#include "glsl/GrGLSLVertexGeoBuilder.h"
Brian Salomon34169692017-08-28 15:32:01 -040031
32namespace {
33
34/**
35 * Geometry Processor that draws a texture modulated by a vertex color (though, this is meant to be
36 * the same value across all vertices of a quad and uses flat interpolation when available). This is
37 * used by TextureOp below.
38 */
39class TextureGeometryProcessor : public GrGeometryProcessor {
40public:
41 struct Vertex {
42 SkPoint fPosition;
43 SkPoint fTextureCoords;
44 GrColor fColor;
45 };
Brian Salomonb5ef1f92018-01-11 11:46:21 -050046 struct AAVertex {
47 SkPoint fPosition;
48 SkPoint fTextureCoords;
49 SkPoint3 fEdges[4];
50 GrColor fColor;
51 };
Brian Salomon336ce7b2017-09-08 08:23:58 -040052 struct MultiTextureVertex {
53 SkPoint fPosition;
54 int fTextureIdx;
55 SkPoint fTextureCoords;
56 GrColor fColor;
57 };
Brian Salomonb5ef1f92018-01-11 11:46:21 -050058 struct AAMultiTextureVertex {
59 SkPoint fPosition;
60 int fTextureIdx;
61 SkPoint fTextureCoords;
62 SkPoint3 fEdges[4];
63 GrColor fColor;
64 };
Brian Salomon336ce7b2017-09-08 08:23:58 -040065
66 // Maximum number of textures supported by this op. Must also be checked against the caps
67 // limit. These numbers were based on some limited experiments on a HP Z840 and Pixel XL 2016
68 // and could probably use more tuning.
69#ifdef SK_BUILD_FOR_ANDROID
70 static constexpr int kMaxTextures = 4;
71#else
72 static constexpr int kMaxTextures = 8;
73#endif
74
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040075 static int SupportsMultitexture(const GrShaderCaps& caps) {
Brian Salomon762d5e72017-12-01 10:25:08 -050076 return caps.integerSupport() && caps.maxFragmentSamplers() > 1;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040077 }
Brian Salomon336ce7b2017-09-08 08:23:58 -040078
79 static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxies[], int proxyCnt,
Brian Salomon485b8c62018-01-12 15:11:06 -050080 sk_sp<GrColorSpaceXform> csxf, bool coverageAA,
Brian Salomon336ce7b2017-09-08 08:23:58 -040081 const GrSamplerState::Filter filters[],
82 const GrShaderCaps& caps) {
83 // We use placement new to avoid always allocating space for kMaxTextures TextureSampler
84 // instances.
85 int samplerCnt = NumSamplersToUse(proxyCnt, caps);
86 size_t size = sizeof(TextureGeometryProcessor) + sizeof(TextureSampler) * (samplerCnt - 1);
87 void* mem = GrGeometryProcessor::operator new(size);
88 return sk_sp<TextureGeometryProcessor>(new (mem) TextureGeometryProcessor(
Brian Salomon485b8c62018-01-12 15:11:06 -050089 proxies, proxyCnt, samplerCnt, std::move(csxf), coverageAA, filters, caps));
Brian Salomon336ce7b2017-09-08 08:23:58 -040090 }
91
92 ~TextureGeometryProcessor() override {
93 int cnt = this->numTextureSamplers();
94 for (int i = 1; i < cnt; ++i) {
95 fSamplers[i].~TextureSampler();
96 }
Brian Salomon34169692017-08-28 15:32:01 -040097 }
98
99 const char* name() const override { return "TextureGeometryProcessor"; }
100
101 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
102 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
Brian Salomon485b8c62018-01-12 15:11:06 -0500103 b->add32(static_cast<uint32_t>(this->usesCoverageEdgeAA()));
Brian Salomon34169692017-08-28 15:32:01 -0400104 }
105
106 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
107 class GLSLProcessor : public GrGLSLGeometryProcessor {
108 public:
109 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
110 FPCoordTransformIter&& transformIter) override {
111 const auto& textureGP = proc.cast<TextureGeometryProcessor>();
112 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
113 if (fColorSpaceXformHelper.isValid()) {
114 fColorSpaceXformHelper.setData(pdman, textureGP.fColorSpaceXform.get());
115 }
116 }
117
118 private:
119 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Chris Dalton7b046312018-02-02 11:06:30 -0700120 using Interpolation = GrGLSLVaryingHandler::Interpolation;
Brian Salomon34169692017-08-28 15:32:01 -0400121 const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>();
122 fColorSpaceXformHelper.emitCode(
123 args.fUniformHandler, textureGP.fColorSpaceXform.get());
124 args.fVaryingHandler->setNoPerspective();
125 args.fVaryingHandler->emitAttributes(textureGP);
126 this->writeOutputPosition(args.fVertBuilder, gpArgs, textureGP.fPositions.fName);
127 this->emitTransforms(args.fVertBuilder,
128 args.fVaryingHandler,
129 args.fUniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -0500130 textureGP.fTextureCoords.asShaderVar(),
Brian Salomon34169692017-08-28 15:32:01 -0400131 args.fFPCoordTransformHandler);
Chris Dalton7b046312018-02-02 11:06:30 -0700132 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors,
133 args.fOutputColor,
134 Interpolation::kCanBeFlat);
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400135 args.fFragBuilder->codeAppend("float2 texCoord;");
Chris Daltonfdde34e2017-10-16 14:15:26 -0600136 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords,
137 "texCoord");
Brian Salomon336ce7b2017-09-08 08:23:58 -0400138 if (textureGP.numTextureSamplers() > 1) {
Chris Dalton7b046312018-02-02 11:06:30 -0700139 // If this changes to float, reconsider Interpolation::kMustBeFlat.
140 SkASSERT(kInt_GrVertexAttribType == textureGP.fTextureIdx.fType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400141 SkASSERT(args.fShaderCaps->integerSupport());
142 args.fFragBuilder->codeAppend("int texIdx;");
Chris Dalton7b046312018-02-02 11:06:30 -0700143 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx, "texIdx",
144 Interpolation::kMustBeFlat);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400145 args.fFragBuilder->codeAppend("switch (texIdx) {");
146 for (int i = 0; i < textureGP.numTextureSamplers(); ++i) {
147 args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor);
148 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
149 args.fTexSamplers[i],
150 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400151 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400152 &fColorSpaceXformHelper);
153 args.fFragBuilder->codeAppend("; break;");
154 }
155 args.fFragBuilder->codeAppend("}");
156 } else {
157 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
158 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
159 args.fTexSamplers[0],
160 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400161 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400162 &fColorSpaceXformHelper);
163 }
Brian Salomon34169692017-08-28 15:32:01 -0400164 args.fFragBuilder->codeAppend(";");
Brian Salomon485b8c62018-01-12 15:11:06 -0500165 if (textureGP.usesCoverageEdgeAA()) {
Brian Salomondba65f92018-01-22 08:43:38 -0500166 const char* aaDistName = nullptr;
167 // When interpolation is innacurate we perform the evaluation of the edge
168 // equations in the fragment shader rather than interpolating values computed
169 // in the vertex shader.
170 if (!args.fShaderCaps->interpolantsAreInaccurate()) {
171 GrGLSLVarying aaDistVarying(kFloat4_GrSLType,
172 GrGLSLVarying::Scope::kVertToFrag);
173 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
174 args.fVertBuilder->codeAppendf(
175 R"(%s = float4(dot(aaEdge0.xy, %s.xy) + aaEdge0.z,
176 dot(aaEdge1.xy, %s.xy) + aaEdge1.z,
177 dot(aaEdge2.xy, %s.xy) + aaEdge2.z,
178 dot(aaEdge3.xy, %s.xy) + aaEdge3.z);)",
179 aaDistVarying.vsOut(), textureGP.fPositions.fName,
180 textureGP.fPositions.fName, textureGP.fPositions.fName,
181 textureGP.fPositions.fName);
182 aaDistName = aaDistVarying.fsIn();
183 } else {
184 GrGLSLVarying aaEdgeVarying[4]{
185 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
186 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
187 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
188 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag}
189 };
190 for (int i = 0; i < 4; ++i) {
191 SkString name;
192 name.printf("aaEdge%d", i);
Brian Salomon7d982c62018-02-05 16:20:47 -0500193 args.fVaryingHandler->addVarying(name.c_str(), &aaEdgeVarying[i],
194 Interpolation::kCanBeFlat);
Brian Salomondba65f92018-01-22 08:43:38 -0500195 args.fVertBuilder->codeAppendf(
196 "%s = aaEdge%d;", aaEdgeVarying[i].vsOut(), i);
197 }
198 args.fFragBuilder->codeAppendf(
199 R"(float4 aaDists = float4(dot(%s.xy, sk_FragCoord.xy) + %s.z,
200 dot(%s.xy, sk_FragCoord.xy) + %s.z,
201 dot(%s.xy, sk_FragCoord.xy) + %s.z,
202 dot(%s.xy, sk_FragCoord.xy) + %s.z);)",
203 aaEdgeVarying[0].fsIn(), aaEdgeVarying[0].fsIn(),
204 aaEdgeVarying[1].fsIn(), aaEdgeVarying[1].fsIn(),
205 aaEdgeVarying[2].fsIn(), aaEdgeVarying[2].fsIn(),
206 aaEdgeVarying[3].fsIn(), aaEdgeVarying[3].fsIn());
207 aaDistName = "aaDists";
208 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500209 args.fFragBuilder->codeAppendf(
210 "float mindist = min(min(%s.x, %s.y), min(%s.z, %s.w));",
Brian Salomondba65f92018-01-22 08:43:38 -0500211 aaDistName, aaDistName, aaDistName, aaDistName);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500212 args.fFragBuilder->codeAppendf("%s = float4(clamp(mindist, 0, 1));",
213 args.fOutputCoverage);
214 } else {
215 args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
216 }
Brian Salomon34169692017-08-28 15:32:01 -0400217 }
218 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
219 };
220 return new GLSLProcessor;
221 }
222
Brian Salomon485b8c62018-01-12 15:11:06 -0500223 bool usesCoverageEdgeAA() const { return SkToBool(fAAEdges[0].isInitialized()); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500224
Brian Salomon34169692017-08-28 15:32:01 -0400225private:
Brian Salomon336ce7b2017-09-08 08:23:58 -0400226 // This exists to reduce the number of shaders generated. It does some rounding of sampler
227 // counts.
228 static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) {
229 SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures &&
230 numRealProxies <= caps.maxFragmentSamplers());
231 if (1 == numRealProxies) {
232 return 1;
233 }
234 if (numRealProxies <= 4) {
235 return 4;
236 }
237 // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps.
238 return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers()));
239 }
240
241 TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt,
Brian Salomon485b8c62018-01-12 15:11:06 -0500242 sk_sp<GrColorSpaceXform> csxf, bool coverageAA,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500243 const GrSamplerState::Filter filters[], const GrShaderCaps& caps)
244 : INHERITED(kTextureGeometryProcessor_ClassID), fColorSpaceXform(std::move(csxf)) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400245 SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400246 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400247 fSamplers[0].reset(std::move(proxies[0]), filters[0]);
248 this->addTextureSampler(&fSamplers[0]);
249 for (int i = 1; i < proxyCnt; ++i) {
250 // This class has one sampler built in, the rest come from memory this processor was
251 // placement-newed into and so haven't been constructed.
252 new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]);
253 this->addTextureSampler(&fSamplers[i]);
254 }
255 if (samplerCnt > 1) {
256 // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt
257 // times.
258 GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy();
259 for (int i = proxyCnt; i < samplerCnt; ++i) {
260 new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]);
261 this->addTextureSampler(&fSamplers[i]);
262 }
263 SkASSERT(caps.integerSupport());
264 fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType);
265 }
266
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400267 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
Brian Salomon485b8c62018-01-12 15:11:06 -0500268 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500269 fAAEdges[0] = this->addVertexAttrib("aaEdge0", kFloat3_GrVertexAttribType);
270 fAAEdges[1] = this->addVertexAttrib("aaEdge1", kFloat3_GrVertexAttribType);
271 fAAEdges[2] = this->addVertexAttrib("aaEdge2", kFloat3_GrVertexAttribType);
272 fAAEdges[3] = this->addVertexAttrib("aaEdge3", kFloat3_GrVertexAttribType);
273 }
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400274 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Brian Salomon34169692017-08-28 15:32:01 -0400275 }
276
277 Attribute fPositions;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400278 Attribute fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400279 Attribute fTextureCoords;
280 Attribute fColors;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500281 Attribute fAAEdges[4];
Brian Salomon34169692017-08-28 15:32:01 -0400282 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400283 TextureSampler fSamplers[1];
Ethan Nicholasabff9562017-10-09 10:54:08 -0400284
285 typedef GrGeometryProcessor INHERITED;
Brian Salomon34169692017-08-28 15:32:01 -0400286};
287
Brian Salomon6872e942018-05-18 10:29:54 -0400288// This computes the four edge equations for a quad, then outsets them and computes a new quad
289// as the intersection points of the outset edges. 'x' and 'y' contain the original points as input
290// and the outset points as output. 'a', 'b', and 'c' are the edge equation coefficients on output.
291static void compute_quad_edges_and_outset_vertices(Sk4f* x, Sk4f* y, Sk4f* a, Sk4f* b, Sk4f* c) {
292 static constexpr auto fma = SkNx_fma<4, float>;
293 // These rotate the points/edge values either clockwise or counterclockwise assuming tri strip
294 // order.
295 auto nextCW = [](const Sk4f& v) { return SkNx_shuffle<2, 0, 3, 1>(v); };
296 auto nextCCW = [](const Sk4f& v) { return SkNx_shuffle<1, 3, 0, 2>(v); };
297
298 auto xnext = nextCCW(*x);
299 auto ynext = nextCCW(*y);
300 *a = ynext - *y;
301 *b = *x - xnext;
302 *c = fma(xnext, *y, -ynext * *x);
303 Sk4f invNormLengths = (*a * *a + *b * *b).rsqrt();
304 // Make sure the edge equations have their normals facing into the quad in device space.
305 auto test = fma(*a, nextCW(*x), fma(*b, nextCW(*y), *c));
306 if ((test < Sk4f(0)).anyTrue()) {
307 invNormLengths = -invNormLengths;
308 }
309 *a *= invNormLengths;
310 *b *= invNormLengths;
311 *c *= invNormLengths;
312
313 // Here is the outset. This makes our edge equations compute coverage without requiring a
314 // half pixel offset and is also used to compute the bloated quad that will cover all
315 // pixels.
316 *c += Sk4f(0.5f);
317
318 // Reverse the process to compute the points of the bloated quad from the edge equations.
319 // This time the inputs don't have 1s as their third coord and we want to homogenize rather
320 // than normalize.
321 auto anext = nextCW(*a);
322 auto bnext = nextCW(*b);
323 auto cnext = nextCW(*c);
324 *x = fma(bnext, *c, -*b * cnext);
325 *y = fma(*a, cnext, -anext * *c);
326 auto ic = (fma(anext, *b, -bnext * *a)).invert();
327 *x *= ic;
328 *y *= ic;
329}
330
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500331namespace {
332// This is a class soley so it can be partially specialized (functions cannot be).
333template<GrAA, typename Vertex> class VertexAAHandler;
334
335template<typename Vertex> class VertexAAHandler<GrAA::kNo, Vertex> {
336public:
337 static void AssignPositionsAndTexCoords(Vertex* vertices, const GrQuad& quad,
338 const SkRect& texRect) {
339 vertices[0].fPosition = quad.point(0);
340 vertices[0].fTextureCoords = {texRect.fLeft, texRect.fTop};
341 vertices[1].fPosition = quad.point(1);
342 vertices[1].fTextureCoords = {texRect.fLeft, texRect.fBottom};
343 vertices[2].fPosition = quad.point(2);
344 vertices[2].fTextureCoords = {texRect.fRight, texRect.fTop};
345 vertices[3].fPosition = quad.point(3);
346 vertices[3].fTextureCoords = {texRect.fRight, texRect.fBottom};
347 }
348};
349
350template<typename Vertex> class VertexAAHandler<GrAA::kYes, Vertex> {
351public:
352 static void AssignPositionsAndTexCoords(Vertex* vertices, const GrQuad& quad,
353 const SkRect& texRect) {
Brian Salomon6872e942018-05-18 10:29:54 -0400354 auto x = quad.x4f();
355 auto y = quad.y4f();
356 Sk4f a, b, c;
357 compute_quad_edges_and_outset_vertices(&x, &y, &a, &b, &c);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500358
359 for (int i = 0; i < 4; ++i) {
Brian Salomon6872e942018-05-18 10:29:54 -0400360 vertices[i].fPosition = {x[i], y[i]};
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500361 for (int j = 0; j < 4; ++j) {
Brian Salomon6872e942018-05-18 10:29:54 -0400362 vertices[i].fEdges[j] = {a[j], b[j], c[j]};
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500363 }
364 }
365
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500366 AssignTexCoords(vertices, quad, texRect);
367 }
368
369private:
370 static void AssignTexCoords(Vertex* vertices, const GrQuad& quad, const SkRect& tex) {
Brian Salomona33b67c2018-05-17 10:42:14 -0400371 SkMatrix q = SkMatrix::MakeAll(quad.x(0), quad.x(1), quad.x(2),
372 quad.y(0), quad.y(1), quad.y(2),
373 1.f, 1.f, 1.f);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500374 SkMatrix qinv;
375 if (!q.invert(&qinv)) {
376 return;
377 }
378 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
379 tex.fTop, tex.fBottom, tex.fTop,
380 1.f, 1.f, 1.f);
381 SkMatrix map;
382 map.setConcat(t, qinv);
383 SkMatrixPriv::MapPointsWithStride(map, &vertices[0].fTextureCoords, sizeof(Vertex),
384 &vertices[0].fPosition, sizeof(Vertex), 4);
385 }
386};
387
388template <typename Vertex, bool IsMultiTex> struct TexIdAssigner;
389
390template <typename Vertex> struct TexIdAssigner<Vertex, true> {
391 static void Assign(Vertex* vertices, int textureIdx) {
392 vertices[0].fTextureIdx = textureIdx;
393 vertices[1].fTextureIdx = textureIdx;
394 vertices[2].fTextureIdx = textureIdx;
395 vertices[3].fTextureIdx = textureIdx;
396 }
397};
398
399template <typename Vertex> struct TexIdAssigner<Vertex, false> {
400 static void Assign(Vertex* vertices, int textureIdx) {}
401};
402} // anonymous namespace
403
404template <typename Vertex, bool IsMultiTex, GrAA AA>
405static void tessellate_quad(const GrQuad& devQuad, const SkRect& srcRect, GrColor color,
406 GrSurfaceOrigin origin, Vertex* vertices, SkScalar iw, SkScalar ih,
407 int textureIdx) {
408 SkRect texRect = {
409 iw * srcRect.fLeft,
410 ih * srcRect.fTop,
411 iw * srcRect.fRight,
412 ih * srcRect.fBottom
413 };
414 if (origin == kBottomLeft_GrSurfaceOrigin) {
415 texRect.fTop = 1.f - texRect.fTop;
416 texRect.fBottom = 1.f - texRect.fBottom;
417 }
418 VertexAAHandler<AA, Vertex>::AssignPositionsAndTexCoords(vertices, devQuad, texRect);
419 vertices[0].fColor = color;
420 vertices[1].fColor = color;
421 vertices[2].fColor = color;
422 vertices[3].fColor = color;
423 TexIdAssigner<Vertex, IsMultiTex>::Assign(vertices, textureIdx);
424}
Brian Salomon34169692017-08-28 15:32:01 -0400425/**
426 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
427 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
428 */
429class TextureOp final : public GrMeshDrawOp {
430public:
431 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400432 GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500433 const SkRect& srcRect, const SkRect& dstRect,
434 GrAAType aaType, const SkMatrix& viewMatrix,
435 sk_sp<GrColorSpaceXform> csxf, bool allowSRBInputs) {
Brian Salomon34169692017-08-28 15:32:01 -0400436 return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect,
Brian Salomon485b8c62018-01-12 15:11:06 -0500437 dstRect, aaType, viewMatrix, std::move(csxf),
Brian Salomon34169692017-08-28 15:32:01 -0400438 allowSRBInputs));
439 }
440
Brian Salomon336ce7b2017-09-08 08:23:58 -0400441 ~TextureOp() override {
442 if (fFinalized) {
443 auto proxies = this->proxies();
444 for (int i = 0; i < fProxyCnt; ++i) {
445 proxies[i]->completedRead();
446 }
447 if (fProxyCnt > 1) {
448 delete[] reinterpret_cast<const char*>(proxies);
449 }
450 } else {
451 SkASSERT(1 == fProxyCnt);
452 fProxy0->unref();
453 }
454 }
Brian Salomon34169692017-08-28 15:32:01 -0400455
456 const char* name() const override { return "TextureOp"; }
457
Robert Phillipsf1748f52017-09-14 14:11:24 -0400458 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400459 auto proxies = this->proxies();
460 for (int i = 0; i < fProxyCnt; ++i) {
461 func(proxies[i]);
462 }
463 }
464
Brian Salomon34169692017-08-28 15:32:01 -0400465 SkString dumpInfo() const override {
466 SkString str;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400467 str.appendf("AllowSRGBInputs: %d\n", fAllowSRGBInputs);
Brian Salomon34169692017-08-28 15:32:01 -0400468 str.appendf("# draws: %d\n", fDraws.count());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400469 auto proxies = this->proxies();
470 for (int i = 0; i < fProxyCnt; ++i) {
471 str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(),
472 static_cast<int>(this->filters()[i]));
473 }
Brian Salomon34169692017-08-28 15:32:01 -0400474 for (int i = 0; i < fDraws.count(); ++i) {
475 const Draw& draw = fDraws[i];
476 str.appendf(
Brian Salomon336ce7b2017-09-08 08:23:58 -0400477 "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
478 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
479 i, draw.fColor, draw.fTextureIdx, draw.fSrcRect.fLeft, draw.fSrcRect.fTop,
Brian Salomona33b67c2018-05-17 10:42:14 -0400480 draw.fSrcRect.fRight, draw.fSrcRect.fBottom, draw.fQuad.point(0).fX,
481 draw.fQuad.point(0).fY, draw.fQuad.point(1).fX, draw.fQuad.point(1).fY,
482 draw.fQuad.point(2).fX, draw.fQuad.point(2).fY, draw.fQuad.point(3).fX,
483 draw.fQuad.point(3).fY);
Brian Salomon34169692017-08-28 15:32:01 -0400484 }
485 str += INHERITED::dumpInfo();
486 return str;
487 }
488
Brian Osman9a725dd2017-09-20 09:53:22 -0400489 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
490 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon34169692017-08-28 15:32:01 -0400491 SkASSERT(!fFinalized);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400492 SkASSERT(1 == fProxyCnt);
Brian Salomon34169692017-08-28 15:32:01 -0400493 fFinalized = true;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400494 fProxy0->addPendingRead();
495 fProxy0->unref();
Brian Salomon34169692017-08-28 15:32:01 -0400496 return RequiresDstTexture::kNo;
497 }
498
Brian Salomon485b8c62018-01-12 15:11:06 -0500499 FixedFunctionFlags fixedFunctionFlags() const override {
500 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
501 : FixedFunctionFlags::kNone;
502 }
Brian Salomon34169692017-08-28 15:32:01 -0400503
504 DEFINE_OP_CLASS_ID
505
506private:
Brian Salomon762d5e72017-12-01 10:25:08 -0500507
508 // This is used in a heursitic for choosing a code path. We don't care what happens with
509 // really large rects, infs, nans, etc.
510#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
511__attribute__((no_sanitize("float-cast-overflow")))
512#endif
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500513 size_t RectSizeAsSizeT(const SkRect& rect) {;
Brian Salomon762d5e72017-12-01 10:25:08 -0500514 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
515 }
516
Brian Salomon336ce7b2017-09-08 08:23:58 -0400517 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
518
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400519 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500520 const SkRect& srcRect, const SkRect& dstRect, GrAAType aaType,
521 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs)
Brian Salomon34169692017-08-28 15:32:01 -0400522 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400523 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400524 , fProxy0(proxy.release())
525 , fFilter0(filter)
526 , fProxyCnt(1)
Brian Salomon485b8c62018-01-12 15:11:06 -0500527 , fAAType(static_cast<unsigned>(aaType))
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500528 , fFinalized(0)
529 , fAllowSRGBInputs(allowSRGBInputs ? 1 : 0) {
Brian Salomon485b8c62018-01-12 15:11:06 -0500530 SkASSERT(aaType != GrAAType::kMixedSamples);
Brian Salomon34169692017-08-28 15:32:01 -0400531 Draw& draw = fDraws.push_back();
532 draw.fSrcRect = srcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400533 draw.fTextureIdx = 0;
Brian Salomon34169692017-08-28 15:32:01 -0400534 draw.fColor = color;
Brian Salomona33b67c2018-05-17 10:42:14 -0400535 draw.fQuad = GrQuad(dstRect, viewMatrix);
536 SkRect bounds = draw.fQuad.bounds();
Brian Salomon34169692017-08-28 15:32:01 -0400537 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon762d5e72017-12-01 10:25:08 -0500538
539 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400540 }
541
542 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400543 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
544 auto proxies = this->proxies();
545 auto filters = this->filters();
546 for (int i = 0; i < fProxyCnt; ++i) {
547 if (!proxies[i]->instantiate(target->resourceProvider())) {
548 return;
549 }
550 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400551 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400552
Brian Salomon485b8c62018-01-12 15:11:06 -0500553 bool coverageAA = GrAAType::kCoverage == this->aaType();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400554 sk_sp<GrGeometryProcessor> gp =
555 TextureGeometryProcessor::Make(proxiesSPs, fProxyCnt, std::move(fColorSpaceXform),
Brian Salomon485b8c62018-01-12 15:11:06 -0500556 coverageAA, filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400557 GrPipeline::InitArgs args;
558 args.fProxy = target->proxy();
559 args.fCaps = &target->caps();
560 args.fResourceProvider = target->resourceProvider();
Brian Salomon485b8c62018-01-12 15:11:06 -0500561 args.fFlags = 0;
562 if (fAllowSRGBInputs) {
563 args.fFlags |= GrPipeline::kAllowSRGBInputs_Flag;
564 }
565 if (GrAAType::kMSAA == this->aaType()) {
566 args.fFlags |= GrPipeline::kHWAntialias_Flag;
567 }
568
Brian Salomon34169692017-08-28 15:32:01 -0400569 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
570 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400571 int vstart;
572 const GrBuffer* vbuffer;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400573 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
574 &vstart);
575 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400576 SkDebugf("Could not allocate vertices\n");
577 return;
578 }
Brian Salomon57caa662017-10-18 12:21:05 +0000579 if (1 == fProxyCnt) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500580 GrSurfaceOrigin origin = proxies[0]->origin();
581 GrTexture* texture = proxies[0]->priv().peekTexture();
582 float iw = 1.f / texture->width();
583 float ih = 1.f / texture->height();
Brian Salomon485b8c62018-01-12 15:11:06 -0500584 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500585 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::AAVertex));
586 auto vertices = static_cast<TextureGeometryProcessor::AAVertex*>(vdata);
587 for (int i = 0; i < fDraws.count(); ++i) {
588 tessellate_quad<TextureGeometryProcessor::AAVertex, false, GrAA::kYes>(
589 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
590 vertices + 4 * i, iw, ih, 0);
Brian Salomon57caa662017-10-18 12:21:05 +0000591 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500592 } else {
593 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex));
594 auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata);
595 for (int i = 0; i < fDraws.count(); ++i) {
596 tessellate_quad<TextureGeometryProcessor::Vertex, false, GrAA::kNo>(
597 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
598 vertices + 4 * i, iw, ih, 0);
599 }
Brian Salomon57caa662017-10-18 12:21:05 +0000600 }
601 } else {
Brian Salomon57caa662017-10-18 12:21:05 +0000602 GrTexture* textures[kMaxTextures];
603 float iw[kMaxTextures];
604 float ih[kMaxTextures];
605 for (int t = 0; t < fProxyCnt; ++t) {
606 textures[t] = proxies[t]->priv().peekTexture();
607 iw[t] = 1.f / textures[t]->width();
608 ih[t] = 1.f / textures[t]->height();
609 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500610 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500611 SkASSERT(gp->getVertexStride() ==
612 sizeof(TextureGeometryProcessor::AAMultiTextureVertex));
613 auto vertices = static_cast<TextureGeometryProcessor::AAMultiTextureVertex*>(vdata);
614 for (int i = 0; i < fDraws.count(); ++i) {
615 auto tidx = fDraws[i].fTextureIdx;
616 GrSurfaceOrigin origin = proxies[tidx]->origin();
617 tessellate_quad<TextureGeometryProcessor::AAMultiTextureVertex, true,
618 GrAA::kYes>(fDraws[i].fQuad, fDraws[i].fSrcRect,
619 fDraws[i].fColor, origin, vertices + 4 * i,
620 iw[tidx], ih[tidx], tidx);
Brian Salomon57caa662017-10-18 12:21:05 +0000621 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500622 } else {
623 SkASSERT(gp->getVertexStride() ==
624 sizeof(TextureGeometryProcessor::MultiTextureVertex));
625 auto vertices = static_cast<TextureGeometryProcessor::MultiTextureVertex*>(vdata);
626 for (int i = 0; i < fDraws.count(); ++i) {
627 auto tidx = fDraws[i].fTextureIdx;
628 GrSurfaceOrigin origin = proxies[tidx]->origin();
629 tessellate_quad<TextureGeometryProcessor::MultiTextureVertex, true, GrAA::kNo>(
630 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
631 vertices + 4 * i, iw[tidx], ih[tidx], tidx);
632 }
Brian Salomon57caa662017-10-18 12:21:05 +0000633 }
634 }
635 GrPrimitiveType primitiveType =
636 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
637 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400638 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000639 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400640 if (!ibuffer) {
641 SkDebugf("Could not allocate quad indices\n");
642 return;
643 }
Brian Salomon34169692017-08-28 15:32:01 -0400644 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
645 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400646 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400647 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400648 }
Brian Salomon57caa662017-10-18 12:21:05 +0000649 mesh.setVertexData(vbuffer, vstart);
650 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400651 }
652
653 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
654 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500655 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400656 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400657 return false;
658 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500659 if (this->aaType() != that->aaType()) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500660 return false;
661 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400662 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
663 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500664 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
665 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
666 that->fMaxApproxDstPixelArea <=
667 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400668 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500669 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400670 if (numNewProxies < 0) {
671 return false;
672 }
673 if (1 == fProxyCnt && numNewProxies) {
674 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
675 kMaxTextures];
676 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
677 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
678 proxies[0] = fProxy0;
679 filters[0] = fFilter0;
680 fProxyArray = proxies;
681 }
682 fProxyCnt += numNewProxies;
683 auto thisProxies = fProxyArray;
684 auto thatProxies = that->proxies();
685 auto thatFilters = that->filters();
686 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
687 kMaxTextures);
688 for (int i = 0; i < that->fProxyCnt; ++i) {
689 if (map[i] < 0) {
690 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400691
Brian Salomon336ce7b2017-09-08 08:23:58 -0400692 thisProxies[-map[i]] = thatProxies[i];
693 thisFilters[-map[i]] = thatFilters[i];
694 map[i] = -map[i];
695 }
696 }
697 int firstNewDraw = fDraws.count();
698 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
699 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
700 fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx];
701 }
702 } else {
Brian Salomonbbf05752017-11-30 11:30:48 -0500703 // We can get here when one of the ops is already multitextured but the other cannot
704 // be because of the dst rect size.
705 if (fProxyCnt > 1 || that->fProxyCnt > 1) {
706 return false;
707 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400708 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
709 return false;
710 }
711 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
712 }
Brian Salomon34169692017-08-28 15:32:01 -0400713 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500714 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomon34169692017-08-28 15:32:01 -0400715 return true;
716 }
717
Brian Salomon336ce7b2017-09-08 08:23:58 -0400718 /**
719 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
720 * value means that's proxy should be added to this's proxy array at the absolute value of
721 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
722 * negative value is returned. Otherwise, return value indicates the number of proxies that have
723 * to be added to this op or, equivalently, the number of negative entries in map.
724 */
725 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
726 std::fill_n(map, kMaxTextures, -kMaxTextures);
727 int sharedProxyCnt = 0;
728 auto thisProxies = this->proxies();
729 auto thisFilters = this->filters();
730 auto thatProxies = that->proxies();
731 auto thatFilters = that->filters();
732 for (int i = 0; i < fProxyCnt; ++i) {
733 for (int j = 0; j < that->fProxyCnt; ++j) {
734 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
735 if (thisFilters[i] != thatFilters[j]) {
736 // In GL we don't currently support using the same texture with different
737 // samplers. If we added support for sampler objects and a cap bit to know
738 // it's ok to use different filter modes then we could support this.
739 // Otherwise, we could also only allow a single filter mode for each op
740 // instance.
741 return -1;
742 }
743 map[j] = i;
744 ++sharedProxyCnt;
745 break;
746 }
747 }
748 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500749 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400750 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
751 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
752 return -1;
753 }
754 GrPixelConfig config = thisProxies[0]->config();
755 int nextSlot = fProxyCnt;
756 for (int j = 0; j < that->fProxyCnt; ++j) {
757 // We want to avoid making many shaders because of different permutations of shader
758 // based swizzle and sampler types. The approach taken here is to require the configs to
759 // be the same and to only allow already instantiated proxies that have the most
760 // common sampler type. Otherwise we don't merge.
761 if (thatProxies[j]->config() != config) {
762 return -1;
763 }
764 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
765 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
766 return -1;
767 }
768 }
769 if (map[j] < 0) {
770 map[j] = -(nextSlot++);
771 }
772 }
773 return newProxyCnt;
774 }
775
Brian Salomon485b8c62018-01-12 15:11:06 -0500776 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500777
Brian Salomon336ce7b2017-09-08 08:23:58 -0400778 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
779
780 const GrSamplerState::Filter* filters() const {
781 if (fProxyCnt > 1) {
782 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
783 }
784 return &fFilter0;
785 }
786
Brian Salomon34169692017-08-28 15:32:01 -0400787 struct Draw {
788 SkRect fSrcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400789 int fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400790 GrQuad fQuad;
791 GrColor fColor;
792 };
793 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -0400794 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400795 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
796 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
797 // followed by kMaxTextures filters.
798 union {
799 GrTextureProxy* fProxy0;
800 GrTextureProxy** fProxyArray;
801 };
Brian Salomonbbf05752017-11-30 11:30:48 -0500802 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400803 GrSamplerState::Filter fFilter0;
804 uint8_t fProxyCnt;
Brian Salomon485b8c62018-01-12 15:11:06 -0500805 unsigned fAAType : 2;
Brian Salomon34169692017-08-28 15:32:01 -0400806 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500807 unsigned fFinalized : 1;
808 unsigned fAllowSRGBInputs : 1;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400809
Brian Salomon34169692017-08-28 15:32:01 -0400810 typedef GrMeshDrawOp INHERITED;
811};
812
Brian Salomon336ce7b2017-09-08 08:23:58 -0400813constexpr int TextureGeometryProcessor::kMaxTextures;
814constexpr int TextureOp::kMaxTextures;
815
Brian Salomon34169692017-08-28 15:32:01 -0400816} // anonymous namespace
817
818namespace GrTextureOp {
819
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400820std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter,
Brian Salomon485b8c62018-01-12 15:11:06 -0500821 GrColor color, const SkRect& srcRect, const SkRect& dstRect,
822 GrAAType aaType, const SkMatrix& viewMatrix,
823 sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs) {
Brian Salomon34169692017-08-28 15:32:01 -0400824 SkASSERT(!viewMatrix.hasPerspective());
Brian Salomon485b8c62018-01-12 15:11:06 -0500825 return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, aaType, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400826 std::move(csxf), allowSRGBInputs);
827}
828
829} // namespace GrTextureOp
830
831#if GR_TEST_UTILS
832#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500833#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500834#include "GrProxyProvider.h"
Brian Salomon34169692017-08-28 15:32:01 -0400835
836GR_DRAW_OP_TEST_DEFINE(TextureOp) {
837 GrSurfaceDesc desc;
838 desc.fConfig = kRGBA_8888_GrPixelConfig;
839 desc.fHeight = random->nextULessThan(90) + 10;
840 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500841 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon34169692017-08-28 15:32:01 -0400842 SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500843
844 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomon2a4f9832018-03-03 22:43:43 -0500845 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(desc, origin, fit, SkBudgeted::kNo);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500846
Brian Salomon34169692017-08-28 15:32:01 -0400847 SkRect rect = GrTest::TestRect(random);
848 SkRect srcRect;
849 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
850 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
851 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
852 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
853 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
854 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400855 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
856 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Brian Salomon34169692017-08-28 15:32:01 -0400857 auto csxf = GrTest::TestColorXform(random);
858 bool allowSRGBInputs = random->nextBool();
Brian Salomon485b8c62018-01-12 15:11:06 -0500859 GrAAType aaType = GrAAType::kNone;
860 if (random->nextBool()) {
861 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
862 }
863 return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, aaType, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400864 std::move(csxf), allowSRGBInputs);
865}
866
867#endif