blob: f07148e8a95ca1b255f8ad69f21341f743cf7350 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/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 Osman6f5e9402020-01-22 10:39:31 -050033#include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040034#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040035#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040036#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/glsl/GrGLSLVarying.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040038#include "src/gpu/ops/GrFillRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/ops/GrMeshDrawOp.h"
40#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050041#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040042#include "src/gpu/ops/GrTextureOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040043
44namespace {
45
Brian Salomon2432d062020-04-16 20:48:09 -040046using Subset = GrQuadPerEdgeAA::Subset;
Michael Ludwigc182b942018-11-16 10:27:51 -050047using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050048using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040049
Michael Ludwig22429f92019-06-27 10:44:48 -040050// Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
51// between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
52static SkSize axis_aligned_quad_size(const GrQuad& quad) {
53 SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
54 // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
55 float dw = sk_float_abs(quad.x(2) - quad.x(0)) + sk_float_abs(quad.y(2) - quad.y(0));
56 float dh = sk_float_abs(quad.x(1) - quad.x(0)) + sk_float_abs(quad.y(1) - quad.y(0));
57 return {dw, dh};
58}
59
60static bool filter_has_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
61 // If not axis-aligned in src or dst, then always say it has an effect
62 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
63 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
64 return true;
65 }
66
67 SkRect srcRect;
68 SkRect dstRect;
69 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
70 // Disable filtering when there is no scaling (width and height are the same), and the
71 // top-left corners have the same fraction (so src and dst snap to the pixel grid
72 // identically).
73 SkASSERT(srcRect.isSorted());
74 return srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
75 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
76 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
77 } else {
78 // Although the quads are axis-aligned, the local coordinate system is transformed such
79 // that fractionally-aligned sample centers will not align with the device coordinate system
80 // So disable filtering when edges are the same length and both srcQuad and dstQuad
81 // 0th vertex is integer aligned.
82 if (SkScalarIsInt(srcQuad.x(0)) && SkScalarIsInt(srcQuad.y(0)) &&
83 SkScalarIsInt(dstQuad.x(0)) && SkScalarIsInt(dstQuad.y(0))) {
84 // Extract edge lengths
85 SkSize srcSize = axis_aligned_quad_size(srcQuad);
86 SkSize dstSize = axis_aligned_quad_size(dstQuad);
87 return srcSize.fWidth != dstSize.fWidth || srcSize.fHeight != dstSize.fHeight;
88 } else {
89 return true;
90 }
91 }
92}
93
Michael Ludwig119ac6d2019-11-21 09:26:46 -050094// Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
95// regular and rectangular textures, w/ or w/o origin correction.
96struct NormalizationParams {
97 float fIW; // 1 / width of texture, or 1.0 for texture rectangles
Michael Ludwigc453a502020-05-29 12:29:12 -040098 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 -050099 float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
100};
Michael Ludwigadb12e72019-12-04 16:19:18 -0500101static NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
102 GrSurfaceOrigin origin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500103 // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
104 // normalize the src coordinates up front.
Michael Ludwigadb12e72019-12-04 16:19:18 -0500105 SkISize dimensions = proxy->backingStoreDimensions();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500106 float iw, ih, h;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500107 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500108 iw = ih = 1.f;
109 h = dimensions.height();
110 } else {
111 iw = 1.f / dimensions.width();
112 ih = 1.f / dimensions.height();
113 h = 1.f;
114 }
115
Michael Ludwigadb12e72019-12-04 16:19:18 -0500116 if (origin == kBottomLeft_GrSurfaceOrigin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500117 return {iw, -ih, h};
118 } else {
119 return {iw, ih, 0.0f};
120 }
121}
122
Brian Salomon2432d062020-04-16 20:48:09 -0400123// Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500124// 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 -0400125// subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
126// and y-flipping are applied as indicated by NormalizationParams.
127static SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
128 const NormalizationParams& params,
129 const SkRect* subsetRect) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500130 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Brian Salomon2432d062020-04-16 20:48:09 -0400131 if (!subsetRect) {
132 // Either the quad has no subset constraint and is batched with a subset constrained op
133 // (in which case we want a subset that doesn't restrict normalized tex coords), or the
134 // entire op doesn't use the subset, in which case the returned value is ignored.
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500135 return kLargeRect;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400136 }
137
Brian Salomon2432d062020-04-16 20:48:09 -0400138 auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400139 auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
140 if (filter == GrSamplerState::Filter::kNearest) {
141 // Make sure our insetting puts us at pixel centers.
142 ltrb = skvx::floor(ltrb*flipHi)*flipHi;
143 }
144 // Inset with pin to the rect center.
145 ltrb += skvx::Vec<4, float>({.5f, .5f, -.5f, -.5f});
146 auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
147 ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
148
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500149 // Normalize and offset
Michael Ludwigc453a502020-05-29 12:29:12 -0400150 ltrb = mad(ltrb, {params.fIW, params.fInvH, params.fIW, params.fInvH},
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500151 {0.f, params.fYOffset, 0.f, params.fYOffset});
Michael Ludwigc453a502020-05-29 12:29:12 -0400152 if (params.fInvH < 0.f) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500153 // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
154 ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400155 }
156
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500157 SkRect out;
158 ltrb.store(&out);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500159 return out;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400160}
161
Michael Ludwig009b92e2019-02-15 16:03:53 -0500162// Normalizes logical src coords and corrects for origin
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500163static void normalize_src_quad(const NormalizationParams& params,
164 GrQuad* srcQuad) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500165 // The src quad should not have any perspective
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500166 SkASSERT(!srcQuad->hasPerspective());
167 skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
Michael Ludwigc453a502020-05-29 12:29:12 -0400168 skvx::Vec<4, float> ys = mad(srcQuad->y4f(), params.fInvH, params.fYOffset);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500169 xs.store(srcQuad->xs());
170 ys.store(srcQuad->ys());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500171}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400172
Michael Ludwig379e4962019-12-06 13:21:26 -0500173// Count the number of proxy runs in the entry set. This usually is already computed by
174// SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
175// for each split.
176static int proxy_run_count(const GrRenderTargetContext::TextureSetEntry set[], int count) {
177 int actualProxyRunCount = 0;
178 const GrSurfaceProxy* lastProxy = nullptr;
179 for (int i = 0; i < count; ++i) {
180 if (set[i].fProxyView.proxy() != lastProxy) {
181 actualProxyRunCount++;
182 lastProxy = set[i].fProxyView.proxy();
183 }
184 }
185 return actualProxyRunCount;
186}
187
John Stilescbe4e282020-06-01 10:38:31 -0400188static bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
189 const DrawQuad& quad, const SkRect& subsetRect) {
190 // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
191 // can push all the way up to the edges of the the subset rect and the sampler shouldn't
192 // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
193 // the non-antialiased case.
194 SkRect localBounds = quad.fLocal.bounds();
195 if (aaType == GrAAType::kNone &&
196 filter == GrSamplerState::Filter::kNearest &&
197 quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
198 quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
199 subsetRect.contains(localBounds)) {
200
201 return true;
202 }
203
204 // If the subset rect is inset by at least 0.5 pixels into the local quad's bounds, the
205 // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
206 if (subsetRect.makeInset(0.5f, 0.5f).contains(localBounds)) {
207 return true;
208 }
209
210 // The subset rect cannot be ignored safely.
211 return false;
212}
213
Brian Salomon34169692017-08-28 15:32:01 -0400214/**
215 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
216 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
217 */
218class TextureOp final : public GrMeshDrawOp {
219public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500220 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Greg Daniel549325c2019-10-30 16:19:20 -0400221 GrSurfaceProxyView proxyView,
Michael Ludwig22429f92019-06-27 10:44:48 -0400222 sk_sp<GrColorSpaceXform> textureXform,
Robert Phillips7c525e62018-06-12 10:11:12 -0400223 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500224 const SkPMColor4f& color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400225 GrTextureOp::Saturate saturate,
Robert Phillips7c525e62018-06-12 10:11:12 -0400226 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500227 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400228 const SkRect* subset) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500229 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Greg Daniel549325c2019-10-30 16:19:20 -0400230 return pool->allocate<TextureOp>(std::move(proxyView), std::move(textureXform), filter,
Brian Salomon2432d062020-04-16 20:48:09 -0400231 color, saturate, aaType, quad, subset);
Brian Salomon34169692017-08-28 15:32:01 -0400232 }
Robert Phillipse837e612019-11-15 11:02:50 -0500233
Robert Phillipsb97da532019-02-12 15:24:12 -0500234 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwigadb12e72019-12-04 16:19:18 -0500235 GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400236 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500237 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400238 GrSamplerState::Filter filter,
239 GrTextureOp::Saturate saturate,
240 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400241 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500242 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500243 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500244 // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
245 SkASSERT(proxyRunCnt <= cnt);
246
247 size_t size = sizeof(TextureOp) + sizeof(ViewCountPair) * (proxyRunCnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500248 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400249 void* mem = pool->allocate(size);
Michael Ludwig379e4962019-12-06 13:21:26 -0500250 return std::unique_ptr<GrDrawOp>(
251 new (mem) TextureOp(set, cnt, proxyRunCnt, filter, saturate, aaType, constraint,
252 viewMatrix, std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400253 }
Brian Salomon34169692017-08-28 15:32:01 -0400254
Brian Salomon336ce7b2017-09-08 08:23:58 -0400255 ~TextureOp() override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500256 for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400257 fViewCountPairs[p].~ViewCountPair();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400258 }
259 }
Brian Salomon34169692017-08-28 15:32:01 -0400260
261 const char* name() const override { return "TextureOp"; }
262
Chris Dalton1706cbf2019-05-21 19:35:29 -0600263 void visitProxies(const VisitProxyFunc& func) const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500264 bool mipped = (GrSamplerState::Filter::kMipMap == fMetadata.filter());
265 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
266 func(fViewCountPairs[p].fProxy.get(), GrMipMapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400267 }
Chris Daltondbb833b2020-03-17 12:15:46 -0600268 if (fDesc && fDesc->fProgramInfo) {
269 fDesc->fProgramInfo->visitFPProxies(func);
270 }
Brian Salomond7065e72018-10-12 11:42:02 -0400271 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400272
Brian Osman9a390ac2018-11-12 09:47:48 -0500273#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400274 SkString dumpInfo() const override {
275 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400276 str.appendf("# draws: %d\n", fQuads.count());
Michael Ludwig425eb452019-06-27 10:13:27 -0400277 auto iter = fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500278 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Robert Phillips32803ff2019-10-23 08:26:08 -0400279 str.appendf("Proxy ID: %d, Filter: %d\n",
Michael Ludwigadb12e72019-12-04 16:19:18 -0500280 fViewCountPairs[p].fProxy->uniqueID().asUInt(),
281 static_cast<int>(fMetadata.fFilter));
Michael Ludwig425eb452019-06-27 10:13:27 -0400282 int i = 0;
Greg Daniel549325c2019-10-30 16:19:20 -0400283 while(i < fViewCountPairs[p].fQuadCnt && iter.next()) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500284 const GrQuad* quad = iter.deviceQuad();
285 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
Brian Salomon2432d062020-04-16 20:48:09 -0400286 const ColorSubsetAndAA& info = iter.metadata();
Brian Salomond7065e72018-10-12 11:42:02 -0400287 str.appendf(
Brian Salomon2432d062020-04-16 20:48:09 -0400288 "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400289 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
290 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
Brian Salomon2432d062020-04-16 20:48:09 -0400291 i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
292 info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
Michael Ludwig704d5402019-11-25 09:43:37 -0500293 quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
294 quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400295 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
296 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
297
Michael Ludwig425eb452019-06-27 10:13:27 -0400298 i++;
Brian Salomond7065e72018-10-12 11:42:02 -0400299 }
Brian Salomon34169692017-08-28 15:32:01 -0400300 }
301 str += INHERITED::dumpInfo();
302 return str;
303 }
Michael Ludwig4ef1ca12019-12-19 10:58:52 -0500304
305 static void ValidateResourceLimits() {
306 // The op implementation has an upper bound on the number of quads that it can represent.
307 // However, the resource manager imposes its own limit on the number of quads, which should
308 // always be lower than the numerical limit this op can hold.
309 using CountStorage = decltype(Metadata::fTotalQuadCount);
310 CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
311 // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
312 int resourceLimit = SkTo<int>(maxQuadCount);
313 SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
314 GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
315 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500316#endif
Brian Salomon34169692017-08-28 15:32:01 -0400317
Brian Osman5ced0bf2019-03-15 10:15:29 -0400318 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600319 const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage,
320 GrClampType clampType) override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500321 SkASSERT(fMetadata.colorType() == ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400322 auto iter = fQuads.metadata();
323 while(iter.next()) {
Brian Osman2715bf52019-12-06 14:38:47 -0500324 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
Brian Osman788b9162020-02-07 10:36:46 -0500325 fMetadata.fColorType = std::max(fMetadata.fColorType, static_cast<uint16_t>(colorType));
Brian Osman8fa7ab42019-03-18 10:22:42 -0400326 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700327 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400328 }
329
Brian Salomon485b8c62018-01-12 15:11:06 -0500330 FixedFunctionFlags fixedFunctionFlags() const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500331 return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
332 : FixedFunctionFlags::kNone;
Brian Salomon485b8c62018-01-12 15:11:06 -0500333 }
Brian Salomon34169692017-08-28 15:32:01 -0400334
335 DEFINE_OP_CLASS_ID
336
337private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400338 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500339
Brian Salomon2432d062020-04-16 20:48:09 -0400340 struct ColorSubsetAndAA {
341 ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400342 : fColor(color)
Brian Salomon2432d062020-04-16 20:48:09 -0400343 , fSubsetRect(subsetRect)
Michael Ludwig4384f042019-12-05 10:30:35 -0500344 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
345 SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
Michael Ludwig425eb452019-06-27 10:13:27 -0400346 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400347
348 SkPMColor4f fColor;
Brian Salomon2432d062020-04-16 20:48:09 -0400349 // 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 -0500350 // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
Brian Salomon2432d062020-04-16 20:48:09 -0400351 SkRect fSubsetRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400352 unsigned fAAFlags : 4;
353
Michael Ludwig425eb452019-06-27 10:13:27 -0400354 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
355 };
Michael Ludwigadb12e72019-12-04 16:19:18 -0500356
Greg Daniel549325c2019-10-30 16:19:20 -0400357 struct ViewCountPair {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500358 // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
359 // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
360 // swizzle so that is stored in the op metadata.
361 sk_sp<GrSurfaceProxy> fProxy;
Michael Ludwig425eb452019-06-27 10:13:27 -0400362 int fQuadCnt;
363 };
364
Michael Ludwigadb12e72019-12-04 16:19:18 -0500365 // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
366 // increase the size of the op; increasing the op size can have a surprising impact on
367 // performance (since texture ops are one of the most commonly used in an app).
368 struct Metadata {
369 // AAType must be filled after initialization; ColorType is determined in finalize()
370 Metadata(const GrSwizzle& swizzle, GrSamplerState::Filter filter,
Brian Salomon2432d062020-04-16 20:48:09 -0400371 GrQuadPerEdgeAA::Subset subset, GrTextureOp::Saturate saturate)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500372 : fSwizzle(swizzle)
373 , fProxyCount(1)
374 , fTotalQuadCount(1)
Michael Ludwig4384f042019-12-05 10:30:35 -0500375 , fFilter(static_cast<uint16_t>(filter))
376 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
377 , fColorType(static_cast<uint16_t>(ColorType::kNone))
Brian Salomon2432d062020-04-16 20:48:09 -0400378 , fSubset(static_cast<uint16_t>(subset))
Michael Ludwig4384f042019-12-05 10:30:35 -0500379 , fSaturate(static_cast<uint16_t>(saturate)) {}
Michael Ludwigadb12e72019-12-04 16:19:18 -0500380
Michael Ludwig4384f042019-12-05 10:30:35 -0500381 GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
Michael Ludwigadb12e72019-12-04 16:19:18 -0500382 uint16_t fProxyCount;
383 // This will be >= fProxyCount, since a proxy may be drawn multiple times
384 uint16_t fTotalQuadCount;
385
Michael Ludwig4384f042019-12-05 10:30:35 -0500386 // These must be based on uint16_t to help MSVC's pack bitfields optimally
387 uint16_t fFilter : 2; // GrSamplerState::Filter
388 uint16_t fAAType : 2; // GrAAType
389 uint16_t fColorType : 2; // GrQuadPerEdgeAA::ColorType
Brian Salomon2432d062020-04-16 20:48:09 -0400390 uint16_t fSubset : 1; // bool
Michael Ludwig4384f042019-12-05 10:30:35 -0500391 uint16_t fSaturate : 1; // bool
392 uint16_t fUnused : 8; // # of bits left before Metadata exceeds 8 bytes
Michael Ludwigadb12e72019-12-04 16:19:18 -0500393
394 GrSamplerState::Filter filter() const {
395 return static_cast<GrSamplerState::Filter>(fFilter);
396 }
397 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
398 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
Brian Salomon2432d062020-04-16 20:48:09 -0400399 Subset subset() const { return static_cast<Subset>(fSubset); }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500400 GrTextureOp::Saturate saturate() const {
401 return static_cast<GrTextureOp::Saturate>(fSaturate);
402 }
403
404 static_assert(GrSamplerState::kFilterCount <= 4);
405 static_assert(kGrAATypeCount <= 4);
406 static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
407 };
Michael Ludwig4384f042019-12-05 10:30:35 -0500408 static_assert(sizeof(Metadata) == 8);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500409
Chris Daltondbb833b2020-03-17 12:15:46 -0600410 // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
411 // store the data in a separate struct in order to minimize the size of the TextureOp.
412 // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
413 // may want to re-evaluate whether this is still necessary.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400414 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600415 // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
416 // allocatePrePreparedVertices is also called.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400417 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600418 // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
419 // part of the flushState).
420 struct Desc {
421 VertexSpec fVertexSpec;
422 int fNumProxies = 0;
423 int fNumTotalQuads = 0;
Robert Phillips32803ff2019-10-23 08:26:08 -0400424
Chris Daltondbb833b2020-03-17 12:15:46 -0600425 // This member variable is only used by 'onPrePrepareDraws'.
426 char* fPrePreparedVertices = nullptr;
427
428 GrProgramInfo* fProgramInfo = nullptr;
429
430 sk_sp<const GrBuffer> fIndexBuffer;
431 sk_sp<const GrBuffer> fVertexBuffer;
432 int fBaseVertex;
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400433
434 // How big should 'fVertices' be to hold all the vertex data?
435 size_t totalSizeInBytes() const {
Chris Daltondbb833b2020-03-17 12:15:46 -0600436 return this->totalNumVertices() * fVertexSpec.vertexSize();
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400437 }
438
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400439 int totalNumVertices() const {
440 return fNumTotalQuads * fVertexSpec.verticesPerQuad();
441 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400442
Chris Daltondbb833b2020-03-17 12:15:46 -0600443 void allocatePrePreparedVertices(SkArenaAlloc* arena) {
444 fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400445 }
Robert Phillips32803ff2019-10-23 08:26:08 -0400446 };
Brian Salomon2432d062020-04-16 20:48:09 -0400447 // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
Greg Daniel549325c2019-10-30 16:19:20 -0400448 TextureOp(GrSurfaceProxyView proxyView,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400449 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
450 GrSamplerState::Filter filter,
451 const SkPMColor4f& color,
452 GrTextureOp::Saturate saturate,
453 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500454 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400455 const SkRect* subsetRect)
Brian Salomon34169692017-08-28 15:32:01 -0400456 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400457 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400458 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600459 , fDesc(nullptr)
Brian Salomon2432d062020-04-16 20:48:09 -0400460 , fMetadata(proxyView.swizzle(), filter, Subset(!!subsetRect), saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500461
Michael Ludwig6bee7762018-10-19 09:50:36 -0400462 // Clean up disparities between the overall aa type and edge configuration and apply
463 // optimizations based on the rect and matrix when appropriate
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500464 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
465 &aaType, &quad->fEdgeFlags);
Michael Ludwig4384f042019-12-05 10:30:35 -0500466 fMetadata.fAAType = static_cast<uint16_t>(aaType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400467
Brian Salomonf1709042018-10-03 11:57:00 -0400468 // We expect our caller to have already caught this optimization.
Brian Salomon2432d062020-04-16 20:48:09 -0400469 SkASSERT(!subsetRect ||
470 !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500471
Brian Salomonf09abc52018-10-03 15:59:04 -0400472 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
John Stilescbe4e282020-06-01 10:38:31 -0400473 // Try to identify cases where the subsetting isn't actually necessary, and skip it.
474 if (subsetRect) {
475 if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
476 subsetRect = nullptr;
477 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
478 }
Brian Salomonf09abc52018-10-03 15:59:04 -0400479 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500480
Brian Salomon2432d062020-04-16 20:48:09 -0400481 // Normalize src coordinates and the subset (if set)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500482 NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
483 proxyView.origin());
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500484 normalize_src_quad(params, &quad->fLocal);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400485 SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500486
Michael Ludwig949ceb22020-02-07 10:14:45 -0500487 // Set bounds before clipping so we don't have to worry about unioning the bounds of
488 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500489 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -0400490 IsHairline::kNo);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500491
Brian Salomon2432d062020-04-16 20:48:09 -0400492 int quadCount = this->appendQuad(quad, color, subset);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500493 fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
Brian Salomond7065e72018-10-12 11:42:02 -0400494 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400495
Michael Ludwigadb12e72019-12-04 16:19:18 -0500496 TextureOp(GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400497 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500498 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400499 GrSamplerState::Filter filter,
500 GrTextureOp::Saturate saturate,
501 GrAAType aaType,
502 SkCanvas::SrcRectConstraint constraint,
503 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500504 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400505 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400506 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400507 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600508 , fDesc(nullptr)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500509 , fMetadata(set[0].fProxyView.swizzle(), GrSamplerState::Filter::kNearest,
Brian Salomon2432d062020-04-16 20:48:09 -0400510 Subset::kNo, saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500511 // Update counts to reflect the batch op
Michael Ludwig379e4962019-12-06 13:21:26 -0500512 fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500513 fMetadata.fTotalQuadCount = SkToUInt(cnt);
514
Brian Salomond7065e72018-10-12 11:42:02 -0400515 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500516
517 GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon2432d062020-04-16 20:48:09 -0400518 Subset netSubset = Subset::kNo;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500519 GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
520
Michael Ludwig379e4962019-12-06 13:21:26 -0500521 const GrSurfaceProxy* curProxy = nullptr;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500522
Michael Ludwig379e4962019-12-06 13:21:26 -0500523 // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
524 // increases when set[q]'s proxy changes.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500525 int p = 0;
526 for (int q = 0; q < cnt; ++q) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500527 if (q == 0) {
Greg Daniel549325c2019-10-30 16:19:20 -0400528 // We do not placement new the first ViewCountPair since that one is allocated and
529 // initialized as part of the GrTextureOp creation.
Michael Ludwig379e4962019-12-06 13:21:26 -0500530 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
531 fViewCountPairs[0].fQuadCnt = 0;
532 curProxy = fViewCountPairs[0].fProxy.get();
533 } else if (set[q].fProxyView.proxy() != curProxy) {
Greg Daniel549325c2019-10-30 16:19:20 -0400534 // We must placement new the ViewCountPairs here so that the sk_sps in the
535 // GrSurfaceProxyView get initialized properly.
Michael Ludwig379e4962019-12-06 13:21:26 -0500536 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
Michael Ludwigadb12e72019-12-04 16:19:18 -0500537
Michael Ludwig379e4962019-12-06 13:21:26 -0500538 curProxy = fViewCountPairs[p].fProxy.get();
Greg Danielc71c7962020-01-14 16:44:18 -0500539 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
540 curProxy, fViewCountPairs[0].fProxy.get()));
Michael Ludwig379e4962019-12-06 13:21:26 -0500541 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
Michael Ludwig379e4962019-12-06 13:21:26 -0500542 } // else another quad referencing the same proxy
Michael Ludwigce62dec2019-02-19 11:48:46 -0500543
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500544 SkMatrix ctm = viewMatrix;
Michael Ludwig379e4962019-12-06 13:21:26 -0500545 if (set[q].fPreViewMatrix) {
546 ctm.preConcat(*set[q].fPreViewMatrix);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500547 }
548
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400549 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
550 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500551 DrawQuad quad;
Michael Ludwig379e4962019-12-06 13:21:26 -0500552 if (set[q].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500553 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400554
555 SkPoint srcPts[4];
Michael Ludwig379e4962019-12-06 13:21:26 -0500556 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500557 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400558 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500559 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
560 quad.fLocal = GrQuad(set[q].fSrcRect);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400561 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500562
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500563 if (netFilter != filter && filter_has_effect(quad.fLocal, quad.fDevice)) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500564 // The only way netFilter != filter is if bilerp is requested and we haven't yet
565 // found a quad that requires bilerp (so net is still nearest).
566 SkASSERT(netFilter == GrSamplerState::Filter::kNearest &&
567 filter == GrSamplerState::Filter::kBilerp);
568 netFilter = GrSamplerState::Filter::kBilerp;
Michael Ludwig22429f92019-06-27 10:44:48 -0400569 }
570
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500571 // Update overall bounds of the op as the union of all quads
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500572 bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500573
574 // Determine the AA type for the quad, then merge with net AA type
Michael Ludwig6bee7762018-10-19 09:50:36 -0400575 GrAAType aaForQuad;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500576 GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
577 &aaForQuad, &quad.fEdgeFlags);
John Stilescbe4e282020-06-01 10:38:31 -0400578
Michael Ludwig6bee7762018-10-19 09:50:36 -0400579 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
580 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500581 if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
582 netAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400583 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400584
585 // Calculate metadata for the entry
Brian Salomon2432d062020-04-16 20:48:09 -0400586 const SkRect* subsetForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400587 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
John Stilescbe4e282020-06-01 10:38:31 -0400588 // Check (briefly) if the subset rect is actually needed for this set entry.
589 SkRect* subsetRect = &set[q].fSrcRect;
590 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
591 if (!safe_to_ignore_subset_rect(aaForQuad, filter, quad, *subsetRect)) {
592 netSubset = Subset::kYes;
593 subsetForQuad = subsetRect;
594 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400595 }
596 }
John Stilescbe4e282020-06-01 10:38:31 -0400597
598 // Normalize the src quads and apply origin
599 NormalizationParams proxyParams = proxy_normalization_params(
600 curProxy, set[q].fProxyView.origin());
601 normalize_src_quad(proxyParams, &quad.fLocal);
602
Brian Salomon2432d062020-04-16 20:48:09 -0400603 // This subset may represent a no-op, otherwise it will have the origin and dimensions
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500604 // of the texture applied to it. Insetting for bilinear filtering is deferred until
605 // on[Pre]Prepare so that the overall filter can be lazily determined.
Brian Salomon75cebbe2020-05-18 14:08:14 -0400606 SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500607
Michael Ludwig949ceb22020-02-07 10:14:45 -0500608 // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
609 // ViewCountPair (this frequently happens when Chrome draws 9-patches).
Michael Ludwig379e4962019-12-06 13:21:26 -0500610 float alpha = SkTPin(set[q].fAlpha, 0.f, 1.f);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500611 fViewCountPairs[p].fQuadCnt += this->appendQuad(
Brian Salomon2432d062020-04-16 20:48:09 -0400612 &quad, {alpha, alpha, alpha, alpha}, 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);
908 flushState->bindBuffers(fDesc->fIndexBuffer.get(), nullptr, fDesc->fVertexBuffer.get());
909
910 int totQuadsSeen = 0;
911 SkDEBUGCODE(int numDraws = 0;)
912 for (const auto& op : ChainRange<TextureOp>(this)) {
913 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
914 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
915 SkASSERT(numDraws < fDesc->fNumProxies);
916 flushState->bindTextures(fDesc->fProgramInfo->primProc(),
917 *op.fViewCountPairs[p].fProxy,
918 fDesc->fProgramInfo->pipeline());
919 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(),
920 fDesc->fVertexSpec, totQuadsSeen, quadCnt,
921 fDesc->totalNumVertices(), fDesc->fBaseVertex);
922 totQuadsSeen += quadCnt;
923 SkDEBUGCODE(++numDraws;)
924 }
925 }
926
927 SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
928 SkASSERT(numDraws == fDesc->fNumProxies);
Brian Salomon34169692017-08-28 15:32:01 -0400929 }
930
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500931 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
932 const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400933 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400934 const auto* that = t->cast<TextureOp>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400935
Chris Daltondbb833b2020-03-17 12:15:46 -0600936 if (fDesc || that->fDesc) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400937 // This should never happen (since only DDL recorded ops should be prePrepared)
938 // but, in any case, we should never combine ops that that been prePrepared
939 return CombineResult::kCannotCombine;
940 }
941
Brian Salomon2432d062020-04-16 20:48:09 -0400942 if (fMetadata.subset() != that->fMetadata.subset()) {
943 // It is technically possible to combine operations across subset modes, but performance
Michael Ludwig2929f512019-04-19 13:05:56 -0400944 // testing suggests it's better to make more draw calls where some take advantage of
945 // the more optimal shader path without coordinate clamping.
946 return CombineResult::kCannotCombine;
947 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400948 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
949 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000950 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400951 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400952
Brian Salomonae7d7702018-10-14 15:05:45 -0400953 bool upgradeToCoverageAAOnMerge = false;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500954 if (fMetadata.aaType() != that->fMetadata.aaType()) {
955 if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400956 return CombineResult::kCannotCombine;
957 }
958 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500959 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400960
Michael Ludwigadb12e72019-12-04 16:19:18 -0500961 if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400962 this->numChainedQuads() + that->numChainedQuads())) {
963 return CombineResult::kCannotCombine;
Robert Phillipsb69001f2019-10-29 12:16:35 -0400964 }
965
Michael Ludwigadb12e72019-12-04 16:19:18 -0500966 if (fMetadata.saturate() != that->fMetadata.saturate()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400967 return CombineResult::kCannotCombine;
968 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500969 if (fMetadata.filter() != that->fMetadata.filter()) {
Brian Salomonf7232642018-09-19 08:58:08 -0400970 return CombineResult::kCannotCombine;
971 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500972 if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
973 return CombineResult::kCannotCombine;
974 }
975 const auto* thisProxy = fViewCountPairs[0].fProxy.get();
976 const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
977 if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
978 thisProxy != thatProxy) {
Brian Salomon588cec72018-11-14 13:56:37 -0500979 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -0500980 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Michael Ludwigadb12e72019-12-04 16:19:18 -0500981 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
Brian Salomonf7232642018-09-19 08:58:08 -0400982 return CombineResult::kMayChain;
983 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000984 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400985 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500986
Brian Salomon2432d062020-04-16 20:48:09 -0400987 fMetadata.fSubset |= that->fMetadata.fSubset;
Brian Osman788b9162020-02-07 10:36:46 -0500988 fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -0400989 if (upgradeToCoverageAAOnMerge) {
Michael Ludwig4384f042019-12-05 10:30:35 -0500990 fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
Brian Salomonae7d7702018-10-14 15:05:45 -0400991 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500992
Michael Ludwig425eb452019-06-27 10:13:27 -0400993 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -0500994 fQuads.concat(that->fQuads);
Greg Daniel549325c2019-10-30 16:19:20 -0400995 fViewCountPairs[0].fQuadCnt += that->fQuads.count();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500996 fMetadata.fTotalQuadCount += that->fQuads.count();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500997
Brian Salomon7eae3e02018-08-07 14:02:38 +0000998 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -0400999 }
1000
Brian Salomon2432d062020-04-16 20:48:09 -04001001 GrQuadBuffer<ColorSubsetAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -04001002 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Chris Daltondbb833b2020-03-17 12:15:46 -06001003 // Most state of TextureOp is packed into these two field to minimize the op's size.
Michael Ludwigadb12e72019-12-04 16:19:18 -05001004 // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1005 // consider/measure changes with care.
Chris Daltondbb833b2020-03-17 12:15:46 -06001006 Desc* fDesc;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001007 Metadata fMetadata;
Robert Phillips32803ff2019-10-23 08:26:08 -04001008
1009 // This field must go last. When allocating this op, we will allocate extra space to hold
Greg Daniel549325c2019-10-30 16:19:20 -04001010 // additional ViewCountPairs immediately after the op's allocation so we can treat this
Robert Phillips32803ff2019-10-23 08:26:08 -04001011 // as an fProxyCnt-length array.
Greg Daniel549325c2019-10-30 16:19:20 -04001012 ViewCountPair fViewCountPairs[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -04001013
Brian Salomon34169692017-08-28 15:32:01 -04001014 typedef GrMeshDrawOp INHERITED;
1015};
1016
1017} // anonymous namespace
1018
Robert Phillipse837e612019-11-15 11:02:50 -05001019#if GR_TEST_UTILS
1020uint32_t GrTextureOp::ClassID() {
1021 return TextureOp::ClassID();
1022}
1023#endif
Brian Salomon34169692017-08-28 15:32:01 -04001024
Robert Phillipse837e612019-11-15 11:02:50 -05001025std::unique_ptr<GrDrawOp> GrTextureOp::Make(GrRecordingContext* context,
1026 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -05001027 SkAlphaType alphaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001028 sk_sp<GrColorSpaceXform> textureXform,
1029 GrSamplerState::Filter filter,
1030 const SkPMColor4f& color,
1031 Saturate saturate,
1032 SkBlendMode blendMode,
1033 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001034 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -04001035 const SkRect* subset) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001036 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
Brian Salomon2432d062020-04-16 20:48:09 -04001037 if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1038 // No need for a shader-based subset if hardware clamping achieves the same effect
1039 subset = nullptr;
Michael Ludwig22429f92019-06-27 10:44:48 -04001040 }
1041
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001042 if (filter != GrSamplerState::Filter::kNearest &&
1043 !filter_has_effect(quad->fLocal, quad->fDevice)) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001044 filter = GrSamplerState::Filter::kNearest;
1045 }
1046
1047 if (blendMode == SkBlendMode::kSrcOver) {
Greg Daniel549325c2019-10-30 16:19:20 -04001048 return TextureOp::Make(context, std::move(proxyView), std::move(textureXform), filter,
Brian Salomon2432d062020-04-16 20:48:09 -04001049 color, saturate, aaType, std::move(quad), subset);
Michael Ludwig22429f92019-06-27 10:44:48 -04001050 } else {
1051 // Emulate complex blending using GrFillRectOp
1052 GrPaint paint;
1053 paint.setColor4f(color);
1054 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1055
1056 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon2432d062020-04-16 20:48:09 -04001057 if (subset) {
Brian Salomonca6b2f42020-01-24 11:31:21 -05001058 const auto& caps = *context->priv().caps();
1059 SkRect localRect;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001060 if (quad->fLocal.asRect(&localRect)) {
Brian Salomoned729f92020-02-05 12:15:18 -05001061 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(), filter,
Brian Salomon2432d062020-04-16 20:48:09 -04001062 *subset, localRect, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001063 } else {
Brian Salomoned729f92020-02-05 12:15:18 -05001064 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(), filter,
Brian Salomon2432d062020-04-16 20:48:09 -04001065 *subset, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001066 }
1067 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -05001068 fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), filter);
Michael Ludwig22429f92019-06-27 10:44:48 -04001069 }
1070 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
1071 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001072 if (saturate == GrTextureOp::Saturate::kYes) {
Brian Osman6f5e9402020-01-22 10:39:31 -05001073 paint.addColorFragmentProcessor(GrClampFragmentProcessor::Make(false));
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001074 }
Michael Ludwig22429f92019-06-27 10:44:48 -04001075
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001076 return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
Michael Ludwig22429f92019-06-27 10:44:48 -04001077 }
1078}
1079
Robert Phillipse837e612019-11-15 11:02:50 -05001080// A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1081class GrTextureOp::BatchSizeLimiter {
1082public:
1083 BatchSizeLimiter(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001084 const GrClip* clip,
Robert Phillipse837e612019-11-15 11:02:50 -05001085 GrRecordingContext* context,
1086 int numEntries,
1087 GrSamplerState::Filter filter,
1088 GrTextureOp::Saturate saturate,
1089 SkCanvas::SrcRectConstraint constraint,
1090 const SkMatrix& viewMatrix,
1091 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
1092 : fRTC(rtc)
1093 , fClip(clip)
1094 , fContext(context)
1095 , fFilter(filter)
1096 , fSaturate(saturate)
1097 , fConstraint(constraint)
1098 , fViewMatrix(viewMatrix)
1099 , fTextureColorSpaceXform(textureColorSpaceXform)
1100 , fNumLeft(numEntries) {
1101 }
Brian Salomon34169692017-08-28 15:32:01 -04001102
Michael Ludwigadb12e72019-12-04 16:19:18 -05001103 void createOp(GrRenderTargetContext::TextureSetEntry set[],
Robert Phillipse837e612019-11-15 11:02:50 -05001104 int clumpSize,
1105 GrAAType aaType) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001106 int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
Robert Phillipse837e612019-11-15 11:02:50 -05001107 std::unique_ptr<GrDrawOp> op = TextureOp::Make(fContext, &set[fNumClumped], clumpSize,
Michael Ludwig379e4962019-12-06 13:21:26 -05001108 clumpProxyCount, fFilter, fSaturate, aaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001109 fConstraint, fViewMatrix,
1110 fTextureColorSpaceXform);
1111 fRTC->addDrawOp(fClip, std::move(op));
1112
1113 fNumLeft -= clumpSize;
1114 fNumClumped += clumpSize;
1115 }
1116
1117 int numLeft() const { return fNumLeft; }
1118 int baseIndex() const { return fNumClumped; }
1119
1120private:
1121 GrRenderTargetContext* fRTC;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001122 const GrClip* fClip;
Robert Phillipse837e612019-11-15 11:02:50 -05001123 GrRecordingContext* fContext;
1124 GrSamplerState::Filter fFilter;
1125 GrTextureOp::Saturate fSaturate;
1126 SkCanvas::SrcRectConstraint fConstraint;
1127 const SkMatrix& fViewMatrix;
1128 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
1129
1130 int fNumLeft;
1131 int fNumClumped = 0; // also the offset for the start of the next clump
1132};
1133
1134// Greedily clump quad draws together until the index buffer limit is exceeded.
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001135void GrTextureOp::AddTextureSetOps(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001136 const GrClip* clip,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001137 GrRecordingContext* context,
Michael Ludwigadb12e72019-12-04 16:19:18 -05001138 GrRenderTargetContext::TextureSetEntry set[],
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001139 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -05001140 int proxyRunCnt,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001141 GrSamplerState::Filter filter,
1142 Saturate saturate,
1143 SkBlendMode blendMode,
1144 GrAAType aaType,
1145 SkCanvas::SrcRectConstraint constraint,
1146 const SkMatrix& viewMatrix,
1147 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001148 // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1149 // the op's metadata so we don't need to worry about overflow.
Michael Ludwig4ef1ca12019-12-19 10:58:52 -05001150 SkDEBUGCODE(TextureOp::ValidateResourceLimits();)
Michael Ludwig379e4962019-12-06 13:21:26 -05001151 SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1152
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001153 // First check if we can support batches as a single op
1154 if (blendMode != SkBlendMode::kSrcOver ||
1155 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1156 // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1157 // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1158 // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1159 SkMatrix ctm;
1160 for (int i = 0; i < cnt; ++i) {
1161 float alpha = set[i].fAlpha;
1162 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 Ludwigfe13ca32019-11-21 10:26:41 -05001184 filter, {alpha, alpha, alpha, alpha}, 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001264#include "include/private/GrRecordingContext.h"
1265#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;
Greg Daniel09c94002018-06-08 22:11:51 +00001273 GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
1274 SkBackingFit fit = SkBackingFit::kExact;
1275 if (mipMapped == GrMipMapped::kNo) {
1276 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);
Greg Daniel09c94002018-06-08 22:11:51 +00001296 while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
1297 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