blob: 3cbefeae98804bc7e0b28386550415a76cbae543 [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
Brian Salomond7065e72018-10-12 11:42:02 -04008#include <new>
Brian Salomonf19f9ca2019-09-18 15:54:26 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPoint.h"
11#include "include/core/SkPoint3.h"
12#include "include/gpu/GrTexture.h"
13#include "include/private/GrRecordingContext.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040014#include "include/private/SkFloatingPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/private/SkTo.h"
16#include "src/core/SkMathPriv.h"
17#include "src/core/SkMatrixPriv.h"
18#include "src/core/SkRectPriv.h"
19#include "src/gpu/GrAppliedClip.h"
20#include "src/gpu/GrCaps.h"
21#include "src/gpu/GrDrawOpTest.h"
22#include "src/gpu/GrGeometryProcessor.h"
23#include "src/gpu/GrGpu.h"
24#include "src/gpu/GrMemoryPool.h"
25#include "src/gpu/GrOpFlushState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrRecordingContextPriv.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrResourceProviderPriv.h"
29#include "src/gpu/GrShaderCaps.h"
30#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/SkGr.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040033#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040034#include "src/gpu/effects/generated/GrSaturateProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040035#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040036#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040037#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/glsl/GrGLSLVarying.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040039#include "src/gpu/ops/GrFillRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/ops/GrMeshDrawOp.h"
41#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040042#include "src/gpu/ops/GrTextureOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040043
44namespace {
45
Michael Ludwig460eb5e2018-10-29 11:09:29 -040046using Domain = GrQuadPerEdgeAA::Domain;
Michael Ludwigc182b942018-11-16 10:27:51 -050047using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050048using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040049
Michael Ludwig22429f92019-06-27 10:44:48 -040050// Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
51// between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
52static SkSize axis_aligned_quad_size(const GrQuad& quad) {
53 SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
54 // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
55 float dw = sk_float_abs(quad.x(2) - quad.x(0)) + sk_float_abs(quad.y(2) - quad.y(0));
56 float dh = sk_float_abs(quad.x(1) - quad.x(0)) + sk_float_abs(quad.y(1) - quad.y(0));
57 return {dw, dh};
58}
59
60static bool filter_has_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
61 // If not axis-aligned in src or dst, then always say it has an effect
62 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
63 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
64 return true;
65 }
66
67 SkRect srcRect;
68 SkRect dstRect;
69 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
70 // Disable filtering when there is no scaling (width and height are the same), and the
71 // top-left corners have the same fraction (so src and dst snap to the pixel grid
72 // identically).
73 SkASSERT(srcRect.isSorted());
74 return srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
75 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
76 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
77 } else {
78 // Although the quads are axis-aligned, the local coordinate system is transformed such
79 // that fractionally-aligned sample centers will not align with the device coordinate system
80 // So disable filtering when edges are the same length and both srcQuad and dstQuad
81 // 0th vertex is integer aligned.
82 if (SkScalarIsInt(srcQuad.x(0)) && SkScalarIsInt(srcQuad.y(0)) &&
83 SkScalarIsInt(dstQuad.x(0)) && SkScalarIsInt(dstQuad.y(0))) {
84 // Extract edge lengths
85 SkSize srcSize = axis_aligned_quad_size(srcQuad);
86 SkSize dstSize = axis_aligned_quad_size(dstQuad);
87 return srcSize.fWidth != dstSize.fWidth || srcSize.fHeight != dstSize.fHeight;
88 } else {
89 return true;
90 }
91 }
92}
93
Brian Salomon246bc3d2018-12-06 15:33:02 -050094// if normalizing the domain then pass 1/width, 1/height, 1 for iw, ih, h. Otherwise pass
95// 1, 1, and height.
Michael Ludwigf339dfe2019-06-27 10:41:28 -040096static void compute_domain(Domain domain, GrSamplerState::Filter filter, GrSurfaceOrigin origin,
97 const SkRect& domainRect, float iw, float ih, float h, SkRect* out) {
Brian Salomon246bc3d2018-12-06 15:33:02 -050098 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Michael Ludwig460eb5e2018-10-29 11:09:29 -040099 if (domain == Domain::kNo) {
100 // Either the quad has no domain constraint and is batched with a domain constrained op
101 // (in which case we want a domain that doesn't restrict normalized tex coords), or the
102 // entire op doesn't use the domain, in which case the returned value is ignored.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400103 *out = kLargeRect;
104 return;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400105 }
106
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400107 auto ltrb = Sk4f::Load(&domainRect);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400108 if (filter == GrSamplerState::Filter::kBilerp) {
109 auto rblt = SkNx_shuffle<2, 3, 0, 1>(ltrb);
110 auto whwh = (rblt - ltrb).abs();
111 auto c = (rblt + ltrb) * 0.5f;
112 static const Sk4f kOffsets = {0.5f, 0.5f, -0.5f, -0.5f};
113 ltrb = (whwh < 1.f).thenElse(c, ltrb + kOffsets);
114 }
115 ltrb *= Sk4f(iw, ih, iw, ih);
116 if (origin == kBottomLeft_GrSurfaceOrigin) {
117 static const Sk4f kMul = {1.f, -1.f, 1.f, -1.f};
Brian Salomon246bc3d2018-12-06 15:33:02 -0500118 const Sk4f kAdd = {0.f, h, 0.f, h};
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400119 ltrb = SkNx_shuffle<0, 3, 2, 1>(kMul * ltrb + kAdd);
120 }
121
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400122 ltrb.store(out);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400123}
124
Michael Ludwig009b92e2019-02-15 16:03:53 -0500125// Normalizes logical src coords and corrects for origin
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400126static void compute_src_quad(GrSurfaceOrigin origin, const GrQuad& srcQuad,
127 float iw, float ih, float h, GrQuad* out) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500128 // The src quad should not have any perspective
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400129 SkASSERT(!srcQuad.hasPerspective() && !out->hasPerspective());
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400130 skvx::Vec<4, float> xs = srcQuad.x4f() * iw;
131 skvx::Vec<4, float> ys = srcQuad.y4f() * ih;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500132 if (origin == kBottomLeft_GrSurfaceOrigin) {
133 ys = h - ys;
134 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400135 xs.store(out->xs());
136 ys.store(out->ys());
137 out->setQuadType(srcQuad.quadType());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500138}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400139
Brian Salomon34169692017-08-28 15:32:01 -0400140/**
141 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
142 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
143 */
144class TextureOp final : public GrMeshDrawOp {
145public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500146 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400147 sk_sp<GrTextureProxy> proxy,
Michael Ludwig22429f92019-06-27 10:44:48 -0400148 sk_sp<GrColorSpaceXform> textureXform,
Robert Phillips7c525e62018-06-12 10:11:12 -0400149 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500150 const SkPMColor4f& color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400151 GrTextureOp::Saturate saturate,
Robert Phillips7c525e62018-06-12 10:11:12 -0400152 GrAAType aaType,
Brian Salomon2213ee92018-10-02 10:44:21 -0400153 GrQuadAAFlags aaFlags,
Michael Ludwig22429f92019-06-27 10:44:48 -0400154 const GrQuad& deviceQuad,
155 const GrQuad& localQuad,
156 const SkRect* domain) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500157 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400158 return pool->allocate<TextureOp>(std::move(proxy), std::move(textureXform), filter, color,
159 saturate, aaType, aaFlags, deviceQuad, localQuad, domain);
Brian Salomon34169692017-08-28 15:32:01 -0400160 }
Robert Phillipsb97da532019-02-12 15:24:12 -0500161 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Brian Salomond7065e72018-10-12 11:42:02 -0400162 const GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400163 int cnt,
164 GrSamplerState::Filter filter,
165 GrTextureOp::Saturate saturate,
166 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400167 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500168 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500169 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400170 size_t size = sizeof(TextureOp) + sizeof(Proxy) * (cnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500171 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400172 void* mem = pool->allocate(size);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400173 return std::unique_ptr<GrDrawOp>(new (mem) TextureOp(set, cnt, filter, saturate, aaType,
174 constraint, viewMatrix,
175 std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400176 }
Brian Salomon34169692017-08-28 15:32:01 -0400177
Brian Salomon336ce7b2017-09-08 08:23:58 -0400178 ~TextureOp() override {
Brian Salomond7065e72018-10-12 11:42:02 -0400179 for (unsigned p = 0; p < fProxyCnt; ++p) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400180 fProxies[p].fProxy->unref();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400181 }
182 }
Brian Salomon34169692017-08-28 15:32:01 -0400183
184 const char* name() const override { return "TextureOp"; }
185
Chris Dalton1706cbf2019-05-21 19:35:29 -0600186 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomond7065e72018-10-12 11:42:02 -0400187 for (unsigned p = 0; p < fProxyCnt; ++p) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600188 bool mipped = (GrSamplerState::Filter::kMipMap == this->filter());
189 func(fProxies[p].fProxy, GrMipMapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400190 }
191 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400192
Brian Osman9a390ac2018-11-12 09:47:48 -0500193#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400194 SkString dumpInfo() const override {
195 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400196 str.appendf("# draws: %d\n", fQuads.count());
Michael Ludwig425eb452019-06-27 10:13:27 -0400197 auto iter = fQuads.iterator();
Brian Salomond7065e72018-10-12 11:42:02 -0400198 for (unsigned p = 0; p < fProxyCnt; ++p) {
199 str.appendf("Proxy ID: %d, Filter: %d\n", fProxies[p].fProxy->uniqueID().asUInt(),
200 static_cast<int>(fFilter));
Michael Ludwig425eb452019-06-27 10:13:27 -0400201 int i = 0;
202 while(i < fProxies[p].fQuadCnt && iter.next()) {
203 const GrQuad& quad = iter.deviceQuad();
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400204 const GrQuad& uv = iter.localQuad();
Michael Ludwig425eb452019-06-27 10:13:27 -0400205 const ColorDomainAndAA& info = iter.metadata();
Brian Salomond7065e72018-10-12 11:42:02 -0400206 str.appendf(
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400207 "%d: Color: 0x%08x, Domain(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
208 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
209 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
210 i, info.fColor.toBytes_RGBA(), info.fHasDomain, info.fDomainRect.fLeft,
211 info.fDomainRect.fTop, info.fDomainRect.fRight, info.fDomainRect.fBottom,
212 quad.point(0).fX, quad.point(0).fY, quad.point(1).fX, quad.point(1).fY,
213 quad.point(2).fX, quad.point(2).fY, quad.point(3).fX, quad.point(3).fY,
214 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
215 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
216
Michael Ludwig425eb452019-06-27 10:13:27 -0400217 i++;
Brian Salomond7065e72018-10-12 11:42:02 -0400218 }
Brian Salomon34169692017-08-28 15:32:01 -0400219 }
220 str += INHERITED::dumpInfo();
221 return str;
222 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500223#endif
Brian Salomon34169692017-08-28 15:32:01 -0400224
Brian Osman5ced0bf2019-03-15 10:15:29 -0400225 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600226 const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage,
227 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400228 fColorType = static_cast<unsigned>(ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400229 auto iter = fQuads.metadata();
230 while(iter.next()) {
231 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor, clampType, caps);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400232 fColorType = SkTMax(fColorType, static_cast<unsigned>(colorType));
233 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700234 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400235 }
236
Brian Salomon485b8c62018-01-12 15:11:06 -0500237 FixedFunctionFlags fixedFunctionFlags() const override {
238 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
239 : FixedFunctionFlags::kNone;
240 }
Brian Salomon34169692017-08-28 15:32:01 -0400241
242 DEFINE_OP_CLASS_ID
243
244private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400245 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500246
Michael Ludwig425eb452019-06-27 10:13:27 -0400247 struct ColorDomainAndAA {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400248 ColorDomainAndAA(const SkPMColor4f& color, const SkRect* domainRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400249 : fColor(color)
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400250 , fDomainRect(domainRect ? *domainRect : SkRect::MakeEmpty())
251 , fHasDomain(static_cast<unsigned>(domainRect ? Domain::kYes : Domain::kNo))
Michael Ludwig425eb452019-06-27 10:13:27 -0400252 , fAAFlags(static_cast<unsigned>(aaFlags)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400253 SkASSERT(fAAFlags == static_cast<unsigned>(aaFlags));
254 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400255
256 SkPMColor4f fColor;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400257 SkRect fDomainRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400258 unsigned fHasDomain : 1;
259 unsigned fAAFlags : 4;
260
261 Domain domain() const { return Domain(fHasDomain); }
262 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
263 };
264 struct Proxy {
265 GrTextureProxy* fProxy;
266 int fQuadCnt;
267 };
268
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400269 // dstQuad should be the geometry transformed by the view matrix. If domainRect
270 // is not null it will be used to apply the strict src rect constraint.
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400271 TextureOp(sk_sp<GrTextureProxy> proxy,
272 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
273 GrSamplerState::Filter filter,
274 const SkPMColor4f& color,
275 GrTextureOp::Saturate saturate,
276 GrAAType aaType,
277 GrQuadAAFlags aaFlags,
278 const GrQuad& dstQuad,
279 const GrQuad& srcQuad,
280 const SkRect* domainRect)
Brian Salomon34169692017-08-28 15:32:01 -0400281 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400282 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400283 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400284 , fSaturate(static_cast<unsigned>(saturate))
Robert Phillips7327c9d2019-10-08 16:32:56 -0400285 , fFilter(static_cast<unsigned>(filter))
286 , fPrePrepared(false) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400287 // Clean up disparities between the overall aa type and edge configuration and apply
288 // optimizations based on the rect and matrix when appropriate
Michael Ludwig0f809022019-06-04 09:14:37 -0400289 GrQuadUtils::ResolveAAType(aaType, aaFlags, dstQuad, &aaType, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400290 fAAType = static_cast<unsigned>(aaType);
291
Brian Salomonf1709042018-10-03 11:57:00 -0400292 // We expect our caller to have already caught this optimization.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400293 SkASSERT(!domainRect || !domainRect->contains(proxy->getWorstCaseBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500294
Brian Salomonf09abc52018-10-03 15:59:04 -0400295 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
296 // If we don't have (or determined we don't need) coverage AA then we can skip using a
297 // domain.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400298 if (domainRect && this->filter() == GrSamplerState::Filter::kNearest &&
Michael Ludwig6bee7762018-10-19 09:50:36 -0400299 aaType != GrAAType::kCoverage) {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400300 domainRect = nullptr;
Brian Salomonf09abc52018-10-03 15:59:04 -0400301 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500302
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400303 fQuads.append(dstQuad, {color, domainRect, aaFlags}, &srcQuad);
Michael Ludwig425eb452019-06-27 10:13:27 -0400304
Brian Salomond7065e72018-10-12 11:42:02 -0400305 fProxyCnt = 1;
306 fProxies[0] = {proxy.release(), 1};
Michael Ludwig41f395d2019-05-23 13:59:45 -0400307 this->setBounds(dstQuad.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -0400308 IsHairline::kNo);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400309 fDomain = static_cast<unsigned>(domainRect != nullptr);
Brian Salomond7065e72018-10-12 11:42:02 -0400310 }
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400311 TextureOp(const GrRenderTargetContext::TextureSetEntry set[],
312 int cnt,
313 GrSamplerState::Filter filter,
314 GrTextureOp::Saturate saturate,
315 GrAAType aaType,
316 SkCanvas::SrcRectConstraint constraint,
317 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500318 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400319 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400320 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400321 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400322 , fSaturate(static_cast<unsigned>(saturate))
Robert Phillips7327c9d2019-10-08 16:32:56 -0400323 , fFilter(static_cast<unsigned>(filter))
324 , fPrePrepared(false) {
Brian Salomond7065e72018-10-12 11:42:02 -0400325 fProxyCnt = SkToUInt(cnt);
326 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig6bee7762018-10-19 09:50:36 -0400327 GrAAType overallAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon0087c832018-10-15 14:48:20 -0400328 bool mustFilter = false;
Brian Salomon1d835422019-03-13 16:11:44 -0400329 bool allOpaque = true;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400330 Domain netDomain = Domain::kNo;
Brian Salomond7065e72018-10-12 11:42:02 -0400331 for (unsigned p = 0; p < fProxyCnt; ++p) {
332 fProxies[p].fProxy = SkRef(set[p].fProxy.get());
333 fProxies[p].fQuadCnt = 1;
334 SkASSERT(fProxies[p].fProxy->textureType() == fProxies[0].fProxy->textureType());
335 SkASSERT(fProxies[p].fProxy->config() == fProxies[0].fProxy->config());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500336
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500337 SkMatrix ctm = viewMatrix;
338 if (set[p].fPreViewMatrix) {
339 ctm.preConcat(*set[p].fPreViewMatrix);
340 }
341
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400342 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
343 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
344 GrQuad quad, srcQuad;
345 if (set[p].fDstClipQuad) {
346 quad = GrQuad::MakeFromSkQuad(set[p].fDstClipQuad, ctm);
347
348 SkPoint srcPts[4];
349 GrMapRectPoints(set[p].fDstRect, set[p].fSrcRect, set[p].fDstClipQuad, srcPts, 4);
350 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
351 } else {
352 quad = GrQuad::MakeFromRect(set[p].fDstRect, ctm);
353 srcQuad = GrQuad(set[p].fSrcRect);
354 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500355
Michael Ludwig22429f92019-06-27 10:44:48 -0400356 if (!mustFilter && this->filter() != GrSamplerState::Filter::kNearest) {
357 mustFilter = filter_has_effect(srcQuad, quad);
358 }
359
Michael Ludwig41f395d2019-05-23 13:59:45 -0400360 bounds.joinPossiblyEmptyRect(quad.bounds());
Michael Ludwig6bee7762018-10-19 09:50:36 -0400361 GrQuadAAFlags aaFlags;
362 // Don't update the overall aaType, might be inappropriate for some of the quads
363 GrAAType aaForQuad;
Michael Ludwig0f809022019-06-04 09:14:37 -0400364 GrQuadUtils::ResolveAAType(aaType, set[p].fAAFlags, quad, &aaForQuad, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400365 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
366 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
367 if (overallAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
368 overallAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400369 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400370
371 // Calculate metadata for the entry
372 const SkRect* domainForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400373 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
374 // Check (briefly) if the strict constraint is needed for this set entry
375 if (!set[p].fSrcRect.contains(fProxies[p].fProxy->getWorstCaseBoundsRect()) &&
376 (mustFilter || aaForQuad == GrAAType::kCoverage)) {
377 // Can't rely on hardware clamping and the draw will access outer texels
378 // for AA and/or bilerp
379 netDomain = Domain::kYes;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400380 domainForQuad = &set[p].fSrcRect;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400381 }
382 }
Brian Salomond003d222018-11-26 13:25:05 -0500383 float alpha = SkTPin(set[p].fAlpha, 0.f, 1.f);
Brian Salomon1d835422019-03-13 16:11:44 -0400384 allOpaque &= (1.f == alpha);
Brian Salomond003d222018-11-26 13:25:05 -0500385 SkPMColor4f color{alpha, alpha, alpha, alpha};
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400386 fQuads.append(quad, {color, domainForQuad, aaFlags}, &srcQuad);
Brian Salomond7065e72018-10-12 11:42:02 -0400387 }
Michael Ludwig6bee7762018-10-19 09:50:36 -0400388 fAAType = static_cast<unsigned>(overallAAType);
Brian Salomon0087c832018-10-15 14:48:20 -0400389 if (!mustFilter) {
390 fFilter = static_cast<unsigned>(GrSamplerState::Filter::kNearest);
391 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400392 this->setBounds(bounds, HasAABloat(this->aaType() == GrAAType::kCoverage),
393 IsHairline::kNo);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400394 fDomain = static_cast<unsigned>(netDomain);
Brian Salomon34169692017-08-28 15:32:01 -0400395 }
396
Michael Ludwig425eb452019-06-27 10:13:27 -0400397 void tess(void* v, const VertexSpec& spec, const GrTextureProxy* proxy,
398 GrQuadBuffer<ColorDomainAndAA>::Iter* iter, int cnt) const {
Brian Salomon5f394272019-07-02 14:07:49 -0400399 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond7065e72018-10-12 11:42:02 -0400400 auto origin = proxy->origin();
401 const auto* texture = proxy->peekTexture();
Brian Salomon246bc3d2018-12-06 15:33:02 -0500402 float iw, ih, h;
403 if (proxy->textureType() == GrTextureType::kRectangle) {
404 iw = ih = 1.f;
405 h = texture->height();
406 } else {
407 iw = 1.f / texture->width();
408 ih = 1.f / texture->height();
409 h = 1.f;
410 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000411
Michael Ludwig425eb452019-06-27 10:13:27 -0400412 int i = 0;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400413 // Explicit ctor ensures ws are 1s, which compute_src_quad requires
414 GrQuad srcQuad(SkRect::MakeEmpty());
415 SkRect domain;
Michael Ludwig425eb452019-06-27 10:13:27 -0400416 while(i < cnt && iter->next()) {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400417 SkASSERT(iter->isLocalValid());
Michael Ludwig425eb452019-06-27 10:13:27 -0400418 const ColorDomainAndAA& info = iter->metadata();
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400419 // Must correct the texture coordinates and domain now that the real texture size
420 // is known
421 compute_src_quad(origin, iter->localQuad(), iw, ih, h, &srcQuad);
422 compute_domain(info.domain(), this->filter(), origin, info.fDomainRect, iw, ih, h,
423 &domain);
Michael Ludwig425eb452019-06-27 10:13:27 -0400424 v = GrQuadPerEdgeAA::Tessellate(v, spec, iter->deviceQuad(), info.fColor, srcQuad,
425 domain, info.aaFlags());
426 i++;
Brian Salomon17031a72018-05-22 14:14:07 -0400427 }
428 }
429
Robert Phillips61fc7992019-10-22 11:58:17 -0400430 void onPrePrepareDraws(GrRecordingContext* context, const GrAppliedClip* clip) override {
431 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips29f38542019-10-16 09:20:25 -0400432
Robert Phillips61fc7992019-10-22 11:58:17 -0400433 SkDEBUGCODE(this->validate();)
434 SkASSERT(!fPrePrepared);
435
436 int numProxies, numTotalQuads;
437
438 SkArenaAlloc* arena = context->priv().opPODAllocator();
439
440 const VertexSpec vertexSpec = this->characterize(&numProxies, &numTotalQuads);
441 (void) vertexSpec;
442
443 // We'll use a dynamic state array for the GP textures when there are multiple ops.
444 // Otherwise, we use fixed dynamic state to specify the single op's proxy.
445 // Note: these are being allocated in the opPOD arena not the flush state!
446 SkASSERT(!fDynamicStateArrays && !fFixedDynamicState);
447 if (numProxies > 1) {
448 fDynamicStateArrays = Target::AllocDynamicStateArrays(arena, numProxies, 1, false);
449 fFixedDynamicState = Target::MakeFixedDynamicState(arena, clip, 0);
450 } else {
451 fFixedDynamicState = Target::MakeFixedDynamicState(arena, clip, 1);
452 fFixedDynamicState->fPrimitiveProcessorTextures[0] = fProxies[0].fProxy;
453 }
454
455 // Pull forward the tessellation of the quads to here
Robert Phillips29f38542019-10-16 09:20:25 -0400456
Robert Phillips7327c9d2019-10-08 16:32:56 -0400457 fPrePrepared = true;
458 }
459
Robert Phillips29f38542019-10-16 09:20:25 -0400460#ifdef SK_DEBUG
461 void validate() const override {
Brian Salomond7065e72018-10-12 11:42:02 -0400462 auto textureType = fProxies[0].fProxy->textureType();
Greg Daniel2c3398d2019-06-19 11:58:01 -0400463 const GrSwizzle& swizzle = fProxies[0].fProxy->textureSwizzle();
Brian Salomonae7d7702018-10-14 15:05:45 -0400464 GrAAType aaType = this->aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400465
466 for (const auto& op : ChainRange<TextureOp>(this)) {
467 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
468 auto* proxy = op.fProxies[p].fProxy;
469 SkASSERT(proxy);
470 SkASSERT(proxy->textureType() == textureType);
471 SkASSERT(proxy->textureSwizzle() == swizzle);
472 }
473
474 // Each individual op must be a single aaType. kCoverage and kNone ops can chain
475 // together but kMSAA ones do not.
476 if (aaType == GrAAType::kCoverage || aaType == GrAAType::kNone) {
477 SkASSERT(op.aaType() == GrAAType::kCoverage || op.aaType() == GrAAType::kNone);
478 } else {
479 SkASSERT(aaType == GrAAType::kMSAA && op.aaType() == GrAAType::kMSAA);
480 }
481 }
482 }
483#endif
484
485 VertexSpec characterize(int* numProxies, int* numTotalQuads) const {
486 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
487 ColorType colorType = ColorType::kNone;
488 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
489 Domain domain = Domain::kNo;
490 GrAAType overallAAType = this->aaType();
491
492 *numProxies = 0;
493 *numTotalQuads = 0;
494
Brian Salomonf7232642018-09-19 08:58:08 -0400495 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400496 if (op.fQuads.deviceQuadType() > quadType) {
497 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500498 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400499 if (op.fQuads.localQuadType() > srcQuadType) {
500 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500501 }
Brian Salomonf7232642018-09-19 08:58:08 -0400502 if (op.fDomain) {
503 domain = Domain::kYes;
504 }
Brian Salomon1d835422019-03-13 16:11:44 -0400505 colorType = SkTMax(colorType, static_cast<ColorType>(op.fColorType));
Robert Phillips29f38542019-10-16 09:20:25 -0400506 *numProxies += op.fProxyCnt;
Brian Salomond7065e72018-10-12 11:42:02 -0400507 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
Robert Phillips29f38542019-10-16 09:20:25 -0400508 *numTotalQuads += op.fProxies[p].fQuadCnt;
Brian Salomonf7232642018-09-19 08:58:08 -0400509 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400510 if (op.aaType() == GrAAType::kCoverage) {
Robert Phillips29f38542019-10-16 09:20:25 -0400511 overallAAType = GrAAType::kCoverage;
Brian Salomonae7d7702018-10-14 15:05:45 -0400512 }
Brian Salomon34169692017-08-28 15:32:01 -0400513 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400514
Robert Phillips29f38542019-10-16 09:20:25 -0400515 return VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true, domain,
516 overallAAType, /* alpha as coverage */ true);
517 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500518
Robert Phillips29f38542019-10-16 09:20:25 -0400519 // onPrePrepareDraws may or may not have been called at this point
520 void onPrepareDraws(Target* target) override {
521 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500522
Robert Phillips29f38542019-10-16 09:20:25 -0400523 SkDEBUGCODE(this->validate();)
524
525 int numProxies, numTotalQuads;
526
527 const VertexSpec vertexSpec = this->characterize(&numProxies, &numTotalQuads);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500528
Brian Salomonf7232642018-09-19 08:58:08 -0400529 // We'll use a dynamic state array for the GP textures when there are multiple ops.
530 // Otherwise, we use fixed dynamic state to specify the single op's proxy.
531 GrPipeline::DynamicStateArrays* dynamicStateArrays = nullptr;
532 GrPipeline::FixedDynamicState* fixedDynamicState;
Robert Phillips61fc7992019-10-22 11:58:17 -0400533
534 if (fPrePrepared) {
535 dynamicStateArrays = fDynamicStateArrays;
536 fixedDynamicState = fFixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400537 } else {
Robert Phillips61fc7992019-10-22 11:58:17 -0400538 SkArenaAlloc* arena = target->allocator();
539
540 SkASSERT(!fDynamicStateArrays && !fFixedDynamicState);
541
542 if (numProxies > 1) {
543 dynamicStateArrays = Target::AllocDynamicStateArrays(arena, numProxies, 1, false);
544 fixedDynamicState = Target::MakeFixedDynamicState(arena, target->appliedClip(), 0);
545 } else {
546 fixedDynamicState = Target::MakeFixedDynamicState(arena, target->appliedClip(), 1);
547 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxies[0].fProxy;
548 }
Brian Salomonf7232642018-09-19 08:58:08 -0400549 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400550
Robert Phillips29f38542019-10-16 09:20:25 -0400551 size_t vertexSize = vertexSpec.vertexSize();
Brian Salomon92be2f72018-06-19 14:33:47 -0400552
Brian Salomond7065e72018-10-12 11:42:02 -0400553 GrMesh* meshes = target->allocMeshes(numProxies);
Brian Salomon12d22642019-01-29 14:38:50 -0500554 sk_sp<const GrBuffer> vbuffer;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400555 int vertexOffsetInBuffer = 0;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500556 int numQuadVerticesLeft = numTotalQuads * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400557 int numAllocatedVertices = 0;
558 void* vdata = nullptr;
559
Brian Salomond7065e72018-10-12 11:42:02 -0400560 int m = 0;
Brian Salomonf7232642018-09-19 08:58:08 -0400561 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400562 auto iter = op.fQuads.iterator();
Brian Salomond7065e72018-10-12 11:42:02 -0400563 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
564 int quadCnt = op.fProxies[p].fQuadCnt;
565 auto* proxy = op.fProxies[p].fProxy;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500566 int meshVertexCnt = quadCnt * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400567 if (numAllocatedVertices < meshVertexCnt) {
568 vdata = target->makeVertexSpaceAtLeast(
569 vertexSize, meshVertexCnt, numQuadVerticesLeft, &vbuffer,
570 &vertexOffsetInBuffer, &numAllocatedVertices);
571 SkASSERT(numAllocatedVertices <= numQuadVerticesLeft);
572 if (!vdata) {
573 SkDebugf("Could not allocate vertices\n");
574 return;
575 }
Brian Salomonf7232642018-09-19 08:58:08 -0400576 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400577 SkASSERT(numAllocatedVertices >= meshVertexCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400578
Michael Ludwig425eb452019-06-27 10:13:27 -0400579 op.tess(vdata, vertexSpec, proxy, &iter, quadCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400580
Michael Ludwig93aeba02018-12-21 09:50:31 -0500581 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, &(meshes[m]), vertexSpec,
582 quadCnt)) {
583 SkDebugf("Could not allocate indices");
584 return;
Brian Salomond7065e72018-10-12 11:42:02 -0400585 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400586 meshes[m].setVertexData(vbuffer, vertexOffsetInBuffer);
Brian Salomond7065e72018-10-12 11:42:02 -0400587 if (dynamicStateArrays) {
588 dynamicStateArrays->fPrimitiveProcessorTextures[m] = proxy;
589 }
590 ++m;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400591 numAllocatedVertices -= meshVertexCnt;
592 numQuadVerticesLeft -= meshVertexCnt;
593 vertexOffsetInBuffer += meshVertexCnt;
594 vdata = reinterpret_cast<char*>(vdata) + vertexSize * meshVertexCnt;
Brian Salomonf7232642018-09-19 08:58:08 -0400595 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400596 // If quad counts per proxy were calculated correctly, the entire iterator should have
597 // been consumed.
598 SkASSERT(!iter.next());
Brian Salomon34169692017-08-28 15:32:01 -0400599 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400600 SkASSERT(!numQuadVerticesLeft);
601 SkASSERT(!numAllocatedVertices);
Robert Phillips29f38542019-10-16 09:20:25 -0400602
603 sk_sp<GrGeometryProcessor> gp;
604
605 {
Robert Phillipsf272bea2019-10-17 08:56:16 -0400606 const GrBackendFormat& backendFormat = fProxies[0].fProxy->backendFormat();
Robert Phillips29f38542019-10-16 09:20:25 -0400607 const GrSwizzle& swizzle = fProxies[0].fProxy->textureSwizzle();
608
609 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
610 this->filter());
611
612 auto saturate = static_cast<GrTextureOp::Saturate>(fSaturate);
613
614 GrGpu* gpu = target->resourceProvider()->priv().gpu();
Robert Phillipsf272bea2019-10-17 08:56:16 -0400615 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(samplerState,
616 backendFormat);
Robert Phillips29f38542019-10-16 09:20:25 -0400617
618 gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
Robert Phillipsf272bea2019-10-17 08:56:16 -0400619 vertexSpec, *target->caps().shaderCaps(), backendFormat, samplerState, swizzle,
Robert Phillips29f38542019-10-16 09:20:25 -0400620 extraSamplerKey, std::move(fTextureColorSpaceXform), saturate);
621
622 SkASSERT(vertexSize == gp->vertexStride());
623 }
624
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700625 target->recordDraw(
626 std::move(gp), meshes, numProxies, fixedDynamicState, dynamicStateArrays);
627 }
628
629 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
630 auto pipelineFlags = (GrAAType::kMSAA == this->aaType())
Chris Daltonbaa1b352019-04-03 12:03:00 -0600631 ? GrPipeline::InputFlags::kHWAntialias
632 : GrPipeline::InputFlags::kNone;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700633 flushState->executeDrawsAndUploadsForMeshDrawOp(
634 this, chainBounds, GrProcessorSet::MakeEmptySet(), pipelineFlags);
Brian Salomon34169692017-08-28 15:32:01 -0400635 }
636
Brian Salomonf7232642018-09-19 08:58:08 -0400637 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400638 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400639 const auto* that = t->cast<TextureOp>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400640
641 if (fPrePrepared || that->fPrePrepared) {
642 // This should never happen (since only DDL recorded ops should be prePrepared)
643 // but, in any case, we should never combine ops that that been prePrepared
644 return CombineResult::kCannotCombine;
645 }
646
Michael Ludwig2929f512019-04-19 13:05:56 -0400647 if (fDomain != that->fDomain) {
648 // It is technically possible to combine operations across domain modes, but performance
649 // testing suggests it's better to make more draw calls where some take advantage of
650 // the more optimal shader path without coordinate clamping.
651 return CombineResult::kCannotCombine;
652 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400653 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
654 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000655 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400656 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400657 bool upgradeToCoverageAAOnMerge = false;
Brian Salomon485b8c62018-01-12 15:11:06 -0500658 if (this->aaType() != that->aaType()) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400659 if (!((this->aaType() == GrAAType::kCoverage && that->aaType() == GrAAType::kNone) ||
660 (that->aaType() == GrAAType::kCoverage && this->aaType() == GrAAType::kNone))) {
661 return CombineResult::kCannotCombine;
662 }
663 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500664 }
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400665 if (fSaturate != that->fSaturate) {
666 return CombineResult::kCannotCombine;
667 }
Brian Salomonf7232642018-09-19 08:58:08 -0400668 if (fFilter != that->fFilter) {
669 return CombineResult::kCannotCombine;
670 }
Brian Salomond7065e72018-10-12 11:42:02 -0400671 auto thisProxy = fProxies[0].fProxy;
672 auto thatProxy = that->fProxies[0].fProxy;
673 if (fProxyCnt > 1 || that->fProxyCnt > 1 ||
Brian Salomon588cec72018-11-14 13:56:37 -0500674 thisProxy->uniqueID() != thatProxy->uniqueID()) {
675 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -0500676 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Brian Salomonf7232642018-09-19 08:58:08 -0400677 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
678 return CombineResult::kMayChain;
679 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000680 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400681 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500682
Brian Salomonb80ffee2018-05-23 16:39:39 -0400683 fDomain |= that->fDomain;
Brian Salomon1d835422019-03-13 16:11:44 -0400684 fColorType = SkTMax(fColorType, that->fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -0400685 if (upgradeToCoverageAAOnMerge) {
686 fAAType = static_cast<unsigned>(GrAAType::kCoverage);
687 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500688
Michael Ludwig425eb452019-06-27 10:13:27 -0400689 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -0500690 fQuads.concat(that->fQuads);
691 fProxies[0].fQuadCnt += that->fQuads.count();
692
Brian Salomon7eae3e02018-08-07 14:02:38 +0000693 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -0400694 }
695
Brian Salomon485b8c62018-01-12 15:11:06 -0500696 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomon0087c832018-10-15 14:48:20 -0400697 GrSamplerState::Filter filter() const { return static_cast<GrSamplerState::Filter>(fFilter); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500698
Michael Ludwig425eb452019-06-27 10:13:27 -0400699 GrQuadBuffer<ColorDomainAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -0400700 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Robert Phillips61fc7992019-10-22 11:58:17 -0400701 // fDynamicStateArrays and fFixedDynamicState are only filled in when this op has been
702 // prePrepared. In that case they've been allocated in the opPOD arena not in the
703 // FlushState arena.
704 GrPipeline::DynamicStateArrays* fDynamicStateArrays = nullptr;
705 GrPipeline::FixedDynamicState* fFixedDynamicState = nullptr;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400706 unsigned fSaturate : 1;
Brian Salomon0087c832018-10-15 14:48:20 -0400707 unsigned fFilter : 2;
Brian Salomon485b8c62018-01-12 15:11:06 -0500708 unsigned fAAType : 2;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400709 unsigned fDomain : 1;
Brian Salomon1d835422019-03-13 16:11:44 -0400710 unsigned fColorType : 2;
711 GR_STATIC_ASSERT(GrQuadPerEdgeAA::kColorTypeCount <= 4);
Robert Phillips7327c9d2019-10-08 16:32:56 -0400712 unsigned fPrePrepared : 1;
713 unsigned fProxyCnt : 32 - 7;
Brian Salomond7065e72018-10-12 11:42:02 -0400714 Proxy fProxies[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -0400715
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400716 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type does not fit in 2 bits");
Michael Ludwigf995c052018-11-26 15:24:29 -0500717
Brian Salomon34169692017-08-28 15:32:01 -0400718 typedef GrMeshDrawOp INHERITED;
719};
720
721} // anonymous namespace
722
723namespace GrTextureOp {
724
Michael Ludwig205224f2019-06-27 10:47:42 -0400725std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
726 sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400727 GrColorType srcColorType,
Michael Ludwig205224f2019-06-27 10:47:42 -0400728 sk_sp<GrColorSpaceXform> textureXform,
729 GrSamplerState::Filter filter,
730 const SkPMColor4f& color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400731 Saturate saturate,
Michael Ludwig205224f2019-06-27 10:47:42 -0400732 SkBlendMode blendMode,
733 GrAAType aaType,
734 GrQuadAAFlags aaFlags,
735 const GrQuad& deviceQuad,
736 const GrQuad& localQuad,
737 const SkRect* domain) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400738 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
739 if (domain && domain->contains(proxy->getWorstCaseBoundsRect())) {
740 // No need for a shader-based domain if hardware clamping achieves the same effect
741 domain = nullptr;
742 }
743
744 if (filter != GrSamplerState::Filter::kNearest && !filter_has_effect(localQuad, deviceQuad)) {
745 filter = GrSamplerState::Filter::kNearest;
746 }
747
748 if (blendMode == SkBlendMode::kSrcOver) {
749 return TextureOp::Make(context, std::move(proxy), std::move(textureXform), filter, color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400750 saturate, aaType, aaFlags, deviceQuad, localQuad, domain);
Michael Ludwig22429f92019-06-27 10:44:48 -0400751 } else {
752 // Emulate complex blending using GrFillRectOp
753 GrPaint paint;
754 paint.setColor4f(color);
755 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
756
757 std::unique_ptr<GrFragmentProcessor> fp;
758 if (domain) {
759 // Update domain to match what GrTextureOp computes during tessellation, using top-left
760 // as the origin so that it doesn't depend on final texture size (which the FP handles
761 // later, as well as accounting for the true origin).
762 SkRect correctedDomain;
763 compute_domain(Domain::kYes, filter, kTopLeft_GrSurfaceOrigin, *domain,
764 1.f, 1.f, proxy->height(), &correctedDomain);
Greg Danielc594e622019-10-15 14:01:49 -0400765 fp = GrTextureDomainEffect::Make(std::move(proxy), srcColorType, SkMatrix::I(),
766 correctedDomain, GrTextureDomain::kClamp_Mode, filter);
Michael Ludwig22429f92019-06-27 10:44:48 -0400767 } else {
Greg Danielc594e622019-10-15 14:01:49 -0400768 fp = GrSimpleTextureEffect::Make(std::move(proxy), srcColorType, SkMatrix::I(), filter);
Michael Ludwig22429f92019-06-27 10:44:48 -0400769 }
770 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
771 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400772 if (saturate == GrTextureOp::Saturate::kYes) {
773 paint.addColorFragmentProcessor(GrSaturateProcessor::Make());
774 }
Michael Ludwig22429f92019-06-27 10:44:48 -0400775
776 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
777 deviceQuad, localQuad);
778 }
779}
780
Michael Ludwig009b92e2019-02-15 16:03:53 -0500781std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
782 const GrRenderTargetContext::TextureSetEntry set[],
783 int cnt,
784 GrSamplerState::Filter filter,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400785 Saturate saturate,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500786 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400787 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500788 const SkMatrix& viewMatrix,
789 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400790 return TextureOp::Make(context, set, cnt, filter, saturate, aaType, constraint, viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500791 std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400792}
793
Brian Salomon34169692017-08-28 15:32:01 -0400794} // namespace GrTextureOp
795
796#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500797#include "include/private/GrRecordingContext.h"
798#include "src/gpu/GrProxyProvider.h"
799#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -0400800
801GR_DRAW_OP_TEST_DEFINE(TextureOp) {
802 GrSurfaceDesc desc;
803 desc.fConfig = kRGBA_8888_GrPixelConfig;
804 desc.fHeight = random->nextULessThan(90) + 10;
805 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500806 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel09c94002018-06-08 22:11:51 +0000807 GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
808 SkBackingFit fit = SkBackingFit::kExact;
809 if (mipMapped == GrMipMapped::kNo) {
810 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
811 }
Greg Daniel4065d452018-11-16 15:43:41 -0500812 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400813 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
814 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500815
Robert Phillips9da87e02019-02-04 13:26:26 -0500816 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -0400817 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400818 format, desc, GrRenderable::kNo, 1, origin, mipMapped, fit, SkBudgeted::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400819 GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500820
Brian Salomon34169692017-08-28 15:32:01 -0400821 SkRect rect = GrTest::TestRect(random);
822 SkRect srcRect;
823 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
824 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
825 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
826 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
827 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -0500828 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400829 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
830 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +0000831 while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
832 filter = (GrSamplerState::Filter)random->nextULessThan(
833 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
834 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400835 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -0500836 GrAAType aaType = GrAAType::kNone;
837 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600838 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -0500839 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400840 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
841 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
842 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
843 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
844 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Michael Ludwig205224f2019-06-27 10:47:42 -0400845 bool useDomain = random->nextBool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400846 auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
Greg Danielc594e622019-10-15 14:01:49 -0400847 return GrTextureOp::Make(context, std::move(proxy), GrColorType::kRGBA_8888,
848 std::move(texXform), filter, color, saturate, SkBlendMode::kSrcOver,
849 aaType, aaFlags, GrQuad::MakeFromRect(rect, viewMatrix),
850 GrQuad(srcRect), useDomain ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -0400851}
852
853#endif