blob: a2cd4eb79fef69c6fa310b40e953e13aa24dce6c [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
Brian Salomon17031a72018-05-22 14:14:07 -040034enum class MultiTexture : bool { kNo = false, kYes = true };
35
Brian Salomon34169692017-08-28 15:32:01 -040036/**
37 * Geometry Processor that draws a texture modulated by a vertex color (though, this is meant to be
38 * the same value across all vertices of a quad and uses flat interpolation when available). This is
39 * used by TextureOp below.
40 */
41class TextureGeometryProcessor : public GrGeometryProcessor {
42public:
Brian Salomon17031a72018-05-22 14:14:07 -040043 template <typename Pos> struct VertexCommon {
44 using Position = Pos;
45 Position fPosition;
Brian Salomon34169692017-08-28 15:32:01 -040046 GrColor fColor;
Brian Salomon17031a72018-05-22 14:14:07 -040047 SkPoint fTextureCoords;
Brian Salomon34169692017-08-28 15:32:01 -040048 };
Brian Salomon17031a72018-05-22 14:14:07 -040049
50 template <typename Pos, MultiTexture MT> struct OptionalMultiTextureVertex;
51 template <typename Pos>
52 struct OptionalMultiTextureVertex<Pos, MultiTexture::kNo> : VertexCommon<Pos> {
53 static constexpr MultiTexture kMultiTexture = MultiTexture::kNo;
Brian Salomonb5ef1f92018-01-11 11:46:21 -050054 };
Brian Salomon17031a72018-05-22 14:14:07 -040055 template <typename Pos>
56 struct OptionalMultiTextureVertex<Pos, MultiTexture::kYes> : VertexCommon<Pos> {
57 static constexpr MultiTexture kMultiTexture = MultiTexture::kYes;
Brian Salomon336ce7b2017-09-08 08:23:58 -040058 int fTextureIdx;
Brian Salomon336ce7b2017-09-08 08:23:58 -040059 };
Brian Salomon17031a72018-05-22 14:14:07 -040060
61 template <typename Pos, MultiTexture MT, GrAA> struct OptionalAAVertex;
62 template <typename Pos, MultiTexture MT>
63 struct OptionalAAVertex<Pos, MT, GrAA::kNo> : OptionalMultiTextureVertex<Pos, MT> {
64 static constexpr GrAA kAA = GrAA::kNo;
65 };
66 template <typename Pos, MultiTexture MT>
67 struct OptionalAAVertex<Pos, MT, GrAA::kYes> : OptionalMultiTextureVertex<Pos, MT> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -040068 static constexpr GrAA kAA = GrAA::kYes;
Brian Salomonb5ef1f92018-01-11 11:46:21 -050069 SkPoint3 fEdges[4];
Brian Salomonb5ef1f92018-01-11 11:46:21 -050070 };
Brian Salomon336ce7b2017-09-08 08:23:58 -040071
Brian Salomon17031a72018-05-22 14:14:07 -040072 template <typename Pos, MultiTexture MT, GrAA AA>
73 using Vertex = OptionalAAVertex<Pos, MT, AA>;
74
Brian Salomon336ce7b2017-09-08 08:23:58 -040075 // Maximum number of textures supported by this op. Must also be checked against the caps
76 // limit. These numbers were based on some limited experiments on a HP Z840 and Pixel XL 2016
77 // and could probably use more tuning.
78#ifdef SK_BUILD_FOR_ANDROID
79 static constexpr int kMaxTextures = 4;
80#else
81 static constexpr int kMaxTextures = 8;
82#endif
83
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040084 static int SupportsMultitexture(const GrShaderCaps& caps) {
Brian Salomon762d5e72017-12-01 10:25:08 -050085 return caps.integerSupport() && caps.maxFragmentSamplers() > 1;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040086 }
Brian Salomon336ce7b2017-09-08 08:23:58 -040087
88 static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxies[], int proxyCnt,
Brian Salomon485b8c62018-01-12 15:11:06 -050089 sk_sp<GrColorSpaceXform> csxf, bool coverageAA,
Brian Salomonbe3c1d22018-05-21 12:54:39 -040090 bool perspective, const GrSamplerState::Filter filters[],
Brian Salomon336ce7b2017-09-08 08:23:58 -040091 const GrShaderCaps& caps) {
92 // We use placement new to avoid always allocating space for kMaxTextures TextureSampler
93 // instances.
94 int samplerCnt = NumSamplersToUse(proxyCnt, caps);
95 size_t size = sizeof(TextureGeometryProcessor) + sizeof(TextureSampler) * (samplerCnt - 1);
96 void* mem = GrGeometryProcessor::operator new(size);
Brian Salomonbe3c1d22018-05-21 12:54:39 -040097 return sk_sp<TextureGeometryProcessor>(
98 new (mem) TextureGeometryProcessor(proxies, proxyCnt, samplerCnt, std::move(csxf),
99 coverageAA, perspective, filters, caps));
Brian Salomon336ce7b2017-09-08 08:23:58 -0400100 }
101
102 ~TextureGeometryProcessor() override {
103 int cnt = this->numTextureSamplers();
104 for (int i = 1; i < cnt; ++i) {
105 fSamplers[i].~TextureSampler();
106 }
Brian Salomon34169692017-08-28 15:32:01 -0400107 }
108
109 const char* name() const override { return "TextureGeometryProcessor"; }
110
111 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
112 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400113 uint32_t x = this->usesCoverageEdgeAA() ? 0 : 1;
114 x |= kFloat3_GrVertexAttribType == fPositions.fType ? 0 : 2;
115 b->add32(x);
Brian Salomon34169692017-08-28 15:32:01 -0400116 }
117
118 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
119 class GLSLProcessor : public GrGLSLGeometryProcessor {
120 public:
121 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
122 FPCoordTransformIter&& transformIter) override {
123 const auto& textureGP = proc.cast<TextureGeometryProcessor>();
124 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
125 if (fColorSpaceXformHelper.isValid()) {
126 fColorSpaceXformHelper.setData(pdman, textureGP.fColorSpaceXform.get());
127 }
128 }
129
130 private:
131 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Chris Dalton7b046312018-02-02 11:06:30 -0700132 using Interpolation = GrGLSLVaryingHandler::Interpolation;
Brian Salomon34169692017-08-28 15:32:01 -0400133 const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>();
134 fColorSpaceXformHelper.emitCode(
135 args.fUniformHandler, textureGP.fColorSpaceXform.get());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400136 if (kFloat2_GrVertexAttribType == textureGP.fPositions.fType) {
137 args.fVaryingHandler->setNoPerspective();
138 }
Brian Salomon34169692017-08-28 15:32:01 -0400139 args.fVaryingHandler->emitAttributes(textureGP);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400140 gpArgs->fPositionVar = textureGP.fPositions.asShaderVar();
141
Brian Salomon34169692017-08-28 15:32:01 -0400142 this->emitTransforms(args.fVertBuilder,
143 args.fVaryingHandler,
144 args.fUniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -0500145 textureGP.fTextureCoords.asShaderVar(),
Brian Salomon34169692017-08-28 15:32:01 -0400146 args.fFPCoordTransformHandler);
Chris Dalton7b046312018-02-02 11:06:30 -0700147 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors,
148 args.fOutputColor,
149 Interpolation::kCanBeFlat);
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400150 args.fFragBuilder->codeAppend("float2 texCoord;");
Chris Daltonfdde34e2017-10-16 14:15:26 -0600151 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords,
152 "texCoord");
Brian Salomon336ce7b2017-09-08 08:23:58 -0400153 if (textureGP.numTextureSamplers() > 1) {
Chris Dalton7b046312018-02-02 11:06:30 -0700154 // If this changes to float, reconsider Interpolation::kMustBeFlat.
155 SkASSERT(kInt_GrVertexAttribType == textureGP.fTextureIdx.fType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400156 SkASSERT(args.fShaderCaps->integerSupport());
157 args.fFragBuilder->codeAppend("int texIdx;");
Chris Dalton7b046312018-02-02 11:06:30 -0700158 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx, "texIdx",
159 Interpolation::kMustBeFlat);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400160 args.fFragBuilder->codeAppend("switch (texIdx) {");
161 for (int i = 0; i < textureGP.numTextureSamplers(); ++i) {
162 args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor);
163 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
164 args.fTexSamplers[i],
165 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400166 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400167 &fColorSpaceXformHelper);
168 args.fFragBuilder->codeAppend("; break;");
169 }
170 args.fFragBuilder->codeAppend("}");
171 } else {
172 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
173 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
174 args.fTexSamplers[0],
175 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400176 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400177 &fColorSpaceXformHelper);
178 }
Brian Salomon34169692017-08-28 15:32:01 -0400179 args.fFragBuilder->codeAppend(";");
Brian Salomon485b8c62018-01-12 15:11:06 -0500180 if (textureGP.usesCoverageEdgeAA()) {
Brian Salomondba65f92018-01-22 08:43:38 -0500181 const char* aaDistName = nullptr;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400182 bool mulByFragCoordW = false;
183 // When interpolation is inaccurate we perform the evaluation of the edge
Brian Salomondba65f92018-01-22 08:43:38 -0500184 // equations in the fragment shader rather than interpolating values computed
185 // in the vertex shader.
186 if (!args.fShaderCaps->interpolantsAreInaccurate()) {
187 GrGLSLVarying aaDistVarying(kFloat4_GrSLType,
188 GrGLSLVarying::Scope::kVertToFrag);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400189 if (kFloat3_GrVertexAttribType == textureGP.fPositions.fType) {
190 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
191 // The distance from edge equation e to homogenous point p=sk_Position
192 // is e.x*p.x/p.wx + e.y*p.y/p.w + e.z. However, we want screen space
193 // interpolation of this distance. We can do this by multiplying the
194 // varying in the VS by p.w and then multiplying by sk_FragCoord.w in
195 // the FS. So we output e.x*p.x + e.y*p.y + e.z * p.w
196 args.fVertBuilder->codeAppendf(
197 R"(%s = float4(dot(aaEdge0, %s), dot(aaEdge1, %s),
198 dot(aaEdge2, %s), dot(aaEdge3, %s));)",
199 aaDistVarying.vsOut(), textureGP.fPositions.fName,
200 textureGP.fPositions.fName, textureGP.fPositions.fName,
201 textureGP.fPositions.fName);
202 mulByFragCoordW = true;
203 } else {
204 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
205 args.fVertBuilder->codeAppendf(
206 R"(%s = float4(dot(aaEdge0.xy, %s.xy) + aaEdge0.z,
207 dot(aaEdge1.xy, %s.xy) + aaEdge1.z,
208 dot(aaEdge2.xy, %s.xy) + aaEdge2.z,
209 dot(aaEdge3.xy, %s.xy) + aaEdge3.z);)",
210 aaDistVarying.vsOut(), textureGP.fPositions.fName,
211 textureGP.fPositions.fName, textureGP.fPositions.fName,
212 textureGP.fPositions.fName);
213 }
Brian Salomondba65f92018-01-22 08:43:38 -0500214 aaDistName = aaDistVarying.fsIn();
215 } else {
216 GrGLSLVarying aaEdgeVarying[4]{
217 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
218 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
219 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
220 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag}
221 };
222 for (int i = 0; i < 4; ++i) {
223 SkString name;
224 name.printf("aaEdge%d", i);
Brian Salomon7d982c62018-02-05 16:20:47 -0500225 args.fVaryingHandler->addVarying(name.c_str(), &aaEdgeVarying[i],
226 Interpolation::kCanBeFlat);
Brian Salomondba65f92018-01-22 08:43:38 -0500227 args.fVertBuilder->codeAppendf(
228 "%s = aaEdge%d;", aaEdgeVarying[i].vsOut(), i);
229 }
230 args.fFragBuilder->codeAppendf(
231 R"(float4 aaDists = float4(dot(%s.xy, sk_FragCoord.xy) + %s.z,
232 dot(%s.xy, sk_FragCoord.xy) + %s.z,
233 dot(%s.xy, sk_FragCoord.xy) + %s.z,
234 dot(%s.xy, sk_FragCoord.xy) + %s.z);)",
235 aaEdgeVarying[0].fsIn(), aaEdgeVarying[0].fsIn(),
236 aaEdgeVarying[1].fsIn(), aaEdgeVarying[1].fsIn(),
237 aaEdgeVarying[2].fsIn(), aaEdgeVarying[2].fsIn(),
238 aaEdgeVarying[3].fsIn(), aaEdgeVarying[3].fsIn());
239 aaDistName = "aaDists";
240 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500241 args.fFragBuilder->codeAppendf(
242 "float mindist = min(min(%s.x, %s.y), min(%s.z, %s.w));",
Brian Salomondba65f92018-01-22 08:43:38 -0500243 aaDistName, aaDistName, aaDistName, aaDistName);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400244 if (mulByFragCoordW) {
245 args.fFragBuilder->codeAppend("mindist *= sk_FragCoord.w;");
246 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500247 args.fFragBuilder->codeAppendf("%s = float4(clamp(mindist, 0, 1));",
248 args.fOutputCoverage);
249 } else {
250 args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
251 }
Brian Salomon34169692017-08-28 15:32:01 -0400252 }
253 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
254 };
255 return new GLSLProcessor;
256 }
257
Brian Salomon485b8c62018-01-12 15:11:06 -0500258 bool usesCoverageEdgeAA() const { return SkToBool(fAAEdges[0].isInitialized()); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500259
Brian Salomon34169692017-08-28 15:32:01 -0400260private:
Brian Salomon336ce7b2017-09-08 08:23:58 -0400261 // This exists to reduce the number of shaders generated. It does some rounding of sampler
262 // counts.
263 static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) {
264 SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures &&
265 numRealProxies <= caps.maxFragmentSamplers());
266 if (1 == numRealProxies) {
267 return 1;
268 }
269 if (numRealProxies <= 4) {
270 return 4;
271 }
272 // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps.
273 return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers()));
274 }
275
276 TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400277 sk_sp<GrColorSpaceXform> csxf, bool coverageAA, bool perspective,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500278 const GrSamplerState::Filter filters[], const GrShaderCaps& caps)
279 : INHERITED(kTextureGeometryProcessor_ClassID), fColorSpaceXform(std::move(csxf)) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400280 SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400281 fSamplers[0].reset(std::move(proxies[0]), filters[0]);
282 this->addTextureSampler(&fSamplers[0]);
283 for (int i = 1; i < proxyCnt; ++i) {
284 // This class has one sampler built in, the rest come from memory this processor was
285 // placement-newed into and so haven't been constructed.
286 new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]);
287 this->addTextureSampler(&fSamplers[i]);
288 }
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400289
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400290 if (perspective) {
291 fPositions = this->addVertexAttrib("position", kFloat3_GrVertexAttribType);
292 } else {
293 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
294 }
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400295 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Brian Salomon17031a72018-05-22 14:14:07 -0400296 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400297
Brian Salomon336ce7b2017-09-08 08:23:58 -0400298 if (samplerCnt > 1) {
299 // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt
300 // times.
301 GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy();
302 for (int i = proxyCnt; i < samplerCnt; ++i) {
303 new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]);
304 this->addTextureSampler(&fSamplers[i]);
305 }
306 SkASSERT(caps.integerSupport());
307 fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType);
308 }
309
Brian Salomon485b8c62018-01-12 15:11:06 -0500310 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500311 fAAEdges[0] = this->addVertexAttrib("aaEdge0", kFloat3_GrVertexAttribType);
312 fAAEdges[1] = this->addVertexAttrib("aaEdge1", kFloat3_GrVertexAttribType);
313 fAAEdges[2] = this->addVertexAttrib("aaEdge2", kFloat3_GrVertexAttribType);
314 fAAEdges[3] = this->addVertexAttrib("aaEdge3", kFloat3_GrVertexAttribType);
315 }
Brian Salomon34169692017-08-28 15:32:01 -0400316 }
317
318 Attribute fPositions;
Brian Salomon34169692017-08-28 15:32:01 -0400319 Attribute fColors;
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400320 Attribute fTextureCoords;
321 Attribute fTextureIdx;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500322 Attribute fAAEdges[4];
Brian Salomon34169692017-08-28 15:32:01 -0400323 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400324 TextureSampler fSamplers[1];
Ethan Nicholasabff9562017-10-09 10:54:08 -0400325
326 typedef GrGeometryProcessor INHERITED;
Brian Salomon34169692017-08-28 15:32:01 -0400327};
328
Brian Salomon6872e942018-05-18 10:29:54 -0400329// This computes the four edge equations for a quad, then outsets them and computes a new quad
330// as the intersection points of the outset edges. 'x' and 'y' contain the original points as input
331// and the outset points as output. 'a', 'b', and 'c' are the edge equation coefficients on output.
332static void compute_quad_edges_and_outset_vertices(Sk4f* x, Sk4f* y, Sk4f* a, Sk4f* b, Sk4f* c) {
333 static constexpr auto fma = SkNx_fma<4, float>;
334 // These rotate the points/edge values either clockwise or counterclockwise assuming tri strip
335 // order.
336 auto nextCW = [](const Sk4f& v) { return SkNx_shuffle<2, 0, 3, 1>(v); };
337 auto nextCCW = [](const Sk4f& v) { return SkNx_shuffle<1, 3, 0, 2>(v); };
338
339 auto xnext = nextCCW(*x);
340 auto ynext = nextCCW(*y);
341 *a = ynext - *y;
342 *b = *x - xnext;
343 *c = fma(xnext, *y, -ynext * *x);
344 Sk4f invNormLengths = (*a * *a + *b * *b).rsqrt();
345 // Make sure the edge equations have their normals facing into the quad in device space.
346 auto test = fma(*a, nextCW(*x), fma(*b, nextCW(*y), *c));
347 if ((test < Sk4f(0)).anyTrue()) {
348 invNormLengths = -invNormLengths;
349 }
350 *a *= invNormLengths;
351 *b *= invNormLengths;
352 *c *= invNormLengths;
353
354 // Here is the outset. This makes our edge equations compute coverage without requiring a
355 // half pixel offset and is also used to compute the bloated quad that will cover all
356 // pixels.
357 *c += Sk4f(0.5f);
358
359 // Reverse the process to compute the points of the bloated quad from the edge equations.
360 // This time the inputs don't have 1s as their third coord and we want to homogenize rather
361 // than normalize.
362 auto anext = nextCW(*a);
363 auto bnext = nextCW(*b);
364 auto cnext = nextCW(*c);
365 *x = fma(bnext, *c, -*b * cnext);
366 *y = fma(*a, cnext, -anext * *c);
367 auto ic = (fma(anext, *b, -bnext * *a)).invert();
368 *x *= ic;
369 *y *= ic;
370}
371
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500372namespace {
373// This is a class soley so it can be partially specialized (functions cannot be).
Brian Salomon86c40012018-05-22 10:48:49 -0400374template <typename V, GrAA AA = V::kAA, typename Position = typename V::Position>
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400375class VertexAAHandler;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500376
Brian Salomon86c40012018-05-22 10:48:49 -0400377template<typename V> class VertexAAHandler<V, GrAA::kNo, SkPoint> {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500378public:
Brian Salomon86c40012018-05-22 10:48:49 -0400379 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500380 const SkRect& texRect) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400381 SkASSERT((quad.w4f() == Sk4f(1.f)).allTrue());
Brian Salomon86c40012018-05-22 10:48:49 -0400382 SkPointPriv::SetRectTriStrip(&vertices[0].fTextureCoords, texRect, sizeof(V));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400383 for (int i = 0; i < 4; ++i) {
384 vertices[i].fPosition = {quad.x(i), quad.y(i)};
385 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500386 }
387};
388
Brian Salomon86c40012018-05-22 10:48:49 -0400389template<typename V> class VertexAAHandler<V, GrAA::kNo, SkPoint3> {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500390public:
Brian Salomon86c40012018-05-22 10:48:49 -0400391 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500392 const SkRect& texRect) {
Brian Salomon86c40012018-05-22 10:48:49 -0400393 SkPointPriv::SetRectTriStrip(&vertices[0].fTextureCoords, texRect, sizeof(V));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400394 for (int i = 0; i < 4; ++i) {
395 vertices[i].fPosition = quad.point(i);
396 }
397 }
398};
399
Brian Salomon86c40012018-05-22 10:48:49 -0400400template<typename V> class VertexAAHandler<V, GrAA::kYes, SkPoint> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400401public:
Brian Salomon86c40012018-05-22 10:48:49 -0400402 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400403 const SkRect& texRect) {
404 SkASSERT((quad.w4f() == Sk4f(1.f)).allTrue());
Brian Salomon6872e942018-05-18 10:29:54 -0400405 auto x = quad.x4f();
406 auto y = quad.y4f();
407 Sk4f a, b, c;
408 compute_quad_edges_and_outset_vertices(&x, &y, &a, &b, &c);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500409
410 for (int i = 0; i < 4; ++i) {
Brian Salomon6872e942018-05-18 10:29:54 -0400411 vertices[i].fPosition = {x[i], y[i]};
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500412 for (int j = 0; j < 4; ++j) {
Brian Salomon6872e942018-05-18 10:29:54 -0400413 vertices[i].fEdges[j] = {a[j], b[j], c[j]};
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500414 }
415 }
416
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500417 AssignTexCoords(vertices, quad, texRect);
418 }
419
420private:
Brian Salomon86c40012018-05-22 10:48:49 -0400421 static void AssignTexCoords(V* vertices, const GrPerspQuad& quad, const SkRect& tex) {
Brian Salomona33b67c2018-05-17 10:42:14 -0400422 SkMatrix q = SkMatrix::MakeAll(quad.x(0), quad.x(1), quad.x(2),
423 quad.y(0), quad.y(1), quad.y(2),
424 1.f, 1.f, 1.f);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500425 SkMatrix qinv;
426 if (!q.invert(&qinv)) {
427 return;
428 }
429 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
430 tex.fTop, tex.fBottom, tex.fTop,
431 1.f, 1.f, 1.f);
432 SkMatrix map;
433 map.setConcat(t, qinv);
Brian Salomon86c40012018-05-22 10:48:49 -0400434 SkMatrixPriv::MapPointsWithStride(map, &vertices[0].fTextureCoords, sizeof(V),
435 &vertices[0].fPosition, sizeof(V), 4);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500436 }
437};
438
Brian Salomon86c40012018-05-22 10:48:49 -0400439template<typename V> class VertexAAHandler<V, GrAA::kYes, SkPoint3> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400440public:
Brian Salomon86c40012018-05-22 10:48:49 -0400441 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400442 const SkRect& texRect) {
443 auto x = quad.x4f();
444 auto y = quad.y4f();
445 auto iw = quad.iw4f();
446 x *= iw;
447 y *= iw;
448
449 // Get an equation for w from device space coords.
450 SkMatrix P;
451 P.setAll(x[0], y[0], 1, x[1], y[1], 1, x[2], y[2], 1);
452 SkAssertResult(P.invert(&P));
453 SkPoint3 weq{quad.w(0), quad.w(1), quad.w(2)};
454 P.mapHomogeneousPoints(&weq, &weq, 1);
455
456 Sk4f a, b, c;
457 compute_quad_edges_and_outset_vertices(&x, &y, &a, &b, &c);
458
459 // Compute new w values for the output vertices;
460 auto w = Sk4f(weq.fX) * x + Sk4f(weq.fY) * y + Sk4f(weq.fZ);
461 x *= w;
462 y *= w;
463
464 for (int i = 0; i < 4; ++i) {
465 vertices[i].fPosition = {x[i], y[i], w[i]};
466 for (int j = 0; j < 4; ++j) {
467 vertices[i].fEdges[j] = {a[j], b[j], c[j]};
468 }
469 }
470
471 AssignTexCoords(vertices, quad, texRect);
472 }
473
474private:
Brian Salomon86c40012018-05-22 10:48:49 -0400475 static void AssignTexCoords(V* vertices, const GrPerspQuad& quad, const SkRect& tex) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400476 SkMatrix q = SkMatrix::MakeAll(quad.x(0), quad.x(1), quad.x(2),
477 quad.y(0), quad.y(1), quad.y(2),
478 quad.w(0), quad.w(1), quad.w(2));
479 SkMatrix qinv;
480 if (!q.invert(&qinv)) {
481 return;
482 }
483 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
484 tex.fTop, tex.fBottom, tex.fTop,
485 1.f, 1.f, 1.f);
486 SkMatrix map;
487 map.setConcat(t, qinv);
488 SkPoint3 tempTexCoords[4];
489 SkMatrixPriv::MapHomogeneousPointsWithStride(map, tempTexCoords, sizeof(SkPoint3),
Brian Salomon86c40012018-05-22 10:48:49 -0400490 &vertices[0].fPosition, sizeof(V), 4);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400491 for (int i = 0; i < 4; ++i) {
492 auto invW = 1.f / tempTexCoords[i].fZ;
493 vertices[i].fTextureCoords.fX = tempTexCoords[i].fX * invW;
494 vertices[i].fTextureCoords.fY = tempTexCoords[i].fY * invW;
495 }
496 }
497};
498
Brian Salomon17031a72018-05-22 14:14:07 -0400499template <typename V, MultiTexture MT = V::kMultiTexture> struct TexIdAssigner;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500500
Brian Salomon17031a72018-05-22 14:14:07 -0400501template <typename V> struct TexIdAssigner<V, MultiTexture::kYes> {
Brian Salomon86c40012018-05-22 10:48:49 -0400502 static void Assign(V* vertices, int textureIdx) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400503 for (int i = 0; i < 4; ++i) {
504 vertices[i].fTextureIdx = textureIdx;
505 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500506 }
507};
508
Brian Salomon17031a72018-05-22 14:14:07 -0400509template <typename V> struct TexIdAssigner<V, MultiTexture::kNo> {
Brian Salomon86c40012018-05-22 10:48:49 -0400510 static void Assign(V* vertices, int textureIdx) {}
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500511};
512} // anonymous namespace
513
Brian Salomon86c40012018-05-22 10:48:49 -0400514template <typename V>
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400515static void tessellate_quad(const GrPerspQuad& devQuad, const SkRect& srcRect, GrColor color,
Brian Salomon86c40012018-05-22 10:48:49 -0400516 GrSurfaceOrigin origin, V* vertices, SkScalar iw, SkScalar ih,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500517 int textureIdx) {
518 SkRect texRect = {
519 iw * srcRect.fLeft,
520 ih * srcRect.fTop,
521 iw * srcRect.fRight,
522 ih * srcRect.fBottom
523 };
524 if (origin == kBottomLeft_GrSurfaceOrigin) {
525 texRect.fTop = 1.f - texRect.fTop;
526 texRect.fBottom = 1.f - texRect.fBottom;
527 }
Brian Salomon86c40012018-05-22 10:48:49 -0400528 VertexAAHandler<V>::AssignPositionsAndTexCoords(vertices, devQuad, texRect);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500529 vertices[0].fColor = color;
530 vertices[1].fColor = color;
531 vertices[2].fColor = color;
532 vertices[3].fColor = color;
Brian Salomon86c40012018-05-22 10:48:49 -0400533 TexIdAssigner<V>::Assign(vertices, textureIdx);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500534}
Brian Salomon17031a72018-05-22 14:14:07 -0400535
Brian Salomon34169692017-08-28 15:32:01 -0400536/**
537 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
538 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
539 */
540class TextureOp final : public GrMeshDrawOp {
541public:
542 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400543 GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500544 const SkRect& srcRect, const SkRect& dstRect,
545 GrAAType aaType, const SkMatrix& viewMatrix,
546 sk_sp<GrColorSpaceXform> csxf, bool allowSRBInputs) {
Brian Salomon34169692017-08-28 15:32:01 -0400547 return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect,
Brian Salomon485b8c62018-01-12 15:11:06 -0500548 dstRect, aaType, viewMatrix, std::move(csxf),
Brian Salomon34169692017-08-28 15:32:01 -0400549 allowSRBInputs));
550 }
551
Brian Salomon336ce7b2017-09-08 08:23:58 -0400552 ~TextureOp() override {
553 if (fFinalized) {
554 auto proxies = this->proxies();
555 for (int i = 0; i < fProxyCnt; ++i) {
556 proxies[i]->completedRead();
557 }
558 if (fProxyCnt > 1) {
559 delete[] reinterpret_cast<const char*>(proxies);
560 }
561 } else {
562 SkASSERT(1 == fProxyCnt);
563 fProxy0->unref();
564 }
565 }
Brian Salomon34169692017-08-28 15:32:01 -0400566
567 const char* name() const override { return "TextureOp"; }
568
Robert Phillipsf1748f52017-09-14 14:11:24 -0400569 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400570 auto proxies = this->proxies();
571 for (int i = 0; i < fProxyCnt; ++i) {
572 func(proxies[i]);
573 }
574 }
575
Brian Salomon34169692017-08-28 15:32:01 -0400576 SkString dumpInfo() const override {
577 SkString str;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400578 str.appendf("AllowSRGBInputs: %d\n", fAllowSRGBInputs);
Brian Salomon34169692017-08-28 15:32:01 -0400579 str.appendf("# draws: %d\n", fDraws.count());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400580 auto proxies = this->proxies();
581 for (int i = 0; i < fProxyCnt; ++i) {
582 str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(),
583 static_cast<int>(this->filters()[i]));
584 }
Brian Salomon34169692017-08-28 15:32:01 -0400585 for (int i = 0; i < fDraws.count(); ++i) {
586 const Draw& draw = fDraws[i];
587 str.appendf(
Brian Salomon336ce7b2017-09-08 08:23:58 -0400588 "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
589 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
590 i, draw.fColor, draw.fTextureIdx, draw.fSrcRect.fLeft, draw.fSrcRect.fTop,
Brian Salomona33b67c2018-05-17 10:42:14 -0400591 draw.fSrcRect.fRight, draw.fSrcRect.fBottom, draw.fQuad.point(0).fX,
592 draw.fQuad.point(0).fY, draw.fQuad.point(1).fX, draw.fQuad.point(1).fY,
593 draw.fQuad.point(2).fX, draw.fQuad.point(2).fY, draw.fQuad.point(3).fX,
594 draw.fQuad.point(3).fY);
Brian Salomon34169692017-08-28 15:32:01 -0400595 }
596 str += INHERITED::dumpInfo();
597 return str;
598 }
599
Brian Osman9a725dd2017-09-20 09:53:22 -0400600 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
601 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon34169692017-08-28 15:32:01 -0400602 SkASSERT(!fFinalized);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400603 SkASSERT(1 == fProxyCnt);
Brian Salomon34169692017-08-28 15:32:01 -0400604 fFinalized = true;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400605 fProxy0->addPendingRead();
606 fProxy0->unref();
Brian Salomon34169692017-08-28 15:32:01 -0400607 return RequiresDstTexture::kNo;
608 }
609
Brian Salomon485b8c62018-01-12 15:11:06 -0500610 FixedFunctionFlags fixedFunctionFlags() const override {
611 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
612 : FixedFunctionFlags::kNone;
613 }
Brian Salomon34169692017-08-28 15:32:01 -0400614
615 DEFINE_OP_CLASS_ID
616
617private:
Brian Salomon762d5e72017-12-01 10:25:08 -0500618
619 // This is used in a heursitic for choosing a code path. We don't care what happens with
620 // really large rects, infs, nans, etc.
621#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
622__attribute__((no_sanitize("float-cast-overflow")))
623#endif
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500624 size_t RectSizeAsSizeT(const SkRect& rect) {;
Brian Salomon762d5e72017-12-01 10:25:08 -0500625 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
626 }
627
Brian Salomon336ce7b2017-09-08 08:23:58 -0400628 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
629
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400630 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500631 const SkRect& srcRect, const SkRect& dstRect, GrAAType aaType,
632 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs)
Brian Salomon34169692017-08-28 15:32:01 -0400633 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400634 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400635 , fProxy0(proxy.release())
636 , fFilter0(filter)
637 , fProxyCnt(1)
Brian Salomon485b8c62018-01-12 15:11:06 -0500638 , fAAType(static_cast<unsigned>(aaType))
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500639 , fFinalized(0)
640 , fAllowSRGBInputs(allowSRGBInputs ? 1 : 0) {
Brian Salomon485b8c62018-01-12 15:11:06 -0500641 SkASSERT(aaType != GrAAType::kMixedSamples);
Brian Salomon34169692017-08-28 15:32:01 -0400642 Draw& draw = fDraws.push_back();
643 draw.fSrcRect = srcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400644 draw.fTextureIdx = 0;
Brian Salomon34169692017-08-28 15:32:01 -0400645 draw.fColor = color;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400646 fPerspective = viewMatrix.hasPerspective();
647 SkRect bounds;
648 draw.fQuad = GrPerspQuad(dstRect, viewMatrix);
649 bounds = draw.fQuad.bounds();
Brian Salomon34169692017-08-28 15:32:01 -0400650 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon762d5e72017-12-01 10:25:08 -0500651
652 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400653 }
654
Brian Salomon17031a72018-05-22 14:14:07 -0400655 template <typename Pos, MultiTexture MT, GrAA AA>
656 void tess(void* v, const float iw[], const float ih[], const GrGeometryProcessor* gp) {
657 using Vertex = TextureGeometryProcessor::Vertex<Pos, MT, AA>;
658 SkASSERT(gp->getVertexStride() == sizeof(Vertex));
659 auto vertices = static_cast<Vertex*>(v);
660 auto proxies = this->proxies();
661 for (const auto& draw : fDraws) {
662 auto origin = proxies[draw.fTextureIdx]->origin();
663 tessellate_quad<Vertex>(draw.fQuad, draw.fSrcRect, draw.fColor, origin, vertices,
664 iw[draw.fTextureIdx], ih[draw.fTextureIdx], draw.fTextureIdx);
665 vertices += 4;
666 }
667 }
668
Brian Salomon34169692017-08-28 15:32:01 -0400669 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400670 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
671 auto proxies = this->proxies();
672 auto filters = this->filters();
673 for (int i = 0; i < fProxyCnt; ++i) {
674 if (!proxies[i]->instantiate(target->resourceProvider())) {
675 return;
676 }
677 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400678 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400679
Brian Salomon485b8c62018-01-12 15:11:06 -0500680 bool coverageAA = GrAAType::kCoverage == this->aaType();
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400681 sk_sp<GrGeometryProcessor> gp = TextureGeometryProcessor::Make(
682 proxiesSPs, fProxyCnt, std::move(fColorSpaceXform), coverageAA, fPerspective,
683 filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400684 GrPipeline::InitArgs args;
685 args.fProxy = target->proxy();
686 args.fCaps = &target->caps();
687 args.fResourceProvider = target->resourceProvider();
Brian Salomon485b8c62018-01-12 15:11:06 -0500688 args.fFlags = 0;
689 if (fAllowSRGBInputs) {
690 args.fFlags |= GrPipeline::kAllowSRGBInputs_Flag;
691 }
692 if (GrAAType::kMSAA == this->aaType()) {
693 args.fFlags |= GrPipeline::kHWAntialias_Flag;
694 }
695
Brian Salomon34169692017-08-28 15:32:01 -0400696 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
697 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400698 int vstart;
699 const GrBuffer* vbuffer;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400700 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
701 &vstart);
702 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400703 SkDebugf("Could not allocate vertices\n");
704 return;
705 }
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400706
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400707 float iw[kMaxTextures];
708 float ih[kMaxTextures];
709 for (int t = 0; t < fProxyCnt; ++t) {
710 const auto* texture = proxies[t]->priv().peekTexture();
711 iw[t] = 1.f / texture->width();
712 ih[t] = 1.f / texture->height();
713 }
714
Brian Salomon17031a72018-05-22 14:14:07 -0400715 if (fPerspective) {
716 if (fProxyCnt > 1) {
717 if (coverageAA) {
718 this->tess<SkPoint3, MultiTexture::kYes, GrAA::kYes>(vdata, iw, ih, gp.get());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400719 } else {
Brian Salomon17031a72018-05-22 14:14:07 -0400720 this->tess<SkPoint3, MultiTexture::kYes, GrAA::kNo>(vdata, iw, ih, gp.get());
Brian Salomon57caa662017-10-18 12:21:05 +0000721 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500722 } else {
Brian Salomon17031a72018-05-22 14:14:07 -0400723 if (coverageAA) {
724 this->tess<SkPoint3, MultiTexture::kNo, GrAA::kYes>(vdata, iw, ih, gp.get());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400725 } else {
Brian Salomon17031a72018-05-22 14:14:07 -0400726 this->tess<SkPoint3, MultiTexture::kNo, GrAA::kNo>(vdata, iw, ih, gp.get());
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500727 }
Brian Salomon57caa662017-10-18 12:21:05 +0000728 }
729 } else {
Brian Salomon17031a72018-05-22 14:14:07 -0400730 if (fProxyCnt > 1) {
731 if (coverageAA) {
732 this->tess<SkPoint, MultiTexture::kYes, GrAA::kYes>(vdata, iw, ih, gp.get());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400733 } else {
Brian Salomon17031a72018-05-22 14:14:07 -0400734 this->tess<SkPoint, MultiTexture::kYes, GrAA::kNo>(vdata, iw, ih, gp.get());
Brian Salomon57caa662017-10-18 12:21:05 +0000735 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500736 } else {
Brian Salomon17031a72018-05-22 14:14:07 -0400737 if (coverageAA) {
738 this->tess<SkPoint, MultiTexture::kNo, GrAA::kYes>(vdata, iw, ih, gp.get());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400739 } else {
Brian Salomon17031a72018-05-22 14:14:07 -0400740 this->tess<SkPoint, MultiTexture::kNo, GrAA::kNo>(vdata, iw, ih, gp.get());
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500741 }
Brian Salomon57caa662017-10-18 12:21:05 +0000742 }
743 }
744 GrPrimitiveType primitiveType =
745 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
746 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400747 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000748 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400749 if (!ibuffer) {
750 SkDebugf("Could not allocate quad indices\n");
751 return;
752 }
Brian Salomon34169692017-08-28 15:32:01 -0400753 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
754 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400755 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400756 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400757 }
Brian Salomon57caa662017-10-18 12:21:05 +0000758 mesh.setVertexData(vbuffer, vstart);
759 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400760 }
761
762 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
763 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500764 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400765 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400766 return false;
767 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500768 if (this->aaType() != that->aaType()) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500769 return false;
770 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400771 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
772 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500773 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
774 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
775 that->fMaxApproxDstPixelArea <=
776 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400777 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500778 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400779 if (numNewProxies < 0) {
780 return false;
781 }
782 if (1 == fProxyCnt && numNewProxies) {
783 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
784 kMaxTextures];
785 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
786 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
787 proxies[0] = fProxy0;
788 filters[0] = fFilter0;
789 fProxyArray = proxies;
790 }
791 fProxyCnt += numNewProxies;
792 auto thisProxies = fProxyArray;
793 auto thatProxies = that->proxies();
794 auto thatFilters = that->filters();
795 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
796 kMaxTextures);
797 for (int i = 0; i < that->fProxyCnt; ++i) {
798 if (map[i] < 0) {
799 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400800
Brian Salomon336ce7b2017-09-08 08:23:58 -0400801 thisProxies[-map[i]] = thatProxies[i];
802 thisFilters[-map[i]] = thatFilters[i];
803 map[i] = -map[i];
804 }
805 }
806 int firstNewDraw = fDraws.count();
807 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
808 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
809 fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx];
810 }
811 } else {
Brian Salomonbbf05752017-11-30 11:30:48 -0500812 // We can get here when one of the ops is already multitextured but the other cannot
813 // be because of the dst rect size.
814 if (fProxyCnt > 1 || that->fProxyCnt > 1) {
815 return false;
816 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400817 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
818 return false;
819 }
820 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
821 }
Brian Salomon34169692017-08-28 15:32:01 -0400822 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500823 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400824 fPerspective |= that->fPerspective;
Brian Salomon34169692017-08-28 15:32:01 -0400825 return true;
826 }
827
Brian Salomon336ce7b2017-09-08 08:23:58 -0400828 /**
829 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
830 * value means that's proxy should be added to this's proxy array at the absolute value of
831 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
832 * negative value is returned. Otherwise, return value indicates the number of proxies that have
833 * to be added to this op or, equivalently, the number of negative entries in map.
834 */
835 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
836 std::fill_n(map, kMaxTextures, -kMaxTextures);
837 int sharedProxyCnt = 0;
838 auto thisProxies = this->proxies();
839 auto thisFilters = this->filters();
840 auto thatProxies = that->proxies();
841 auto thatFilters = that->filters();
842 for (int i = 0; i < fProxyCnt; ++i) {
843 for (int j = 0; j < that->fProxyCnt; ++j) {
844 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
845 if (thisFilters[i] != thatFilters[j]) {
846 // In GL we don't currently support using the same texture with different
847 // samplers. If we added support for sampler objects and a cap bit to know
848 // it's ok to use different filter modes then we could support this.
849 // Otherwise, we could also only allow a single filter mode for each op
850 // instance.
851 return -1;
852 }
853 map[j] = i;
854 ++sharedProxyCnt;
855 break;
856 }
857 }
858 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500859 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400860 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
861 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
862 return -1;
863 }
864 GrPixelConfig config = thisProxies[0]->config();
865 int nextSlot = fProxyCnt;
866 for (int j = 0; j < that->fProxyCnt; ++j) {
867 // We want to avoid making many shaders because of different permutations of shader
868 // based swizzle and sampler types. The approach taken here is to require the configs to
869 // be the same and to only allow already instantiated proxies that have the most
870 // common sampler type. Otherwise we don't merge.
871 if (thatProxies[j]->config() != config) {
872 return -1;
873 }
874 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
875 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
876 return -1;
877 }
878 }
879 if (map[j] < 0) {
880 map[j] = -(nextSlot++);
881 }
882 }
883 return newProxyCnt;
884 }
885
Brian Salomon485b8c62018-01-12 15:11:06 -0500886 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500887
Brian Salomon336ce7b2017-09-08 08:23:58 -0400888 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
889
890 const GrSamplerState::Filter* filters() const {
891 if (fProxyCnt > 1) {
892 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
893 }
894 return &fFilter0;
895 }
896
Brian Salomon34169692017-08-28 15:32:01 -0400897 struct Draw {
898 SkRect fSrcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400899 int fTextureIdx;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400900 GrPerspQuad fQuad;
Brian Salomon34169692017-08-28 15:32:01 -0400901 GrColor fColor;
902 };
903 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -0400904 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400905 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
906 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
907 // followed by kMaxTextures filters.
908 union {
909 GrTextureProxy* fProxy0;
910 GrTextureProxy** fProxyArray;
911 };
Brian Salomonbbf05752017-11-30 11:30:48 -0500912 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400913 GrSamplerState::Filter fFilter0;
914 uint8_t fProxyCnt;
Brian Salomon485b8c62018-01-12 15:11:06 -0500915 unsigned fAAType : 2;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400916 unsigned fPerspective : 1;
Brian Salomon34169692017-08-28 15:32:01 -0400917 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500918 unsigned fFinalized : 1;
919 unsigned fAllowSRGBInputs : 1;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400920
Brian Salomon34169692017-08-28 15:32:01 -0400921 typedef GrMeshDrawOp INHERITED;
922};
923
Brian Salomon336ce7b2017-09-08 08:23:58 -0400924constexpr int TextureGeometryProcessor::kMaxTextures;
925constexpr int TextureOp::kMaxTextures;
926
Brian Salomon34169692017-08-28 15:32:01 -0400927} // anonymous namespace
928
929namespace GrTextureOp {
930
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400931std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter,
Brian Salomon485b8c62018-01-12 15:11:06 -0500932 GrColor color, const SkRect& srcRect, const SkRect& dstRect,
933 GrAAType aaType, const SkMatrix& viewMatrix,
934 sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs) {
Brian Salomon485b8c62018-01-12 15:11:06 -0500935 return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, aaType, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400936 std::move(csxf), allowSRGBInputs);
937}
938
939} // namespace GrTextureOp
940
941#if GR_TEST_UTILS
942#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500943#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500944#include "GrProxyProvider.h"
Brian Salomon34169692017-08-28 15:32:01 -0400945
946GR_DRAW_OP_TEST_DEFINE(TextureOp) {
947 GrSurfaceDesc desc;
948 desc.fConfig = kRGBA_8888_GrPixelConfig;
949 desc.fHeight = random->nextULessThan(90) + 10;
950 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500951 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon34169692017-08-28 15:32:01 -0400952 SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500953
954 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomon2a4f9832018-03-03 22:43:43 -0500955 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(desc, origin, fit, SkBudgeted::kNo);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500956
Brian Salomon34169692017-08-28 15:32:01 -0400957 SkRect rect = GrTest::TestRect(random);
958 SkRect srcRect;
959 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
960 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
961 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
962 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
963 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
964 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400965 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
966 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Brian Salomon34169692017-08-28 15:32:01 -0400967 auto csxf = GrTest::TestColorXform(random);
968 bool allowSRGBInputs = random->nextBool();
Brian Salomon485b8c62018-01-12 15:11:06 -0500969 GrAAType aaType = GrAAType::kNone;
970 if (random->nextBool()) {
971 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
972 }
973 return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, aaType, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400974 std::move(csxf), allowSRGBInputs);
975}
976
977#endif