blob: 6d735f51af8740867a9c9f799ae6728348b051ae [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 Salomonb5ef1f92018-01-11 11:46:21 -050079 sk_sp<GrColorSpaceXform> csxf, GrAA aa,
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 Salomonb5ef1f92018-01-11 11:46:21 -050088 proxies, proxyCnt, samplerCnt, std::move(csxf), aa, 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 Salomonb5ef1f92018-01-11 11:46:21 -0500102 b->add32(static_cast<uint32_t>(this->aa()));
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 Salomonb5ef1f92018-01-11 11:46:21 -0500170 if (GrAA::kYes == textureGP.aa()) {
171 GrGLSLVarying aaDistVarying(kFloat4_GrSLType,
172 GrGLSLVarying::Scope::kVertToFrag);
173 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
174 args.fVertBuilder->codeAppendf(
175 R"(%s = float4(dot(aaEdge0.xy, %s.xy) + aaEdge0.z,
176 dot(aaEdge1.xy, %s.xy) + aaEdge1.z,
177 dot(aaEdge2.xy, %s.xy) + aaEdge2.z,
178 dot(aaEdge3.xy, %s.xy) + aaEdge3.z);)",
179 aaDistVarying.vsOut(), textureGP.fPositions.fName,
180 textureGP.fPositions.fName, textureGP.fPositions.fName,
181 textureGP.fPositions.fName);
182
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 Salomonb5ef1f92018-01-11 11:46:21 -0500198 GrAA aa() const { return GrAA(fAAEdges[0].isInitialized()); }
199
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 Salomonb5ef1f92018-01-11 11:46:21 -0500217 sk_sp<GrColorSpaceXform> csxf, GrAA aa,
218 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 Salomonb5ef1f92018-01-11 11:46:21 -0500243 if (GrAA::kYes == aa) {
244 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 Salomonb5ef1f92018-01-11 11:46:21 -0500406 const SkRect& srcRect, const SkRect& dstRect, GrAA aa,
Brian Salomon34169692017-08-28 15:32:01 -0400407 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf,
408 bool allowSRBInputs) {
409 return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500410 dstRect, aa, 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
472 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
473
474 DEFINE_OP_CLASS_ID
475
476private:
Brian Salomon762d5e72017-12-01 10:25:08 -0500477
478 // This is used in a heursitic for choosing a code path. We don't care what happens with
479 // really large rects, infs, nans, etc.
480#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
481__attribute__((no_sanitize("float-cast-overflow")))
482#endif
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500483 size_t RectSizeAsSizeT(const SkRect& rect) {;
Brian Salomon762d5e72017-12-01 10:25:08 -0500484 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
485 }
486
Brian Salomon336ce7b2017-09-08 08:23:58 -0400487 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
488
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400489 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500490 const SkRect& srcRect, const SkRect& dstRect, GrAA aa, const SkMatrix& viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400491 sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs)
492 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400493 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400494 , fProxy0(proxy.release())
495 , fFilter0(filter)
496 , fProxyCnt(1)
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500497 , fAA(aa == GrAA::kYes ? 1 : 0)
498 , fFinalized(0)
499 , fAllowSRGBInputs(allowSRGBInputs ? 1 : 0) {
Brian Salomon34169692017-08-28 15:32:01 -0400500 Draw& draw = fDraws.push_back();
501 draw.fSrcRect = srcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400502 draw.fTextureIdx = 0;
Brian Salomon34169692017-08-28 15:32:01 -0400503 draw.fColor = color;
504 draw.fQuad.setFromMappedRect(dstRect, viewMatrix);
505 SkRect bounds;
506 bounds.setBounds(draw.fQuad.points(), 4);
507 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon762d5e72017-12-01 10:25:08 -0500508
509 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400510 }
511
512 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400513 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
514 auto proxies = this->proxies();
515 auto filters = this->filters();
516 for (int i = 0; i < fProxyCnt; ++i) {
517 if (!proxies[i]->instantiate(target->resourceProvider())) {
518 return;
519 }
520 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400521 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400522
523 sk_sp<GrGeometryProcessor> gp =
524 TextureGeometryProcessor::Make(proxiesSPs, fProxyCnt, std::move(fColorSpaceXform),
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500525 this->aa(), filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400526 GrPipeline::InitArgs args;
527 args.fProxy = target->proxy();
528 args.fCaps = &target->caps();
529 args.fResourceProvider = target->resourceProvider();
530 args.fFlags = fAllowSRGBInputs ? GrPipeline::kAllowSRGBInputs_Flag : 0;
531 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
532 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400533 int vstart;
534 const GrBuffer* vbuffer;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400535 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
536 &vstart);
537 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400538 SkDebugf("Could not allocate vertices\n");
539 return;
540 }
Brian Salomon57caa662017-10-18 12:21:05 +0000541 if (1 == fProxyCnt) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500542 GrSurfaceOrigin origin = proxies[0]->origin();
543 GrTexture* texture = proxies[0]->priv().peekTexture();
544 float iw = 1.f / texture->width();
545 float ih = 1.f / texture->height();
546 if (GrAA::kYes == this->aa()) {
547 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::AAVertex));
548 auto vertices = static_cast<TextureGeometryProcessor::AAVertex*>(vdata);
549 for (int i = 0; i < fDraws.count(); ++i) {
550 tessellate_quad<TextureGeometryProcessor::AAVertex, false, GrAA::kYes>(
551 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
552 vertices + 4 * i, iw, ih, 0);
Brian Salomon57caa662017-10-18 12:21:05 +0000553 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500554 } else {
555 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex));
556 auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata);
557 for (int i = 0; i < fDraws.count(); ++i) {
558 tessellate_quad<TextureGeometryProcessor::Vertex, false, GrAA::kNo>(
559 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
560 vertices + 4 * i, iw, ih, 0);
561 }
Brian Salomon57caa662017-10-18 12:21:05 +0000562 }
563 } else {
Brian Salomon57caa662017-10-18 12:21:05 +0000564 GrTexture* textures[kMaxTextures];
565 float iw[kMaxTextures];
566 float ih[kMaxTextures];
567 for (int t = 0; t < fProxyCnt; ++t) {
568 textures[t] = proxies[t]->priv().peekTexture();
569 iw[t] = 1.f / textures[t]->width();
570 ih[t] = 1.f / textures[t]->height();
571 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500572 if (GrAA::kYes == this->aa()) {
573 SkASSERT(gp->getVertexStride() ==
574 sizeof(TextureGeometryProcessor::AAMultiTextureVertex));
575 auto vertices = static_cast<TextureGeometryProcessor::AAMultiTextureVertex*>(vdata);
576 for (int i = 0; i < fDraws.count(); ++i) {
577 auto tidx = fDraws[i].fTextureIdx;
578 GrSurfaceOrigin origin = proxies[tidx]->origin();
579 tessellate_quad<TextureGeometryProcessor::AAMultiTextureVertex, true,
580 GrAA::kYes>(fDraws[i].fQuad, fDraws[i].fSrcRect,
581 fDraws[i].fColor, origin, vertices + 4 * i,
582 iw[tidx], ih[tidx], tidx);
Brian Salomon57caa662017-10-18 12:21:05 +0000583 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500584 } else {
585 SkASSERT(gp->getVertexStride() ==
586 sizeof(TextureGeometryProcessor::MultiTextureVertex));
587 auto vertices = static_cast<TextureGeometryProcessor::MultiTextureVertex*>(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::MultiTextureVertex, true, GrAA::kNo>(
592 fDraws[i].fQuad, fDraws[i].fSrcRect, fDraws[i].fColor, origin,
593 vertices + 4 * i, iw[tidx], ih[tidx], tidx);
594 }
Brian Salomon57caa662017-10-18 12:21:05 +0000595 }
596 }
597 GrPrimitiveType primitiveType =
598 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
599 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400600 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000601 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400602 if (!ibuffer) {
603 SkDebugf("Could not allocate quad indices\n");
604 return;
605 }
Brian Salomon34169692017-08-28 15:32:01 -0400606 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
607 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400608 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400609 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400610 }
Brian Salomon57caa662017-10-18 12:21:05 +0000611 mesh.setVertexData(vbuffer, vstart);
612 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400613 }
614
615 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
616 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500617 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400618 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400619 return false;
620 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500621 if (this->fAA != that->fAA) {
622 return false;
623 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400624 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
625 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500626 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
627 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
628 that->fMaxApproxDstPixelArea <=
629 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400630 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500631 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400632 if (numNewProxies < 0) {
633 return false;
634 }
635 if (1 == fProxyCnt && numNewProxies) {
636 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
637 kMaxTextures];
638 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
639 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
640 proxies[0] = fProxy0;
641 filters[0] = fFilter0;
642 fProxyArray = proxies;
643 }
644 fProxyCnt += numNewProxies;
645 auto thisProxies = fProxyArray;
646 auto thatProxies = that->proxies();
647 auto thatFilters = that->filters();
648 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
649 kMaxTextures);
650 for (int i = 0; i < that->fProxyCnt; ++i) {
651 if (map[i] < 0) {
652 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400653
Brian Salomon336ce7b2017-09-08 08:23:58 -0400654 thisProxies[-map[i]] = thatProxies[i];
655 thisFilters[-map[i]] = thatFilters[i];
656 map[i] = -map[i];
657 }
658 }
659 int firstNewDraw = fDraws.count();
660 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
661 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
662 fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx];
663 }
664 } else {
Brian Salomonbbf05752017-11-30 11:30:48 -0500665 // We can get here when one of the ops is already multitextured but the other cannot
666 // be because of the dst rect size.
667 if (fProxyCnt > 1 || that->fProxyCnt > 1) {
668 return false;
669 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400670 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
671 return false;
672 }
673 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
674 }
Brian Salomon34169692017-08-28 15:32:01 -0400675 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500676 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomon34169692017-08-28 15:32:01 -0400677 return true;
678 }
679
Brian Salomon336ce7b2017-09-08 08:23:58 -0400680 /**
681 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
682 * value means that's proxy should be added to this's proxy array at the absolute value of
683 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
684 * negative value is returned. Otherwise, return value indicates the number of proxies that have
685 * to be added to this op or, equivalently, the number of negative entries in map.
686 */
687 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
688 std::fill_n(map, kMaxTextures, -kMaxTextures);
689 int sharedProxyCnt = 0;
690 auto thisProxies = this->proxies();
691 auto thisFilters = this->filters();
692 auto thatProxies = that->proxies();
693 auto thatFilters = that->filters();
694 for (int i = 0; i < fProxyCnt; ++i) {
695 for (int j = 0; j < that->fProxyCnt; ++j) {
696 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
697 if (thisFilters[i] != thatFilters[j]) {
698 // In GL we don't currently support using the same texture with different
699 // samplers. If we added support for sampler objects and a cap bit to know
700 // it's ok to use different filter modes then we could support this.
701 // Otherwise, we could also only allow a single filter mode for each op
702 // instance.
703 return -1;
704 }
705 map[j] = i;
706 ++sharedProxyCnt;
707 break;
708 }
709 }
710 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500711 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400712 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
713 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
714 return -1;
715 }
716 GrPixelConfig config = thisProxies[0]->config();
717 int nextSlot = fProxyCnt;
718 for (int j = 0; j < that->fProxyCnt; ++j) {
719 // We want to avoid making many shaders because of different permutations of shader
720 // based swizzle and sampler types. The approach taken here is to require the configs to
721 // be the same and to only allow already instantiated proxies that have the most
722 // common sampler type. Otherwise we don't merge.
723 if (thatProxies[j]->config() != config) {
724 return -1;
725 }
726 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
727 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
728 return -1;
729 }
730 }
731 if (map[j] < 0) {
732 map[j] = -(nextSlot++);
733 }
734 }
735 return newProxyCnt;
736 }
737
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500738 GrAA aa() const { return static_cast<GrAA>(fAA); }
739
Brian Salomon336ce7b2017-09-08 08:23:58 -0400740 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
741
742 const GrSamplerState::Filter* filters() const {
743 if (fProxyCnt > 1) {
744 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
745 }
746 return &fFilter0;
747 }
748
Brian Salomon34169692017-08-28 15:32:01 -0400749 struct Draw {
750 SkRect fSrcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400751 int fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400752 GrQuad fQuad;
753 GrColor fColor;
754 };
755 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -0400756 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400757 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
758 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
759 // followed by kMaxTextures filters.
760 union {
761 GrTextureProxy* fProxy0;
762 GrTextureProxy** fProxyArray;
763 };
Brian Salomonbbf05752017-11-30 11:30:48 -0500764 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400765 GrSamplerState::Filter fFilter0;
766 uint8_t fProxyCnt;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500767 unsigned fAA : 1;
Brian Salomon34169692017-08-28 15:32:01 -0400768 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500769 unsigned fFinalized : 1;
770 unsigned fAllowSRGBInputs : 1;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400771
Brian Salomon34169692017-08-28 15:32:01 -0400772 typedef GrMeshDrawOp INHERITED;
773};
774
Brian Salomon336ce7b2017-09-08 08:23:58 -0400775constexpr int TextureGeometryProcessor::kMaxTextures;
776constexpr int TextureOp::kMaxTextures;
777
Brian Salomon34169692017-08-28 15:32:01 -0400778} // anonymous namespace
779
780namespace GrTextureOp {
781
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400782std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500783 GrColor color, const SkRect& srcRect, const SkRect& dstRect, GrAA aa,
Brian Salomon34169692017-08-28 15:32:01 -0400784 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf,
785 bool allowSRGBInputs) {
786 SkASSERT(!viewMatrix.hasPerspective());
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500787 return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, aa, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400788 std::move(csxf), allowSRGBInputs);
789}
790
791} // namespace GrTextureOp
792
793#if GR_TEST_UTILS
794#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500795#include "GrContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -0400796
797GR_DRAW_OP_TEST_DEFINE(TextureOp) {
798 GrSurfaceDesc desc;
799 desc.fConfig = kRGBA_8888_GrPixelConfig;
800 desc.fHeight = random->nextULessThan(90) + 10;
801 desc.fWidth = random->nextULessThan(90) + 10;
802 desc.fOrigin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
803 SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500804 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(
805 context->contextPriv().proxyProvider(),
806 desc, fit, SkBudgeted::kNo);
Brian Salomon34169692017-08-28 15:32:01 -0400807 SkRect rect = GrTest::TestRect(random);
808 SkRect srcRect;
809 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
810 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
811 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
812 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
813 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
814 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400815 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
816 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Brian Salomon34169692017-08-28 15:32:01 -0400817 auto csxf = GrTest::TestColorXform(random);
818 bool allowSRGBInputs = random->nextBool();
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500819 GrAA aa = GrAA(random->nextBool());
820 return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, aa, viewMatrix,
Brian Salomon34169692017-08-28 15:32:01 -0400821 std::move(csxf), allowSRGBInputs);
822}
823
824#endif