blob: 2301930e938e8d50980838386ea241d9bfa365c3 [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"
Robert Phillips7c525e62018-06-12 10:11:12 -040011#include "GrContext.h"
12#include "GrContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -040013#include "GrDrawOpTest.h"
14#include "GrGeometryProcessor.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040015#include "GrMemoryPool.h"
Brian Salomon34169692017-08-28 15:32:01 -040016#include "GrMeshDrawOp.h"
17#include "GrOpFlushState.h"
18#include "GrQuad.h"
19#include "GrResourceProvider.h"
20#include "GrShaderCaps.h"
21#include "GrTexture.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040022#include "GrTexturePriv.h"
Brian Salomon34169692017-08-28 15:32:01 -040023#include "GrTextureProxy.h"
24#include "SkGr.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040025#include "SkMathPriv.h"
Brian Salomona33b67c2018-05-17 10:42:14 -040026#include "SkMatrixPriv.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050027#include "SkPoint.h"
28#include "SkPoint3.h"
Brian Salomon34169692017-08-28 15:32:01 -040029#include "glsl/GrGLSLColorSpaceXformHelper.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050030#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon34169692017-08-28 15:32:01 -040031#include "glsl/GrGLSLGeometryProcessor.h"
32#include "glsl/GrGLSLVarying.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050033#include "glsl/GrGLSLVertexGeoBuilder.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040034#include <new>
Brian Salomon34169692017-08-28 15:32:01 -040035
36namespace {
37
Brian Salomon17031a72018-05-22 14:14:07 -040038enum class MultiTexture : bool { kNo = false, kYes = true };
39
Brian Salomonb80ffee2018-05-23 16:39:39 -040040enum class Domain : bool { kNo = false, kYes = true };
41
Brian Salomon34169692017-08-28 15:32:01 -040042/**
43 * Geometry Processor that draws a texture modulated by a vertex color (though, this is meant to be
44 * the same value across all vertices of a quad and uses flat interpolation when available). This is
45 * used by TextureOp below.
46 */
47class TextureGeometryProcessor : public GrGeometryProcessor {
48public:
Brian Salomon17031a72018-05-22 14:14:07 -040049 template <typename Pos> struct VertexCommon {
50 using Position = Pos;
51 Position fPosition;
Brian Salomon34169692017-08-28 15:32:01 -040052 GrColor fColor;
Brian Salomon17031a72018-05-22 14:14:07 -040053 SkPoint fTextureCoords;
Brian Salomon34169692017-08-28 15:32:01 -040054 };
Brian Salomon17031a72018-05-22 14:14:07 -040055
56 template <typename Pos, MultiTexture MT> struct OptionalMultiTextureVertex;
57 template <typename Pos>
58 struct OptionalMultiTextureVertex<Pos, MultiTexture::kNo> : VertexCommon<Pos> {
59 static constexpr MultiTexture kMultiTexture = MultiTexture::kNo;
Brian Salomonb5ef1f92018-01-11 11:46:21 -050060 };
Brian Salomon17031a72018-05-22 14:14:07 -040061 template <typename Pos>
62 struct OptionalMultiTextureVertex<Pos, MultiTexture::kYes> : VertexCommon<Pos> {
63 static constexpr MultiTexture kMultiTexture = MultiTexture::kYes;
Brian Salomon336ce7b2017-09-08 08:23:58 -040064 int fTextureIdx;
Brian Salomon336ce7b2017-09-08 08:23:58 -040065 };
Brian Salomon17031a72018-05-22 14:14:07 -040066
Brian Salomonb80ffee2018-05-23 16:39:39 -040067 template <typename Pos, MultiTexture MT, Domain D> struct OptionalDomainVertex;
Brian Salomon17031a72018-05-22 14:14:07 -040068 template <typename Pos, MultiTexture MT>
Brian Salomonb80ffee2018-05-23 16:39:39 -040069 struct OptionalDomainVertex<Pos, MT, Domain::kNo> : OptionalMultiTextureVertex<Pos, MT> {
70 static constexpr Domain kDomain = Domain::kNo;
Brian Salomona0047bc2018-05-23 16:39:39 -040071 };
Stephen White633f20e2018-05-26 18:07:27 +000072 template <typename Pos, MultiTexture MT>
Brian Salomonb80ffee2018-05-23 16:39:39 -040073 struct OptionalDomainVertex<Pos, MT, Domain::kYes> : OptionalMultiTextureVertex<Pos, MT> {
74 static constexpr Domain kDomain = Domain::kYes;
75 SkRect fTextureDomain;
76 };
77
78 template <typename Pos, MultiTexture MT, Domain D, GrAA> struct OptionalAAVertex;
79 template <typename Pos, MultiTexture MT, Domain D>
80 struct OptionalAAVertex<Pos, MT, D, GrAA::kNo> : OptionalDomainVertex<Pos, MT, D> {
81 static constexpr GrAA kAA = GrAA::kNo;
82 };
83 template <typename Pos, MultiTexture MT, Domain D>
84 struct OptionalAAVertex<Pos, MT, D, GrAA::kYes> : OptionalDomainVertex<Pos, MT, D> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -040085 static constexpr GrAA kAA = GrAA::kYes;
Brian Salomonb5ef1f92018-01-11 11:46:21 -050086 SkPoint3 fEdges[4];
Brian Salomonb5ef1f92018-01-11 11:46:21 -050087 };
Brian Salomon336ce7b2017-09-08 08:23:58 -040088
Brian Salomonb80ffee2018-05-23 16:39:39 -040089 template <typename Pos, MultiTexture MT, Domain D, GrAA AA>
90 using Vertex = OptionalAAVertex<Pos, MT, D, AA>;
Brian Salomon17031a72018-05-22 14:14:07 -040091
Brian Salomon336ce7b2017-09-08 08:23:58 -040092 // Maximum number of textures supported by this op. Must also be checked against the caps
93 // limit. These numbers were based on some limited experiments on a HP Z840 and Pixel XL 2016
94 // and could probably use more tuning.
95#ifdef SK_BUILD_FOR_ANDROID
96 static constexpr int kMaxTextures = 4;
97#else
98 static constexpr int kMaxTextures = 8;
99#endif
100
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400101 static int SupportsMultitexture(const GrShaderCaps& caps) {
Brian Salomon762d5e72017-12-01 10:25:08 -0500102 return caps.integerSupport() && caps.maxFragmentSamplers() > 1;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400103 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400104
105 static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxies[], int proxyCnt,
Brian Salomon485b8c62018-01-12 15:11:06 -0500106 sk_sp<GrColorSpaceXform> csxf, bool coverageAA,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400107 bool perspective, Domain domain,
108 const GrSamplerState::Filter filters[],
Brian Salomon336ce7b2017-09-08 08:23:58 -0400109 const GrShaderCaps& caps) {
110 // We use placement new to avoid always allocating space for kMaxTextures TextureSampler
111 // instances.
112 int samplerCnt = NumSamplersToUse(proxyCnt, caps);
113 size_t size = sizeof(TextureGeometryProcessor) + sizeof(TextureSampler) * (samplerCnt - 1);
114 void* mem = GrGeometryProcessor::operator new(size);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400115 return sk_sp<TextureGeometryProcessor>(
116 new (mem) TextureGeometryProcessor(proxies, proxyCnt, samplerCnt, std::move(csxf),
Brian Salomonb80ffee2018-05-23 16:39:39 -0400117 coverageAA, perspective, domain, filters, caps));
Brian Salomon336ce7b2017-09-08 08:23:58 -0400118 }
119
120 ~TextureGeometryProcessor() override {
121 int cnt = this->numTextureSamplers();
122 for (int i = 1; i < cnt; ++i) {
123 fSamplers[i].~TextureSampler();
124 }
Brian Salomon34169692017-08-28 15:32:01 -0400125 }
126
127 const char* name() const override { return "TextureGeometryProcessor"; }
128
129 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
130 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400131 uint32_t x = this->usesCoverageEdgeAA() ? 0 : 1;
Brian Salomon70132d02018-05-29 15:33:06 -0400132 x |= kFloat3_GrVertexAttribType == fPositions.type() ? 0 : 2;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400133 x |= fDomain.isInitialized() ? 4 : 0;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400134 b->add32(x);
Brian Salomon34169692017-08-28 15:32:01 -0400135 }
136
137 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
138 class GLSLProcessor : public GrGLSLGeometryProcessor {
139 public:
140 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
141 FPCoordTransformIter&& transformIter) override {
142 const auto& textureGP = proc.cast<TextureGeometryProcessor>();
143 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
144 if (fColorSpaceXformHelper.isValid()) {
145 fColorSpaceXformHelper.setData(pdman, textureGP.fColorSpaceXform.get());
146 }
147 }
148
149 private:
150 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Chris Dalton7b046312018-02-02 11:06:30 -0700151 using Interpolation = GrGLSLVaryingHandler::Interpolation;
Brian Salomon34169692017-08-28 15:32:01 -0400152 const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>();
153 fColorSpaceXformHelper.emitCode(
154 args.fUniformHandler, textureGP.fColorSpaceXform.get());
Brian Salomon70132d02018-05-29 15:33:06 -0400155 if (kFloat2_GrVertexAttribType == textureGP.fPositions.type()) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400156 args.fVaryingHandler->setNoPerspective();
157 }
Brian Salomon34169692017-08-28 15:32:01 -0400158 args.fVaryingHandler->emitAttributes(textureGP);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400159 gpArgs->fPositionVar = textureGP.fPositions.asShaderVar();
160
Brian Salomon34169692017-08-28 15:32:01 -0400161 this->emitTransforms(args.fVertBuilder,
162 args.fVaryingHandler,
163 args.fUniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -0500164 textureGP.fTextureCoords.asShaderVar(),
Brian Salomon34169692017-08-28 15:32:01 -0400165 args.fFPCoordTransformHandler);
Chris Dalton7b046312018-02-02 11:06:30 -0700166 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors,
167 args.fOutputColor,
168 Interpolation::kCanBeFlat);
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400169 args.fFragBuilder->codeAppend("float2 texCoord;");
Chris Daltonfdde34e2017-10-16 14:15:26 -0600170 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords,
171 "texCoord");
Brian Salomonb80ffee2018-05-23 16:39:39 -0400172 if (textureGP.fDomain.isInitialized()) {
173 args.fFragBuilder->codeAppend("float4 domain;");
174 args.fVaryingHandler->addPassThroughAttribute(
175 &textureGP.fDomain, "domain",
176 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
177 args.fFragBuilder->codeAppend(
178 "texCoord = clamp(texCoord, domain.xy, domain.zw);");
179 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400180 if (textureGP.numTextureSamplers() > 1) {
Chris Dalton7b046312018-02-02 11:06:30 -0700181 // If this changes to float, reconsider Interpolation::kMustBeFlat.
Brian Salomon70132d02018-05-29 15:33:06 -0400182 SkASSERT(kInt_GrVertexAttribType == textureGP.fTextureIdx.type());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400183 SkASSERT(args.fShaderCaps->integerSupport());
184 args.fFragBuilder->codeAppend("int texIdx;");
Chris Dalton7b046312018-02-02 11:06:30 -0700185 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx, "texIdx",
186 Interpolation::kMustBeFlat);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400187 args.fFragBuilder->codeAppend("switch (texIdx) {");
188 for (int i = 0; i < textureGP.numTextureSamplers(); ++i) {
189 args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor);
190 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
191 args.fTexSamplers[i],
192 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400193 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400194 &fColorSpaceXformHelper);
195 args.fFragBuilder->codeAppend("; break;");
196 }
197 args.fFragBuilder->codeAppend("}");
198 } else {
199 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
200 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
201 args.fTexSamplers[0],
202 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400203 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400204 &fColorSpaceXformHelper);
205 }
Brian Salomon34169692017-08-28 15:32:01 -0400206 args.fFragBuilder->codeAppend(";");
Brian Salomon485b8c62018-01-12 15:11:06 -0500207 if (textureGP.usesCoverageEdgeAA()) {
Brian Salomondba65f92018-01-22 08:43:38 -0500208 const char* aaDistName = nullptr;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400209 bool mulByFragCoordW = false;
210 // When interpolation is inaccurate we perform the evaluation of the edge
Brian Salomondba65f92018-01-22 08:43:38 -0500211 // equations in the fragment shader rather than interpolating values computed
212 // in the vertex shader.
213 if (!args.fShaderCaps->interpolantsAreInaccurate()) {
214 GrGLSLVarying aaDistVarying(kFloat4_GrSLType,
215 GrGLSLVarying::Scope::kVertToFrag);
Brian Salomon70132d02018-05-29 15:33:06 -0400216 if (kFloat3_GrVertexAttribType == textureGP.fPositions.type()) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400217 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
218 // The distance from edge equation e to homogenous point p=sk_Position
219 // is e.x*p.x/p.wx + e.y*p.y/p.w + e.z. However, we want screen space
220 // interpolation of this distance. We can do this by multiplying the
221 // varying in the VS by p.w and then multiplying by sk_FragCoord.w in
222 // the FS. So we output e.x*p.x + e.y*p.y + e.z * p.w
223 args.fVertBuilder->codeAppendf(
224 R"(%s = float4(dot(aaEdge0, %s), dot(aaEdge1, %s),
225 dot(aaEdge2, %s), dot(aaEdge3, %s));)",
Brian Salomon70132d02018-05-29 15:33:06 -0400226 aaDistVarying.vsOut(), textureGP.fPositions.name(),
227 textureGP.fPositions.name(), textureGP.fPositions.name(),
228 textureGP.fPositions.name());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400229 mulByFragCoordW = true;
230 } else {
231 args.fVaryingHandler->addVarying("aaDists", &aaDistVarying);
232 args.fVertBuilder->codeAppendf(
233 R"(%s = float4(dot(aaEdge0.xy, %s.xy) + aaEdge0.z,
234 dot(aaEdge1.xy, %s.xy) + aaEdge1.z,
235 dot(aaEdge2.xy, %s.xy) + aaEdge2.z,
236 dot(aaEdge3.xy, %s.xy) + aaEdge3.z);)",
Brian Salomon70132d02018-05-29 15:33:06 -0400237 aaDistVarying.vsOut(), textureGP.fPositions.name(),
238 textureGP.fPositions.name(), textureGP.fPositions.name(),
239 textureGP.fPositions.name());
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400240 }
Brian Salomondba65f92018-01-22 08:43:38 -0500241 aaDistName = aaDistVarying.fsIn();
242 } else {
243 GrGLSLVarying aaEdgeVarying[4]{
244 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
245 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
246 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag},
247 {kFloat3_GrSLType, GrGLSLVarying::Scope::kVertToFrag}
248 };
249 for (int i = 0; i < 4; ++i) {
250 SkString name;
251 name.printf("aaEdge%d", i);
Brian Salomon7d982c62018-02-05 16:20:47 -0500252 args.fVaryingHandler->addVarying(name.c_str(), &aaEdgeVarying[i],
253 Interpolation::kCanBeFlat);
Brian Salomondba65f92018-01-22 08:43:38 -0500254 args.fVertBuilder->codeAppendf(
255 "%s = aaEdge%d;", aaEdgeVarying[i].vsOut(), i);
256 }
257 args.fFragBuilder->codeAppendf(
258 R"(float4 aaDists = float4(dot(%s.xy, sk_FragCoord.xy) + %s.z,
259 dot(%s.xy, sk_FragCoord.xy) + %s.z,
260 dot(%s.xy, sk_FragCoord.xy) + %s.z,
261 dot(%s.xy, sk_FragCoord.xy) + %s.z);)",
262 aaEdgeVarying[0].fsIn(), aaEdgeVarying[0].fsIn(),
263 aaEdgeVarying[1].fsIn(), aaEdgeVarying[1].fsIn(),
264 aaEdgeVarying[2].fsIn(), aaEdgeVarying[2].fsIn(),
265 aaEdgeVarying[3].fsIn(), aaEdgeVarying[3].fsIn());
266 aaDistName = "aaDists";
267 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500268 args.fFragBuilder->codeAppendf(
269 "float mindist = min(min(%s.x, %s.y), min(%s.z, %s.w));",
Brian Salomondba65f92018-01-22 08:43:38 -0500270 aaDistName, aaDistName, aaDistName, aaDistName);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400271 if (mulByFragCoordW) {
272 args.fFragBuilder->codeAppend("mindist *= sk_FragCoord.w;");
273 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500274 args.fFragBuilder->codeAppendf("%s = float4(clamp(mindist, 0, 1));",
275 args.fOutputCoverage);
276 } else {
277 args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
278 }
Brian Salomon34169692017-08-28 15:32:01 -0400279 }
280 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
281 };
282 return new GLSLProcessor;
283 }
284
Brian Salomon485b8c62018-01-12 15:11:06 -0500285 bool usesCoverageEdgeAA() const { return SkToBool(fAAEdges[0].isInitialized()); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500286
Brian Salomon34169692017-08-28 15:32:01 -0400287private:
Brian Salomon336ce7b2017-09-08 08:23:58 -0400288 // This exists to reduce the number of shaders generated. It does some rounding of sampler
289 // counts.
290 static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) {
291 SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures &&
292 numRealProxies <= caps.maxFragmentSamplers());
293 if (1 == numRealProxies) {
294 return 1;
295 }
296 if (numRealProxies <= 4) {
297 return 4;
298 }
299 // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps.
300 return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers()));
301 }
302
303 TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400304 sk_sp<GrColorSpaceXform> csxf, bool coverageAA, bool perspective,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400305 Domain domain, const GrSamplerState::Filter filters[],
306 const GrShaderCaps& caps)
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500307 : INHERITED(kTextureGeometryProcessor_ClassID), fColorSpaceXform(std::move(csxf)) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400308 SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400309 fSamplers[0].reset(std::move(proxies[0]), filters[0]);
310 this->addTextureSampler(&fSamplers[0]);
311 for (int i = 1; i < proxyCnt; ++i) {
312 // This class has one sampler built in, the rest come from memory this processor was
313 // placement-newed into and so haven't been constructed.
314 new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]);
315 this->addTextureSampler(&fSamplers[i]);
316 }
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400317
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400318 if (perspective) {
319 fPositions = this->addVertexAttrib("position", kFloat3_GrVertexAttribType);
320 } else {
321 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
322 }
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400323 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Brian Salomon17031a72018-05-22 14:14:07 -0400324 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400325
Brian Salomon336ce7b2017-09-08 08:23:58 -0400326 if (samplerCnt > 1) {
327 // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt
328 // times.
329 GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy();
330 for (int i = proxyCnt; i < samplerCnt; ++i) {
331 new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]);
332 this->addTextureSampler(&fSamplers[i]);
333 }
334 SkASSERT(caps.integerSupport());
335 fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType);
336 }
Brian Salomonb80ffee2018-05-23 16:39:39 -0400337 if (domain == Domain::kYes) {
338 fDomain = this->addVertexAttrib("domain", kFloat4_GrVertexAttribType);
339 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500340 if (coverageAA) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500341 fAAEdges[0] = this->addVertexAttrib("aaEdge0", kFloat3_GrVertexAttribType);
342 fAAEdges[1] = this->addVertexAttrib("aaEdge1", kFloat3_GrVertexAttribType);
343 fAAEdges[2] = this->addVertexAttrib("aaEdge2", kFloat3_GrVertexAttribType);
344 fAAEdges[3] = this->addVertexAttrib("aaEdge3", kFloat3_GrVertexAttribType);
345 }
Brian Salomon34169692017-08-28 15:32:01 -0400346 }
347
348 Attribute fPositions;
Brian Salomon34169692017-08-28 15:32:01 -0400349 Attribute fColors;
Brian Salomon30e1a5e2018-05-18 12:32:32 -0400350 Attribute fTextureCoords;
351 Attribute fTextureIdx;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400352 Attribute fDomain;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500353 Attribute fAAEdges[4];
Brian Salomon34169692017-08-28 15:32:01 -0400354 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400355 TextureSampler fSamplers[1];
Ethan Nicholasabff9562017-10-09 10:54:08 -0400356
357 typedef GrGeometryProcessor INHERITED;
Brian Salomon34169692017-08-28 15:32:01 -0400358};
359
Brian Salomon6872e942018-05-18 10:29:54 -0400360// This computes the four edge equations for a quad, then outsets them and computes a new quad
361// as the intersection points of the outset edges. 'x' and 'y' contain the original points as input
362// and the outset points as output. 'a', 'b', and 'c' are the edge equation coefficients on output.
363static void compute_quad_edges_and_outset_vertices(Sk4f* x, Sk4f* y, Sk4f* a, Sk4f* b, Sk4f* c) {
364 static constexpr auto fma = SkNx_fma<4, float>;
365 // These rotate the points/edge values either clockwise or counterclockwise assuming tri strip
366 // order.
367 auto nextCW = [](const Sk4f& v) { return SkNx_shuffle<2, 0, 3, 1>(v); };
368 auto nextCCW = [](const Sk4f& v) { return SkNx_shuffle<1, 3, 0, 2>(v); };
369
370 auto xnext = nextCCW(*x);
371 auto ynext = nextCCW(*y);
372 *a = ynext - *y;
373 *b = *x - xnext;
374 *c = fma(xnext, *y, -ynext * *x);
375 Sk4f invNormLengths = (*a * *a + *b * *b).rsqrt();
376 // Make sure the edge equations have their normals facing into the quad in device space.
377 auto test = fma(*a, nextCW(*x), fma(*b, nextCW(*y), *c));
378 if ((test < Sk4f(0)).anyTrue()) {
379 invNormLengths = -invNormLengths;
380 }
381 *a *= invNormLengths;
382 *b *= invNormLengths;
383 *c *= invNormLengths;
384
385 // Here is the outset. This makes our edge equations compute coverage without requiring a
386 // half pixel offset and is also used to compute the bloated quad that will cover all
387 // pixels.
388 *c += Sk4f(0.5f);
389
390 // Reverse the process to compute the points of the bloated quad from the edge equations.
391 // This time the inputs don't have 1s as their third coord and we want to homogenize rather
392 // than normalize.
393 auto anext = nextCW(*a);
394 auto bnext = nextCW(*b);
395 auto cnext = nextCW(*c);
396 *x = fma(bnext, *c, -*b * cnext);
397 *y = fma(*a, cnext, -anext * *c);
398 auto ic = (fma(anext, *b, -bnext * *a)).invert();
399 *x *= ic;
400 *y *= ic;
401}
402
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500403namespace {
404// This is a class soley so it can be partially specialized (functions cannot be).
Brian Salomon86c40012018-05-22 10:48:49 -0400405template <typename V, GrAA AA = V::kAA, typename Position = typename V::Position>
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400406class VertexAAHandler;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500407
Brian Salomon86c40012018-05-22 10:48:49 -0400408template<typename V> class VertexAAHandler<V, GrAA::kNo, SkPoint> {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500409public:
Brian Salomon86c40012018-05-22 10:48:49 -0400410 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500411 const SkRect& texRect) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400412 SkASSERT((quad.w4f() == Sk4f(1.f)).allTrue());
Brian Salomon86c40012018-05-22 10:48:49 -0400413 SkPointPriv::SetRectTriStrip(&vertices[0].fTextureCoords, texRect, sizeof(V));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400414 for (int i = 0; i < 4; ++i) {
415 vertices[i].fPosition = {quad.x(i), quad.y(i)};
416 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500417 }
418};
419
Brian Salomon86c40012018-05-22 10:48:49 -0400420template<typename V> class VertexAAHandler<V, GrAA::kNo, SkPoint3> {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500421public:
Brian Salomon86c40012018-05-22 10:48:49 -0400422 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500423 const SkRect& texRect) {
Brian Salomon86c40012018-05-22 10:48:49 -0400424 SkPointPriv::SetRectTriStrip(&vertices[0].fTextureCoords, texRect, sizeof(V));
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400425 for (int i = 0; i < 4; ++i) {
426 vertices[i].fPosition = quad.point(i);
427 }
428 }
429};
430
Brian Salomon86c40012018-05-22 10:48:49 -0400431template<typename V> class VertexAAHandler<V, GrAA::kYes, SkPoint> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400432public:
Brian Salomon86c40012018-05-22 10:48:49 -0400433 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400434 const SkRect& texRect) {
435 SkASSERT((quad.w4f() == Sk4f(1.f)).allTrue());
Brian Salomon6872e942018-05-18 10:29:54 -0400436 auto x = quad.x4f();
437 auto y = quad.y4f();
438 Sk4f a, b, c;
439 compute_quad_edges_and_outset_vertices(&x, &y, &a, &b, &c);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500440
441 for (int i = 0; i < 4; ++i) {
Brian Salomon6872e942018-05-18 10:29:54 -0400442 vertices[i].fPosition = {x[i], y[i]};
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500443 for (int j = 0; j < 4; ++j) {
Brian Salomon6872e942018-05-18 10:29:54 -0400444 vertices[i].fEdges[j] = {a[j], b[j], c[j]};
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500445 }
446 }
447
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500448 AssignTexCoords(vertices, quad, texRect);
449 }
450
451private:
Brian Salomon86c40012018-05-22 10:48:49 -0400452 static void AssignTexCoords(V* vertices, const GrPerspQuad& quad, const SkRect& tex) {
Brian Salomona33b67c2018-05-17 10:42:14 -0400453 SkMatrix q = SkMatrix::MakeAll(quad.x(0), quad.x(1), quad.x(2),
454 quad.y(0), quad.y(1), quad.y(2),
455 1.f, 1.f, 1.f);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500456 SkMatrix qinv;
457 if (!q.invert(&qinv)) {
458 return;
459 }
460 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
461 tex.fTop, tex.fBottom, tex.fTop,
462 1.f, 1.f, 1.f);
463 SkMatrix map;
464 map.setConcat(t, qinv);
Brian Salomon86c40012018-05-22 10:48:49 -0400465 SkMatrixPriv::MapPointsWithStride(map, &vertices[0].fTextureCoords, sizeof(V),
466 &vertices[0].fPosition, sizeof(V), 4);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500467 }
468};
469
Brian Salomon86c40012018-05-22 10:48:49 -0400470template<typename V> class VertexAAHandler<V, GrAA::kYes, SkPoint3> {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400471public:
Brian Salomon86c40012018-05-22 10:48:49 -0400472 static void AssignPositionsAndTexCoords(V* vertices, const GrPerspQuad& quad,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400473 const SkRect& texRect) {
474 auto x = quad.x4f();
475 auto y = quad.y4f();
476 auto iw = quad.iw4f();
477 x *= iw;
478 y *= iw;
479
480 // Get an equation for w from device space coords.
481 SkMatrix P;
482 P.setAll(x[0], y[0], 1, x[1], y[1], 1, x[2], y[2], 1);
483 SkAssertResult(P.invert(&P));
484 SkPoint3 weq{quad.w(0), quad.w(1), quad.w(2)};
485 P.mapHomogeneousPoints(&weq, &weq, 1);
486
487 Sk4f a, b, c;
488 compute_quad_edges_and_outset_vertices(&x, &y, &a, &b, &c);
489
490 // Compute new w values for the output vertices;
491 auto w = Sk4f(weq.fX) * x + Sk4f(weq.fY) * y + Sk4f(weq.fZ);
492 x *= w;
493 y *= w;
494
495 for (int i = 0; i < 4; ++i) {
496 vertices[i].fPosition = {x[i], y[i], w[i]};
497 for (int j = 0; j < 4; ++j) {
498 vertices[i].fEdges[j] = {a[j], b[j], c[j]};
499 }
500 }
501
502 AssignTexCoords(vertices, quad, texRect);
503 }
504
505private:
Brian Salomon86c40012018-05-22 10:48:49 -0400506 static void AssignTexCoords(V* vertices, const GrPerspQuad& quad, const SkRect& tex) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400507 SkMatrix q = SkMatrix::MakeAll(quad.x(0), quad.x(1), quad.x(2),
508 quad.y(0), quad.y(1), quad.y(2),
509 quad.w(0), quad.w(1), quad.w(2));
510 SkMatrix qinv;
511 if (!q.invert(&qinv)) {
512 return;
513 }
514 SkMatrix t = SkMatrix::MakeAll(tex.fLeft, tex.fLeft, tex.fRight,
515 tex.fTop, tex.fBottom, tex.fTop,
516 1.f, 1.f, 1.f);
517 SkMatrix map;
518 map.setConcat(t, qinv);
519 SkPoint3 tempTexCoords[4];
520 SkMatrixPriv::MapHomogeneousPointsWithStride(map, tempTexCoords, sizeof(SkPoint3),
Brian Salomon86c40012018-05-22 10:48:49 -0400521 &vertices[0].fPosition, sizeof(V), 4);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400522 for (int i = 0; i < 4; ++i) {
523 auto invW = 1.f / tempTexCoords[i].fZ;
524 vertices[i].fTextureCoords.fX = tempTexCoords[i].fX * invW;
525 vertices[i].fTextureCoords.fY = tempTexCoords[i].fY * invW;
526 }
527 }
528};
529
Brian Salomon17031a72018-05-22 14:14:07 -0400530template <typename V, MultiTexture MT = V::kMultiTexture> struct TexIdAssigner;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500531
Brian Salomon17031a72018-05-22 14:14:07 -0400532template <typename V> struct TexIdAssigner<V, MultiTexture::kYes> {
Brian Salomon86c40012018-05-22 10:48:49 -0400533 static void Assign(V* vertices, int textureIdx) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400534 for (int i = 0; i < 4; ++i) {
535 vertices[i].fTextureIdx = textureIdx;
536 }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500537 }
538};
539
Brian Salomon17031a72018-05-22 14:14:07 -0400540template <typename V> struct TexIdAssigner<V, MultiTexture::kNo> {
Brian Salomon86c40012018-05-22 10:48:49 -0400541 static void Assign(V* vertices, int textureIdx) {}
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500542};
Brian Salomonb80ffee2018-05-23 16:39:39 -0400543
544template <typename V, Domain D = V::kDomain> struct DomainAssigner;
545
546template <typename V> struct DomainAssigner<V, Domain::kYes> {
547 static void Assign(V* vertices, Domain domain, GrSamplerState::Filter filter,
548 const SkRect& srcRect, GrSurfaceOrigin origin, float iw, float ih) {
549 static constexpr SkRect kLargeRect = {-2, -2, 2, 2};
550 SkRect domainRect;
551 if (domain == Domain::kYes) {
552 auto ltrb = Sk4f::Load(&srcRect);
553 if (filter == GrSamplerState::Filter::kBilerp) {
554 auto rblt = SkNx_shuffle<2, 3, 0, 1>(ltrb);
555 auto whwh = (rblt - ltrb).abs();
556 auto c = (rblt + ltrb) * 0.5f;
557 static const Sk4f kOffsets = {0.5f, 0.5f, -0.5f, -0.5f};
558 ltrb = (whwh < 1.f).thenElse(c, ltrb + kOffsets);
559 }
560 ltrb *= Sk4f(iw, ih, iw, ih);
561 if (origin == kBottomLeft_GrSurfaceOrigin) {
562 static const Sk4f kMul = {1.f, -1.f, 1.f, -1.f};
563 static const Sk4f kAdd = {0.f, 1.f, 0.f, 1.f};
564 ltrb = SkNx_shuffle<0, 3, 2, 1>(kMul * ltrb + kAdd);
565 }
566 ltrb.store(&domainRect);
567 } else {
568 domainRect = kLargeRect;
569 }
570 for (int i = 0; i < 4; ++i) {
571 vertices[i].fTextureDomain = domainRect;
572 }
573 }
574};
575
576template <typename V> struct DomainAssigner<V, Domain::kNo> {
577 static void Assign(V*, Domain domain, GrSamplerState::Filter, const SkRect&, GrSurfaceOrigin,
578 float iw, float ih) {
579 SkASSERT(domain == Domain::kNo);
580 }
581};
582
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500583} // anonymous namespace
584
Brian Salomon86c40012018-05-22 10:48:49 -0400585template <typename V>
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400586static void tessellate_quad(const GrPerspQuad& devQuad, const SkRect& srcRect, GrColor color,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400587 GrSurfaceOrigin origin, GrSamplerState::Filter filter, V* vertices,
588 SkScalar iw, SkScalar ih, int textureIdx, Domain domain) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500589 SkRect texRect = {
590 iw * srcRect.fLeft,
591 ih * srcRect.fTop,
592 iw * srcRect.fRight,
593 ih * srcRect.fBottom
594 };
595 if (origin == kBottomLeft_GrSurfaceOrigin) {
596 texRect.fTop = 1.f - texRect.fTop;
597 texRect.fBottom = 1.f - texRect.fBottom;
598 }
Brian Salomon86c40012018-05-22 10:48:49 -0400599 VertexAAHandler<V>::AssignPositionsAndTexCoords(vertices, devQuad, texRect);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500600 vertices[0].fColor = color;
601 vertices[1].fColor = color;
602 vertices[2].fColor = color;
603 vertices[3].fColor = color;
Brian Salomon86c40012018-05-22 10:48:49 -0400604 TexIdAssigner<V>::Assign(vertices, textureIdx);
Brian Salomonb80ffee2018-05-23 16:39:39 -0400605 DomainAssigner<V>::Assign(vertices, domain, filter, srcRect, origin, iw, ih);
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500606}
Brian Salomon17031a72018-05-22 14:14:07 -0400607
Brian Salomon34169692017-08-28 15:32:01 -0400608/**
609 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
610 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
611 */
612class TextureOp final : public GrMeshDrawOp {
613public:
Robert Phillips7c525e62018-06-12 10:11:12 -0400614 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
615 sk_sp<GrTextureProxy> proxy,
616 GrSamplerState::Filter filter,
617 GrColor color,
618 const SkRect& srcRect,
619 const SkRect& dstRect,
620 GrAAType aaType,
621 SkCanvas::SrcRectConstraint constraint,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400622 const SkMatrix& viewMatrix,
623 sk_sp<GrColorSpaceXform> csxf) {
Brian Salomon34169692017-08-28 15:32:01 -0400624 return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400625 dstRect, aaType, constraint, viewMatrix,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400626 std::move(csxf)));
Brian Salomon34169692017-08-28 15:32:01 -0400627 }
628
Brian Salomon336ce7b2017-09-08 08:23:58 -0400629 ~TextureOp() override {
630 if (fFinalized) {
631 auto proxies = this->proxies();
632 for (int i = 0; i < fProxyCnt; ++i) {
633 proxies[i]->completedRead();
634 }
635 if (fProxyCnt > 1) {
636 delete[] reinterpret_cast<const char*>(proxies);
637 }
638 } else {
639 SkASSERT(1 == fProxyCnt);
640 fProxy0->unref();
641 }
642 }
Brian Salomon34169692017-08-28 15:32:01 -0400643
644 const char* name() const override { return "TextureOp"; }
645
Robert Phillipsf1748f52017-09-14 14:11:24 -0400646 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400647 auto proxies = this->proxies();
648 for (int i = 0; i < fProxyCnt; ++i) {
649 func(proxies[i]);
650 }
651 }
652
Brian Salomon34169692017-08-28 15:32:01 -0400653 SkString dumpInfo() const override {
654 SkString str;
Brian Salomon34169692017-08-28 15:32:01 -0400655 str.appendf("# draws: %d\n", fDraws.count());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400656 auto proxies = this->proxies();
657 for (int i = 0; i < fProxyCnt; ++i) {
658 str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(),
659 static_cast<int>(this->filters()[i]));
660 }
Brian Salomon34169692017-08-28 15:32:01 -0400661 for (int i = 0; i < fDraws.count(); ++i) {
662 const Draw& draw = fDraws[i];
663 str.appendf(
Brian Salomon336ce7b2017-09-08 08:23:58 -0400664 "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
665 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
Brian Salomonb80ffee2018-05-23 16:39:39 -0400666 i, draw.color(), draw.textureIdx(), draw.srcRect().fLeft, draw.srcRect().fTop,
667 draw.srcRect().fRight, draw.srcRect().fBottom, draw.quad().point(0).fX,
668 draw.quad().point(0).fY, draw.quad().point(1).fX, draw.quad().point(1).fY,
669 draw.quad().point(2).fX, draw.quad().point(2).fY, draw.quad().point(3).fX,
670 draw.quad().point(3).fY);
Brian Salomon34169692017-08-28 15:32:01 -0400671 }
672 str += INHERITED::dumpInfo();
673 return str;
674 }
675
Brian Osman9a725dd2017-09-20 09:53:22 -0400676 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
677 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon34169692017-08-28 15:32:01 -0400678 SkASSERT(!fFinalized);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400679 SkASSERT(1 == fProxyCnt);
Brian Salomon34169692017-08-28 15:32:01 -0400680 fFinalized = true;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400681 fProxy0->addPendingRead();
682 fProxy0->unref();
Brian Salomon34169692017-08-28 15:32:01 -0400683 return RequiresDstTexture::kNo;
684 }
685
Brian Salomon485b8c62018-01-12 15:11:06 -0500686 FixedFunctionFlags fixedFunctionFlags() const override {
687 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
688 : FixedFunctionFlags::kNone;
689 }
Brian Salomon34169692017-08-28 15:32:01 -0400690
691 DEFINE_OP_CLASS_ID
692
693private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400694 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500695
696 // This is used in a heursitic for choosing a code path. We don't care what happens with
697 // really large rects, infs, nans, etc.
698#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
699__attribute__((no_sanitize("float-cast-overflow")))
700#endif
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500701 size_t RectSizeAsSizeT(const SkRect& rect) {;
Brian Salomon762d5e72017-12-01 10:25:08 -0500702 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
703 }
704
Brian Salomon336ce7b2017-09-08 08:23:58 -0400705 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
706
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400707 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomon485b8c62018-01-12 15:11:06 -0500708 const SkRect& srcRect, const SkRect& dstRect, GrAAType aaType,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400709 SkCanvas::SrcRectConstraint constraint, const SkMatrix& viewMatrix,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400710 sk_sp<GrColorSpaceXform> csxf)
Brian Salomon34169692017-08-28 15:32:01 -0400711 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400712 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400713 , fProxy0(proxy.release())
714 , fFilter0(filter)
715 , fProxyCnt(1)
Brian Salomon485b8c62018-01-12 15:11:06 -0500716 , fAAType(static_cast<unsigned>(aaType))
Brian Osman2b23c4b2018-06-01 12:25:08 -0400717 , fFinalized(0) {
Brian Salomon485b8c62018-01-12 15:11:06 -0500718 SkASSERT(aaType != GrAAType::kMixedSamples);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400719 fPerspective = viewMatrix.hasPerspective();
Brian Salomon594b64c2018-05-29 12:47:57 -0400720 auto quad = GrPerspQuad(dstRect, viewMatrix);
721 auto bounds = quad.bounds();
722#ifndef SK_DONT_DROP_UNNECESSARY_AA_IN_TEXTURE_OP
723 if (GrAAType::kCoverage == this->aaType() && viewMatrix.rectStaysRect()) {
724 // Disable coverage AA when rect falls on integers in device space.
725 auto is_int = [](float f) { return f == sk_float_floor(f); };
726 if (is_int(bounds.fLeft) && is_int(bounds.fTop) && is_int(bounds.fRight) &&
727 is_int(bounds.fBottom)) {
728 fAAType = static_cast<unsigned>(GrAAType::kNone);
729 // We may have had a strict constraint with nearest filter soley due to possible AA
730 // bloat. In that case it's no longer necessary.
731 if (constraint == SkCanvas::kStrict_SrcRectConstraint &&
732 filter == GrSamplerState::Filter::kNearest) {
733 constraint = SkCanvas::kFast_SrcRectConstraint;
734 }
735 }
736 }
737#endif
738 const auto& draw = fDraws.emplace_back(srcRect, 0, quad, constraint, color);
Brian Salomon34169692017-08-28 15:32:01 -0400739 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon594b64c2018-05-29 12:47:57 -0400740 fDomain = static_cast<bool>(draw.domain());
Brian Salomon762d5e72017-12-01 10:25:08 -0500741 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400742 }
743
Brian Salomonb80ffee2018-05-23 16:39:39 -0400744 template <typename Pos, MultiTexture MT, Domain D, GrAA AA>
Brian Salomon17031a72018-05-22 14:14:07 -0400745 void tess(void* v, const float iw[], const float ih[], const GrGeometryProcessor* gp) {
Brian Salomonb80ffee2018-05-23 16:39:39 -0400746 using Vertex = TextureGeometryProcessor::Vertex<Pos, MT, D, AA>;
Brian Salomon17031a72018-05-22 14:14:07 -0400747 SkASSERT(gp->getVertexStride() == sizeof(Vertex));
748 auto vertices = static_cast<Vertex*>(v);
749 auto proxies = this->proxies();
Brian Salomonb80ffee2018-05-23 16:39:39 -0400750 auto filters = this->filters();
Brian Salomon17031a72018-05-22 14:14:07 -0400751 for (const auto& draw : fDraws) {
Brian Salomonb80ffee2018-05-23 16:39:39 -0400752 auto textureIdx = draw.textureIdx();
753 auto origin = proxies[textureIdx]->origin();
754 tessellate_quad<Vertex>(draw.quad(), draw.srcRect(), draw.color(), origin,
755 filters[textureIdx], vertices, iw[textureIdx], ih[textureIdx],
756 textureIdx, draw.domain());
Brian Salomon17031a72018-05-22 14:14:07 -0400757 vertices += 4;
758 }
759 }
760
Brian Salomon34169692017-08-28 15:32:01 -0400761 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400762 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
763 auto proxies = this->proxies();
764 auto filters = this->filters();
765 for (int i = 0; i < fProxyCnt; ++i) {
766 if (!proxies[i]->instantiate(target->resourceProvider())) {
767 return;
768 }
769 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400770 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400771
Brian Salomonb80ffee2018-05-23 16:39:39 -0400772 Domain domain = fDomain ? Domain::kYes : Domain::kNo;
Brian Salomon485b8c62018-01-12 15:11:06 -0500773 bool coverageAA = GrAAType::kCoverage == this->aaType();
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400774 sk_sp<GrGeometryProcessor> gp = TextureGeometryProcessor::Make(
775 proxiesSPs, fProxyCnt, std::move(fColorSpaceXform), coverageAA, fPerspective,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400776 domain, filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400777 GrPipeline::InitArgs args;
778 args.fProxy = target->proxy();
779 args.fCaps = &target->caps();
780 args.fResourceProvider = target->resourceProvider();
Brian Salomon485b8c62018-01-12 15:11:06 -0500781 args.fFlags = 0;
Brian Salomon485b8c62018-01-12 15:11:06 -0500782 if (GrAAType::kMSAA == this->aaType()) {
783 args.fFlags |= GrPipeline::kHWAntialias_Flag;
784 }
785
Brian Salomon34169692017-08-28 15:32:01 -0400786 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
787 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400788 int vstart;
789 const GrBuffer* vbuffer;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400790 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
791 &vstart);
792 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400793 SkDebugf("Could not allocate vertices\n");
794 return;
795 }
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400796
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400797 float iw[kMaxTextures];
798 float ih[kMaxTextures];
799 for (int t = 0; t < fProxyCnt; ++t) {
800 const auto* texture = proxies[t]->priv().peekTexture();
801 iw[t] = 1.f / texture->width();
802 ih[t] = 1.f / texture->height();
803 }
804
Brian Salomonb80ffee2018-05-23 16:39:39 -0400805 using TessFn =
806 decltype(&TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kNo, GrAA::kNo>);
Brian Salomonfff19662018-06-08 12:22:10 -0400807 static constexpr TessFn kTessFns[] = {
Brian Salomonb80ffee2018-05-23 16:39:39 -0400808 &TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kNo, GrAA::kNo>,
809 &TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kNo, GrAA::kYes>,
810 &TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kYes, GrAA::kNo>,
811 &TextureOp::tess<SkPoint, MultiTexture::kNo, Domain::kYes, GrAA::kYes>,
812 &TextureOp::tess<SkPoint, MultiTexture::kYes, Domain::kNo, GrAA::kNo>,
813 &TextureOp::tess<SkPoint, MultiTexture::kYes, Domain::kNo, GrAA::kYes>,
814 &TextureOp::tess<SkPoint, MultiTexture::kYes, Domain::kYes, GrAA::kNo>,
815 &TextureOp::tess<SkPoint, MultiTexture::kYes, Domain::kYes, GrAA::kYes>,
816 &TextureOp::tess<SkPoint3, MultiTexture::kNo, Domain::kNo, GrAA::kNo>,
817 &TextureOp::tess<SkPoint3, MultiTexture::kNo, Domain::kNo, GrAA::kYes>,
818 &TextureOp::tess<SkPoint3, MultiTexture::kNo, Domain::kYes, GrAA::kNo>,
819 &TextureOp::tess<SkPoint3, MultiTexture::kNo, Domain::kYes, GrAA::kYes>,
820 &TextureOp::tess<SkPoint3, MultiTexture::kYes, Domain::kNo, GrAA::kNo>,
821 &TextureOp::tess<SkPoint3, MultiTexture::kYes, Domain::kNo, GrAA::kYes>,
822 &TextureOp::tess<SkPoint3, MultiTexture::kYes, Domain::kYes, GrAA::kNo>,
823 &TextureOp::tess<SkPoint3, MultiTexture::kYes, Domain::kYes, GrAA::kYes>,
824 };
825 int tessFnIdx = 0;
826 tessFnIdx |= coverageAA ? 0x1 : 0x0;
827 tessFnIdx |= fDomain ? 0x2 : 0x0;
828 tessFnIdx |= (fProxyCnt > 1) ? 0x4 : 0x0;
829 tessFnIdx |= fPerspective ? 0x8 : 0x0;
830 (this->*(kTessFns[tessFnIdx]))(vdata, iw, ih, gp.get());
831
Brian Salomon57caa662017-10-18 12:21:05 +0000832 GrPrimitiveType primitiveType =
833 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
834 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400835 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000836 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400837 if (!ibuffer) {
838 SkDebugf("Could not allocate quad indices\n");
839 return;
840 }
Brian Salomon34169692017-08-28 15:32:01 -0400841 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
842 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400843 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400844 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400845 }
Brian Salomon57caa662017-10-18 12:21:05 +0000846 mesh.setVertexData(vbuffer, vstart);
847 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400848 }
849
850 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
851 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500852 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400853 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400854 return false;
855 }
Brian Salomon485b8c62018-01-12 15:11:06 -0500856 if (this->aaType() != that->aaType()) {
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500857 return false;
858 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400859 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
860 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500861 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
862 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
863 that->fMaxApproxDstPixelArea <=
864 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400865 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500866 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400867 if (numNewProxies < 0) {
868 return false;
869 }
870 if (1 == fProxyCnt && numNewProxies) {
871 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
872 kMaxTextures];
873 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
874 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
875 proxies[0] = fProxy0;
876 filters[0] = fFilter0;
877 fProxyArray = proxies;
878 }
879 fProxyCnt += numNewProxies;
880 auto thisProxies = fProxyArray;
881 auto thatProxies = that->proxies();
882 auto thatFilters = that->filters();
883 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
884 kMaxTextures);
885 for (int i = 0; i < that->fProxyCnt; ++i) {
886 if (map[i] < 0) {
887 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400888
Brian Salomon336ce7b2017-09-08 08:23:58 -0400889 thisProxies[-map[i]] = thatProxies[i];
890 thisFilters[-map[i]] = thatFilters[i];
891 map[i] = -map[i];
892 }
893 }
894 int firstNewDraw = fDraws.count();
895 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
896 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
Brian Salomonb80ffee2018-05-23 16:39:39 -0400897 fDraws[i].setTextureIdx(map[fDraws[i].textureIdx()]);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400898 }
899 } else {
Brian Salomonbbf05752017-11-30 11:30:48 -0500900 // We can get here when one of the ops is already multitextured but the other cannot
901 // be because of the dst rect size.
902 if (fProxyCnt > 1 || that->fProxyCnt > 1) {
903 return false;
904 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400905 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
906 return false;
907 }
908 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
909 }
Brian Salomon34169692017-08-28 15:32:01 -0400910 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500911 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400912 fPerspective |= that->fPerspective;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400913 fDomain |= that->fDomain;
Brian Salomon34169692017-08-28 15:32:01 -0400914 return true;
915 }
916
Brian Salomon336ce7b2017-09-08 08:23:58 -0400917 /**
918 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
919 * value means that's proxy should be added to this's proxy array at the absolute value of
920 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
921 * negative value is returned. Otherwise, return value indicates the number of proxies that have
922 * to be added to this op or, equivalently, the number of negative entries in map.
923 */
924 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
925 std::fill_n(map, kMaxTextures, -kMaxTextures);
926 int sharedProxyCnt = 0;
927 auto thisProxies = this->proxies();
928 auto thisFilters = this->filters();
929 auto thatProxies = that->proxies();
930 auto thatFilters = that->filters();
931 for (int i = 0; i < fProxyCnt; ++i) {
932 for (int j = 0; j < that->fProxyCnt; ++j) {
933 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
934 if (thisFilters[i] != thatFilters[j]) {
935 // In GL we don't currently support using the same texture with different
936 // samplers. If we added support for sampler objects and a cap bit to know
937 // it's ok to use different filter modes then we could support this.
938 // Otherwise, we could also only allow a single filter mode for each op
939 // instance.
940 return -1;
941 }
942 map[j] = i;
943 ++sharedProxyCnt;
944 break;
945 }
946 }
947 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500948 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400949 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
950 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
951 return -1;
952 }
953 GrPixelConfig config = thisProxies[0]->config();
954 int nextSlot = fProxyCnt;
955 for (int j = 0; j < that->fProxyCnt; ++j) {
956 // We want to avoid making many shaders because of different permutations of shader
957 // based swizzle and sampler types. The approach taken here is to require the configs to
958 // be the same and to only allow already instantiated proxies that have the most
959 // common sampler type. Otherwise we don't merge.
960 if (thatProxies[j]->config() != config) {
961 return -1;
962 }
963 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
964 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
965 return -1;
966 }
967 }
968 if (map[j] < 0) {
969 map[j] = -(nextSlot++);
970 }
971 }
972 return newProxyCnt;
973 }
974
Brian Salomon485b8c62018-01-12 15:11:06 -0500975 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500976
Brian Salomon336ce7b2017-09-08 08:23:58 -0400977 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
978
979 const GrSamplerState::Filter* filters() const {
980 if (fProxyCnt > 1) {
981 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
982 }
983 return &fFilter0;
984 }
985
Brian Salomonb80ffee2018-05-23 16:39:39 -0400986 class Draw {
987 public:
988 Draw(const SkRect& srcRect, int textureIdx, const GrPerspQuad& quad,
989 SkCanvas::SrcRectConstraint constraint, GrColor color)
990 : fSrcRect(srcRect)
991 , fHasDomain(constraint == SkCanvas::kStrict_SrcRectConstraint)
992 , fTextureIdx(SkToUInt(textureIdx))
993 , fQuad(quad)
994 , fColor(color) {}
995 const GrPerspQuad& quad() const { return fQuad; }
996 int textureIdx() const { return SkToInt(fTextureIdx); }
997 const SkRect& srcRect() const { return fSrcRect; }
998 GrColor color() const { return fColor; }
999 Domain domain() const { return Domain(fHasDomain); }
1000 void setTextureIdx(int i) { fTextureIdx = SkToUInt(i); }
1001
1002 private:
Brian Salomon34169692017-08-28 15:32:01 -04001003 SkRect fSrcRect;
Brian Salomonb80ffee2018-05-23 16:39:39 -04001004 unsigned fHasDomain : 1;
1005 unsigned fTextureIdx : 31;
Brian Salomonbe3c1d22018-05-21 12:54:39 -04001006 GrPerspQuad fQuad;
Brian Salomon34169692017-08-28 15:32:01 -04001007 GrColor fColor;
1008 };
1009 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -04001010 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001011 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
1012 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
1013 // followed by kMaxTextures filters.
1014 union {
1015 GrTextureProxy* fProxy0;
1016 GrTextureProxy** fProxyArray;
1017 };
Brian Salomonbbf05752017-11-30 11:30:48 -05001018 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001019 GrSamplerState::Filter fFilter0;
1020 uint8_t fProxyCnt;
Brian Salomon485b8c62018-01-12 15:11:06 -05001021 unsigned fAAType : 2;
Brian Salomonbe3c1d22018-05-21 12:54:39 -04001022 unsigned fPerspective : 1;
Brian Salomonb80ffee2018-05-23 16:39:39 -04001023 unsigned fDomain : 1;
Brian Salomon34169692017-08-28 15:32:01 -04001024 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -05001025 unsigned fFinalized : 1;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001026
Brian Salomon34169692017-08-28 15:32:01 -04001027 typedef GrMeshDrawOp INHERITED;
1028};
1029
Brian Salomon336ce7b2017-09-08 08:23:58 -04001030constexpr int TextureGeometryProcessor::kMaxTextures;
1031constexpr int TextureOp::kMaxTextures;
1032
Brian Salomon34169692017-08-28 15:32:01 -04001033} // anonymous namespace
1034
1035namespace GrTextureOp {
1036
Robert Phillips7c525e62018-06-12 10:11:12 -04001037std::unique_ptr<GrDrawOp> Make(GrContext* context,
1038 sk_sp<GrTextureProxy> proxy,
1039 GrSamplerState::Filter filter,
1040 GrColor color,
1041 const SkRect& srcRect,
1042 const SkRect& dstRect,
1043 GrAAType aaType,
1044 SkCanvas::SrcRectConstraint constraint,
1045 const SkMatrix& viewMatrix,
1046 sk_sp<GrColorSpaceXform> csxf) {
1047 return TextureOp::Make(context, std::move(proxy), filter, color, srcRect, dstRect, aaType,
1048 constraint, viewMatrix, std::move(csxf));
Brian Salomon34169692017-08-28 15:32:01 -04001049}
1050
1051} // namespace GrTextureOp
1052
1053#if GR_TEST_UTILS
1054#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -05001055#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001056#include "GrProxyProvider.h"
Brian Salomon34169692017-08-28 15:32:01 -04001057
1058GR_DRAW_OP_TEST_DEFINE(TextureOp) {
1059 GrSurfaceDesc desc;
1060 desc.fConfig = kRGBA_8888_GrPixelConfig;
1061 desc.fHeight = random->nextULessThan(90) + 10;
1062 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001063 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel09c94002018-06-08 22:11:51 +00001064 GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
1065 SkBackingFit fit = SkBackingFit::kExact;
1066 if (mipMapped == GrMipMapped::kNo) {
1067 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1068 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001069
1070 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Greg Daniel09c94002018-06-08 22:11:51 +00001071 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(desc, origin, mipMapped, fit,
1072 SkBudgeted::kNo,
1073 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001074
Brian Salomon34169692017-08-28 15:32:01 -04001075 SkRect rect = GrTest::TestRect(random);
1076 SkRect srcRect;
1077 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1078 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1079 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1080 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1081 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
1082 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001083 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
1084 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +00001085 while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
1086 filter = (GrSamplerState::Filter)random->nextULessThan(
1087 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
1088 }
Brian Salomon34169692017-08-28 15:32:01 -04001089 auto csxf = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001090 GrAAType aaType = GrAAType::kNone;
1091 if (random->nextBool()) {
1092 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
1093 }
Brian Salomonb80ffee2018-05-23 16:39:39 -04001094 auto constraint = random->nextBool() ? SkCanvas::kStrict_SrcRectConstraint
1095 : SkCanvas::kFast_SrcRectConstraint;
Robert Phillips7c525e62018-06-12 10:11:12 -04001096 return GrTextureOp::Make(context, std::move(proxy), filter, color, srcRect, rect, aaType,
1097 constraint, viewMatrix, std::move(csxf));
Brian Salomon34169692017-08-28 15:32:01 -04001098}
1099
1100#endif