blob: 79a22d83e4c8971a537677b357012620ed766ca5 [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 Salomon34169692017-08-28 15:32:01 -040023#include "glsl/GrGLSLColorSpaceXformHelper.h"
24#include "glsl/GrGLSLGeometryProcessor.h"
25#include "glsl/GrGLSLVarying.h"
26
27namespace {
28
29/**
30 * Geometry Processor that draws a texture modulated by a vertex color (though, this is meant to be
31 * the same value across all vertices of a quad and uses flat interpolation when available). This is
32 * used by TextureOp below.
33 */
34class TextureGeometryProcessor : public GrGeometryProcessor {
35public:
36 struct Vertex {
37 SkPoint fPosition;
38 SkPoint fTextureCoords;
39 GrColor fColor;
40 };
Brian Salomon336ce7b2017-09-08 08:23:58 -040041 struct MultiTextureVertex {
42 SkPoint fPosition;
43 int fTextureIdx;
44 SkPoint fTextureCoords;
45 GrColor fColor;
46 };
47
48 // Maximum number of textures supported by this op. Must also be checked against the caps
49 // limit. These numbers were based on some limited experiments on a HP Z840 and Pixel XL 2016
50 // and could probably use more tuning.
51#ifdef SK_BUILD_FOR_ANDROID
52 static constexpr int kMaxTextures = 4;
53#else
54 static constexpr int kMaxTextures = 8;
55#endif
56
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040057 static int SupportsMultitexture(const GrShaderCaps& caps) {
Brian Salomon762d5e72017-12-01 10:25:08 -050058 return caps.integerSupport() && caps.maxFragmentSamplers() > 1;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040059 }
Brian Salomon336ce7b2017-09-08 08:23:58 -040060
61 static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxies[], int proxyCnt,
Brian Salomon34169692017-08-28 15:32:01 -040062 sk_sp<GrColorSpaceXform> csxf,
Brian Salomon336ce7b2017-09-08 08:23:58 -040063 const GrSamplerState::Filter filters[],
64 const GrShaderCaps& caps) {
65 // We use placement new to avoid always allocating space for kMaxTextures TextureSampler
66 // instances.
67 int samplerCnt = NumSamplersToUse(proxyCnt, caps);
68 size_t size = sizeof(TextureGeometryProcessor) + sizeof(TextureSampler) * (samplerCnt - 1);
69 void* mem = GrGeometryProcessor::operator new(size);
70 return sk_sp<TextureGeometryProcessor>(new (mem) TextureGeometryProcessor(
71 proxies, proxyCnt, samplerCnt, std::move(csxf), filters, caps));
72 }
73
74 ~TextureGeometryProcessor() override {
75 int cnt = this->numTextureSamplers();
76 for (int i = 1; i < cnt; ++i) {
77 fSamplers[i].~TextureSampler();
78 }
Brian Salomon34169692017-08-28 15:32:01 -040079 }
80
81 const char* name() const override { return "TextureGeometryProcessor"; }
82
83 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
84 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
85 }
86
87 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
88 class GLSLProcessor : public GrGLSLGeometryProcessor {
89 public:
90 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
91 FPCoordTransformIter&& transformIter) override {
92 const auto& textureGP = proc.cast<TextureGeometryProcessor>();
93 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
94 if (fColorSpaceXformHelper.isValid()) {
95 fColorSpaceXformHelper.setData(pdman, textureGP.fColorSpaceXform.get());
96 }
97 }
98
99 private:
100 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
101 const auto& textureGP = args.fGP.cast<TextureGeometryProcessor>();
102 fColorSpaceXformHelper.emitCode(
103 args.fUniformHandler, textureGP.fColorSpaceXform.get());
104 args.fVaryingHandler->setNoPerspective();
105 args.fVaryingHandler->emitAttributes(textureGP);
106 this->writeOutputPosition(args.fVertBuilder, gpArgs, textureGP.fPositions.fName);
107 this->emitTransforms(args.fVertBuilder,
108 args.fVaryingHandler,
109 args.fUniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -0500110 textureGP.fTextureCoords.asShaderVar(),
Brian Salomon34169692017-08-28 15:32:01 -0400111 args.fFPCoordTransformHandler);
Brian Salomon41274562017-09-15 09:40:03 -0700112 if (args.fShaderCaps->preferFlatInterpolation()) {
Brian Salomon34169692017-08-28 15:32:01 -0400113 args.fVaryingHandler->addFlatPassThroughAttribute(&textureGP.fColors,
114 args.fOutputColor);
115 } else {
116 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors,
117 args.fOutputColor);
118 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400119 args.fFragBuilder->codeAppend("float2 texCoord;");
Chris Daltonfdde34e2017-10-16 14:15:26 -0600120 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords,
121 "texCoord");
Brian Salomon336ce7b2017-09-08 08:23:58 -0400122 if (textureGP.numTextureSamplers() > 1) {
123 SkASSERT(args.fShaderCaps->integerSupport());
124 args.fFragBuilder->codeAppend("int texIdx;");
125 if (args.fShaderCaps->flatInterpolationSupport()) {
126 args.fVaryingHandler->addFlatPassThroughAttribute(&textureGP.fTextureIdx,
127 "texIdx");
128 } else {
129 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx,
130 "texIdx");
131 }
132 args.fFragBuilder->codeAppend("switch (texIdx) {");
133 for (int i = 0; i < textureGP.numTextureSamplers(); ++i) {
134 args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor);
135 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
136 args.fTexSamplers[i],
137 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400138 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400139 &fColorSpaceXformHelper);
140 args.fFragBuilder->codeAppend("; break;");
141 }
142 args.fFragBuilder->codeAppend("}");
143 } else {
144 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
145 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
146 args.fTexSamplers[0],
147 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400148 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400149 &fColorSpaceXformHelper);
150 }
Brian Salomon34169692017-08-28 15:32:01 -0400151 args.fFragBuilder->codeAppend(";");
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400152 args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
Brian Salomon34169692017-08-28 15:32:01 -0400153 }
154 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
155 };
156 return new GLSLProcessor;
157 }
158
159private:
Brian Salomon336ce7b2017-09-08 08:23:58 -0400160 // This exists to reduce the number of shaders generated. It does some rounding of sampler
161 // counts.
162 static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) {
163 SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures &&
164 numRealProxies <= caps.maxFragmentSamplers());
165 if (1 == numRealProxies) {
166 return 1;
167 }
168 if (numRealProxies <= 4) {
169 return 4;
170 }
171 // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps.
172 return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers()));
173 }
174
175 TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt,
176 sk_sp<GrColorSpaceXform> csxf, const GrSamplerState::Filter filters[],
177 const GrShaderCaps& caps)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400178 : INHERITED(kTextureGeometryProcessor_ClassID)
179 , fColorSpaceXform(std::move(csxf)) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400180 SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400181 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400182 fSamplers[0].reset(std::move(proxies[0]), filters[0]);
183 this->addTextureSampler(&fSamplers[0]);
184 for (int i = 1; i < proxyCnt; ++i) {
185 // This class has one sampler built in, the rest come from memory this processor was
186 // placement-newed into and so haven't been constructed.
187 new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]);
188 this->addTextureSampler(&fSamplers[i]);
189 }
190 if (samplerCnt > 1) {
191 // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt
192 // times.
193 GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy();
194 for (int i = proxyCnt; i < samplerCnt; ++i) {
195 new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]);
196 this->addTextureSampler(&fSamplers[i]);
197 }
198 SkASSERT(caps.integerSupport());
199 fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType);
200 }
201
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400202 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
203 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Brian Salomon34169692017-08-28 15:32:01 -0400204 }
205
206 Attribute fPositions;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400207 Attribute fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400208 Attribute fTextureCoords;
209 Attribute fColors;
Brian Salomon34169692017-08-28 15:32:01 -0400210 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400211 TextureSampler fSamplers[1];
Ethan Nicholasabff9562017-10-09 10:54:08 -0400212
213 typedef GrGeometryProcessor INHERITED;
Brian Salomon34169692017-08-28 15:32:01 -0400214};
215
216/**
217 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
218 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
219 */
220class TextureOp final : public GrMeshDrawOp {
221public:
222 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400223 GrSamplerState::Filter filter, GrColor color,
Brian Salomon34169692017-08-28 15:32:01 -0400224 const SkRect srcRect, const SkRect dstRect,
225 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf,
226 bool allowSRBInputs) {
227 return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect,
228 dstRect, viewMatrix, std::move(csxf),
229 allowSRBInputs));
230 }
231
Brian Salomon336ce7b2017-09-08 08:23:58 -0400232 ~TextureOp() override {
233 if (fFinalized) {
234 auto proxies = this->proxies();
235 for (int i = 0; i < fProxyCnt; ++i) {
236 proxies[i]->completedRead();
237 }
238 if (fProxyCnt > 1) {
239 delete[] reinterpret_cast<const char*>(proxies);
240 }
241 } else {
242 SkASSERT(1 == fProxyCnt);
243 fProxy0->unref();
244 }
245 }
Brian Salomon34169692017-08-28 15:32:01 -0400246
247 const char* name() const override { return "TextureOp"; }
248
Robert Phillipsf1748f52017-09-14 14:11:24 -0400249 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400250 auto proxies = this->proxies();
251 for (int i = 0; i < fProxyCnt; ++i) {
252 func(proxies[i]);
253 }
254 }
255
Brian Salomon34169692017-08-28 15:32:01 -0400256 SkString dumpInfo() const override {
257 SkString str;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400258 str.appendf("AllowSRGBInputs: %d\n", fAllowSRGBInputs);
Brian Salomon34169692017-08-28 15:32:01 -0400259 str.appendf("# draws: %d\n", fDraws.count());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400260 auto proxies = this->proxies();
261 for (int i = 0; i < fProxyCnt; ++i) {
262 str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(),
263 static_cast<int>(this->filters()[i]));
264 }
Brian Salomon34169692017-08-28 15:32:01 -0400265 for (int i = 0; i < fDraws.count(); ++i) {
266 const Draw& draw = fDraws[i];
267 str.appendf(
Brian Salomon336ce7b2017-09-08 08:23:58 -0400268 "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
269 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
270 i, draw.fColor, draw.fTextureIdx, draw.fSrcRect.fLeft, draw.fSrcRect.fTop,
271 draw.fSrcRect.fRight, draw.fSrcRect.fBottom, draw.fQuad.points()[0].fX,
272 draw.fQuad.points()[0].fY, draw.fQuad.points()[1].fX, draw.fQuad.points()[1].fY,
273 draw.fQuad.points()[2].fX, draw.fQuad.points()[2].fY, draw.fQuad.points()[3].fX,
Brian Salomon34169692017-08-28 15:32:01 -0400274 draw.fQuad.points()[3].fY);
275 }
276 str += INHERITED::dumpInfo();
277 return str;
278 }
279
Brian Osman9a725dd2017-09-20 09:53:22 -0400280 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
281 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon34169692017-08-28 15:32:01 -0400282 SkASSERT(!fFinalized);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400283 SkASSERT(1 == fProxyCnt);
Brian Salomon34169692017-08-28 15:32:01 -0400284 fFinalized = true;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400285 fProxy0->addPendingRead();
286 fProxy0->unref();
Brian Salomon34169692017-08-28 15:32:01 -0400287 return RequiresDstTexture::kNo;
288 }
289
290 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
291
292 DEFINE_OP_CLASS_ID
293
294private:
Brian Salomon762d5e72017-12-01 10:25:08 -0500295
296 // This is used in a heursitic for choosing a code path. We don't care what happens with
297 // really large rects, infs, nans, etc.
298#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
299__attribute__((no_sanitize("float-cast-overflow")))
300#endif
301 size_t RectSizeAsSizeT(const SkRect &rect) {;
302 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
303 }
304
Brian Salomon336ce7b2017-09-08 08:23:58 -0400305 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
306
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400307 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomon34169692017-08-28 15:32:01 -0400308 const SkRect& srcRect, const SkRect& dstRect, const SkMatrix& viewMatrix,
309 sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs)
310 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400311 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400312 , fProxy0(proxy.release())
313 , fFilter0(filter)
314 , fProxyCnt(1)
Brian Salomon34169692017-08-28 15:32:01 -0400315 , fFinalized(false)
316 , fAllowSRGBInputs(allowSRGBInputs) {
317 Draw& draw = fDraws.push_back();
318 draw.fSrcRect = srcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400319 draw.fTextureIdx = 0;
Brian Salomon34169692017-08-28 15:32:01 -0400320 draw.fColor = color;
321 draw.fQuad.setFromMappedRect(dstRect, viewMatrix);
322 SkRect bounds;
323 bounds.setBounds(draw.fQuad.points(), 4);
324 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon762d5e72017-12-01 10:25:08 -0500325
326 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400327 }
328
329 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400330 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
331 auto proxies = this->proxies();
332 auto filters = this->filters();
333 for (int i = 0; i < fProxyCnt; ++i) {
334 if (!proxies[i]->instantiate(target->resourceProvider())) {
335 return;
336 }
337 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400338 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400339
340 sk_sp<GrGeometryProcessor> gp =
341 TextureGeometryProcessor::Make(proxiesSPs, fProxyCnt, std::move(fColorSpaceXform),
342 filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400343 GrPipeline::InitArgs args;
344 args.fProxy = target->proxy();
345 args.fCaps = &target->caps();
346 args.fResourceProvider = target->resourceProvider();
347 args.fFlags = fAllowSRGBInputs ? GrPipeline::kAllowSRGBInputs_Flag : 0;
348 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
349 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400350 int vstart;
351 const GrBuffer* vbuffer;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400352 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
353 &vstart);
354 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400355 SkDebugf("Could not allocate vertices\n");
356 return;
357 }
Brian Salomon57caa662017-10-18 12:21:05 +0000358 if (1 == fProxyCnt) {
359 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex));
360 for (int i = 0; i < fDraws.count(); ++i) {
361 auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata);
362 GrTexture* texture = proxies[0]->priv().peekTexture();
363 float iw = 1.f / texture->width();
364 float ih = 1.f / texture->height();
365 float tl = iw * fDraws[i].fSrcRect.fLeft;
366 float tr = iw * fDraws[i].fSrcRect.fRight;
367 float tt = ih * fDraws[i].fSrcRect.fTop;
368 float tb = ih * fDraws[i].fSrcRect.fBottom;
369 if (proxies[0]->origin() == kBottomLeft_GrSurfaceOrigin) {
370 tt = 1.f - tt;
371 tb = 1.f - tb;
372 }
373 vertices[0 + 4 * i].fPosition = fDraws[i].fQuad.points()[0];
374 vertices[0 + 4 * i].fTextureCoords = {tl, tt};
375 vertices[0 + 4 * i].fColor = fDraws[i].fColor;
376 vertices[1 + 4 * i].fPosition = fDraws[i].fQuad.points()[1];
377 vertices[1 + 4 * i].fTextureCoords = {tl, tb};
378 vertices[1 + 4 * i].fColor = fDraws[i].fColor;
379 vertices[2 + 4 * i].fPosition = fDraws[i].fQuad.points()[2];
380 vertices[2 + 4 * i].fTextureCoords = {tr, tt};
381 vertices[2 + 4 * i].fColor = fDraws[i].fColor;
382 vertices[3 + 4 * i].fPosition = fDraws[i].fQuad.points()[3];
383 vertices[3 + 4 * i].fTextureCoords = {tr, tb};
384 vertices[3 + 4 * i].fColor = fDraws[i].fColor;
385 }
386 } else {
387 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::MultiTextureVertex));
388 GrTexture* textures[kMaxTextures];
389 float iw[kMaxTextures];
390 float ih[kMaxTextures];
391 for (int t = 0; t < fProxyCnt; ++t) {
392 textures[t] = proxies[t]->priv().peekTexture();
393 iw[t] = 1.f / textures[t]->width();
394 ih[t] = 1.f / textures[t]->height();
395 }
396 for (int i = 0; i < fDraws.count(); ++i) {
397 int t = fDraws[i].fTextureIdx;
398 auto vertices = static_cast<TextureGeometryProcessor::MultiTextureVertex*>(vdata);
399 float tl = iw[t] * fDraws[i].fSrcRect.fLeft;
400 float tr = iw[t] * fDraws[i].fSrcRect.fRight;
401 float tt = ih[t] * fDraws[i].fSrcRect.fTop;
402 float tb = ih[t] * fDraws[i].fSrcRect.fBottom;
403 if (proxies[t]->origin() == kBottomLeft_GrSurfaceOrigin) {
404 tt = 1.f - tt;
405 tb = 1.f - tb;
406 }
407 vertices[0 + 4 * i].fPosition = fDraws[i].fQuad.points()[0];
408 vertices[0 + 4 * i].fTextureIdx = t;
409 vertices[0 + 4 * i].fTextureCoords = {tl, tt};
410 vertices[0 + 4 * i].fColor = fDraws[i].fColor;
411 vertices[1 + 4 * i].fPosition = fDraws[i].fQuad.points()[1];
412 vertices[1 + 4 * i].fTextureIdx = t;
413 vertices[1 + 4 * i].fTextureCoords = {tl, tb};
414 vertices[1 + 4 * i].fColor = fDraws[i].fColor;
415 vertices[2 + 4 * i].fPosition = fDraws[i].fQuad.points()[2];
416 vertices[2 + 4 * i].fTextureIdx = t;
417 vertices[2 + 4 * i].fTextureCoords = {tr, tt};
418 vertices[2 + 4 * i].fColor = fDraws[i].fColor;
419 vertices[3 + 4 * i].fPosition = fDraws[i].fQuad.points()[3];
420 vertices[3 + 4 * i].fTextureIdx = t;
421 vertices[3 + 4 * i].fTextureCoords = {tr, tb};
422 vertices[3 + 4 * i].fColor = fDraws[i].fColor;
423 }
424 }
425 GrPrimitiveType primitiveType =
426 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
427 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400428 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000429 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400430 if (!ibuffer) {
431 SkDebugf("Could not allocate quad indices\n");
432 return;
433 }
Brian Salomon34169692017-08-28 15:32:01 -0400434 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
435 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400436 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400437 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400438 }
Brian Salomon57caa662017-10-18 12:21:05 +0000439 mesh.setVertexData(vbuffer, vstart);
440 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400441 }
442
443 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
444 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500445 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400446 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400447 return false;
448 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400449 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
450 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500451 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
452 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
453 that->fMaxApproxDstPixelArea <=
454 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400455 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500456 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400457 if (numNewProxies < 0) {
458 return false;
459 }
460 if (1 == fProxyCnt && numNewProxies) {
461 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
462 kMaxTextures];
463 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
464 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
465 proxies[0] = fProxy0;
466 filters[0] = fFilter0;
467 fProxyArray = proxies;
468 }
469 fProxyCnt += numNewProxies;
470 auto thisProxies = fProxyArray;
471 auto thatProxies = that->proxies();
472 auto thatFilters = that->filters();
473 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
474 kMaxTextures);
475 for (int i = 0; i < that->fProxyCnt; ++i) {
476 if (map[i] < 0) {
477 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400478
Brian Salomon336ce7b2017-09-08 08:23:58 -0400479 thisProxies[-map[i]] = thatProxies[i];
480 thisFilters[-map[i]] = thatFilters[i];
481 map[i] = -map[i];
482 }
483 }
484 int firstNewDraw = fDraws.count();
485 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
486 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
487 fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx];
488 }
489 } else {
Brian Salomonbbf05752017-11-30 11:30:48 -0500490 // We can get here when one of the ops is already multitextured but the other cannot
491 // be because of the dst rect size.
492 if (fProxyCnt > 1 || that->fProxyCnt > 1) {
493 return false;
494 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400495 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
496 return false;
497 }
498 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
499 }
Brian Salomon34169692017-08-28 15:32:01 -0400500 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500501 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomon34169692017-08-28 15:32:01 -0400502 return true;
503 }
504
Brian Salomon336ce7b2017-09-08 08:23:58 -0400505 /**
506 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
507 * value means that's proxy should be added to this's proxy array at the absolute value of
508 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
509 * negative value is returned. Otherwise, return value indicates the number of proxies that have
510 * to be added to this op or, equivalently, the number of negative entries in map.
511 */
512 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
513 std::fill_n(map, kMaxTextures, -kMaxTextures);
514 int sharedProxyCnt = 0;
515 auto thisProxies = this->proxies();
516 auto thisFilters = this->filters();
517 auto thatProxies = that->proxies();
518 auto thatFilters = that->filters();
519 for (int i = 0; i < fProxyCnt; ++i) {
520 for (int j = 0; j < that->fProxyCnt; ++j) {
521 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
522 if (thisFilters[i] != thatFilters[j]) {
523 // In GL we don't currently support using the same texture with different
524 // samplers. If we added support for sampler objects and a cap bit to know
525 // it's ok to use different filter modes then we could support this.
526 // Otherwise, we could also only allow a single filter mode for each op
527 // instance.
528 return -1;
529 }
530 map[j] = i;
531 ++sharedProxyCnt;
532 break;
533 }
534 }
535 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500536 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400537 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
538 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
539 return -1;
540 }
541 GrPixelConfig config = thisProxies[0]->config();
542 int nextSlot = fProxyCnt;
543 for (int j = 0; j < that->fProxyCnt; ++j) {
544 // We want to avoid making many shaders because of different permutations of shader
545 // based swizzle and sampler types. The approach taken here is to require the configs to
546 // be the same and to only allow already instantiated proxies that have the most
547 // common sampler type. Otherwise we don't merge.
548 if (thatProxies[j]->config() != config) {
549 return -1;
550 }
551 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
552 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
553 return -1;
554 }
555 }
556 if (map[j] < 0) {
557 map[j] = -(nextSlot++);
558 }
559 }
560 return newProxyCnt;
561 }
562
563 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
564
565 const GrSamplerState::Filter* filters() const {
566 if (fProxyCnt > 1) {
567 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
568 }
569 return &fFilter0;
570 }
571
Brian Salomon34169692017-08-28 15:32:01 -0400572 struct Draw {
573 SkRect fSrcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400574 int fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400575 GrQuad fQuad;
576 GrColor fColor;
577 };
578 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -0400579 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400580 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
581 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
582 // followed by kMaxTextures filters.
583 union {
584 GrTextureProxy* fProxy0;
585 GrTextureProxy** fProxyArray;
586 };
Brian Salomonbbf05752017-11-30 11:30:48 -0500587 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400588 // The next four members should pack.
589 GrSamplerState::Filter fFilter0;
590 uint8_t fProxyCnt;
Brian Salomon34169692017-08-28 15:32:01 -0400591 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomon336ce7b2017-09-08 08:23:58 -0400592 uint8_t fFinalized;
593 uint8_t fAllowSRGBInputs;
594
Brian Salomon34169692017-08-28 15:32:01 -0400595 typedef GrMeshDrawOp INHERITED;
596};
597
Brian Salomon336ce7b2017-09-08 08:23:58 -0400598constexpr int TextureGeometryProcessor::kMaxTextures;
599constexpr int TextureOp::kMaxTextures;
600
Brian Salomon34169692017-08-28 15:32:01 -0400601} // anonymous namespace
602
603namespace GrTextureOp {
604
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400605std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter,
Brian Salomon34169692017-08-28 15:32:01 -0400606 GrColor color, const SkRect& srcRect, const SkRect& dstRect,
607 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf,
608 bool allowSRGBInputs) {
609 SkASSERT(!viewMatrix.hasPerspective());
610 return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, viewMatrix,
611 std::move(csxf), allowSRGBInputs);
612}
613
614} // namespace GrTextureOp
615
616#if GR_TEST_UTILS
617#include "GrContext.h"
618
619GR_DRAW_OP_TEST_DEFINE(TextureOp) {
620 GrSurfaceDesc desc;
621 desc.fConfig = kRGBA_8888_GrPixelConfig;
622 desc.fHeight = random->nextULessThan(90) + 10;
623 desc.fWidth = random->nextULessThan(90) + 10;
624 desc.fOrigin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
625 SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
626 auto proxy =
627 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, fit, SkBudgeted::kNo);
628 SkRect rect = GrTest::TestRect(random);
629 SkRect srcRect;
630 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
631 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
632 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
633 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
634 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
635 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400636 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
637 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Brian Salomon34169692017-08-28 15:32:01 -0400638 auto csxf = GrTest::TestColorXform(random);
639 bool allowSRGBInputs = random->nextBool();
640 return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, viewMatrix,
641 std::move(csxf), allowSRGBInputs);
642}
643
644#endif