blob: f027a0ded9855c1629234f7cf55b9b4d3eeb1acb [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"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040044#include "src/gpu/ops/GrTextureOp.h"
Robert Phillips46eb3ab2021-08-02 17:09:01 -040045#if SK_GPU_V1
Robert Phillips4dca8312021-07-28 15:13:20 -040046#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Robert Phillips46eb3ab2021-08-02 17:09:01 -040047#endif
Brian Salomon34169692017-08-28 15:32:01 -040048
49namespace {
50
Brian Salomon2432d062020-04-16 20:48:09 -040051using Subset = GrQuadPerEdgeAA::Subset;
Michael Ludwigc182b942018-11-16 10:27:51 -050052using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050053using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040054
Michael Ludwig22429f92019-06-27 10:44:48 -040055// Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
56// between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
57static SkSize axis_aligned_quad_size(const GrQuad& quad) {
58 SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
59 // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
60 float dw = sk_float_abs(quad.x(2) - quad.x(0)) + sk_float_abs(quad.y(2) - quad.y(0));
61 float dh = sk_float_abs(quad.x(1) - quad.x(0)) + sk_float_abs(quad.y(1) - quad.y(0));
62 return {dw, dh};
63}
64
Brian Salomone69b9ef2020-07-22 11:18:06 -040065static std::tuple<bool /* filter */,
66 bool /* mipmap */>
67filter_and_mm_have_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
Michael Ludwig22429f92019-06-27 10:44:48 -040068 // If not axis-aligned in src or dst, then always say it has an effect
69 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
70 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040071 return {true, true};
Michael Ludwig22429f92019-06-27 10:44:48 -040072 }
73
74 SkRect srcRect;
75 SkRect dstRect;
76 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
77 // Disable filtering when there is no scaling (width and height are the same), and the
78 // top-left corners have the same fraction (so src and dst snap to the pixel grid
79 // identically).
80 SkASSERT(srcRect.isSorted());
Brian Salomone69b9ef2020-07-22 11:18:06 -040081 bool filter = srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
82 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
83 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
84 bool mm = srcRect.width() > dstRect.width() || srcRect.height() > dstRect.height();
85 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040086 }
Brian Salomone69b9ef2020-07-22 11:18:06 -040087 // Extract edge lengths
88 SkSize srcSize = axis_aligned_quad_size(srcQuad);
89 SkSize dstSize = axis_aligned_quad_size(dstQuad);
90 // Although the quads are axis-aligned, the local coordinate system is transformed such
91 // that fractionally-aligned sample centers will not align with the device coordinate system
92 // So disable filtering when edges are the same length and both srcQuad and dstQuad
93 // 0th vertex is integer aligned.
94 bool filter = srcSize != dstSize ||
95 !SkScalarIsInt(srcQuad.x(0)) ||
96 !SkScalarIsInt(srcQuad.y(0)) ||
97 !SkScalarIsInt(dstQuad.x(0)) ||
98 !SkScalarIsInt(dstQuad.y(0));
99 bool mm = srcSize.fWidth > dstSize.fWidth || srcSize.fHeight > dstSize.fHeight;
100 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -0400101}
102
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500103// Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
104// regular and rectangular textures, w/ or w/o origin correction.
105struct NormalizationParams {
106 float fIW; // 1 / width of texture, or 1.0 for texture rectangles
Michael Ludwigc453a502020-05-29 12:29:12 -0400107 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 -0500108 float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
109};
Michael Ludwigadb12e72019-12-04 16:19:18 -0500110static NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
111 GrSurfaceOrigin origin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500112 // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
113 // normalize the src coordinates up front.
Michael Ludwigadb12e72019-12-04 16:19:18 -0500114 SkISize dimensions = proxy->backingStoreDimensions();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500115 float iw, ih, h;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500116 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500117 iw = ih = 1.f;
118 h = dimensions.height();
119 } else {
120 iw = 1.f / dimensions.width();
121 ih = 1.f / dimensions.height();
122 h = 1.f;
123 }
124
Michael Ludwigadb12e72019-12-04 16:19:18 -0500125 if (origin == kBottomLeft_GrSurfaceOrigin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500126 return {iw, -ih, h};
127 } else {
128 return {iw, ih, 0.0f};
129 }
130}
131
Brian Salomon2432d062020-04-16 20:48:09 -0400132// Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500133// 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 -0400134// subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
135// and y-flipping are applied as indicated by NormalizationParams.
136static SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
137 const NormalizationParams& params,
138 const SkRect* subsetRect) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500139 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Brian Salomon2432d062020-04-16 20:48:09 -0400140 if (!subsetRect) {
141 // Either the quad has no subset constraint and is batched with a subset constrained op
142 // (in which case we want a subset that doesn't restrict normalized tex coords), or the
143 // entire op doesn't use the subset, in which case the returned value is ignored.
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500144 return kLargeRect;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400145 }
146
Brian Salomon2432d062020-04-16 20:48:09 -0400147 auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400148 auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
149 if (filter == GrSamplerState::Filter::kNearest) {
150 // Make sure our insetting puts us at pixel centers.
151 ltrb = skvx::floor(ltrb*flipHi)*flipHi;
152 }
153 // Inset with pin to the rect center.
154 ltrb += skvx::Vec<4, float>({.5f, .5f, -.5f, -.5f});
155 auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
156 ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
157
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500158 // Normalize and offset
Elliot Evans9fdddf62020-07-29 16:39:11 -0400159 ltrb = ltrb * skvx::Vec<4, float>{params.fIW, params.fInvH, params.fIW, params.fInvH} +
160 skvx::Vec<4, float>{0.f, params.fYOffset, 0.f, params.fYOffset};
Michael Ludwigc453a502020-05-29 12:29:12 -0400161 if (params.fInvH < 0.f) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500162 // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
163 ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400164 }
165
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500166 SkRect out;
167 ltrb.store(&out);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500168 return out;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400169}
170
Michael Ludwig009b92e2019-02-15 16:03:53 -0500171// Normalizes logical src coords and corrects for origin
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500172static void normalize_src_quad(const NormalizationParams& params,
173 GrQuad* srcQuad) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500174 // The src quad should not have any perspective
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500175 SkASSERT(!srcQuad->hasPerspective());
176 skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
Elliot Evans9fdddf62020-07-29 16:39:11 -0400177 skvx::Vec<4, float> ys = srcQuad->y4f() * params.fInvH + params.fYOffset;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500178 xs.store(srcQuad->xs());
179 ys.store(srcQuad->ys());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500180}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400181
Robert Phillips46eb3ab2021-08-02 17:09:01 -0400182#if SK_GPU_V1
183
Michael Ludwig379e4962019-12-06 13:21:26 -0500184// Count the number of proxy runs in the entry set. This usually is already computed by
185// SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
186// for each split.
Robert Phillipse40495d2021-07-20 09:40:13 -0400187static int proxy_run_count(const GrTextureSetEntry set[], int count) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500188 int actualProxyRunCount = 0;
189 const GrSurfaceProxy* lastProxy = nullptr;
190 for (int i = 0; i < count; ++i) {
191 if (set[i].fProxyView.proxy() != lastProxy) {
192 actualProxyRunCount++;
193 lastProxy = set[i].fProxyView.proxy();
194 }
195 }
196 return actualProxyRunCount;
197}
Robert Phillips46eb3ab2021-08-02 17:09:01 -0400198#endif
Michael Ludwig379e4962019-12-06 13:21:26 -0500199
John Stilescbe4e282020-06-01 10:38:31 -0400200static bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
201 const DrawQuad& quad, const SkRect& subsetRect) {
202 // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
203 // can push all the way up to the edges of the the subset rect and the sampler shouldn't
204 // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
205 // the non-antialiased case.
206 SkRect localBounds = quad.fLocal.bounds();
207 if (aaType == GrAAType::kNone &&
208 filter == GrSamplerState::Filter::kNearest &&
209 quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
210 quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
211 subsetRect.contains(localBounds)) {
212
213 return true;
214 }
215
216 // If the subset rect is inset by at least 0.5 pixels into the local quad's bounds, the
217 // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
218 if (subsetRect.makeInset(0.5f, 0.5f).contains(localBounds)) {
219 return true;
220 }
221
222 // The subset rect cannot be ignored safely.
223 return false;
224}
225
Brian Salomon34169692017-08-28 15:32:01 -0400226/**
227 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
228 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
229 */
230class TextureOp final : public GrMeshDrawOp {
231public:
Herb Derbyc76d4092020-10-07 16:46:15 -0400232 static GrOp::Owner Make(GrRecordingContext* context,
233 GrSurfaceProxyView proxyView,
234 sk_sp<GrColorSpaceXform> textureXform,
235 GrSamplerState::Filter filter,
236 GrSamplerState::MipmapMode mm,
237 const SkPMColor4f& color,
238 GrTextureOp::Saturate saturate,
239 GrAAType aaType,
240 DrawQuad* quad,
241 const SkRect* subset) {
242
243 return GrOp::Make<TextureOp>(context, std::move(proxyView), std::move(textureXform),
244 filter, mm, color, saturate, aaType, quad, subset);
Brian Salomon34169692017-08-28 15:32:01 -0400245 }
Robert Phillipse837e612019-11-15 11:02:50 -0500246
Herb Derbyc76d4092020-10-07 16:46:15 -0400247 static GrOp::Owner Make(GrRecordingContext* context,
Robert Phillipse40495d2021-07-20 09:40:13 -0400248 GrTextureSetEntry set[],
Herb Derbyc76d4092020-10-07 16:46:15 -0400249 int cnt,
250 int proxyRunCnt,
251 GrSamplerState::Filter filter,
252 GrSamplerState::MipmapMode mm,
253 GrTextureOp::Saturate saturate,
254 GrAAType aaType,
255 SkCanvas::SrcRectConstraint constraint,
256 const SkMatrix& viewMatrix,
257 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500258 // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
259 SkASSERT(proxyRunCnt <= cnt);
Herb Derbyc76d4092020-10-07 16:46:15 -0400260 return GrOp::MakeWithExtraMemory<TextureOp>(
261 context, sizeof(ViewCountPair) * (proxyRunCnt - 1),
262 set, cnt, proxyRunCnt, filter, mm, saturate, aaType, constraint,
263 viewMatrix, std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400264 }
Brian Salomon34169692017-08-28 15:32:01 -0400265
Brian Salomon336ce7b2017-09-08 08:23:58 -0400266 ~TextureOp() override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500267 for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400268 fViewCountPairs[p].~ViewCountPair();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400269 }
270 }
Brian Salomon34169692017-08-28 15:32:01 -0400271
272 const char* name() const override { return "TextureOp"; }
273
Robert Phillips294723d2021-06-17 09:23:58 -0400274 void visitProxies(const GrVisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400275 bool mipped = (fMetadata.mipmapMode() != GrSamplerState::MipmapMode::kNone);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500276 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400277 func(fViewCountPairs[p].fProxy.get(), GrMipmapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400278 }
Chris Daltondbb833b2020-03-17 12:15:46 -0600279 if (fDesc && fDesc->fProgramInfo) {
280 fDesc->fProgramInfo->visitFPProxies(func);
281 }
Brian Salomond7065e72018-10-12 11:42:02 -0400282 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400283
John Stiles8d9bf642020-08-12 15:07:45 -0400284#ifdef SK_DEBUG
Michael Ludwig4ef1ca12019-12-19 10:58:52 -0500285 static void ValidateResourceLimits() {
286 // The op implementation has an upper bound on the number of quads that it can represent.
287 // However, the resource manager imposes its own limit on the number of quads, which should
288 // always be lower than the numerical limit this op can hold.
289 using CountStorage = decltype(Metadata::fTotalQuadCount);
290 CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
291 // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
292 int resourceLimit = SkTo<int>(maxQuadCount);
293 SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
294 GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
295 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500296#endif
Brian Salomon34169692017-08-28 15:32:01 -0400297
Chris Dalton57ab06c2021-04-22 12:57:28 -0600298 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip*,
299 GrClampType clampType) override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500300 SkASSERT(fMetadata.colorType() == ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400301 auto iter = fQuads.metadata();
302 while(iter.next()) {
Brian Osman2715bf52019-12-06 14:38:47 -0500303 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
Brian Salomon5e29e312021-04-27 11:30:00 -0400304 colorType = std::max(static_cast<GrQuadPerEdgeAA::ColorType>(fMetadata.fColorType),
305 colorType);
306 if (caps.reducedShaderMode()) {
307 colorType = std::max(colorType, GrQuadPerEdgeAA::ColorType::kByte);
308 }
309 fMetadata.fColorType = static_cast<uint16_t>(colorType);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400310 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700311 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400312 }
313
Brian Salomon485b8c62018-01-12 15:11:06 -0500314 FixedFunctionFlags fixedFunctionFlags() const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500315 return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
316 : FixedFunctionFlags::kNone;
Brian Salomon485b8c62018-01-12 15:11:06 -0500317 }
Brian Salomon34169692017-08-28 15:32:01 -0400318
319 DEFINE_OP_CLASS_ID
320
321private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400322 friend class ::GrOp;
Brian Salomon762d5e72017-12-01 10:25:08 -0500323
Brian Salomon2432d062020-04-16 20:48:09 -0400324 struct ColorSubsetAndAA {
325 ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400326 : fColor(color)
Brian Salomon2432d062020-04-16 20:48:09 -0400327 , fSubsetRect(subsetRect)
Michael Ludwig4384f042019-12-05 10:30:35 -0500328 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
329 SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
Michael Ludwig425eb452019-06-27 10:13:27 -0400330 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400331
332 SkPMColor4f fColor;
Brian Salomon2432d062020-04-16 20:48:09 -0400333 // 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 -0500334 // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
Brian Salomon2432d062020-04-16 20:48:09 -0400335 SkRect fSubsetRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400336 unsigned fAAFlags : 4;
337
Michael Ludwig425eb452019-06-27 10:13:27 -0400338 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
339 };
Michael Ludwigadb12e72019-12-04 16:19:18 -0500340
Greg Daniel549325c2019-10-30 16:19:20 -0400341 struct ViewCountPair {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500342 // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
343 // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
344 // swizzle so that is stored in the op metadata.
345 sk_sp<GrSurfaceProxy> fProxy;
Michael Ludwig425eb452019-06-27 10:13:27 -0400346 int fQuadCnt;
347 };
348
Michael Ludwigadb12e72019-12-04 16:19:18 -0500349 // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
350 // increase the size of the op; increasing the op size can have a surprising impact on
351 // performance (since texture ops are one of the most commonly used in an app).
352 struct Metadata {
353 // AAType must be filled after initialization; ColorType is determined in finalize()
Brian Salomone69b9ef2020-07-22 11:18:06 -0400354 Metadata(const GrSwizzle& swizzle,
355 GrSamplerState::Filter filter,
356 GrSamplerState::MipmapMode mm,
357 GrQuadPerEdgeAA::Subset subset,
358 GrTextureOp::Saturate saturate)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500359 : fSwizzle(swizzle)
360 , fProxyCount(1)
361 , fTotalQuadCount(1)
Michael Ludwig4384f042019-12-05 10:30:35 -0500362 , fFilter(static_cast<uint16_t>(filter))
Brian Salomone69b9ef2020-07-22 11:18:06 -0400363 , fMipmapMode(static_cast<uint16_t>(mm))
Michael Ludwig4384f042019-12-05 10:30:35 -0500364 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
365 , fColorType(static_cast<uint16_t>(ColorType::kNone))
Brian Salomon2432d062020-04-16 20:48:09 -0400366 , fSubset(static_cast<uint16_t>(subset))
Michael Ludwig4384f042019-12-05 10:30:35 -0500367 , fSaturate(static_cast<uint16_t>(saturate)) {}
Michael Ludwigadb12e72019-12-04 16:19:18 -0500368
Michael Ludwig4384f042019-12-05 10:30:35 -0500369 GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
Michael Ludwigadb12e72019-12-04 16:19:18 -0500370 uint16_t fProxyCount;
371 // This will be >= fProxyCount, since a proxy may be drawn multiple times
372 uint16_t fTotalQuadCount;
373
Michael Ludwig4384f042019-12-05 10:30:35 -0500374 // These must be based on uint16_t to help MSVC's pack bitfields optimally
375 uint16_t fFilter : 2; // GrSamplerState::Filter
Brian Salomone69b9ef2020-07-22 11:18:06 -0400376 uint16_t fMipmapMode : 2; // GrSamplerState::MipmapMode
Michael Ludwig4384f042019-12-05 10:30:35 -0500377 uint16_t fAAType : 2; // GrAAType
378 uint16_t fColorType : 2; // GrQuadPerEdgeAA::ColorType
Brian Salomon2432d062020-04-16 20:48:09 -0400379 uint16_t fSubset : 1; // bool
Michael Ludwig4384f042019-12-05 10:30:35 -0500380 uint16_t fSaturate : 1; // bool
Brian Salomone69b9ef2020-07-22 11:18:06 -0400381 uint16_t fUnused : 6; // # of bits left before Metadata exceeds 8 bytes
Michael Ludwigadb12e72019-12-04 16:19:18 -0500382
383 GrSamplerState::Filter filter() const {
384 return static_cast<GrSamplerState::Filter>(fFilter);
385 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400386 GrSamplerState::MipmapMode mipmapMode() const {
387 return static_cast<GrSamplerState::MipmapMode>(fMipmapMode);
388 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500389 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
390 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
Brian Salomon2432d062020-04-16 20:48:09 -0400391 Subset subset() const { return static_cast<Subset>(fSubset); }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500392 GrTextureOp::Saturate saturate() const {
393 return static_cast<GrTextureOp::Saturate>(fSaturate);
394 }
395
396 static_assert(GrSamplerState::kFilterCount <= 4);
397 static_assert(kGrAATypeCount <= 4);
398 static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
399 };
Michael Ludwig4384f042019-12-05 10:30:35 -0500400 static_assert(sizeof(Metadata) == 8);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500401
Chris Daltondbb833b2020-03-17 12:15:46 -0600402 // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
403 // store the data in a separate struct in order to minimize the size of the TextureOp.
404 // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
405 // may want to re-evaluate whether this is still necessary.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400406 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600407 // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
408 // allocatePrePreparedVertices is also called.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400409 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600410 // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
411 // part of the flushState).
412 struct Desc {
413 VertexSpec fVertexSpec;
414 int fNumProxies = 0;
415 int fNumTotalQuads = 0;
Robert Phillips32803ff2019-10-23 08:26:08 -0400416
Chris Daltondbb833b2020-03-17 12:15:46 -0600417 // This member variable is only used by 'onPrePrepareDraws'.
418 char* fPrePreparedVertices = nullptr;
419
420 GrProgramInfo* fProgramInfo = nullptr;
421
422 sk_sp<const GrBuffer> fIndexBuffer;
423 sk_sp<const GrBuffer> fVertexBuffer;
424 int fBaseVertex;
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400425
426 // How big should 'fVertices' be to hold all the vertex data?
427 size_t totalSizeInBytes() const {
Chris Daltondbb833b2020-03-17 12:15:46 -0600428 return this->totalNumVertices() * fVertexSpec.vertexSize();
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400429 }
430
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400431 int totalNumVertices() const {
432 return fNumTotalQuads * fVertexSpec.verticesPerQuad();
433 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400434
Chris Daltondbb833b2020-03-17 12:15:46 -0600435 void allocatePrePreparedVertices(SkArenaAlloc* arena) {
436 fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400437 }
Robert Phillips32803ff2019-10-23 08:26:08 -0400438 };
Brian Salomon2432d062020-04-16 20:48:09 -0400439 // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
Greg Daniel549325c2019-10-30 16:19:20 -0400440 TextureOp(GrSurfaceProxyView proxyView,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400441 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
442 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400443 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400444 const SkPMColor4f& color,
445 GrTextureOp::Saturate saturate,
446 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500447 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400448 const SkRect* subsetRect)
Brian Salomon34169692017-08-28 15:32:01 -0400449 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400450 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400451 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600452 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400453 , fMetadata(proxyView.swizzle(), filter, mm, Subset(!!subsetRect), saturate) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400454 // Clean up disparities between the overall aa type and edge configuration and apply
455 // optimizations based on the rect and matrix when appropriate
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500456 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
457 &aaType, &quad->fEdgeFlags);
Michael Ludwig4384f042019-12-05 10:30:35 -0500458 fMetadata.fAAType = static_cast<uint16_t>(aaType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400459
Brian Salomonf1709042018-10-03 11:57:00 -0400460 // We expect our caller to have already caught this optimization.
Brian Salomon2432d062020-04-16 20:48:09 -0400461 SkASSERT(!subsetRect ||
462 !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500463
Brian Salomonf09abc52018-10-03 15:59:04 -0400464 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
John Stilescbe4e282020-06-01 10:38:31 -0400465 // Try to identify cases where the subsetting isn't actually necessary, and skip it.
466 if (subsetRect) {
467 if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
468 subsetRect = nullptr;
469 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
470 }
Brian Salomonf09abc52018-10-03 15:59:04 -0400471 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500472
Brian Salomon2432d062020-04-16 20:48:09 -0400473 // Normalize src coordinates and the subset (if set)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500474 NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
475 proxyView.origin());
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500476 normalize_src_quad(params, &quad->fLocal);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400477 SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500478
Michael Ludwig949ceb22020-02-07 10:14:45 -0500479 // Set bounds before clipping so we don't have to worry about unioning the bounds of
480 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig575c9212021-07-13 11:09:52 -0400481 bool hairline = GrQuadUtils::WillUseHairline(quad->fDevice, aaType, quad->fEdgeFlags);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500482 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Michael Ludwig575c9212021-07-13 11:09:52 -0400483 hairline ? IsHairline::kYes : IsHairline::kNo);
Brian Salomon2432d062020-04-16 20:48:09 -0400484 int quadCount = this->appendQuad(quad, color, subset);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500485 fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
Brian Salomond7065e72018-10-12 11:42:02 -0400486 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400487
Robert Phillipse40495d2021-07-20 09:40:13 -0400488 TextureOp(GrTextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400489 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500490 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400491 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400492 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400493 GrTextureOp::Saturate saturate,
494 GrAAType aaType,
495 SkCanvas::SrcRectConstraint constraint,
496 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500497 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400498 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400499 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400500 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600501 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400502 , fMetadata(set[0].fProxyView.swizzle(),
503 GrSamplerState::Filter::kNearest,
504 GrSamplerState::MipmapMode::kNone,
505 Subset::kNo,
506 saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500507 // Update counts to reflect the batch op
Michael Ludwig379e4962019-12-06 13:21:26 -0500508 fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500509 fMetadata.fTotalQuadCount = SkToUInt(cnt);
510
Brian Salomond7065e72018-10-12 11:42:02 -0400511 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500512
513 GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon2432d062020-04-16 20:48:09 -0400514 Subset netSubset = Subset::kNo;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500515 GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400516 GrSamplerState::MipmapMode netMM = GrSamplerState::MipmapMode::kNone;
Michael Ludwig575c9212021-07-13 11:09:52 -0400517 bool hasSubpixel = false;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500518
Michael Ludwig379e4962019-12-06 13:21:26 -0500519 const GrSurfaceProxy* curProxy = nullptr;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500520
Michael Ludwig379e4962019-12-06 13:21:26 -0500521 // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
522 // increases when set[q]'s proxy changes.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500523 int p = 0;
524 for (int q = 0; q < cnt; ++q) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400525 SkASSERT(mm == GrSamplerState::MipmapMode::kNone ||
526 (set[0].fProxyView.proxy()->asTextureProxy()->mipmapped() ==
527 GrMipmapped::kYes));
Michael Ludwig379e4962019-12-06 13:21:26 -0500528 if (q == 0) {
Greg Daniel549325c2019-10-30 16:19:20 -0400529 // We do not placement new the first ViewCountPair since that one is allocated and
530 // initialized as part of the GrTextureOp creation.
Michael Ludwig379e4962019-12-06 13:21:26 -0500531 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
532 fViewCountPairs[0].fQuadCnt = 0;
533 curProxy = fViewCountPairs[0].fProxy.get();
534 } else if (set[q].fProxyView.proxy() != curProxy) {
Greg Daniel549325c2019-10-30 16:19:20 -0400535 // We must placement new the ViewCountPairs here so that the sk_sps in the
536 // GrSurfaceProxyView get initialized properly.
Michael Ludwig379e4962019-12-06 13:21:26 -0500537 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
Michael Ludwigadb12e72019-12-04 16:19:18 -0500538
Michael Ludwig379e4962019-12-06 13:21:26 -0500539 curProxy = fViewCountPairs[p].fProxy.get();
Greg Danielc71c7962020-01-14 16:44:18 -0500540 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
541 curProxy, fViewCountPairs[0].fProxy.get()));
Michael Ludwig379e4962019-12-06 13:21:26 -0500542 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
Michael Ludwig379e4962019-12-06 13:21:26 -0500543 } // else another quad referencing the same proxy
Michael Ludwigce62dec2019-02-19 11:48:46 -0500544
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500545 SkMatrix ctm = viewMatrix;
Michael Ludwig379e4962019-12-06 13:21:26 -0500546 if (set[q].fPreViewMatrix) {
547 ctm.preConcat(*set[q].fPreViewMatrix);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500548 }
549
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400550 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
551 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500552 DrawQuad quad;
Michael Ludwig379e4962019-12-06 13:21:26 -0500553 if (set[q].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500554 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400555
556 SkPoint srcPts[4];
Michael Ludwig379e4962019-12-06 13:21:26 -0500557 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500558 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400559 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500560 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
561 quad.fLocal = GrQuad(set[q].fSrcRect);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400562 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500563
Brian Salomone69b9ef2020-07-22 11:18:06 -0400564 if (netFilter != filter || netMM != mm) {
565 // The only way netFilter != filter is if linear is requested and we haven't yet
566 // found a quad that requires linear (so net is still nearest). Similar for mip
567 // mapping.
Brian Salomonf7353512020-07-22 19:26:48 -0400568 SkASSERT(filter == netFilter ||
569 (netFilter == GrSamplerState::Filter::kNearest && filter > netFilter));
570 SkASSERT(mm == netMM ||
571 (netMM == GrSamplerState::MipmapMode::kNone && mm > netMM));
Brian Salomone69b9ef2020-07-22 11:18:06 -0400572 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad.fLocal, quad.fDevice);
Michael Ludwig03f17cd2021-08-17 18:27:48 +0000573 if (mustFilter && filter != GrSamplerState::Filter::kNearest) {
574 netFilter = filter;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400575 }
Michael Ludwig03f17cd2021-08-17 18:27:48 +0000576 if (mustMM && mm != GrSamplerState::MipmapMode::kNone) {
577 netMM = mm;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400578 }
Michael Ludwig22429f92019-06-27 10:44:48 -0400579 }
580
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500581 // Determine the AA type for the quad, then merge with net AA type
Michael Ludwig6bee7762018-10-19 09:50:36 -0400582 GrAAType aaForQuad;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500583 GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
584 &aaForQuad, &quad.fEdgeFlags);
Michael Ludwig81159972021-07-13 15:42:04 -0400585 // Update overall bounds of the op as the union of all quads
586 bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
587 hasSubpixel |= GrQuadUtils::WillUseHairline(quad.fDevice, aaForQuad, quad.fEdgeFlags);
John Stilescbe4e282020-06-01 10:38:31 -0400588
Michael Ludwig6bee7762018-10-19 09:50:36 -0400589 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
590 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500591 if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
592 netAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400593 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400594
595 // Calculate metadata for the entry
Brian Salomon2432d062020-04-16 20:48:09 -0400596 const SkRect* subsetForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400597 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
John Stilescbe4e282020-06-01 10:38:31 -0400598 // Check (briefly) if the subset rect is actually needed for this set entry.
599 SkRect* subsetRect = &set[q].fSrcRect;
600 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
601 if (!safe_to_ignore_subset_rect(aaForQuad, filter, quad, *subsetRect)) {
602 netSubset = Subset::kYes;
603 subsetForQuad = subsetRect;
604 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400605 }
606 }
John Stilescbe4e282020-06-01 10:38:31 -0400607
608 // Normalize the src quads and apply origin
609 NormalizationParams proxyParams = proxy_normalization_params(
610 curProxy, set[q].fProxyView.origin());
611 normalize_src_quad(proxyParams, &quad.fLocal);
612
Brian Salomon2432d062020-04-16 20:48:09 -0400613 // This subset may represent a no-op, otherwise it will have the origin and dimensions
Michael Ludwig03f17cd2021-08-17 18:27:48 +0000614 // of the texture applied to it. Insetting for bilinear filtering is deferred until
615 // on[Pre]Prepare so that the overall filter can be lazily determined.
Brian Salomon75cebbe2020-05-18 14:08:14 -0400616 SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500617
Michael Ludwig949ceb22020-02-07 10:14:45 -0500618 // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
619 // ViewCountPair (this frequently happens when Chrome draws 9-patches).
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400620 fViewCountPairs[p].fQuadCnt += this->appendQuad(&quad, set[q].fColor, subset);
Brian Salomond7065e72018-10-12 11:42:02 -0400621 }
Michael Ludwig406172a2019-12-06 14:05:19 -0500622 // The # of proxy switches should match what was provided (+1 because we incremented p
Michael Ludwig379e4962019-12-06 13:21:26 -0500623 // when a new proxy was encountered).
Michael Ludwig406172a2019-12-06 14:05:19 -0500624 SkASSERT((p + 1) == fMetadata.fProxyCount);
Michael Ludwig379e4962019-12-06 13:21:26 -0500625 SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500626
Michael Ludwig4384f042019-12-05 10:30:35 -0500627 fMetadata.fAAType = static_cast<uint16_t>(netAAType);
628 fMetadata.fFilter = static_cast<uint16_t>(netFilter);
Brian Salomon2432d062020-04-16 20:48:09 -0400629 fMetadata.fSubset = static_cast<uint16_t>(netSubset);
Brian Salomon34169692017-08-28 15:32:01 -0400630
Michael Ludwig575c9212021-07-13 11:09:52 -0400631 this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage),
632 hasSubpixel ? IsHairline::kYes : IsHairline::kNo);
Brian Salomon17031a72018-05-22 14:14:07 -0400633 }
634
Brian Salomon2432d062020-04-16 20:48:09 -0400635 int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& subset) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500636 DrawQuad extra;
Michael Ludwig65299902021-05-13 12:02:23 -0400637 // Always clip to W0 to stay consistent with GrQuad::bounds
638 int quadCount = GrQuadUtils::ClipToW0(quad, &extra);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500639 if (quadCount == 0) {
640 // We can't discard the op at this point, but disable AA flags so it won't go through
641 // inset/outset processing
642 quad->fEdgeFlags = GrQuadAAFlags::kNone;
643 quadCount = 1;
644 }
Brian Salomon2432d062020-04-16 20:48:09 -0400645 fQuads.append(quad->fDevice, {color, subset, quad->fEdgeFlags}, &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500646 if (quadCount > 1) {
Brian Salomon2432d062020-04-16 20:48:09 -0400647 fQuads.append(extra.fDevice, {color, subset, extra.fEdgeFlags}, &extra.fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500648 fMetadata.fTotalQuadCount++;
649 }
650 return quadCount;
651 }
652
Robert Phillips2669a7b2020-03-12 12:07:19 -0400653 GrProgramInfo* programInfo() override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600654 // Although this Op implements its own onPrePrepareDraws it calls GrMeshDrawOps' version so
655 // this entry point will be called.
656 return (fDesc) ? fDesc->fProgramInfo : nullptr;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400657 }
658
Chris Daltondbb833b2020-03-17 12:15:46 -0600659 void onCreateProgramInfo(const GrCaps* caps,
660 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500661 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600662 bool usesMSAASurface,
Chris Daltondbb833b2020-03-17 12:15:46 -0600663 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400664 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500665 GrXferBarrierFlags renderPassXferBarriers,
666 GrLoadOp colorLoadOp) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600667 SkASSERT(fDesc);
668
669 GrGeometryProcessor* gp;
670
671 {
672 const GrBackendFormat& backendFormat =
673 fViewCountPairs[0].fProxy->backendFormat();
674
675 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
676 fMetadata.filter());
677
678 gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
679 arena, fDesc->fVertexSpec, *caps->shaderCaps(), backendFormat, samplerState,
680 fMetadata.fSwizzle, std::move(fTextureColorSpaceXform), fMetadata.saturate());
681
682 SkASSERT(fDesc->fVertexSpec.vertexSize() == gp->vertexStride());
683 }
684
685 auto pipelineFlags = (GrAAType::kMSAA == fMetadata.aaType()) ?
686 GrPipeline::InputFlags::kHWAntialias : GrPipeline::InputFlags::kNone;
687
688 fDesc->fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Brian Salomon8afde5f2020-04-01 16:22:00 -0400689 caps, arena, writeView, std::move(appliedClip), dstProxyView, gp,
Chris Daltondbb833b2020-03-17 12:15:46 -0600690 GrProcessorSet::MakeEmptySet(), fDesc->fVertexSpec.primitiveType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500691 renderPassXferBarriers, colorLoadOp, pipelineFlags);
Robert Phillips4133dc42020-03-11 15:55:55 -0400692 }
693
Robert Phillipsdf70f152019-11-15 14:57:05 -0500694 void onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -0500695 const GrSurfaceProxyView& writeView,
Robert Phillips8053c972019-11-21 10:44:53 -0500696 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -0400697 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500698 GrXferBarrierFlags renderPassXferBarriers,
699 GrLoadOp colorLoadOp) override {
Robert Phillips61fc7992019-10-22 11:58:17 -0400700 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips29f38542019-10-16 09:20:25 -0400701
Robert Phillips61fc7992019-10-22 11:58:17 -0400702 SkDEBUGCODE(this->validate();)
Chris Daltondbb833b2020-03-17 12:15:46 -0600703 SkASSERT(!fDesc);
Robert Phillips61fc7992019-10-22 11:58:17 -0400704
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500705 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillips61fc7992019-10-22 11:58:17 -0400706
Chris Daltondbb833b2020-03-17 12:15:46 -0600707 fDesc = arena->make<Desc>();
Brian Salomonb8c4add2021-06-28 09:20:44 -0400708 this->characterize(fDesc);
Chris Daltondbb833b2020-03-17 12:15:46 -0600709 fDesc->allocatePrePreparedVertices(arena);
710 FillInVertices(*context->priv().caps(), this, fDesc, fDesc->fPrePreparedVertices);
Robert Phillips61fc7992019-10-22 11:58:17 -0400711
Chris Daltondbb833b2020-03-17 12:15:46 -0600712 // This will call onCreateProgramInfo and register the created program with the DDL.
Greg Danield358cbe2020-09-11 09:33:54 -0400713 this->INHERITED::onPrePrepareDraws(context, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500714 renderPassXferBarriers, colorLoadOp);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400715 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400716
Chris Daltondbb833b2020-03-17 12:15:46 -0600717 static void FillInVertices(const GrCaps& caps, TextureOp* texOp, Desc* desc, char* vertexData) {
718 SkASSERT(vertexData);
719
Ben Wagner097a9a42021-07-27 12:20:03 -0400720 SkDEBUGCODE(int totQuadsSeen = 0;)
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400721 SkDEBUGCODE(int totVerticesSeen = 0;)
Michael Ludwig189c9802019-11-21 11:21:12 -0500722 SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400723
Chris Daltondbb833b2020-03-17 12:15:46 -0600724 GrQuadPerEdgeAA::Tessellator tessellator(desc->fVertexSpec, vertexData);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400725 for (const auto& op : ChainRange<TextureOp>(texOp)) {
726 auto iter = op.fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500727 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500728 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
729 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400730
Chris Daltondbb833b2020-03-17 12:15:46 -0600731 for (int i = 0; i < quadCnt && iter.next(); ++i) {
732 SkASSERT(iter.isLocalValid());
Brian Salomon2432d062020-04-16 20:48:09 -0400733 const ColorSubsetAndAA& info = iter.metadata();
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500734
Chris Daltondbb833b2020-03-17 12:15:46 -0600735 tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
Brian Salomon75cebbe2020-05-18 14:08:14 -0400736 info.fSubsetRect, info.aaFlags());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400737 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400738
Chris Daltondbb833b2020-03-17 12:15:46 -0600739 SkASSERT((totVerticesSeen + meshVertexCnt) * vertexSize
740 == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400741
Ben Wagner097a9a42021-07-27 12:20:03 -0400742 SkDEBUGCODE(totQuadsSeen += quadCnt;)
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400743 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
744 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400745 }
746
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400747 // If quad counts per proxy were calculated correctly, the entire iterator
748 // should have been consumed.
Chris Daltondbb833b2020-03-17 12:15:46 -0600749 SkASSERT(!iter.next());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400750 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400751
Chris Daltondbb833b2020-03-17 12:15:46 -0600752 SkASSERT(desc->totalSizeInBytes() == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400753 SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
754 SkASSERT(totVerticesSeen == desc->totalNumVertices());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400755 }
756
Robert Phillips29f38542019-10-16 09:20:25 -0400757#ifdef SK_DEBUG
Robert Phillips6bf11252020-07-31 12:15:00 -0400758 static int validate_op(GrTextureType textureType,
759 GrAAType aaType,
760 GrSwizzle swizzle,
761 const TextureOp* op) {
762 SkASSERT(op->fMetadata.fSwizzle == swizzle);
763
764 int quadCount = 0;
765 for (unsigned p = 0; p < op->fMetadata.fProxyCount; ++p) {
766 auto* proxy = op->fViewCountPairs[p].fProxy->asTextureProxy();
767 quadCount += op->fViewCountPairs[p].fQuadCnt;
768 SkASSERT(proxy);
769 SkASSERT(proxy->textureType() == textureType);
770 }
771
772 SkASSERT(aaType == op->fMetadata.aaType());
773 return quadCount;
774 }
775
Robert Phillips29f38542019-10-16 09:20:25 -0400776 void validate() const override {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500777 // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
Michael Ludwigadb12e72019-12-04 16:19:18 -0500778 auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
779 GrAAType aaType = fMetadata.aaType();
Robert Phillips6bf11252020-07-31 12:15:00 -0400780 GrSwizzle swizzle = fMetadata.fSwizzle;
Robert Phillips29f38542019-10-16 09:20:25 -0400781
Robert Phillips6bf11252020-07-31 12:15:00 -0400782 int quadCount = validate_op(textureType, aaType, swizzle, this);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500783
Robert Phillips6bf11252020-07-31 12:15:00 -0400784 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
785 quadCount += validate_op(textureType, aaType, swizzle,
786 static_cast<const TextureOp*>(tmp));
787 }
Robert Phillips29f38542019-10-16 09:20:25 -0400788
Robert Phillips6bf11252020-07-31 12:15:00 -0400789 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
790 quadCount += validate_op(textureType, aaType, swizzle,
791 static_cast<const TextureOp*>(tmp));
Robert Phillips29f38542019-10-16 09:20:25 -0400792 }
Robert Phillipse837e612019-11-15 11:02:50 -0500793
794 SkASSERT(quadCount == this->numChainedQuads());
Robert Phillips29f38542019-10-16 09:20:25 -0400795 }
Robert Phillips6bf11252020-07-31 12:15:00 -0400796
Robert Phillips29f38542019-10-16 09:20:25 -0400797#endif
798
Robert Phillipse837e612019-11-15 11:02:50 -0500799#if GR_TEST_UTILS
800 int numQuads() const final { return this->totNumQuads(); }
801#endif
802
Brian Salomonb8c4add2021-06-28 09:20:44 -0400803 void characterize(Desc* desc) const {
Robert Phillips6bf11252020-07-31 12:15:00 -0400804 SkDEBUGCODE(this->validate();)
805
Robert Phillips29f38542019-10-16 09:20:25 -0400806 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
807 ColorType colorType = ColorType::kNone;
808 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomon2432d062020-04-16 20:48:09 -0400809 Subset subset = Subset::kNo;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500810 GrAAType overallAAType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400811
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400812 desc->fNumProxies = 0;
813 desc->fNumTotalQuads = 0;
814 int maxQuadsPerMesh = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400815
Brian Salomonf7232642018-09-19 08:58:08 -0400816 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400817 if (op.fQuads.deviceQuadType() > quadType) {
818 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500819 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400820 if (op.fQuads.localQuadType() > srcQuadType) {
821 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500822 }
Brian Salomon2432d062020-04-16 20:48:09 -0400823 if (op.fMetadata.subset() == Subset::kYes) {
824 subset = Subset::kYes;
Brian Salomonf7232642018-09-19 08:58:08 -0400825 }
Brian Osman788b9162020-02-07 10:36:46 -0500826 colorType = std::max(colorType, op.fMetadata.colorType());
Michael Ludwigadb12e72019-12-04 16:19:18 -0500827 desc->fNumProxies += op.fMetadata.fProxyCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400828
Michael Ludwigadb12e72019-12-04 16:19:18 -0500829 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Brian Osman788b9162020-02-07 10:36:46 -0500830 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
Brian Salomonf7232642018-09-19 08:58:08 -0400831 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400832 desc->fNumTotalQuads += op.totNumQuads();
833
Michael Ludwigadb12e72019-12-04 16:19:18 -0500834 if (op.fMetadata.aaType() == GrAAType::kCoverage) {
Robert Phillips29f38542019-10-16 09:20:25 -0400835 overallAAType = GrAAType::kCoverage;
Brian Salomonae7d7702018-10-14 15:05:45 -0400836 }
Brian Salomon34169692017-08-28 15:32:01 -0400837 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400838
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400839 SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
840
841 SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
842
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400843 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(overallAAType,
Brian Salomonb8c4add2021-06-28 09:20:44 -0400844 maxQuadsPerMesh);
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400845
846 desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
Brian Salomon2432d062020-04-16 20:48:09 -0400847 subset, overallAAType, /* alpha as coverage */ true,
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400848 indexBufferOption);
Robert Phillipse837e612019-11-15 11:02:50 -0500849
850 SkASSERT(desc->fNumTotalQuads <= GrQuadPerEdgeAA::QuadLimit(indexBufferOption));
Robert Phillips29f38542019-10-16 09:20:25 -0400851 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500852
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400853 int totNumQuads() const {
854#ifdef SK_DEBUG
855 int tmp = 0;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500856 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400857 tmp += fViewCountPairs[p].fQuadCnt;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400858 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500859 SkASSERT(tmp == fMetadata.fTotalQuadCount);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400860#endif
861
Michael Ludwigadb12e72019-12-04 16:19:18 -0500862 return fMetadata.fTotalQuadCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400863 }
864
865 int numChainedQuads() const {
866 int numChainedQuads = this->totNumQuads();
867
868 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
869 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
870 }
871
872 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
873 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
874 }
875
876 return numChainedQuads;
877 }
878
Robert Phillips29f38542019-10-16 09:20:25 -0400879 // onPrePrepareDraws may or may not have been called at this point
Robert Phillips71143952021-06-17 14:55:07 -0400880 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips29f38542019-10-16 09:20:25 -0400881 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500882
Robert Phillips29f38542019-10-16 09:20:25 -0400883 SkDEBUGCODE(this->validate();)
884
Chris Daltondbb833b2020-03-17 12:15:46 -0600885 SkASSERT(!fDesc || fDesc->fPrePreparedVertices);
Robert Phillips29f38542019-10-16 09:20:25 -0400886
Chris Daltondbb833b2020-03-17 12:15:46 -0600887 if (!fDesc) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400888 SkArenaAlloc* arena = target->allocator();
Chris Daltondbb833b2020-03-17 12:15:46 -0600889 fDesc = arena->make<Desc>();
Brian Salomonb8c4add2021-06-28 09:20:44 -0400890 this->characterize(fDesc);
Chris Daltondbb833b2020-03-17 12:15:46 -0600891 SkASSERT(!fDesc->fPrePreparedVertices);
Brian Salomonf7232642018-09-19 08:58:08 -0400892 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400893
Chris Daltondbb833b2020-03-17 12:15:46 -0600894 size_t vertexSize = fDesc->fVertexSpec.vertexSize();
Brian Salomon92be2f72018-06-19 14:33:47 -0400895
Chris Daltondbb833b2020-03-17 12:15:46 -0600896 void* vdata = target->makeVertexSpace(vertexSize, fDesc->totalNumVertices(),
897 &fDesc->fVertexBuffer, &fDesc->fBaseVertex);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400898 if (!vdata) {
899 SkDebugf("Could not allocate vertices\n");
900 return;
Brian Salomon34169692017-08-28 15:32:01 -0400901 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400902
Chris Daltondbb833b2020-03-17 12:15:46 -0600903 if (fDesc->fVertexSpec.needsIndexBuffer()) {
904 fDesc->fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(
905 target, fDesc->fVertexSpec.indexBufferOption());
906 if (!fDesc->fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400907 SkDebugf("Could not allocate indices\n");
908 return;
909 }
910 }
911
Chris Daltondbb833b2020-03-17 12:15:46 -0600912 if (fDesc->fPrePreparedVertices) {
913 memcpy(vdata, fDesc->fPrePreparedVertices, fDesc->totalSizeInBytes());
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400914 } else {
Chris Daltondbb833b2020-03-17 12:15:46 -0600915 FillInVertices(target->caps(), this, fDesc, (char*) vdata);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400916 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700917 }
918
919 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600920 if (!fDesc->fVertexBuffer) {
921 return;
922 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500923
Chris Daltondbb833b2020-03-17 12:15:46 -0600924 if (fDesc->fVertexSpec.needsIndexBuffer() && !fDesc->fIndexBuffer) {
925 return;
926 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500927
Chris Daltondbb833b2020-03-17 12:15:46 -0600928 if (!fDesc->fProgramInfo) {
929 this->createProgramInfo(flushState);
930 SkASSERT(fDesc->fProgramInfo);
931 }
932
933 flushState->bindPipelineAndScissorClip(*fDesc->fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400934 flushState->bindBuffers(std::move(fDesc->fIndexBuffer), nullptr,
935 std::move(fDesc->fVertexBuffer));
Chris Daltondbb833b2020-03-17 12:15:46 -0600936
937 int totQuadsSeen = 0;
938 SkDEBUGCODE(int numDraws = 0;)
939 for (const auto& op : ChainRange<TextureOp>(this)) {
940 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
941 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
942 SkASSERT(numDraws < fDesc->fNumProxies);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400943 flushState->bindTextures(fDesc->fProgramInfo->geomProc(),
Chris Daltondbb833b2020-03-17 12:15:46 -0600944 *op.fViewCountPairs[p].fProxy,
945 fDesc->fProgramInfo->pipeline());
946 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(),
947 fDesc->fVertexSpec, totQuadsSeen, quadCnt,
948 fDesc->totalNumVertices(), fDesc->fBaseVertex);
949 totQuadsSeen += quadCnt;
950 SkDEBUGCODE(++numDraws;)
951 }
952 }
953
954 SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
955 SkASSERT(numDraws == fDesc->fNumProxies);
Brian Salomon34169692017-08-28 15:32:01 -0400956 }
957
Robert Phillips6bf11252020-07-31 12:15:00 -0400958 void propagateCoverageAAThroughoutChain() {
959 fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
960
961 for (GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
962 TextureOp* tex = static_cast<TextureOp*>(tmp);
963 SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
964 tex->fMetadata.aaType() == GrAAType::kNone);
965 tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
966 }
967
968 for (GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
969 TextureOp* tex = static_cast<TextureOp*>(tmp);
970 SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
971 tex->fMetadata.aaType() == GrAAType::kNone);
972 tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
973 }
974 }
975
Herb Derbye25c3002020-10-27 15:57:27 -0400976 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400977 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips089b7c92020-08-12 11:57:07 -0400978 auto* that = t->cast<TextureOp>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400979
Robert Phillips6bf11252020-07-31 12:15:00 -0400980 SkDEBUGCODE(this->validate();)
981 SkDEBUGCODE(that->validate();)
982
Chris Daltondbb833b2020-03-17 12:15:46 -0600983 if (fDesc || that->fDesc) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400984 // This should never happen (since only DDL recorded ops should be prePrepared)
985 // but, in any case, we should never combine ops that that been prePrepared
986 return CombineResult::kCannotCombine;
987 }
988
Brian Salomon2432d062020-04-16 20:48:09 -0400989 if (fMetadata.subset() != that->fMetadata.subset()) {
990 // It is technically possible to combine operations across subset modes, but performance
Michael Ludwig2929f512019-04-19 13:05:56 -0400991 // testing suggests it's better to make more draw calls where some take advantage of
992 // the more optimal shader path without coordinate clamping.
993 return CombineResult::kCannotCombine;
994 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400995 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
996 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000997 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400998 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400999
Brian Salomonae7d7702018-10-14 15:05:45 -04001000 bool upgradeToCoverageAAOnMerge = false;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001001 if (fMetadata.aaType() != that->fMetadata.aaType()) {
1002 if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
Brian Salomonae7d7702018-10-14 15:05:45 -04001003 return CombineResult::kCannotCombine;
1004 }
1005 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -05001006 }
Robert Phillipsb69001f2019-10-29 12:16:35 -04001007
Michael Ludwigadb12e72019-12-04 16:19:18 -05001008 if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
Robert Phillipsbbd459d2019-10-29 14:40:03 -04001009 this->numChainedQuads() + that->numChainedQuads())) {
1010 return CombineResult::kCannotCombine;
Robert Phillipsb69001f2019-10-29 12:16:35 -04001011 }
1012
Michael Ludwigadb12e72019-12-04 16:19:18 -05001013 if (fMetadata.saturate() != that->fMetadata.saturate()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001014 return CombineResult::kCannotCombine;
1015 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001016 if (fMetadata.filter() != that->fMetadata.filter()) {
Brian Salomonf7232642018-09-19 08:58:08 -04001017 return CombineResult::kCannotCombine;
1018 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001019 if (fMetadata.mipmapMode() != that->fMetadata.mipmapMode()) {
1020 return CombineResult::kCannotCombine;
1021 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001022 if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
1023 return CombineResult::kCannotCombine;
1024 }
1025 const auto* thisProxy = fViewCountPairs[0].fProxy.get();
1026 const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
1027 if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
1028 thisProxy != thatProxy) {
Brian Salomon588cec72018-11-14 13:56:37 -05001029 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -05001030 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Robert Phillips6bf11252020-07-31 12:15:00 -04001031 caps.dynamicStateArrayGeometryProcessorTextureSupport() &&
1032 fMetadata.aaType() == that->fMetadata.aaType()) {
1033 // We only allow chaining when the aaTypes match bc otherwise the AA type
1034 // reported by the chain can be inconsistent. That is, since chaining doesn't
1035 // propagate revised AA information throughout the chain, the head of the chain
1036 // could have an AA setting of kNone while the chain as a whole could have a
1037 // setting of kCoverage. This inconsistency would then interfere with the validity
1038 // of the CombinedQuadCountWillOverflow calls.
1039 // This problem doesn't occur w/ merging bc we do propagate the AA information
1040 // (in propagateCoverageAAThroughoutChain) below.
Brian Salomonf7232642018-09-19 08:58:08 -04001041 return CombineResult::kMayChain;
1042 }
Brian Salomon7eae3e02018-08-07 14:02:38 +00001043 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001044 }
Michael Ludwig009b92e2019-02-15 16:03:53 -05001045
Brian Salomon2432d062020-04-16 20:48:09 -04001046 fMetadata.fSubset |= that->fMetadata.fSubset;
Brian Osman788b9162020-02-07 10:36:46 -05001047 fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
Michael Ludwig009b92e2019-02-15 16:03:53 -05001048
Michael Ludwig425eb452019-06-27 10:13:27 -04001049 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -05001050 fQuads.concat(that->fQuads);
Greg Daniel549325c2019-10-30 16:19:20 -04001051 fViewCountPairs[0].fQuadCnt += that->fQuads.count();
Michael Ludwigadb12e72019-12-04 16:19:18 -05001052 fMetadata.fTotalQuadCount += that->fQuads.count();
Michael Ludwig009b92e2019-02-15 16:03:53 -05001053
Robert Phillips6bf11252020-07-31 12:15:00 -04001054 if (upgradeToCoverageAAOnMerge) {
Robert Phillips089b7c92020-08-12 11:57:07 -04001055 // This merger may be the start of a concatenation of two chains. When one
1056 // of the chains mutates its AA the other must follow suit or else the above AA
1057 // check may prevent later ops from chaining together. A specific example of this is
1058 // when chain2 is prepended onto chain1:
1059 // chain1 (that): opA (non-AA/mergeable) opB (non-AA/non-mergeable)
1060 // chain2 (this): opC (cov-AA/non-mergeable) opD (cov-AA/mergeable)
1061 // W/o this propagation, after opD & opA merge, opB and opC would say they couldn't
1062 // chain - which would stop the concatenation process.
Robert Phillips6bf11252020-07-31 12:15:00 -04001063 this->propagateCoverageAAThroughoutChain();
Robert Phillips089b7c92020-08-12 11:57:07 -04001064 that->propagateCoverageAAThroughoutChain();
Robert Phillips6bf11252020-07-31 12:15:00 -04001065 }
1066
1067 SkDEBUGCODE(this->validate();)
1068
Brian Salomon7eae3e02018-08-07 14:02:38 +00001069 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -04001070 }
John Stilesaf366522020-08-13 09:57:34 -04001071
1072#if GR_TEST_UTILS
1073 SkString onDumpInfo() const override {
1074 SkString str = SkStringPrintf("# draws: %d\n", fQuads.count());
1075 auto iter = fQuads.iterator();
1076 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Robert Phillips047d5bb2021-01-08 13:39:19 -05001077 SkString proxyStr = fViewCountPairs[p].fProxy->dump();
1078 str.append(proxyStr);
1079 str.appendf(", Filter: %d, MM: %d\n",
John Stilesaf366522020-08-13 09:57:34 -04001080 static_cast<int>(fMetadata.fFilter),
1081 static_cast<int>(fMetadata.fMipmapMode));
Robert Phillips047d5bb2021-01-08 13:39:19 -05001082 for (int i = 0; i < fViewCountPairs[p].fQuadCnt && iter.next(); ++i) {
John Stilesaf366522020-08-13 09:57:34 -04001083 const GrQuad* quad = iter.deviceQuad();
1084 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
1085 const ColorSubsetAndAA& info = iter.metadata();
1086 str.appendf(
1087 "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
1088 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
1089 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
1090 i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
1091 info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
1092 quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
1093 quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
1094 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
1095 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
John Stilesaf366522020-08-13 09:57:34 -04001096 }
1097 }
1098 return str;
1099 }
1100#endif
1101
Brian Salomon2432d062020-04-16 20:48:09 -04001102 GrQuadBuffer<ColorSubsetAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -04001103 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Chris Daltondbb833b2020-03-17 12:15:46 -06001104 // Most state of TextureOp is packed into these two field to minimize the op's size.
Michael Ludwigadb12e72019-12-04 16:19:18 -05001105 // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1106 // consider/measure changes with care.
Chris Daltondbb833b2020-03-17 12:15:46 -06001107 Desc* fDesc;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001108 Metadata fMetadata;
Robert Phillips32803ff2019-10-23 08:26:08 -04001109
1110 // This field must go last. When allocating this op, we will allocate extra space to hold
Greg Daniel549325c2019-10-30 16:19:20 -04001111 // additional ViewCountPairs immediately after the op's allocation so we can treat this
Robert Phillips32803ff2019-10-23 08:26:08 -04001112 // as an fProxyCnt-length array.
Greg Daniel549325c2019-10-30 16:19:20 -04001113 ViewCountPair fViewCountPairs[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -04001114
John Stiles7571f9e2020-09-02 22:42:33 -04001115 using INHERITED = GrMeshDrawOp;
Brian Salomon34169692017-08-28 15:32:01 -04001116};
1117
1118} // anonymous namespace
1119
Robert Phillipse837e612019-11-15 11:02:50 -05001120#if GR_TEST_UTILS
1121uint32_t GrTextureOp::ClassID() {
1122 return TextureOp::ClassID();
1123}
1124#endif
Brian Salomon34169692017-08-28 15:32:01 -04001125
Herb Derbyc76d4092020-10-07 16:46:15 -04001126GrOp::Owner GrTextureOp::Make(GrRecordingContext* context,
1127 GrSurfaceProxyView proxyView,
1128 SkAlphaType alphaType,
1129 sk_sp<GrColorSpaceXform> textureXform,
1130 GrSamplerState::Filter filter,
1131 GrSamplerState::MipmapMode mm,
1132 const SkPMColor4f& color,
1133 Saturate saturate,
1134 SkBlendMode blendMode,
1135 GrAAType aaType,
1136 DrawQuad* quad,
1137 const SkRect* subset) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001138 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
Brian Salomon2432d062020-04-16 20:48:09 -04001139 if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1140 // No need for a shader-based subset if hardware clamping achieves the same effect
1141 subset = nullptr;
Michael Ludwig22429f92019-06-27 10:44:48 -04001142 }
1143
Brian Salomone69b9ef2020-07-22 11:18:06 -04001144 if (filter != GrSamplerState::Filter::kNearest || mm != GrSamplerState::MipmapMode::kNone) {
1145 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad->fLocal, quad->fDevice);
1146 if (!mustFilter) {
1147 filter = GrSamplerState::Filter::kNearest;
1148 }
1149 if (!mustMM) {
1150 mm = GrSamplerState::MipmapMode::kNone;
1151 }
Michael Ludwig22429f92019-06-27 10:44:48 -04001152 }
1153
1154 if (blendMode == SkBlendMode::kSrcOver) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001155 return TextureOp::Make(context, std::move(proxyView), std::move(textureXform), filter, mm,
Brian Salomon2432d062020-04-16 20:48:09 -04001156 color, saturate, aaType, std::move(quad), subset);
Michael Ludwig22429f92019-06-27 10:44:48 -04001157 } else {
1158 // Emulate complex blending using GrFillRectOp
Brian Salomonb030b1f2020-11-23 15:40:55 -05001159 GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, filter, mm);
Michael Ludwig22429f92019-06-27 10:44:48 -04001160 GrPaint paint;
1161 paint.setColor4f(color);
1162 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1163
1164 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomonb030b1f2020-11-23 15:40:55 -05001165 const auto& caps = *context->priv().caps();
Brian Salomon2432d062020-04-16 20:48:09 -04001166 if (subset) {
Brian Salomonca6b2f42020-01-24 11:31:21 -05001167 SkRect localRect;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001168 if (quad->fLocal.asRect(&localRect)) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001169 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
Brian Salomonb030b1f2020-11-23 15:40:55 -05001170 samplerState, *subset, localRect, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001171 } else {
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, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001174 }
1175 } else {
Brian Salomonb030b1f2020-11-23 15:40:55 -05001176 fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), samplerState,
1177 caps);
Michael Ludwig22429f92019-06-27 10:44:48 -04001178 }
1179 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
Brian Salomon4b6e2f02021-07-08 14:04:21 -04001180 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001181 if (saturate == GrTextureOp::Saturate::kYes) {
Brian Osman8e814b32021-06-17 14:14:26 -04001182 fp = GrFragmentProcessor::ClampOutput(std::move(fp));
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001183 }
John Stiles5933d7d2020-07-21 12:28:35 -04001184 paint.setColorFragmentProcessor(std::move(fp));
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001185 return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
Michael Ludwig22429f92019-06-27 10:44:48 -04001186 }
1187}
1188
Robert Phillips46eb3ab2021-08-02 17:09:01 -04001189#if SK_GPU_V1
1190
Robert Phillipse837e612019-11-15 11:02:50 -05001191// A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1192class GrTextureOp::BatchSizeLimiter {
1193public:
Robert Phillips4dca8312021-07-28 15:13:20 -04001194 BatchSizeLimiter(skgpu::v1::SurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001195 const GrClip* clip,
Robert Phillips4dca8312021-07-28 15:13:20 -04001196 GrRecordingContext* rContext,
Robert Phillipse837e612019-11-15 11:02:50 -05001197 int numEntries,
1198 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001199 GrSamplerState::MipmapMode mm,
Robert Phillipse837e612019-11-15 11:02:50 -05001200 GrTextureOp::Saturate saturate,
1201 SkCanvas::SrcRectConstraint constraint,
1202 const SkMatrix& viewMatrix,
1203 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
John Stiles0fbc6a32021-06-04 14:40:57 -04001204 : fSDC(sdc)
Robert Phillipse837e612019-11-15 11:02:50 -05001205 , fClip(clip)
Robert Phillips4dca8312021-07-28 15:13:20 -04001206 , fContext(rContext)
Robert Phillipse837e612019-11-15 11:02:50 -05001207 , fFilter(filter)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001208 , fMipmapMode(mm)
Robert Phillipse837e612019-11-15 11:02:50 -05001209 , fSaturate(saturate)
1210 , fConstraint(constraint)
1211 , fViewMatrix(viewMatrix)
1212 , fTextureColorSpaceXform(textureColorSpaceXform)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001213 , fNumLeft(numEntries) {}
Brian Salomon34169692017-08-28 15:32:01 -04001214
Robert Phillipse40495d2021-07-20 09:40:13 -04001215 void createOp(GrTextureSetEntry set[], int clumpSize, GrAAType aaType) {
1216
Michael Ludwig379e4962019-12-06 13:21:26 -05001217 int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
Herb Derbyc76d4092020-10-07 16:46:15 -04001218 GrOp::Owner op = TextureOp::Make(fContext,
1219 &set[fNumClumped],
1220 clumpSize,
1221 clumpProxyCount,
1222 fFilter,
1223 fMipmapMode,
1224 fSaturate,
1225 aaType,
1226 fConstraint,
1227 fViewMatrix,
1228 fTextureColorSpaceXform);
John Stiles0fbc6a32021-06-04 14:40:57 -04001229 fSDC->addDrawOp(fClip, std::move(op));
Robert Phillipse837e612019-11-15 11:02:50 -05001230
1231 fNumLeft -= clumpSize;
1232 fNumClumped += clumpSize;
1233 }
1234
1235 int numLeft() const { return fNumLeft; }
1236 int baseIndex() const { return fNumClumped; }
1237
1238private:
Robert Phillips4dca8312021-07-28 15:13:20 -04001239 skgpu::v1::SurfaceDrawContext* fSDC;
1240 const GrClip* fClip;
1241 GrRecordingContext* fContext;
1242 GrSamplerState::Filter fFilter;
1243 GrSamplerState::MipmapMode fMipmapMode;
1244 GrTextureOp::Saturate fSaturate;
1245 SkCanvas::SrcRectConstraint fConstraint;
1246 const SkMatrix& fViewMatrix;
1247 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Robert Phillipse837e612019-11-15 11:02:50 -05001248
Robert Phillips4dca8312021-07-28 15:13:20 -04001249 int fNumLeft;
1250 int fNumClumped = 0; // also the offset for the start of the next clump
Robert Phillipse837e612019-11-15 11:02:50 -05001251};
1252
1253// Greedily clump quad draws together until the index buffer limit is exceeded.
Robert Phillips4dca8312021-07-28 15:13:20 -04001254void GrTextureOp::AddTextureSetOps(skgpu::v1::SurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001255 const GrClip* clip,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001256 GrRecordingContext* context,
Robert Phillipse40495d2021-07-20 09:40:13 -04001257 GrTextureSetEntry set[],
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001258 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -05001259 int proxyRunCnt,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001260 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001261 GrSamplerState::MipmapMode mm,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001262 Saturate saturate,
1263 SkBlendMode blendMode,
1264 GrAAType aaType,
1265 SkCanvas::SrcRectConstraint constraint,
1266 const SkMatrix& viewMatrix,
1267 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001268 // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1269 // the op's metadata so we don't need to worry about overflow.
Michael Ludwig4ef1ca12019-12-19 10:58:52 -05001270 SkDEBUGCODE(TextureOp::ValidateResourceLimits();)
Michael Ludwig379e4962019-12-06 13:21:26 -05001271 SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1272
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001273 // First check if we can support batches as a single op
1274 if (blendMode != SkBlendMode::kSrcOver ||
1275 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1276 // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1277 // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1278 // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1279 SkMatrix ctm;
1280 for (int i = 0; i < cnt; ++i) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001281 ctm = viewMatrix;
1282 if (set[i].fPreViewMatrix) {
1283 ctm.preConcat(*set[i].fPreViewMatrix);
1284 }
Robert Phillipse837e612019-11-15 11:02:50 -05001285
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001286 DrawQuad quad;
1287 quad.fEdgeFlags = set[i].fAAFlags;
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001288 if (set[i].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001289 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001290
1291 SkPoint srcPts[4];
1292 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001293 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001294 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001295 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1296 quad.fLocal = GrQuad(set[i].fSrcRect);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001297 }
1298
Brian Salomon2432d062020-04-16 20:48:09 -04001299 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001300 ? &set[i].fSrcRect : nullptr;
1301
Brian Salomonfc118442019-11-22 19:09:27 -05001302 auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001303 filter, mm, set[i].fColor, saturate, blendMode, aaType, &quad, subset);
Robert Phillips4dca8312021-07-28 15:13:20 -04001304 sdc->addDrawOp(clip, std::move(op));
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001305 }
1306 return;
1307 }
1308
1309 // Second check if we can always just make a single op and avoid the extra iteration
Robert Phillipse837e612019-11-15 11:02:50 -05001310 // needed to clump things together.
Brian Osman788b9162020-02-07 10:36:46 -05001311 if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
Robert Phillipse837e612019-11-15 11:02:50 -05001312 GrResourceProvider::MaxNumAAQuads())) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001313 auto op = TextureOp::Make(context, set, cnt, proxyRunCnt, filter, mm, saturate, aaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001314 constraint, viewMatrix, std::move(textureColorSpaceXform));
Robert Phillips4dca8312021-07-28 15:13:20 -04001315 sdc->addDrawOp(clip, std::move(op));
Robert Phillipse837e612019-11-15 11:02:50 -05001316 return;
1317 }
1318
Robert Phillips4dca8312021-07-28 15:13:20 -04001319 BatchSizeLimiter state(sdc, clip, context, cnt, filter, mm, saturate, constraint, viewMatrix,
Robert Phillipse837e612019-11-15 11:02:50 -05001320 std::move(textureColorSpaceXform));
1321
1322 // kNone and kMSAA never get altered
1323 if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1324 // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1325 while (state.numLeft() > 0) {
Brian Osman788b9162020-02-07 10:36:46 -05001326 int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
Robert Phillipse837e612019-11-15 11:02:50 -05001327
1328 state.createOp(set, clumpSize, aaType);
1329 }
1330 } else {
1331 // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1332 // can also get downgraded to kNone if all the quads are on integer coordinates and
1333 // axis-aligned.
1334 SkASSERT(aaType == GrAAType::kCoverage);
1335
1336 while (state.numLeft() > 0) {
1337 GrAAType runningAA = GrAAType::kNone;
1338 bool clumped = false;
1339
1340 for (int i = 0; i < state.numLeft(); ++i) {
1341 int absIndex = state.baseIndex() + i;
1342
Robert Phillips6bf11252020-07-31 12:15:00 -04001343 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone ||
1344 runningAA == GrAAType::kCoverage) {
Robert Phillipse837e612019-11-15 11:02:50 -05001345
1346 if (i >= GrResourceProvider::MaxNumAAQuads()) {
1347 // Here we either need to boost the AA type to kCoverage, but doing so with
1348 // all the accumulated quads would overflow, or we have a set of AA quads
1349 // that has just gotten too large. In either case, calve off the existing
1350 // quads as their own TextureOp.
1351 state.createOp(
1352 set,
1353 runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1354 runningAA); // maybe downgrading AA here
1355 clumped = true;
1356 break;
1357 }
1358
1359 runningAA = GrAAType::kCoverage;
1360 } else if (runningAA == GrAAType::kNone) {
1361
1362 if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1363 // Here we've found a consistent batch of non-AA quads that has gotten too
1364 // large. Calve it off as its own GrTextureOp.
1365 state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1366 GrAAType::kNone); // definitely downgrading AA here
1367 clumped = true;
1368 break;
1369 }
1370 }
1371 }
1372
1373 if (!clumped) {
1374 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1375 // quads and call it a day.
1376 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1377 }
1378 }
1379 }
1380}
Robert Phillips46eb3ab2021-08-02 17:09:01 -04001381#endif
Robert Phillipsae01f622019-11-13 15:56:31 +00001382
Brian Salomon34169692017-08-28 15:32:01 -04001383#if GR_TEST_UTILS
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04001384#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001385#include "src/gpu/GrProxyProvider.h"
1386#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -04001387
1388GR_DRAW_OP_TEST_DEFINE(TextureOp) {
Brian Salomona56a7462020-02-07 14:17:25 -05001389 SkISize dims;
1390 dims.fHeight = random->nextULessThan(90) + 10;
1391 dims.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001392 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001393 GrMipmapped mipMapped = random->nextBool() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel09c94002018-06-08 22:11:51 +00001394 SkBackingFit fit = SkBackingFit::kExact;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001395 if (mipMapped == GrMipmapped::kNo) {
Greg Daniel09c94002018-06-08 22:11:51 +00001396 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1397 }
Greg Daniel4065d452018-11-16 15:43:41 -05001398 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -04001399 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1400 GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -05001401 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -04001402 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001403 format, dims, GrRenderable::kNo, 1, mipMapped, fit, SkBudgeted::kNo, GrProtected::kNo,
1404 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001405
Brian Salomon34169692017-08-28 15:32:01 -04001406 SkRect rect = GrTest::TestRect(random);
1407 SkRect srcRect;
1408 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1409 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1410 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1411 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1412 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -05001413 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001414 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
Brian Salomone69b9ef2020-07-22 11:18:06 -04001415 static_cast<uint32_t>(GrSamplerState::Filter::kLast) + 1);
1416 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
1417 if (mipMapped == GrMipmapped::kYes) {
1418 mm = (GrSamplerState::MipmapMode)random->nextULessThan(
1419 static_cast<uint32_t>(GrSamplerState::MipmapMode::kLast) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +00001420 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001421
Brian Osman3ebd3542018-07-30 14:36:53 -04001422 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001423 GrAAType aaType = GrAAType::kNone;
1424 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06001425 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -05001426 }
Brian Salomon2213ee92018-10-02 10:44:21 -04001427 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1428 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1429 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1430 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1431 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomon2432d062020-04-16 20:48:09 -04001432 bool useSubset = random->nextBool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001433 auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
Greg Daniel549325c2019-10-30 16:19:20 -04001434 GrSurfaceProxyView proxyView(
1435 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -05001436 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Brian Salomonfc118442019-11-22 19:09:27 -05001437 auto alphaType = static_cast<SkAlphaType>(
1438 random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
Greg Daniel549325c2019-10-30 16:19:20 -04001439
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001440 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
Brian Salomonfc118442019-11-22 19:09:27 -05001441 return GrTextureOp::Make(context, std::move(proxyView), alphaType, std::move(texXform), filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001442 mm, color, saturate, SkBlendMode::kSrcOver, aaType, &quad,
1443 useSubset ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -04001444}
1445
1446#endif