blob: ee268c339b75f6463556bf6907e65194cca1426e [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"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrRecordingContext.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040013#include "include/private/SkFloatingPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkTo.h"
15#include "src/core/SkMathPriv.h"
16#include "src/core/SkMatrixPriv.h"
17#include "src/core/SkRectPriv.h"
18#include "src/gpu/GrAppliedClip.h"
19#include "src/gpu/GrCaps.h"
20#include "src/gpu/GrDrawOpTest.h"
21#include "src/gpu/GrGeometryProcessor.h"
22#include "src/gpu/GrGpu.h"
23#include "src/gpu/GrMemoryPool.h"
24#include "src/gpu/GrOpFlushState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrRecordingContextPriv.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/GrResourceProviderPriv.h"
28#include "src/gpu/GrShaderCaps.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000029#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040030#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/SkGr.h"
John Stilesf743d4e2020-07-23 11:35:08 -040032#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Osman6f5e9402020-01-22 10:39:31 -050033#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040034#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040035#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040036#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/glsl/GrGLSLVarying.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040038#include "src/gpu/ops/GrFillRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/ops/GrMeshDrawOp.h"
40#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050041#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.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
Brian Salomon2432d062020-04-16 20:48:09 -040046using Subset = GrQuadPerEdgeAA::Subset;
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
Brian Salomone69b9ef2020-07-22 11:18:06 -040060static std::tuple<bool /* filter */,
61 bool /* mipmap */>
62filter_and_mm_have_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
Michael Ludwig22429f92019-06-27 10:44:48 -040063 // If not axis-aligned in src or dst, then always say it has an effect
64 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
65 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040066 return {true, true};
Michael Ludwig22429f92019-06-27 10:44:48 -040067 }
68
69 SkRect srcRect;
70 SkRect dstRect;
71 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
72 // Disable filtering when there is no scaling (width and height are the same), and the
73 // top-left corners have the same fraction (so src and dst snap to the pixel grid
74 // identically).
75 SkASSERT(srcRect.isSorted());
Brian Salomone69b9ef2020-07-22 11:18:06 -040076 bool filter = srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
77 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
78 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
79 bool mm = srcRect.width() > dstRect.width() || srcRect.height() > dstRect.height();
80 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040081 }
Brian Salomone69b9ef2020-07-22 11:18:06 -040082 // Extract edge lengths
83 SkSize srcSize = axis_aligned_quad_size(srcQuad);
84 SkSize dstSize = axis_aligned_quad_size(dstQuad);
85 // Although the quads are axis-aligned, the local coordinate system is transformed such
86 // that fractionally-aligned sample centers will not align with the device coordinate system
87 // So disable filtering when edges are the same length and both srcQuad and dstQuad
88 // 0th vertex is integer aligned.
89 bool filter = srcSize != dstSize ||
90 !SkScalarIsInt(srcQuad.x(0)) ||
91 !SkScalarIsInt(srcQuad.y(0)) ||
92 !SkScalarIsInt(dstQuad.x(0)) ||
93 !SkScalarIsInt(dstQuad.y(0));
94 bool mm = srcSize.fWidth > dstSize.fWidth || srcSize.fHeight > dstSize.fHeight;
95 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040096}
97
Michael Ludwig119ac6d2019-11-21 09:26:46 -050098// Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
99// regular and rectangular textures, w/ or w/o origin correction.
100struct NormalizationParams {
101 float fIW; // 1 / width of texture, or 1.0 for texture rectangles
Michael Ludwigc453a502020-05-29 12:29:12 -0400102 float fInvH; // 1 / height of texture, or 1.0 for tex rects, X -1 if bottom-left origin
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500103 float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
104};
Michael Ludwigadb12e72019-12-04 16:19:18 -0500105static NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
106 GrSurfaceOrigin origin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500107 // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
108 // normalize the src coordinates up front.
Michael Ludwigadb12e72019-12-04 16:19:18 -0500109 SkISize dimensions = proxy->backingStoreDimensions();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500110 float iw, ih, h;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500111 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500112 iw = ih = 1.f;
113 h = dimensions.height();
114 } else {
115 iw = 1.f / dimensions.width();
116 ih = 1.f / dimensions.height();
117 h = 1.f;
118 }
119
Michael Ludwigadb12e72019-12-04 16:19:18 -0500120 if (origin == kBottomLeft_GrSurfaceOrigin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500121 return {iw, -ih, h};
122 } else {
123 return {iw, ih, 0.0f};
124 }
125}
126
Brian Salomon2432d062020-04-16 20:48:09 -0400127// Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500128// so a sufficiently large rect is returned even if the quad ends up batched with an op that uses
Brian Salomon75cebbe2020-05-18 14:08:14 -0400129// subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
130// and y-flipping are applied as indicated by NormalizationParams.
131static SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
132 const NormalizationParams& params,
133 const SkRect* subsetRect) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500134 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Brian Salomon2432d062020-04-16 20:48:09 -0400135 if (!subsetRect) {
136 // Either the quad has no subset constraint and is batched with a subset constrained op
137 // (in which case we want a subset that doesn't restrict normalized tex coords), or the
138 // entire op doesn't use the subset, in which case the returned value is ignored.
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500139 return kLargeRect;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400140 }
141
Brian Salomon2432d062020-04-16 20:48:09 -0400142 auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400143 auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
144 if (filter == GrSamplerState::Filter::kNearest) {
145 // Make sure our insetting puts us at pixel centers.
146 ltrb = skvx::floor(ltrb*flipHi)*flipHi;
147 }
148 // Inset with pin to the rect center.
149 ltrb += skvx::Vec<4, float>({.5f, .5f, -.5f, -.5f});
150 auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
151 ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
152
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500153 // Normalize and offset
Elliot Evans9fdddf62020-07-29 16:39:11 -0400154 ltrb = ltrb * skvx::Vec<4, float>{params.fIW, params.fInvH, params.fIW, params.fInvH} +
155 skvx::Vec<4, float>{0.f, params.fYOffset, 0.f, params.fYOffset};
Michael Ludwigc453a502020-05-29 12:29:12 -0400156 if (params.fInvH < 0.f) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500157 // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
158 ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400159 }
160
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500161 SkRect out;
162 ltrb.store(&out);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500163 return out;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400164}
165
Michael Ludwig009b92e2019-02-15 16:03:53 -0500166// Normalizes logical src coords and corrects for origin
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500167static void normalize_src_quad(const NormalizationParams& params,
168 GrQuad* srcQuad) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500169 // The src quad should not have any perspective
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500170 SkASSERT(!srcQuad->hasPerspective());
171 skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
Elliot Evans9fdddf62020-07-29 16:39:11 -0400172 skvx::Vec<4, float> ys = srcQuad->y4f() * params.fInvH + params.fYOffset;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500173 xs.store(srcQuad->xs());
174 ys.store(srcQuad->ys());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500175}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400176
Michael Ludwig379e4962019-12-06 13:21:26 -0500177// Count the number of proxy runs in the entry set. This usually is already computed by
178// SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
179// for each split.
Brian Salomoneebe7352020-12-09 16:37:04 -0500180static int proxy_run_count(const GrSurfaceDrawContext::TextureSetEntry set[], int count) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500181 int actualProxyRunCount = 0;
182 const GrSurfaceProxy* lastProxy = nullptr;
183 for (int i = 0; i < count; ++i) {
184 if (set[i].fProxyView.proxy() != lastProxy) {
185 actualProxyRunCount++;
186 lastProxy = set[i].fProxyView.proxy();
187 }
188 }
189 return actualProxyRunCount;
190}
191
John Stilescbe4e282020-06-01 10:38:31 -0400192static bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
193 const DrawQuad& quad, const SkRect& subsetRect) {
194 // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
195 // can push all the way up to the edges of the the subset rect and the sampler shouldn't
196 // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
197 // the non-antialiased case.
198 SkRect localBounds = quad.fLocal.bounds();
199 if (aaType == GrAAType::kNone &&
200 filter == GrSamplerState::Filter::kNearest &&
201 quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
202 quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
203 subsetRect.contains(localBounds)) {
204
205 return true;
206 }
207
208 // If the subset rect is inset by at least 0.5 pixels into the local quad's bounds, the
209 // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
210 if (subsetRect.makeInset(0.5f, 0.5f).contains(localBounds)) {
211 return true;
212 }
213
214 // The subset rect cannot be ignored safely.
215 return false;
216}
217
Brian Salomon34169692017-08-28 15:32:01 -0400218/**
219 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
220 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
221 */
222class TextureOp final : public GrMeshDrawOp {
223public:
Herb Derbyc76d4092020-10-07 16:46:15 -0400224 static GrOp::Owner Make(GrRecordingContext* context,
225 GrSurfaceProxyView proxyView,
226 sk_sp<GrColorSpaceXform> textureXform,
227 GrSamplerState::Filter filter,
228 GrSamplerState::MipmapMode mm,
229 const SkPMColor4f& color,
230 GrTextureOp::Saturate saturate,
231 GrAAType aaType,
232 DrawQuad* quad,
233 const SkRect* subset) {
234
235 return GrOp::Make<TextureOp>(context, std::move(proxyView), std::move(textureXform),
236 filter, mm, color, saturate, aaType, quad, subset);
Brian Salomon34169692017-08-28 15:32:01 -0400237 }
Robert Phillipse837e612019-11-15 11:02:50 -0500238
Herb Derbyc76d4092020-10-07 16:46:15 -0400239 static GrOp::Owner Make(GrRecordingContext* context,
Brian Salomoneebe7352020-12-09 16:37:04 -0500240 GrSurfaceDrawContext::TextureSetEntry set[],
Herb Derbyc76d4092020-10-07 16:46:15 -0400241 int cnt,
242 int proxyRunCnt,
243 GrSamplerState::Filter filter,
244 GrSamplerState::MipmapMode mm,
245 GrTextureOp::Saturate saturate,
246 GrAAType aaType,
247 SkCanvas::SrcRectConstraint constraint,
248 const SkMatrix& viewMatrix,
249 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500250 // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
251 SkASSERT(proxyRunCnt <= cnt);
Herb Derbyc76d4092020-10-07 16:46:15 -0400252 return GrOp::MakeWithExtraMemory<TextureOp>(
253 context, sizeof(ViewCountPair) * (proxyRunCnt - 1),
254 set, cnt, proxyRunCnt, filter, mm, saturate, aaType, constraint,
255 viewMatrix, std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400256 }
Brian Salomon34169692017-08-28 15:32:01 -0400257
Brian Salomon336ce7b2017-09-08 08:23:58 -0400258 ~TextureOp() override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500259 for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400260 fViewCountPairs[p].~ViewCountPair();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400261 }
262 }
Brian Salomon34169692017-08-28 15:32:01 -0400263
264 const char* name() const override { return "TextureOp"; }
265
Chris Dalton1706cbf2019-05-21 19:35:29 -0600266 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400267 bool mipped = (fMetadata.mipmapMode() != GrSamplerState::MipmapMode::kNone);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500268 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400269 func(fViewCountPairs[p].fProxy.get(), GrMipmapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400270 }
Chris Daltondbb833b2020-03-17 12:15:46 -0600271 if (fDesc && fDesc->fProgramInfo) {
272 fDesc->fProgramInfo->visitFPProxies(func);
273 }
Brian Salomond7065e72018-10-12 11:42:02 -0400274 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400275
John Stiles8d9bf642020-08-12 15:07:45 -0400276#ifdef SK_DEBUG
Michael Ludwig4ef1ca12019-12-19 10:58:52 -0500277 static void ValidateResourceLimits() {
278 // The op implementation has an upper bound on the number of quads that it can represent.
279 // However, the resource manager imposes its own limit on the number of quads, which should
280 // always be lower than the numerical limit this op can hold.
281 using CountStorage = decltype(Metadata::fTotalQuadCount);
282 CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
283 // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
284 int resourceLimit = SkTo<int>(maxQuadCount);
285 SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
286 GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
287 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500288#endif
Brian Salomon34169692017-08-28 15:32:01 -0400289
Chris Dalton57ab06c2021-04-22 12:57:28 -0600290 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip*,
291 GrClampType clampType) override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500292 SkASSERT(fMetadata.colorType() == ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400293 auto iter = fQuads.metadata();
294 while(iter.next()) {
Brian Osman2715bf52019-12-06 14:38:47 -0500295 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
Brian Salomon5e29e312021-04-27 11:30:00 -0400296 colorType = std::max(static_cast<GrQuadPerEdgeAA::ColorType>(fMetadata.fColorType),
297 colorType);
298 if (caps.reducedShaderMode()) {
299 colorType = std::max(colorType, GrQuadPerEdgeAA::ColorType::kByte);
300 }
301 fMetadata.fColorType = static_cast<uint16_t>(colorType);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400302 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700303 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400304 }
305
Brian Salomon485b8c62018-01-12 15:11:06 -0500306 FixedFunctionFlags fixedFunctionFlags() const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500307 return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
308 : FixedFunctionFlags::kNone;
Brian Salomon485b8c62018-01-12 15:11:06 -0500309 }
Brian Salomon34169692017-08-28 15:32:01 -0400310
311 DEFINE_OP_CLASS_ID
312
313private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400314 friend class ::GrOp;
Brian Salomon762d5e72017-12-01 10:25:08 -0500315
Brian Salomon2432d062020-04-16 20:48:09 -0400316 struct ColorSubsetAndAA {
317 ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400318 : fColor(color)
Brian Salomon2432d062020-04-16 20:48:09 -0400319 , fSubsetRect(subsetRect)
Michael Ludwig4384f042019-12-05 10:30:35 -0500320 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
321 SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
Michael Ludwig425eb452019-06-27 10:13:27 -0400322 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400323
324 SkPMColor4f fColor;
Brian Salomon2432d062020-04-16 20:48:09 -0400325 // If the op doesn't use subsets, this is ignored. If the op uses subsets and the specific
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500326 // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
Brian Salomon2432d062020-04-16 20:48:09 -0400327 SkRect fSubsetRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400328 unsigned fAAFlags : 4;
329
Michael Ludwig425eb452019-06-27 10:13:27 -0400330 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
331 };
Michael Ludwigadb12e72019-12-04 16:19:18 -0500332
Greg Daniel549325c2019-10-30 16:19:20 -0400333 struct ViewCountPair {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500334 // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
335 // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
336 // swizzle so that is stored in the op metadata.
337 sk_sp<GrSurfaceProxy> fProxy;
Michael Ludwig425eb452019-06-27 10:13:27 -0400338 int fQuadCnt;
339 };
340
Michael Ludwigadb12e72019-12-04 16:19:18 -0500341 // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
342 // increase the size of the op; increasing the op size can have a surprising impact on
343 // performance (since texture ops are one of the most commonly used in an app).
344 struct Metadata {
345 // AAType must be filled after initialization; ColorType is determined in finalize()
Brian Salomone69b9ef2020-07-22 11:18:06 -0400346 Metadata(const GrSwizzle& swizzle,
347 GrSamplerState::Filter filter,
348 GrSamplerState::MipmapMode mm,
349 GrQuadPerEdgeAA::Subset subset,
350 GrTextureOp::Saturate saturate)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500351 : fSwizzle(swizzle)
352 , fProxyCount(1)
353 , fTotalQuadCount(1)
Michael Ludwig4384f042019-12-05 10:30:35 -0500354 , fFilter(static_cast<uint16_t>(filter))
Brian Salomone69b9ef2020-07-22 11:18:06 -0400355 , fMipmapMode(static_cast<uint16_t>(mm))
Michael Ludwig4384f042019-12-05 10:30:35 -0500356 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
357 , fColorType(static_cast<uint16_t>(ColorType::kNone))
Brian Salomon2432d062020-04-16 20:48:09 -0400358 , fSubset(static_cast<uint16_t>(subset))
Michael Ludwig4384f042019-12-05 10:30:35 -0500359 , fSaturate(static_cast<uint16_t>(saturate)) {}
Michael Ludwigadb12e72019-12-04 16:19:18 -0500360
Michael Ludwig4384f042019-12-05 10:30:35 -0500361 GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
Michael Ludwigadb12e72019-12-04 16:19:18 -0500362 uint16_t fProxyCount;
363 // This will be >= fProxyCount, since a proxy may be drawn multiple times
364 uint16_t fTotalQuadCount;
365
Michael Ludwig4384f042019-12-05 10:30:35 -0500366 // These must be based on uint16_t to help MSVC's pack bitfields optimally
367 uint16_t fFilter : 2; // GrSamplerState::Filter
Brian Salomone69b9ef2020-07-22 11:18:06 -0400368 uint16_t fMipmapMode : 2; // GrSamplerState::MipmapMode
Michael Ludwig4384f042019-12-05 10:30:35 -0500369 uint16_t fAAType : 2; // GrAAType
370 uint16_t fColorType : 2; // GrQuadPerEdgeAA::ColorType
Brian Salomon2432d062020-04-16 20:48:09 -0400371 uint16_t fSubset : 1; // bool
Michael Ludwig4384f042019-12-05 10:30:35 -0500372 uint16_t fSaturate : 1; // bool
Brian Salomone69b9ef2020-07-22 11:18:06 -0400373 uint16_t fUnused : 6; // # of bits left before Metadata exceeds 8 bytes
Michael Ludwigadb12e72019-12-04 16:19:18 -0500374
375 GrSamplerState::Filter filter() const {
376 return static_cast<GrSamplerState::Filter>(fFilter);
377 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400378 GrSamplerState::MipmapMode mipmapMode() const {
379 return static_cast<GrSamplerState::MipmapMode>(fMipmapMode);
380 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500381 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
382 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
Brian Salomon2432d062020-04-16 20:48:09 -0400383 Subset subset() const { return static_cast<Subset>(fSubset); }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500384 GrTextureOp::Saturate saturate() const {
385 return static_cast<GrTextureOp::Saturate>(fSaturate);
386 }
387
388 static_assert(GrSamplerState::kFilterCount <= 4);
389 static_assert(kGrAATypeCount <= 4);
390 static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
391 };
Michael Ludwig4384f042019-12-05 10:30:35 -0500392 static_assert(sizeof(Metadata) == 8);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500393
Chris Daltondbb833b2020-03-17 12:15:46 -0600394 // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
395 // store the data in a separate struct in order to minimize the size of the TextureOp.
396 // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
397 // may want to re-evaluate whether this is still necessary.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400398 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600399 // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
400 // allocatePrePreparedVertices is also called.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400401 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600402 // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
403 // part of the flushState).
404 struct Desc {
405 VertexSpec fVertexSpec;
406 int fNumProxies = 0;
407 int fNumTotalQuads = 0;
Robert Phillips32803ff2019-10-23 08:26:08 -0400408
Chris Daltondbb833b2020-03-17 12:15:46 -0600409 // This member variable is only used by 'onPrePrepareDraws'.
410 char* fPrePreparedVertices = nullptr;
411
412 GrProgramInfo* fProgramInfo = nullptr;
413
414 sk_sp<const GrBuffer> fIndexBuffer;
415 sk_sp<const GrBuffer> fVertexBuffer;
416 int fBaseVertex;
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400417
418 // How big should 'fVertices' be to hold all the vertex data?
419 size_t totalSizeInBytes() const {
Chris Daltondbb833b2020-03-17 12:15:46 -0600420 return this->totalNumVertices() * fVertexSpec.vertexSize();
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400421 }
422
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400423 int totalNumVertices() const {
424 return fNumTotalQuads * fVertexSpec.verticesPerQuad();
425 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400426
Chris Daltondbb833b2020-03-17 12:15:46 -0600427 void allocatePrePreparedVertices(SkArenaAlloc* arena) {
428 fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400429 }
Robert Phillips32803ff2019-10-23 08:26:08 -0400430 };
Brian Salomon2432d062020-04-16 20:48:09 -0400431 // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
Greg Daniel549325c2019-10-30 16:19:20 -0400432 TextureOp(GrSurfaceProxyView proxyView,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400433 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
434 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400435 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400436 const SkPMColor4f& color,
437 GrTextureOp::Saturate saturate,
438 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500439 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400440 const SkRect* subsetRect)
Brian Salomon34169692017-08-28 15:32:01 -0400441 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400442 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400443 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600444 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400445 , fMetadata(proxyView.swizzle(), filter, mm, Subset(!!subsetRect), saturate) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400446 // Clean up disparities between the overall aa type and edge configuration and apply
447 // optimizations based on the rect and matrix when appropriate
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500448 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
449 &aaType, &quad->fEdgeFlags);
Michael Ludwig4384f042019-12-05 10:30:35 -0500450 fMetadata.fAAType = static_cast<uint16_t>(aaType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400451
Brian Salomonf1709042018-10-03 11:57:00 -0400452 // We expect our caller to have already caught this optimization.
Brian Salomon2432d062020-04-16 20:48:09 -0400453 SkASSERT(!subsetRect ||
454 !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500455
Brian Salomonf09abc52018-10-03 15:59:04 -0400456 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
John Stilescbe4e282020-06-01 10:38:31 -0400457 // Try to identify cases where the subsetting isn't actually necessary, and skip it.
458 if (subsetRect) {
459 if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
460 subsetRect = nullptr;
461 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
462 }
Brian Salomonf09abc52018-10-03 15:59:04 -0400463 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500464
Brian Salomon2432d062020-04-16 20:48:09 -0400465 // Normalize src coordinates and the subset (if set)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500466 NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
467 proxyView.origin());
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500468 normalize_src_quad(params, &quad->fLocal);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400469 SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500470
Michael Ludwig949ceb22020-02-07 10:14:45 -0500471 // Set bounds before clipping so we don't have to worry about unioning the bounds of
472 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500473 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -0400474 IsHairline::kNo);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500475
Brian Salomon2432d062020-04-16 20:48:09 -0400476 int quadCount = this->appendQuad(quad, color, subset);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500477 fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
Brian Salomond7065e72018-10-12 11:42:02 -0400478 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400479
Brian Salomoneebe7352020-12-09 16:37:04 -0500480 TextureOp(GrSurfaceDrawContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400481 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500482 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400483 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400484 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400485 GrTextureOp::Saturate saturate,
486 GrAAType aaType,
487 SkCanvas::SrcRectConstraint constraint,
488 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500489 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400490 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400491 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400492 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600493 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400494 , fMetadata(set[0].fProxyView.swizzle(),
495 GrSamplerState::Filter::kNearest,
496 GrSamplerState::MipmapMode::kNone,
497 Subset::kNo,
498 saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500499 // Update counts to reflect the batch op
Michael Ludwig379e4962019-12-06 13:21:26 -0500500 fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500501 fMetadata.fTotalQuadCount = SkToUInt(cnt);
502
Brian Salomond7065e72018-10-12 11:42:02 -0400503 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500504
505 GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon2432d062020-04-16 20:48:09 -0400506 Subset netSubset = Subset::kNo;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500507 GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400508 GrSamplerState::MipmapMode netMM = GrSamplerState::MipmapMode::kNone;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500509
Michael Ludwig379e4962019-12-06 13:21:26 -0500510 const GrSurfaceProxy* curProxy = nullptr;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500511
Michael Ludwig379e4962019-12-06 13:21:26 -0500512 // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
513 // increases when set[q]'s proxy changes.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500514 int p = 0;
515 for (int q = 0; q < cnt; ++q) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400516 SkASSERT(mm == GrSamplerState::MipmapMode::kNone ||
517 (set[0].fProxyView.proxy()->asTextureProxy()->mipmapped() ==
518 GrMipmapped::kYes));
Michael Ludwig379e4962019-12-06 13:21:26 -0500519 if (q == 0) {
Greg Daniel549325c2019-10-30 16:19:20 -0400520 // We do not placement new the first ViewCountPair since that one is allocated and
521 // initialized as part of the GrTextureOp creation.
Michael Ludwig379e4962019-12-06 13:21:26 -0500522 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
523 fViewCountPairs[0].fQuadCnt = 0;
524 curProxy = fViewCountPairs[0].fProxy.get();
525 } else if (set[q].fProxyView.proxy() != curProxy) {
Greg Daniel549325c2019-10-30 16:19:20 -0400526 // We must placement new the ViewCountPairs here so that the sk_sps in the
527 // GrSurfaceProxyView get initialized properly.
Michael Ludwig379e4962019-12-06 13:21:26 -0500528 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
Michael Ludwigadb12e72019-12-04 16:19:18 -0500529
Michael Ludwig379e4962019-12-06 13:21:26 -0500530 curProxy = fViewCountPairs[p].fProxy.get();
Greg Danielc71c7962020-01-14 16:44:18 -0500531 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
532 curProxy, fViewCountPairs[0].fProxy.get()));
Michael Ludwig379e4962019-12-06 13:21:26 -0500533 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
Michael Ludwig379e4962019-12-06 13:21:26 -0500534 } // else another quad referencing the same proxy
Michael Ludwigce62dec2019-02-19 11:48:46 -0500535
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500536 SkMatrix ctm = viewMatrix;
Michael Ludwig379e4962019-12-06 13:21:26 -0500537 if (set[q].fPreViewMatrix) {
538 ctm.preConcat(*set[q].fPreViewMatrix);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500539 }
540
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400541 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
542 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500543 DrawQuad quad;
Michael Ludwig379e4962019-12-06 13:21:26 -0500544 if (set[q].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500545 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400546
547 SkPoint srcPts[4];
Michael Ludwig379e4962019-12-06 13:21:26 -0500548 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500549 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400550 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500551 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
552 quad.fLocal = GrQuad(set[q].fSrcRect);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400553 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500554
Brian Salomone69b9ef2020-07-22 11:18:06 -0400555 if (netFilter != filter || netMM != mm) {
556 // The only way netFilter != filter is if linear is requested and we haven't yet
557 // found a quad that requires linear (so net is still nearest). Similar for mip
558 // mapping.
Brian Salomonf7353512020-07-22 19:26:48 -0400559 SkASSERT(filter == netFilter ||
560 (netFilter == GrSamplerState::Filter::kNearest && filter > netFilter));
561 SkASSERT(mm == netMM ||
562 (netMM == GrSamplerState::MipmapMode::kNone && mm > netMM));
Brian Salomone69b9ef2020-07-22 11:18:06 -0400563 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad.fLocal, quad.fDevice);
564 if (mustFilter && filter != GrSamplerState::Filter::kNearest) {
Brian Salomonf7353512020-07-22 19:26:48 -0400565 netFilter = filter;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400566 }
567 if (mustMM && mm != GrSamplerState::MipmapMode::kNone) {
Brian Salomonf7353512020-07-22 19:26:48 -0400568 netMM = mm;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400569 }
Michael Ludwig22429f92019-06-27 10:44:48 -0400570 }
571
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500572 // Update overall bounds of the op as the union of all quads
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500573 bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500574
575 // Determine the AA type for the quad, then merge with net AA type
Michael Ludwig6bee7762018-10-19 09:50:36 -0400576 GrAAType aaForQuad;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500577 GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
578 &aaForQuad, &quad.fEdgeFlags);
John Stilescbe4e282020-06-01 10:38:31 -0400579
Michael Ludwig6bee7762018-10-19 09:50:36 -0400580 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
581 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500582 if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
583 netAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400584 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400585
586 // Calculate metadata for the entry
Brian Salomon2432d062020-04-16 20:48:09 -0400587 const SkRect* subsetForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400588 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
John Stilescbe4e282020-06-01 10:38:31 -0400589 // Check (briefly) if the subset rect is actually needed for this set entry.
590 SkRect* subsetRect = &set[q].fSrcRect;
591 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
592 if (!safe_to_ignore_subset_rect(aaForQuad, filter, quad, *subsetRect)) {
593 netSubset = Subset::kYes;
594 subsetForQuad = subsetRect;
595 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400596 }
597 }
John Stilescbe4e282020-06-01 10:38:31 -0400598
599 // Normalize the src quads and apply origin
600 NormalizationParams proxyParams = proxy_normalization_params(
601 curProxy, set[q].fProxyView.origin());
602 normalize_src_quad(proxyParams, &quad.fLocal);
603
Brian Salomon2432d062020-04-16 20:48:09 -0400604 // This subset may represent a no-op, otherwise it will have the origin and dimensions
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500605 // of the texture applied to it. Insetting for bilinear filtering is deferred until
606 // on[Pre]Prepare so that the overall filter can be lazily determined.
Brian Salomon75cebbe2020-05-18 14:08:14 -0400607 SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500608
Michael Ludwig949ceb22020-02-07 10:14:45 -0500609 // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
610 // ViewCountPair (this frequently happens when Chrome draws 9-patches).
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400611 fViewCountPairs[p].fQuadCnt += this->appendQuad(&quad, set[q].fColor, subset);
Brian Salomond7065e72018-10-12 11:42:02 -0400612 }
Michael Ludwig406172a2019-12-06 14:05:19 -0500613 // The # of proxy switches should match what was provided (+1 because we incremented p
Michael Ludwig379e4962019-12-06 13:21:26 -0500614 // when a new proxy was encountered).
Michael Ludwig406172a2019-12-06 14:05:19 -0500615 SkASSERT((p + 1) == fMetadata.fProxyCount);
Michael Ludwig379e4962019-12-06 13:21:26 -0500616 SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500617
Michael Ludwig4384f042019-12-05 10:30:35 -0500618 fMetadata.fAAType = static_cast<uint16_t>(netAAType);
619 fMetadata.fFilter = static_cast<uint16_t>(netFilter);
Brian Salomon2432d062020-04-16 20:48:09 -0400620 fMetadata.fSubset = static_cast<uint16_t>(netSubset);
Brian Salomon34169692017-08-28 15:32:01 -0400621
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500622 this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage), IsHairline::kNo);
Brian Salomon17031a72018-05-22 14:14:07 -0400623 }
624
Brian Salomon2432d062020-04-16 20:48:09 -0400625 int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& subset) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500626 DrawQuad extra;
Michael Ludwig65299902021-05-13 12:02:23 -0400627 // Always clip to W0 to stay consistent with GrQuad::bounds
628 int quadCount = GrQuadUtils::ClipToW0(quad, &extra);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500629 if (quadCount == 0) {
630 // We can't discard the op at this point, but disable AA flags so it won't go through
631 // inset/outset processing
632 quad->fEdgeFlags = GrQuadAAFlags::kNone;
633 quadCount = 1;
634 }
Brian Salomon2432d062020-04-16 20:48:09 -0400635 fQuads.append(quad->fDevice, {color, subset, quad->fEdgeFlags}, &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500636 if (quadCount > 1) {
Brian Salomon2432d062020-04-16 20:48:09 -0400637 fQuads.append(extra.fDevice, {color, subset, extra.fEdgeFlags}, &extra.fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500638 fMetadata.fTotalQuadCount++;
639 }
640 return quadCount;
641 }
642
Robert Phillips2669a7b2020-03-12 12:07:19 -0400643 GrProgramInfo* programInfo() override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600644 // Although this Op implements its own onPrePrepareDraws it calls GrMeshDrawOps' version so
645 // this entry point will be called.
646 return (fDesc) ? fDesc->fProgramInfo : nullptr;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400647 }
648
Chris Daltondbb833b2020-03-17 12:15:46 -0600649 void onCreateProgramInfo(const GrCaps* caps,
650 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500651 const GrSurfaceProxyView& writeView,
Chris Daltondbb833b2020-03-17 12:15:46 -0600652 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400653 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500654 GrXferBarrierFlags renderPassXferBarriers,
655 GrLoadOp colorLoadOp) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600656 SkASSERT(fDesc);
657
658 GrGeometryProcessor* gp;
659
660 {
661 const GrBackendFormat& backendFormat =
662 fViewCountPairs[0].fProxy->backendFormat();
663
664 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
665 fMetadata.filter());
666
667 gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
668 arena, fDesc->fVertexSpec, *caps->shaderCaps(), backendFormat, samplerState,
669 fMetadata.fSwizzle, std::move(fTextureColorSpaceXform), fMetadata.saturate());
670
671 SkASSERT(fDesc->fVertexSpec.vertexSize() == gp->vertexStride());
672 }
673
674 auto pipelineFlags = (GrAAType::kMSAA == fMetadata.aaType()) ?
675 GrPipeline::InputFlags::kHWAntialias : GrPipeline::InputFlags::kNone;
676
677 fDesc->fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Brian Salomon8afde5f2020-04-01 16:22:00 -0400678 caps, arena, writeView, std::move(appliedClip), dstProxyView, gp,
Chris Daltondbb833b2020-03-17 12:15:46 -0600679 GrProcessorSet::MakeEmptySet(), fDesc->fVertexSpec.primitiveType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500680 renderPassXferBarriers, colorLoadOp, pipelineFlags);
Robert Phillips4133dc42020-03-11 15:55:55 -0400681 }
682
Robert Phillipsdf70f152019-11-15 14:57:05 -0500683 void onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -0500684 const GrSurfaceProxyView& writeView,
Robert Phillips8053c972019-11-21 10:44:53 -0500685 GrAppliedClip* clip,
Greg Danield358cbe2020-09-11 09:33:54 -0400686 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500687 GrXferBarrierFlags renderPassXferBarriers,
688 GrLoadOp colorLoadOp) override {
Robert Phillips61fc7992019-10-22 11:58:17 -0400689 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips29f38542019-10-16 09:20:25 -0400690
Robert Phillips61fc7992019-10-22 11:58:17 -0400691 SkDEBUGCODE(this->validate();)
Chris Daltondbb833b2020-03-17 12:15:46 -0600692 SkASSERT(!fDesc);
Robert Phillips61fc7992019-10-22 11:58:17 -0400693
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500694 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillips61fc7992019-10-22 11:58:17 -0400695
Chris Daltondbb833b2020-03-17 12:15:46 -0600696 fDesc = arena->make<Desc>();
697 this->characterize(fDesc);
698 fDesc->allocatePrePreparedVertices(arena);
699 FillInVertices(*context->priv().caps(), this, fDesc, fDesc->fPrePreparedVertices);
Robert Phillips61fc7992019-10-22 11:58:17 -0400700
Chris Daltondbb833b2020-03-17 12:15:46 -0600701 // This will call onCreateProgramInfo and register the created program with the DDL.
Greg Danield358cbe2020-09-11 09:33:54 -0400702 this->INHERITED::onPrePrepareDraws(context, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500703 renderPassXferBarriers, colorLoadOp);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400704 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400705
Chris Daltondbb833b2020-03-17 12:15:46 -0600706 static void FillInVertices(const GrCaps& caps, TextureOp* texOp, Desc* desc, char* vertexData) {
707 SkASSERT(vertexData);
708
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400709 int totQuadsSeen = 0;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400710 SkDEBUGCODE(int totVerticesSeen = 0;)
Michael Ludwig189c9802019-11-21 11:21:12 -0500711 SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400712
Chris Daltondbb833b2020-03-17 12:15:46 -0600713 GrQuadPerEdgeAA::Tessellator tessellator(desc->fVertexSpec, vertexData);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400714 for (const auto& op : ChainRange<TextureOp>(texOp)) {
715 auto iter = op.fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500716 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500717 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
718 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400719
Chris Daltondbb833b2020-03-17 12:15:46 -0600720 for (int i = 0; i < quadCnt && iter.next(); ++i) {
721 SkASSERT(iter.isLocalValid());
Brian Salomon2432d062020-04-16 20:48:09 -0400722 const ColorSubsetAndAA& info = iter.metadata();
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500723
Chris Daltondbb833b2020-03-17 12:15:46 -0600724 tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
Brian Salomon75cebbe2020-05-18 14:08:14 -0400725 info.fSubsetRect, info.aaFlags());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400726 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400727
Chris Daltondbb833b2020-03-17 12:15:46 -0600728 SkASSERT((totVerticesSeen + meshVertexCnt) * vertexSize
729 == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400730
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400731 totQuadsSeen += quadCnt;
732 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
733 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400734 }
735
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400736 // If quad counts per proxy were calculated correctly, the entire iterator
737 // should have been consumed.
Chris Daltondbb833b2020-03-17 12:15:46 -0600738 SkASSERT(!iter.next());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400739 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400740
Chris Daltondbb833b2020-03-17 12:15:46 -0600741 SkASSERT(desc->totalSizeInBytes() == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400742 SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
743 SkASSERT(totVerticesSeen == desc->totalNumVertices());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400744 }
745
Robert Phillips29f38542019-10-16 09:20:25 -0400746#ifdef SK_DEBUG
Robert Phillips6bf11252020-07-31 12:15:00 -0400747 static int validate_op(GrTextureType textureType,
748 GrAAType aaType,
749 GrSwizzle swizzle,
750 const TextureOp* op) {
751 SkASSERT(op->fMetadata.fSwizzle == swizzle);
752
753 int quadCount = 0;
754 for (unsigned p = 0; p < op->fMetadata.fProxyCount; ++p) {
755 auto* proxy = op->fViewCountPairs[p].fProxy->asTextureProxy();
756 quadCount += op->fViewCountPairs[p].fQuadCnt;
757 SkASSERT(proxy);
758 SkASSERT(proxy->textureType() == textureType);
759 }
760
761 SkASSERT(aaType == op->fMetadata.aaType());
762 return quadCount;
763 }
764
Robert Phillips29f38542019-10-16 09:20:25 -0400765 void validate() const override {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500766 // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
Michael Ludwigadb12e72019-12-04 16:19:18 -0500767 auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
768 GrAAType aaType = fMetadata.aaType();
Robert Phillips6bf11252020-07-31 12:15:00 -0400769 GrSwizzle swizzle = fMetadata.fSwizzle;
Robert Phillips29f38542019-10-16 09:20:25 -0400770
Robert Phillips6bf11252020-07-31 12:15:00 -0400771 int quadCount = validate_op(textureType, aaType, swizzle, this);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500772
Robert Phillips6bf11252020-07-31 12:15:00 -0400773 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
774 quadCount += validate_op(textureType, aaType, swizzle,
775 static_cast<const TextureOp*>(tmp));
776 }
Robert Phillips29f38542019-10-16 09:20:25 -0400777
Robert Phillips6bf11252020-07-31 12:15:00 -0400778 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
779 quadCount += validate_op(textureType, aaType, swizzle,
780 static_cast<const TextureOp*>(tmp));
Robert Phillips29f38542019-10-16 09:20:25 -0400781 }
Robert Phillipse837e612019-11-15 11:02:50 -0500782
783 SkASSERT(quadCount == this->numChainedQuads());
Robert Phillips29f38542019-10-16 09:20:25 -0400784 }
Robert Phillips6bf11252020-07-31 12:15:00 -0400785
Robert Phillips29f38542019-10-16 09:20:25 -0400786#endif
787
Robert Phillipse837e612019-11-15 11:02:50 -0500788#if GR_TEST_UTILS
789 int numQuads() const final { return this->totNumQuads(); }
790#endif
791
Chris Daltondbb833b2020-03-17 12:15:46 -0600792 void characterize(Desc* desc) const {
Robert Phillips6bf11252020-07-31 12:15:00 -0400793 SkDEBUGCODE(this->validate();)
794
Robert Phillips29f38542019-10-16 09:20:25 -0400795 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
796 ColorType colorType = ColorType::kNone;
797 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomon2432d062020-04-16 20:48:09 -0400798 Subset subset = Subset::kNo;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500799 GrAAType overallAAType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400800
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400801 desc->fNumProxies = 0;
802 desc->fNumTotalQuads = 0;
803 int maxQuadsPerMesh = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400804
Brian Salomonf7232642018-09-19 08:58:08 -0400805 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400806 if (op.fQuads.deviceQuadType() > quadType) {
807 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500808 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400809 if (op.fQuads.localQuadType() > srcQuadType) {
810 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500811 }
Brian Salomon2432d062020-04-16 20:48:09 -0400812 if (op.fMetadata.subset() == Subset::kYes) {
813 subset = Subset::kYes;
Brian Salomonf7232642018-09-19 08:58:08 -0400814 }
Brian Osman788b9162020-02-07 10:36:46 -0500815 colorType = std::max(colorType, op.fMetadata.colorType());
Michael Ludwigadb12e72019-12-04 16:19:18 -0500816 desc->fNumProxies += op.fMetadata.fProxyCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400817
Michael Ludwigadb12e72019-12-04 16:19:18 -0500818 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Brian Osman788b9162020-02-07 10:36:46 -0500819 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
Brian Salomonf7232642018-09-19 08:58:08 -0400820 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400821 desc->fNumTotalQuads += op.totNumQuads();
822
Michael Ludwigadb12e72019-12-04 16:19:18 -0500823 if (op.fMetadata.aaType() == GrAAType::kCoverage) {
Robert Phillips29f38542019-10-16 09:20:25 -0400824 overallAAType = GrAAType::kCoverage;
Brian Salomonae7d7702018-10-14 15:05:45 -0400825 }
Brian Salomon34169692017-08-28 15:32:01 -0400826 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400827
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400828 SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
829
830 SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
831
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400832 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(overallAAType,
833 maxQuadsPerMesh);
834
835 desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
Brian Salomon2432d062020-04-16 20:48:09 -0400836 subset, overallAAType, /* alpha as coverage */ true,
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400837 indexBufferOption);
Robert Phillipse837e612019-11-15 11:02:50 -0500838
839 SkASSERT(desc->fNumTotalQuads <= GrQuadPerEdgeAA::QuadLimit(indexBufferOption));
Robert Phillips29f38542019-10-16 09:20:25 -0400840 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500841
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400842 int totNumQuads() const {
843#ifdef SK_DEBUG
844 int tmp = 0;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500845 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400846 tmp += fViewCountPairs[p].fQuadCnt;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400847 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500848 SkASSERT(tmp == fMetadata.fTotalQuadCount);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400849#endif
850
Michael Ludwigadb12e72019-12-04 16:19:18 -0500851 return fMetadata.fTotalQuadCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400852 }
853
854 int numChainedQuads() const {
855 int numChainedQuads = this->totNumQuads();
856
857 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
858 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
859 }
860
861 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
862 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
863 }
864
865 return numChainedQuads;
866 }
867
Robert Phillips29f38542019-10-16 09:20:25 -0400868 // onPrePrepareDraws may or may not have been called at this point
869 void onPrepareDraws(Target* target) override {
870 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500871
Robert Phillips29f38542019-10-16 09:20:25 -0400872 SkDEBUGCODE(this->validate();)
873
Chris Daltondbb833b2020-03-17 12:15:46 -0600874 SkASSERT(!fDesc || fDesc->fPrePreparedVertices);
Robert Phillips29f38542019-10-16 09:20:25 -0400875
Chris Daltondbb833b2020-03-17 12:15:46 -0600876 if (!fDesc) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400877 SkArenaAlloc* arena = target->allocator();
Chris Daltondbb833b2020-03-17 12:15:46 -0600878 fDesc = arena->make<Desc>();
879 this->characterize(fDesc);
880 SkASSERT(!fDesc->fPrePreparedVertices);
Brian Salomonf7232642018-09-19 08:58:08 -0400881 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400882
Chris Daltondbb833b2020-03-17 12:15:46 -0600883 size_t vertexSize = fDesc->fVertexSpec.vertexSize();
Brian Salomon92be2f72018-06-19 14:33:47 -0400884
Chris Daltondbb833b2020-03-17 12:15:46 -0600885 void* vdata = target->makeVertexSpace(vertexSize, fDesc->totalNumVertices(),
886 &fDesc->fVertexBuffer, &fDesc->fBaseVertex);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400887 if (!vdata) {
888 SkDebugf("Could not allocate vertices\n");
889 return;
Brian Salomon34169692017-08-28 15:32:01 -0400890 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400891
Chris Daltondbb833b2020-03-17 12:15:46 -0600892 if (fDesc->fVertexSpec.needsIndexBuffer()) {
893 fDesc->fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(
894 target, fDesc->fVertexSpec.indexBufferOption());
895 if (!fDesc->fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400896 SkDebugf("Could not allocate indices\n");
897 return;
898 }
899 }
900
Chris Daltondbb833b2020-03-17 12:15:46 -0600901 if (fDesc->fPrePreparedVertices) {
902 memcpy(vdata, fDesc->fPrePreparedVertices, fDesc->totalSizeInBytes());
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400903 } else {
Chris Daltondbb833b2020-03-17 12:15:46 -0600904 FillInVertices(target->caps(), this, fDesc, (char*) vdata);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400905 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700906 }
907
908 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600909 if (!fDesc->fVertexBuffer) {
910 return;
911 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500912
Chris Daltondbb833b2020-03-17 12:15:46 -0600913 if (fDesc->fVertexSpec.needsIndexBuffer() && !fDesc->fIndexBuffer) {
914 return;
915 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500916
Chris Daltondbb833b2020-03-17 12:15:46 -0600917 if (!fDesc->fProgramInfo) {
918 this->createProgramInfo(flushState);
919 SkASSERT(fDesc->fProgramInfo);
920 }
921
922 flushState->bindPipelineAndScissorClip(*fDesc->fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400923 flushState->bindBuffers(std::move(fDesc->fIndexBuffer), nullptr,
924 std::move(fDesc->fVertexBuffer));
Chris Daltondbb833b2020-03-17 12:15:46 -0600925
926 int totQuadsSeen = 0;
927 SkDEBUGCODE(int numDraws = 0;)
928 for (const auto& op : ChainRange<TextureOp>(this)) {
929 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
930 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
931 SkASSERT(numDraws < fDesc->fNumProxies);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400932 flushState->bindTextures(fDesc->fProgramInfo->geomProc(),
Chris Daltondbb833b2020-03-17 12:15:46 -0600933 *op.fViewCountPairs[p].fProxy,
934 fDesc->fProgramInfo->pipeline());
935 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(),
936 fDesc->fVertexSpec, totQuadsSeen, quadCnt,
937 fDesc->totalNumVertices(), fDesc->fBaseVertex);
938 totQuadsSeen += quadCnt;
939 SkDEBUGCODE(++numDraws;)
940 }
941 }
942
943 SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
944 SkASSERT(numDraws == fDesc->fNumProxies);
Brian Salomon34169692017-08-28 15:32:01 -0400945 }
946
Robert Phillips6bf11252020-07-31 12:15:00 -0400947 void propagateCoverageAAThroughoutChain() {
948 fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
949
950 for (GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
951 TextureOp* tex = static_cast<TextureOp*>(tmp);
952 SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
953 tex->fMetadata.aaType() == GrAAType::kNone);
954 tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
955 }
956
957 for (GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
958 TextureOp* tex = static_cast<TextureOp*>(tmp);
959 SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
960 tex->fMetadata.aaType() == GrAAType::kNone);
961 tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
962 }
963 }
964
Herb Derbye25c3002020-10-27 15:57:27 -0400965 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400966 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips089b7c92020-08-12 11:57:07 -0400967 auto* that = t->cast<TextureOp>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400968
Robert Phillips6bf11252020-07-31 12:15:00 -0400969 SkDEBUGCODE(this->validate();)
970 SkDEBUGCODE(that->validate();)
971
Chris Daltondbb833b2020-03-17 12:15:46 -0600972 if (fDesc || that->fDesc) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400973 // This should never happen (since only DDL recorded ops should be prePrepared)
974 // but, in any case, we should never combine ops that that been prePrepared
975 return CombineResult::kCannotCombine;
976 }
977
Brian Salomon2432d062020-04-16 20:48:09 -0400978 if (fMetadata.subset() != that->fMetadata.subset()) {
979 // It is technically possible to combine operations across subset modes, but performance
Michael Ludwig2929f512019-04-19 13:05:56 -0400980 // testing suggests it's better to make more draw calls where some take advantage of
981 // the more optimal shader path without coordinate clamping.
982 return CombineResult::kCannotCombine;
983 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400984 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
985 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000986 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400987 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400988
Brian Salomonae7d7702018-10-14 15:05:45 -0400989 bool upgradeToCoverageAAOnMerge = false;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500990 if (fMetadata.aaType() != that->fMetadata.aaType()) {
991 if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400992 return CombineResult::kCannotCombine;
993 }
994 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500995 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400996
Michael Ludwigadb12e72019-12-04 16:19:18 -0500997 if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400998 this->numChainedQuads() + that->numChainedQuads())) {
999 return CombineResult::kCannotCombine;
Robert Phillipsb69001f2019-10-29 12:16:35 -04001000 }
1001
Michael Ludwigadb12e72019-12-04 16:19:18 -05001002 if (fMetadata.saturate() != that->fMetadata.saturate()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001003 return CombineResult::kCannotCombine;
1004 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001005 if (fMetadata.filter() != that->fMetadata.filter()) {
Brian Salomonf7232642018-09-19 08:58:08 -04001006 return CombineResult::kCannotCombine;
1007 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001008 if (fMetadata.mipmapMode() != that->fMetadata.mipmapMode()) {
1009 return CombineResult::kCannotCombine;
1010 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001011 if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
1012 return CombineResult::kCannotCombine;
1013 }
1014 const auto* thisProxy = fViewCountPairs[0].fProxy.get();
1015 const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
1016 if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
1017 thisProxy != thatProxy) {
Brian Salomon588cec72018-11-14 13:56:37 -05001018 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -05001019 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Robert Phillips6bf11252020-07-31 12:15:00 -04001020 caps.dynamicStateArrayGeometryProcessorTextureSupport() &&
1021 fMetadata.aaType() == that->fMetadata.aaType()) {
1022 // We only allow chaining when the aaTypes match bc otherwise the AA type
1023 // reported by the chain can be inconsistent. That is, since chaining doesn't
1024 // propagate revised AA information throughout the chain, the head of the chain
1025 // could have an AA setting of kNone while the chain as a whole could have a
1026 // setting of kCoverage. This inconsistency would then interfere with the validity
1027 // of the CombinedQuadCountWillOverflow calls.
1028 // This problem doesn't occur w/ merging bc we do propagate the AA information
1029 // (in propagateCoverageAAThroughoutChain) below.
Brian Salomonf7232642018-09-19 08:58:08 -04001030 return CombineResult::kMayChain;
1031 }
Brian Salomon7eae3e02018-08-07 14:02:38 +00001032 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001033 }
Michael Ludwig009b92e2019-02-15 16:03:53 -05001034
Brian Salomon2432d062020-04-16 20:48:09 -04001035 fMetadata.fSubset |= that->fMetadata.fSubset;
Brian Osman788b9162020-02-07 10:36:46 -05001036 fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
Michael Ludwig009b92e2019-02-15 16:03:53 -05001037
Michael Ludwig425eb452019-06-27 10:13:27 -04001038 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -05001039 fQuads.concat(that->fQuads);
Greg Daniel549325c2019-10-30 16:19:20 -04001040 fViewCountPairs[0].fQuadCnt += that->fQuads.count();
Michael Ludwigadb12e72019-12-04 16:19:18 -05001041 fMetadata.fTotalQuadCount += that->fQuads.count();
Michael Ludwig009b92e2019-02-15 16:03:53 -05001042
Robert Phillips6bf11252020-07-31 12:15:00 -04001043 if (upgradeToCoverageAAOnMerge) {
Robert Phillips089b7c92020-08-12 11:57:07 -04001044 // This merger may be the start of a concatenation of two chains. When one
1045 // of the chains mutates its AA the other must follow suit or else the above AA
1046 // check may prevent later ops from chaining together. A specific example of this is
1047 // when chain2 is prepended onto chain1:
1048 // chain1 (that): opA (non-AA/mergeable) opB (non-AA/non-mergeable)
1049 // chain2 (this): opC (cov-AA/non-mergeable) opD (cov-AA/mergeable)
1050 // W/o this propagation, after opD & opA merge, opB and opC would say they couldn't
1051 // chain - which would stop the concatenation process.
Robert Phillips6bf11252020-07-31 12:15:00 -04001052 this->propagateCoverageAAThroughoutChain();
Robert Phillips089b7c92020-08-12 11:57:07 -04001053 that->propagateCoverageAAThroughoutChain();
Robert Phillips6bf11252020-07-31 12:15:00 -04001054 }
1055
1056 SkDEBUGCODE(this->validate();)
1057
Brian Salomon7eae3e02018-08-07 14:02:38 +00001058 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -04001059 }
John Stilesaf366522020-08-13 09:57:34 -04001060
1061#if GR_TEST_UTILS
1062 SkString onDumpInfo() const override {
1063 SkString str = SkStringPrintf("# draws: %d\n", fQuads.count());
1064 auto iter = fQuads.iterator();
1065 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Robert Phillips047d5bb2021-01-08 13:39:19 -05001066 SkString proxyStr = fViewCountPairs[p].fProxy->dump();
1067 str.append(proxyStr);
1068 str.appendf(", Filter: %d, MM: %d\n",
John Stilesaf366522020-08-13 09:57:34 -04001069 static_cast<int>(fMetadata.fFilter),
1070 static_cast<int>(fMetadata.fMipmapMode));
Robert Phillips047d5bb2021-01-08 13:39:19 -05001071 for (int i = 0; i < fViewCountPairs[p].fQuadCnt && iter.next(); ++i) {
John Stilesaf366522020-08-13 09:57:34 -04001072 const GrQuad* quad = iter.deviceQuad();
1073 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
1074 const ColorSubsetAndAA& info = iter.metadata();
1075 str.appendf(
1076 "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
1077 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
1078 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
1079 i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
1080 info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
1081 quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
1082 quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
1083 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
1084 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
John Stilesaf366522020-08-13 09:57:34 -04001085 }
1086 }
1087 return str;
1088 }
1089#endif
1090
Brian Salomon2432d062020-04-16 20:48:09 -04001091 GrQuadBuffer<ColorSubsetAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -04001092 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Chris Daltondbb833b2020-03-17 12:15:46 -06001093 // Most state of TextureOp is packed into these two field to minimize the op's size.
Michael Ludwigadb12e72019-12-04 16:19:18 -05001094 // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1095 // consider/measure changes with care.
Chris Daltondbb833b2020-03-17 12:15:46 -06001096 Desc* fDesc;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001097 Metadata fMetadata;
Robert Phillips32803ff2019-10-23 08:26:08 -04001098
1099 // This field must go last. When allocating this op, we will allocate extra space to hold
Greg Daniel549325c2019-10-30 16:19:20 -04001100 // additional ViewCountPairs immediately after the op's allocation so we can treat this
Robert Phillips32803ff2019-10-23 08:26:08 -04001101 // as an fProxyCnt-length array.
Greg Daniel549325c2019-10-30 16:19:20 -04001102 ViewCountPair fViewCountPairs[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -04001103
John Stiles7571f9e2020-09-02 22:42:33 -04001104 using INHERITED = GrMeshDrawOp;
Brian Salomon34169692017-08-28 15:32:01 -04001105};
1106
1107} // anonymous namespace
1108
Robert Phillipse837e612019-11-15 11:02:50 -05001109#if GR_TEST_UTILS
1110uint32_t GrTextureOp::ClassID() {
1111 return TextureOp::ClassID();
1112}
1113#endif
Brian Salomon34169692017-08-28 15:32:01 -04001114
Herb Derbyc76d4092020-10-07 16:46:15 -04001115GrOp::Owner GrTextureOp::Make(GrRecordingContext* context,
1116 GrSurfaceProxyView proxyView,
1117 SkAlphaType alphaType,
1118 sk_sp<GrColorSpaceXform> textureXform,
1119 GrSamplerState::Filter filter,
1120 GrSamplerState::MipmapMode mm,
1121 const SkPMColor4f& color,
1122 Saturate saturate,
1123 SkBlendMode blendMode,
1124 GrAAType aaType,
1125 DrawQuad* quad,
1126 const SkRect* subset) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001127 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
Brian Salomon2432d062020-04-16 20:48:09 -04001128 if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1129 // No need for a shader-based subset if hardware clamping achieves the same effect
1130 subset = nullptr;
Michael Ludwig22429f92019-06-27 10:44:48 -04001131 }
1132
Brian Salomone69b9ef2020-07-22 11:18:06 -04001133 if (filter != GrSamplerState::Filter::kNearest || mm != GrSamplerState::MipmapMode::kNone) {
1134 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad->fLocal, quad->fDevice);
1135 if (!mustFilter) {
1136 filter = GrSamplerState::Filter::kNearest;
1137 }
1138 if (!mustMM) {
1139 mm = GrSamplerState::MipmapMode::kNone;
1140 }
Michael Ludwig22429f92019-06-27 10:44:48 -04001141 }
1142
1143 if (blendMode == SkBlendMode::kSrcOver) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001144 return TextureOp::Make(context, std::move(proxyView), std::move(textureXform), filter, mm,
Brian Salomon2432d062020-04-16 20:48:09 -04001145 color, saturate, aaType, std::move(quad), subset);
Michael Ludwig22429f92019-06-27 10:44:48 -04001146 } else {
1147 // Emulate complex blending using GrFillRectOp
Brian Salomonb030b1f2020-11-23 15:40:55 -05001148 GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, filter, mm);
Michael Ludwig22429f92019-06-27 10:44:48 -04001149 GrPaint paint;
1150 paint.setColor4f(color);
1151 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1152
1153 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomonb030b1f2020-11-23 15:40:55 -05001154 const auto& caps = *context->priv().caps();
Brian Salomon2432d062020-04-16 20:48:09 -04001155 if (subset) {
Brian Salomonca6b2f42020-01-24 11:31:21 -05001156 SkRect localRect;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001157 if (quad->fLocal.asRect(&localRect)) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001158 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
Brian Salomonb030b1f2020-11-23 15:40:55 -05001159 samplerState, *subset, localRect, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001160 } else {
John Stiles5a2a7b32020-06-04 10:57:21 -04001161 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
Brian Salomonb030b1f2020-11-23 15:40:55 -05001162 samplerState, *subset, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001163 }
1164 } else {
Brian Salomonb030b1f2020-11-23 15:40:55 -05001165 fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), samplerState,
1166 caps);
Michael Ludwig22429f92019-06-27 10:44:48 -04001167 }
1168 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
Brian Osman958a3bb2020-07-30 14:13:23 -04001169 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001170 if (saturate == GrTextureOp::Saturate::kYes) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001171 fp = GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/false);
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001172 }
John Stiles5933d7d2020-07-21 12:28:35 -04001173 paint.setColorFragmentProcessor(std::move(fp));
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001174 return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
Michael Ludwig22429f92019-06-27 10:44:48 -04001175 }
1176}
1177
Robert Phillipse837e612019-11-15 11:02:50 -05001178// A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1179class GrTextureOp::BatchSizeLimiter {
1180public:
Brian Salomoneebe7352020-12-09 16:37:04 -05001181 BatchSizeLimiter(GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001182 const GrClip* clip,
Robert Phillipse837e612019-11-15 11:02:50 -05001183 GrRecordingContext* context,
1184 int numEntries,
1185 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001186 GrSamplerState::MipmapMode mm,
Robert Phillipse837e612019-11-15 11:02:50 -05001187 GrTextureOp::Saturate saturate,
1188 SkCanvas::SrcRectConstraint constraint,
1189 const SkMatrix& viewMatrix,
1190 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
1191 : fRTC(rtc)
1192 , fClip(clip)
1193 , fContext(context)
1194 , fFilter(filter)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001195 , fMipmapMode(mm)
Robert Phillipse837e612019-11-15 11:02:50 -05001196 , fSaturate(saturate)
1197 , fConstraint(constraint)
1198 , fViewMatrix(viewMatrix)
1199 , fTextureColorSpaceXform(textureColorSpaceXform)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001200 , fNumLeft(numEntries) {}
Brian Salomon34169692017-08-28 15:32:01 -04001201
Brian Salomoneebe7352020-12-09 16:37:04 -05001202 void createOp(GrSurfaceDrawContext::TextureSetEntry set[],
Robert Phillipse837e612019-11-15 11:02:50 -05001203 int clumpSize,
1204 GrAAType aaType) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001205 int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
Herb Derbyc76d4092020-10-07 16:46:15 -04001206 GrOp::Owner op = TextureOp::Make(fContext,
1207 &set[fNumClumped],
1208 clumpSize,
1209 clumpProxyCount,
1210 fFilter,
1211 fMipmapMode,
1212 fSaturate,
1213 aaType,
1214 fConstraint,
1215 fViewMatrix,
1216 fTextureColorSpaceXform);
Robert Phillipse837e612019-11-15 11:02:50 -05001217 fRTC->addDrawOp(fClip, std::move(op));
1218
1219 fNumLeft -= clumpSize;
1220 fNumClumped += clumpSize;
1221 }
1222
1223 int numLeft() const { return fNumLeft; }
1224 int baseIndex() const { return fNumClumped; }
1225
1226private:
Brian Salomoneebe7352020-12-09 16:37:04 -05001227 GrSurfaceDrawContext* fRTC;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001228 const GrClip* fClip;
Robert Phillipse837e612019-11-15 11:02:50 -05001229 GrRecordingContext* fContext;
1230 GrSamplerState::Filter fFilter;
Brian Salomone69b9ef2020-07-22 11:18:06 -04001231 GrSamplerState::MipmapMode fMipmapMode;
Robert Phillipse837e612019-11-15 11:02:50 -05001232 GrTextureOp::Saturate fSaturate;
1233 SkCanvas::SrcRectConstraint fConstraint;
1234 const SkMatrix& fViewMatrix;
1235 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
1236
1237 int fNumLeft;
1238 int fNumClumped = 0; // also the offset for the start of the next clump
1239};
1240
1241// Greedily clump quad draws together until the index buffer limit is exceeded.
Brian Salomoneebe7352020-12-09 16:37:04 -05001242void GrTextureOp::AddTextureSetOps(GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001243 const GrClip* clip,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001244 GrRecordingContext* context,
Brian Salomoneebe7352020-12-09 16:37:04 -05001245 GrSurfaceDrawContext::TextureSetEntry set[],
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001246 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -05001247 int proxyRunCnt,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001248 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001249 GrSamplerState::MipmapMode mm,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001250 Saturate saturate,
1251 SkBlendMode blendMode,
1252 GrAAType aaType,
1253 SkCanvas::SrcRectConstraint constraint,
1254 const SkMatrix& viewMatrix,
1255 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001256 // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1257 // the op's metadata so we don't need to worry about overflow.
Michael Ludwig4ef1ca12019-12-19 10:58:52 -05001258 SkDEBUGCODE(TextureOp::ValidateResourceLimits();)
Michael Ludwig379e4962019-12-06 13:21:26 -05001259 SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1260
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001261 // First check if we can support batches as a single op
1262 if (blendMode != SkBlendMode::kSrcOver ||
1263 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1264 // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1265 // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1266 // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1267 SkMatrix ctm;
1268 for (int i = 0; i < cnt; ++i) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001269 ctm = viewMatrix;
1270 if (set[i].fPreViewMatrix) {
1271 ctm.preConcat(*set[i].fPreViewMatrix);
1272 }
Robert Phillipse837e612019-11-15 11:02:50 -05001273
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001274 DrawQuad quad;
1275 quad.fEdgeFlags = set[i].fAAFlags;
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001276 if (set[i].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001277 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001278
1279 SkPoint srcPts[4];
1280 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001281 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001282 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001283 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1284 quad.fLocal = GrQuad(set[i].fSrcRect);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001285 }
1286
Brian Salomon2432d062020-04-16 20:48:09 -04001287 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001288 ? &set[i].fSrcRect : nullptr;
1289
Brian Salomonfc118442019-11-22 19:09:27 -05001290 auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001291 filter, mm, set[i].fColor, saturate, blendMode, aaType, &quad, subset);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001292 rtc->addDrawOp(clip, std::move(op));
1293 }
1294 return;
1295 }
1296
1297 // Second check if we can always just make a single op and avoid the extra iteration
Robert Phillipse837e612019-11-15 11:02:50 -05001298 // needed to clump things together.
Brian Osman788b9162020-02-07 10:36:46 -05001299 if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
Robert Phillipse837e612019-11-15 11:02:50 -05001300 GrResourceProvider::MaxNumAAQuads())) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001301 auto op = TextureOp::Make(context, set, cnt, proxyRunCnt, filter, mm, saturate, aaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001302 constraint, viewMatrix, std::move(textureColorSpaceXform));
1303 rtc->addDrawOp(clip, std::move(op));
1304 return;
1305 }
1306
Brian Salomone69b9ef2020-07-22 11:18:06 -04001307 BatchSizeLimiter state(rtc, clip, context, cnt, filter, mm, saturate, constraint, viewMatrix,
Robert Phillipse837e612019-11-15 11:02:50 -05001308 std::move(textureColorSpaceXform));
1309
1310 // kNone and kMSAA never get altered
1311 if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1312 // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1313 while (state.numLeft() > 0) {
Brian Osman788b9162020-02-07 10:36:46 -05001314 int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
Robert Phillipse837e612019-11-15 11:02:50 -05001315
1316 state.createOp(set, clumpSize, aaType);
1317 }
1318 } else {
1319 // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1320 // can also get downgraded to kNone if all the quads are on integer coordinates and
1321 // axis-aligned.
1322 SkASSERT(aaType == GrAAType::kCoverage);
1323
1324 while (state.numLeft() > 0) {
1325 GrAAType runningAA = GrAAType::kNone;
1326 bool clumped = false;
1327
1328 for (int i = 0; i < state.numLeft(); ++i) {
1329 int absIndex = state.baseIndex() + i;
1330
Robert Phillips6bf11252020-07-31 12:15:00 -04001331 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone ||
1332 runningAA == GrAAType::kCoverage) {
Robert Phillipse837e612019-11-15 11:02:50 -05001333
1334 if (i >= GrResourceProvider::MaxNumAAQuads()) {
1335 // Here we either need to boost the AA type to kCoverage, but doing so with
1336 // all the accumulated quads would overflow, or we have a set of AA quads
1337 // that has just gotten too large. In either case, calve off the existing
1338 // quads as their own TextureOp.
1339 state.createOp(
1340 set,
1341 runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1342 runningAA); // maybe downgrading AA here
1343 clumped = true;
1344 break;
1345 }
1346
1347 runningAA = GrAAType::kCoverage;
1348 } else if (runningAA == GrAAType::kNone) {
1349
1350 if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1351 // Here we've found a consistent batch of non-AA quads that has gotten too
1352 // large. Calve it off as its own GrTextureOp.
1353 state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1354 GrAAType::kNone); // definitely downgrading AA here
1355 clumped = true;
1356 break;
1357 }
1358 }
1359 }
1360
1361 if (!clumped) {
1362 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1363 // quads and call it a day.
1364 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1365 }
1366 }
1367 }
1368}
Robert Phillipsae01f622019-11-13 15:56:31 +00001369
Brian Salomon34169692017-08-28 15:32:01 -04001370#if GR_TEST_UTILS
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04001371#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001372#include "src/gpu/GrProxyProvider.h"
1373#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -04001374
1375GR_DRAW_OP_TEST_DEFINE(TextureOp) {
Brian Salomona56a7462020-02-07 14:17:25 -05001376 SkISize dims;
1377 dims.fHeight = random->nextULessThan(90) + 10;
1378 dims.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001379 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001380 GrMipmapped mipMapped = random->nextBool() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel09c94002018-06-08 22:11:51 +00001381 SkBackingFit fit = SkBackingFit::kExact;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001382 if (mipMapped == GrMipmapped::kNo) {
Greg Daniel09c94002018-06-08 22:11:51 +00001383 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1384 }
Greg Daniel4065d452018-11-16 15:43:41 -05001385 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -04001386 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1387 GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -05001388 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -04001389 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001390 format, dims, GrRenderable::kNo, 1, mipMapped, fit, SkBudgeted::kNo, GrProtected::kNo,
1391 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001392
Brian Salomon34169692017-08-28 15:32:01 -04001393 SkRect rect = GrTest::TestRect(random);
1394 SkRect srcRect;
1395 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1396 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1397 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1398 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1399 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -05001400 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001401 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
Brian Salomone69b9ef2020-07-22 11:18:06 -04001402 static_cast<uint32_t>(GrSamplerState::Filter::kLast) + 1);
1403 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
1404 if (mipMapped == GrMipmapped::kYes) {
1405 mm = (GrSamplerState::MipmapMode)random->nextULessThan(
1406 static_cast<uint32_t>(GrSamplerState::MipmapMode::kLast) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +00001407 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001408
Brian Osman3ebd3542018-07-30 14:36:53 -04001409 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001410 GrAAType aaType = GrAAType::kNone;
1411 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06001412 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -05001413 }
Brian Salomon2213ee92018-10-02 10:44:21 -04001414 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1415 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1416 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1417 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1418 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomon2432d062020-04-16 20:48:09 -04001419 bool useSubset = random->nextBool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001420 auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
Greg Daniel549325c2019-10-30 16:19:20 -04001421 GrSurfaceProxyView proxyView(
1422 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -05001423 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Brian Salomonfc118442019-11-22 19:09:27 -05001424 auto alphaType = static_cast<SkAlphaType>(
1425 random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
Greg Daniel549325c2019-10-30 16:19:20 -04001426
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001427 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
Brian Salomonfc118442019-11-22 19:09:27 -05001428 return GrTextureOp::Make(context, std::move(proxyView), alphaType, std::move(texXform), filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001429 mm, color, saturate, SkBlendMode::kSrcOver, aaType, &quad,
1430 useSubset ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -04001431}
1432
1433#endif