blob: b3b32b774b7002e3203e5ab699c0f4d762756bc8 [file] [log] [blame]
Brian Salomon34169692017-08-28 15:32:01 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomond7065e72018-10-12 11:42:02 -04008#include <new>
Brian Salomonf19f9ca2019-09-18 15:54:26 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPoint.h"
11#include "include/core/SkPoint3.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrRecordingContext.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040013#include "include/private/SkFloatingPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkTo.h"
15#include "src/core/SkMathPriv.h"
16#include "src/core/SkMatrixPriv.h"
17#include "src/core/SkRectPriv.h"
18#include "src/gpu/GrAppliedClip.h"
19#include "src/gpu/GrCaps.h"
20#include "src/gpu/GrDrawOpTest.h"
21#include "src/gpu/GrGeometryProcessor.h"
22#include "src/gpu/GrGpu.h"
23#include "src/gpu/GrMemoryPool.h"
24#include "src/gpu/GrOpFlushState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrRecordingContextPriv.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/GrResourceProviderPriv.h"
28#include "src/gpu/GrShaderCaps.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000029#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrTexturePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/SkGr.h"
Brian Osmanf48f76e2020-07-15 16:04:17 -040033#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
Brian Osman6f5e9402020-01-22 10:39:31 -050034#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040035#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040036#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040037#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/glsl/GrGLSLVarying.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040039#include "src/gpu/ops/GrFillRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/ops/GrMeshDrawOp.h"
41#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050042#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040043#include "src/gpu/ops/GrTextureOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040044
45namespace {
46
Brian Salomon2432d062020-04-16 20:48:09 -040047using Subset = GrQuadPerEdgeAA::Subset;
Michael Ludwigc182b942018-11-16 10:27:51 -050048using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050049using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040050
Michael Ludwig22429f92019-06-27 10:44:48 -040051// Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
52// between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
53static SkSize axis_aligned_quad_size(const GrQuad& quad) {
54 SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
55 // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
56 float dw = sk_float_abs(quad.x(2) - quad.x(0)) + sk_float_abs(quad.y(2) - quad.y(0));
57 float dh = sk_float_abs(quad.x(1) - quad.x(0)) + sk_float_abs(quad.y(1) - quad.y(0));
58 return {dw, dh};
59}
60
61static bool filter_has_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
62 // If not axis-aligned in src or dst, then always say it has an effect
63 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
64 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
65 return true;
66 }
67
68 SkRect srcRect;
69 SkRect dstRect;
70 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
71 // Disable filtering when there is no scaling (width and height are the same), and the
72 // top-left corners have the same fraction (so src and dst snap to the pixel grid
73 // identically).
74 SkASSERT(srcRect.isSorted());
75 return srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
76 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
77 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
78 } else {
79 // Although the quads are axis-aligned, the local coordinate system is transformed such
80 // that fractionally-aligned sample centers will not align with the device coordinate system
81 // So disable filtering when edges are the same length and both srcQuad and dstQuad
82 // 0th vertex is integer aligned.
83 if (SkScalarIsInt(srcQuad.x(0)) && SkScalarIsInt(srcQuad.y(0)) &&
84 SkScalarIsInt(dstQuad.x(0)) && SkScalarIsInt(dstQuad.y(0))) {
85 // Extract edge lengths
86 SkSize srcSize = axis_aligned_quad_size(srcQuad);
87 SkSize dstSize = axis_aligned_quad_size(dstQuad);
88 return srcSize.fWidth != dstSize.fWidth || srcSize.fHeight != dstSize.fHeight;
89 } else {
90 return true;
91 }
92 }
93}
94
Michael Ludwig119ac6d2019-11-21 09:26:46 -050095// Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
96// regular and rectangular textures, w/ or w/o origin correction.
97struct NormalizationParams {
98 float fIW; // 1 / width of texture, or 1.0 for texture rectangles
Michael Ludwigc453a502020-05-29 12:29:12 -040099 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 -0500100 float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
101};
Michael Ludwigadb12e72019-12-04 16:19:18 -0500102static NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
103 GrSurfaceOrigin origin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500104 // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
105 // normalize the src coordinates up front.
Michael Ludwigadb12e72019-12-04 16:19:18 -0500106 SkISize dimensions = proxy->backingStoreDimensions();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500107 float iw, ih, h;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500108 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500109 iw = ih = 1.f;
110 h = dimensions.height();
111 } else {
112 iw = 1.f / dimensions.width();
113 ih = 1.f / dimensions.height();
114 h = 1.f;
115 }
116
Michael Ludwigadb12e72019-12-04 16:19:18 -0500117 if (origin == kBottomLeft_GrSurfaceOrigin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500118 return {iw, -ih, h};
119 } else {
120 return {iw, ih, 0.0f};
121 }
122}
123
Brian Salomon2432d062020-04-16 20:48:09 -0400124// Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500125// 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 -0400126// subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
127// and y-flipping are applied as indicated by NormalizationParams.
128static SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
129 const NormalizationParams& params,
130 const SkRect* subsetRect) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500131 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Brian Salomon2432d062020-04-16 20:48:09 -0400132 if (!subsetRect) {
133 // Either the quad has no subset constraint and is batched with a subset constrained op
134 // (in which case we want a subset that doesn't restrict normalized tex coords), or the
135 // entire op doesn't use the subset, in which case the returned value is ignored.
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500136 return kLargeRect;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400137 }
138
Brian Salomon2432d062020-04-16 20:48:09 -0400139 auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400140 auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
141 if (filter == GrSamplerState::Filter::kNearest) {
142 // Make sure our insetting puts us at pixel centers.
143 ltrb = skvx::floor(ltrb*flipHi)*flipHi;
144 }
145 // Inset with pin to the rect center.
146 ltrb += skvx::Vec<4, float>({.5f, .5f, -.5f, -.5f});
147 auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
148 ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
149
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500150 // Normalize and offset
Michael Ludwigc453a502020-05-29 12:29:12 -0400151 ltrb = mad(ltrb, {params.fIW, params.fInvH, params.fIW, params.fInvH},
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500152 {0.f, params.fYOffset, 0.f, params.fYOffset});
Michael Ludwigc453a502020-05-29 12:29:12 -0400153 if (params.fInvH < 0.f) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500154 // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
155 ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400156 }
157
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500158 SkRect out;
159 ltrb.store(&out);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500160 return out;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400161}
162
Michael Ludwig009b92e2019-02-15 16:03:53 -0500163// Normalizes logical src coords and corrects for origin
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500164static void normalize_src_quad(const NormalizationParams& params,
165 GrQuad* srcQuad) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500166 // The src quad should not have any perspective
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500167 SkASSERT(!srcQuad->hasPerspective());
168 skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
Michael Ludwigc453a502020-05-29 12:29:12 -0400169 skvx::Vec<4, float> ys = mad(srcQuad->y4f(), params.fInvH, params.fYOffset);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500170 xs.store(srcQuad->xs());
171 ys.store(srcQuad->ys());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500172}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400173
Michael Ludwig379e4962019-12-06 13:21:26 -0500174// Count the number of proxy runs in the entry set. This usually is already computed by
175// SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
176// for each split.
177static int proxy_run_count(const GrRenderTargetContext::TextureSetEntry set[], int count) {
178 int actualProxyRunCount = 0;
179 const GrSurfaceProxy* lastProxy = nullptr;
180 for (int i = 0; i < count; ++i) {
181 if (set[i].fProxyView.proxy() != lastProxy) {
182 actualProxyRunCount++;
183 lastProxy = set[i].fProxyView.proxy();
184 }
185 }
186 return actualProxyRunCount;
187}
188
John Stilescbe4e282020-06-01 10:38:31 -0400189static bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
190 const DrawQuad& quad, const SkRect& subsetRect) {
191 // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
192 // can push all the way up to the edges of the the subset rect and the sampler shouldn't
193 // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
194 // the non-antialiased case.
195 SkRect localBounds = quad.fLocal.bounds();
196 if (aaType == GrAAType::kNone &&
197 filter == GrSamplerState::Filter::kNearest &&
198 quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
199 quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
200 subsetRect.contains(localBounds)) {
201
202 return true;
203 }
204
205 // If the subset rect is inset by at least 0.5 pixels into the local quad's bounds, the
206 // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
207 if (subsetRect.makeInset(0.5f, 0.5f).contains(localBounds)) {
208 return true;
209 }
210
211 // The subset rect cannot be ignored safely.
212 return false;
213}
214
Brian Salomon34169692017-08-28 15:32:01 -0400215/**
216 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
217 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
218 */
219class TextureOp final : public GrMeshDrawOp {
220public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500221 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Greg Daniel549325c2019-10-30 16:19:20 -0400222 GrSurfaceProxyView proxyView,
Michael Ludwig22429f92019-06-27 10:44:48 -0400223 sk_sp<GrColorSpaceXform> textureXform,
Robert Phillips7c525e62018-06-12 10:11:12 -0400224 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500225 const SkPMColor4f& color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400226 GrTextureOp::Saturate saturate,
Robert Phillips7c525e62018-06-12 10:11:12 -0400227 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500228 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400229 const SkRect* subset) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500230 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Greg Daniel549325c2019-10-30 16:19:20 -0400231 return pool->allocate<TextureOp>(std::move(proxyView), std::move(textureXform), filter,
Brian Salomon2432d062020-04-16 20:48:09 -0400232 color, saturate, aaType, quad, subset);
Brian Salomon34169692017-08-28 15:32:01 -0400233 }
Robert Phillipse837e612019-11-15 11:02:50 -0500234
Robert Phillipsb97da532019-02-12 15:24:12 -0500235 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwigadb12e72019-12-04 16:19:18 -0500236 GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400237 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500238 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400239 GrSamplerState::Filter filter,
240 GrTextureOp::Saturate saturate,
241 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400242 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500243 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500244 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500245 // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
246 SkASSERT(proxyRunCnt <= cnt);
247
248 size_t size = sizeof(TextureOp) + sizeof(ViewCountPair) * (proxyRunCnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500249 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400250 void* mem = pool->allocate(size);
Michael Ludwig379e4962019-12-06 13:21:26 -0500251 return std::unique_ptr<GrDrawOp>(
252 new (mem) TextureOp(set, cnt, proxyRunCnt, filter, saturate, aaType, constraint,
253 viewMatrix, std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400254 }
Brian Salomon34169692017-08-28 15:32:01 -0400255
Brian Salomon336ce7b2017-09-08 08:23:58 -0400256 ~TextureOp() override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500257 for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400258 fViewCountPairs[p].~ViewCountPair();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400259 }
260 }
Brian Salomon34169692017-08-28 15:32:01 -0400261
262 const char* name() const override { return "TextureOp"; }
263
Chris Dalton1706cbf2019-05-21 19:35:29 -0600264 void visitProxies(const VisitProxyFunc& func) const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500265 bool mipped = (GrSamplerState::Filter::kMipMap == fMetadata.filter());
266 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400267 func(fViewCountPairs[p].fProxy.get(), GrMipmapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400268 }
Chris Daltondbb833b2020-03-17 12:15:46 -0600269 if (fDesc && fDesc->fProgramInfo) {
270 fDesc->fProgramInfo->visitFPProxies(func);
271 }
Brian Salomond7065e72018-10-12 11:42:02 -0400272 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400273
Brian Osman9a390ac2018-11-12 09:47:48 -0500274#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400275 SkString dumpInfo() const override {
276 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400277 str.appendf("# draws: %d\n", fQuads.count());
Michael Ludwig425eb452019-06-27 10:13:27 -0400278 auto iter = fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500279 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Robert Phillips32803ff2019-10-23 08:26:08 -0400280 str.appendf("Proxy ID: %d, Filter: %d\n",
Michael Ludwigadb12e72019-12-04 16:19:18 -0500281 fViewCountPairs[p].fProxy->uniqueID().asUInt(),
282 static_cast<int>(fMetadata.fFilter));
Michael Ludwig425eb452019-06-27 10:13:27 -0400283 int i = 0;
Greg Daniel549325c2019-10-30 16:19:20 -0400284 while(i < fViewCountPairs[p].fQuadCnt && iter.next()) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500285 const GrQuad* quad = iter.deviceQuad();
286 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
Brian Salomon2432d062020-04-16 20:48:09 -0400287 const ColorSubsetAndAA& info = iter.metadata();
Brian Salomond7065e72018-10-12 11:42:02 -0400288 str.appendf(
Brian Salomon2432d062020-04-16 20:48:09 -0400289 "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400290 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
291 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
Brian Salomon2432d062020-04-16 20:48:09 -0400292 i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
293 info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
Michael Ludwig704d5402019-11-25 09:43:37 -0500294 quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
295 quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400296 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
297 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
298
Michael Ludwig425eb452019-06-27 10:13:27 -0400299 i++;
Brian Salomond7065e72018-10-12 11:42:02 -0400300 }
Brian Salomon34169692017-08-28 15:32:01 -0400301 }
302 str += INHERITED::dumpInfo();
303 return str;
304 }
Michael Ludwig4ef1ca12019-12-19 10:58:52 -0500305
306 static void ValidateResourceLimits() {
307 // The op implementation has an upper bound on the number of quads that it can represent.
308 // However, the resource manager imposes its own limit on the number of quads, which should
309 // always be lower than the numerical limit this op can hold.
310 using CountStorage = decltype(Metadata::fTotalQuadCount);
311 CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
312 // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
313 int resourceLimit = SkTo<int>(maxQuadCount);
314 SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
315 GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
316 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500317#endif
Brian Salomon34169692017-08-28 15:32:01 -0400318
Brian Osman5ced0bf2019-03-15 10:15:29 -0400319 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600320 const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage,
321 GrClampType clampType) override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500322 SkASSERT(fMetadata.colorType() == ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400323 auto iter = fQuads.metadata();
324 while(iter.next()) {
Brian Osman2715bf52019-12-06 14:38:47 -0500325 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
Brian Osman788b9162020-02-07 10:36:46 -0500326 fMetadata.fColorType = std::max(fMetadata.fColorType, static_cast<uint16_t>(colorType));
Brian Osman8fa7ab42019-03-18 10:22:42 -0400327 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700328 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400329 }
330
Brian Salomon485b8c62018-01-12 15:11:06 -0500331 FixedFunctionFlags fixedFunctionFlags() const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500332 return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
333 : FixedFunctionFlags::kNone;
Brian Salomon485b8c62018-01-12 15:11:06 -0500334 }
Brian Salomon34169692017-08-28 15:32:01 -0400335
336 DEFINE_OP_CLASS_ID
337
338private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400339 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500340
Brian Salomon2432d062020-04-16 20:48:09 -0400341 struct ColorSubsetAndAA {
342 ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400343 : fColor(color)
Brian Salomon2432d062020-04-16 20:48:09 -0400344 , fSubsetRect(subsetRect)
Michael Ludwig4384f042019-12-05 10:30:35 -0500345 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
346 SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
Michael Ludwig425eb452019-06-27 10:13:27 -0400347 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400348
349 SkPMColor4f fColor;
Brian Salomon2432d062020-04-16 20:48:09 -0400350 // 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 -0500351 // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
Brian Salomon2432d062020-04-16 20:48:09 -0400352 SkRect fSubsetRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400353 unsigned fAAFlags : 4;
354
Michael Ludwig425eb452019-06-27 10:13:27 -0400355 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
356 };
Michael Ludwigadb12e72019-12-04 16:19:18 -0500357
Greg Daniel549325c2019-10-30 16:19:20 -0400358 struct ViewCountPair {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500359 // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
360 // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
361 // swizzle so that is stored in the op metadata.
362 sk_sp<GrSurfaceProxy> fProxy;
Michael Ludwig425eb452019-06-27 10:13:27 -0400363 int fQuadCnt;
364 };
365
Michael Ludwigadb12e72019-12-04 16:19:18 -0500366 // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
367 // increase the size of the op; increasing the op size can have a surprising impact on
368 // performance (since texture ops are one of the most commonly used in an app).
369 struct Metadata {
370 // AAType must be filled after initialization; ColorType is determined in finalize()
371 Metadata(const GrSwizzle& swizzle, GrSamplerState::Filter filter,
Brian Salomon2432d062020-04-16 20:48:09 -0400372 GrQuadPerEdgeAA::Subset subset, GrTextureOp::Saturate saturate)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500373 : fSwizzle(swizzle)
374 , fProxyCount(1)
375 , fTotalQuadCount(1)
Michael Ludwig4384f042019-12-05 10:30:35 -0500376 , fFilter(static_cast<uint16_t>(filter))
377 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
378 , fColorType(static_cast<uint16_t>(ColorType::kNone))
Brian Salomon2432d062020-04-16 20:48:09 -0400379 , fSubset(static_cast<uint16_t>(subset))
Michael Ludwig4384f042019-12-05 10:30:35 -0500380 , fSaturate(static_cast<uint16_t>(saturate)) {}
Michael Ludwigadb12e72019-12-04 16:19:18 -0500381
Michael Ludwig4384f042019-12-05 10:30:35 -0500382 GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
Michael Ludwigadb12e72019-12-04 16:19:18 -0500383 uint16_t fProxyCount;
384 // This will be >= fProxyCount, since a proxy may be drawn multiple times
385 uint16_t fTotalQuadCount;
386
Michael Ludwig4384f042019-12-05 10:30:35 -0500387 // These must be based on uint16_t to help MSVC's pack bitfields optimally
388 uint16_t fFilter : 2; // GrSamplerState::Filter
389 uint16_t fAAType : 2; // GrAAType
390 uint16_t fColorType : 2; // GrQuadPerEdgeAA::ColorType
Brian Salomon2432d062020-04-16 20:48:09 -0400391 uint16_t fSubset : 1; // bool
Michael Ludwig4384f042019-12-05 10:30:35 -0500392 uint16_t fSaturate : 1; // bool
393 uint16_t fUnused : 8; // # of bits left before Metadata exceeds 8 bytes
Michael Ludwigadb12e72019-12-04 16:19:18 -0500394
395 GrSamplerState::Filter filter() const {
396 return static_cast<GrSamplerState::Filter>(fFilter);
397 }
398 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
399 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
Brian Salomon2432d062020-04-16 20:48:09 -0400400 Subset subset() const { return static_cast<Subset>(fSubset); }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500401 GrTextureOp::Saturate saturate() const {
402 return static_cast<GrTextureOp::Saturate>(fSaturate);
403 }
404
405 static_assert(GrSamplerState::kFilterCount <= 4);
406 static_assert(kGrAATypeCount <= 4);
407 static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
408 };
Michael Ludwig4384f042019-12-05 10:30:35 -0500409 static_assert(sizeof(Metadata) == 8);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500410
Chris Daltondbb833b2020-03-17 12:15:46 -0600411 // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
412 // store the data in a separate struct in order to minimize the size of the TextureOp.
413 // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
414 // may want to re-evaluate whether this is still necessary.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400415 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600416 // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
417 // allocatePrePreparedVertices is also called.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400418 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600419 // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
420 // part of the flushState).
421 struct Desc {
422 VertexSpec fVertexSpec;
423 int fNumProxies = 0;
424 int fNumTotalQuads = 0;
Robert Phillips32803ff2019-10-23 08:26:08 -0400425
Chris Daltondbb833b2020-03-17 12:15:46 -0600426 // This member variable is only used by 'onPrePrepareDraws'.
427 char* fPrePreparedVertices = nullptr;
428
429 GrProgramInfo* fProgramInfo = nullptr;
430
431 sk_sp<const GrBuffer> fIndexBuffer;
432 sk_sp<const GrBuffer> fVertexBuffer;
433 int fBaseVertex;
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400434
435 // How big should 'fVertices' be to hold all the vertex data?
436 size_t totalSizeInBytes() const {
Chris Daltondbb833b2020-03-17 12:15:46 -0600437 return this->totalNumVertices() * fVertexSpec.vertexSize();
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400438 }
439
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400440 int totalNumVertices() const {
441 return fNumTotalQuads * fVertexSpec.verticesPerQuad();
442 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400443
Chris Daltondbb833b2020-03-17 12:15:46 -0600444 void allocatePrePreparedVertices(SkArenaAlloc* arena) {
445 fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400446 }
Robert Phillips32803ff2019-10-23 08:26:08 -0400447 };
Brian Salomon2432d062020-04-16 20:48:09 -0400448 // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
Greg Daniel549325c2019-10-30 16:19:20 -0400449 TextureOp(GrSurfaceProxyView proxyView,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400450 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
451 GrSamplerState::Filter filter,
452 const SkPMColor4f& color,
453 GrTextureOp::Saturate saturate,
454 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500455 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400456 const SkRect* subsetRect)
Brian Salomon34169692017-08-28 15:32:01 -0400457 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400458 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400459 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600460 , fDesc(nullptr)
Brian Salomon2432d062020-04-16 20:48:09 -0400461 , fMetadata(proxyView.swizzle(), filter, Subset(!!subsetRect), saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500462
Michael Ludwig6bee7762018-10-19 09:50:36 -0400463 // Clean up disparities between the overall aa type and edge configuration and apply
464 // optimizations based on the rect and matrix when appropriate
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500465 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
466 &aaType, &quad->fEdgeFlags);
Michael Ludwig4384f042019-12-05 10:30:35 -0500467 fMetadata.fAAType = static_cast<uint16_t>(aaType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400468
Brian Salomonf1709042018-10-03 11:57:00 -0400469 // We expect our caller to have already caught this optimization.
Brian Salomon2432d062020-04-16 20:48:09 -0400470 SkASSERT(!subsetRect ||
471 !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500472
Brian Salomonf09abc52018-10-03 15:59:04 -0400473 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
John Stilescbe4e282020-06-01 10:38:31 -0400474 // Try to identify cases where the subsetting isn't actually necessary, and skip it.
475 if (subsetRect) {
476 if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
477 subsetRect = nullptr;
478 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
479 }
Brian Salomonf09abc52018-10-03 15:59:04 -0400480 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500481
Brian Salomon2432d062020-04-16 20:48:09 -0400482 // Normalize src coordinates and the subset (if set)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500483 NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
484 proxyView.origin());
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500485 normalize_src_quad(params, &quad->fLocal);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400486 SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500487
Michael Ludwig949ceb22020-02-07 10:14:45 -0500488 // Set bounds before clipping so we don't have to worry about unioning the bounds of
489 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500490 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -0400491 IsHairline::kNo);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500492
Brian Salomon2432d062020-04-16 20:48:09 -0400493 int quadCount = this->appendQuad(quad, color, subset);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500494 fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
Brian Salomond7065e72018-10-12 11:42:02 -0400495 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400496
Michael Ludwigadb12e72019-12-04 16:19:18 -0500497 TextureOp(GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400498 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500499 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400500 GrSamplerState::Filter filter,
501 GrTextureOp::Saturate saturate,
502 GrAAType aaType,
503 SkCanvas::SrcRectConstraint constraint,
504 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500505 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400506 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400507 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400508 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600509 , fDesc(nullptr)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500510 , fMetadata(set[0].fProxyView.swizzle(), GrSamplerState::Filter::kNearest,
Brian Salomon2432d062020-04-16 20:48:09 -0400511 Subset::kNo, saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500512 // Update counts to reflect the batch op
Michael Ludwig379e4962019-12-06 13:21:26 -0500513 fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500514 fMetadata.fTotalQuadCount = SkToUInt(cnt);
515
Brian Salomond7065e72018-10-12 11:42:02 -0400516 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500517
518 GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon2432d062020-04-16 20:48:09 -0400519 Subset netSubset = Subset::kNo;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500520 GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
521
Michael Ludwig379e4962019-12-06 13:21:26 -0500522 const GrSurfaceProxy* curProxy = nullptr;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500523
Michael Ludwig379e4962019-12-06 13:21:26 -0500524 // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
525 // increases when set[q]'s proxy changes.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500526 int p = 0;
527 for (int q = 0; q < cnt; ++q) {
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
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500564 if (netFilter != filter && filter_has_effect(quad.fLocal, quad.fDevice)) {
Brian Salomona3b02f52020-07-15 16:02:01 -0400565 // The only way netFilter != filter is if linear filtering is requested and we
566 // haven't yet found a quad that requires linear filtering (so net is still
567 // nearest).
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500568 SkASSERT(netFilter == GrSamplerState::Filter::kNearest &&
Brian Salomona3b02f52020-07-15 16:02:01 -0400569 filter == GrSamplerState::Filter::kLinear);
570 netFilter = GrSamplerState::Filter::kLinear;
Michael Ludwig22429f92019-06-27 10:44:48 -0400571 }
572
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500573 // Update overall bounds of the op as the union of all quads
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500574 bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500575
576 // Determine the AA type for the quad, then merge with net AA type
Michael Ludwig6bee7762018-10-19 09:50:36 -0400577 GrAAType aaForQuad;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500578 GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
579 &aaForQuad, &quad.fEdgeFlags);
John Stilescbe4e282020-06-01 10:38:31 -0400580
Michael Ludwig6bee7762018-10-19 09:50:36 -0400581 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
582 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500583 if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
584 netAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400585 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400586
587 // Calculate metadata for the entry
Brian Salomon2432d062020-04-16 20:48:09 -0400588 const SkRect* subsetForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400589 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
John Stilescbe4e282020-06-01 10:38:31 -0400590 // Check (briefly) if the subset rect is actually needed for this set entry.
591 SkRect* subsetRect = &set[q].fSrcRect;
592 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
593 if (!safe_to_ignore_subset_rect(aaForQuad, filter, quad, *subsetRect)) {
594 netSubset = Subset::kYes;
595 subsetForQuad = subsetRect;
596 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400597 }
598 }
John Stilescbe4e282020-06-01 10:38:31 -0400599
600 // Normalize the src quads and apply origin
601 NormalizationParams proxyParams = proxy_normalization_params(
602 curProxy, set[q].fProxyView.origin());
603 normalize_src_quad(proxyParams, &quad.fLocal);
604
Brian Salomon2432d062020-04-16 20:48:09 -0400605 // This subset may represent a no-op, otherwise it will have the origin and dimensions
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500606 // of the texture applied to it. Insetting for bilinear filtering is deferred until
607 // on[Pre]Prepare so that the overall filter can be lazily determined.
Brian Salomon75cebbe2020-05-18 14:08:14 -0400608 SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500609
Michael Ludwig949ceb22020-02-07 10:14:45 -0500610 // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
611 // ViewCountPair (this frequently happens when Chrome draws 9-patches).
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400612 fViewCountPairs[p].fQuadCnt += this->appendQuad(&quad, set[q].fColor, subset);
Brian Salomond7065e72018-10-12 11:42:02 -0400613 }
Michael Ludwig406172a2019-12-06 14:05:19 -0500614 // The # of proxy switches should match what was provided (+1 because we incremented p
Michael Ludwig379e4962019-12-06 13:21:26 -0500615 // when a new proxy was encountered).
Michael Ludwig406172a2019-12-06 14:05:19 -0500616 SkASSERT((p + 1) == fMetadata.fProxyCount);
Michael Ludwig379e4962019-12-06 13:21:26 -0500617 SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500618
Michael Ludwig4384f042019-12-05 10:30:35 -0500619 fMetadata.fAAType = static_cast<uint16_t>(netAAType);
620 fMetadata.fFilter = static_cast<uint16_t>(netFilter);
Brian Salomon2432d062020-04-16 20:48:09 -0400621 fMetadata.fSubset = static_cast<uint16_t>(netSubset);
Brian Salomon34169692017-08-28 15:32:01 -0400622
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500623 this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage), IsHairline::kNo);
Brian Salomon17031a72018-05-22 14:14:07 -0400624 }
625
Brian Salomon2432d062020-04-16 20:48:09 -0400626 int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& subset) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500627 DrawQuad extra;
Michael Ludwig465864c2020-02-10 09:30:04 -0500628 // Only clip when there's anti-aliasing. When non-aa, the GPU clips just fine and there's
629 // no inset/outset math that requires w > 0.
630 int quadCount = quad->fEdgeFlags != GrQuadAAFlags::kNone ?
631 GrQuadUtils::ClipToW0(quad, &extra) : 1;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500632 if (quadCount == 0) {
633 // We can't discard the op at this point, but disable AA flags so it won't go through
634 // inset/outset processing
635 quad->fEdgeFlags = GrQuadAAFlags::kNone;
636 quadCount = 1;
637 }
Brian Salomon2432d062020-04-16 20:48:09 -0400638 fQuads.append(quad->fDevice, {color, subset, quad->fEdgeFlags}, &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500639 if (quadCount > 1) {
Brian Salomon2432d062020-04-16 20:48:09 -0400640 fQuads.append(extra.fDevice, {color, subset, extra.fEdgeFlags}, &extra.fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500641 fMetadata.fTotalQuadCount++;
642 }
643 return quadCount;
644 }
645
Robert Phillips2669a7b2020-03-12 12:07:19 -0400646 GrProgramInfo* programInfo() override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600647 // Although this Op implements its own onPrePrepareDraws it calls GrMeshDrawOps' version so
648 // this entry point will be called.
649 return (fDesc) ? fDesc->fProgramInfo : nullptr;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400650 }
651
Chris Daltondbb833b2020-03-17 12:15:46 -0600652 void onCreateProgramInfo(const GrCaps* caps,
653 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400654 const GrSurfaceProxyView* writeView,
Chris Daltondbb833b2020-03-17 12:15:46 -0600655 GrAppliedClip&& appliedClip,
656 const GrXferProcessor::DstProxyView& dstProxyView) override {
657 SkASSERT(fDesc);
658
659 GrGeometryProcessor* gp;
660
661 {
662 const GrBackendFormat& backendFormat =
663 fViewCountPairs[0].fProxy->backendFormat();
664
665 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
666 fMetadata.filter());
667
668 gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
669 arena, fDesc->fVertexSpec, *caps->shaderCaps(), backendFormat, samplerState,
670 fMetadata.fSwizzle, std::move(fTextureColorSpaceXform), fMetadata.saturate());
671
672 SkASSERT(fDesc->fVertexSpec.vertexSize() == gp->vertexStride());
673 }
674
675 auto pipelineFlags = (GrAAType::kMSAA == fMetadata.aaType()) ?
676 GrPipeline::InputFlags::kHWAntialias : GrPipeline::InputFlags::kNone;
677
678 fDesc->fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Brian Salomon8afde5f2020-04-01 16:22:00 -0400679 caps, arena, writeView, std::move(appliedClip), dstProxyView, gp,
Chris Daltondbb833b2020-03-17 12:15:46 -0600680 GrProcessorSet::MakeEmptySet(), fDesc->fVertexSpec.primitiveType(),
681 pipelineFlags);
Robert Phillips4133dc42020-03-11 15:55:55 -0400682 }
683
Robert Phillipsdf70f152019-11-15 14:57:05 -0500684 void onPrePrepareDraws(GrRecordingContext* context,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400685 const GrSurfaceProxyView* writeView,
Robert Phillips8053c972019-11-21 10:44:53 -0500686 GrAppliedClip* clip,
687 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips61fc7992019-10-22 11:58:17 -0400688 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips29f38542019-10-16 09:20:25 -0400689
Robert Phillips61fc7992019-10-22 11:58:17 -0400690 SkDEBUGCODE(this->validate();)
Chris Daltondbb833b2020-03-17 12:15:46 -0600691 SkASSERT(!fDesc);
Robert Phillips61fc7992019-10-22 11:58:17 -0400692
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500693 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillips61fc7992019-10-22 11:58:17 -0400694
Chris Daltondbb833b2020-03-17 12:15:46 -0600695 fDesc = arena->make<Desc>();
696 this->characterize(fDesc);
697 fDesc->allocatePrePreparedVertices(arena);
698 FillInVertices(*context->priv().caps(), this, fDesc, fDesc->fPrePreparedVertices);
Robert Phillips61fc7992019-10-22 11:58:17 -0400699
Chris Daltondbb833b2020-03-17 12:15:46 -0600700 // This will call onCreateProgramInfo and register the created program with the DDL.
Brian Salomon8afde5f2020-04-01 16:22:00 -0400701 this->INHERITED::onPrePrepareDraws(context, writeView, clip, dstProxyView);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400702 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400703
Chris Daltondbb833b2020-03-17 12:15:46 -0600704 static void FillInVertices(const GrCaps& caps, TextureOp* texOp, Desc* desc, char* vertexData) {
705 SkASSERT(vertexData);
706
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400707 int totQuadsSeen = 0;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400708 SkDEBUGCODE(int totVerticesSeen = 0;)
Michael Ludwig189c9802019-11-21 11:21:12 -0500709 SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400710
Chris Daltondbb833b2020-03-17 12:15:46 -0600711 GrQuadPerEdgeAA::Tessellator tessellator(desc->fVertexSpec, vertexData);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400712 for (const auto& op : ChainRange<TextureOp>(texOp)) {
713 auto iter = op.fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500714 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500715 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
716 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400717
Chris Daltondbb833b2020-03-17 12:15:46 -0600718 for (int i = 0; i < quadCnt && iter.next(); ++i) {
719 SkASSERT(iter.isLocalValid());
Brian Salomon2432d062020-04-16 20:48:09 -0400720 const ColorSubsetAndAA& info = iter.metadata();
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500721
Chris Daltondbb833b2020-03-17 12:15:46 -0600722 tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
Brian Salomon75cebbe2020-05-18 14:08:14 -0400723 info.fSubsetRect, info.aaFlags());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400724 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400725
Chris Daltondbb833b2020-03-17 12:15:46 -0600726 SkASSERT((totVerticesSeen + meshVertexCnt) * vertexSize
727 == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400728
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400729 totQuadsSeen += quadCnt;
730 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
731 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400732 }
733
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400734 // If quad counts per proxy were calculated correctly, the entire iterator
735 // should have been consumed.
Chris Daltondbb833b2020-03-17 12:15:46 -0600736 SkASSERT(!iter.next());
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(desc->totalSizeInBytes() == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400740 SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
741 SkASSERT(totVerticesSeen == desc->totalNumVertices());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400742 }
743
Robert Phillips29f38542019-10-16 09:20:25 -0400744#ifdef SK_DEBUG
745 void validate() const override {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500746 // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
Michael Ludwigadb12e72019-12-04 16:19:18 -0500747 auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
748 GrAAType aaType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400749
Robert Phillipse837e612019-11-15 11:02:50 -0500750 int quadCount = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400751 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500752 SkASSERT(op.fMetadata.fSwizzle == fMetadata.fSwizzle);
753
754 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
755 auto* proxy = op.fViewCountPairs[p].fProxy->asTextureProxy();
Robert Phillipse837e612019-11-15 11:02:50 -0500756 quadCount += op.fViewCountPairs[p].fQuadCnt;
Robert Phillips29f38542019-10-16 09:20:25 -0400757 SkASSERT(proxy);
758 SkASSERT(proxy->textureType() == textureType);
Robert Phillips29f38542019-10-16 09:20:25 -0400759 }
760
761 // Each individual op must be a single aaType. kCoverage and kNone ops can chain
762 // together but kMSAA ones do not.
763 if (aaType == GrAAType::kCoverage || aaType == GrAAType::kNone) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500764 SkASSERT(op.fMetadata.aaType() == GrAAType::kCoverage ||
765 op.fMetadata.aaType() == GrAAType::kNone);
Robert Phillips29f38542019-10-16 09:20:25 -0400766 } else {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500767 SkASSERT(aaType == GrAAType::kMSAA && op.fMetadata.aaType() == GrAAType::kMSAA);
Robert Phillips29f38542019-10-16 09:20:25 -0400768 }
769 }
Robert Phillipse837e612019-11-15 11:02:50 -0500770
771 SkASSERT(quadCount == this->numChainedQuads());
Robert Phillips29f38542019-10-16 09:20:25 -0400772 }
773#endif
774
Robert Phillipse837e612019-11-15 11:02:50 -0500775#if GR_TEST_UTILS
776 int numQuads() const final { return this->totNumQuads(); }
777#endif
778
Chris Daltondbb833b2020-03-17 12:15:46 -0600779 void characterize(Desc* desc) const {
Robert Phillips29f38542019-10-16 09:20:25 -0400780 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
781 ColorType colorType = ColorType::kNone;
782 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomon2432d062020-04-16 20:48:09 -0400783 Subset subset = Subset::kNo;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500784 GrAAType overallAAType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400785
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400786 desc->fNumProxies = 0;
787 desc->fNumTotalQuads = 0;
788 int maxQuadsPerMesh = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400789
Brian Salomonf7232642018-09-19 08:58:08 -0400790 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400791 if (op.fQuads.deviceQuadType() > quadType) {
792 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500793 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400794 if (op.fQuads.localQuadType() > srcQuadType) {
795 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500796 }
Brian Salomon2432d062020-04-16 20:48:09 -0400797 if (op.fMetadata.subset() == Subset::kYes) {
798 subset = Subset::kYes;
Brian Salomonf7232642018-09-19 08:58:08 -0400799 }
Brian Osman788b9162020-02-07 10:36:46 -0500800 colorType = std::max(colorType, op.fMetadata.colorType());
Michael Ludwigadb12e72019-12-04 16:19:18 -0500801 desc->fNumProxies += op.fMetadata.fProxyCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400802
Michael Ludwigadb12e72019-12-04 16:19:18 -0500803 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Brian Osman788b9162020-02-07 10:36:46 -0500804 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
Brian Salomonf7232642018-09-19 08:58:08 -0400805 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400806 desc->fNumTotalQuads += op.totNumQuads();
807
Michael Ludwigadb12e72019-12-04 16:19:18 -0500808 if (op.fMetadata.aaType() == GrAAType::kCoverage) {
Robert Phillips29f38542019-10-16 09:20:25 -0400809 overallAAType = GrAAType::kCoverage;
Brian Salomonae7d7702018-10-14 15:05:45 -0400810 }
Brian Salomon34169692017-08-28 15:32:01 -0400811 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400812
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400813 SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
814
815 SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
816
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400817 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(overallAAType,
818 maxQuadsPerMesh);
819
820 desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
Brian Salomon2432d062020-04-16 20:48:09 -0400821 subset, overallAAType, /* alpha as coverage */ true,
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400822 indexBufferOption);
Robert Phillipse837e612019-11-15 11:02:50 -0500823
824 SkASSERT(desc->fNumTotalQuads <= GrQuadPerEdgeAA::QuadLimit(indexBufferOption));
Robert Phillips29f38542019-10-16 09:20:25 -0400825 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500826
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400827 int totNumQuads() const {
828#ifdef SK_DEBUG
829 int tmp = 0;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500830 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400831 tmp += fViewCountPairs[p].fQuadCnt;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400832 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500833 SkASSERT(tmp == fMetadata.fTotalQuadCount);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400834#endif
835
Michael Ludwigadb12e72019-12-04 16:19:18 -0500836 return fMetadata.fTotalQuadCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400837 }
838
839 int numChainedQuads() const {
840 int numChainedQuads = this->totNumQuads();
841
842 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
843 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
844 }
845
846 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
847 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
848 }
849
850 return numChainedQuads;
851 }
852
Robert Phillips29f38542019-10-16 09:20:25 -0400853 // onPrePrepareDraws may or may not have been called at this point
854 void onPrepareDraws(Target* target) override {
855 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500856
Robert Phillips29f38542019-10-16 09:20:25 -0400857 SkDEBUGCODE(this->validate();)
858
Chris Daltondbb833b2020-03-17 12:15:46 -0600859 SkASSERT(!fDesc || fDesc->fPrePreparedVertices);
Robert Phillips29f38542019-10-16 09:20:25 -0400860
Chris Daltondbb833b2020-03-17 12:15:46 -0600861 if (!fDesc) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400862 SkArenaAlloc* arena = target->allocator();
Chris Daltondbb833b2020-03-17 12:15:46 -0600863 fDesc = arena->make<Desc>();
864 this->characterize(fDesc);
865 SkASSERT(!fDesc->fPrePreparedVertices);
Brian Salomonf7232642018-09-19 08:58:08 -0400866 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400867
Chris Daltondbb833b2020-03-17 12:15:46 -0600868 size_t vertexSize = fDesc->fVertexSpec.vertexSize();
Brian Salomon92be2f72018-06-19 14:33:47 -0400869
Chris Daltondbb833b2020-03-17 12:15:46 -0600870 void* vdata = target->makeVertexSpace(vertexSize, fDesc->totalNumVertices(),
871 &fDesc->fVertexBuffer, &fDesc->fBaseVertex);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400872 if (!vdata) {
873 SkDebugf("Could not allocate vertices\n");
874 return;
Brian Salomon34169692017-08-28 15:32:01 -0400875 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400876
Chris Daltondbb833b2020-03-17 12:15:46 -0600877 if (fDesc->fVertexSpec.needsIndexBuffer()) {
878 fDesc->fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(
879 target, fDesc->fVertexSpec.indexBufferOption());
880 if (!fDesc->fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400881 SkDebugf("Could not allocate indices\n");
882 return;
883 }
884 }
885
Chris Daltondbb833b2020-03-17 12:15:46 -0600886 if (fDesc->fPrePreparedVertices) {
887 memcpy(vdata, fDesc->fPrePreparedVertices, fDesc->totalSizeInBytes());
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400888 } else {
Chris Daltondbb833b2020-03-17 12:15:46 -0600889 FillInVertices(target->caps(), this, fDesc, (char*) vdata);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400890 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700891 }
892
893 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600894 if (!fDesc->fVertexBuffer) {
895 return;
896 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500897
Chris Daltondbb833b2020-03-17 12:15:46 -0600898 if (fDesc->fVertexSpec.needsIndexBuffer() && !fDesc->fIndexBuffer) {
899 return;
900 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500901
Chris Daltondbb833b2020-03-17 12:15:46 -0600902 if (!fDesc->fProgramInfo) {
903 this->createProgramInfo(flushState);
904 SkASSERT(fDesc->fProgramInfo);
905 }
906
907 flushState->bindPipelineAndScissorClip(*fDesc->fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400908 flushState->bindBuffers(std::move(fDesc->fIndexBuffer), nullptr,
909 std::move(fDesc->fVertexBuffer));
Chris Daltondbb833b2020-03-17 12:15:46 -0600910
911 int totQuadsSeen = 0;
912 SkDEBUGCODE(int numDraws = 0;)
913 for (const auto& op : ChainRange<TextureOp>(this)) {
914 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
915 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
916 SkASSERT(numDraws < fDesc->fNumProxies);
917 flushState->bindTextures(fDesc->fProgramInfo->primProc(),
918 *op.fViewCountPairs[p].fProxy,
919 fDesc->fProgramInfo->pipeline());
920 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(),
921 fDesc->fVertexSpec, totQuadsSeen, quadCnt,
922 fDesc->totalNumVertices(), fDesc->fBaseVertex);
923 totQuadsSeen += quadCnt;
924 SkDEBUGCODE(++numDraws;)
925 }
926 }
927
928 SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
929 SkASSERT(numDraws == fDesc->fNumProxies);
Brian Salomon34169692017-08-28 15:32:01 -0400930 }
931
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500932 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
933 const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400934 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400935 const auto* that = t->cast<TextureOp>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400936
Chris Daltondbb833b2020-03-17 12:15:46 -0600937 if (fDesc || that->fDesc) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400938 // This should never happen (since only DDL recorded ops should be prePrepared)
939 // but, in any case, we should never combine ops that that been prePrepared
940 return CombineResult::kCannotCombine;
941 }
942
Brian Salomon2432d062020-04-16 20:48:09 -0400943 if (fMetadata.subset() != that->fMetadata.subset()) {
944 // It is technically possible to combine operations across subset modes, but performance
Michael Ludwig2929f512019-04-19 13:05:56 -0400945 // testing suggests it's better to make more draw calls where some take advantage of
946 // the more optimal shader path without coordinate clamping.
947 return CombineResult::kCannotCombine;
948 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400949 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
950 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000951 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400952 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400953
Brian Salomonae7d7702018-10-14 15:05:45 -0400954 bool upgradeToCoverageAAOnMerge = false;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500955 if (fMetadata.aaType() != that->fMetadata.aaType()) {
956 if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400957 return CombineResult::kCannotCombine;
958 }
959 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500960 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400961
Michael Ludwigadb12e72019-12-04 16:19:18 -0500962 if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400963 this->numChainedQuads() + that->numChainedQuads())) {
964 return CombineResult::kCannotCombine;
Robert Phillipsb69001f2019-10-29 12:16:35 -0400965 }
966
Michael Ludwigadb12e72019-12-04 16:19:18 -0500967 if (fMetadata.saturate() != that->fMetadata.saturate()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400968 return CombineResult::kCannotCombine;
969 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500970 if (fMetadata.filter() != that->fMetadata.filter()) {
Brian Salomonf7232642018-09-19 08:58:08 -0400971 return CombineResult::kCannotCombine;
972 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500973 if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
974 return CombineResult::kCannotCombine;
975 }
976 const auto* thisProxy = fViewCountPairs[0].fProxy.get();
977 const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
978 if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
979 thisProxy != thatProxy) {
Brian Salomon588cec72018-11-14 13:56:37 -0500980 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -0500981 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Michael Ludwigadb12e72019-12-04 16:19:18 -0500982 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
Brian Salomonf7232642018-09-19 08:58:08 -0400983 return CombineResult::kMayChain;
984 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000985 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400986 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500987
Brian Salomon2432d062020-04-16 20:48:09 -0400988 fMetadata.fSubset |= that->fMetadata.fSubset;
Brian Osman788b9162020-02-07 10:36:46 -0500989 fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -0400990 if (upgradeToCoverageAAOnMerge) {
Michael Ludwig4384f042019-12-05 10:30:35 -0500991 fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
Brian Salomonae7d7702018-10-14 15:05:45 -0400992 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500993
Michael Ludwig425eb452019-06-27 10:13:27 -0400994 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -0500995 fQuads.concat(that->fQuads);
Greg Daniel549325c2019-10-30 16:19:20 -0400996 fViewCountPairs[0].fQuadCnt += that->fQuads.count();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500997 fMetadata.fTotalQuadCount += that->fQuads.count();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500998
Brian Salomon7eae3e02018-08-07 14:02:38 +0000999 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -04001000 }
1001
Brian Salomon2432d062020-04-16 20:48:09 -04001002 GrQuadBuffer<ColorSubsetAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -04001003 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Chris Daltondbb833b2020-03-17 12:15:46 -06001004 // Most state of TextureOp is packed into these two field to minimize the op's size.
Michael Ludwigadb12e72019-12-04 16:19:18 -05001005 // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1006 // consider/measure changes with care.
Chris Daltondbb833b2020-03-17 12:15:46 -06001007 Desc* fDesc;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001008 Metadata fMetadata;
Robert Phillips32803ff2019-10-23 08:26:08 -04001009
1010 // This field must go last. When allocating this op, we will allocate extra space to hold
Greg Daniel549325c2019-10-30 16:19:20 -04001011 // additional ViewCountPairs immediately after the op's allocation so we can treat this
Robert Phillips32803ff2019-10-23 08:26:08 -04001012 // as an fProxyCnt-length array.
Greg Daniel549325c2019-10-30 16:19:20 -04001013 ViewCountPair fViewCountPairs[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -04001014
Brian Salomon34169692017-08-28 15:32:01 -04001015 typedef GrMeshDrawOp INHERITED;
1016};
1017
1018} // anonymous namespace
1019
Robert Phillipse837e612019-11-15 11:02:50 -05001020#if GR_TEST_UTILS
1021uint32_t GrTextureOp::ClassID() {
1022 return TextureOp::ClassID();
1023}
1024#endif
Brian Salomon34169692017-08-28 15:32:01 -04001025
Robert Phillipse837e612019-11-15 11:02:50 -05001026std::unique_ptr<GrDrawOp> GrTextureOp::Make(GrRecordingContext* context,
1027 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -05001028 SkAlphaType alphaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001029 sk_sp<GrColorSpaceXform> textureXform,
1030 GrSamplerState::Filter filter,
1031 const SkPMColor4f& color,
1032 Saturate saturate,
1033 SkBlendMode blendMode,
1034 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001035 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -04001036 const SkRect* subset) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001037 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
Brian Salomon2432d062020-04-16 20:48:09 -04001038 if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1039 // No need for a shader-based subset if hardware clamping achieves the same effect
1040 subset = nullptr;
Michael Ludwig22429f92019-06-27 10:44:48 -04001041 }
1042
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001043 if (filter != GrSamplerState::Filter::kNearest &&
1044 !filter_has_effect(quad->fLocal, quad->fDevice)) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001045 filter = GrSamplerState::Filter::kNearest;
1046 }
1047
1048 if (blendMode == SkBlendMode::kSrcOver) {
Greg Daniel549325c2019-10-30 16:19:20 -04001049 return TextureOp::Make(context, std::move(proxyView), std::move(textureXform), filter,
Brian Salomon2432d062020-04-16 20:48:09 -04001050 color, saturate, aaType, std::move(quad), subset);
Michael Ludwig22429f92019-06-27 10:44:48 -04001051 } else {
1052 // Emulate complex blending using GrFillRectOp
1053 GrPaint paint;
1054 paint.setColor4f(color);
1055 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1056
1057 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon2432d062020-04-16 20:48:09 -04001058 if (subset) {
Brian Salomonca6b2f42020-01-24 11:31:21 -05001059 const auto& caps = *context->priv().caps();
1060 SkRect localRect;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001061 if (quad->fLocal.asRect(&localRect)) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001062 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
1063 filter, *subset, localRect, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001064 } else {
John Stiles5a2a7b32020-06-04 10:57:21 -04001065 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
1066 filter, *subset, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001067 }
1068 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -05001069 fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), filter);
Michael Ludwig22429f92019-06-27 10:44:48 -04001070 }
Brian Osmanf48f76e2020-07-15 16:04:17 -04001071 fp = GrXfermodeFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Michael Ludwig22429f92019-06-27 10:44:48 -04001072 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001073 if (saturate == GrTextureOp::Saturate::kYes) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001074 fp = GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/false);
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001075 }
John Stiles5933d7d2020-07-21 12:28:35 -04001076 paint.setColorFragmentProcessor(std::move(fp));
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001077 return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
Michael Ludwig22429f92019-06-27 10:44:48 -04001078 }
1079}
1080
Robert Phillipse837e612019-11-15 11:02:50 -05001081// A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1082class GrTextureOp::BatchSizeLimiter {
1083public:
1084 BatchSizeLimiter(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001085 const GrClip* clip,
Robert Phillipse837e612019-11-15 11:02:50 -05001086 GrRecordingContext* context,
1087 int numEntries,
1088 GrSamplerState::Filter filter,
1089 GrTextureOp::Saturate saturate,
1090 SkCanvas::SrcRectConstraint constraint,
1091 const SkMatrix& viewMatrix,
1092 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
1093 : fRTC(rtc)
1094 , fClip(clip)
1095 , fContext(context)
1096 , fFilter(filter)
1097 , fSaturate(saturate)
1098 , fConstraint(constraint)
1099 , fViewMatrix(viewMatrix)
1100 , fTextureColorSpaceXform(textureColorSpaceXform)
1101 , fNumLeft(numEntries) {
1102 }
Brian Salomon34169692017-08-28 15:32:01 -04001103
Michael Ludwigadb12e72019-12-04 16:19:18 -05001104 void createOp(GrRenderTargetContext::TextureSetEntry set[],
Robert Phillipse837e612019-11-15 11:02:50 -05001105 int clumpSize,
1106 GrAAType aaType) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001107 int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
Robert Phillipse837e612019-11-15 11:02:50 -05001108 std::unique_ptr<GrDrawOp> op = TextureOp::Make(fContext, &set[fNumClumped], clumpSize,
Michael Ludwig379e4962019-12-06 13:21:26 -05001109 clumpProxyCount, fFilter, fSaturate, aaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001110 fConstraint, fViewMatrix,
1111 fTextureColorSpaceXform);
1112 fRTC->addDrawOp(fClip, std::move(op));
1113
1114 fNumLeft -= clumpSize;
1115 fNumClumped += clumpSize;
1116 }
1117
1118 int numLeft() const { return fNumLeft; }
1119 int baseIndex() const { return fNumClumped; }
1120
1121private:
1122 GrRenderTargetContext* fRTC;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001123 const GrClip* fClip;
Robert Phillipse837e612019-11-15 11:02:50 -05001124 GrRecordingContext* fContext;
1125 GrSamplerState::Filter fFilter;
1126 GrTextureOp::Saturate fSaturate;
1127 SkCanvas::SrcRectConstraint fConstraint;
1128 const SkMatrix& fViewMatrix;
1129 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
1130
1131 int fNumLeft;
1132 int fNumClumped = 0; // also the offset for the start of the next clump
1133};
1134
1135// Greedily clump quad draws together until the index buffer limit is exceeded.
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001136void GrTextureOp::AddTextureSetOps(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001137 const GrClip* clip,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001138 GrRecordingContext* context,
Michael Ludwigadb12e72019-12-04 16:19:18 -05001139 GrRenderTargetContext::TextureSetEntry set[],
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001140 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -05001141 int proxyRunCnt,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001142 GrSamplerState::Filter filter,
1143 Saturate saturate,
1144 SkBlendMode blendMode,
1145 GrAAType aaType,
1146 SkCanvas::SrcRectConstraint constraint,
1147 const SkMatrix& viewMatrix,
1148 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001149 // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1150 // the op's metadata so we don't need to worry about overflow.
Michael Ludwig4ef1ca12019-12-19 10:58:52 -05001151 SkDEBUGCODE(TextureOp::ValidateResourceLimits();)
Michael Ludwig379e4962019-12-06 13:21:26 -05001152 SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1153
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001154 // First check if we can support batches as a single op
1155 if (blendMode != SkBlendMode::kSrcOver ||
1156 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1157 // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1158 // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1159 // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1160 SkMatrix ctm;
1161 for (int i = 0; i < cnt; ++i) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001162 ctm = viewMatrix;
1163 if (set[i].fPreViewMatrix) {
1164 ctm.preConcat(*set[i].fPreViewMatrix);
1165 }
Robert Phillipse837e612019-11-15 11:02:50 -05001166
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001167 DrawQuad quad;
1168 quad.fEdgeFlags = set[i].fAAFlags;
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001169 if (set[i].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001170 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001171
1172 SkPoint srcPts[4];
1173 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001174 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001175 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001176 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1177 quad.fLocal = GrQuad(set[i].fSrcRect);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001178 }
1179
Brian Salomon2432d062020-04-16 20:48:09 -04001180 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001181 ? &set[i].fSrcRect : nullptr;
1182
Brian Salomonfc118442019-11-22 19:09:27 -05001183 auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
Michael Ludwig1c66ad92020-07-10 08:59:44 -04001184 filter, set[i].fColor, saturate, blendMode, aaType,
Brian Salomon2432d062020-04-16 20:48:09 -04001185 &quad, subset);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001186 rtc->addDrawOp(clip, std::move(op));
1187 }
1188 return;
1189 }
1190
1191 // Second check if we can always just make a single op and avoid the extra iteration
Robert Phillipse837e612019-11-15 11:02:50 -05001192 // needed to clump things together.
Brian Osman788b9162020-02-07 10:36:46 -05001193 if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
Robert Phillipse837e612019-11-15 11:02:50 -05001194 GrResourceProvider::MaxNumAAQuads())) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001195 auto op = TextureOp::Make(context, set, cnt, proxyRunCnt, filter, saturate, aaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001196 constraint, viewMatrix, std::move(textureColorSpaceXform));
1197 rtc->addDrawOp(clip, std::move(op));
1198 return;
1199 }
1200
1201 BatchSizeLimiter state(rtc, clip, context, cnt, filter, saturate, constraint, viewMatrix,
1202 std::move(textureColorSpaceXform));
1203
1204 // kNone and kMSAA never get altered
1205 if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1206 // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1207 while (state.numLeft() > 0) {
Brian Osman788b9162020-02-07 10:36:46 -05001208 int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
Robert Phillipse837e612019-11-15 11:02:50 -05001209
1210 state.createOp(set, clumpSize, aaType);
1211 }
1212 } else {
1213 // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1214 // can also get downgraded to kNone if all the quads are on integer coordinates and
1215 // axis-aligned.
1216 SkASSERT(aaType == GrAAType::kCoverage);
1217
1218 while (state.numLeft() > 0) {
1219 GrAAType runningAA = GrAAType::kNone;
1220 bool clumped = false;
1221
1222 for (int i = 0; i < state.numLeft(); ++i) {
1223 int absIndex = state.baseIndex() + i;
1224
1225 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone) {
1226
1227 if (i >= GrResourceProvider::MaxNumAAQuads()) {
1228 // Here we either need to boost the AA type to kCoverage, but doing so with
1229 // all the accumulated quads would overflow, or we have a set of AA quads
1230 // that has just gotten too large. In either case, calve off the existing
1231 // quads as their own TextureOp.
1232 state.createOp(
1233 set,
1234 runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1235 runningAA); // maybe downgrading AA here
1236 clumped = true;
1237 break;
1238 }
1239
1240 runningAA = GrAAType::kCoverage;
1241 } else if (runningAA == GrAAType::kNone) {
1242
1243 if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1244 // Here we've found a consistent batch of non-AA quads that has gotten too
1245 // large. Calve it off as its own GrTextureOp.
1246 state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1247 GrAAType::kNone); // definitely downgrading AA here
1248 clumped = true;
1249 break;
1250 }
1251 }
1252 }
1253
1254 if (!clumped) {
1255 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1256 // quads and call it a day.
1257 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1258 }
1259 }
1260 }
1261}
Robert Phillipsae01f622019-11-13 15:56:31 +00001262
Brian Salomon34169692017-08-28 15:32:01 -04001263#if GR_TEST_UTILS
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04001264#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001265#include "src/gpu/GrProxyProvider.h"
1266#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -04001267
1268GR_DRAW_OP_TEST_DEFINE(TextureOp) {
Brian Salomona56a7462020-02-07 14:17:25 -05001269 SkISize dims;
1270 dims.fHeight = random->nextULessThan(90) + 10;
1271 dims.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001272 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001273 GrMipmapped mipMapped = random->nextBool() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel09c94002018-06-08 22:11:51 +00001274 SkBackingFit fit = SkBackingFit::kExact;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001275 if (mipMapped == GrMipmapped::kNo) {
Greg Daniel09c94002018-06-08 22:11:51 +00001276 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1277 }
Greg Daniel4065d452018-11-16 15:43:41 -05001278 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -04001279 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1280 GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -05001281 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -04001282 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001283 format, dims, GrRenderable::kNo, 1, mipMapped, fit, SkBudgeted::kNo, GrProtected::kNo,
1284 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001285
Brian Salomon34169692017-08-28 15:32:01 -04001286 SkRect rect = GrTest::TestRect(random);
1287 SkRect srcRect;
1288 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1289 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1290 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1291 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1292 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -05001293 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001294 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
1295 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Brian Salomon7e67dca2020-07-21 09:27:25 -04001296 while (mipMapped == GrMipmapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
Greg Daniel09c94002018-06-08 22:11:51 +00001297 filter = (GrSamplerState::Filter)random->nextULessThan(
1298 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
1299 }
Brian Osman3ebd3542018-07-30 14:36:53 -04001300 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001301 GrAAType aaType = GrAAType::kNone;
1302 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06001303 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -05001304 }
Brian Salomon2213ee92018-10-02 10:44:21 -04001305 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1306 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1307 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1308 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1309 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomon2432d062020-04-16 20:48:09 -04001310 bool useSubset = random->nextBool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001311 auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
Greg Daniel549325c2019-10-30 16:19:20 -04001312 GrSurfaceProxyView proxyView(
1313 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -05001314 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Brian Salomonfc118442019-11-22 19:09:27 -05001315 auto alphaType = static_cast<SkAlphaType>(
1316 random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
Greg Daniel549325c2019-10-30 16:19:20 -04001317
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001318 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
Brian Salomonfc118442019-11-22 19:09:27 -05001319 return GrTextureOp::Make(context, std::move(proxyView), alphaType, std::move(texXform), filter,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001320 color, saturate, SkBlendMode::kSrcOver, aaType,
Brian Salomon2432d062020-04-16 20:48:09 -04001321 &quad, useSubset ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -04001322}
1323
1324#endif