blob: 2aeb49beb6f75f0202500bee620dd23d3cb72c39 [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 Salomonb5ef1f92018-01-11 11:46:21 -050023#include "SkPoint.h"
24#include "SkPoint3.h"
Brian Salomon34169692017-08-28 15:32:01 -040025#include "glsl/GrGLSLColorSpaceXformHelper.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050026#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon34169692017-08-28 15:32:01 -040027#include "glsl/GrGLSLGeometryProcessor.h"
28#include "glsl/GrGLSLVarying.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050029#include "glsl/GrGLSLVertexGeoBuilder.h"
Brian Salomon34169692017-08-28 15:32:01 -040030
31namespace {
32
33/**
34 * Geometry Processor that draws a texture modulated by a vertex color (though, this is meant to be
35 * the same value across all vertices of a quad and uses flat interpolation when available). This is
36 * used by TextureOp below.
37 */
38class TextureGeometryProcessor : public GrGeometryProcessor {
39public:
40 struct Vertex {
41 SkPoint fPosition;
42 SkPoint fTextureCoords;
43 GrColor fColor;
44 };
Brian Salomonb5ef1f92018-01-11 11:46:21 -050045 struct AAVertex {
46 SkPoint fPosition;
47 SkPoint fTextureCoords;
48 SkPoint3 fEdges[4];
49 GrColor fColor;
50 };
Brian Salomon336ce7b2017-09-08 08:23:58 -040051 struct MultiTextureVertex {
52 SkPoint fPosition;
53 int fTextureIdx;
54 SkPoint fTextureCoords;
55 GrColor fColor;
56 };
Brian Salomonb5ef1f92018-01-11 11:46:21 -050057 struct AAMultiTextureVertex {
58 SkPoint fPosition;
59 int fTextureIdx;
60 SkPoint fTextureCoords;
61 SkPoint3 fEdges[4];
62 GrColor fColor;
63 };
Brian Salomon336ce7b2017-09-08 08:23:58 -040064
65 // Maximum number of textures supported by this op. Must also be checked against the caps
66 // limit. These numbers were based on some limited experiments on a HP Z840 and Pixel XL 2016
67 // and could probably use more tuning.
68#ifdef SK_BUILD_FOR_ANDROID
69 static constexpr int kMaxTextures = 4;
70#else
71 static constexpr int kMaxTextures = 8;
72#endif
73
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040074 static int SupportsMultitexture(const GrShaderCaps& caps) {
Brian Salomon762d5e72017-12-01 10:25:08 -050075 return caps.integerSupport() && caps.maxFragmentSamplers() > 1;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040076 }
Brian Salomon336ce7b2017-09-08 08:23:58 -040077
78 static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxies[], int proxyCnt,
Brian Salomon485b8c62018-01-12 15:11:06 -050079 sk_sp<GrColorSpaceXform> csxf, bool coverageAA,
Brian Salomon336ce7b2017-09-08 08:23:58 -040080 const GrSamplerState::Filter filters[],
81 const GrShaderCaps& caps) {
82 // We use placement new to avoid always allocating space for kMaxTextures TextureSampler
83 // instances.
84 int samplerCnt = NumSamplersToUse(proxyCnt, caps);
85 size_t size = sizeof(TextureGeometryProcessor) + sizeof(TextureSampler) * (samplerCnt - 1);
86 void* mem = GrGeometryProcessor::operator new(size);
87 return sk_sp<TextureGeometryProcessor>(new (mem) TextureGeometryProcessor(
Brian Salomon485b8c62018-01-12 15:11:06 -050088 proxies, proxyCnt, samplerCnt, std::move(csxf), coverageAA, filters, caps));
Brian Salomon336ce7b2017-09-08 08:23:58 -040089 }
90
91 ~TextureGeometryProcessor() override {
92 int cnt = this->numTextureSamplers();
93 for (int i = 1; i < cnt; ++i) {
94 fSamplers[i].~TextureSampler();
95 }
Brian Salomon34169692017-08-28 15:32:01 -040096 }
97
98 const char* name() const override { return "TextureGeometryProcessor"; }
99
100 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
101 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
Brian Salomon485b8c62018-01-12 15:11:06 -0500102 b->add32(static_cast<uint32_t>(this->usesCoverageEdgeAA()));
Brian Salomon34169692017-08-28 15:32:01 -0400103 }
104
105 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
106 class GLSLProcessor : public GrGLSLGeometryProcessor {
107 public:
108 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
109 FPCoordTransformIter&& transformIter) override {
110 const auto& textureGP = proc.cast<TextureGeometryProcessor>();
111 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
112 if (fColorSpaceXformHelper.isValid()) {
113 fColorSpaceXformHelper.setData(pdman, textureGP.fColorSpaceXform.get());
114 }
115 }
116
117 private:
118 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Chris Dalton7b046312018-02-02 11:06:30 -0700119 using Interpolation = GrGLSLVaryingHandler::Interpolation;
Brian Salomon34169692017-08-28 15:32:01 -0400120 const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>();
121 fColorSpaceXformHelper.emitCode(
122 args.fUniformHandler, textureGP.fColorSpaceXform.get());
123 args.fVaryingHandler->setNoPerspective();
124 args.fVaryingHandler->emitAttributes(textureGP);
125 this->writeOutputPosition(args.fVertBuilder, gpArgs, textureGP.fPositions.fName);
126 this->emitTransforms(args.fVertBuilder,
127 args.fVaryingHandler,
128 args.fUniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -0500129 textureGP.fTextureCoords.asShaderVar(),
Brian Salomon34169692017-08-28 15:32:01 -0400130 args.fFPCoordTransformHandler);
Chris Dalton7b046312018-02-02 11:06:30 -0700131 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors,
132 args.fOutputColor,
133 Interpolation::kCanBeFlat);
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400134 args.fFragBuilder->codeAppend("float2 texCoord;");
Chris Daltonfdde34e2017-10-16 14:15:26 -0600135 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords,
136 "texCoord");
Brian Salomon336ce7b2017-09-08 08:23:58 -0400137 if (textureGP.numTextureSamplers() > 1) {
Chris Dalton7b046312018-02-02 11:06:30 -0700138 // If this changes to float, reconsider Interpolation::kMustBeFlat.
139 SkASSERT(kInt_GrVertexAttribType == textureGP.fTextureIdx.fType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400140 SkASSERT(args.fShaderCaps->integerSupport());
141 args.fFragBuilder->codeAppend("int texIdx;");
Chris Dalton7b046312018-02-02 11:06:30 -0700142 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx, "texIdx",
143 Interpolation::kMustBeFlat);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400144 args.fFragBuilder->codeAppend("switch (texIdx) {");
145 for (int i = 0; i < textureGP.numTextureSamplers(); ++i) {
146 args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor);
147 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
148 args.fTexSamplers[i],
149 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400150 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400151 &fColorSpaceXformHelper);
152 args.fFragBuilder->codeAppend("; break;");
153 }
154 args.fFragBuilder->codeAppend("}");
155 } else {
156 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
157 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
158 args.fTexSamplers[0],
159 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400160 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400161 &fColorSpaceXformHelper);
162 }
Brian Salomon34169692017-08-28 15:32:01 -0400163 args.fFragBuilder->codeAppend(";");
Brian Salomon485b8c62018-01-12 15:11:06 -0500164 if (textureGP.usesCoverageEdgeAA()) {
Brian Salomondba65f92018-01-22 08:43:38 -0500165 const char* aaDistName = nullptr;
166 // When interpolation is innacurate we perform the evaluation of the edge
167 // equations in the fragment shader rather than interpolating values computed
168 // in the vertex shader.
169 if (!args.fShaderCaps->interpolantsAreInaccurate()) {
170 GrGLSLVarying aaDistVarying(kFloat4_GrSLType,
171 GrGLSLVarying::Scope::kVertToFrag);
172 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
173 args.fVertBuilder->codeAppendf(
174 R"(%s = float4(dot(aaEdge0.xy, %s.xy) + aaEdge0.z,
175 dot(aaEdge1.xy, %s.xy) + aaEdge1.z,
176 dot(aaEdge2.xy, %s.xy) + aaEdge2.z,
177 dot(aaEdge3.xy, %s.xy) + aaEdge3.z);)",
178 aaDistVarying.vsOut(), textureGP.fPositions.fName,
179 textureGP.fPositions.fName, textureGP.fPositions.fName,
180 textureGP.fPositions.fName);
181 aaDistName = aaDistVarying.fsIn();
182 } else {
183 GrGLSLVarying aaEdgeVarying[4]{
184 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
185 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
186 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
187 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag}
188 };
189 for (int i = 0; i < 4; ++i) {
190 SkString name;
191 name.printf("aaEdge%d", i);
Brian Salomon7d982c62018-02-05 16:20:47 -0500192 args.fVaryingHandler->addVarying(name.c_str(), &aaEdgeVarying[i],
193 Interpolation::kCanBeFlat);
Brian Salomondba65f92018-01-22 08:43:38 -0500194 args.fVertBuilder->codeAppendf(
195 "%s = aaEdge%d;", aaEdgeVarying[i].vsOut(), i);
196 }
197 args.fFragBuilder->codeAppendf(
198 R"(float4 aaDists = float4(dot(%s.xy, sk_FragCoord.xy) + %s.z,
199 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 aaEdgeVarying[0].fsIn(), aaEdgeVarying[0].fsIn(),
203 aaEdgeVarying[1].fsIn(), aaEdgeVarying[1].fsIn(),
204 aaEdgeVarying[2].fsIn(), aaEdgeVarying[2].fsIn(),
205 aaEdgeVarying[3].fsIn(), aaEdgeVarying[3].fsIn());
206 aaDistName = "aaDists";
207 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500208 args.fFragBuilder->codeAppendf(
209 "float mindist = min(min(%s.x, %s.y), min(%s.z, %s.w));",
Brian Salomondba65f92018-01-22 08:43:38 -0500210 aaDistName, aaDistName, aaDistName, aaDistName);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500211 args.fFragBuilder->codeAppendf("%s = float4(clamp(mindist, 0, 1));",
212 args.fOutputCoverage);
213 } else {
214 args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
215 }
Brian Salomon34169692017-08-28 15:32:01 -0400216 }
217 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
218 };
219 return new GLSLProcessor;
220 }
221
Brian Salomon485b8c62018-01-12 15:11:06 -0500222 bool usesCoverageEdgeAA() const { return SkToBool(fAAEdges[0].isInitialized()); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500223
Brian Salomon34169692017-08-28 15:32:01 -0400224private:
Brian Salomon336ce7b2017-09-08 08:23:58 -0400225 // This exists to reduce the number of shaders generated. It does some rounding of sampler
226 // counts.
227 static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) {
228 SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures &&
229 numRealProxies <= caps.maxFragmentSamplers());
230 if (1 == numRealProxies) {
231 return 1;
232 }
233 if (numRealProxies <= 4) {
234 return 4;
235 }
236 // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps.
237 return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers()));
238 }
239
240 TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt,
Brian Salomon485b8c62018-01-12 15:11:06 -0500241 sk_sp<GrColorSpaceXform> csxf, bool coverageAA,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500242 const GrSamplerState::Filter filters[], const GrShaderCaps& caps)
243 : INHERITED(kTextureGeometryProcessor_ClassID), fColorSpaceXform(std::move(csxf)) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400244 SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400245 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400246 fSamplers[0].reset(std::move(proxies[0]), filters[0]);
247 this->addTextureSampler(&fSamplers[0]);
248 for (int i = 1; i < proxyCnt; ++i) {
249 // This class has one sampler built in, the rest come from memory this processor was
250 // placement-newed into and so haven't been constructed.
251 new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]);
252 this->addTextureSampler(&fSamplers[i]);
253 }
254 if (samplerCnt > 1) {
255 // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt
256 // times.
257 GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy();
258 for (int i = proxyCnt; i < samplerCnt; ++i) {
259 new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]);
260 this->addTextureSampler(&fSamplers[i]);
261 }
262 SkASSERT(caps.integerSupport());
263 fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType);
264 }
265
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400266 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
Brian Salomon485b8c62018-01-12 15:11:06 -0500267 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500268 fAAEdges[0] = this->addVertexAttrib("aaEdge0", kFloat3_GrVertexAttribType);
269 fAAEdges[1] = this->addVertexAttrib("aaEdge1", kFloat3_GrVertexAttribType);
270 fAAEdges[2] = this->addVertexAttrib("aaEdge2", kFloat3_GrVertexAttribType);
271 fAAEdges[3] = this->addVertexAttrib("aaEdge3", kFloat3_GrVertexAttribType);
272 }
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400273 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Brian Salomon34169692017-08-28 15:32:01 -0400274 }
275
276 Attribute fPositions;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400277 Attribute fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400278 Attribute fTextureCoords;
279 Attribute fColors;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500280 Attribute fAAEdges[4];
Brian Salomon34169692017-08-28 15:32:01 -0400281 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400282 TextureSampler fSamplers[1];
Ethan Nicholasabff9562017-10-09 10:54:08 -0400283
284 typedef GrGeometryProcessor INHERITED;
Brian Salomon34169692017-08-28 15:32:01 -0400285};
286
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500287namespace {
288// This is a class soley so it can be partially specialized (functions cannot be).
289template<GrAA, typename Vertex> class VertexAAHandler;
290
291template<typename Vertex> class VertexAAHandler<GrAA::kNo, Vertex> {
292public:
293 static void AssignPositionsAndTexCoords(Vertex* vertices, const GrQuad& quad,
294 const SkRect& texRect) {
295 vertices[0].fPosition = quad.point(0);
296 vertices[0].fTextureCoords = {texRect.fLeft, texRect.fTop};
297 vertices[1].fPosition = quad.point(1);
298 vertices[1].fTextureCoords = {texRect.fLeft, texRect.fBottom};
299 vertices[2].fPosition = quad.point(2);
300 vertices[2].fTextureCoords = {texRect.fRight, texRect.fTop};
301 vertices[3].fPosition = quad.point(3);
302 vertices[3].fTextureCoords = {texRect.fRight, texRect.fBottom};
303 }
304};
305
306template<typename Vertex> class VertexAAHandler<GrAA::kYes, Vertex> {
307public:
308 static void AssignPositionsAndTexCoords(Vertex* vertices, const GrQuad& quad,
309 const SkRect& texRect) {
310 // We compute the four edge equations for quad, then outset them and compute a new quad
311 // as the intersection points of the outset edges.
312
313 // GrQuad is in tristip order but we want the points to be in a fan order, so swap 2 and 3.
314 Sk4f xs(quad.point(0).fX, quad.point(1).fX, quad.point(3).fX, quad.point(2).fX);
315 Sk4f ys(quad.point(0).fY, quad.point(1).fY, quad.point(3).fY, quad.point(2).fY);
316 Sk4f xsrot = SkNx_shuffle<1, 2, 3, 0>(xs);
317 Sk4f ysrot = SkNx_shuffle<1, 2, 3, 0>(ys);
318 Sk4f normXs = ysrot - ys;
319 Sk4f normYs = xs - xsrot;
320 Sk4f ds = xsrot * ys - ysrot * xs;
321 Sk4f invNormLengths = (normXs * normXs + normYs * normYs).rsqrt();
322 float test = normXs[0] * xs[2] + normYs[0] * ys[2] + ds[0];
323 // Make sure the edge equations have their normals facing into the quad in device space
324 if (test < 0) {
325 invNormLengths = -invNormLengths;
326 }
327 normXs *= invNormLengths;
328 normYs *= invNormLengths;
329 ds *= invNormLengths;
330
331 // Here is the bloat. This makes our edge equations compute coverage without requiring a
332 // half pixel offset and is also used to compute the bloated quad that will cover all
333 // pixels.
334 ds += Sk4f(0.5f);
335
336 for (int i = 0; i < 4; ++i) {
337 for (int j = 0; j < 4; ++j) {
338 vertices[j].fEdges[i].fX = normXs[i];
339 vertices[j].fEdges[i].fY = normYs[i];
340 vertices[j].fEdges[i].fZ = ds[i];
341 }
342 }
343
344 // Reverse the process to compute the points of the bloated quad from the edge equations.
345 // This time the inputs don't have 1s as their third coord and we want to homogenize rather
346 // than normalize the output since we need a GrQuad with 2D points.
347 xsrot = SkNx_shuffle<3, 0, 1, 2>(normXs);
348 ysrot = SkNx_shuffle<3, 0, 1, 2>(normYs);
349 Sk4f dsrot = SkNx_shuffle<3, 0, 1, 2>(ds);
350 xs = ysrot * ds - normYs * dsrot;
351 ys = normXs * dsrot - xsrot * ds;
352 ds = xsrot * normYs - ysrot * normXs;
353 ds = ds.invert();
354 xs *= ds;
355 ys *= ds;
356
357 // Go back to tri strip order when writing out the bloated quad to vertex positions.
358 vertices[0].fPosition = {xs[0], ys[0]};
359 vertices[1].fPosition = {xs[1], ys[1]};
360 vertices[3].fPosition = {xs[2], ys[2]};
361 vertices[2].fPosition = {xs[3], ys[3]};
362
363 AssignTexCoords(vertices, quad, texRect);
364 }
365
366private:
367 static void AssignTexCoords(Vertex* vertices, const GrQuad& quad, const SkRect& tex) {
368 SkMatrix q = SkMatrix::MakeAll(quad.point(0).fX, quad.point(1).fX, quad.point(2).fX,
369 quad.point(0).fY, quad.point(1).fY, quad.point(2).fY,
370 1.f, 1.f, 1.f);
371 SkMatrix qinv;
372 if (!q.invert(&qinv)) {
373 return;
374 }
375 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
376 tex.fTop, tex.fBottom, tex.fTop,
377 1.f, 1.f, 1.f);
378 SkMatrix map;
379 map.setConcat(t, qinv);
380 SkMatrixPriv::MapPointsWithStride(map, &vertices[0].fTextureCoords, sizeof(Vertex),
381 &vertices[0].fPosition, sizeof(Vertex), 4);
382 }
383};
384
385template <typename Vertex, bool IsMultiTex> struct TexIdAssigner;
386
387template <typename Vertex> struct TexIdAssigner<Vertex, true> {
388 static void Assign(Vertex* vertices, int textureIdx) {
389 vertices[0].fTextureIdx = textureIdx;
390 vertices[1].fTextureIdx = textureIdx;
391 vertices[2].fTextureIdx = textureIdx;
392 vertices[3].fTextureIdx = textureIdx;
393 }
394};
395
396template <typename Vertex> struct TexIdAssigner<Vertex, false> {
397 static void Assign(Vertex* vertices, int textureIdx) {}
398};
399} // anonymous namespace
400
401template <typename Vertex, bool IsMultiTex, GrAA AA>
402static void tessellate_quad(const GrQuad& devQuad, const SkRect& srcRect, GrColor color,
403 GrSurfaceOrigin origin, Vertex* vertices, SkScalar iw, SkScalar ih,
404 int textureIdx) {
405 SkRect texRect = {
406 iw * srcRect.fLeft,
407 ih * srcRect.fTop,
408 iw * srcRect.fRight,
409 ih * srcRect.fBottom
410 };
411 if (origin == kBottomLeft_GrSurfaceOrigin) {
412 texRect.fTop = 1.f - texRect.fTop;
413 texRect.fBottom = 1.f - texRect.fBottom;
414 }
415 VertexAAHandler<AA, Vertex>::AssignPositionsAndTexCoords(vertices, devQuad, texRect);
416 vertices[0].fColor = color;
417 vertices[1].fColor = color;
418 vertices[2].fColor = color;
419 vertices[3].fColor = color;
420 TexIdAssigner<Vertex, IsMultiTex>::Assign(vertices, textureIdx);
421}
Brian Salomon34169692017-08-28 15:32:01 -0400422/**
423 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
424 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
425 */
426class TextureOp final : public GrMeshDrawOp {
427public:
428 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400429 GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500430 const SkRect& srcRect, const SkRect& dstRect,
431 GrAAType aaType, const SkMatrix& viewMatrix,
432 sk_sp<GrColorSpaceXform> csxf, bool allowSRBInputs) {
Brian Salomon34169692017-08-28 15:32:01 -0400433 return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect,
Brian Salomon485b8c62018-01-12 15:11:06 -0500434 dstRect, aaType, viewMatrix, std::move(csxf),
Brian Salomon34169692017-08-28 15:32:01 -0400435 allowSRBInputs));
436 }
437
Brian Salomon336ce7b2017-09-08 08:23:58 -0400438 ~TextureOp() override {
439 if (fFinalized) {
440 auto proxies = this->proxies();
441 for (int i = 0; i < fProxyCnt; ++i) {
442 proxies[i]->completedRead();
443 }
444 if (fProxyCnt > 1) {
445 delete[] reinterpret_cast<const char*>(proxies);
446 }
447 } else {
448 SkASSERT(1 == fProxyCnt);
449 fProxy0->unref();
450 }
451 }
Brian Salomon34169692017-08-28 15:32:01 -0400452
453 const char* name() const override { return "TextureOp"; }
454
Robert Phillipsf1748f52017-09-14 14:11:24 -0400455 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400456 auto proxies = this->proxies();
457 for (int i = 0; i < fProxyCnt; ++i) {
458 func(proxies[i]);
459 }
460 }
461
Brian Salomon34169692017-08-28 15:32:01 -0400462 SkString dumpInfo() const override {
463 SkString str;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400464 str.appendf("AllowSRGBInputs: %d\n", fAllowSRGBInputs);
Brian Salomon34169692017-08-28 15:32:01 -0400465 str.appendf("# draws: %d\n", fDraws.count());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400466 auto proxies = this->proxies();
467 for (int i = 0; i < fProxyCnt; ++i) {
468 str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(),
469 static_cast<int>(this->filters()[i]));
470 }
Brian Salomon34169692017-08-28 15:32:01 -0400471 for (int i = 0; i < fDraws.count(); ++i) {
472 const Draw& draw = fDraws[i];
473 str.appendf(
Brian Salomon336ce7b2017-09-08 08:23:58 -0400474 "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
475 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
476 i, draw.fColor, draw.fTextureIdx, draw.fSrcRect.fLeft, draw.fSrcRect.fTop,
477 draw.fSrcRect.fRight, draw.fSrcRect.fBottom, draw.fQuad.points()[0].fX,
478 draw.fQuad.points()[0].fY, draw.fQuad.points()[1].fX, draw.fQuad.points()[1].fY,
479 draw.fQuad.points()[2].fX, draw.fQuad.points()[2].fY, draw.fQuad.points()[3].fX,
Brian Salomon34169692017-08-28 15:32:01 -0400480 draw.fQuad.points()[3].fY);
481 }
482 str += INHERITED::dumpInfo();
483 return str;
484 }
485
Brian Osman9a725dd2017-09-20 09:53:22 -0400486 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
487 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon34169692017-08-28 15:32:01 -0400488 SkASSERT(!fFinalized);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400489 SkASSERT(1 == fProxyCnt);
Brian Salomon34169692017-08-28 15:32:01 -0400490 fFinalized = true;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400491 fProxy0->addPendingRead();
492 fProxy0->unref();
Brian Salomon34169692017-08-28 15:32:01 -0400493 return RequiresDstTexture::kNo;
494 }
495
Brian Salomon485b8c62018-01-12 15:11:06 -0500496 FixedFunctionFlags fixedFunctionFlags() const override {
497 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
498 : FixedFunctionFlags::kNone;
499 }
Brian Salomon34169692017-08-28 15:32:01 -0400500
501 DEFINE_OP_CLASS_ID
502
503private:
Brian Salomon762d5e72017-12-01 10:25:08 -0500504
505 // This is used in a heursitic for choosing a code path. We don't care what happens with
506 // really large rects, infs, nans, etc.
507#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
508__attribute__((no_sanitize("float-cast-overflow")))
509#endif
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500510 size_t RectSizeAsSizeT(const SkRect& rect) {;
Brian Salomon762d5e72017-12-01 10:25:08 -0500511 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
512 }
513
Brian Salomon336ce7b2017-09-08 08:23:58 -0400514 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
515
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400516 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500517 const SkRect& srcRect, const SkRect& dstRect, GrAAType aaType,
518 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs)
Brian Salomon34169692017-08-28 15:32:01 -0400519 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400520 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400521 , fProxy0(proxy.release())
522 , fFilter0(filter)
523 , fProxyCnt(1)
Brian Salomon485b8c62018-01-12 15:11:06 -0500524 , fAAType(static_cast<unsigned>(aaType))
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500525 , fFinalized(0)
526 , fAllowSRGBInputs(allowSRGBInputs ? 1 : 0) {
Brian Salomon485b8c62018-01-12 15:11:06 -0500527 SkASSERT(aaType != GrAAType::kMixedSamples);
Brian Salomon34169692017-08-28 15:32:01 -0400528 Draw& draw = fDraws.push_back();
529 draw.fSrcRect = srcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400530 draw.fTextureIdx = 0;
Brian Salomon34169692017-08-28 15:32:01 -0400531 draw.fColor = color;
532 draw.fQuad.setFromMappedRect(dstRect, viewMatrix);
533 SkRect bounds;
534 bounds.setBounds(draw.fQuad.points(), 4);
535 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon762d5e72017-12-01 10:25:08 -0500536
537 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400538 }
539
540 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400541 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
542 auto proxies = this->proxies();
543 auto filters = this->filters();
544 for (int i = 0; i < fProxyCnt; ++i) {
545 if (!proxies[i]->instantiate(target->resourceProvider())) {
546 return;
547 }
548 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400549 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400550
Brian Salomon485b8c62018-01-12 15:11:06 -0500551 bool coverageAA = GrAAType::kCoverage == this->aaType();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400552 sk_sp<GrGeometryProcessor> gp =
553 TextureGeometryProcessor::Make(proxiesSPs, fProxyCnt, std::move(fColorSpaceXform),
Brian Salomon485b8c62018-01-12 15:11:06 -0500554 coverageAA, filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400555 GrPipeline::InitArgs args;
556 args.fProxy = target->proxy();
557 args.fCaps = &target->caps();
558 args.fResourceProvider = target->resourceProvider();
Brian Salomon485b8c62018-01-12 15:11:06 -0500559 args.fFlags = 0;
560 if (fAllowSRGBInputs) {
561 args.fFlags |= GrPipeline::kAllowSRGBInputs_Flag;
562 }
563 if (GrAAType::kMSAA == this->aaType()) {
564 args.fFlags |= GrPipeline::kHWAntialias_Flag;
565 }
566
Brian Salomon34169692017-08-28 15:32:01 -0400567 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
568 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400569 int vstart;
570 const GrBuffer* vbuffer;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400571 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
572 &vstart);
573 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400574 SkDebugf("Could not allocate vertices\n");
575 return;
576 }
Brian Salomon57caa662017-10-18 12:21:05 +0000577 if (1 == fProxyCnt) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500578 GrSurfaceOrigin origin = proxies[0]->origin();
579 GrTexture* texture = proxies[0]->priv().peekTexture();
580 float iw = 1.f / texture->width();
581 float ih = 1.f / texture->height();
Brian Salomon485b8c62018-01-12 15:11:06 -0500582 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500583 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::AAVertex));
584 auto vertices = static_cast<TextureGeometryProcessor::AAVertex*>(vdata);
585 for (int i = 0; i < fDraws.count(); ++i) {
586 tessellate_quad<TextureGeometryProcessor::AAVertex, false, GrAA::kYes>(
587 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
588 vertices + 4 * i, iw, ih, 0);
Brian Salomon57caa662017-10-18 12:21:05 +0000589 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500590 } else {
591 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex));
592 auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata);
593 for (int i = 0; i < fDraws.count(); ++i) {
594 tessellate_quad<TextureGeometryProcessor::Vertex, false, GrAA::kNo>(
595 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
596 vertices + 4 * i, iw, ih, 0);
597 }
Brian Salomon57caa662017-10-18 12:21:05 +0000598 }
599 } else {
Brian Salomon57caa662017-10-18 12:21:05 +0000600 GrTexture* textures[kMaxTextures];
601 float iw[kMaxTextures];
602 float ih[kMaxTextures];
603 for (int t = 0; t < fProxyCnt; ++t) {
604 textures[t] = proxies[t]->priv().peekTexture();
605 iw[t] = 1.f / textures[t]->width();
606 ih[t] = 1.f / textures[t]->height();
607 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500608 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500609 SkASSERT(gp->getVertexStride() ==
610 sizeof(TextureGeometryProcessor::AAMultiTextureVertex));
611 auto vertices = static_cast<TextureGeometryProcessor::AAMultiTextureVertex*>(vdata);
612 for (int i = 0; i < fDraws.count(); ++i) {
613 auto tidx = fDraws[i].fTextureIdx;
614 GrSurfaceOrigin origin = proxies[tidx]->origin();
615 tessellate_quad<TextureGeometryProcessor::AAMultiTextureVertex, true,
616 GrAA::kYes>(fDraws[i].fQuad, fDraws[i].fSrcRect,
617 fDraws[i].fColor, origin, vertices + 4 * i,
618 iw[tidx], ih[tidx], tidx);
Brian Salomon57caa662017-10-18 12:21:05 +0000619 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500620 } else {
621 SkASSERT(gp->getVertexStride() ==
622 sizeof(TextureGeometryProcessor::MultiTextureVertex));
623 auto vertices = static_cast<TextureGeometryProcessor::MultiTextureVertex*>(vdata);
624 for (int i = 0; i < fDraws.count(); ++i) {
625 auto tidx = fDraws[i].fTextureIdx;
626 GrSurfaceOrigin origin = proxies[tidx]->origin();
627 tessellate_quad<TextureGeometryProcessor::MultiTextureVertex, true, GrAA::kNo>(
628 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
629 vertices + 4 * i, iw[tidx], ih[tidx], tidx);
630 }
Brian Salomon57caa662017-10-18 12:21:05 +0000631 }
632 }
633 GrPrimitiveType primitiveType =
634 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
635 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400636 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000637 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400638 if (!ibuffer) {
639 SkDebugf("Could not allocate quad indices\n");
640 return;
641 }
Brian Salomon34169692017-08-28 15:32:01 -0400642 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
643 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400644 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400645 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400646 }
Brian Salomon57caa662017-10-18 12:21:05 +0000647 mesh.setVertexData(vbuffer, vstart);
648 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400649 }
650
651 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
652 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500653 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400654 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400655 return false;
656 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500657 if (this->aaType() != that->aaType()) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500658 return false;
659 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400660 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
661 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500662 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
663 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
664 that->fMaxApproxDstPixelArea <=
665 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400666 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500667 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400668 if (numNewProxies < 0) {
669 return false;
670 }
671 if (1 == fProxyCnt && numNewProxies) {
672 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
673 kMaxTextures];
674 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
675 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
676 proxies[0] = fProxy0;
677 filters[0] = fFilter0;
678 fProxyArray = proxies;
679 }
680 fProxyCnt += numNewProxies;
681 auto thisProxies = fProxyArray;
682 auto thatProxies = that->proxies();
683 auto thatFilters = that->filters();
684 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
685 kMaxTextures);
686 for (int i = 0; i < that->fProxyCnt; ++i) {
687 if (map[i] < 0) {
688 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400689
Brian Salomon336ce7b2017-09-08 08:23:58 -0400690 thisProxies[-map[i]] = thatProxies[i];
691 thisFilters[-map[i]] = thatFilters[i];
692 map[i] = -map[i];
693 }
694 }
695 int firstNewDraw = fDraws.count();
696 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
697 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
698 fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx];
699 }
700 } else {
Brian Salomonbbf05752017-11-30 11:30:48 -0500701 // We can get here when one of the ops is already multitextured but the other cannot
702 // be because of the dst rect size.
703 if (fProxyCnt > 1 || that->fProxyCnt > 1) {
704 return false;
705 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400706 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
707 return false;
708 }
709 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
710 }
Brian Salomon34169692017-08-28 15:32:01 -0400711 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500712 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomon34169692017-08-28 15:32:01 -0400713 return true;
714 }
715
Brian Salomon336ce7b2017-09-08 08:23:58 -0400716 /**
717 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
718 * value means that's proxy should be added to this's proxy array at the absolute value of
719 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
720 * negative value is returned. Otherwise, return value indicates the number of proxies that have
721 * to be added to this op or, equivalently, the number of negative entries in map.
722 */
723 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
724 std::fill_n(map, kMaxTextures, -kMaxTextures);
725 int sharedProxyCnt = 0;
726 auto thisProxies = this->proxies();
727 auto thisFilters = this->filters();
728 auto thatProxies = that->proxies();
729 auto thatFilters = that->filters();
730 for (int i = 0; i < fProxyCnt; ++i) {
731 for (int j = 0; j < that->fProxyCnt; ++j) {
732 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
733 if (thisFilters[i] != thatFilters[j]) {
734 // In GL we don't currently support using the same texture with different
735 // samplers. If we added support for sampler objects and a cap bit to know
736 // it's ok to use different filter modes then we could support this.
737 // Otherwise, we could also only allow a single filter mode for each op
738 // instance.
739 return -1;
740 }
741 map[j] = i;
742 ++sharedProxyCnt;
743 break;
744 }
745 }
746 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500747 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400748 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
749 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
750 return -1;
751 }
752 GrPixelConfig config = thisProxies[0]->config();
753 int nextSlot = fProxyCnt;
754 for (int j = 0; j < that->fProxyCnt; ++j) {
755 // We want to avoid making many shaders because of different permutations of shader
756 // based swizzle and sampler types. The approach taken here is to require the configs to
757 // be the same and to only allow already instantiated proxies that have the most
758 // common sampler type. Otherwise we don't merge.
759 if (thatProxies[j]->config() != config) {
760 return -1;
761 }
762 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
763 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
764 return -1;
765 }
766 }
767 if (map[j] < 0) {
768 map[j] = -(nextSlot++);
769 }
770 }
771 return newProxyCnt;
772 }
773
Brian Salomon485b8c62018-01-12 15:11:06 -0500774 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500775
Brian Salomon336ce7b2017-09-08 08:23:58 -0400776 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
777
778 const GrSamplerState::Filter* filters() const {
779 if (fProxyCnt > 1) {
780 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
781 }
782 return &fFilter0;
783 }
784
Brian Salomon34169692017-08-28 15:32:01 -0400785 struct Draw {
786 SkRect fSrcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400787 int fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400788 GrQuad fQuad;
789 GrColor fColor;
790 };
791 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -0400792 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400793 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
794 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
795 // followed by kMaxTextures filters.
796 union {
797 GrTextureProxy* fProxy0;
798 GrTextureProxy** fProxyArray;
799 };
Brian Salomonbbf05752017-11-30 11:30:48 -0500800 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400801 GrSamplerState::Filter fFilter0;
802 uint8_t fProxyCnt;
Brian Salomon485b8c62018-01-12 15:11:06 -0500803 unsigned fAAType : 2;
Brian Salomon34169692017-08-28 15:32:01 -0400804 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500805 unsigned fFinalized : 1;
806 unsigned fAllowSRGBInputs : 1;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400807
Brian Salomon34169692017-08-28 15:32:01 -0400808 typedef GrMeshDrawOp INHERITED;
809};
810
Brian Salomon336ce7b2017-09-08 08:23:58 -0400811constexpr int TextureGeometryProcessor::kMaxTextures;
812constexpr int TextureOp::kMaxTextures;
813
Brian Salomon34169692017-08-28 15:32:01 -0400814} // anonymous namespace
815
816namespace GrTextureOp {
817
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400818std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter,
Brian Salomon485b8c62018-01-12 15:11:06 -0500819 GrColor color, const SkRect& srcRect, const SkRect& dstRect,
820 GrAAType aaType, const SkMatrix& viewMatrix,
821 sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs) {
Brian Salomon34169692017-08-28 15:32:01 -0400822 SkASSERT(!viewMatrix.hasPerspective());
Brian Salomon485b8c62018-01-12 15:11:06 -0500823 return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, aaType, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400824 std::move(csxf), allowSRGBInputs);
825}
826
827} // namespace GrTextureOp
828
829#if GR_TEST_UTILS
830#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500831#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500832#include "GrProxyProvider.h"
Brian Salomon34169692017-08-28 15:32:01 -0400833
834GR_DRAW_OP_TEST_DEFINE(TextureOp) {
835 GrSurfaceDesc desc;
836 desc.fConfig = kRGBA_8888_GrPixelConfig;
837 desc.fHeight = random->nextULessThan(90) + 10;
838 desc.fWidth = random->nextULessThan(90) + 10;
839 desc.fOrigin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
840 SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500841
842 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
843 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(desc, fit, SkBudgeted::kNo);
844
Brian Salomon34169692017-08-28 15:32:01 -0400845 SkRect rect = GrTest::TestRect(random);
846 SkRect srcRect;
847 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
848 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
849 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
850 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
851 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
852 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400853 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
854 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Brian Salomon34169692017-08-28 15:32:01 -0400855 auto csxf = GrTest::TestColorXform(random);
856 bool allowSRGBInputs = random->nextBool();
Brian Salomon485b8c62018-01-12 15:11:06 -0500857 GrAAType aaType = GrAAType::kNone;
858 if (random->nextBool()) {
859 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
860 }
861 return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, aaType, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400862 std::move(csxf), allowSRGBInputs);
863}
864
865#endif