blob: be37bd303d5b564b67b6f9468cee7c7bbe8d33db [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,
110 gpArgs->fPositionVar,
111 textureGP.fTextureCoords.fName,
112 args.fFPCoordTransformHandler);
Brian Salomon41274562017-09-15 09:40:03 -0700113 if (args.fShaderCaps->preferFlatInterpolation()) {
Brian Salomon34169692017-08-28 15:32:01 -0400114 args.fVaryingHandler->addFlatPassThroughAttribute(&textureGP.fColors,
115 args.fOutputColor);
116 } else {
117 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fColors,
118 args.fOutputColor);
119 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400120 args.fFragBuilder->codeAppend("float2 texCoord;");
Chris Daltonfdde34e2017-10-16 14:15:26 -0600121 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureCoords,
122 "texCoord");
Brian Salomon336ce7b2017-09-08 08:23:58 -0400123 if (textureGP.numTextureSamplers() > 1) {
124 SkASSERT(args.fShaderCaps->integerSupport());
125 args.fFragBuilder->codeAppend("int texIdx;");
126 if (args.fShaderCaps->flatInterpolationSupport()) {
127 args.fVaryingHandler->addFlatPassThroughAttribute(&textureGP.fTextureIdx,
128 "texIdx");
129 } else {
130 args.fVaryingHandler->addPassThroughAttribute(&textureGP.fTextureIdx,
131 "texIdx");
132 }
133 args.fFragBuilder->codeAppend("switch (texIdx) {");
134 for (int i = 0; i < textureGP.numTextureSamplers(); ++i) {
135 args.fFragBuilder->codeAppendf("case %d: %s = ", i, args.fOutputColor);
136 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
137 args.fTexSamplers[i],
138 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400139 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400140 &fColorSpaceXformHelper);
141 args.fFragBuilder->codeAppend("; break;");
142 }
143 args.fFragBuilder->codeAppend("}");
144 } else {
145 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
146 args.fFragBuilder->appendTextureLookupAndModulate(args.fOutputColor,
147 args.fTexSamplers[0],
148 "texCoord",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400149 kFloat2_GrSLType,
Brian Salomon336ce7b2017-09-08 08:23:58 -0400150 &fColorSpaceXformHelper);
151 }
Brian Salomon34169692017-08-28 15:32:01 -0400152 args.fFragBuilder->codeAppend(";");
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400153 args.fFragBuilder->codeAppendf("%s = float4(1);", args.fOutputCoverage);
Brian Salomon34169692017-08-28 15:32:01 -0400154 }
155 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
156 };
157 return new GLSLProcessor;
158 }
159
160private:
Brian Salomon336ce7b2017-09-08 08:23:58 -0400161 // This exists to reduce the number of shaders generated. It does some rounding of sampler
162 // counts.
163 static int NumSamplersToUse(int numRealProxies, const GrShaderCaps& caps) {
164 SkASSERT(numRealProxies > 0 && numRealProxies <= kMaxTextures &&
165 numRealProxies <= caps.maxFragmentSamplers());
166 if (1 == numRealProxies) {
167 return 1;
168 }
169 if (numRealProxies <= 4) {
170 return 4;
171 }
172 // Round to the next power of 2 and then clamp to kMaxTextures and the max allowed by caps.
173 return SkTMin(SkNextPow2(numRealProxies), SkTMin(kMaxTextures, caps.maxFragmentSamplers()));
174 }
175
176 TextureGeometryProcessor(sk_sp<GrTextureProxy> proxies[], int proxyCnt, int samplerCnt,
177 sk_sp<GrColorSpaceXform> csxf, const GrSamplerState::Filter filters[],
178 const GrShaderCaps& caps)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400179 : INHERITED(kTextureGeometryProcessor_ClassID)
180 , fColorSpaceXform(std::move(csxf)) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400181 SkASSERT(proxyCnt > 0 && samplerCnt >= proxyCnt);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400182 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400183 fSamplers[0].reset(std::move(proxies[0]), filters[0]);
184 this->addTextureSampler(&fSamplers[0]);
185 for (int i = 1; i < proxyCnt; ++i) {
186 // This class has one sampler built in, the rest come from memory this processor was
187 // placement-newed into and so haven't been constructed.
188 new (&fSamplers[i]) TextureSampler(std::move(proxies[i]), filters[i]);
189 this->addTextureSampler(&fSamplers[i]);
190 }
191 if (samplerCnt > 1) {
192 // Here we initialize any extra samplers by repeating the last one samplerCnt - proxyCnt
193 // times.
194 GrTextureProxy* dupeProxy = fSamplers[proxyCnt - 1].proxy();
195 for (int i = proxyCnt; i < samplerCnt; ++i) {
196 new (&fSamplers[i]) TextureSampler(sk_ref_sp(dupeProxy), filters[proxyCnt - 1]);
197 this->addTextureSampler(&fSamplers[i]);
198 }
199 SkASSERT(caps.integerSupport());
200 fTextureIdx = this->addVertexAttrib("textureIdx", kInt_GrVertexAttribType);
201 }
202
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400203 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
204 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Brian Salomon34169692017-08-28 15:32:01 -0400205 }
206
207 Attribute fPositions;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400208 Attribute fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400209 Attribute fTextureCoords;
210 Attribute fColors;
Brian Salomon34169692017-08-28 15:32:01 -0400211 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400212 TextureSampler fSamplers[1];
Ethan Nicholasabff9562017-10-09 10:54:08 -0400213
214 typedef GrGeometryProcessor INHERITED;
Brian Salomon34169692017-08-28 15:32:01 -0400215};
216
217/**
218 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
219 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
220 */
221class TextureOp final : public GrMeshDrawOp {
222public:
223 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400224 GrSamplerState::Filter filter, GrColor color,
Brian Salomon34169692017-08-28 15:32:01 -0400225 const SkRect srcRect, const SkRect dstRect,
226 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf,
227 bool allowSRBInputs) {
228 return std::unique_ptr<GrDrawOp>(new TextureOp(std::move(proxy), filter, color, srcRect,
229 dstRect, viewMatrix, std::move(csxf),
230 allowSRBInputs));
231 }
232
Brian Salomon336ce7b2017-09-08 08:23:58 -0400233 ~TextureOp() override {
234 if (fFinalized) {
235 auto proxies = this->proxies();
236 for (int i = 0; i < fProxyCnt; ++i) {
237 proxies[i]->completedRead();
238 }
239 if (fProxyCnt > 1) {
240 delete[] reinterpret_cast<const char*>(proxies);
241 }
242 } else {
243 SkASSERT(1 == fProxyCnt);
244 fProxy0->unref();
245 }
246 }
Brian Salomon34169692017-08-28 15:32:01 -0400247
248 const char* name() const override { return "TextureOp"; }
249
Robert Phillipsf1748f52017-09-14 14:11:24 -0400250 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400251 auto proxies = this->proxies();
252 for (int i = 0; i < fProxyCnt; ++i) {
253 func(proxies[i]);
254 }
255 }
256
Brian Salomon34169692017-08-28 15:32:01 -0400257 SkString dumpInfo() const override {
258 SkString str;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400259 str.appendf("AllowSRGBInputs: %d\n", fAllowSRGBInputs);
Brian Salomon34169692017-08-28 15:32:01 -0400260 str.appendf("# draws: %d\n", fDraws.count());
Brian Salomon336ce7b2017-09-08 08:23:58 -0400261 auto proxies = this->proxies();
262 for (int i = 0; i < fProxyCnt; ++i) {
263 str.appendf("Proxy ID %d: %d, Filter: %d\n", i, proxies[i]->uniqueID().asUInt(),
264 static_cast<int>(this->filters()[i]));
265 }
Brian Salomon34169692017-08-28 15:32:01 -0400266 for (int i = 0; i < fDraws.count(); ++i) {
267 const Draw& draw = fDraws[i];
268 str.appendf(
Brian Salomon336ce7b2017-09-08 08:23:58 -0400269 "%d: Color: 0x%08x, ProxyIdx: %d, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
270 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
271 i, draw.fColor, draw.fTextureIdx, draw.fSrcRect.fLeft, draw.fSrcRect.fTop,
272 draw.fSrcRect.fRight, draw.fSrcRect.fBottom, draw.fQuad.points()[0].fX,
273 draw.fQuad.points()[0].fY, draw.fQuad.points()[1].fX, draw.fQuad.points()[1].fY,
274 draw.fQuad.points()[2].fX, draw.fQuad.points()[2].fY, draw.fQuad.points()[3].fX,
Brian Salomon34169692017-08-28 15:32:01 -0400275 draw.fQuad.points()[3].fY);
276 }
277 str += INHERITED::dumpInfo();
278 return str;
279 }
280
Brian Osman9a725dd2017-09-20 09:53:22 -0400281 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
282 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon34169692017-08-28 15:32:01 -0400283 SkASSERT(!fFinalized);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400284 SkASSERT(1 == fProxyCnt);
Brian Salomon34169692017-08-28 15:32:01 -0400285 fFinalized = true;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400286 fProxy0->addPendingRead();
287 fProxy0->unref();
Brian Salomon34169692017-08-28 15:32:01 -0400288 return RequiresDstTexture::kNo;
289 }
290
291 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
292
293 DEFINE_OP_CLASS_ID
294
295private:
Brian Salomon762d5e72017-12-01 10:25:08 -0500296
297 // This is used in a heursitic for choosing a code path. We don't care what happens with
298 // really large rects, infs, nans, etc.
299#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
300__attribute__((no_sanitize("float-cast-overflow")))
301#endif
302 size_t RectSizeAsSizeT(const SkRect &rect) {;
303 return static_cast<size_t>(SkTMax(rect.width(), 1.f) * SkTMax(rect.height(), 1.f));
304 }
305
Brian Salomon336ce7b2017-09-08 08:23:58 -0400306 static constexpr int kMaxTextures = TextureGeometryProcessor::kMaxTextures;
307
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400308 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, GrColor color,
Brian Salomon34169692017-08-28 15:32:01 -0400309 const SkRect& srcRect, const SkRect& dstRect, const SkMatrix& viewMatrix,
310 sk_sp<GrColorSpaceXform> csxf, bool allowSRGBInputs)
311 : INHERITED(ClassID())
Brian Salomon34169692017-08-28 15:32:01 -0400312 , fColorSpaceXform(std::move(csxf))
Brian Salomon336ce7b2017-09-08 08:23:58 -0400313 , fProxy0(proxy.release())
314 , fFilter0(filter)
315 , fProxyCnt(1)
Brian Salomon34169692017-08-28 15:32:01 -0400316 , fFinalized(false)
317 , fAllowSRGBInputs(allowSRGBInputs) {
318 Draw& draw = fDraws.push_back();
319 draw.fSrcRect = srcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400320 draw.fTextureIdx = 0;
Brian Salomon34169692017-08-28 15:32:01 -0400321 draw.fColor = color;
322 draw.fQuad.setFromMappedRect(dstRect, viewMatrix);
323 SkRect bounds;
324 bounds.setBounds(draw.fQuad.points(), 4);
325 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
Brian Salomon762d5e72017-12-01 10:25:08 -0500326
327 fMaxApproxDstPixelArea = RectSizeAsSizeT(bounds);
Brian Salomon34169692017-08-28 15:32:01 -0400328 }
329
330 void onPrepareDraws(Target* target) override {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400331 sk_sp<GrTextureProxy> proxiesSPs[kMaxTextures];
332 auto proxies = this->proxies();
333 auto filters = this->filters();
334 for (int i = 0; i < fProxyCnt; ++i) {
335 if (!proxies[i]->instantiate(target->resourceProvider())) {
336 return;
337 }
338 proxiesSPs[i] = sk_ref_sp(proxies[i]);
Brian Salomon34169692017-08-28 15:32:01 -0400339 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400340
341 sk_sp<GrGeometryProcessor> gp =
342 TextureGeometryProcessor::Make(proxiesSPs, fProxyCnt, std::move(fColorSpaceXform),
343 filters, *target->caps().shaderCaps());
Brian Salomon34169692017-08-28 15:32:01 -0400344 GrPipeline::InitArgs args;
345 args.fProxy = target->proxy();
346 args.fCaps = &target->caps();
347 args.fResourceProvider = target->resourceProvider();
348 args.fFlags = fAllowSRGBInputs ? GrPipeline::kAllowSRGBInputs_Flag : 0;
349 const GrPipeline* pipeline = target->allocPipeline(args, GrProcessorSet::MakeEmptySet(),
350 target->detachAppliedClip());
Brian Salomon34169692017-08-28 15:32:01 -0400351 int vstart;
352 const GrBuffer* vbuffer;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400353 void* vdata = target->makeVertexSpace(gp->getVertexStride(), 4 * fDraws.count(), &vbuffer,
354 &vstart);
355 if (!vdata) {
Brian Salomon34169692017-08-28 15:32:01 -0400356 SkDebugf("Could not allocate vertices\n");
357 return;
358 }
Brian Salomon57caa662017-10-18 12:21:05 +0000359 if (1 == fProxyCnt) {
360 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::Vertex));
361 for (int i = 0; i < fDraws.count(); ++i) {
362 auto vertices = static_cast<TextureGeometryProcessor::Vertex*>(vdata);
363 GrTexture* texture = proxies[0]->priv().peekTexture();
364 float iw = 1.f / texture->width();
365 float ih = 1.f / texture->height();
366 float tl = iw * fDraws[i].fSrcRect.fLeft;
367 float tr = iw * fDraws[i].fSrcRect.fRight;
368 float tt = ih * fDraws[i].fSrcRect.fTop;
369 float tb = ih * fDraws[i].fSrcRect.fBottom;
370 if (proxies[0]->origin() == kBottomLeft_GrSurfaceOrigin) {
371 tt = 1.f - tt;
372 tb = 1.f - tb;
373 }
374 vertices[0 + 4 * i].fPosition = fDraws[i].fQuad.points()[0];
375 vertices[0 + 4 * i].fTextureCoords = {tl, tt};
376 vertices[0 + 4 * i].fColor = fDraws[i].fColor;
377 vertices[1 + 4 * i].fPosition = fDraws[i].fQuad.points()[1];
378 vertices[1 + 4 * i].fTextureCoords = {tl, tb};
379 vertices[1 + 4 * i].fColor = fDraws[i].fColor;
380 vertices[2 + 4 * i].fPosition = fDraws[i].fQuad.points()[2];
381 vertices[2 + 4 * i].fTextureCoords = {tr, tt};
382 vertices[2 + 4 * i].fColor = fDraws[i].fColor;
383 vertices[3 + 4 * i].fPosition = fDraws[i].fQuad.points()[3];
384 vertices[3 + 4 * i].fTextureCoords = {tr, tb};
385 vertices[3 + 4 * i].fColor = fDraws[i].fColor;
386 }
387 } else {
388 SkASSERT(gp->getVertexStride() == sizeof(TextureGeometryProcessor::MultiTextureVertex));
389 GrTexture* textures[kMaxTextures];
390 float iw[kMaxTextures];
391 float ih[kMaxTextures];
392 for (int t = 0; t < fProxyCnt; ++t) {
393 textures[t] = proxies[t]->priv().peekTexture();
394 iw[t] = 1.f / textures[t]->width();
395 ih[t] = 1.f / textures[t]->height();
396 }
397 for (int i = 0; i < fDraws.count(); ++i) {
398 int t = fDraws[i].fTextureIdx;
399 auto vertices = static_cast<TextureGeometryProcessor::MultiTextureVertex*>(vdata);
400 float tl = iw[t] * fDraws[i].fSrcRect.fLeft;
401 float tr = iw[t] * fDraws[i].fSrcRect.fRight;
402 float tt = ih[t] * fDraws[i].fSrcRect.fTop;
403 float tb = ih[t] * fDraws[i].fSrcRect.fBottom;
404 if (proxies[t]->origin() == kBottomLeft_GrSurfaceOrigin) {
405 tt = 1.f - tt;
406 tb = 1.f - tb;
407 }
408 vertices[0 + 4 * i].fPosition = fDraws[i].fQuad.points()[0];
409 vertices[0 + 4 * i].fTextureIdx = t;
410 vertices[0 + 4 * i].fTextureCoords = {tl, tt};
411 vertices[0 + 4 * i].fColor = fDraws[i].fColor;
412 vertices[1 + 4 * i].fPosition = fDraws[i].fQuad.points()[1];
413 vertices[1 + 4 * i].fTextureIdx = t;
414 vertices[1 + 4 * i].fTextureCoords = {tl, tb};
415 vertices[1 + 4 * i].fColor = fDraws[i].fColor;
416 vertices[2 + 4 * i].fPosition = fDraws[i].fQuad.points()[2];
417 vertices[2 + 4 * i].fTextureIdx = t;
418 vertices[2 + 4 * i].fTextureCoords = {tr, tt};
419 vertices[2 + 4 * i].fColor = fDraws[i].fColor;
420 vertices[3 + 4 * i].fPosition = fDraws[i].fQuad.points()[3];
421 vertices[3 + 4 * i].fTextureIdx = t;
422 vertices[3 + 4 * i].fTextureCoords = {tr, tb};
423 vertices[3 + 4 * i].fColor = fDraws[i].fColor;
424 }
425 }
426 GrPrimitiveType primitiveType =
427 fDraws.count() > 1 ? GrPrimitiveType::kTriangles : GrPrimitiveType::kTriangleStrip;
428 GrMesh mesh(primitiveType);
Brian Salomon34169692017-08-28 15:32:01 -0400429 if (fDraws.count() > 1) {
Brian Salomon57caa662017-10-18 12:21:05 +0000430 sk_sp<const GrBuffer> ibuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon34169692017-08-28 15:32:01 -0400431 if (!ibuffer) {
432 SkDebugf("Could not allocate quad indices\n");
433 return;
434 }
Brian Salomon34169692017-08-28 15:32:01 -0400435 mesh.setIndexedPatterned(ibuffer.get(), 6, 4, fDraws.count(),
436 GrResourceProvider::QuadCountOfQuadBuffer());
Brian Salomon34169692017-08-28 15:32:01 -0400437 } else {
Brian Salomon34169692017-08-28 15:32:01 -0400438 mesh.setNonIndexedNonInstanced(4);
Brian Salomon34169692017-08-28 15:32:01 -0400439 }
Brian Salomon57caa662017-10-18 12:21:05 +0000440 mesh.setVertexData(vbuffer, vstart);
441 target->draw(gp.get(), pipeline, mesh);
Brian Salomon34169692017-08-28 15:32:01 -0400442 }
443
444 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
445 const auto* that = t->cast<TextureOp>();
Brian Salomon762d5e72017-12-01 10:25:08 -0500446 const auto& shaderCaps = *caps.shaderCaps();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400447 if (!GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon34169692017-08-28 15:32:01 -0400448 return false;
449 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400450 // Because of an issue where GrColorSpaceXform adds the same function every time it is used
451 // in a texture lookup, we only allow multiple textures when there is no transform.
Brian Salomon762d5e72017-12-01 10:25:08 -0500452 if (TextureGeometryProcessor::SupportsMultitexture(shaderCaps) && !fColorSpaceXform &&
453 fMaxApproxDstPixelArea <= shaderCaps.disableImageMultitexturingDstRectAreaThreshold() &&
454 that->fMaxApproxDstPixelArea <=
455 shaderCaps.disableImageMultitexturingDstRectAreaThreshold()) {
Brian Salomon336ce7b2017-09-08 08:23:58 -0400456 int map[kMaxTextures];
Brian Salomon762d5e72017-12-01 10:25:08 -0500457 int numNewProxies = this->mergeProxies(that, map, shaderCaps);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400458 if (numNewProxies < 0) {
459 return false;
460 }
461 if (1 == fProxyCnt && numNewProxies) {
462 void* mem = new char[(sizeof(GrSamplerState::Filter) + sizeof(GrTextureProxy*)) *
463 kMaxTextures];
464 auto proxies = reinterpret_cast<GrTextureProxy**>(mem);
465 auto filters = reinterpret_cast<GrSamplerState::Filter*>(proxies + kMaxTextures);
466 proxies[0] = fProxy0;
467 filters[0] = fFilter0;
468 fProxyArray = proxies;
469 }
470 fProxyCnt += numNewProxies;
471 auto thisProxies = fProxyArray;
472 auto thatProxies = that->proxies();
473 auto thatFilters = that->filters();
474 auto thisFilters = reinterpret_cast<GrSamplerState::Filter*>(thisProxies +
475 kMaxTextures);
476 for (int i = 0; i < that->fProxyCnt; ++i) {
477 if (map[i] < 0) {
478 thatProxies[i]->addPendingRead();
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400479
Brian Salomon336ce7b2017-09-08 08:23:58 -0400480 thisProxies[-map[i]] = thatProxies[i];
481 thisFilters[-map[i]] = thatFilters[i];
482 map[i] = -map[i];
483 }
484 }
485 int firstNewDraw = fDraws.count();
486 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
487 for (int i = firstNewDraw; i < fDraws.count(); ++i) {
488 fDraws[i].fTextureIdx = map[fDraws[i].fTextureIdx];
489 }
490 } else {
491 if (fProxy0->uniqueID() != that->fProxy0->uniqueID() || fFilter0 != that->fFilter0) {
492 return false;
493 }
494 fDraws.push_back_n(that->fDraws.count(), that->fDraws.begin());
495 }
Brian Salomon34169692017-08-28 15:32:01 -0400496 this->joinBounds(*that);
Brian Salomon762d5e72017-12-01 10:25:08 -0500497 fMaxApproxDstPixelArea = SkTMax(that->fMaxApproxDstPixelArea, fMaxApproxDstPixelArea);
Brian Salomon34169692017-08-28 15:32:01 -0400498 return true;
499 }
500
Brian Salomon336ce7b2017-09-08 08:23:58 -0400501 /**
502 * Determines a mapping of indices from that's proxy array to this's proxy array. A negative map
503 * value means that's proxy should be added to this's proxy array at the absolute value of
504 * the map entry. If it is determined that the ops shouldn't combine their proxies then a
505 * negative value is returned. Otherwise, return value indicates the number of proxies that have
506 * to be added to this op or, equivalently, the number of negative entries in map.
507 */
508 int mergeProxies(const TextureOp* that, int map[kMaxTextures], const GrShaderCaps& caps) const {
509 std::fill_n(map, kMaxTextures, -kMaxTextures);
510 int sharedProxyCnt = 0;
511 auto thisProxies = this->proxies();
512 auto thisFilters = this->filters();
513 auto thatProxies = that->proxies();
514 auto thatFilters = that->filters();
515 for (int i = 0; i < fProxyCnt; ++i) {
516 for (int j = 0; j < that->fProxyCnt; ++j) {
517 if (thisProxies[i]->uniqueID() == thatProxies[j]->uniqueID()) {
518 if (thisFilters[i] != thatFilters[j]) {
519 // In GL we don't currently support using the same texture with different
520 // samplers. If we added support for sampler objects and a cap bit to know
521 // it's ok to use different filter modes then we could support this.
522 // Otherwise, we could also only allow a single filter mode for each op
523 // instance.
524 return -1;
525 }
526 map[j] = i;
527 ++sharedProxyCnt;
528 break;
529 }
530 }
531 }
Brian Salomon2b6f6142017-11-13 11:49:13 -0500532 int actualMaxTextures = SkTMin(caps.maxFragmentSamplers(), kMaxTextures);
Brian Salomon336ce7b2017-09-08 08:23:58 -0400533 int newProxyCnt = that->fProxyCnt - sharedProxyCnt;
534 if (newProxyCnt + fProxyCnt > actualMaxTextures) {
535 return -1;
536 }
537 GrPixelConfig config = thisProxies[0]->config();
538 int nextSlot = fProxyCnt;
539 for (int j = 0; j < that->fProxyCnt; ++j) {
540 // We want to avoid making many shaders because of different permutations of shader
541 // based swizzle and sampler types. The approach taken here is to require the configs to
542 // be the same and to only allow already instantiated proxies that have the most
543 // common sampler type. Otherwise we don't merge.
544 if (thatProxies[j]->config() != config) {
545 return -1;
546 }
547 if (GrTexture* tex = thatProxies[j]->priv().peekTexture()) {
548 if (tex->texturePriv().samplerType() != kTexture2DSampler_GrSLType) {
549 return -1;
550 }
551 }
552 if (map[j] < 0) {
553 map[j] = -(nextSlot++);
554 }
555 }
556 return newProxyCnt;
557 }
558
559 GrTextureProxy* const* proxies() const { return fProxyCnt > 1 ? fProxyArray : &fProxy0; }
560
561 const GrSamplerState::Filter* filters() const {
562 if (fProxyCnt > 1) {
563 return reinterpret_cast<const GrSamplerState::Filter*>(fProxyArray + kMaxTextures);
564 }
565 return &fFilter0;
566 }
567
Brian Salomon34169692017-08-28 15:32:01 -0400568 struct Draw {
569 SkRect fSrcRect;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400570 int fTextureIdx;
Brian Salomon34169692017-08-28 15:32:01 -0400571 GrQuad fQuad;
572 GrColor fColor;
573 };
574 SkSTArray<1, Draw, true> fDraws;
Brian Salomon34169692017-08-28 15:32:01 -0400575 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400576 // Initially we store a single proxy ptr and a single filter. If we grow to have more than
577 // one proxy we instead store pointers to dynamically allocated arrays of size kMaxTextures
578 // followed by kMaxTextures filters.
579 union {
580 GrTextureProxy* fProxy0;
581 GrTextureProxy** fProxyArray;
582 };
583 // The next four members should pack.
584 GrSamplerState::Filter fFilter0;
585 uint8_t fProxyCnt;
Brian Salomon34169692017-08-28 15:32:01 -0400586 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomon336ce7b2017-09-08 08:23:58 -0400587 uint8_t fFinalized;
588 uint8_t fAllowSRGBInputs;
Brian Salomon762d5e72017-12-01 10:25:08 -0500589 size_t fMaxApproxDstPixelArea;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400590
Brian Salomon34169692017-08-28 15:32:01 -0400591 typedef GrMeshDrawOp INHERITED;
592};
593
Brian Salomon336ce7b2017-09-08 08:23:58 -0400594constexpr int TextureGeometryProcessor::kMaxTextures;
595constexpr int TextureOp::kMaxTextures;
596
Brian Salomon34169692017-08-28 15:32:01 -0400597} // anonymous namespace
598
599namespace GrTextureOp {
600
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400601std::unique_ptr<GrDrawOp> Make(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter,
Brian Salomon34169692017-08-28 15:32:01 -0400602 GrColor color, const SkRect& srcRect, const SkRect& dstRect,
603 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> csxf,
604 bool allowSRGBInputs) {
605 SkASSERT(!viewMatrix.hasPerspective());
606 return TextureOp::Make(std::move(proxy), filter, color, srcRect, dstRect, viewMatrix,
607 std::move(csxf), allowSRGBInputs);
608}
609
610} // namespace GrTextureOp
611
612#if GR_TEST_UTILS
613#include "GrContext.h"
614
615GR_DRAW_OP_TEST_DEFINE(TextureOp) {
616 GrSurfaceDesc desc;
617 desc.fConfig = kRGBA_8888_GrPixelConfig;
618 desc.fHeight = random->nextULessThan(90) + 10;
619 desc.fWidth = random->nextULessThan(90) + 10;
620 desc.fOrigin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
621 SkBackingFit fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
622 auto proxy =
623 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, fit, SkBudgeted::kNo);
624 SkRect rect = GrTest::TestRect(random);
625 SkRect srcRect;
626 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
627 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
628 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
629 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
630 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
631 GrColor color = SkColorToPremulGrColor(random->nextU());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400632 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
633 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Brian Salomon34169692017-08-28 15:32:01 -0400634 auto csxf = GrTest::TestColorXform(random);
635 bool allowSRGBInputs = random->nextBool();
636 return GrTextureOp::Make(std::move(proxy), filter, color, srcRect, rect, viewMatrix,
637 std::move(csxf), allowSRGBInputs);
638}
639
640#endif