blob: c1c8a46d7e0d05377e2f3a514f90f06424da496b [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"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040033#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040034#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040035#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/glsl/GrGLSLVarying.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040037#include "src/gpu/ops/GrFillRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/ops/GrMeshDrawOp.h"
39#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050040#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040041#include "src/gpu/ops/GrTextureOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040042
43namespace {
44
Brian Salomon2432d062020-04-16 20:48:09 -040045using Subset = GrQuadPerEdgeAA::Subset;
Michael Ludwigc182b942018-11-16 10:27:51 -050046using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050047using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040048
Michael Ludwig22429f92019-06-27 10:44:48 -040049// Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
50// between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
51static SkSize axis_aligned_quad_size(const GrQuad& quad) {
52 SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
53 // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
54 float dw = sk_float_abs(quad.x(2) - quad.x(0)) + sk_float_abs(quad.y(2) - quad.y(0));
55 float dh = sk_float_abs(quad.x(1) - quad.x(0)) + sk_float_abs(quad.y(1) - quad.y(0));
56 return {dw, dh};
57}
58
Brian Salomone69b9ef2020-07-22 11:18:06 -040059static std::tuple<bool /* filter */,
60 bool /* mipmap */>
61filter_and_mm_have_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
Michael Ludwig22429f92019-06-27 10:44:48 -040062 // If not axis-aligned in src or dst, then always say it has an effect
63 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
64 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040065 return {true, true};
Michael Ludwig22429f92019-06-27 10:44:48 -040066 }
67
68 SkRect srcRect;
69 SkRect dstRect;
70 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
71 // Disable filtering when there is no scaling (width and height are the same), and the
72 // top-left corners have the same fraction (so src and dst snap to the pixel grid
73 // identically).
74 SkASSERT(srcRect.isSorted());
Brian Salomone69b9ef2020-07-22 11:18:06 -040075 bool filter = srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
76 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
77 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
78 bool mm = srcRect.width() > dstRect.width() || srcRect.height() > dstRect.height();
79 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040080 }
Brian Salomone69b9ef2020-07-22 11:18:06 -040081 // Extract edge lengths
82 SkSize srcSize = axis_aligned_quad_size(srcQuad);
83 SkSize dstSize = axis_aligned_quad_size(dstQuad);
84 // Although the quads are axis-aligned, the local coordinate system is transformed such
85 // that fractionally-aligned sample centers will not align with the device coordinate system
86 // So disable filtering when edges are the same length and both srcQuad and dstQuad
87 // 0th vertex is integer aligned.
88 bool filter = srcSize != dstSize ||
89 !SkScalarIsInt(srcQuad.x(0)) ||
90 !SkScalarIsInt(srcQuad.y(0)) ||
91 !SkScalarIsInt(dstQuad.x(0)) ||
92 !SkScalarIsInt(dstQuad.y(0));
93 bool mm = srcSize.fWidth > dstSize.fWidth || srcSize.fHeight > dstSize.fHeight;
94 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040095}
96
Michael Ludwig119ac6d2019-11-21 09:26:46 -050097// Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
98// regular and rectangular textures, w/ or w/o origin correction.
99struct NormalizationParams {
100 float fIW; // 1 / width of texture, or 1.0 for texture rectangles
Michael Ludwigc453a502020-05-29 12:29:12 -0400101 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 -0500102 float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
103};
Michael Ludwigadb12e72019-12-04 16:19:18 -0500104static NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
105 GrSurfaceOrigin origin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500106 // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
107 // normalize the src coordinates up front.
Michael Ludwigadb12e72019-12-04 16:19:18 -0500108 SkISize dimensions = proxy->backingStoreDimensions();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500109 float iw, ih, h;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500110 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500111 iw = ih = 1.f;
112 h = dimensions.height();
113 } else {
114 iw = 1.f / dimensions.width();
115 ih = 1.f / dimensions.height();
116 h = 1.f;
117 }
118
Michael Ludwigadb12e72019-12-04 16:19:18 -0500119 if (origin == kBottomLeft_GrSurfaceOrigin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500120 return {iw, -ih, h};
121 } else {
122 return {iw, ih, 0.0f};
123 }
124}
125
Brian Salomon2432d062020-04-16 20:48:09 -0400126// Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500127// 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 -0400128// subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
129// and y-flipping are applied as indicated by NormalizationParams.
130static SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
131 const NormalizationParams& params,
132 const SkRect* subsetRect) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500133 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Brian Salomon2432d062020-04-16 20:48:09 -0400134 if (!subsetRect) {
135 // Either the quad has no subset constraint and is batched with a subset constrained op
136 // (in which case we want a subset that doesn't restrict normalized tex coords), or the
137 // entire op doesn't use the subset, in which case the returned value is ignored.
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500138 return kLargeRect;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400139 }
140
Brian Salomon2432d062020-04-16 20:48:09 -0400141 auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400142 auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
143 if (filter == GrSamplerState::Filter::kNearest) {
144 // Make sure our insetting puts us at pixel centers.
145 ltrb = skvx::floor(ltrb*flipHi)*flipHi;
146 }
147 // Inset with pin to the rect center.
148 ltrb += skvx::Vec<4, float>({.5f, .5f, -.5f, -.5f});
149 auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
150 ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
151
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500152 // Normalize and offset
Elliot Evans9fdddf62020-07-29 16:39:11 -0400153 ltrb = ltrb * skvx::Vec<4, float>{params.fIW, params.fInvH, params.fIW, params.fInvH} +
154 skvx::Vec<4, float>{0.f, params.fYOffset, 0.f, params.fYOffset};
Michael Ludwigc453a502020-05-29 12:29:12 -0400155 if (params.fInvH < 0.f) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500156 // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
157 ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400158 }
159
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500160 SkRect out;
161 ltrb.store(&out);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500162 return out;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400163}
164
Michael Ludwig009b92e2019-02-15 16:03:53 -0500165// Normalizes logical src coords and corrects for origin
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500166static void normalize_src_quad(const NormalizationParams& params,
167 GrQuad* srcQuad) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500168 // The src quad should not have any perspective
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500169 SkASSERT(!srcQuad->hasPerspective());
170 skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
Elliot Evans9fdddf62020-07-29 16:39:11 -0400171 skvx::Vec<4, float> ys = srcQuad->y4f() * params.fInvH + params.fYOffset;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500172 xs.store(srcQuad->xs());
173 ys.store(srcQuad->ys());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500174}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400175
Michael Ludwig379e4962019-12-06 13:21:26 -0500176// Count the number of proxy runs in the entry set. This usually is already computed by
177// SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
178// for each split.
Brian Salomoneebe7352020-12-09 16:37:04 -0500179static int proxy_run_count(const GrSurfaceDrawContext::TextureSetEntry set[], int count) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500180 int actualProxyRunCount = 0;
181 const GrSurfaceProxy* lastProxy = nullptr;
182 for (int i = 0; i < count; ++i) {
183 if (set[i].fProxyView.proxy() != lastProxy) {
184 actualProxyRunCount++;
185 lastProxy = set[i].fProxyView.proxy();
186 }
187 }
188 return actualProxyRunCount;
189}
190
John Stilescbe4e282020-06-01 10:38:31 -0400191static bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
192 const DrawQuad& quad, const SkRect& subsetRect) {
193 // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
194 // can push all the way up to the edges of the the subset rect and the sampler shouldn't
195 // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
196 // the non-antialiased case.
197 SkRect localBounds = quad.fLocal.bounds();
198 if (aaType == GrAAType::kNone &&
199 filter == GrSamplerState::Filter::kNearest &&
200 quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
201 quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
202 subsetRect.contains(localBounds)) {
203
204 return true;
205 }
206
207 // If the subset rect is inset by at least 0.5 pixels into the local quad's bounds, the
208 // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
209 if (subsetRect.makeInset(0.5f, 0.5f).contains(localBounds)) {
210 return true;
211 }
212
213 // The subset rect cannot be ignored safely.
214 return false;
215}
216
Brian Salomon34169692017-08-28 15:32:01 -0400217/**
218 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
219 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
220 */
221class TextureOp final : public GrMeshDrawOp {
222public:
Herb Derbyc76d4092020-10-07 16:46:15 -0400223 static GrOp::Owner Make(GrRecordingContext* context,
224 GrSurfaceProxyView proxyView,
225 sk_sp<GrColorSpaceXform> textureXform,
226 GrSamplerState::Filter filter,
227 GrSamplerState::MipmapMode mm,
228 const SkPMColor4f& color,
229 GrTextureOp::Saturate saturate,
230 GrAAType aaType,
231 DrawQuad* quad,
232 const SkRect* subset) {
233
234 return GrOp::Make<TextureOp>(context, std::move(proxyView), std::move(textureXform),
235 filter, mm, color, saturate, aaType, quad, subset);
Brian Salomon34169692017-08-28 15:32:01 -0400236 }
Robert Phillipse837e612019-11-15 11:02:50 -0500237
Herb Derbyc76d4092020-10-07 16:46:15 -0400238 static GrOp::Owner Make(GrRecordingContext* context,
Brian Salomoneebe7352020-12-09 16:37:04 -0500239 GrSurfaceDrawContext::TextureSetEntry set[],
Herb Derbyc76d4092020-10-07 16:46:15 -0400240 int cnt,
241 int proxyRunCnt,
242 GrSamplerState::Filter filter,
243 GrSamplerState::MipmapMode mm,
244 GrTextureOp::Saturate saturate,
245 GrAAType aaType,
246 SkCanvas::SrcRectConstraint constraint,
247 const SkMatrix& viewMatrix,
248 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500249 // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
250 SkASSERT(proxyRunCnt <= cnt);
Herb Derbyc76d4092020-10-07 16:46:15 -0400251 return GrOp::MakeWithExtraMemory<TextureOp>(
252 context, sizeof(ViewCountPair) * (proxyRunCnt - 1),
253 set, cnt, proxyRunCnt, filter, mm, saturate, aaType, constraint,
254 viewMatrix, std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400255 }
Brian Salomon34169692017-08-28 15:32:01 -0400256
Brian Salomon336ce7b2017-09-08 08:23:58 -0400257 ~TextureOp() override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500258 for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400259 fViewCountPairs[p].~ViewCountPair();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400260 }
261 }
Brian Salomon34169692017-08-28 15:32:01 -0400262
263 const char* name() const override { return "TextureOp"; }
264
Robert Phillips294723d2021-06-17 09:23:58 -0400265 void visitProxies(const GrVisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400266 bool mipped = (fMetadata.mipmapMode() != GrSamplerState::MipmapMode::kNone);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500267 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400268 func(fViewCountPairs[p].fProxy.get(), GrMipmapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400269 }
Chris Daltondbb833b2020-03-17 12:15:46 -0600270 if (fDesc && fDesc->fProgramInfo) {
271 fDesc->fProgramInfo->visitFPProxies(func);
272 }
Brian Salomond7065e72018-10-12 11:42:02 -0400273 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400274
John Stiles8d9bf642020-08-12 15:07:45 -0400275#ifdef SK_DEBUG
Michael Ludwig4ef1ca12019-12-19 10:58:52 -0500276 static void ValidateResourceLimits() {
277 // The op implementation has an upper bound on the number of quads that it can represent.
278 // However, the resource manager imposes its own limit on the number of quads, which should
279 // always be lower than the numerical limit this op can hold.
280 using CountStorage = decltype(Metadata::fTotalQuadCount);
281 CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
282 // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
283 int resourceLimit = SkTo<int>(maxQuadCount);
284 SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
285 GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
286 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500287#endif
Brian Salomon34169692017-08-28 15:32:01 -0400288
Chris Dalton57ab06c2021-04-22 12:57:28 -0600289 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip*,
290 GrClampType clampType) override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500291 SkASSERT(fMetadata.colorType() == ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400292 auto iter = fQuads.metadata();
293 while(iter.next()) {
Brian Osman2715bf52019-12-06 14:38:47 -0500294 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
Brian Salomon5e29e312021-04-27 11:30:00 -0400295 colorType = std::max(static_cast<GrQuadPerEdgeAA::ColorType>(fMetadata.fColorType),
296 colorType);
297 if (caps.reducedShaderMode()) {
298 colorType = std::max(colorType, GrQuadPerEdgeAA::ColorType::kByte);
299 }
300 fMetadata.fColorType = static_cast<uint16_t>(colorType);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400301 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700302 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400303 }
304
Brian Salomon485b8c62018-01-12 15:11:06 -0500305 FixedFunctionFlags fixedFunctionFlags() const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500306 return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
307 : FixedFunctionFlags::kNone;
Brian Salomon485b8c62018-01-12 15:11:06 -0500308 }
Brian Salomon34169692017-08-28 15:32:01 -0400309
310 DEFINE_OP_CLASS_ID
311
312private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400313 friend class ::GrOp;
Brian Salomon762d5e72017-12-01 10:25:08 -0500314
Brian Salomon2432d062020-04-16 20:48:09 -0400315 struct ColorSubsetAndAA {
316 ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400317 : fColor(color)
Brian Salomon2432d062020-04-16 20:48:09 -0400318 , fSubsetRect(subsetRect)
Michael Ludwig4384f042019-12-05 10:30:35 -0500319 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
320 SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
Michael Ludwig425eb452019-06-27 10:13:27 -0400321 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400322
323 SkPMColor4f fColor;
Brian Salomon2432d062020-04-16 20:48:09 -0400324 // 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 -0500325 // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
Brian Salomon2432d062020-04-16 20:48:09 -0400326 SkRect fSubsetRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400327 unsigned fAAFlags : 4;
328
Michael Ludwig425eb452019-06-27 10:13:27 -0400329 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
330 };
Michael Ludwigadb12e72019-12-04 16:19:18 -0500331
Greg Daniel549325c2019-10-30 16:19:20 -0400332 struct ViewCountPair {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500333 // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
334 // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
335 // swizzle so that is stored in the op metadata.
336 sk_sp<GrSurfaceProxy> fProxy;
Michael Ludwig425eb452019-06-27 10:13:27 -0400337 int fQuadCnt;
338 };
339
Michael Ludwigadb12e72019-12-04 16:19:18 -0500340 // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
341 // increase the size of the op; increasing the op size can have a surprising impact on
342 // performance (since texture ops are one of the most commonly used in an app).
343 struct Metadata {
344 // AAType must be filled after initialization; ColorType is determined in finalize()
Brian Salomone69b9ef2020-07-22 11:18:06 -0400345 Metadata(const GrSwizzle& swizzle,
346 GrSamplerState::Filter filter,
347 GrSamplerState::MipmapMode mm,
348 GrQuadPerEdgeAA::Subset subset,
349 GrTextureOp::Saturate saturate)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500350 : fSwizzle(swizzle)
351 , fProxyCount(1)
352 , fTotalQuadCount(1)
Michael Ludwig4384f042019-12-05 10:30:35 -0500353 , fFilter(static_cast<uint16_t>(filter))
Brian Salomone69b9ef2020-07-22 11:18:06 -0400354 , fMipmapMode(static_cast<uint16_t>(mm))
Michael Ludwig4384f042019-12-05 10:30:35 -0500355 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
356 , fColorType(static_cast<uint16_t>(ColorType::kNone))
Brian Salomon2432d062020-04-16 20:48:09 -0400357 , fSubset(static_cast<uint16_t>(subset))
Michael Ludwig4384f042019-12-05 10:30:35 -0500358 , fSaturate(static_cast<uint16_t>(saturate)) {}
Michael Ludwigadb12e72019-12-04 16:19:18 -0500359
Michael Ludwig4384f042019-12-05 10:30:35 -0500360 GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
Michael Ludwigadb12e72019-12-04 16:19:18 -0500361 uint16_t fProxyCount;
362 // This will be >= fProxyCount, since a proxy may be drawn multiple times
363 uint16_t fTotalQuadCount;
364
Michael Ludwig4384f042019-12-05 10:30:35 -0500365 // These must be based on uint16_t to help MSVC's pack bitfields optimally
366 uint16_t fFilter : 2; // GrSamplerState::Filter
Brian Salomone69b9ef2020-07-22 11:18:06 -0400367 uint16_t fMipmapMode : 2; // GrSamplerState::MipmapMode
Michael Ludwig4384f042019-12-05 10:30:35 -0500368 uint16_t fAAType : 2; // GrAAType
369 uint16_t fColorType : 2; // GrQuadPerEdgeAA::ColorType
Brian Salomon2432d062020-04-16 20:48:09 -0400370 uint16_t fSubset : 1; // bool
Michael Ludwig4384f042019-12-05 10:30:35 -0500371 uint16_t fSaturate : 1; // bool
Brian Salomone69b9ef2020-07-22 11:18:06 -0400372 uint16_t fUnused : 6; // # of bits left before Metadata exceeds 8 bytes
Michael Ludwigadb12e72019-12-04 16:19:18 -0500373
374 GrSamplerState::Filter filter() const {
375 return static_cast<GrSamplerState::Filter>(fFilter);
376 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400377 GrSamplerState::MipmapMode mipmapMode() const {
378 return static_cast<GrSamplerState::MipmapMode>(fMipmapMode);
379 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500380 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
381 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
Brian Salomon2432d062020-04-16 20:48:09 -0400382 Subset subset() const { return static_cast<Subset>(fSubset); }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500383 GrTextureOp::Saturate saturate() const {
384 return static_cast<GrTextureOp::Saturate>(fSaturate);
385 }
386
387 static_assert(GrSamplerState::kFilterCount <= 4);
388 static_assert(kGrAATypeCount <= 4);
389 static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
390 };
Michael Ludwig4384f042019-12-05 10:30:35 -0500391 static_assert(sizeof(Metadata) == 8);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500392
Chris Daltondbb833b2020-03-17 12:15:46 -0600393 // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
394 // store the data in a separate struct in order to minimize the size of the TextureOp.
395 // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
396 // may want to re-evaluate whether this is still necessary.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400397 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600398 // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
399 // allocatePrePreparedVertices is also called.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400400 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600401 // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
402 // part of the flushState).
403 struct Desc {
404 VertexSpec fVertexSpec;
405 int fNumProxies = 0;
406 int fNumTotalQuads = 0;
Robert Phillips32803ff2019-10-23 08:26:08 -0400407
Chris Daltondbb833b2020-03-17 12:15:46 -0600408 // This member variable is only used by 'onPrePrepareDraws'.
409 char* fPrePreparedVertices = nullptr;
410
411 GrProgramInfo* fProgramInfo = nullptr;
412
413 sk_sp<const GrBuffer> fIndexBuffer;
414 sk_sp<const GrBuffer> fVertexBuffer;
415 int fBaseVertex;
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400416
417 // How big should 'fVertices' be to hold all the vertex data?
418 size_t totalSizeInBytes() const {
Chris Daltondbb833b2020-03-17 12:15:46 -0600419 return this->totalNumVertices() * fVertexSpec.vertexSize();
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400420 }
421
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400422 int totalNumVertices() const {
423 return fNumTotalQuads * fVertexSpec.verticesPerQuad();
424 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400425
Chris Daltondbb833b2020-03-17 12:15:46 -0600426 void allocatePrePreparedVertices(SkArenaAlloc* arena) {
427 fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400428 }
Robert Phillips32803ff2019-10-23 08:26:08 -0400429 };
Brian Salomon2432d062020-04-16 20:48:09 -0400430 // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
Greg Daniel549325c2019-10-30 16:19:20 -0400431 TextureOp(GrSurfaceProxyView proxyView,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400432 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
433 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400434 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400435 const SkPMColor4f& color,
436 GrTextureOp::Saturate saturate,
437 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500438 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400439 const SkRect* subsetRect)
Brian Salomon34169692017-08-28 15:32:01 -0400440 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400441 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400442 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600443 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400444 , fMetadata(proxyView.swizzle(), filter, mm, Subset(!!subsetRect), saturate) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400445 // Clean up disparities between the overall aa type and edge configuration and apply
446 // optimizations based on the rect and matrix when appropriate
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500447 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
448 &aaType, &quad->fEdgeFlags);
Michael Ludwig4384f042019-12-05 10:30:35 -0500449 fMetadata.fAAType = static_cast<uint16_t>(aaType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400450
Brian Salomonf1709042018-10-03 11:57:00 -0400451 // We expect our caller to have already caught this optimization.
Brian Salomon2432d062020-04-16 20:48:09 -0400452 SkASSERT(!subsetRect ||
453 !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500454
Brian Salomonf09abc52018-10-03 15:59:04 -0400455 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
John Stilescbe4e282020-06-01 10:38:31 -0400456 // Try to identify cases where the subsetting isn't actually necessary, and skip it.
457 if (subsetRect) {
458 if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
459 subsetRect = nullptr;
460 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
461 }
Brian Salomonf09abc52018-10-03 15:59:04 -0400462 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500463
Brian Salomon2432d062020-04-16 20:48:09 -0400464 // Normalize src coordinates and the subset (if set)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500465 NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
466 proxyView.origin());
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500467 normalize_src_quad(params, &quad->fLocal);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400468 SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500469
Michael Ludwig949ceb22020-02-07 10:14:45 -0500470 // Set bounds before clipping so we don't have to worry about unioning the bounds of
471 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500472 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -0400473 IsHairline::kNo);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500474
Brian Salomon2432d062020-04-16 20:48:09 -0400475 int quadCount = this->appendQuad(quad, color, subset);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500476 fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
Brian Salomond7065e72018-10-12 11:42:02 -0400477 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400478
Brian Salomoneebe7352020-12-09 16:37:04 -0500479 TextureOp(GrSurfaceDrawContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400480 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500481 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400482 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400483 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400484 GrTextureOp::Saturate saturate,
485 GrAAType aaType,
486 SkCanvas::SrcRectConstraint constraint,
487 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500488 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400489 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400490 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400491 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600492 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400493 , fMetadata(set[0].fProxyView.swizzle(),
494 GrSamplerState::Filter::kNearest,
495 GrSamplerState::MipmapMode::kNone,
496 Subset::kNo,
497 saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500498 // Update counts to reflect the batch op
Michael Ludwig379e4962019-12-06 13:21:26 -0500499 fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500500 fMetadata.fTotalQuadCount = SkToUInt(cnt);
501
Brian Salomond7065e72018-10-12 11:42:02 -0400502 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500503
504 GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon2432d062020-04-16 20:48:09 -0400505 Subset netSubset = Subset::kNo;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500506 GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400507 GrSamplerState::MipmapMode netMM = GrSamplerState::MipmapMode::kNone;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500508
Michael Ludwig379e4962019-12-06 13:21:26 -0500509 const GrSurfaceProxy* curProxy = nullptr;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500510
Michael Ludwig379e4962019-12-06 13:21:26 -0500511 // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
512 // increases when set[q]'s proxy changes.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500513 int p = 0;
514 for (int q = 0; q < cnt; ++q) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400515 SkASSERT(mm == GrSamplerState::MipmapMode::kNone ||
516 (set[0].fProxyView.proxy()->asTextureProxy()->mipmapped() ==
517 GrMipmapped::kYes));
Michael Ludwig379e4962019-12-06 13:21:26 -0500518 if (q == 0) {
Greg Daniel549325c2019-10-30 16:19:20 -0400519 // We do not placement new the first ViewCountPair since that one is allocated and
520 // initialized as part of the GrTextureOp creation.
Michael Ludwig379e4962019-12-06 13:21:26 -0500521 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
522 fViewCountPairs[0].fQuadCnt = 0;
523 curProxy = fViewCountPairs[0].fProxy.get();
524 } else if (set[q].fProxyView.proxy() != curProxy) {
Greg Daniel549325c2019-10-30 16:19:20 -0400525 // We must placement new the ViewCountPairs here so that the sk_sps in the
526 // GrSurfaceProxyView get initialized properly.
Michael Ludwig379e4962019-12-06 13:21:26 -0500527 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
Michael Ludwigadb12e72019-12-04 16:19:18 -0500528
Michael Ludwig379e4962019-12-06 13:21:26 -0500529 curProxy = fViewCountPairs[p].fProxy.get();
Greg Danielc71c7962020-01-14 16:44:18 -0500530 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
531 curProxy, fViewCountPairs[0].fProxy.get()));
Michael Ludwig379e4962019-12-06 13:21:26 -0500532 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
Michael Ludwig379e4962019-12-06 13:21:26 -0500533 } // else another quad referencing the same proxy
Michael Ludwigce62dec2019-02-19 11:48:46 -0500534
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500535 SkMatrix ctm = viewMatrix;
Michael Ludwig379e4962019-12-06 13:21:26 -0500536 if (set[q].fPreViewMatrix) {
537 ctm.preConcat(*set[q].fPreViewMatrix);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500538 }
539
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400540 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
541 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500542 DrawQuad quad;
Michael Ludwig379e4962019-12-06 13:21:26 -0500543 if (set[q].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500544 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400545
546 SkPoint srcPts[4];
Michael Ludwig379e4962019-12-06 13:21:26 -0500547 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500548 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400549 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500550 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
551 quad.fLocal = GrQuad(set[q].fSrcRect);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400552 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500553
Brian Salomone69b9ef2020-07-22 11:18:06 -0400554 if (netFilter != filter || netMM != mm) {
555 // The only way netFilter != filter is if linear is requested and we haven't yet
556 // found a quad that requires linear (so net is still nearest). Similar for mip
557 // mapping.
Brian Salomonf7353512020-07-22 19:26:48 -0400558 SkASSERT(filter == netFilter ||
559 (netFilter == GrSamplerState::Filter::kNearest && filter > netFilter));
560 SkASSERT(mm == netMM ||
561 (netMM == GrSamplerState::MipmapMode::kNone && mm > netMM));
Brian Salomone69b9ef2020-07-22 11:18:06 -0400562 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad.fLocal, quad.fDevice);
563 if (mustFilter && filter != GrSamplerState::Filter::kNearest) {
Brian Salomonf7353512020-07-22 19:26:48 -0400564 netFilter = filter;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400565 }
566 if (mustMM && mm != GrSamplerState::MipmapMode::kNone) {
Brian Salomonf7353512020-07-22 19:26:48 -0400567 netMM = mm;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400568 }
Michael Ludwig22429f92019-06-27 10:44:48 -0400569 }
570
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500571 // Update overall bounds of the op as the union of all quads
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500572 bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500573
574 // Determine the AA type for the quad, then merge with net AA type
Michael Ludwig6bee7762018-10-19 09:50:36 -0400575 GrAAType aaForQuad;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500576 GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
577 &aaForQuad, &quad.fEdgeFlags);
John Stilescbe4e282020-06-01 10:38:31 -0400578
Michael Ludwig6bee7762018-10-19 09:50:36 -0400579 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
580 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500581 if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
582 netAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400583 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400584
585 // Calculate metadata for the entry
Brian Salomon2432d062020-04-16 20:48:09 -0400586 const SkRect* subsetForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400587 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
John Stilescbe4e282020-06-01 10:38:31 -0400588 // Check (briefly) if the subset rect is actually needed for this set entry.
589 SkRect* subsetRect = &set[q].fSrcRect;
590 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
591 if (!safe_to_ignore_subset_rect(aaForQuad, filter, quad, *subsetRect)) {
592 netSubset = Subset::kYes;
593 subsetForQuad = subsetRect;
594 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400595 }
596 }
John Stilescbe4e282020-06-01 10:38:31 -0400597
598 // Normalize the src quads and apply origin
599 NormalizationParams proxyParams = proxy_normalization_params(
600 curProxy, set[q].fProxyView.origin());
601 normalize_src_quad(proxyParams, &quad.fLocal);
602
Brian Salomon2432d062020-04-16 20:48:09 -0400603 // This subset may represent a no-op, otherwise it will have the origin and dimensions
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500604 // of the texture applied to it. Insetting for bilinear filtering is deferred until
605 // on[Pre]Prepare so that the overall filter can be lazily determined.
Brian Salomon75cebbe2020-05-18 14:08:14 -0400606 SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500607
Michael Ludwig949ceb22020-02-07 10:14:45 -0500608 // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
609 // ViewCountPair (this frequently happens when Chrome draws 9-patches).
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400610 fViewCountPairs[p].fQuadCnt += this->appendQuad(&quad, set[q].fColor, subset);
Brian Salomond7065e72018-10-12 11:42:02 -0400611 }
Michael Ludwig406172a2019-12-06 14:05:19 -0500612 // The # of proxy switches should match what was provided (+1 because we incremented p
Michael Ludwig379e4962019-12-06 13:21:26 -0500613 // when a new proxy was encountered).
Michael Ludwig406172a2019-12-06 14:05:19 -0500614 SkASSERT((p + 1) == fMetadata.fProxyCount);
Michael Ludwig379e4962019-12-06 13:21:26 -0500615 SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500616
Michael Ludwig4384f042019-12-05 10:30:35 -0500617 fMetadata.fAAType = static_cast<uint16_t>(netAAType);
618 fMetadata.fFilter = static_cast<uint16_t>(netFilter);
Brian Salomon2432d062020-04-16 20:48:09 -0400619 fMetadata.fSubset = static_cast<uint16_t>(netSubset);
Brian Salomon34169692017-08-28 15:32:01 -0400620
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500621 this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage), IsHairline::kNo);
Brian Salomon17031a72018-05-22 14:14:07 -0400622 }
623
Brian Salomon2432d062020-04-16 20:48:09 -0400624 int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& subset) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500625 DrawQuad extra;
Michael Ludwig65299902021-05-13 12:02:23 -0400626 // Always clip to W0 to stay consistent with GrQuad::bounds
627 int quadCount = GrQuadUtils::ClipToW0(quad, &extra);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500628 if (quadCount == 0) {
629 // We can't discard the op at this point, but disable AA flags so it won't go through
630 // inset/outset processing
631 quad->fEdgeFlags = GrQuadAAFlags::kNone;
632 quadCount = 1;
633 }
Brian Salomon2432d062020-04-16 20:48:09 -0400634 fQuads.append(quad->fDevice, {color, subset, quad->fEdgeFlags}, &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500635 if (quadCount > 1) {
Brian Salomon2432d062020-04-16 20:48:09 -0400636 fQuads.append(extra.fDevice, {color, subset, extra.fEdgeFlags}, &extra.fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500637 fMetadata.fTotalQuadCount++;
638 }
639 return quadCount;
640 }
641
Robert Phillips2669a7b2020-03-12 12:07:19 -0400642 GrProgramInfo* programInfo() override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600643 // Although this Op implements its own onPrePrepareDraws it calls GrMeshDrawOps' version so
644 // this entry point will be called.
645 return (fDesc) ? fDesc->fProgramInfo : nullptr;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400646 }
647
Chris Daltondbb833b2020-03-17 12:15:46 -0600648 void onCreateProgramInfo(const GrCaps* caps,
649 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500650 const GrSurfaceProxyView& writeView,
Chris Daltondbb833b2020-03-17 12:15:46 -0600651 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400652 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500653 GrXferBarrierFlags renderPassXferBarriers,
654 GrLoadOp colorLoadOp) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600655 SkASSERT(fDesc);
656
657 GrGeometryProcessor* gp;
658
659 {
660 const GrBackendFormat& backendFormat =
661 fViewCountPairs[0].fProxy->backendFormat();
662
663 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
664 fMetadata.filter());
665
666 gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
667 arena, fDesc->fVertexSpec, *caps->shaderCaps(), backendFormat, samplerState,
668 fMetadata.fSwizzle, std::move(fTextureColorSpaceXform), fMetadata.saturate());
669
670 SkASSERT(fDesc->fVertexSpec.vertexSize() == gp->vertexStride());
671 }
672
673 auto pipelineFlags = (GrAAType::kMSAA == fMetadata.aaType()) ?
674 GrPipeline::InputFlags::kHWAntialias : GrPipeline::InputFlags::kNone;
675
676 fDesc->fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Brian Salomon8afde5f2020-04-01 16:22:00 -0400677 caps, arena, writeView, std::move(appliedClip), dstProxyView, gp,
Chris Daltondbb833b2020-03-17 12:15:46 -0600678 GrProcessorSet::MakeEmptySet(), fDesc->fVertexSpec.primitiveType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500679 renderPassXferBarriers, colorLoadOp, pipelineFlags);
Robert Phillips4133dc42020-03-11 15:55:55 -0400680 }
681
Robert Phillipsdf70f152019-11-15 14:57:05 -0500682 void onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -0500683 const GrSurfaceProxyView& writeView,
Robert Phillips8053c972019-11-21 10:44:53 -0500684 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -0400685 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500686 GrXferBarrierFlags renderPassXferBarriers,
687 GrLoadOp colorLoadOp) override {
Robert Phillips61fc7992019-10-22 11:58:17 -0400688 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips29f38542019-10-16 09:20:25 -0400689
Robert Phillips61fc7992019-10-22 11:58:17 -0400690 SkDEBUGCODE(this->validate();)
Chris Daltondbb833b2020-03-17 12:15:46 -0600691 SkASSERT(!fDesc);
Robert Phillips61fc7992019-10-22 11:58:17 -0400692
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500693 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillips61fc7992019-10-22 11:58:17 -0400694
Chris Daltondbb833b2020-03-17 12:15:46 -0600695 fDesc = arena->make<Desc>();
Brian Salomonb8c4add2021-06-28 09:20:44 -0400696 this->characterize(fDesc);
Chris Daltondbb833b2020-03-17 12:15:46 -0600697 fDesc->allocatePrePreparedVertices(arena);
698 FillInVertices(*context->priv().caps(), this, fDesc, fDesc->fPrePreparedVertices);
Robert Phillips61fc7992019-10-22 11:58:17 -0400699
Chris Daltondbb833b2020-03-17 12:15:46 -0600700 // This will call onCreateProgramInfo and register the created program with the DDL.
Greg Danield358cbe2020-09-11 09:33:54 -0400701 this->INHERITED::onPrePrepareDraws(context, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500702 renderPassXferBarriers, colorLoadOp);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400703 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400704
Chris Daltondbb833b2020-03-17 12:15:46 -0600705 static void FillInVertices(const GrCaps& caps, TextureOp* texOp, Desc* desc, char* vertexData) {
706 SkASSERT(vertexData);
707
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400708 int totQuadsSeen = 0;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400709 SkDEBUGCODE(int totVerticesSeen = 0;)
Michael Ludwig189c9802019-11-21 11:21:12 -0500710 SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400711
Chris Daltondbb833b2020-03-17 12:15:46 -0600712 GrQuadPerEdgeAA::Tessellator tessellator(desc->fVertexSpec, vertexData);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400713 for (const auto& op : ChainRange<TextureOp>(texOp)) {
714 auto iter = op.fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500715 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500716 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
717 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400718
Chris Daltondbb833b2020-03-17 12:15:46 -0600719 for (int i = 0; i < quadCnt && iter.next(); ++i) {
720 SkASSERT(iter.isLocalValid());
Brian Salomon2432d062020-04-16 20:48:09 -0400721 const ColorSubsetAndAA& info = iter.metadata();
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500722
Chris Daltondbb833b2020-03-17 12:15:46 -0600723 tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
Brian Salomon75cebbe2020-05-18 14:08:14 -0400724 info.fSubsetRect, info.aaFlags());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400725 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400726
Chris Daltondbb833b2020-03-17 12:15:46 -0600727 SkASSERT((totVerticesSeen + meshVertexCnt) * vertexSize
728 == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400729
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400730 totQuadsSeen += quadCnt;
731 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
732 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400733 }
734
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400735 // If quad counts per proxy were calculated correctly, the entire iterator
736 // should have been consumed.
Chris Daltondbb833b2020-03-17 12:15:46 -0600737 SkASSERT(!iter.next());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400738 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400739
Chris Daltondbb833b2020-03-17 12:15:46 -0600740 SkASSERT(desc->totalSizeInBytes() == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400741 SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
742 SkASSERT(totVerticesSeen == desc->totalNumVertices());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400743 }
744
Robert Phillips29f38542019-10-16 09:20:25 -0400745#ifdef SK_DEBUG
Robert Phillips6bf11252020-07-31 12:15:00 -0400746 static int validate_op(GrTextureType textureType,
747 GrAAType aaType,
748 GrSwizzle swizzle,
749 const TextureOp* op) {
750 SkASSERT(op->fMetadata.fSwizzle == swizzle);
751
752 int quadCount = 0;
753 for (unsigned p = 0; p < op->fMetadata.fProxyCount; ++p) {
754 auto* proxy = op->fViewCountPairs[p].fProxy->asTextureProxy();
755 quadCount += op->fViewCountPairs[p].fQuadCnt;
756 SkASSERT(proxy);
757 SkASSERT(proxy->textureType() == textureType);
758 }
759
760 SkASSERT(aaType == op->fMetadata.aaType());
761 return quadCount;
762 }
763
Robert Phillips29f38542019-10-16 09:20:25 -0400764 void validate() const override {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500765 // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
Michael Ludwigadb12e72019-12-04 16:19:18 -0500766 auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
767 GrAAType aaType = fMetadata.aaType();
Robert Phillips6bf11252020-07-31 12:15:00 -0400768 GrSwizzle swizzle = fMetadata.fSwizzle;
Robert Phillips29f38542019-10-16 09:20:25 -0400769
Robert Phillips6bf11252020-07-31 12:15:00 -0400770 int quadCount = validate_op(textureType, aaType, swizzle, this);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500771
Robert Phillips6bf11252020-07-31 12:15:00 -0400772 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
773 quadCount += validate_op(textureType, aaType, swizzle,
774 static_cast<const TextureOp*>(tmp));
775 }
Robert Phillips29f38542019-10-16 09:20:25 -0400776
Robert Phillips6bf11252020-07-31 12:15:00 -0400777 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
778 quadCount += validate_op(textureType, aaType, swizzle,
779 static_cast<const TextureOp*>(tmp));
Robert Phillips29f38542019-10-16 09:20:25 -0400780 }
Robert Phillipse837e612019-11-15 11:02:50 -0500781
782 SkASSERT(quadCount == this->numChainedQuads());
Robert Phillips29f38542019-10-16 09:20:25 -0400783 }
Robert Phillips6bf11252020-07-31 12:15:00 -0400784
Robert Phillips29f38542019-10-16 09:20:25 -0400785#endif
786
Robert Phillipse837e612019-11-15 11:02:50 -0500787#if GR_TEST_UTILS
788 int numQuads() const final { return this->totNumQuads(); }
789#endif
790
Brian Salomonb8c4add2021-06-28 09:20:44 -0400791 void characterize(Desc* desc) const {
Robert Phillips6bf11252020-07-31 12:15:00 -0400792 SkDEBUGCODE(this->validate();)
793
Robert Phillips29f38542019-10-16 09:20:25 -0400794 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
795 ColorType colorType = ColorType::kNone;
796 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomon2432d062020-04-16 20:48:09 -0400797 Subset subset = Subset::kNo;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500798 GrAAType overallAAType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400799
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400800 desc->fNumProxies = 0;
801 desc->fNumTotalQuads = 0;
802 int maxQuadsPerMesh = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400803
Brian Salomonf7232642018-09-19 08:58:08 -0400804 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400805 if (op.fQuads.deviceQuadType() > quadType) {
806 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500807 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400808 if (op.fQuads.localQuadType() > srcQuadType) {
809 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500810 }
Brian Salomon2432d062020-04-16 20:48:09 -0400811 if (op.fMetadata.subset() == Subset::kYes) {
812 subset = Subset::kYes;
Brian Salomonf7232642018-09-19 08:58:08 -0400813 }
Brian Osman788b9162020-02-07 10:36:46 -0500814 colorType = std::max(colorType, op.fMetadata.colorType());
Michael Ludwigadb12e72019-12-04 16:19:18 -0500815 desc->fNumProxies += op.fMetadata.fProxyCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400816
Michael Ludwigadb12e72019-12-04 16:19:18 -0500817 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Brian Osman788b9162020-02-07 10:36:46 -0500818 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
Brian Salomonf7232642018-09-19 08:58:08 -0400819 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400820 desc->fNumTotalQuads += op.totNumQuads();
821
Michael Ludwigadb12e72019-12-04 16:19:18 -0500822 if (op.fMetadata.aaType() == GrAAType::kCoverage) {
Robert Phillips29f38542019-10-16 09:20:25 -0400823 overallAAType = GrAAType::kCoverage;
Brian Salomonae7d7702018-10-14 15:05:45 -0400824 }
Brian Salomon34169692017-08-28 15:32:01 -0400825 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400826
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400827 SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
828
829 SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
830
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400831 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(overallAAType,
Brian Salomonb8c4add2021-06-28 09:20:44 -0400832 maxQuadsPerMesh);
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400833
834 desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
Brian Salomon2432d062020-04-16 20:48:09 -0400835 subset, overallAAType, /* alpha as coverage */ true,
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400836 indexBufferOption);
Robert Phillipse837e612019-11-15 11:02:50 -0500837
838 SkASSERT(desc->fNumTotalQuads <= GrQuadPerEdgeAA::QuadLimit(indexBufferOption));
Robert Phillips29f38542019-10-16 09:20:25 -0400839 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500840
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400841 int totNumQuads() const {
842#ifdef SK_DEBUG
843 int tmp = 0;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500844 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400845 tmp += fViewCountPairs[p].fQuadCnt;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400846 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500847 SkASSERT(tmp == fMetadata.fTotalQuadCount);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400848#endif
849
Michael Ludwigadb12e72019-12-04 16:19:18 -0500850 return fMetadata.fTotalQuadCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400851 }
852
853 int numChainedQuads() const {
854 int numChainedQuads = this->totNumQuads();
855
856 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
857 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
858 }
859
860 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
861 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
862 }
863
864 return numChainedQuads;
865 }
866
Robert Phillips29f38542019-10-16 09:20:25 -0400867 // onPrePrepareDraws may or may not have been called at this point
Robert Phillips71143952021-06-17 14:55:07 -0400868 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips29f38542019-10-16 09:20:25 -0400869 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500870
Robert Phillips29f38542019-10-16 09:20:25 -0400871 SkDEBUGCODE(this->validate();)
872
Chris Daltondbb833b2020-03-17 12:15:46 -0600873 SkASSERT(!fDesc || fDesc->fPrePreparedVertices);
Robert Phillips29f38542019-10-16 09:20:25 -0400874
Chris Daltondbb833b2020-03-17 12:15:46 -0600875 if (!fDesc) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400876 SkArenaAlloc* arena = target->allocator();
Chris Daltondbb833b2020-03-17 12:15:46 -0600877 fDesc = arena->make<Desc>();
Brian Salomonb8c4add2021-06-28 09:20:44 -0400878 this->characterize(fDesc);
Chris Daltondbb833b2020-03-17 12:15:46 -0600879 SkASSERT(!fDesc->fPrePreparedVertices);
Brian Salomonf7232642018-09-19 08:58:08 -0400880 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400881
Chris Daltondbb833b2020-03-17 12:15:46 -0600882 size_t vertexSize = fDesc->fVertexSpec.vertexSize();
Brian Salomon92be2f72018-06-19 14:33:47 -0400883
Chris Daltondbb833b2020-03-17 12:15:46 -0600884 void* vdata = target->makeVertexSpace(vertexSize, fDesc->totalNumVertices(),
885 &fDesc->fVertexBuffer, &fDesc->fBaseVertex);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400886 if (!vdata) {
887 SkDebugf("Could not allocate vertices\n");
888 return;
Brian Salomon34169692017-08-28 15:32:01 -0400889 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400890
Chris Daltondbb833b2020-03-17 12:15:46 -0600891 if (fDesc->fVertexSpec.needsIndexBuffer()) {
892 fDesc->fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(
893 target, fDesc->fVertexSpec.indexBufferOption());
894 if (!fDesc->fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400895 SkDebugf("Could not allocate indices\n");
896 return;
897 }
898 }
899
Chris Daltondbb833b2020-03-17 12:15:46 -0600900 if (fDesc->fPrePreparedVertices) {
901 memcpy(vdata, fDesc->fPrePreparedVertices, fDesc->totalSizeInBytes());
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400902 } else {
Chris Daltondbb833b2020-03-17 12:15:46 -0600903 FillInVertices(target->caps(), this, fDesc, (char*) vdata);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400904 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700905 }
906
907 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600908 if (!fDesc->fVertexBuffer) {
909 return;
910 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500911
Chris Daltondbb833b2020-03-17 12:15:46 -0600912 if (fDesc->fVertexSpec.needsIndexBuffer() && !fDesc->fIndexBuffer) {
913 return;
914 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500915
Chris Daltondbb833b2020-03-17 12:15:46 -0600916 if (!fDesc->fProgramInfo) {
917 this->createProgramInfo(flushState);
918 SkASSERT(fDesc->fProgramInfo);
919 }
920
921 flushState->bindPipelineAndScissorClip(*fDesc->fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400922 flushState->bindBuffers(std::move(fDesc->fIndexBuffer), nullptr,
923 std::move(fDesc->fVertexBuffer));
Chris Daltondbb833b2020-03-17 12:15:46 -0600924
925 int totQuadsSeen = 0;
926 SkDEBUGCODE(int numDraws = 0;)
927 for (const auto& op : ChainRange<TextureOp>(this)) {
928 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
929 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
930 SkASSERT(numDraws < fDesc->fNumProxies);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400931 flushState->bindTextures(fDesc->fProgramInfo->geomProc(),
Chris Daltondbb833b2020-03-17 12:15:46 -0600932 *op.fViewCountPairs[p].fProxy,
933 fDesc->fProgramInfo->pipeline());
934 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(),
935 fDesc->fVertexSpec, totQuadsSeen, quadCnt,
936 fDesc->totalNumVertices(), fDesc->fBaseVertex);
937 totQuadsSeen += quadCnt;
938 SkDEBUGCODE(++numDraws;)
939 }
940 }
941
942 SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
943 SkASSERT(numDraws == fDesc->fNumProxies);
Brian Salomon34169692017-08-28 15:32:01 -0400944 }
945
Robert Phillips6bf11252020-07-31 12:15:00 -0400946 void propagateCoverageAAThroughoutChain() {
947 fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
948
949 for (GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
950 TextureOp* tex = static_cast<TextureOp*>(tmp);
951 SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
952 tex->fMetadata.aaType() == GrAAType::kNone);
953 tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
954 }
955
956 for (GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
957 TextureOp* tex = static_cast<TextureOp*>(tmp);
958 SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
959 tex->fMetadata.aaType() == GrAAType::kNone);
960 tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
961 }
962 }
963
Herb Derbye25c3002020-10-27 15:57:27 -0400964 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400965 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips089b7c92020-08-12 11:57:07 -0400966 auto* that = t->cast<TextureOp>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400967
Robert Phillips6bf11252020-07-31 12:15:00 -0400968 SkDEBUGCODE(this->validate();)
969 SkDEBUGCODE(that->validate();)
970
Chris Daltondbb833b2020-03-17 12:15:46 -0600971 if (fDesc || that->fDesc) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400972 // This should never happen (since only DDL recorded ops should be prePrepared)
973 // but, in any case, we should never combine ops that that been prePrepared
974 return CombineResult::kCannotCombine;
975 }
976
Brian Salomon2432d062020-04-16 20:48:09 -0400977 if (fMetadata.subset() != that->fMetadata.subset()) {
978 // It is technically possible to combine operations across subset modes, but performance
Michael Ludwig2929f512019-04-19 13:05:56 -0400979 // testing suggests it's better to make more draw calls where some take advantage of
980 // the more optimal shader path without coordinate clamping.
981 return CombineResult::kCannotCombine;
982 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400983 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
984 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000985 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400986 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400987
Brian Salomonae7d7702018-10-14 15:05:45 -0400988 bool upgradeToCoverageAAOnMerge = false;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500989 if (fMetadata.aaType() != that->fMetadata.aaType()) {
990 if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400991 return CombineResult::kCannotCombine;
992 }
993 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500994 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400995
Michael Ludwigadb12e72019-12-04 16:19:18 -0500996 if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400997 this->numChainedQuads() + that->numChainedQuads())) {
998 return CombineResult::kCannotCombine;
Robert Phillipsb69001f2019-10-29 12:16:35 -0400999 }
1000
Michael Ludwigadb12e72019-12-04 16:19:18 -05001001 if (fMetadata.saturate() != that->fMetadata.saturate()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001002 return CombineResult::kCannotCombine;
1003 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001004 if (fMetadata.filter() != that->fMetadata.filter()) {
Brian Salomonf7232642018-09-19 08:58:08 -04001005 return CombineResult::kCannotCombine;
1006 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001007 if (fMetadata.mipmapMode() != that->fMetadata.mipmapMode()) {
1008 return CombineResult::kCannotCombine;
1009 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001010 if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
1011 return CombineResult::kCannotCombine;
1012 }
1013 const auto* thisProxy = fViewCountPairs[0].fProxy.get();
1014 const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
1015 if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
1016 thisProxy != thatProxy) {
Brian Salomon588cec72018-11-14 13:56:37 -05001017 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -05001018 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Robert Phillips6bf11252020-07-31 12:15:00 -04001019 caps.dynamicStateArrayGeometryProcessorTextureSupport() &&
1020 fMetadata.aaType() == that->fMetadata.aaType()) {
1021 // We only allow chaining when the aaTypes match bc otherwise the AA type
1022 // reported by the chain can be inconsistent. That is, since chaining doesn't
1023 // propagate revised AA information throughout the chain, the head of the chain
1024 // could have an AA setting of kNone while the chain as a whole could have a
1025 // setting of kCoverage. This inconsistency would then interfere with the validity
1026 // of the CombinedQuadCountWillOverflow calls.
1027 // This problem doesn't occur w/ merging bc we do propagate the AA information
1028 // (in propagateCoverageAAThroughoutChain) below.
Brian Salomonf7232642018-09-19 08:58:08 -04001029 return CombineResult::kMayChain;
1030 }
Brian Salomon7eae3e02018-08-07 14:02:38 +00001031 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001032 }
Michael Ludwig009b92e2019-02-15 16:03:53 -05001033
Brian Salomon2432d062020-04-16 20:48:09 -04001034 fMetadata.fSubset |= that->fMetadata.fSubset;
Brian Osman788b9162020-02-07 10:36:46 -05001035 fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
Michael Ludwig009b92e2019-02-15 16:03:53 -05001036
Michael Ludwig425eb452019-06-27 10:13:27 -04001037 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -05001038 fQuads.concat(that->fQuads);
Greg Daniel549325c2019-10-30 16:19:20 -04001039 fViewCountPairs[0].fQuadCnt += that->fQuads.count();
Michael Ludwigadb12e72019-12-04 16:19:18 -05001040 fMetadata.fTotalQuadCount += that->fQuads.count();
Michael Ludwig009b92e2019-02-15 16:03:53 -05001041
Robert Phillips6bf11252020-07-31 12:15:00 -04001042 if (upgradeToCoverageAAOnMerge) {
Robert Phillips089b7c92020-08-12 11:57:07 -04001043 // This merger may be the start of a concatenation of two chains. When one
1044 // of the chains mutates its AA the other must follow suit or else the above AA
1045 // check may prevent later ops from chaining together. A specific example of this is
1046 // when chain2 is prepended onto chain1:
1047 // chain1 (that): opA (non-AA/mergeable) opB (non-AA/non-mergeable)
1048 // chain2 (this): opC (cov-AA/non-mergeable) opD (cov-AA/mergeable)
1049 // W/o this propagation, after opD & opA merge, opB and opC would say they couldn't
1050 // chain - which would stop the concatenation process.
Robert Phillips6bf11252020-07-31 12:15:00 -04001051 this->propagateCoverageAAThroughoutChain();
Robert Phillips089b7c92020-08-12 11:57:07 -04001052 that->propagateCoverageAAThroughoutChain();
Robert Phillips6bf11252020-07-31 12:15:00 -04001053 }
1054
1055 SkDEBUGCODE(this->validate();)
1056
Brian Salomon7eae3e02018-08-07 14:02:38 +00001057 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -04001058 }
John Stilesaf366522020-08-13 09:57:34 -04001059
1060#if GR_TEST_UTILS
1061 SkString onDumpInfo() const override {
1062 SkString str = SkStringPrintf("# draws: %d\n", fQuads.count());
1063 auto iter = fQuads.iterator();
1064 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Robert Phillips047d5bb2021-01-08 13:39:19 -05001065 SkString proxyStr = fViewCountPairs[p].fProxy->dump();
1066 str.append(proxyStr);
1067 str.appendf(", Filter: %d, MM: %d\n",
John Stilesaf366522020-08-13 09:57:34 -04001068 static_cast<int>(fMetadata.fFilter),
1069 static_cast<int>(fMetadata.fMipmapMode));
Robert Phillips047d5bb2021-01-08 13:39:19 -05001070 for (int i = 0; i < fViewCountPairs[p].fQuadCnt && iter.next(); ++i) {
John Stilesaf366522020-08-13 09:57:34 -04001071 const GrQuad* quad = iter.deviceQuad();
1072 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
1073 const ColorSubsetAndAA& info = iter.metadata();
1074 str.appendf(
1075 "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
1076 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
1077 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
1078 i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
1079 info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
1080 quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
1081 quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
1082 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
1083 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
John Stilesaf366522020-08-13 09:57:34 -04001084 }
1085 }
1086 return str;
1087 }
1088#endif
1089
Brian Salomon2432d062020-04-16 20:48:09 -04001090 GrQuadBuffer<ColorSubsetAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -04001091 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Chris Daltondbb833b2020-03-17 12:15:46 -06001092 // Most state of TextureOp is packed into these two field to minimize the op's size.
Michael Ludwigadb12e72019-12-04 16:19:18 -05001093 // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1094 // consider/measure changes with care.
Chris Daltondbb833b2020-03-17 12:15:46 -06001095 Desc* fDesc;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001096 Metadata fMetadata;
Robert Phillips32803ff2019-10-23 08:26:08 -04001097
1098 // This field must go last. When allocating this op, we will allocate extra space to hold
Greg Daniel549325c2019-10-30 16:19:20 -04001099 // additional ViewCountPairs immediately after the op's allocation so we can treat this
Robert Phillips32803ff2019-10-23 08:26:08 -04001100 // as an fProxyCnt-length array.
Greg Daniel549325c2019-10-30 16:19:20 -04001101 ViewCountPair fViewCountPairs[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -04001102
John Stiles7571f9e2020-09-02 22:42:33 -04001103 using INHERITED = GrMeshDrawOp;
Brian Salomon34169692017-08-28 15:32:01 -04001104};
1105
1106} // anonymous namespace
1107
Robert Phillipse837e612019-11-15 11:02:50 -05001108#if GR_TEST_UTILS
1109uint32_t GrTextureOp::ClassID() {
1110 return TextureOp::ClassID();
1111}
1112#endif
Brian Salomon34169692017-08-28 15:32:01 -04001113
Herb Derbyc76d4092020-10-07 16:46:15 -04001114GrOp::Owner GrTextureOp::Make(GrRecordingContext* context,
1115 GrSurfaceProxyView proxyView,
1116 SkAlphaType alphaType,
1117 sk_sp<GrColorSpaceXform> textureXform,
1118 GrSamplerState::Filter filter,
1119 GrSamplerState::MipmapMode mm,
1120 const SkPMColor4f& color,
1121 Saturate saturate,
1122 SkBlendMode blendMode,
1123 GrAAType aaType,
1124 DrawQuad* quad,
1125 const SkRect* subset) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001126 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
Brian Salomon2432d062020-04-16 20:48:09 -04001127 if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1128 // No need for a shader-based subset if hardware clamping achieves the same effect
1129 subset = nullptr;
Michael Ludwig22429f92019-06-27 10:44:48 -04001130 }
1131
Brian Salomone69b9ef2020-07-22 11:18:06 -04001132 if (filter != GrSamplerState::Filter::kNearest || mm != GrSamplerState::MipmapMode::kNone) {
1133 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad->fLocal, quad->fDevice);
1134 if (!mustFilter) {
1135 filter = GrSamplerState::Filter::kNearest;
1136 }
1137 if (!mustMM) {
1138 mm = GrSamplerState::MipmapMode::kNone;
1139 }
Michael Ludwig22429f92019-06-27 10:44:48 -04001140 }
1141
1142 if (blendMode == SkBlendMode::kSrcOver) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001143 return TextureOp::Make(context, std::move(proxyView), std::move(textureXform), filter, mm,
Brian Salomon2432d062020-04-16 20:48:09 -04001144 color, saturate, aaType, std::move(quad), subset);
Michael Ludwig22429f92019-06-27 10:44:48 -04001145 } else {
1146 // Emulate complex blending using GrFillRectOp
Brian Salomonb030b1f2020-11-23 15:40:55 -05001147 GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, filter, mm);
Michael Ludwig22429f92019-06-27 10:44:48 -04001148 GrPaint paint;
1149 paint.setColor4f(color);
1150 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1151
1152 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomonb030b1f2020-11-23 15:40:55 -05001153 const auto& caps = *context->priv().caps();
Brian Salomon2432d062020-04-16 20:48:09 -04001154 if (subset) {
Brian Salomonca6b2f42020-01-24 11:31:21 -05001155 SkRect localRect;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001156 if (quad->fLocal.asRect(&localRect)) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001157 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
Brian Salomonb030b1f2020-11-23 15:40:55 -05001158 samplerState, *subset, localRect, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001159 } else {
John Stiles5a2a7b32020-06-04 10:57:21 -04001160 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
Brian Salomonb030b1f2020-11-23 15:40:55 -05001161 samplerState, *subset, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001162 }
1163 } else {
Brian Salomonb030b1f2020-11-23 15:40:55 -05001164 fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), samplerState,
1165 caps);
Michael Ludwig22429f92019-06-27 10:44:48 -04001166 }
1167 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
Brian Osman958a3bb2020-07-30 14:13:23 -04001168 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001169 if (saturate == GrTextureOp::Saturate::kYes) {
Brian Osman8e814b32021-06-17 14:14:26 -04001170 fp = GrFragmentProcessor::ClampOutput(std::move(fp));
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001171 }
John Stiles5933d7d2020-07-21 12:28:35 -04001172 paint.setColorFragmentProcessor(std::move(fp));
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001173 return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
Michael Ludwig22429f92019-06-27 10:44:48 -04001174 }
1175}
1176
Robert Phillipse837e612019-11-15 11:02:50 -05001177// A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1178class GrTextureOp::BatchSizeLimiter {
1179public:
John Stiles0fbc6a32021-06-04 14:40:57 -04001180 BatchSizeLimiter(GrSurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001181 const GrClip* clip,
Robert Phillipse837e612019-11-15 11:02:50 -05001182 GrRecordingContext* context,
1183 int numEntries,
1184 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001185 GrSamplerState::MipmapMode mm,
Robert Phillipse837e612019-11-15 11:02:50 -05001186 GrTextureOp::Saturate saturate,
1187 SkCanvas::SrcRectConstraint constraint,
1188 const SkMatrix& viewMatrix,
1189 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
John Stiles0fbc6a32021-06-04 14:40:57 -04001190 : fSDC(sdc)
Robert Phillipse837e612019-11-15 11:02:50 -05001191 , fClip(clip)
1192 , fContext(context)
1193 , fFilter(filter)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001194 , fMipmapMode(mm)
Robert Phillipse837e612019-11-15 11:02:50 -05001195 , fSaturate(saturate)
1196 , fConstraint(constraint)
1197 , fViewMatrix(viewMatrix)
1198 , fTextureColorSpaceXform(textureColorSpaceXform)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001199 , fNumLeft(numEntries) {}
Brian Salomon34169692017-08-28 15:32:01 -04001200
Brian Salomoneebe7352020-12-09 16:37:04 -05001201 void createOp(GrSurfaceDrawContext::TextureSetEntry set[],
Robert Phillipse837e612019-11-15 11:02:50 -05001202 int clumpSize,
1203 GrAAType aaType) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001204 int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
Herb Derbyc76d4092020-10-07 16:46:15 -04001205 GrOp::Owner op = TextureOp::Make(fContext,
1206 &set[fNumClumped],
1207 clumpSize,
1208 clumpProxyCount,
1209 fFilter,
1210 fMipmapMode,
1211 fSaturate,
1212 aaType,
1213 fConstraint,
1214 fViewMatrix,
1215 fTextureColorSpaceXform);
John Stiles0fbc6a32021-06-04 14:40:57 -04001216 fSDC->addDrawOp(fClip, std::move(op));
Robert Phillipse837e612019-11-15 11:02:50 -05001217
1218 fNumLeft -= clumpSize;
1219 fNumClumped += clumpSize;
1220 }
1221
1222 int numLeft() const { return fNumLeft; }
1223 int baseIndex() const { return fNumClumped; }
1224
1225private:
John Stiles0fbc6a32021-06-04 14:40:57 -04001226 GrSurfaceDrawContext* fSDC;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001227 const GrClip* fClip;
Robert Phillipse837e612019-11-15 11:02:50 -05001228 GrRecordingContext* fContext;
1229 GrSamplerState::Filter fFilter;
Brian Salomone69b9ef2020-07-22 11:18:06 -04001230 GrSamplerState::MipmapMode fMipmapMode;
Robert Phillipse837e612019-11-15 11:02:50 -05001231 GrTextureOp::Saturate fSaturate;
1232 SkCanvas::SrcRectConstraint fConstraint;
1233 const SkMatrix& fViewMatrix;
1234 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
1235
1236 int fNumLeft;
1237 int fNumClumped = 0; // also the offset for the start of the next clump
1238};
1239
1240// Greedily clump quad draws together until the index buffer limit is exceeded.
Brian Salomoneebe7352020-12-09 16:37:04 -05001241void GrTextureOp::AddTextureSetOps(GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001242 const GrClip* clip,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001243 GrRecordingContext* context,
Brian Salomoneebe7352020-12-09 16:37:04 -05001244 GrSurfaceDrawContext::TextureSetEntry set[],
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001245 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -05001246 int proxyRunCnt,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001247 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001248 GrSamplerState::MipmapMode mm,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001249 Saturate saturate,
1250 SkBlendMode blendMode,
1251 GrAAType aaType,
1252 SkCanvas::SrcRectConstraint constraint,
1253 const SkMatrix& viewMatrix,
1254 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001255 // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1256 // the op's metadata so we don't need to worry about overflow.
Michael Ludwig4ef1ca12019-12-19 10:58:52 -05001257 SkDEBUGCODE(TextureOp::ValidateResourceLimits();)
Michael Ludwig379e4962019-12-06 13:21:26 -05001258 SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1259
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001260 // First check if we can support batches as a single op
1261 if (blendMode != SkBlendMode::kSrcOver ||
1262 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1263 // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1264 // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1265 // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1266 SkMatrix ctm;
1267 for (int i = 0; i < cnt; ++i) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001268 ctm = viewMatrix;
1269 if (set[i].fPreViewMatrix) {
1270 ctm.preConcat(*set[i].fPreViewMatrix);
1271 }
Robert Phillipse837e612019-11-15 11:02:50 -05001272
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001273 DrawQuad quad;
1274 quad.fEdgeFlags = set[i].fAAFlags;
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001275 if (set[i].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001276 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001277
1278 SkPoint srcPts[4];
1279 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001280 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001281 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001282 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1283 quad.fLocal = GrQuad(set[i].fSrcRect);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001284 }
1285
Brian Salomon2432d062020-04-16 20:48:09 -04001286 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001287 ? &set[i].fSrcRect : nullptr;
1288
Brian Salomonfc118442019-11-22 19:09:27 -05001289 auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001290 filter, mm, set[i].fColor, saturate, blendMode, aaType, &quad, subset);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001291 rtc->addDrawOp(clip, std::move(op));
1292 }
1293 return;
1294 }
1295
1296 // Second check if we can always just make a single op and avoid the extra iteration
Robert Phillipse837e612019-11-15 11:02:50 -05001297 // needed to clump things together.
Brian Osman788b9162020-02-07 10:36:46 -05001298 if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
Robert Phillipse837e612019-11-15 11:02:50 -05001299 GrResourceProvider::MaxNumAAQuads())) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001300 auto op = TextureOp::Make(context, set, cnt, proxyRunCnt, filter, mm, saturate, aaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001301 constraint, viewMatrix, std::move(textureColorSpaceXform));
1302 rtc->addDrawOp(clip, std::move(op));
1303 return;
1304 }
1305
Brian Salomone69b9ef2020-07-22 11:18:06 -04001306 BatchSizeLimiter state(rtc, clip, context, cnt, filter, mm, saturate, constraint, viewMatrix,
Robert Phillipse837e612019-11-15 11:02:50 -05001307 std::move(textureColorSpaceXform));
1308
1309 // kNone and kMSAA never get altered
1310 if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1311 // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1312 while (state.numLeft() > 0) {
Brian Osman788b9162020-02-07 10:36:46 -05001313 int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
Robert Phillipse837e612019-11-15 11:02:50 -05001314
1315 state.createOp(set, clumpSize, aaType);
1316 }
1317 } else {
1318 // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1319 // can also get downgraded to kNone if all the quads are on integer coordinates and
1320 // axis-aligned.
1321 SkASSERT(aaType == GrAAType::kCoverage);
1322
1323 while (state.numLeft() > 0) {
1324 GrAAType runningAA = GrAAType::kNone;
1325 bool clumped = false;
1326
1327 for (int i = 0; i < state.numLeft(); ++i) {
1328 int absIndex = state.baseIndex() + i;
1329
Robert Phillips6bf11252020-07-31 12:15:00 -04001330 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone ||
1331 runningAA == GrAAType::kCoverage) {
Robert Phillipse837e612019-11-15 11:02:50 -05001332
1333 if (i >= GrResourceProvider::MaxNumAAQuads()) {
1334 // Here we either need to boost the AA type to kCoverage, but doing so with
1335 // all the accumulated quads would overflow, or we have a set of AA quads
1336 // that has just gotten too large. In either case, calve off the existing
1337 // quads as their own TextureOp.
1338 state.createOp(
1339 set,
1340 runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1341 runningAA); // maybe downgrading AA here
1342 clumped = true;
1343 break;
1344 }
1345
1346 runningAA = GrAAType::kCoverage;
1347 } else if (runningAA == GrAAType::kNone) {
1348
1349 if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1350 // Here we've found a consistent batch of non-AA quads that has gotten too
1351 // large. Calve it off as its own GrTextureOp.
1352 state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1353 GrAAType::kNone); // definitely downgrading AA here
1354 clumped = true;
1355 break;
1356 }
1357 }
1358 }
1359
1360 if (!clumped) {
1361 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1362 // quads and call it a day.
1363 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1364 }
1365 }
1366 }
1367}
Robert Phillipsae01f622019-11-13 15:56:31 +00001368
Brian Salomon34169692017-08-28 15:32:01 -04001369#if GR_TEST_UTILS
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04001370#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001371#include "src/gpu/GrProxyProvider.h"
1372#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -04001373
1374GR_DRAW_OP_TEST_DEFINE(TextureOp) {
Brian Salomona56a7462020-02-07 14:17:25 -05001375 SkISize dims;
1376 dims.fHeight = random->nextULessThan(90) + 10;
1377 dims.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001378 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001379 GrMipmapped mipMapped = random->nextBool() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel09c94002018-06-08 22:11:51 +00001380 SkBackingFit fit = SkBackingFit::kExact;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001381 if (mipMapped == GrMipmapped::kNo) {
Greg Daniel09c94002018-06-08 22:11:51 +00001382 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1383 }
Greg Daniel4065d452018-11-16 15:43:41 -05001384 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -04001385 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1386 GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -05001387 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -04001388 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001389 format, dims, GrRenderable::kNo, 1, mipMapped, fit, SkBudgeted::kNo, GrProtected::kNo,
1390 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001391
Brian Salomon34169692017-08-28 15:32:01 -04001392 SkRect rect = GrTest::TestRect(random);
1393 SkRect srcRect;
1394 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1395 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1396 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1397 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1398 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -05001399 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001400 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
Brian Salomone69b9ef2020-07-22 11:18:06 -04001401 static_cast<uint32_t>(GrSamplerState::Filter::kLast) + 1);
1402 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
1403 if (mipMapped == GrMipmapped::kYes) {
1404 mm = (GrSamplerState::MipmapMode)random->nextULessThan(
1405 static_cast<uint32_t>(GrSamplerState::MipmapMode::kLast) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +00001406 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001407
Brian Osman3ebd3542018-07-30 14:36:53 -04001408 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001409 GrAAType aaType = GrAAType::kNone;
1410 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06001411 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -05001412 }
Brian Salomon2213ee92018-10-02 10:44:21 -04001413 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1414 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1415 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1416 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1417 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomon2432d062020-04-16 20:48:09 -04001418 bool useSubset = random->nextBool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001419 auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
Greg Daniel549325c2019-10-30 16:19:20 -04001420 GrSurfaceProxyView proxyView(
1421 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -05001422 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Brian Salomonfc118442019-11-22 19:09:27 -05001423 auto alphaType = static_cast<SkAlphaType>(
1424 random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
Greg Daniel549325c2019-10-30 16:19:20 -04001425
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001426 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
Brian Salomonfc118442019-11-22 19:09:27 -05001427 return GrTextureOp::Make(context, std::move(proxyView), alphaType, std::move(texXform), filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001428 mm, color, saturate, SkBlendMode::kSrcOver, aaType, &quad,
1429 useSubset ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -04001430}
1431
1432#endif