blob: 4e11cc284ca31a7b462da6a18d4b3ca1b8e0efaa [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"
Robert Phillipse40495d2021-07-20 09:40:13 -040025#include "src/gpu/GrOpsTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrRecordingContextPriv.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrResourceProviderPriv.h"
29#include "src/gpu/GrShaderCaps.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000030#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/SkGr.h"
John Stilesf743d4e2020-07-23 11:35:08 -040033#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040034#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040035#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040036#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040037#include "src/gpu/geometry/GrQuadUtils.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040038#include "src/gpu/geometry/GrRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/glsl/GrGLSLVarying.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040040#include "src/gpu/ops/GrFillRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/ops/GrMeshDrawOp.h"
42#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050043#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Robert Phillips21453902021-08-27 16:05:04 -040044#include "src/gpu/ops/TextureOp.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040045#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Brian Salomon34169692017-08-28 15:32:01 -040046
47namespace {
48
Brian Salomon2432d062020-04-16 20:48:09 -040049using Subset = GrQuadPerEdgeAA::Subset;
Michael Ludwigc182b942018-11-16 10:27:51 -050050using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050051using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040052
Michael Ludwig22429f92019-06-27 10:44:48 -040053// Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
54// between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
Robert Phillips21453902021-08-27 16:05:04 -040055SkSize axis_aligned_quad_size(const GrQuad& quad) {
Michael Ludwig22429f92019-06-27 10:44:48 -040056 SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
57 // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
58 float dw = sk_float_abs(quad.x(2) - quad.x(0)) + sk_float_abs(quad.y(2) - quad.y(0));
59 float dh = sk_float_abs(quad.x(1) - quad.x(0)) + sk_float_abs(quad.y(1) - quad.y(0));
60 return {dw, dh};
61}
62
Robert Phillips21453902021-08-27 16:05:04 -040063std::tuple<bool /* filter */,
64 bool /* mipmap */>
Brian Salomone69b9ef2020-07-22 11:18:06 -040065filter_and_mm_have_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
Michael Ludwig22429f92019-06-27 10:44:48 -040066 // If not axis-aligned in src or dst, then always say it has an effect
67 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
68 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040069 return {true, true};
Michael Ludwig22429f92019-06-27 10:44:48 -040070 }
71
72 SkRect srcRect;
73 SkRect dstRect;
74 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
75 // Disable filtering when there is no scaling (width and height are the same), and the
76 // top-left corners have the same fraction (so src and dst snap to the pixel grid
77 // identically).
78 SkASSERT(srcRect.isSorted());
Brian Salomone69b9ef2020-07-22 11:18:06 -040079 bool filter = srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
80 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
81 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
82 bool mm = srcRect.width() > dstRect.width() || srcRect.height() > dstRect.height();
83 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040084 }
Brian Salomone69b9ef2020-07-22 11:18:06 -040085 // Extract edge lengths
86 SkSize srcSize = axis_aligned_quad_size(srcQuad);
87 SkSize dstSize = axis_aligned_quad_size(dstQuad);
88 // Although the quads are axis-aligned, the local coordinate system is transformed such
89 // that fractionally-aligned sample centers will not align with the device coordinate system
90 // So disable filtering when edges are the same length and both srcQuad and dstQuad
91 // 0th vertex is integer aligned.
92 bool filter = srcSize != dstSize ||
93 !SkScalarIsInt(srcQuad.x(0)) ||
94 !SkScalarIsInt(srcQuad.y(0)) ||
95 !SkScalarIsInt(dstQuad.x(0)) ||
96 !SkScalarIsInt(dstQuad.y(0));
97 bool mm = srcSize.fWidth > dstSize.fWidth || srcSize.fHeight > dstSize.fHeight;
98 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040099}
100
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500101// Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
102// regular and rectangular textures, w/ or w/o origin correction.
103struct NormalizationParams {
104 float fIW; // 1 / width of texture, or 1.0 for texture rectangles
Michael Ludwigc453a502020-05-29 12:29:12 -0400105 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 -0500106 float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
107};
Robert Phillips21453902021-08-27 16:05:04 -0400108NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
109 GrSurfaceOrigin origin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500110 // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
111 // normalize the src coordinates up front.
Michael Ludwigadb12e72019-12-04 16:19:18 -0500112 SkISize dimensions = proxy->backingStoreDimensions();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500113 float iw, ih, h;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500114 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500115 iw = ih = 1.f;
116 h = dimensions.height();
117 } else {
118 iw = 1.f / dimensions.width();
119 ih = 1.f / dimensions.height();
120 h = 1.f;
121 }
122
Michael Ludwigadb12e72019-12-04 16:19:18 -0500123 if (origin == kBottomLeft_GrSurfaceOrigin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500124 return {iw, -ih, h};
125 } else {
126 return {iw, ih, 0.0f};
127 }
128}
129
Brian Salomon2432d062020-04-16 20:48:09 -0400130// Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500131// 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 -0400132// subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
133// and y-flipping are applied as indicated by NormalizationParams.
Robert Phillips21453902021-08-27 16:05:04 -0400134SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
135 const NormalizationParams& params,
136 const SkRect* subsetRect) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500137 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Brian Salomon2432d062020-04-16 20:48:09 -0400138 if (!subsetRect) {
139 // Either the quad has no subset constraint and is batched with a subset constrained op
140 // (in which case we want a subset that doesn't restrict normalized tex coords), or the
141 // entire op doesn't use the subset, in which case the returned value is ignored.
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500142 return kLargeRect;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400143 }
144
Brian Salomon2432d062020-04-16 20:48:09 -0400145 auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400146 auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
147 if (filter == GrSamplerState::Filter::kNearest) {
148 // Make sure our insetting puts us at pixel centers.
149 ltrb = skvx::floor(ltrb*flipHi)*flipHi;
150 }
151 // Inset with pin to the rect center.
152 ltrb += skvx::Vec<4, float>({.5f, .5f, -.5f, -.5f});
153 auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
154 ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
155
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500156 // Normalize and offset
Elliot Evans9fdddf62020-07-29 16:39:11 -0400157 ltrb = ltrb * skvx::Vec<4, float>{params.fIW, params.fInvH, params.fIW, params.fInvH} +
158 skvx::Vec<4, float>{0.f, params.fYOffset, 0.f, params.fYOffset};
Michael Ludwigc453a502020-05-29 12:29:12 -0400159 if (params.fInvH < 0.f) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500160 // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
161 ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400162 }
163
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500164 SkRect out;
165 ltrb.store(&out);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500166 return out;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400167}
168
Michael Ludwig009b92e2019-02-15 16:03:53 -0500169// Normalizes logical src coords and corrects for origin
Robert Phillips21453902021-08-27 16:05:04 -0400170void normalize_src_quad(const NormalizationParams& params,
171 GrQuad* srcQuad) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500172 // The src quad should not have any perspective
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500173 SkASSERT(!srcQuad->hasPerspective());
174 skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
Elliot Evans9fdddf62020-07-29 16:39:11 -0400175 skvx::Vec<4, float> ys = srcQuad->y4f() * params.fInvH + params.fYOffset;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500176 xs.store(srcQuad->xs());
177 ys.store(srcQuad->ys());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500178}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400179
Michael Ludwig379e4962019-12-06 13:21:26 -0500180// Count the number of proxy runs in the entry set. This usually is already computed by
181// SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
182// for each split.
Robert Phillips21453902021-08-27 16:05:04 -0400183int proxy_run_count(const GrTextureSetEntry set[], int count) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500184 int actualProxyRunCount = 0;
185 const GrSurfaceProxy* lastProxy = nullptr;
186 for (int i = 0; i < count; ++i) {
187 if (set[i].fProxyView.proxy() != lastProxy) {
188 actualProxyRunCount++;
189 lastProxy = set[i].fProxyView.proxy();
190 }
191 }
192 return actualProxyRunCount;
193}
194
Robert Phillips21453902021-08-27 16:05:04 -0400195bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
196 const DrawQuad& quad, const SkRect& subsetRect) {
John Stilescbe4e282020-06-01 10:38:31 -0400197 // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
198 // can push all the way up to the edges of the the subset rect and the sampler shouldn't
199 // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
200 // the non-antialiased case.
201 SkRect localBounds = quad.fLocal.bounds();
202 if (aaType == GrAAType::kNone &&
203 filter == GrSamplerState::Filter::kNearest &&
204 quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
205 quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
206 subsetRect.contains(localBounds)) {
207
208 return true;
209 }
210
211 // If the subset rect is inset by at least 0.5 pixels into the local quad's bounds, the
212 // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
213 if (subsetRect.makeInset(0.5f, 0.5f).contains(localBounds)) {
214 return true;
215 }
216
217 // The subset rect cannot be ignored safely.
218 return false;
219}
220
Brian Salomon34169692017-08-28 15:32:01 -0400221/**
222 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
223 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
224 */
Robert Phillips21453902021-08-27 16:05:04 -0400225class TextureOpImpl final : public GrMeshDrawOp {
Brian Salomon34169692017-08-28 15:32:01 -0400226public:
Robert Phillips21453902021-08-27 16:05:04 -0400227 using Saturate = skgpu::v1::TextureOp::Saturate;
228
Herb Derbyc76d4092020-10-07 16:46:15 -0400229 static GrOp::Owner Make(GrRecordingContext* context,
230 GrSurfaceProxyView proxyView,
231 sk_sp<GrColorSpaceXform> textureXform,
232 GrSamplerState::Filter filter,
233 GrSamplerState::MipmapMode mm,
234 const SkPMColor4f& color,
Robert Phillips21453902021-08-27 16:05:04 -0400235 Saturate saturate,
Herb Derbyc76d4092020-10-07 16:46:15 -0400236 GrAAType aaType,
237 DrawQuad* quad,
238 const SkRect* subset) {
239
Robert Phillips21453902021-08-27 16:05:04 -0400240 return GrOp::Make<TextureOpImpl>(context, std::move(proxyView), std::move(textureXform),
241 filter, mm, color, saturate, aaType, quad, subset);
Brian Salomon34169692017-08-28 15:32:01 -0400242 }
Robert Phillipse837e612019-11-15 11:02:50 -0500243
Herb Derbyc76d4092020-10-07 16:46:15 -0400244 static GrOp::Owner Make(GrRecordingContext* context,
Robert Phillipse40495d2021-07-20 09:40:13 -0400245 GrTextureSetEntry set[],
Herb Derbyc76d4092020-10-07 16:46:15 -0400246 int cnt,
247 int proxyRunCnt,
248 GrSamplerState::Filter filter,
249 GrSamplerState::MipmapMode mm,
Robert Phillips21453902021-08-27 16:05:04 -0400250 Saturate saturate,
Herb Derbyc76d4092020-10-07 16:46:15 -0400251 GrAAType aaType,
252 SkCanvas::SrcRectConstraint constraint,
253 const SkMatrix& viewMatrix,
254 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500255 // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
256 SkASSERT(proxyRunCnt <= cnt);
Robert Phillips21453902021-08-27 16:05:04 -0400257 return GrOp::MakeWithExtraMemory<TextureOpImpl>(
Herb Derbyc76d4092020-10-07 16:46:15 -0400258 context, sizeof(ViewCountPair) * (proxyRunCnt - 1),
259 set, cnt, proxyRunCnt, filter, mm, saturate, aaType, constraint,
260 viewMatrix, std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400261 }
Brian Salomon34169692017-08-28 15:32:01 -0400262
Robert Phillips21453902021-08-27 16:05:04 -0400263 ~TextureOpImpl() override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500264 for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400265 fViewCountPairs[p].~ViewCountPair();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400266 }
267 }
Brian Salomon34169692017-08-28 15:32:01 -0400268
269 const char* name() const override { return "TextureOp"; }
270
Robert Phillips294723d2021-06-17 09:23:58 -0400271 void visitProxies(const GrVisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400272 bool mipped = (fMetadata.mipmapMode() != GrSamplerState::MipmapMode::kNone);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500273 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400274 func(fViewCountPairs[p].fProxy.get(), GrMipmapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400275 }
Chris Daltondbb833b2020-03-17 12:15:46 -0600276 if (fDesc && fDesc->fProgramInfo) {
277 fDesc->fProgramInfo->visitFPProxies(func);
278 }
Brian Salomond7065e72018-10-12 11:42:02 -0400279 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400280
John Stiles8d9bf642020-08-12 15:07:45 -0400281#ifdef SK_DEBUG
Michael Ludwig4ef1ca12019-12-19 10:58:52 -0500282 static void ValidateResourceLimits() {
283 // The op implementation has an upper bound on the number of quads that it can represent.
284 // However, the resource manager imposes its own limit on the number of quads, which should
285 // always be lower than the numerical limit this op can hold.
286 using CountStorage = decltype(Metadata::fTotalQuadCount);
287 CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
288 // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
289 int resourceLimit = SkTo<int>(maxQuadCount);
290 SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
291 GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
292 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500293#endif
Brian Salomon34169692017-08-28 15:32:01 -0400294
Chris Dalton57ab06c2021-04-22 12:57:28 -0600295 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip*,
296 GrClampType clampType) override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500297 SkASSERT(fMetadata.colorType() == ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400298 auto iter = fQuads.metadata();
299 while(iter.next()) {
Brian Osman2715bf52019-12-06 14:38:47 -0500300 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
Brian Salomon5e29e312021-04-27 11:30:00 -0400301 colorType = std::max(static_cast<GrQuadPerEdgeAA::ColorType>(fMetadata.fColorType),
302 colorType);
303 if (caps.reducedShaderMode()) {
304 colorType = std::max(colorType, GrQuadPerEdgeAA::ColorType::kByte);
305 }
306 fMetadata.fColorType = static_cast<uint16_t>(colorType);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400307 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700308 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400309 }
310
Brian Salomon485b8c62018-01-12 15:11:06 -0500311 FixedFunctionFlags fixedFunctionFlags() const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500312 return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
313 : FixedFunctionFlags::kNone;
Brian Salomon485b8c62018-01-12 15:11:06 -0500314 }
Brian Salomon34169692017-08-28 15:32:01 -0400315
316 DEFINE_OP_CLASS_ID
317
318private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400319 friend class ::GrOp;
Brian Salomon762d5e72017-12-01 10:25:08 -0500320
Brian Salomon2432d062020-04-16 20:48:09 -0400321 struct ColorSubsetAndAA {
322 ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400323 : fColor(color)
Brian Salomon2432d062020-04-16 20:48:09 -0400324 , fSubsetRect(subsetRect)
Michael Ludwig4384f042019-12-05 10:30:35 -0500325 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
326 SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
Michael Ludwig425eb452019-06-27 10:13:27 -0400327 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400328
329 SkPMColor4f fColor;
Brian Salomon2432d062020-04-16 20:48:09 -0400330 // 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 -0500331 // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
Brian Salomon2432d062020-04-16 20:48:09 -0400332 SkRect fSubsetRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400333 unsigned fAAFlags : 4;
334
Michael Ludwig425eb452019-06-27 10:13:27 -0400335 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
336 };
Michael Ludwigadb12e72019-12-04 16:19:18 -0500337
Greg Daniel549325c2019-10-30 16:19:20 -0400338 struct ViewCountPair {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500339 // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
340 // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
341 // swizzle so that is stored in the op metadata.
342 sk_sp<GrSurfaceProxy> fProxy;
Michael Ludwig425eb452019-06-27 10:13:27 -0400343 int fQuadCnt;
344 };
345
Michael Ludwigadb12e72019-12-04 16:19:18 -0500346 // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
347 // increase the size of the op; increasing the op size can have a surprising impact on
348 // performance (since texture ops are one of the most commonly used in an app).
349 struct Metadata {
350 // AAType must be filled after initialization; ColorType is determined in finalize()
Brian Salomone69b9ef2020-07-22 11:18:06 -0400351 Metadata(const GrSwizzle& swizzle,
352 GrSamplerState::Filter filter,
353 GrSamplerState::MipmapMode mm,
354 GrQuadPerEdgeAA::Subset subset,
Robert Phillips21453902021-08-27 16:05:04 -0400355 Saturate saturate)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500356 : fSwizzle(swizzle)
357 , fProxyCount(1)
358 , fTotalQuadCount(1)
Michael Ludwig4384f042019-12-05 10:30:35 -0500359 , fFilter(static_cast<uint16_t>(filter))
Brian Salomone69b9ef2020-07-22 11:18:06 -0400360 , fMipmapMode(static_cast<uint16_t>(mm))
Michael Ludwig4384f042019-12-05 10:30:35 -0500361 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
362 , fColorType(static_cast<uint16_t>(ColorType::kNone))
Brian Salomon2432d062020-04-16 20:48:09 -0400363 , fSubset(static_cast<uint16_t>(subset))
Michael Ludwig4384f042019-12-05 10:30:35 -0500364 , fSaturate(static_cast<uint16_t>(saturate)) {}
Michael Ludwigadb12e72019-12-04 16:19:18 -0500365
Michael Ludwig4384f042019-12-05 10:30:35 -0500366 GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
Michael Ludwigadb12e72019-12-04 16:19:18 -0500367 uint16_t fProxyCount;
368 // This will be >= fProxyCount, since a proxy may be drawn multiple times
369 uint16_t fTotalQuadCount;
370
Michael Ludwig4384f042019-12-05 10:30:35 -0500371 // These must be based on uint16_t to help MSVC's pack bitfields optimally
372 uint16_t fFilter : 2; // GrSamplerState::Filter
Brian Salomone69b9ef2020-07-22 11:18:06 -0400373 uint16_t fMipmapMode : 2; // GrSamplerState::MipmapMode
Michael Ludwig4384f042019-12-05 10:30:35 -0500374 uint16_t fAAType : 2; // GrAAType
375 uint16_t fColorType : 2; // GrQuadPerEdgeAA::ColorType
Brian Salomon2432d062020-04-16 20:48:09 -0400376 uint16_t fSubset : 1; // bool
Michael Ludwig4384f042019-12-05 10:30:35 -0500377 uint16_t fSaturate : 1; // bool
Brian Salomone69b9ef2020-07-22 11:18:06 -0400378 uint16_t fUnused : 6; // # of bits left before Metadata exceeds 8 bytes
Michael Ludwigadb12e72019-12-04 16:19:18 -0500379
380 GrSamplerState::Filter filter() const {
381 return static_cast<GrSamplerState::Filter>(fFilter);
382 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400383 GrSamplerState::MipmapMode mipmapMode() const {
384 return static_cast<GrSamplerState::MipmapMode>(fMipmapMode);
385 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500386 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
387 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
Brian Salomon2432d062020-04-16 20:48:09 -0400388 Subset subset() const { return static_cast<Subset>(fSubset); }
Robert Phillips21453902021-08-27 16:05:04 -0400389 Saturate saturate() const { return static_cast<Saturate>(fSaturate); }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500390
391 static_assert(GrSamplerState::kFilterCount <= 4);
392 static_assert(kGrAATypeCount <= 4);
393 static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
394 };
Michael Ludwig4384f042019-12-05 10:30:35 -0500395 static_assert(sizeof(Metadata) == 8);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500396
Chris Daltondbb833b2020-03-17 12:15:46 -0600397 // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
398 // store the data in a separate struct in order to minimize the size of the TextureOp.
399 // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
400 // may want to re-evaluate whether this is still necessary.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400401 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600402 // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
403 // allocatePrePreparedVertices is also called.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400404 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600405 // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
406 // part of the flushState).
407 struct Desc {
408 VertexSpec fVertexSpec;
409 int fNumProxies = 0;
410 int fNumTotalQuads = 0;
Robert Phillips32803ff2019-10-23 08:26:08 -0400411
Chris Daltondbb833b2020-03-17 12:15:46 -0600412 // This member variable is only used by 'onPrePrepareDraws'.
413 char* fPrePreparedVertices = nullptr;
414
415 GrProgramInfo* fProgramInfo = nullptr;
416
417 sk_sp<const GrBuffer> fIndexBuffer;
418 sk_sp<const GrBuffer> fVertexBuffer;
419 int fBaseVertex;
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400420
421 // How big should 'fVertices' be to hold all the vertex data?
422 size_t totalSizeInBytes() const {
Chris Daltondbb833b2020-03-17 12:15:46 -0600423 return this->totalNumVertices() * fVertexSpec.vertexSize();
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400424 }
425
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400426 int totalNumVertices() const {
427 return fNumTotalQuads * fVertexSpec.verticesPerQuad();
428 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400429
Chris Daltondbb833b2020-03-17 12:15:46 -0600430 void allocatePrePreparedVertices(SkArenaAlloc* arena) {
431 fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400432 }
Robert Phillips32803ff2019-10-23 08:26:08 -0400433 };
Brian Salomon2432d062020-04-16 20:48:09 -0400434 // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
Robert Phillips21453902021-08-27 16:05:04 -0400435 TextureOpImpl(GrSurfaceProxyView proxyView,
436 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
437 GrSamplerState::Filter filter,
438 GrSamplerState::MipmapMode mm,
439 const SkPMColor4f& color,
440 Saturate saturate,
441 GrAAType aaType,
442 DrawQuad* quad,
443 const SkRect* subsetRect)
Brian Salomon34169692017-08-28 15:32:01 -0400444 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400445 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400446 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600447 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400448 , fMetadata(proxyView.swizzle(), filter, mm, Subset(!!subsetRect), saturate) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400449 // Clean up disparities between the overall aa type and edge configuration and apply
450 // optimizations based on the rect and matrix when appropriate
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500451 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
452 &aaType, &quad->fEdgeFlags);
Michael Ludwig4384f042019-12-05 10:30:35 -0500453 fMetadata.fAAType = static_cast<uint16_t>(aaType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400454
Brian Salomonf1709042018-10-03 11:57:00 -0400455 // We expect our caller to have already caught this optimization.
Brian Salomon2432d062020-04-16 20:48:09 -0400456 SkASSERT(!subsetRect ||
457 !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500458
Brian Salomonf09abc52018-10-03 15:59:04 -0400459 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
John Stilescbe4e282020-06-01 10:38:31 -0400460 // Try to identify cases where the subsetting isn't actually necessary, and skip it.
461 if (subsetRect) {
462 if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
463 subsetRect = nullptr;
464 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
465 }
Brian Salomonf09abc52018-10-03 15:59:04 -0400466 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500467
Brian Salomon2432d062020-04-16 20:48:09 -0400468 // Normalize src coordinates and the subset (if set)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500469 NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
470 proxyView.origin());
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500471 normalize_src_quad(params, &quad->fLocal);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400472 SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500473
Michael Ludwig949ceb22020-02-07 10:14:45 -0500474 // Set bounds before clipping so we don't have to worry about unioning the bounds of
475 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig575c9212021-07-13 11:09:52 -0400476 bool hairline = GrQuadUtils::WillUseHairline(quad->fDevice, aaType, quad->fEdgeFlags);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500477 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Michael Ludwig575c9212021-07-13 11:09:52 -0400478 hairline ? IsHairline::kYes : IsHairline::kNo);
Brian Salomon2432d062020-04-16 20:48:09 -0400479 int quadCount = this->appendQuad(quad, color, subset);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500480 fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
Brian Salomond7065e72018-10-12 11:42:02 -0400481 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400482
Robert Phillips21453902021-08-27 16:05:04 -0400483 TextureOpImpl(GrTextureSetEntry set[],
484 int cnt,
485 int proxyRunCnt,
486 const GrSamplerState::Filter filter,
487 const GrSamplerState::MipmapMode mm,
488 const Saturate saturate,
489 const GrAAType aaType,
490 const SkCanvas::SrcRectConstraint constraint,
491 const SkMatrix& viewMatrix,
492 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400493 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400494 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400495 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600496 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400497 , fMetadata(set[0].fProxyView.swizzle(),
498 GrSamplerState::Filter::kNearest,
499 GrSamplerState::MipmapMode::kNone,
500 Subset::kNo,
501 saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500502 // Update counts to reflect the batch op
Michael Ludwig379e4962019-12-06 13:21:26 -0500503 fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500504 fMetadata.fTotalQuadCount = SkToUInt(cnt);
505
Brian Salomond7065e72018-10-12 11:42:02 -0400506 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500507
508 GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon2432d062020-04-16 20:48:09 -0400509 Subset netSubset = Subset::kNo;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500510 GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400511 GrSamplerState::MipmapMode netMM = GrSamplerState::MipmapMode::kNone;
Michael Ludwig575c9212021-07-13 11:09:52 -0400512 bool hasSubpixel = false;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500513
Michael Ludwig379e4962019-12-06 13:21:26 -0500514 const GrSurfaceProxy* curProxy = nullptr;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500515
Michael Ludwig379e4962019-12-06 13:21:26 -0500516 // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
517 // increases when set[q]'s proxy changes.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500518 int p = 0;
519 for (int q = 0; q < cnt; ++q) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400520 SkASSERT(mm == GrSamplerState::MipmapMode::kNone ||
521 (set[0].fProxyView.proxy()->asTextureProxy()->mipmapped() ==
522 GrMipmapped::kYes));
Michael Ludwig379e4962019-12-06 13:21:26 -0500523 if (q == 0) {
Greg Daniel549325c2019-10-30 16:19:20 -0400524 // We do not placement new the first ViewCountPair since that one is allocated and
525 // initialized as part of the GrTextureOp creation.
Michael Ludwig379e4962019-12-06 13:21:26 -0500526 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
527 fViewCountPairs[0].fQuadCnt = 0;
528 curProxy = fViewCountPairs[0].fProxy.get();
529 } else if (set[q].fProxyView.proxy() != curProxy) {
Greg Daniel549325c2019-10-30 16:19:20 -0400530 // We must placement new the ViewCountPairs here so that the sk_sps in the
531 // GrSurfaceProxyView get initialized properly.
Michael Ludwig379e4962019-12-06 13:21:26 -0500532 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
Michael Ludwigadb12e72019-12-04 16:19:18 -0500533
Michael Ludwig379e4962019-12-06 13:21:26 -0500534 curProxy = fViewCountPairs[p].fProxy.get();
Greg Danielc71c7962020-01-14 16:44:18 -0500535 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
536 curProxy, fViewCountPairs[0].fProxy.get()));
Michael Ludwig379e4962019-12-06 13:21:26 -0500537 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
Michael Ludwig379e4962019-12-06 13:21:26 -0500538 } // else another quad referencing the same proxy
Michael Ludwigce62dec2019-02-19 11:48:46 -0500539
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500540 SkMatrix ctm = viewMatrix;
Michael Ludwig379e4962019-12-06 13:21:26 -0500541 if (set[q].fPreViewMatrix) {
542 ctm.preConcat(*set[q].fPreViewMatrix);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500543 }
544
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400545 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
546 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500547 DrawQuad quad;
Michael Ludwig379e4962019-12-06 13:21:26 -0500548 if (set[q].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500549 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400550
551 SkPoint srcPts[4];
Michael Ludwig379e4962019-12-06 13:21:26 -0500552 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500553 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400554 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500555 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
556 quad.fLocal = GrQuad(set[q].fSrcRect);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400557 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500558
Michael Ludwig2ed4a362021-08-17 21:32:12 -0400559 // This may be reduced per-quad from the requested aggregate filtering level, and used
560 // to determine if the subset is needed for the entry as well.
561 GrSamplerState::Filter filterForQuad = filter;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400562 if (netFilter != filter || netMM != mm) {
563 // The only way netFilter != filter is if linear is requested and we haven't yet
564 // found a quad that requires linear (so net is still nearest). Similar for mip
565 // mapping.
Brian Salomonf7353512020-07-22 19:26:48 -0400566 SkASSERT(filter == netFilter ||
567 (netFilter == GrSamplerState::Filter::kNearest && filter > netFilter));
568 SkASSERT(mm == netMM ||
569 (netMM == GrSamplerState::MipmapMode::kNone && mm > netMM));
Brian Salomone69b9ef2020-07-22 11:18:06 -0400570 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad.fLocal, quad.fDevice);
Michael Ludwig2ed4a362021-08-17 21:32:12 -0400571 if (filter != GrSamplerState::Filter::kNearest) {
572 if (mustFilter) {
573 netFilter = filter; // upgrade batch to higher filter level
574 } else {
575 filterForQuad = GrSamplerState::Filter::kNearest; // downgrade entry
576 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400577 }
Michael Ludwig03f17cd2021-08-17 18:27:48 +0000578 if (mustMM && mm != GrSamplerState::MipmapMode::kNone) {
579 netMM = mm;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400580 }
Michael Ludwig22429f92019-06-27 10:44:48 -0400581 }
582
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500583 // Determine the AA type for the quad, then merge with net AA type
Michael Ludwig6bee7762018-10-19 09:50:36 -0400584 GrAAType aaForQuad;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500585 GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
586 &aaForQuad, &quad.fEdgeFlags);
Michael Ludwig81159972021-07-13 15:42:04 -0400587 // Update overall bounds of the op as the union of all quads
588 bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
589 hasSubpixel |= GrQuadUtils::WillUseHairline(quad.fDevice, aaForQuad, quad.fEdgeFlags);
John Stilescbe4e282020-06-01 10:38:31 -0400590
Michael Ludwig6bee7762018-10-19 09:50:36 -0400591 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
592 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500593 if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
594 netAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400595 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400596
597 // Calculate metadata for the entry
Brian Salomon2432d062020-04-16 20:48:09 -0400598 const SkRect* subsetForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400599 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
John Stilescbe4e282020-06-01 10:38:31 -0400600 // Check (briefly) if the subset rect is actually needed for this set entry.
601 SkRect* subsetRect = &set[q].fSrcRect;
602 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
Michael Ludwig2ed4a362021-08-17 21:32:12 -0400603 if (!safe_to_ignore_subset_rect(aaForQuad, filterForQuad, quad, *subsetRect)) {
John Stilescbe4e282020-06-01 10:38:31 -0400604 netSubset = Subset::kYes;
605 subsetForQuad = subsetRect;
606 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400607 }
608 }
John Stilescbe4e282020-06-01 10:38:31 -0400609
610 // Normalize the src quads and apply origin
611 NormalizationParams proxyParams = proxy_normalization_params(
612 curProxy, set[q].fProxyView.origin());
613 normalize_src_quad(proxyParams, &quad.fLocal);
614
Brian Salomon2432d062020-04-16 20:48:09 -0400615 // This subset may represent a no-op, otherwise it will have the origin and dimensions
Michael Ludwig2ed4a362021-08-17 21:32:12 -0400616 // of the texture applied to it.
Brian Salomon75cebbe2020-05-18 14:08:14 -0400617 SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500618
Michael Ludwig949ceb22020-02-07 10:14:45 -0500619 // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
620 // ViewCountPair (this frequently happens when Chrome draws 9-patches).
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400621 fViewCountPairs[p].fQuadCnt += this->appendQuad(&quad, set[q].fColor, subset);
Brian Salomond7065e72018-10-12 11:42:02 -0400622 }
Michael Ludwig406172a2019-12-06 14:05:19 -0500623 // The # of proxy switches should match what was provided (+1 because we incremented p
Michael Ludwig379e4962019-12-06 13:21:26 -0500624 // when a new proxy was encountered).
Michael Ludwig406172a2019-12-06 14:05:19 -0500625 SkASSERT((p + 1) == fMetadata.fProxyCount);
Michael Ludwig379e4962019-12-06 13:21:26 -0500626 SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500627
Michael Ludwig4384f042019-12-05 10:30:35 -0500628 fMetadata.fAAType = static_cast<uint16_t>(netAAType);
629 fMetadata.fFilter = static_cast<uint16_t>(netFilter);
Brian Salomon2432d062020-04-16 20:48:09 -0400630 fMetadata.fSubset = static_cast<uint16_t>(netSubset);
Brian Salomon34169692017-08-28 15:32:01 -0400631
Michael Ludwig575c9212021-07-13 11:09:52 -0400632 this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage),
633 hasSubpixel ? IsHairline::kYes : IsHairline::kNo);
Brian Salomon17031a72018-05-22 14:14:07 -0400634 }
635
Brian Salomon2432d062020-04-16 20:48:09 -0400636 int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& subset) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500637 DrawQuad extra;
Michael Ludwig65299902021-05-13 12:02:23 -0400638 // Always clip to W0 to stay consistent with GrQuad::bounds
639 int quadCount = GrQuadUtils::ClipToW0(quad, &extra);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500640 if (quadCount == 0) {
641 // We can't discard the op at this point, but disable AA flags so it won't go through
642 // inset/outset processing
643 quad->fEdgeFlags = GrQuadAAFlags::kNone;
644 quadCount = 1;
645 }
Brian Salomon2432d062020-04-16 20:48:09 -0400646 fQuads.append(quad->fDevice, {color, subset, quad->fEdgeFlags}, &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500647 if (quadCount > 1) {
Brian Salomon2432d062020-04-16 20:48:09 -0400648 fQuads.append(extra.fDevice, {color, subset, extra.fEdgeFlags}, &extra.fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500649 fMetadata.fTotalQuadCount++;
650 }
651 return quadCount;
652 }
653
Robert Phillips2669a7b2020-03-12 12:07:19 -0400654 GrProgramInfo* programInfo() override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600655 // Although this Op implements its own onPrePrepareDraws it calls GrMeshDrawOps' version so
656 // this entry point will be called.
657 return (fDesc) ? fDesc->fProgramInfo : nullptr;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400658 }
659
Chris Daltondbb833b2020-03-17 12:15:46 -0600660 void onCreateProgramInfo(const GrCaps* caps,
661 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500662 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600663 bool usesMSAASurface,
Chris Daltondbb833b2020-03-17 12:15:46 -0600664 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400665 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500666 GrXferBarrierFlags renderPassXferBarriers,
667 GrLoadOp colorLoadOp) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600668 SkASSERT(fDesc);
669
670 GrGeometryProcessor* gp;
671
672 {
673 const GrBackendFormat& backendFormat =
674 fViewCountPairs[0].fProxy->backendFormat();
675
676 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
677 fMetadata.filter());
678
679 gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
680 arena, fDesc->fVertexSpec, *caps->shaderCaps(), backendFormat, samplerState,
681 fMetadata.fSwizzle, std::move(fTextureColorSpaceXform), fMetadata.saturate());
682
683 SkASSERT(fDesc->fVertexSpec.vertexSize() == gp->vertexStride());
684 }
685
Chris Daltondbb833b2020-03-17 12:15:46 -0600686 fDesc->fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Chris Dalton2a26c502021-08-26 10:05:11 -0600687 caps, arena, writeView, usesMSAASurface, std::move(appliedClip), dstProxyView, gp,
Chris Daltondbb833b2020-03-17 12:15:46 -0600688 GrProcessorSet::MakeEmptySet(), fDesc->fVertexSpec.primitiveType(),
Chris Daltoneb0195e2021-08-18 21:39:02 -0600689 renderPassXferBarriers, colorLoadOp, GrPipeline::InputFlags::kNone);
Robert Phillips4133dc42020-03-11 15:55:55 -0400690 }
691
Robert Phillipsdf70f152019-11-15 14:57:05 -0500692 void onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -0500693 const GrSurfaceProxyView& writeView,
Robert Phillips8053c972019-11-21 10:44:53 -0500694 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -0400695 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500696 GrXferBarrierFlags renderPassXferBarriers,
697 GrLoadOp colorLoadOp) override {
Robert Phillips61fc7992019-10-22 11:58:17 -0400698 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips29f38542019-10-16 09:20:25 -0400699
Robert Phillips61fc7992019-10-22 11:58:17 -0400700 SkDEBUGCODE(this->validate();)
Chris Daltondbb833b2020-03-17 12:15:46 -0600701 SkASSERT(!fDesc);
Robert Phillips61fc7992019-10-22 11:58:17 -0400702
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500703 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillips61fc7992019-10-22 11:58:17 -0400704
Chris Daltondbb833b2020-03-17 12:15:46 -0600705 fDesc = arena->make<Desc>();
Brian Salomonb8c4add2021-06-28 09:20:44 -0400706 this->characterize(fDesc);
Chris Daltondbb833b2020-03-17 12:15:46 -0600707 fDesc->allocatePrePreparedVertices(arena);
708 FillInVertices(*context->priv().caps(), this, fDesc, fDesc->fPrePreparedVertices);
Robert Phillips61fc7992019-10-22 11:58:17 -0400709
Chris Daltondbb833b2020-03-17 12:15:46 -0600710 // This will call onCreateProgramInfo and register the created program with the DDL.
Greg Danield358cbe2020-09-11 09:33:54 -0400711 this->INHERITED::onPrePrepareDraws(context, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500712 renderPassXferBarriers, colorLoadOp);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400713 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400714
Robert Phillips21453902021-08-27 16:05:04 -0400715 static void FillInVertices(const GrCaps& caps,
716 TextureOpImpl* texOp,
717 Desc* desc,
718 char* vertexData) {
Chris Daltondbb833b2020-03-17 12:15:46 -0600719 SkASSERT(vertexData);
720
Ben Wagner097a9a42021-07-27 12:20:03 -0400721 SkDEBUGCODE(int totQuadsSeen = 0;)
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400722 SkDEBUGCODE(int totVerticesSeen = 0;)
Michael Ludwig189c9802019-11-21 11:21:12 -0500723 SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400724
Chris Daltondbb833b2020-03-17 12:15:46 -0600725 GrQuadPerEdgeAA::Tessellator tessellator(desc->fVertexSpec, vertexData);
Robert Phillips21453902021-08-27 16:05:04 -0400726 for (const auto& op : ChainRange<TextureOpImpl>(texOp)) {
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400727 auto iter = op.fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500728 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500729 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
730 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400731
Chris Daltondbb833b2020-03-17 12:15:46 -0600732 for (int i = 0; i < quadCnt && iter.next(); ++i) {
733 SkASSERT(iter.isLocalValid());
Brian Salomon2432d062020-04-16 20:48:09 -0400734 const ColorSubsetAndAA& info = iter.metadata();
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500735
Chris Daltondbb833b2020-03-17 12:15:46 -0600736 tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
Brian Salomon75cebbe2020-05-18 14:08:14 -0400737 info.fSubsetRect, info.aaFlags());
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((totVerticesSeen + meshVertexCnt) * vertexSize
741 == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400742
Ben Wagner097a9a42021-07-27 12:20:03 -0400743 SkDEBUGCODE(totQuadsSeen += quadCnt;)
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400744 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
745 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400746 }
747
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400748 // If quad counts per proxy were calculated correctly, the entire iterator
749 // should have been consumed.
Chris Daltondbb833b2020-03-17 12:15:46 -0600750 SkASSERT(!iter.next());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400751 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400752
Chris Daltondbb833b2020-03-17 12:15:46 -0600753 SkASSERT(desc->totalSizeInBytes() == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400754 SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
755 SkASSERT(totVerticesSeen == desc->totalNumVertices());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400756 }
757
Robert Phillips29f38542019-10-16 09:20:25 -0400758#ifdef SK_DEBUG
Robert Phillips6bf11252020-07-31 12:15:00 -0400759 static int validate_op(GrTextureType textureType,
760 GrAAType aaType,
761 GrSwizzle swizzle,
Robert Phillips21453902021-08-27 16:05:04 -0400762 const TextureOpImpl* op) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400763 SkASSERT(op->fMetadata.fSwizzle == swizzle);
764
765 int quadCount = 0;
766 for (unsigned p = 0; p < op->fMetadata.fProxyCount; ++p) {
767 auto* proxy = op->fViewCountPairs[p].fProxy->asTextureProxy();
768 quadCount += op->fViewCountPairs[p].fQuadCnt;
769 SkASSERT(proxy);
770 SkASSERT(proxy->textureType() == textureType);
771 }
772
773 SkASSERT(aaType == op->fMetadata.aaType());
774 return quadCount;
775 }
776
Robert Phillips29f38542019-10-16 09:20:25 -0400777 void validate() const override {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500778 // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
Michael Ludwigadb12e72019-12-04 16:19:18 -0500779 auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
780 GrAAType aaType = fMetadata.aaType();
Robert Phillips6bf11252020-07-31 12:15:00 -0400781 GrSwizzle swizzle = fMetadata.fSwizzle;
Robert Phillips29f38542019-10-16 09:20:25 -0400782
Robert Phillips6bf11252020-07-31 12:15:00 -0400783 int quadCount = validate_op(textureType, aaType, swizzle, this);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500784
Robert Phillips6bf11252020-07-31 12:15:00 -0400785 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
786 quadCount += validate_op(textureType, aaType, swizzle,
Robert Phillips21453902021-08-27 16:05:04 -0400787 static_cast<const TextureOpImpl*>(tmp));
Robert Phillips6bf11252020-07-31 12:15:00 -0400788 }
Robert Phillips29f38542019-10-16 09:20:25 -0400789
Robert Phillips6bf11252020-07-31 12:15:00 -0400790 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
791 quadCount += validate_op(textureType, aaType, swizzle,
Robert Phillips21453902021-08-27 16:05:04 -0400792 static_cast<const TextureOpImpl*>(tmp));
Robert Phillips29f38542019-10-16 09:20:25 -0400793 }
Robert Phillipse837e612019-11-15 11:02:50 -0500794
795 SkASSERT(quadCount == this->numChainedQuads());
Robert Phillips29f38542019-10-16 09:20:25 -0400796 }
Robert Phillips6bf11252020-07-31 12:15:00 -0400797
Robert Phillips29f38542019-10-16 09:20:25 -0400798#endif
799
Robert Phillipse837e612019-11-15 11:02:50 -0500800#if GR_TEST_UTILS
801 int numQuads() const final { return this->totNumQuads(); }
802#endif
803
Brian Salomonb8c4add2021-06-28 09:20:44 -0400804 void characterize(Desc* desc) const {
Robert Phillips6bf11252020-07-31 12:15:00 -0400805 SkDEBUGCODE(this->validate();)
806
Robert Phillips29f38542019-10-16 09:20:25 -0400807 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
808 ColorType colorType = ColorType::kNone;
809 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomon2432d062020-04-16 20:48:09 -0400810 Subset subset = Subset::kNo;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500811 GrAAType overallAAType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400812
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400813 desc->fNumProxies = 0;
814 desc->fNumTotalQuads = 0;
815 int maxQuadsPerMesh = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400816
Robert Phillips21453902021-08-27 16:05:04 -0400817 for (const auto& op : ChainRange<TextureOpImpl>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400818 if (op.fQuads.deviceQuadType() > quadType) {
819 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500820 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400821 if (op.fQuads.localQuadType() > srcQuadType) {
822 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500823 }
Brian Salomon2432d062020-04-16 20:48:09 -0400824 if (op.fMetadata.subset() == Subset::kYes) {
825 subset = Subset::kYes;
Brian Salomonf7232642018-09-19 08:58:08 -0400826 }
Brian Osman788b9162020-02-07 10:36:46 -0500827 colorType = std::max(colorType, op.fMetadata.colorType());
Michael Ludwigadb12e72019-12-04 16:19:18 -0500828 desc->fNumProxies += op.fMetadata.fProxyCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400829
Michael Ludwigadb12e72019-12-04 16:19:18 -0500830 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Brian Osman788b9162020-02-07 10:36:46 -0500831 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
Brian Salomonf7232642018-09-19 08:58:08 -0400832 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400833 desc->fNumTotalQuads += op.totNumQuads();
834
Michael Ludwigadb12e72019-12-04 16:19:18 -0500835 if (op.fMetadata.aaType() == GrAAType::kCoverage) {
Robert Phillips29f38542019-10-16 09:20:25 -0400836 overallAAType = GrAAType::kCoverage;
Brian Salomonae7d7702018-10-14 15:05:45 -0400837 }
Brian Salomon34169692017-08-28 15:32:01 -0400838 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400839
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400840 SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
841
842 SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
843
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400844 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(overallAAType,
Brian Salomonb8c4add2021-06-28 09:20:44 -0400845 maxQuadsPerMesh);
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400846
847 desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
Brian Salomon2432d062020-04-16 20:48:09 -0400848 subset, overallAAType, /* alpha as coverage */ true,
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400849 indexBufferOption);
Robert Phillipse837e612019-11-15 11:02:50 -0500850
851 SkASSERT(desc->fNumTotalQuads <= GrQuadPerEdgeAA::QuadLimit(indexBufferOption));
Robert Phillips29f38542019-10-16 09:20:25 -0400852 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500853
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400854 int totNumQuads() const {
855#ifdef SK_DEBUG
856 int tmp = 0;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500857 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400858 tmp += fViewCountPairs[p].fQuadCnt;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400859 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500860 SkASSERT(tmp == fMetadata.fTotalQuadCount);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400861#endif
862
Michael Ludwigadb12e72019-12-04 16:19:18 -0500863 return fMetadata.fTotalQuadCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400864 }
865
866 int numChainedQuads() const {
867 int numChainedQuads = this->totNumQuads();
868
869 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
Robert Phillips21453902021-08-27 16:05:04 -0400870 numChainedQuads += ((const TextureOpImpl*)tmp)->totNumQuads();
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400871 }
872
873 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
Robert Phillips21453902021-08-27 16:05:04 -0400874 numChainedQuads += ((const TextureOpImpl*)tmp)->totNumQuads();
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400875 }
876
877 return numChainedQuads;
878 }
879
Robert Phillips29f38542019-10-16 09:20:25 -0400880 // onPrePrepareDraws may or may not have been called at this point
Robert Phillips71143952021-06-17 14:55:07 -0400881 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips29f38542019-10-16 09:20:25 -0400882 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500883
Robert Phillips29f38542019-10-16 09:20:25 -0400884 SkDEBUGCODE(this->validate();)
885
Chris Daltondbb833b2020-03-17 12:15:46 -0600886 SkASSERT(!fDesc || fDesc->fPrePreparedVertices);
Robert Phillips29f38542019-10-16 09:20:25 -0400887
Chris Daltondbb833b2020-03-17 12:15:46 -0600888 if (!fDesc) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400889 SkArenaAlloc* arena = target->allocator();
Chris Daltondbb833b2020-03-17 12:15:46 -0600890 fDesc = arena->make<Desc>();
Brian Salomonb8c4add2021-06-28 09:20:44 -0400891 this->characterize(fDesc);
Chris Daltondbb833b2020-03-17 12:15:46 -0600892 SkASSERT(!fDesc->fPrePreparedVertices);
Brian Salomonf7232642018-09-19 08:58:08 -0400893 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400894
Chris Daltondbb833b2020-03-17 12:15:46 -0600895 size_t vertexSize = fDesc->fVertexSpec.vertexSize();
Brian Salomon92be2f72018-06-19 14:33:47 -0400896
Chris Daltondbb833b2020-03-17 12:15:46 -0600897 void* vdata = target->makeVertexSpace(vertexSize, fDesc->totalNumVertices(),
898 &fDesc->fVertexBuffer, &fDesc->fBaseVertex);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400899 if (!vdata) {
900 SkDebugf("Could not allocate vertices\n");
901 return;
Brian Salomon34169692017-08-28 15:32:01 -0400902 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400903
Chris Daltondbb833b2020-03-17 12:15:46 -0600904 if (fDesc->fVertexSpec.needsIndexBuffer()) {
905 fDesc->fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(
906 target, fDesc->fVertexSpec.indexBufferOption());
907 if (!fDesc->fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400908 SkDebugf("Could not allocate indices\n");
909 return;
910 }
911 }
912
Chris Daltondbb833b2020-03-17 12:15:46 -0600913 if (fDesc->fPrePreparedVertices) {
914 memcpy(vdata, fDesc->fPrePreparedVertices, fDesc->totalSizeInBytes());
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400915 } else {
Chris Daltondbb833b2020-03-17 12:15:46 -0600916 FillInVertices(target->caps(), this, fDesc, (char*) vdata);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400917 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700918 }
919
920 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600921 if (!fDesc->fVertexBuffer) {
922 return;
923 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500924
Chris Daltondbb833b2020-03-17 12:15:46 -0600925 if (fDesc->fVertexSpec.needsIndexBuffer() && !fDesc->fIndexBuffer) {
926 return;
927 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500928
Chris Daltondbb833b2020-03-17 12:15:46 -0600929 if (!fDesc->fProgramInfo) {
930 this->createProgramInfo(flushState);
931 SkASSERT(fDesc->fProgramInfo);
932 }
933
934 flushState->bindPipelineAndScissorClip(*fDesc->fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400935 flushState->bindBuffers(std::move(fDesc->fIndexBuffer), nullptr,
936 std::move(fDesc->fVertexBuffer));
Chris Daltondbb833b2020-03-17 12:15:46 -0600937
938 int totQuadsSeen = 0;
939 SkDEBUGCODE(int numDraws = 0;)
Robert Phillips21453902021-08-27 16:05:04 -0400940 for (const auto& op : ChainRange<TextureOpImpl>(this)) {
Chris Daltondbb833b2020-03-17 12:15:46 -0600941 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
942 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
943 SkASSERT(numDraws < fDesc->fNumProxies);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400944 flushState->bindTextures(fDesc->fProgramInfo->geomProc(),
Chris Daltondbb833b2020-03-17 12:15:46 -0600945 *op.fViewCountPairs[p].fProxy,
946 fDesc->fProgramInfo->pipeline());
947 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(),
948 fDesc->fVertexSpec, totQuadsSeen, quadCnt,
949 fDesc->totalNumVertices(), fDesc->fBaseVertex);
950 totQuadsSeen += quadCnt;
951 SkDEBUGCODE(++numDraws;)
952 }
953 }
954
955 SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
956 SkASSERT(numDraws == fDesc->fNumProxies);
Brian Salomon34169692017-08-28 15:32:01 -0400957 }
958
Robert Phillips6bf11252020-07-31 12:15:00 -0400959 void propagateCoverageAAThroughoutChain() {
960 fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
961
962 for (GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
Robert Phillips21453902021-08-27 16:05:04 -0400963 auto tex = static_cast<TextureOpImpl*>(tmp);
Robert Phillips6bf11252020-07-31 12:15:00 -0400964 SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
965 tex->fMetadata.aaType() == GrAAType::kNone);
966 tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
967 }
968
969 for (GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
Robert Phillips21453902021-08-27 16:05:04 -0400970 auto tex = static_cast<TextureOpImpl*>(tmp);
Robert Phillips6bf11252020-07-31 12:15:00 -0400971 SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
972 tex->fMetadata.aaType() == GrAAType::kNone);
973 tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
974 }
975 }
976
Herb Derbye25c3002020-10-27 15:57:27 -0400977 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400978 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips21453902021-08-27 16:05:04 -0400979 auto that = t->cast<TextureOpImpl>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400980
Robert Phillips6bf11252020-07-31 12:15:00 -0400981 SkDEBUGCODE(this->validate();)
982 SkDEBUGCODE(that->validate();)
983
Chris Daltondbb833b2020-03-17 12:15:46 -0600984 if (fDesc || that->fDesc) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400985 // This should never happen (since only DDL recorded ops should be prePrepared)
986 // but, in any case, we should never combine ops that that been prePrepared
987 return CombineResult::kCannotCombine;
988 }
989
Brian Salomon2432d062020-04-16 20:48:09 -0400990 if (fMetadata.subset() != that->fMetadata.subset()) {
991 // It is technically possible to combine operations across subset modes, but performance
Michael Ludwig2929f512019-04-19 13:05:56 -0400992 // testing suggests it's better to make more draw calls where some take advantage of
993 // the more optimal shader path without coordinate clamping.
994 return CombineResult::kCannotCombine;
995 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400996 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
997 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000998 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400999 }
Robert Phillipsb69001f2019-10-29 12:16:35 -04001000
Brian Salomonae7d7702018-10-14 15:05:45 -04001001 bool upgradeToCoverageAAOnMerge = false;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001002 if (fMetadata.aaType() != that->fMetadata.aaType()) {
1003 if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
Brian Salomonae7d7702018-10-14 15:05:45 -04001004 return CombineResult::kCannotCombine;
1005 }
1006 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -05001007 }
Robert Phillipsb69001f2019-10-29 12:16:35 -04001008
Michael Ludwigadb12e72019-12-04 16:19:18 -05001009 if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
Robert Phillipsbbd459d2019-10-29 14:40:03 -04001010 this->numChainedQuads() + that->numChainedQuads())) {
1011 return CombineResult::kCannotCombine;
Robert Phillipsb69001f2019-10-29 12:16:35 -04001012 }
1013
Michael Ludwigadb12e72019-12-04 16:19:18 -05001014 if (fMetadata.saturate() != that->fMetadata.saturate()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001015 return CombineResult::kCannotCombine;
1016 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001017 if (fMetadata.filter() != that->fMetadata.filter()) {
Brian Salomonf7232642018-09-19 08:58:08 -04001018 return CombineResult::kCannotCombine;
1019 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001020 if (fMetadata.mipmapMode() != that->fMetadata.mipmapMode()) {
1021 return CombineResult::kCannotCombine;
1022 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001023 if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
1024 return CombineResult::kCannotCombine;
1025 }
1026 const auto* thisProxy = fViewCountPairs[0].fProxy.get();
1027 const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
1028 if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
1029 thisProxy != thatProxy) {
Brian Salomon588cec72018-11-14 13:56:37 -05001030 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -05001031 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Robert Phillips6bf11252020-07-31 12:15:00 -04001032 caps.dynamicStateArrayGeometryProcessorTextureSupport() &&
1033 fMetadata.aaType() == that->fMetadata.aaType()) {
1034 // We only allow chaining when the aaTypes match bc otherwise the AA type
1035 // reported by the chain can be inconsistent. That is, since chaining doesn't
1036 // propagate revised AA information throughout the chain, the head of the chain
1037 // could have an AA setting of kNone while the chain as a whole could have a
1038 // setting of kCoverage. This inconsistency would then interfere with the validity
1039 // of the CombinedQuadCountWillOverflow calls.
1040 // This problem doesn't occur w/ merging bc we do propagate the AA information
1041 // (in propagateCoverageAAThroughoutChain) below.
Brian Salomonf7232642018-09-19 08:58:08 -04001042 return CombineResult::kMayChain;
1043 }
Brian Salomon7eae3e02018-08-07 14:02:38 +00001044 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001045 }
Michael Ludwig009b92e2019-02-15 16:03:53 -05001046
Brian Salomon2432d062020-04-16 20:48:09 -04001047 fMetadata.fSubset |= that->fMetadata.fSubset;
Brian Osman788b9162020-02-07 10:36:46 -05001048 fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
Michael Ludwig009b92e2019-02-15 16:03:53 -05001049
Michael Ludwig425eb452019-06-27 10:13:27 -04001050 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -05001051 fQuads.concat(that->fQuads);
Greg Daniel549325c2019-10-30 16:19:20 -04001052 fViewCountPairs[0].fQuadCnt += that->fQuads.count();
Michael Ludwigadb12e72019-12-04 16:19:18 -05001053 fMetadata.fTotalQuadCount += that->fQuads.count();
Michael Ludwig009b92e2019-02-15 16:03:53 -05001054
Robert Phillips6bf11252020-07-31 12:15:00 -04001055 if (upgradeToCoverageAAOnMerge) {
Robert Phillips089b7c92020-08-12 11:57:07 -04001056 // This merger may be the start of a concatenation of two chains. When one
1057 // of the chains mutates its AA the other must follow suit or else the above AA
1058 // check may prevent later ops from chaining together. A specific example of this is
1059 // when chain2 is prepended onto chain1:
1060 // chain1 (that): opA (non-AA/mergeable) opB (non-AA/non-mergeable)
1061 // chain2 (this): opC (cov-AA/non-mergeable) opD (cov-AA/mergeable)
1062 // W/o this propagation, after opD & opA merge, opB and opC would say they couldn't
1063 // chain - which would stop the concatenation process.
Robert Phillips6bf11252020-07-31 12:15:00 -04001064 this->propagateCoverageAAThroughoutChain();
Robert Phillips089b7c92020-08-12 11:57:07 -04001065 that->propagateCoverageAAThroughoutChain();
Robert Phillips6bf11252020-07-31 12:15:00 -04001066 }
1067
1068 SkDEBUGCODE(this->validate();)
1069
Brian Salomon7eae3e02018-08-07 14:02:38 +00001070 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -04001071 }
John Stilesaf366522020-08-13 09:57:34 -04001072
1073#if GR_TEST_UTILS
1074 SkString onDumpInfo() const override {
1075 SkString str = SkStringPrintf("# draws: %d\n", fQuads.count());
1076 auto iter = fQuads.iterator();
1077 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Robert Phillips047d5bb2021-01-08 13:39:19 -05001078 SkString proxyStr = fViewCountPairs[p].fProxy->dump();
1079 str.append(proxyStr);
1080 str.appendf(", Filter: %d, MM: %d\n",
John Stilesaf366522020-08-13 09:57:34 -04001081 static_cast<int>(fMetadata.fFilter),
1082 static_cast<int>(fMetadata.fMipmapMode));
Robert Phillips047d5bb2021-01-08 13:39:19 -05001083 for (int i = 0; i < fViewCountPairs[p].fQuadCnt && iter.next(); ++i) {
John Stilesaf366522020-08-13 09:57:34 -04001084 const GrQuad* quad = iter.deviceQuad();
1085 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
1086 const ColorSubsetAndAA& info = iter.metadata();
1087 str.appendf(
1088 "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
1089 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
1090 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
1091 i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
1092 info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
1093 quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
1094 quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
1095 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
1096 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
John Stilesaf366522020-08-13 09:57:34 -04001097 }
1098 }
1099 return str;
1100 }
1101#endif
1102
Brian Salomon2432d062020-04-16 20:48:09 -04001103 GrQuadBuffer<ColorSubsetAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -04001104 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Chris Daltondbb833b2020-03-17 12:15:46 -06001105 // Most state of TextureOp is packed into these two field to minimize the op's size.
Michael Ludwigadb12e72019-12-04 16:19:18 -05001106 // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1107 // consider/measure changes with care.
Chris Daltondbb833b2020-03-17 12:15:46 -06001108 Desc* fDesc;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001109 Metadata fMetadata;
Robert Phillips32803ff2019-10-23 08:26:08 -04001110
1111 // This field must go last. When allocating this op, we will allocate extra space to hold
Greg Daniel549325c2019-10-30 16:19:20 -04001112 // additional ViewCountPairs immediately after the op's allocation so we can treat this
Robert Phillips32803ff2019-10-23 08:26:08 -04001113 // as an fProxyCnt-length array.
Greg Daniel549325c2019-10-30 16:19:20 -04001114 ViewCountPair fViewCountPairs[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -04001115
John Stiles7571f9e2020-09-02 22:42:33 -04001116 using INHERITED = GrMeshDrawOp;
Brian Salomon34169692017-08-28 15:32:01 -04001117};
1118
1119} // anonymous namespace
1120
Robert Phillips21453902021-08-27 16:05:04 -04001121namespace skgpu::v1 {
1122
Robert Phillipse837e612019-11-15 11:02:50 -05001123#if GR_TEST_UTILS
Robert Phillips21453902021-08-27 16:05:04 -04001124uint32_t TextureOp::ClassID() {
1125 return TextureOpImpl::ClassID();
Robert Phillipse837e612019-11-15 11:02:50 -05001126}
1127#endif
Brian Salomon34169692017-08-28 15:32:01 -04001128
Robert Phillips21453902021-08-27 16:05:04 -04001129GrOp::Owner TextureOp::Make(GrRecordingContext* context,
1130 GrSurfaceProxyView proxyView,
1131 SkAlphaType alphaType,
1132 sk_sp<GrColorSpaceXform> textureXform,
1133 GrSamplerState::Filter filter,
1134 GrSamplerState::MipmapMode mm,
1135 const SkPMColor4f& color,
1136 Saturate saturate,
1137 SkBlendMode blendMode,
1138 GrAAType aaType,
1139 DrawQuad* quad,
1140 const SkRect* subset) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001141 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
Brian Salomon2432d062020-04-16 20:48:09 -04001142 if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1143 // No need for a shader-based subset if hardware clamping achieves the same effect
1144 subset = nullptr;
Michael Ludwig22429f92019-06-27 10:44:48 -04001145 }
1146
Brian Salomone69b9ef2020-07-22 11:18:06 -04001147 if (filter != GrSamplerState::Filter::kNearest || mm != GrSamplerState::MipmapMode::kNone) {
1148 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad->fLocal, quad->fDevice);
1149 if (!mustFilter) {
1150 filter = GrSamplerState::Filter::kNearest;
1151 }
1152 if (!mustMM) {
1153 mm = GrSamplerState::MipmapMode::kNone;
1154 }
Michael Ludwig22429f92019-06-27 10:44:48 -04001155 }
1156
1157 if (blendMode == SkBlendMode::kSrcOver) {
Robert Phillips21453902021-08-27 16:05:04 -04001158 return TextureOpImpl::Make(context, std::move(proxyView), std::move(textureXform), filter,
1159 mm, color, saturate, aaType, std::move(quad), subset);
Michael Ludwig22429f92019-06-27 10:44:48 -04001160 } else {
1161 // Emulate complex blending using GrFillRectOp
Brian Salomonb030b1f2020-11-23 15:40:55 -05001162 GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, filter, mm);
Michael Ludwig22429f92019-06-27 10:44:48 -04001163 GrPaint paint;
1164 paint.setColor4f(color);
1165 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1166
1167 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomonb030b1f2020-11-23 15:40:55 -05001168 const auto& caps = *context->priv().caps();
Brian Salomon2432d062020-04-16 20:48:09 -04001169 if (subset) {
Brian Salomonca6b2f42020-01-24 11:31:21 -05001170 SkRect localRect;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001171 if (quad->fLocal.asRect(&localRect)) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001172 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
Brian Salomonb030b1f2020-11-23 15:40:55 -05001173 samplerState, *subset, localRect, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001174 } else {
John Stiles5a2a7b32020-06-04 10:57:21 -04001175 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
Brian Salomonb030b1f2020-11-23 15:40:55 -05001176 samplerState, *subset, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001177 }
1178 } else {
Brian Salomonb030b1f2020-11-23 15:40:55 -05001179 fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), samplerState,
1180 caps);
Michael Ludwig22429f92019-06-27 10:44:48 -04001181 }
1182 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
Brian Salomon4b6e2f02021-07-08 14:04:21 -04001183 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Robert Phillips21453902021-08-27 16:05:04 -04001184 if (saturate == Saturate::kYes) {
Brian Osman8e814b32021-06-17 14:14:26 -04001185 fp = GrFragmentProcessor::ClampOutput(std::move(fp));
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001186 }
John Stiles5933d7d2020-07-21 12:28:35 -04001187 paint.setColorFragmentProcessor(std::move(fp));
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001188 return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
Michael Ludwig22429f92019-06-27 10:44:48 -04001189 }
1190}
1191
Robert Phillipse837e612019-11-15 11:02:50 -05001192// A helper class that assists in breaking up bulk API quad draws into manageable chunks.
Robert Phillips21453902021-08-27 16:05:04 -04001193class TextureOp::BatchSizeLimiter {
Robert Phillipse837e612019-11-15 11:02:50 -05001194public:
Robert Phillips21453902021-08-27 16:05:04 -04001195 BatchSizeLimiter(SurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001196 const GrClip* clip,
Robert Phillips4dca8312021-07-28 15:13:20 -04001197 GrRecordingContext* rContext,
Robert Phillipse837e612019-11-15 11:02:50 -05001198 int numEntries,
1199 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001200 GrSamplerState::MipmapMode mm,
Robert Phillips21453902021-08-27 16:05:04 -04001201 Saturate saturate,
Robert Phillipse837e612019-11-15 11:02:50 -05001202 SkCanvas::SrcRectConstraint constraint,
1203 const SkMatrix& viewMatrix,
1204 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
John Stiles0fbc6a32021-06-04 14:40:57 -04001205 : fSDC(sdc)
Robert Phillipse837e612019-11-15 11:02:50 -05001206 , fClip(clip)
Robert Phillips4dca8312021-07-28 15:13:20 -04001207 , fContext(rContext)
Robert Phillipse837e612019-11-15 11:02:50 -05001208 , fFilter(filter)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001209 , fMipmapMode(mm)
Robert Phillipse837e612019-11-15 11:02:50 -05001210 , fSaturate(saturate)
1211 , fConstraint(constraint)
1212 , fViewMatrix(viewMatrix)
1213 , fTextureColorSpaceXform(textureColorSpaceXform)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001214 , fNumLeft(numEntries) {}
Brian Salomon34169692017-08-28 15:32:01 -04001215
Robert Phillipse40495d2021-07-20 09:40:13 -04001216 void createOp(GrTextureSetEntry set[], int clumpSize, GrAAType aaType) {
1217
Michael Ludwig379e4962019-12-06 13:21:26 -05001218 int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
Robert Phillips21453902021-08-27 16:05:04 -04001219 GrOp::Owner op = TextureOpImpl::Make(fContext,
1220 &set[fNumClumped],
1221 clumpSize,
1222 clumpProxyCount,
1223 fFilter,
1224 fMipmapMode,
1225 fSaturate,
1226 aaType,
1227 fConstraint,
1228 fViewMatrix,
1229 fTextureColorSpaceXform);
John Stiles0fbc6a32021-06-04 14:40:57 -04001230 fSDC->addDrawOp(fClip, std::move(op));
Robert Phillipse837e612019-11-15 11:02:50 -05001231
1232 fNumLeft -= clumpSize;
1233 fNumClumped += clumpSize;
1234 }
1235
1236 int numLeft() const { return fNumLeft; }
1237 int baseIndex() const { return fNumClumped; }
1238
1239private:
Robert Phillips21453902021-08-27 16:05:04 -04001240 SurfaceDrawContext* fSDC;
1241 const GrClip* fClip;
1242 GrRecordingContext* fContext;
1243 GrSamplerState::Filter fFilter;
1244 GrSamplerState::MipmapMode fMipmapMode;
1245 Saturate fSaturate;
1246 SkCanvas::SrcRectConstraint fConstraint;
1247 const SkMatrix& fViewMatrix;
1248 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Robert Phillipse837e612019-11-15 11:02:50 -05001249
Robert Phillips21453902021-08-27 16:05:04 -04001250 int fNumLeft;
1251 int fNumClumped = 0; // also the offset for the start of the next clump
Robert Phillipse837e612019-11-15 11:02:50 -05001252};
1253
1254// Greedily clump quad draws together until the index buffer limit is exceeded.
Robert Phillips21453902021-08-27 16:05:04 -04001255void TextureOp::AddTextureSetOps(SurfaceDrawContext* sdc,
1256 const GrClip* clip,
1257 GrRecordingContext* context,
1258 GrTextureSetEntry set[],
1259 int cnt,
1260 int proxyRunCnt,
1261 GrSamplerState::Filter filter,
1262 GrSamplerState::MipmapMode mm,
1263 Saturate saturate,
1264 SkBlendMode blendMode,
1265 GrAAType aaType,
1266 SkCanvas::SrcRectConstraint constraint,
1267 const SkMatrix& viewMatrix,
1268 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001269 // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1270 // the op's metadata so we don't need to worry about overflow.
Robert Phillips21453902021-08-27 16:05:04 -04001271 SkDEBUGCODE(TextureOpImpl::ValidateResourceLimits();)
Michael Ludwig379e4962019-12-06 13:21:26 -05001272 SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1273
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001274 // First check if we can support batches as a single op
1275 if (blendMode != SkBlendMode::kSrcOver ||
1276 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1277 // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1278 // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1279 // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1280 SkMatrix ctm;
1281 for (int i = 0; i < cnt; ++i) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001282 ctm = viewMatrix;
1283 if (set[i].fPreViewMatrix) {
1284 ctm.preConcat(*set[i].fPreViewMatrix);
1285 }
Robert Phillipse837e612019-11-15 11:02:50 -05001286
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001287 DrawQuad quad;
1288 quad.fEdgeFlags = set[i].fAAFlags;
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001289 if (set[i].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001290 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001291
1292 SkPoint srcPts[4];
1293 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001294 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001295 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001296 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1297 quad.fLocal = GrQuad(set[i].fSrcRect);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001298 }
1299
Brian Salomon2432d062020-04-16 20:48:09 -04001300 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001301 ? &set[i].fSrcRect : nullptr;
1302
Brian Salomonfc118442019-11-22 19:09:27 -05001303 auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001304 filter, mm, set[i].fColor, saturate, blendMode, aaType, &quad, subset);
Robert Phillips4dca8312021-07-28 15:13:20 -04001305 sdc->addDrawOp(clip, std::move(op));
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001306 }
1307 return;
1308 }
1309
1310 // Second check if we can always just make a single op and avoid the extra iteration
Robert Phillipse837e612019-11-15 11:02:50 -05001311 // needed to clump things together.
Brian Osman788b9162020-02-07 10:36:46 -05001312 if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
Robert Phillipse837e612019-11-15 11:02:50 -05001313 GrResourceProvider::MaxNumAAQuads())) {
Robert Phillips21453902021-08-27 16:05:04 -04001314 auto op = TextureOpImpl::Make(context, set, cnt, proxyRunCnt, filter, mm, saturate, aaType,
1315 constraint, viewMatrix, std::move(textureColorSpaceXform));
Robert Phillips4dca8312021-07-28 15:13:20 -04001316 sdc->addDrawOp(clip, std::move(op));
Robert Phillipse837e612019-11-15 11:02:50 -05001317 return;
1318 }
1319
Robert Phillips4dca8312021-07-28 15:13:20 -04001320 BatchSizeLimiter state(sdc, clip, context, cnt, filter, mm, saturate, constraint, viewMatrix,
Robert Phillipse837e612019-11-15 11:02:50 -05001321 std::move(textureColorSpaceXform));
1322
1323 // kNone and kMSAA never get altered
1324 if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1325 // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1326 while (state.numLeft() > 0) {
Brian Osman788b9162020-02-07 10:36:46 -05001327 int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
Robert Phillipse837e612019-11-15 11:02:50 -05001328
1329 state.createOp(set, clumpSize, aaType);
1330 }
1331 } else {
1332 // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1333 // can also get downgraded to kNone if all the quads are on integer coordinates and
1334 // axis-aligned.
1335 SkASSERT(aaType == GrAAType::kCoverage);
1336
1337 while (state.numLeft() > 0) {
1338 GrAAType runningAA = GrAAType::kNone;
1339 bool clumped = false;
1340
1341 for (int i = 0; i < state.numLeft(); ++i) {
1342 int absIndex = state.baseIndex() + i;
1343
Robert Phillips6bf11252020-07-31 12:15:00 -04001344 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone ||
1345 runningAA == GrAAType::kCoverage) {
Robert Phillipse837e612019-11-15 11:02:50 -05001346
1347 if (i >= GrResourceProvider::MaxNumAAQuads()) {
1348 // Here we either need to boost the AA type to kCoverage, but doing so with
1349 // all the accumulated quads would overflow, or we have a set of AA quads
1350 // that has just gotten too large. In either case, calve off the existing
1351 // quads as their own TextureOp.
1352 state.createOp(
1353 set,
1354 runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1355 runningAA); // maybe downgrading AA here
1356 clumped = true;
1357 break;
1358 }
1359
1360 runningAA = GrAAType::kCoverage;
1361 } else if (runningAA == GrAAType::kNone) {
1362
1363 if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1364 // Here we've found a consistent batch of non-AA quads that has gotten too
1365 // large. Calve it off as its own GrTextureOp.
1366 state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1367 GrAAType::kNone); // definitely downgrading AA here
1368 clumped = true;
1369 break;
1370 }
1371 }
1372 }
1373
1374 if (!clumped) {
1375 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1376 // quads and call it a day.
1377 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1378 }
1379 }
1380 }
1381}
Robert Phillips21453902021-08-27 16:05:04 -04001382
1383} // namespace skgpu::v1
Robert Phillipsae01f622019-11-13 15:56:31 +00001384
Brian Salomon34169692017-08-28 15:32:01 -04001385#if GR_TEST_UTILS
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04001386#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001387#include "src/gpu/GrProxyProvider.h"
1388#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -04001389
Robert Phillips21453902021-08-27 16:05:04 -04001390GR_DRAW_OP_TEST_DEFINE(TextureOpImpl) {
Brian Salomona56a7462020-02-07 14:17:25 -05001391 SkISize dims;
1392 dims.fHeight = random->nextULessThan(90) + 10;
1393 dims.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001394 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001395 GrMipmapped mipMapped = random->nextBool() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel09c94002018-06-08 22:11:51 +00001396 SkBackingFit fit = SkBackingFit::kExact;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001397 if (mipMapped == GrMipmapped::kNo) {
Greg Daniel09c94002018-06-08 22:11:51 +00001398 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1399 }
Greg Daniel4065d452018-11-16 15:43:41 -05001400 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -04001401 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1402 GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -05001403 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -04001404 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001405 format, dims, GrRenderable::kNo, 1, mipMapped, fit, SkBudgeted::kNo, GrProtected::kNo,
1406 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001407
Brian Salomon34169692017-08-28 15:32:01 -04001408 SkRect rect = GrTest::TestRect(random);
1409 SkRect srcRect;
1410 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1411 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1412 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1413 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1414 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -05001415 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001416 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
Brian Salomone69b9ef2020-07-22 11:18:06 -04001417 static_cast<uint32_t>(GrSamplerState::Filter::kLast) + 1);
1418 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
1419 if (mipMapped == GrMipmapped::kYes) {
1420 mm = (GrSamplerState::MipmapMode)random->nextULessThan(
1421 static_cast<uint32_t>(GrSamplerState::MipmapMode::kLast) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +00001422 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001423
Brian Osman3ebd3542018-07-30 14:36:53 -04001424 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001425 GrAAType aaType = GrAAType::kNone;
1426 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06001427 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -05001428 }
Brian Salomon2213ee92018-10-02 10:44:21 -04001429 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1430 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1431 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1432 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1433 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomon2432d062020-04-16 20:48:09 -04001434 bool useSubset = random->nextBool();
Robert Phillips21453902021-08-27 16:05:04 -04001435 auto saturate = random->nextBool() ? skgpu::v1::TextureOp::Saturate::kYes
1436 : skgpu::v1::TextureOp::Saturate::kNo;
Greg Daniel549325c2019-10-30 16:19:20 -04001437 GrSurfaceProxyView proxyView(
1438 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -05001439 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Brian Salomonfc118442019-11-22 19:09:27 -05001440 auto alphaType = static_cast<SkAlphaType>(
1441 random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
Greg Daniel549325c2019-10-30 16:19:20 -04001442
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001443 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
Robert Phillips21453902021-08-27 16:05:04 -04001444 return skgpu::v1::TextureOp::Make(context, std::move(proxyView), alphaType,
1445 std::move(texXform), filter, mm, color, saturate,
1446 SkBlendMode::kSrcOver, aaType, &quad,
1447 useSubset ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -04001448}
1449
Robert Phillips21453902021-08-27 16:05:04 -04001450#endif // GR_TEST_UTILS