blob: 7c4842f73a7266d52d7e57263c980a27214c4aa9 [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 {
119 const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>();
120 fColorSpaceXformHelper.emitCode(
121 args.fUniformHandler, textureGP.fColorSpaceXform.get());
122 args.fVaryingHandler->setNoPerspective();
123 args.fVaryingHandler->emitAttributes(textureGP);
124 this->writeOutputPosition(args.fVertBuilder, gpArgs, textureGP.fPositions.fName);
125 this->emitTransforms(args.fVertBuilder,
126 args.fVaryingHandler,
127 args.fUniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -0500128 textureGP.fTextureCoords.asShaderVar(),
Brian Salomon34169692017-08-28 15:32:01 -0400129 args.fFPCoordTransformHandler);
Brian Salomon41274562017-09-15 09:40:03 -0700130 if (args.fShaderCaps->preferFlatInterpolation()) {
Brian Salomon34169692017-08-28 15:32:01 -0400131 args.fVaryingHandler->addFlatPassThroughAttribute(&textureGP.fColors,
132 args.fOutputColor);
133 } else {
134 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors,
135 args.fOutputColor);
136 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400137 args.fFragBuilder->codeAppend("float2 texCoord;");
Chris Daltonfdde34e2017-10-16 14:15:26 -0600138 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords,
139 "texCoord");
Brian Salomon336ce7b2017-09-08 08:23:58 -0400140 if (textureGP.numTextureSamplers() > 1) {
141 SkASSERT(args.fShaderCaps->integerSupport());
142 args.fFragBuilder->codeAppend("int texIdx;");
143 if (args.fShaderCaps->flatInterpolationSupport()) {
144 args.fVaryingHandler->addFlatPassThroughAttribute(&textureGP.fTextureIdx,
145 "texIdx");
146 } else {
147 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx,
148 "texIdx");
149 }
150 args.fFragBuilder->codeAppend("switch (texIdx) {");
151 for (int i = 0; i < textureGP.numTextureSamplers(); ++i) {
152 args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor);
153 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
154 args.fTexSamplers[i],
155 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400156 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400157 &fColorSpaceXformHelper);
158 args.fFragBuilder->codeAppend("; break;");
159 }
160 args.fFragBuilder->codeAppend("}");
161 } else {
162 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
163 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
164 args.fTexSamplers[0],
165 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400166 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400167 &fColorSpaceXformHelper);
168 }
Brian Salomon34169692017-08-28 15:32:01 -0400169 args.fFragBuilder->codeAppend(";");
Brian Salomon485b8c62018-01-12 15:11:06 -0500170 if (textureGP.usesCoverageEdgeAA()) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500171 GrGLSLVarying aaDistVarying(kFloat4_GrSLType,
172 GrGLSLVarying::Scope::kVertToFrag);
173 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
174 args.fVertBuilder->codeAppendf(
175 R"(%s = float4(dot(aaEdge0.xy, %s.xy) + aaEdge0.z,
176 dot(aaEdge1.xy, %s.xy) + aaEdge1.z,
177 dot(aaEdge2.xy, %s.xy) + aaEdge2.z,
178 dot(aaEdge3.xy, %s.xy) + aaEdge3.z);)",
179 aaDistVarying.vsOut(), textureGP.fPositions.fName,
180 textureGP.fPositions.fName, textureGP.fPositions.fName,
181 textureGP.fPositions.fName);
182
183 args.fFragBuilder->codeAppendf(
184 "float mindist = min(min(%s.x, %s.y), min(%s.z, %s.w));",
185 aaDistVarying.fsIn(), aaDistVarying.fsIn(), aaDistVarying.fsIn(),
186 aaDistVarying.fsIn());
187 args.fFragBuilder->codeAppendf("%s = float4(clamp(mindist, 0, 1));",
188 args.fOutputCoverage);
189 } else {
190 args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
191 }
Brian Salomon34169692017-08-28 15:32:01 -0400192 }
193 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
194 };
195 return new GLSLProcessor;
196 }
197
Brian Salomon485b8c62018-01-12 15:11:06 -0500198 bool usesCoverageEdgeAA() const { return SkToBool(fAAEdges[0].isInitialized()); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500199
Brian Salomon34169692017-08-28 15:32:01 -0400200private:
Brian Salomon336ce7b2017-09-08 08:23:58 -0400201 // This exists to reduce the number of shaders generated. It does some rounding of sampler
202 // counts.
203 static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) {
204 SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures &&
205 numRealProxies <= caps.maxFragmentSamplers());
206 if (1 == numRealProxies) {
207 return 1;
208 }
209 if (numRealProxies <= 4) {
210 return 4;
211 }
212 // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps.
213 return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers()));
214 }
215
216 TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt,
Brian Salomon485b8c62018-01-12 15:11:06 -0500217 sk_sp<GrColorSpaceXform> csxf, bool coverageAA,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500218 const GrSamplerState::Filter filters[], const GrShaderCaps& caps)
219 : INHERITED(kTextureGeometryProcessor_ClassID), fColorSpaceXform(std::move(csxf)) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400220 SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400221 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400222 fSamplers[0].reset(std::move(proxies[0]), filters[0]);
223 this->addTextureSampler(&fSamplers[0]);
224 for (int i = 1; i < proxyCnt; ++i) {
225 // This class has one sampler built in, the rest come from memory this processor was
226 // placement-newed into and so haven't been constructed.
227 new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]);
228 this->addTextureSampler(&fSamplers[i]);
229 }
230 if (samplerCnt > 1) {
231 // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt
232 // times.
233 GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy();
234 for (int i = proxyCnt; i < samplerCnt; ++i) {
235 new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]);
236 this->addTextureSampler(&fSamplers[i]);
237 }
238 SkASSERT(caps.integerSupport());
239 fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType);
240 }
241
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400242 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
Brian Salomon485b8c62018-01-12 15:11:06 -0500243 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500244 fAAEdges[0] = this->addVertexAttrib("aaEdge0", kFloat3_GrVertexAttribType);
245 fAAEdges[1] = this->addVertexAttrib("aaEdge1", kFloat3_GrVertexAttribType);
246 fAAEdges[2] = this->addVertexAttrib("aaEdge2", kFloat3_GrVertexAttribType);
247 fAAEdges[3] = this->addVertexAttrib("aaEdge3", kFloat3_GrVertexAttribType);
248 }
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400249 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Brian Salomon34169692017-08-28 15:32:01 -0400250 }
251
252 Attribute fPositions;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400253 Attribute fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400254 Attribute fTextureCoords;
255 Attribute fColors;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500256 Attribute fAAEdges[4];
Brian Salomon34169692017-08-28 15:32:01 -0400257 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400258 TextureSampler fSamplers[1];
Ethan Nicholasabff9562017-10-09 10:54:08 -0400259
260 typedef GrGeometryProcessor INHERITED;
Brian Salomon34169692017-08-28 15:32:01 -0400261};
262
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500263namespace {
264// This is a class soley so it can be partially specialized (functions cannot be).
265template<GrAA, typename Vertex> class VertexAAHandler;
266
267template<typename Vertex> class VertexAAHandler<GrAA::kNo, Vertex> {
268public:
269 static void AssignPositionsAndTexCoords(Vertex* vertices, const GrQuad& quad,
270 const SkRect& texRect) {
271 vertices[0].fPosition = quad.point(0);
272 vertices[0].fTextureCoords = {texRect.fLeft, texRect.fTop};
273 vertices[1].fPosition = quad.point(1);
274 vertices[1].fTextureCoords = {texRect.fLeft, texRect.fBottom};
275 vertices[2].fPosition = quad.point(2);
276 vertices[2].fTextureCoords = {texRect.fRight, texRect.fTop};
277 vertices[3].fPosition = quad.point(3);
278 vertices[3].fTextureCoords = {texRect.fRight, texRect.fBottom};
279 }
280};
281
282template<typename Vertex> class VertexAAHandler<GrAA::kYes, Vertex> {
283public:
284 static void AssignPositionsAndTexCoords(Vertex* vertices, const GrQuad& quad,
285 const SkRect& texRect) {
286 // We compute the four edge equations for quad, then outset them and compute a new quad
287 // as the intersection points of the outset edges.
288
289 // GrQuad is in tristip order but we want the points to be in a fan order, so swap 2 and 3.
290 Sk4f xs(quad.point(0).fX, quad.point(1).fX, quad.point(3).fX, quad.point(2).fX);
291 Sk4f ys(quad.point(0).fY, quad.point(1).fY, quad.point(3).fY, quad.point(2).fY);
292 Sk4f xsrot = SkNx_shuffle<1, 2, 3, 0>(xs);
293 Sk4f ysrot = SkNx_shuffle<1, 2, 3, 0>(ys);
294 Sk4f normXs = ysrot - ys;
295 Sk4f normYs = xs - xsrot;
296 Sk4f ds = xsrot * ys - ysrot * xs;
297 Sk4f invNormLengths = (normXs * normXs + normYs * normYs).rsqrt();
298 float test = normXs[0] * xs[2] + normYs[0] * ys[2] + ds[0];
299 // Make sure the edge equations have their normals facing into the quad in device space
300 if (test < 0) {
301 invNormLengths = -invNormLengths;
302 }
303 normXs *= invNormLengths;
304 normYs *= invNormLengths;
305 ds *= invNormLengths;
306
307 // Here is the bloat. This makes our edge equations compute coverage without requiring a
308 // half pixel offset and is also used to compute the bloated quad that will cover all
309 // pixels.
310 ds += Sk4f(0.5f);
311
312 for (int i = 0; i < 4; ++i) {
313 for (int j = 0; j < 4; ++j) {
314 vertices[j].fEdges[i].fX = normXs[i];
315 vertices[j].fEdges[i].fY = normYs[i];
316 vertices[j].fEdges[i].fZ = ds[i];
317 }
318 }
319
320 // Reverse the process to compute the points of the bloated quad from the edge equations.
321 // This time the inputs don't have 1s as their third coord and we want to homogenize rather
322 // than normalize the output since we need a GrQuad with 2D points.
323 xsrot = SkNx_shuffle<3, 0, 1, 2>(normXs);
324 ysrot = SkNx_shuffle<3, 0, 1, 2>(normYs);
325 Sk4f dsrot = SkNx_shuffle<3, 0, 1, 2>(ds);
326 xs = ysrot * ds - normYs * dsrot;
327 ys = normXs * dsrot - xsrot * ds;
328 ds = xsrot * normYs - ysrot * normXs;
329 ds = ds.invert();
330 xs *= ds;
331 ys *= ds;
332
333 // Go back to tri strip order when writing out the bloated quad to vertex positions.
334 vertices[0].fPosition = {xs[0], ys[0]};
335 vertices[1].fPosition = {xs[1], ys[1]};
336 vertices[3].fPosition = {xs[2], ys[2]};
337 vertices[2].fPosition = {xs[3], ys[3]};
338
339 AssignTexCoords(vertices, quad, texRect);
340 }
341
342private:
343 static void AssignTexCoords(Vertex* vertices, const GrQuad& quad, const SkRect& tex) {
344 SkMatrix q = SkMatrix::MakeAll(quad.point(0).fX, quad.point(1).fX, quad.point(2).fX,
345 quad.point(0).fY, quad.point(1).fY, quad.point(2).fY,
346 1.f, 1.f, 1.f);
347 SkMatrix qinv;
348 if (!q.invert(&qinv)) {
349 return;
350 }
351 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
352 tex.fTop, tex.fBottom, tex.fTop,
353 1.f, 1.f, 1.f);
354 SkMatrix map;
355 map.setConcat(t, qinv);
356 SkMatrixPriv::MapPointsWithStride(map, &vertices[0].fTextureCoords, sizeof(Vertex),
357 &vertices[0].fPosition, sizeof(Vertex), 4);
358 }
359};
360
361template <typename Vertex, bool IsMultiTex> struct TexIdAssigner;
362
363template <typename Vertex> struct TexIdAssigner<Vertex, true> {
364 static void Assign(Vertex* vertices, int textureIdx) {
365 vertices[0].fTextureIdx = textureIdx;
366 vertices[1].fTextureIdx = textureIdx;
367 vertices[2].fTextureIdx = textureIdx;
368 vertices[3].fTextureIdx = textureIdx;
369 }
370};
371
372template <typename Vertex> struct TexIdAssigner<Vertex, false> {
373 static void Assign(Vertex* vertices, int textureIdx) {}
374};
375} // anonymous namespace
376
377template <typename Vertex, bool IsMultiTex, GrAA AA>
378static void tessellate_quad(const GrQuad& devQuad, const SkRect& srcRect, GrColor color,
379 GrSurfaceOrigin origin, Vertex* vertices, SkScalar iw, SkScalar ih,
380 int textureIdx) {
381 SkRect texRect = {
382 iw * srcRect.fLeft,
383 ih * srcRect.fTop,
384 iw * srcRect.fRight,
385 ih * srcRect.fBottom
386 };
387 if (origin == kBottomLeft_GrSurfaceOrigin) {
388 texRect.fTop = 1.f - texRect.fTop;
389 texRect.fBottom = 1.f - texRect.fBottom;
390 }
391 VertexAAHandler<AA, Vertex>::AssignPositionsAndTexCoords(vertices, devQuad, texRect);
392 vertices[0].fColor = color;
393 vertices[1].fColor = color;
394 vertices[2].fColor = color;
395 vertices[3].fColor = color;
396 TexIdAssigner<Vertex, IsMultiTex>::Assign(vertices, textureIdx);
397}
Brian Salomon34169692017-08-28 15:32:01 -0400398/**
399 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
400 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
401 */
402class TextureOp final : public GrMeshDrawOp {
403public:
404 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400405 GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500406 const SkRect& srcRect, const SkRect& dstRect,
407 GrAAType aaType, const SkMatrix& viewMatrix,
408 sk_sp<GrColorSpaceXform> csxf, bool allowSRBInputs) {
Brian Salomon34169692017-08-28 15:32:01 -0400409 return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect,
Brian Salomon485b8c62018-01-12 15:11:06 -0500410 dstRect, aaType, viewMatrix, std::move(csxf),
Brian Salomon34169692017-08-28 15:32:01 -0400411 allowSRBInputs));
412 }
413
Brian Salomon336ce7b2017-09-08 08:23:58 -0400414 ~TextureOp() override {
415 if (fFinalized) {
416 auto proxies = this->proxies();
417 for (int i = 0; i < fProxyCnt; ++i) {
418 proxies[i]->completedRead();
419 }
420 if (fProxyCnt > 1) {
421 delete[] reinterpret_cast<const char*>(proxies);
422 }
423 } else {
424 SkASSERT(1 == fProxyCnt);
425 fProxy0->unref();
426 }
427 }
Brian Salomon34169692017-08-28 15:32:01 -0400428
429 const char* name() const override { return "TextureOp"; }
430
Robert Phillipsf1748f52017-09-14 14:11:24 -0400431 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400432 auto proxies = this->proxies();
433 for (int i = 0; i < fProxyCnt; ++i) {
434 func(proxies[i]);
435 }
436 }
437
Brian Salomon34169692017-08-28 15:32:01 -0400438 SkString dumpInfo() const override {
439 SkString str;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400440 str.appendf("AllowSRGBInputs: %d\n", fAllowSRGBInputs);
Brian Salomon34169692017-08-28 15:32:01 -0400441 str.appendf("# draws: %d\n", fDraws.count());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400442 auto proxies = this->proxies();
443 for (int i = 0; i < fProxyCnt; ++i) {
444 str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(),
445 static_cast<int>(this->filters()[i]));
446 }
Brian Salomon34169692017-08-28 15:32:01 -0400447 for (int i = 0; i < fDraws.count(); ++i) {
448 const Draw& draw = fDraws[i];
449 str.appendf(
Brian Salomon336ce7b2017-09-08 08:23:58 -0400450 "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
451 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
452 i, draw.fColor, draw.fTextureIdx, draw.fSrcRect.fLeft, draw.fSrcRect.fTop,
453 draw.fSrcRect.fRight, draw.fSrcRect.fBottom, draw.fQuad.points()[0].fX,
454 draw.fQuad.points()[0].fY, draw.fQuad.points()[1].fX, draw.fQuad.points()[1].fY,
455 draw.fQuad.points()[2].fX, draw.fQuad.points()[2].fY, draw.fQuad.points()[3].fX,
Brian Salomon34169692017-08-28 15:32:01 -0400456 draw.fQuad.points()[3].fY);
457 }
458 str += INHERITED::dumpInfo();
459 return str;
460 }
461
Brian Osman9a725dd2017-09-20 09:53:22 -0400462 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
463 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon34169692017-08-28 15:32:01 -0400464 SkASSERT(!fFinalized);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400465 SkASSERT(1 == fProxyCnt);
Brian Salomon34169692017-08-28 15:32:01 -0400466 fFinalized = true;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400467 fProxy0->addPendingRead();
468 fProxy0->unref();
Brian Salomon34169692017-08-28 15:32:01 -0400469 return RequiresDstTexture::kNo;
470 }
471
Brian Salomon485b8c62018-01-12 15:11:06 -0500472 FixedFunctionFlags fixedFunctionFlags() const override {
473 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
474 : FixedFunctionFlags::kNone;
475 }
Brian Salomon34169692017-08-28 15:32:01 -0400476
477 DEFINE_OP_CLASS_ID
478
479private:
Brian Salomon762d5e72017-12-01 10:25:08 -0500480
481 // This is used in a heursitic for choosing a code path. We don't care what happens with
482 // really large rects, infs, nans, etc.
483#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
484__attribute__((no_sanitize("float-cast-overflow")))
485#endif
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500486 size_t RectSizeAsSizeT(const SkRect& rect) {;
Brian Salomon762d5e72017-12-01 10:25:08 -0500487 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
488 }
489
Brian Salomon336ce7b2017-09-08 08:23:58 -0400490 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
491
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400492 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500493 const SkRect& srcRect, const SkRect& dstRect, GrAAType aaType,
494 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs)
Brian Salomon34169692017-08-28 15:32:01 -0400495 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400496 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400497 , fProxy0(proxy.release())
498 , fFilter0(filter)
499 , fProxyCnt(1)
Brian Salomon485b8c62018-01-12 15:11:06 -0500500 , fAAType(static_cast<unsigned>(aaType))
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500501 , fFinalized(0)
502 , fAllowSRGBInputs(allowSRGBInputs ? 1 : 0) {
Brian Salomon485b8c62018-01-12 15:11:06 -0500503 SkASSERT(aaType != GrAAType::kMixedSamples);
Brian Salomon34169692017-08-28 15:32:01 -0400504 Draw& draw = fDraws.push_back();
505 draw.fSrcRect = srcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400506 draw.fTextureIdx = 0;
Brian Salomon34169692017-08-28 15:32:01 -0400507 draw.fColor = color;
508 draw.fQuad.setFromMappedRect(dstRect, viewMatrix);
509 SkRect bounds;
510 bounds.setBounds(draw.fQuad.points(), 4);
511 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon762d5e72017-12-01 10:25:08 -0500512
513 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400514 }
515
516 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400517 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
518 auto proxies = this->proxies();
519 auto filters = this->filters();
520 for (int i = 0; i < fProxyCnt; ++i) {
521 if (!proxies[i]->instantiate(target->resourceProvider())) {
522 return;
523 }
524 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400525 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400526
Brian Salomon485b8c62018-01-12 15:11:06 -0500527 bool coverageAA = GrAAType::kCoverage == this->aaType();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400528 sk_sp<GrGeometryProcessor> gp =
529 TextureGeometryProcessor::Make(proxiesSPs, fProxyCnt, std::move(fColorSpaceXform),
Brian Salomon485b8c62018-01-12 15:11:06 -0500530 coverageAA, filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400531 GrPipeline::InitArgs args;
532 args.fProxy = target->proxy();
533 args.fCaps = &target->caps();
534 args.fResourceProvider = target->resourceProvider();
Brian Salomon485b8c62018-01-12 15:11:06 -0500535 args.fFlags = 0;
536 if (fAllowSRGBInputs) {
537 args.fFlags |= GrPipeline::kAllowSRGBInputs_Flag;
538 }
539 if (GrAAType::kMSAA == this->aaType()) {
540 args.fFlags |= GrPipeline::kHWAntialias_Flag;
541 }
542
Brian Salomon34169692017-08-28 15:32:01 -0400543 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
544 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400545 int vstart;
546 const GrBuffer* vbuffer;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400547 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
548 &vstart);
549 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400550 SkDebugf("Could not allocate vertices\n");
551 return;
552 }
Brian Salomon57caa662017-10-18 12:21:05 +0000553 if (1 == fProxyCnt) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500554 GrSurfaceOrigin origin = proxies[0]->origin();
555 GrTexture* texture = proxies[0]->priv().peekTexture();
556 float iw = 1.f / texture->width();
557 float ih = 1.f / texture->height();
Brian Salomon485b8c62018-01-12 15:11:06 -0500558 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500559 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::AAVertex));
560 auto vertices = static_cast<TextureGeometryProcessor::AAVertex*>(vdata);
561 for (int i = 0; i < fDraws.count(); ++i) {
562 tessellate_quad<TextureGeometryProcessor::AAVertex, false, GrAA::kYes>(
563 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
564 vertices + 4 * i, iw, ih, 0);
Brian Salomon57caa662017-10-18 12:21:05 +0000565 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500566 } else {
567 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex));
568 auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata);
569 for (int i = 0; i < fDraws.count(); ++i) {
570 tessellate_quad<TextureGeometryProcessor::Vertex, false, GrAA::kNo>(
571 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
572 vertices + 4 * i, iw, ih, 0);
573 }
Brian Salomon57caa662017-10-18 12:21:05 +0000574 }
575 } else {
Brian Salomon57caa662017-10-18 12:21:05 +0000576 GrTexture* textures[kMaxTextures];
577 float iw[kMaxTextures];
578 float ih[kMaxTextures];
579 for (int t = 0; t < fProxyCnt; ++t) {
580 textures[t] = proxies[t]->priv().peekTexture();
581 iw[t] = 1.f / textures[t]->width();
582 ih[t] = 1.f / textures[t]->height();
583 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500584 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500585 SkASSERT(gp->getVertexStride() ==
586 sizeof(TextureGeometryProcessor::AAMultiTextureVertex));
587 auto vertices = static_cast<TextureGeometryProcessor::AAMultiTextureVertex*>(vdata);
588 for (int i = 0; i < fDraws.count(); ++i) {
589 auto tidx = fDraws[i].fTextureIdx;
590 GrSurfaceOrigin origin = proxies[tidx]->origin();
591 tessellate_quad<TextureGeometryProcessor::AAMultiTextureVertex, true,
592 GrAA::kYes>(fDraws[i].fQuad, fDraws[i].fSrcRect,
593 fDraws[i].fColor, origin, vertices + 4 * i,
594 iw[tidx], ih[tidx], tidx);
Brian Salomon57caa662017-10-18 12:21:05 +0000595 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500596 } else {
597 SkASSERT(gp->getVertexStride() ==
598 sizeof(TextureGeometryProcessor::MultiTextureVertex));
599 auto vertices = static_cast<TextureGeometryProcessor::MultiTextureVertex*>(vdata);
600 for (int i = 0; i < fDraws.count(); ++i) {
601 auto tidx = fDraws[i].fTextureIdx;
602 GrSurfaceOrigin origin = proxies[tidx]->origin();
603 tessellate_quad<TextureGeometryProcessor::MultiTextureVertex, true, GrAA::kNo>(
604 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
605 vertices + 4 * i, iw[tidx], ih[tidx], tidx);
606 }
Brian Salomon57caa662017-10-18 12:21:05 +0000607 }
608 }
609 GrPrimitiveType primitiveType =
610 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
611 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400612 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000613 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400614 if (!ibuffer) {
615 SkDebugf("Could not allocate quad indices\n");
616 return;
617 }
Brian Salomon34169692017-08-28 15:32:01 -0400618 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
619 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400620 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400621 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400622 }
Brian Salomon57caa662017-10-18 12:21:05 +0000623 mesh.setVertexData(vbuffer, vstart);
624 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400625 }
626
627 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
628 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500629 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400630 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400631 return false;
632 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500633 if (this->aaType() != that->aaType()) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500634 return false;
635 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400636 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
637 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500638 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
639 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
640 that->fMaxApproxDstPixelArea <=
641 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400642 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500643 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400644 if (numNewProxies < 0) {
645 return false;
646 }
647 if (1 == fProxyCnt && numNewProxies) {
648 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
649 kMaxTextures];
650 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
651 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
652 proxies[0] = fProxy0;
653 filters[0] = fFilter0;
654 fProxyArray = proxies;
655 }
656 fProxyCnt += numNewProxies;
657 auto thisProxies = fProxyArray;
658 auto thatProxies = that->proxies();
659 auto thatFilters = that->filters();
660 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
661 kMaxTextures);
662 for (int i = 0; i < that->fProxyCnt; ++i) {
663 if (map[i] < 0) {
664 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400665
Brian Salomon336ce7b2017-09-08 08:23:58 -0400666 thisProxies[-map[i]] = thatProxies[i];
667 thisFilters[-map[i]] = thatFilters[i];
668 map[i] = -map[i];
669 }
670 }
671 int firstNewDraw = fDraws.count();
672 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
673 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
674 fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx];
675 }
676 } else {
Brian Salomonbbf05752017-11-30 11:30:48 -0500677 // We can get here when one of the ops is already multitextured but the other cannot
678 // be because of the dst rect size.
679 if (fProxyCnt > 1 || that->fProxyCnt > 1) {
680 return false;
681 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400682 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
683 return false;
684 }
685 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
686 }
Brian Salomon34169692017-08-28 15:32:01 -0400687 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500688 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomon34169692017-08-28 15:32:01 -0400689 return true;
690 }
691
Brian Salomon336ce7b2017-09-08 08:23:58 -0400692 /**
693 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
694 * value means that's proxy should be added to this's proxy array at the absolute value of
695 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
696 * negative value is returned. Otherwise, return value indicates the number of proxies that have
697 * to be added to this op or, equivalently, the number of negative entries in map.
698 */
699 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
700 std::fill_n(map, kMaxTextures, -kMaxTextures);
701 int sharedProxyCnt = 0;
702 auto thisProxies = this->proxies();
703 auto thisFilters = this->filters();
704 auto thatProxies = that->proxies();
705 auto thatFilters = that->filters();
706 for (int i = 0; i < fProxyCnt; ++i) {
707 for (int j = 0; j < that->fProxyCnt; ++j) {
708 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
709 if (thisFilters[i] != thatFilters[j]) {
710 // In GL we don't currently support using the same texture with different
711 // samplers. If we added support for sampler objects and a cap bit to know
712 // it's ok to use different filter modes then we could support this.
713 // Otherwise, we could also only allow a single filter mode for each op
714 // instance.
715 return -1;
716 }
717 map[j] = i;
718 ++sharedProxyCnt;
719 break;
720 }
721 }
722 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500723 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400724 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
725 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
726 return -1;
727 }
728 GrPixelConfig config = thisProxies[0]->config();
729 int nextSlot = fProxyCnt;
730 for (int j = 0; j < that->fProxyCnt; ++j) {
731 // We want to avoid making many shaders because of different permutations of shader
732 // based swizzle and sampler types. The approach taken here is to require the configs to
733 // be the same and to only allow already instantiated proxies that have the most
734 // common sampler type. Otherwise we don't merge.
735 if (thatProxies[j]->config() != config) {
736 return -1;
737 }
738 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
739 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
740 return -1;
741 }
742 }
743 if (map[j] < 0) {
744 map[j] = -(nextSlot++);
745 }
746 }
747 return newProxyCnt;
748 }
749
Brian Salomon485b8c62018-01-12 15:11:06 -0500750 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500751
Brian Salomon336ce7b2017-09-08 08:23:58 -0400752 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
753
754 const GrSamplerState::Filter* filters() const {
755 if (fProxyCnt > 1) {
756 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
757 }
758 return &fFilter0;
759 }
760
Brian Salomon34169692017-08-28 15:32:01 -0400761 struct Draw {
762 SkRect fSrcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400763 int fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400764 GrQuad fQuad;
765 GrColor fColor;
766 };
767 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -0400768 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400769 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
770 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
771 // followed by kMaxTextures filters.
772 union {
773 GrTextureProxy* fProxy0;
774 GrTextureProxy** fProxyArray;
775 };
Brian Salomonbbf05752017-11-30 11:30:48 -0500776 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400777 GrSamplerState::Filter fFilter0;
778 uint8_t fProxyCnt;
Brian Salomon485b8c62018-01-12 15:11:06 -0500779 unsigned fAAType : 2;
Brian Salomon34169692017-08-28 15:32:01 -0400780 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500781 unsigned fFinalized : 1;
782 unsigned fAllowSRGBInputs : 1;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400783
Brian Salomon34169692017-08-28 15:32:01 -0400784 typedef GrMeshDrawOp INHERITED;
785};
786
Brian Salomon336ce7b2017-09-08 08:23:58 -0400787constexpr int TextureGeometryProcessor::kMaxTextures;
788constexpr int TextureOp::kMaxTextures;
789
Brian Salomon34169692017-08-28 15:32:01 -0400790} // anonymous namespace
791
792namespace GrTextureOp {
793
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400794std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter,
Brian Salomon485b8c62018-01-12 15:11:06 -0500795 GrColor color, const SkRect& srcRect, const SkRect& dstRect,
796 GrAAType aaType, const SkMatrix& viewMatrix,
797 sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs) {
Brian Salomon34169692017-08-28 15:32:01 -0400798 SkASSERT(!viewMatrix.hasPerspective());
Brian Salomon485b8c62018-01-12 15:11:06 -0500799 return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, aaType, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400800 std::move(csxf), allowSRGBInputs);
801}
802
803} // namespace GrTextureOp
804
805#if GR_TEST_UTILS
806#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500807#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500808#include "GrProxyProvider.h"
Brian Salomon34169692017-08-28 15:32:01 -0400809
810GR_DRAW_OP_TEST_DEFINE(TextureOp) {
811 GrSurfaceDesc desc;
812 desc.fConfig = kRGBA_8888_GrPixelConfig;
813 desc.fHeight = random->nextULessThan(90) + 10;
814 desc.fWidth = random->nextULessThan(90) + 10;
815 desc.fOrigin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
816 SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500817
818 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
819 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(desc, fit, SkBudgeted::kNo);
820
Brian Salomon34169692017-08-28 15:32:01 -0400821 SkRect rect = GrTest::TestRect(random);
822 SkRect srcRect;
823 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
824 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
825 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
826 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
827 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
828 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400829 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
830 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Brian Salomon34169692017-08-28 15:32:01 -0400831 auto csxf = GrTest::TestColorXform(random);
832 bool allowSRGBInputs = random->nextBool();
Brian Salomon485b8c62018-01-12 15:11:06 -0500833 GrAAType aaType = GrAAType::kNone;
834 if (random->nextBool()) {
835 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
836 }
837 return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, aaType, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400838 std::move(csxf), allowSRGBInputs);
839}
840
841#endif