blob: b672d1a4b600c891f7f91818b7ae8f5de0139bf9 [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
Brian Salomone69b9ef2020-07-22 11:18:06 -040061static std::tuple<bool /* filter */,
62 bool /* mipmap */>
63filter_and_mm_have_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
Michael Ludwig22429f92019-06-27 10:44:48 -040064 // If not axis-aligned in src or dst, then always say it has an effect
65 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
66 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040067 return {true, true};
Michael Ludwig22429f92019-06-27 10:44:48 -040068 }
69
70 SkRect srcRect;
71 SkRect dstRect;
72 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
73 // Disable filtering when there is no scaling (width and height are the same), and the
74 // top-left corners have the same fraction (so src and dst snap to the pixel grid
75 // identically).
76 SkASSERT(srcRect.isSorted());
Brian Salomone69b9ef2020-07-22 11:18:06 -040077 bool filter = srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
78 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
79 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
80 bool mm = srcRect.width() > dstRect.width() || srcRect.height() > dstRect.height();
81 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040082 }
Brian Salomone69b9ef2020-07-22 11:18:06 -040083 // Extract edge lengths
84 SkSize srcSize = axis_aligned_quad_size(srcQuad);
85 SkSize dstSize = axis_aligned_quad_size(dstQuad);
86 // Although the quads are axis-aligned, the local coordinate system is transformed such
87 // that fractionally-aligned sample centers will not align with the device coordinate system
88 // So disable filtering when edges are the same length and both srcQuad and dstQuad
89 // 0th vertex is integer aligned.
90 bool filter = srcSize != dstSize ||
91 !SkScalarIsInt(srcQuad.x(0)) ||
92 !SkScalarIsInt(srcQuad.y(0)) ||
93 !SkScalarIsInt(dstQuad.x(0)) ||
94 !SkScalarIsInt(dstQuad.y(0));
95 bool mm = srcSize.fWidth > dstSize.fWidth || srcSize.fHeight > dstSize.fHeight;
96 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040097}
98
Michael Ludwig119ac6d2019-11-21 09:26:46 -050099// Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
100// regular and rectangular textures, w/ or w/o origin correction.
101struct NormalizationParams {
102 float fIW; // 1 / width of texture, or 1.0 for texture rectangles
Michael Ludwigc453a502020-05-29 12:29:12 -0400103 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 -0500104 float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
105};
Michael Ludwigadb12e72019-12-04 16:19:18 -0500106static NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
107 GrSurfaceOrigin origin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500108 // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
109 // normalize the src coordinates up front.
Michael Ludwigadb12e72019-12-04 16:19:18 -0500110 SkISize dimensions = proxy->backingStoreDimensions();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500111 float iw, ih, h;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500112 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500113 iw = ih = 1.f;
114 h = dimensions.height();
115 } else {
116 iw = 1.f / dimensions.width();
117 ih = 1.f / dimensions.height();
118 h = 1.f;
119 }
120
Michael Ludwigadb12e72019-12-04 16:19:18 -0500121 if (origin == kBottomLeft_GrSurfaceOrigin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500122 return {iw, -ih, h};
123 } else {
124 return {iw, ih, 0.0f};
125 }
126}
127
Brian Salomon2432d062020-04-16 20:48:09 -0400128// Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500129// 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 -0400130// subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
131// and y-flipping are applied as indicated by NormalizationParams.
132static SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
133 const NormalizationParams& params,
134 const SkRect* subsetRect) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500135 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Brian Salomon2432d062020-04-16 20:48:09 -0400136 if (!subsetRect) {
137 // Either the quad has no subset constraint and is batched with a subset constrained op
138 // (in which case we want a subset that doesn't restrict normalized tex coords), or the
139 // entire op doesn't use the subset, in which case the returned value is ignored.
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500140 return kLargeRect;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400141 }
142
Brian Salomon2432d062020-04-16 20:48:09 -0400143 auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400144 auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
145 if (filter == GrSamplerState::Filter::kNearest) {
146 // Make sure our insetting puts us at pixel centers.
147 ltrb = skvx::floor(ltrb*flipHi)*flipHi;
148 }
149 // Inset with pin to the rect center.
150 ltrb += skvx::Vec<4, float>({.5f, .5f, -.5f, -.5f});
151 auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
152 ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
153
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500154 // Normalize and offset
Michael Ludwigc453a502020-05-29 12:29:12 -0400155 ltrb = mad(ltrb, {params.fIW, params.fInvH, params.fIW, params.fInvH},
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500156 {0.f, params.fYOffset, 0.f, params.fYOffset});
Michael Ludwigc453a502020-05-29 12:29:12 -0400157 if (params.fInvH < 0.f) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500158 // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
159 ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400160 }
161
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500162 SkRect out;
163 ltrb.store(&out);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500164 return out;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400165}
166
Michael Ludwig009b92e2019-02-15 16:03:53 -0500167// Normalizes logical src coords and corrects for origin
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500168static void normalize_src_quad(const NormalizationParams& params,
169 GrQuad* srcQuad) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500170 // The src quad should not have any perspective
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500171 SkASSERT(!srcQuad->hasPerspective());
172 skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
Michael Ludwigc453a502020-05-29 12:29:12 -0400173 skvx::Vec<4, float> ys = mad(srcQuad->y4f(), params.fInvH, params.fYOffset);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500174 xs.store(srcQuad->xs());
175 ys.store(srcQuad->ys());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500176}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400177
Michael Ludwig379e4962019-12-06 13:21:26 -0500178// Count the number of proxy runs in the entry set. This usually is already computed by
179// SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
180// for each split.
181static int proxy_run_count(const GrRenderTargetContext::TextureSetEntry set[], int count) {
182 int actualProxyRunCount = 0;
183 const GrSurfaceProxy* lastProxy = nullptr;
184 for (int i = 0; i < count; ++i) {
185 if (set[i].fProxyView.proxy() != lastProxy) {
186 actualProxyRunCount++;
187 lastProxy = set[i].fProxyView.proxy();
188 }
189 }
190 return actualProxyRunCount;
191}
192
John Stilescbe4e282020-06-01 10:38:31 -0400193static bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
194 const DrawQuad& quad, const SkRect& subsetRect) {
195 // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
196 // can push all the way up to the edges of the the subset rect and the sampler shouldn't
197 // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
198 // the non-antialiased case.
199 SkRect localBounds = quad.fLocal.bounds();
200 if (aaType == GrAAType::kNone &&
201 filter == GrSamplerState::Filter::kNearest &&
202 quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
203 quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
204 subsetRect.contains(localBounds)) {
205
206 return true;
207 }
208
209 // If the subset rect is inset by at least 0.5 pixels into the local quad's bounds, the
210 // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
211 if (subsetRect.makeInset(0.5f, 0.5f).contains(localBounds)) {
212 return true;
213 }
214
215 // The subset rect cannot be ignored safely.
216 return false;
217}
218
Brian Salomon34169692017-08-28 15:32:01 -0400219/**
220 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
221 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
222 */
223class TextureOp final : public GrMeshDrawOp {
224public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500225 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Greg Daniel549325c2019-10-30 16:19:20 -0400226 GrSurfaceProxyView proxyView,
Michael Ludwig22429f92019-06-27 10:44:48 -0400227 sk_sp<GrColorSpaceXform> textureXform,
Robert Phillips7c525e62018-06-12 10:11:12 -0400228 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400229 GrSamplerState::MipmapMode mm,
Brian Osman3d139a42018-11-19 10:42:10 -0500230 const SkPMColor4f& color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400231 GrTextureOp::Saturate saturate,
Robert Phillips7c525e62018-06-12 10:11:12 -0400232 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500233 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400234 const SkRect* subset) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500235 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomone69b9ef2020-07-22 11:18:06 -0400236 return pool->allocate<TextureOp>(std::move(proxyView), std::move(textureXform), filter, mm,
Brian Salomon2432d062020-04-16 20:48:09 -0400237 color, saturate, aaType, quad, subset);
Brian Salomon34169692017-08-28 15:32:01 -0400238 }
Robert Phillipse837e612019-11-15 11:02:50 -0500239
Robert Phillipsb97da532019-02-12 15:24:12 -0500240 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwigadb12e72019-12-04 16:19:18 -0500241 GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400242 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500243 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400244 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400245 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400246 GrTextureOp::Saturate saturate,
247 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400248 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500249 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500250 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500251 // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
252 SkASSERT(proxyRunCnt <= cnt);
253
254 size_t size = sizeof(TextureOp) + sizeof(ViewCountPair) * (proxyRunCnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500255 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400256 void* mem = pool->allocate(size);
Michael Ludwig379e4962019-12-06 13:21:26 -0500257 return std::unique_ptr<GrDrawOp>(
Brian Salomone69b9ef2020-07-22 11:18:06 -0400258 new (mem) TextureOp(set, cnt, proxyRunCnt, filter, mm, saturate, aaType, constraint,
Michael Ludwig379e4962019-12-06 13:21:26 -0500259 viewMatrix, std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400260 }
Brian Salomon34169692017-08-28 15:32:01 -0400261
Brian Salomon336ce7b2017-09-08 08:23:58 -0400262 ~TextureOp() override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500263 for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400264 fViewCountPairs[p].~ViewCountPair();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400265 }
266 }
Brian Salomon34169692017-08-28 15:32:01 -0400267
268 const char* name() const override { return "TextureOp"; }
269
Chris Dalton1706cbf2019-05-21 19:35:29 -0600270 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400271 bool mipped = (fMetadata.mipmapMode() != GrSamplerState::MipmapMode::kNone);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500272 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400273 func(fViewCountPairs[p].fProxy.get(), GrMipmapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400274 }
Chris Daltondbb833b2020-03-17 12:15:46 -0600275 if (fDesc && fDesc->fProgramInfo) {
276 fDesc->fProgramInfo->visitFPProxies(func);
277 }
Brian Salomond7065e72018-10-12 11:42:02 -0400278 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400279
Brian Osman9a390ac2018-11-12 09:47:48 -0500280#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400281 SkString dumpInfo() const override {
282 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400283 str.appendf("# draws: %d\n", fQuads.count());
Michael Ludwig425eb452019-06-27 10:13:27 -0400284 auto iter = fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500285 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400286 str.appendf("Proxy ID: %d, Filter: %d, MM: %d\n",
Michael Ludwigadb12e72019-12-04 16:19:18 -0500287 fViewCountPairs[p].fProxy->uniqueID().asUInt(),
Brian Salomone69b9ef2020-07-22 11:18:06 -0400288 static_cast<int>(fMetadata.fFilter),
289 static_cast<int>(fMetadata.fMipmapMode));
Michael Ludwig425eb452019-06-27 10:13:27 -0400290 int i = 0;
Greg Daniel549325c2019-10-30 16:19:20 -0400291 while(i < fViewCountPairs[p].fQuadCnt && iter.next()) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500292 const GrQuad* quad = iter.deviceQuad();
293 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
Brian Salomon2432d062020-04-16 20:48:09 -0400294 const ColorSubsetAndAA& info = iter.metadata();
Brian Salomond7065e72018-10-12 11:42:02 -0400295 str.appendf(
Brian Salomon2432d062020-04-16 20:48:09 -0400296 "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400297 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
298 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
Brian Salomon2432d062020-04-16 20:48:09 -0400299 i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
300 info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
Michael Ludwig704d5402019-11-25 09:43:37 -0500301 quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
302 quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400303 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
304 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
305
Michael Ludwig425eb452019-06-27 10:13:27 -0400306 i++;
Brian Salomond7065e72018-10-12 11:42:02 -0400307 }
Brian Salomon34169692017-08-28 15:32:01 -0400308 }
309 str += INHERITED::dumpInfo();
310 return str;
311 }
Michael Ludwig4ef1ca12019-12-19 10:58:52 -0500312
313 static void ValidateResourceLimits() {
314 // The op implementation has an upper bound on the number of quads that it can represent.
315 // However, the resource manager imposes its own limit on the number of quads, which should
316 // always be lower than the numerical limit this op can hold.
317 using CountStorage = decltype(Metadata::fTotalQuadCount);
318 CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
319 // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
320 int resourceLimit = SkTo<int>(maxQuadCount);
321 SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
322 GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
323 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500324#endif
Brian Salomon34169692017-08-28 15:32:01 -0400325
Brian Osman5ced0bf2019-03-15 10:15:29 -0400326 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600327 const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage,
328 GrClampType clampType) override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500329 SkASSERT(fMetadata.colorType() == ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400330 auto iter = fQuads.metadata();
331 while(iter.next()) {
Brian Osman2715bf52019-12-06 14:38:47 -0500332 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
Brian Osman788b9162020-02-07 10:36:46 -0500333 fMetadata.fColorType = std::max(fMetadata.fColorType, static_cast<uint16_t>(colorType));
Brian Osman8fa7ab42019-03-18 10:22:42 -0400334 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700335 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400336 }
337
Brian Salomon485b8c62018-01-12 15:11:06 -0500338 FixedFunctionFlags fixedFunctionFlags() const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500339 return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
340 : FixedFunctionFlags::kNone;
Brian Salomon485b8c62018-01-12 15:11:06 -0500341 }
Brian Salomon34169692017-08-28 15:32:01 -0400342
343 DEFINE_OP_CLASS_ID
344
345private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400346 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500347
Brian Salomon2432d062020-04-16 20:48:09 -0400348 struct ColorSubsetAndAA {
349 ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400350 : fColor(color)
Brian Salomon2432d062020-04-16 20:48:09 -0400351 , fSubsetRect(subsetRect)
Michael Ludwig4384f042019-12-05 10:30:35 -0500352 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
353 SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
Michael Ludwig425eb452019-06-27 10:13:27 -0400354 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400355
356 SkPMColor4f fColor;
Brian Salomon2432d062020-04-16 20:48:09 -0400357 // 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 -0500358 // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
Brian Salomon2432d062020-04-16 20:48:09 -0400359 SkRect fSubsetRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400360 unsigned fAAFlags : 4;
361
Michael Ludwig425eb452019-06-27 10:13:27 -0400362 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
363 };
Michael Ludwigadb12e72019-12-04 16:19:18 -0500364
Greg Daniel549325c2019-10-30 16:19:20 -0400365 struct ViewCountPair {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500366 // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
367 // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
368 // swizzle so that is stored in the op metadata.
369 sk_sp<GrSurfaceProxy> fProxy;
Michael Ludwig425eb452019-06-27 10:13:27 -0400370 int fQuadCnt;
371 };
372
Michael Ludwigadb12e72019-12-04 16:19:18 -0500373 // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
374 // increase the size of the op; increasing the op size can have a surprising impact on
375 // performance (since texture ops are one of the most commonly used in an app).
376 struct Metadata {
377 // AAType must be filled after initialization; ColorType is determined in finalize()
Brian Salomone69b9ef2020-07-22 11:18:06 -0400378 Metadata(const GrSwizzle& swizzle,
379 GrSamplerState::Filter filter,
380 GrSamplerState::MipmapMode mm,
381 GrQuadPerEdgeAA::Subset subset,
382 GrTextureOp::Saturate saturate)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500383 : fSwizzle(swizzle)
384 , fProxyCount(1)
385 , fTotalQuadCount(1)
Michael Ludwig4384f042019-12-05 10:30:35 -0500386 , fFilter(static_cast<uint16_t>(filter))
Brian Salomone69b9ef2020-07-22 11:18:06 -0400387 , fMipmapMode(static_cast<uint16_t>(mm))
Michael Ludwig4384f042019-12-05 10:30:35 -0500388 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
389 , fColorType(static_cast<uint16_t>(ColorType::kNone))
Brian Salomon2432d062020-04-16 20:48:09 -0400390 , fSubset(static_cast<uint16_t>(subset))
Michael Ludwig4384f042019-12-05 10:30:35 -0500391 , fSaturate(static_cast<uint16_t>(saturate)) {}
Michael Ludwigadb12e72019-12-04 16:19:18 -0500392
Michael Ludwig4384f042019-12-05 10:30:35 -0500393 GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
Michael Ludwigadb12e72019-12-04 16:19:18 -0500394 uint16_t fProxyCount;
395 // This will be >= fProxyCount, since a proxy may be drawn multiple times
396 uint16_t fTotalQuadCount;
397
Michael Ludwig4384f042019-12-05 10:30:35 -0500398 // These must be based on uint16_t to help MSVC's pack bitfields optimally
399 uint16_t fFilter : 2; // GrSamplerState::Filter
Brian Salomone69b9ef2020-07-22 11:18:06 -0400400 uint16_t fMipmapMode : 2; // GrSamplerState::MipmapMode
Michael Ludwig4384f042019-12-05 10:30:35 -0500401 uint16_t fAAType : 2; // GrAAType
402 uint16_t fColorType : 2; // GrQuadPerEdgeAA::ColorType
Brian Salomon2432d062020-04-16 20:48:09 -0400403 uint16_t fSubset : 1; // bool
Michael Ludwig4384f042019-12-05 10:30:35 -0500404 uint16_t fSaturate : 1; // bool
Brian Salomone69b9ef2020-07-22 11:18:06 -0400405 uint16_t fUnused : 6; // # of bits left before Metadata exceeds 8 bytes
Michael Ludwigadb12e72019-12-04 16:19:18 -0500406
407 GrSamplerState::Filter filter() const {
408 return static_cast<GrSamplerState::Filter>(fFilter);
409 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400410 GrSamplerState::MipmapMode mipmapMode() const {
411 return static_cast<GrSamplerState::MipmapMode>(fMipmapMode);
412 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500413 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
414 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
Brian Salomon2432d062020-04-16 20:48:09 -0400415 Subset subset() const { return static_cast<Subset>(fSubset); }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500416 GrTextureOp::Saturate saturate() const {
417 return static_cast<GrTextureOp::Saturate>(fSaturate);
418 }
419
420 static_assert(GrSamplerState::kFilterCount <= 4);
421 static_assert(kGrAATypeCount <= 4);
422 static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
423 };
Michael Ludwig4384f042019-12-05 10:30:35 -0500424 static_assert(sizeof(Metadata) == 8);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500425
Chris Daltondbb833b2020-03-17 12:15:46 -0600426 // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
427 // store the data in a separate struct in order to minimize the size of the TextureOp.
428 // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
429 // may want to re-evaluate whether this is still necessary.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400430 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600431 // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
432 // allocatePrePreparedVertices is also called.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400433 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600434 // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
435 // part of the flushState).
436 struct Desc {
437 VertexSpec fVertexSpec;
438 int fNumProxies = 0;
439 int fNumTotalQuads = 0;
Robert Phillips32803ff2019-10-23 08:26:08 -0400440
Chris Daltondbb833b2020-03-17 12:15:46 -0600441 // This member variable is only used by 'onPrePrepareDraws'.
442 char* fPrePreparedVertices = nullptr;
443
444 GrProgramInfo* fProgramInfo = nullptr;
445
446 sk_sp<const GrBuffer> fIndexBuffer;
447 sk_sp<const GrBuffer> fVertexBuffer;
448 int fBaseVertex;
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400449
450 // How big should 'fVertices' be to hold all the vertex data?
451 size_t totalSizeInBytes() const {
Chris Daltondbb833b2020-03-17 12:15:46 -0600452 return this->totalNumVertices() * fVertexSpec.vertexSize();
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400453 }
454
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400455 int totalNumVertices() const {
456 return fNumTotalQuads * fVertexSpec.verticesPerQuad();
457 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400458
Chris Daltondbb833b2020-03-17 12:15:46 -0600459 void allocatePrePreparedVertices(SkArenaAlloc* arena) {
460 fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400461 }
Robert Phillips32803ff2019-10-23 08:26:08 -0400462 };
Brian Salomon2432d062020-04-16 20:48:09 -0400463 // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
Greg Daniel549325c2019-10-30 16:19:20 -0400464 TextureOp(GrSurfaceProxyView proxyView,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400465 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
466 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400467 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400468 const SkPMColor4f& color,
469 GrTextureOp::Saturate saturate,
470 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500471 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400472 const SkRect* subsetRect)
Brian Salomon34169692017-08-28 15:32:01 -0400473 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400474 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400475 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600476 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400477 , fMetadata(proxyView.swizzle(), filter, mm, Subset(!!subsetRect), saturate) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400478 // Clean up disparities between the overall aa type and edge configuration and apply
479 // optimizations based on the rect and matrix when appropriate
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500480 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
481 &aaType, &quad->fEdgeFlags);
Michael Ludwig4384f042019-12-05 10:30:35 -0500482 fMetadata.fAAType = static_cast<uint16_t>(aaType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400483
Brian Salomonf1709042018-10-03 11:57:00 -0400484 // We expect our caller to have already caught this optimization.
Brian Salomon2432d062020-04-16 20:48:09 -0400485 SkASSERT(!subsetRect ||
486 !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500487
Brian Salomonf09abc52018-10-03 15:59:04 -0400488 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
John Stilescbe4e282020-06-01 10:38:31 -0400489 // Try to identify cases where the subsetting isn't actually necessary, and skip it.
490 if (subsetRect) {
491 if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
492 subsetRect = nullptr;
493 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
494 }
Brian Salomonf09abc52018-10-03 15:59:04 -0400495 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500496
Brian Salomon2432d062020-04-16 20:48:09 -0400497 // Normalize src coordinates and the subset (if set)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500498 NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
499 proxyView.origin());
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500500 normalize_src_quad(params, &quad->fLocal);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400501 SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500502
Michael Ludwig949ceb22020-02-07 10:14:45 -0500503 // Set bounds before clipping so we don't have to worry about unioning the bounds of
504 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500505 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -0400506 IsHairline::kNo);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500507
Brian Salomon2432d062020-04-16 20:48:09 -0400508 int quadCount = this->appendQuad(quad, color, subset);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500509 fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
Brian Salomond7065e72018-10-12 11:42:02 -0400510 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400511
Michael Ludwigadb12e72019-12-04 16:19:18 -0500512 TextureOp(GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400513 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500514 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400515 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400516 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400517 GrTextureOp::Saturate saturate,
518 GrAAType aaType,
519 SkCanvas::SrcRectConstraint constraint,
520 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500521 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400522 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400523 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400524 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600525 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400526 , fMetadata(set[0].fProxyView.swizzle(),
527 GrSamplerState::Filter::kNearest,
528 GrSamplerState::MipmapMode::kNone,
529 Subset::kNo,
530 saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500531 // Update counts to reflect the batch op
Michael Ludwig379e4962019-12-06 13:21:26 -0500532 fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500533 fMetadata.fTotalQuadCount = SkToUInt(cnt);
534
Brian Salomond7065e72018-10-12 11:42:02 -0400535 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500536
537 GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon2432d062020-04-16 20:48:09 -0400538 Subset netSubset = Subset::kNo;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500539 GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400540 GrSamplerState::MipmapMode netMM = GrSamplerState::MipmapMode::kNone;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500541
Michael Ludwig379e4962019-12-06 13:21:26 -0500542 const GrSurfaceProxy* curProxy = nullptr;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500543
Michael Ludwig379e4962019-12-06 13:21:26 -0500544 // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
545 // increases when set[q]'s proxy changes.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500546 int p = 0;
547 for (int q = 0; q < cnt; ++q) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400548 SkASSERT(mm == GrSamplerState::MipmapMode::kNone ||
549 (set[0].fProxyView.proxy()->asTextureProxy()->mipmapped() ==
550 GrMipmapped::kYes));
Michael Ludwig379e4962019-12-06 13:21:26 -0500551 if (q == 0) {
Greg Daniel549325c2019-10-30 16:19:20 -0400552 // We do not placement new the first ViewCountPair since that one is allocated and
553 // initialized as part of the GrTextureOp creation.
Michael Ludwig379e4962019-12-06 13:21:26 -0500554 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
555 fViewCountPairs[0].fQuadCnt = 0;
556 curProxy = fViewCountPairs[0].fProxy.get();
557 } else if (set[q].fProxyView.proxy() != curProxy) {
Greg Daniel549325c2019-10-30 16:19:20 -0400558 // We must placement new the ViewCountPairs here so that the sk_sps in the
559 // GrSurfaceProxyView get initialized properly.
Michael Ludwig379e4962019-12-06 13:21:26 -0500560 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
Michael Ludwigadb12e72019-12-04 16:19:18 -0500561
Michael Ludwig379e4962019-12-06 13:21:26 -0500562 curProxy = fViewCountPairs[p].fProxy.get();
Greg Danielc71c7962020-01-14 16:44:18 -0500563 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
564 curProxy, fViewCountPairs[0].fProxy.get()));
Michael Ludwig379e4962019-12-06 13:21:26 -0500565 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
Michael Ludwig379e4962019-12-06 13:21:26 -0500566 } // else another quad referencing the same proxy
Michael Ludwigce62dec2019-02-19 11:48:46 -0500567
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500568 SkMatrix ctm = viewMatrix;
Michael Ludwig379e4962019-12-06 13:21:26 -0500569 if (set[q].fPreViewMatrix) {
570 ctm.preConcat(*set[q].fPreViewMatrix);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500571 }
572
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400573 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
574 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500575 DrawQuad quad;
Michael Ludwig379e4962019-12-06 13:21:26 -0500576 if (set[q].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500577 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400578
579 SkPoint srcPts[4];
Michael Ludwig379e4962019-12-06 13:21:26 -0500580 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500581 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400582 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500583 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
584 quad.fLocal = GrQuad(set[q].fSrcRect);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400585 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500586
Brian Salomone69b9ef2020-07-22 11:18:06 -0400587 if (netFilter != filter || netMM != mm) {
588 // The only way netFilter != filter is if linear is requested and we haven't yet
589 // found a quad that requires linear (so net is still nearest). Similar for mip
590 // mapping.
591 SkASSERT(netFilter <= filter);
592 SkASSERT(netMM <= mm);
593 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad.fLocal, quad.fDevice);
594 if (mustFilter && filter != GrSamplerState::Filter::kNearest) {
595 netFilter = GrSamplerState::Filter::kLinear;
596 }
597 if (mustMM && mm != GrSamplerState::MipmapMode::kNone) {
598 netMM = GrSamplerState::MipmapMode::kLinear;
599 }
Michael Ludwig22429f92019-06-27 10:44:48 -0400600 }
601
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500602 // Update overall bounds of the op as the union of all quads
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500603 bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500604
605 // Determine the AA type for the quad, then merge with net AA type
Michael Ludwig6bee7762018-10-19 09:50:36 -0400606 GrAAType aaForQuad;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500607 GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
608 &aaForQuad, &quad.fEdgeFlags);
John Stilescbe4e282020-06-01 10:38:31 -0400609
Michael Ludwig6bee7762018-10-19 09:50:36 -0400610 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
611 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500612 if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
613 netAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400614 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400615
616 // Calculate metadata for the entry
Brian Salomon2432d062020-04-16 20:48:09 -0400617 const SkRect* subsetForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400618 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
John Stilescbe4e282020-06-01 10:38:31 -0400619 // Check (briefly) if the subset rect is actually needed for this set entry.
620 SkRect* subsetRect = &set[q].fSrcRect;
621 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
622 if (!safe_to_ignore_subset_rect(aaForQuad, filter, quad, *subsetRect)) {
623 netSubset = Subset::kYes;
624 subsetForQuad = subsetRect;
625 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400626 }
627 }
John Stilescbe4e282020-06-01 10:38:31 -0400628
629 // Normalize the src quads and apply origin
630 NormalizationParams proxyParams = proxy_normalization_params(
631 curProxy, set[q].fProxyView.origin());
632 normalize_src_quad(proxyParams, &quad.fLocal);
633
Brian Salomon2432d062020-04-16 20:48:09 -0400634 // This subset may represent a no-op, otherwise it will have the origin and dimensions
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500635 // of the texture applied to it. Insetting for bilinear filtering is deferred until
636 // on[Pre]Prepare so that the overall filter can be lazily determined.
Brian Salomon75cebbe2020-05-18 14:08:14 -0400637 SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500638
Michael Ludwig949ceb22020-02-07 10:14:45 -0500639 // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
640 // ViewCountPair (this frequently happens when Chrome draws 9-patches).
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400641 fViewCountPairs[p].fQuadCnt += this->appendQuad(&quad, set[q].fColor, subset);
Brian Salomond7065e72018-10-12 11:42:02 -0400642 }
Michael Ludwig406172a2019-12-06 14:05:19 -0500643 // The # of proxy switches should match what was provided (+1 because we incremented p
Michael Ludwig379e4962019-12-06 13:21:26 -0500644 // when a new proxy was encountered).
Michael Ludwig406172a2019-12-06 14:05:19 -0500645 SkASSERT((p + 1) == fMetadata.fProxyCount);
Michael Ludwig379e4962019-12-06 13:21:26 -0500646 SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500647
Michael Ludwig4384f042019-12-05 10:30:35 -0500648 fMetadata.fAAType = static_cast<uint16_t>(netAAType);
649 fMetadata.fFilter = static_cast<uint16_t>(netFilter);
Brian Salomon2432d062020-04-16 20:48:09 -0400650 fMetadata.fSubset = static_cast<uint16_t>(netSubset);
Brian Salomon34169692017-08-28 15:32:01 -0400651
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500652 this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage), IsHairline::kNo);
Brian Salomon17031a72018-05-22 14:14:07 -0400653 }
654
Brian Salomon2432d062020-04-16 20:48:09 -0400655 int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& subset) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500656 DrawQuad extra;
Michael Ludwig465864c2020-02-10 09:30:04 -0500657 // Only clip when there's anti-aliasing. When non-aa, the GPU clips just fine and there's
658 // no inset/outset math that requires w > 0.
659 int quadCount = quad->fEdgeFlags != GrQuadAAFlags::kNone ?
660 GrQuadUtils::ClipToW0(quad, &extra) : 1;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500661 if (quadCount == 0) {
662 // We can't discard the op at this point, but disable AA flags so it won't go through
663 // inset/outset processing
664 quad->fEdgeFlags = GrQuadAAFlags::kNone;
665 quadCount = 1;
666 }
Brian Salomon2432d062020-04-16 20:48:09 -0400667 fQuads.append(quad->fDevice, {color, subset, quad->fEdgeFlags}, &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500668 if (quadCount > 1) {
Brian Salomon2432d062020-04-16 20:48:09 -0400669 fQuads.append(extra.fDevice, {color, subset, extra.fEdgeFlags}, &extra.fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500670 fMetadata.fTotalQuadCount++;
671 }
672 return quadCount;
673 }
674
Robert Phillips2669a7b2020-03-12 12:07:19 -0400675 GrProgramInfo* programInfo() override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600676 // Although this Op implements its own onPrePrepareDraws it calls GrMeshDrawOps' version so
677 // this entry point will be called.
678 return (fDesc) ? fDesc->fProgramInfo : nullptr;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400679 }
680
Chris Daltondbb833b2020-03-17 12:15:46 -0600681 void onCreateProgramInfo(const GrCaps* caps,
682 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400683 const GrSurfaceProxyView* writeView,
Chris Daltondbb833b2020-03-17 12:15:46 -0600684 GrAppliedClip&& appliedClip,
685 const GrXferProcessor::DstProxyView& dstProxyView) override {
686 SkASSERT(fDesc);
687
688 GrGeometryProcessor* gp;
689
690 {
691 const GrBackendFormat& backendFormat =
692 fViewCountPairs[0].fProxy->backendFormat();
693
694 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
695 fMetadata.filter());
696
697 gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
698 arena, fDesc->fVertexSpec, *caps->shaderCaps(), backendFormat, samplerState,
699 fMetadata.fSwizzle, std::move(fTextureColorSpaceXform), fMetadata.saturate());
700
701 SkASSERT(fDesc->fVertexSpec.vertexSize() == gp->vertexStride());
702 }
703
704 auto pipelineFlags = (GrAAType::kMSAA == fMetadata.aaType()) ?
705 GrPipeline::InputFlags::kHWAntialias : GrPipeline::InputFlags::kNone;
706
707 fDesc->fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Brian Salomon8afde5f2020-04-01 16:22:00 -0400708 caps, arena, writeView, std::move(appliedClip), dstProxyView, gp,
Chris Daltondbb833b2020-03-17 12:15:46 -0600709 GrProcessorSet::MakeEmptySet(), fDesc->fVertexSpec.primitiveType(),
710 pipelineFlags);
Robert Phillips4133dc42020-03-11 15:55:55 -0400711 }
712
Robert Phillipsdf70f152019-11-15 14:57:05 -0500713 void onPrePrepareDraws(GrRecordingContext* context,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400714 const GrSurfaceProxyView* writeView,
Robert Phillips8053c972019-11-21 10:44:53 -0500715 GrAppliedClip* clip,
716 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips61fc7992019-10-22 11:58:17 -0400717 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips29f38542019-10-16 09:20:25 -0400718
Robert Phillips61fc7992019-10-22 11:58:17 -0400719 SkDEBUGCODE(this->validate();)
Chris Daltondbb833b2020-03-17 12:15:46 -0600720 SkASSERT(!fDesc);
Robert Phillips61fc7992019-10-22 11:58:17 -0400721
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500722 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillips61fc7992019-10-22 11:58:17 -0400723
Chris Daltondbb833b2020-03-17 12:15:46 -0600724 fDesc = arena->make<Desc>();
725 this->characterize(fDesc);
726 fDesc->allocatePrePreparedVertices(arena);
727 FillInVertices(*context->priv().caps(), this, fDesc, fDesc->fPrePreparedVertices);
Robert Phillips61fc7992019-10-22 11:58:17 -0400728
Chris Daltondbb833b2020-03-17 12:15:46 -0600729 // This will call onCreateProgramInfo and register the created program with the DDL.
Brian Salomon8afde5f2020-04-01 16:22:00 -0400730 this->INHERITED::onPrePrepareDraws(context, writeView, clip, dstProxyView);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400731 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400732
Chris Daltondbb833b2020-03-17 12:15:46 -0600733 static void FillInVertices(const GrCaps& caps, TextureOp* texOp, Desc* desc, char* vertexData) {
734 SkASSERT(vertexData);
735
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400736 int totQuadsSeen = 0;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400737 SkDEBUGCODE(int totVerticesSeen = 0;)
Michael Ludwig189c9802019-11-21 11:21:12 -0500738 SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400739
Chris Daltondbb833b2020-03-17 12:15:46 -0600740 GrQuadPerEdgeAA::Tessellator tessellator(desc->fVertexSpec, vertexData);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400741 for (const auto& op : ChainRange<TextureOp>(texOp)) {
742 auto iter = op.fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500743 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500744 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
745 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400746
Chris Daltondbb833b2020-03-17 12:15:46 -0600747 for (int i = 0; i < quadCnt && iter.next(); ++i) {
748 SkASSERT(iter.isLocalValid());
Brian Salomon2432d062020-04-16 20:48:09 -0400749 const ColorSubsetAndAA& info = iter.metadata();
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500750
Chris Daltondbb833b2020-03-17 12:15:46 -0600751 tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
Brian Salomon75cebbe2020-05-18 14:08:14 -0400752 info.fSubsetRect, info.aaFlags());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400753 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400754
Chris Daltondbb833b2020-03-17 12:15:46 -0600755 SkASSERT((totVerticesSeen + meshVertexCnt) * vertexSize
756 == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400757
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400758 totQuadsSeen += quadCnt;
759 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
760 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400761 }
762
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400763 // If quad counts per proxy were calculated correctly, the entire iterator
764 // should have been consumed.
Chris Daltondbb833b2020-03-17 12:15:46 -0600765 SkASSERT(!iter.next());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400766 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400767
Chris Daltondbb833b2020-03-17 12:15:46 -0600768 SkASSERT(desc->totalSizeInBytes() == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400769 SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
770 SkASSERT(totVerticesSeen == desc->totalNumVertices());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400771 }
772
Robert Phillips29f38542019-10-16 09:20:25 -0400773#ifdef SK_DEBUG
774 void validate() const override {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500775 // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
Michael Ludwigadb12e72019-12-04 16:19:18 -0500776 auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
777 GrAAType aaType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400778
Robert Phillipse837e612019-11-15 11:02:50 -0500779 int quadCount = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400780 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500781 SkASSERT(op.fMetadata.fSwizzle == fMetadata.fSwizzle);
782
783 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
784 auto* proxy = op.fViewCountPairs[p].fProxy->asTextureProxy();
Robert Phillipse837e612019-11-15 11:02:50 -0500785 quadCount += op.fViewCountPairs[p].fQuadCnt;
Robert Phillips29f38542019-10-16 09:20:25 -0400786 SkASSERT(proxy);
787 SkASSERT(proxy->textureType() == textureType);
Robert Phillips29f38542019-10-16 09:20:25 -0400788 }
789
790 // Each individual op must be a single aaType. kCoverage and kNone ops can chain
791 // together but kMSAA ones do not.
792 if (aaType == GrAAType::kCoverage || aaType == GrAAType::kNone) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500793 SkASSERT(op.fMetadata.aaType() == GrAAType::kCoverage ||
794 op.fMetadata.aaType() == GrAAType::kNone);
Robert Phillips29f38542019-10-16 09:20:25 -0400795 } else {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500796 SkASSERT(aaType == GrAAType::kMSAA && op.fMetadata.aaType() == GrAAType::kMSAA);
Robert Phillips29f38542019-10-16 09:20:25 -0400797 }
798 }
Robert Phillipse837e612019-11-15 11:02:50 -0500799
800 SkASSERT(quadCount == this->numChainedQuads());
Robert Phillips29f38542019-10-16 09:20:25 -0400801 }
802#endif
803
Robert Phillipse837e612019-11-15 11:02:50 -0500804#if GR_TEST_UTILS
805 int numQuads() const final { return this->totNumQuads(); }
806#endif
807
Chris Daltondbb833b2020-03-17 12:15:46 -0600808 void characterize(Desc* desc) const {
Robert Phillips29f38542019-10-16 09:20:25 -0400809 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
810 ColorType colorType = ColorType::kNone;
811 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomon2432d062020-04-16 20:48:09 -0400812 Subset subset = Subset::kNo;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500813 GrAAType overallAAType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400814
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400815 desc->fNumProxies = 0;
816 desc->fNumTotalQuads = 0;
817 int maxQuadsPerMesh = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400818
Brian Salomonf7232642018-09-19 08:58:08 -0400819 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400820 if (op.fQuads.deviceQuadType() > quadType) {
821 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500822 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400823 if (op.fQuads.localQuadType() > srcQuadType) {
824 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500825 }
Brian Salomon2432d062020-04-16 20:48:09 -0400826 if (op.fMetadata.subset() == Subset::kYes) {
827 subset = Subset::kYes;
Brian Salomonf7232642018-09-19 08:58:08 -0400828 }
Brian Osman788b9162020-02-07 10:36:46 -0500829 colorType = std::max(colorType, op.fMetadata.colorType());
Michael Ludwigadb12e72019-12-04 16:19:18 -0500830 desc->fNumProxies += op.fMetadata.fProxyCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400831
Michael Ludwigadb12e72019-12-04 16:19:18 -0500832 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Brian Osman788b9162020-02-07 10:36:46 -0500833 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
Brian Salomonf7232642018-09-19 08:58:08 -0400834 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400835 desc->fNumTotalQuads += op.totNumQuads();
836
Michael Ludwigadb12e72019-12-04 16:19:18 -0500837 if (op.fMetadata.aaType() == GrAAType::kCoverage) {
Robert Phillips29f38542019-10-16 09:20:25 -0400838 overallAAType = GrAAType::kCoverage;
Brian Salomonae7d7702018-10-14 15:05:45 -0400839 }
Brian Salomon34169692017-08-28 15:32:01 -0400840 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400841
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400842 SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
843
844 SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
845
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400846 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(overallAAType,
847 maxQuadsPerMesh);
848
849 desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
Brian Salomon2432d062020-04-16 20:48:09 -0400850 subset, overallAAType, /* alpha as coverage */ true,
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400851 indexBufferOption);
Robert Phillipse837e612019-11-15 11:02:50 -0500852
853 SkASSERT(desc->fNumTotalQuads <= GrQuadPerEdgeAA::QuadLimit(indexBufferOption));
Robert Phillips29f38542019-10-16 09:20:25 -0400854 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500855
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400856 int totNumQuads() const {
857#ifdef SK_DEBUG
858 int tmp = 0;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500859 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400860 tmp += fViewCountPairs[p].fQuadCnt;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400861 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500862 SkASSERT(tmp == fMetadata.fTotalQuadCount);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400863#endif
864
Michael Ludwigadb12e72019-12-04 16:19:18 -0500865 return fMetadata.fTotalQuadCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400866 }
867
868 int numChainedQuads() const {
869 int numChainedQuads = this->totNumQuads();
870
871 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
872 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
873 }
874
875 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
876 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
877 }
878
879 return numChainedQuads;
880 }
881
Robert Phillips29f38542019-10-16 09:20:25 -0400882 // onPrePrepareDraws may or may not have been called at this point
883 void onPrepareDraws(Target* target) override {
884 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500885
Robert Phillips29f38542019-10-16 09:20:25 -0400886 SkDEBUGCODE(this->validate();)
887
Chris Daltondbb833b2020-03-17 12:15:46 -0600888 SkASSERT(!fDesc || fDesc->fPrePreparedVertices);
Robert Phillips29f38542019-10-16 09:20:25 -0400889
Chris Daltondbb833b2020-03-17 12:15:46 -0600890 if (!fDesc) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400891 SkArenaAlloc* arena = target->allocator();
Chris Daltondbb833b2020-03-17 12:15:46 -0600892 fDesc = arena->make<Desc>();
893 this->characterize(fDesc);
894 SkASSERT(!fDesc->fPrePreparedVertices);
Brian Salomonf7232642018-09-19 08:58:08 -0400895 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400896
Chris Daltondbb833b2020-03-17 12:15:46 -0600897 size_t vertexSize = fDesc->fVertexSpec.vertexSize();
Brian Salomon92be2f72018-06-19 14:33:47 -0400898
Chris Daltondbb833b2020-03-17 12:15:46 -0600899 void* vdata = target->makeVertexSpace(vertexSize, fDesc->totalNumVertices(),
900 &fDesc->fVertexBuffer, &fDesc->fBaseVertex);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400901 if (!vdata) {
902 SkDebugf("Could not allocate vertices\n");
903 return;
Brian Salomon34169692017-08-28 15:32:01 -0400904 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400905
Chris Daltondbb833b2020-03-17 12:15:46 -0600906 if (fDesc->fVertexSpec.needsIndexBuffer()) {
907 fDesc->fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(
908 target, fDesc->fVertexSpec.indexBufferOption());
909 if (!fDesc->fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400910 SkDebugf("Could not allocate indices\n");
911 return;
912 }
913 }
914
Chris Daltondbb833b2020-03-17 12:15:46 -0600915 if (fDesc->fPrePreparedVertices) {
916 memcpy(vdata, fDesc->fPrePreparedVertices, fDesc->totalSizeInBytes());
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400917 } else {
Chris Daltondbb833b2020-03-17 12:15:46 -0600918 FillInVertices(target->caps(), this, fDesc, (char*) vdata);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400919 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700920 }
921
922 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600923 if (!fDesc->fVertexBuffer) {
924 return;
925 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500926
Chris Daltondbb833b2020-03-17 12:15:46 -0600927 if (fDesc->fVertexSpec.needsIndexBuffer() && !fDesc->fIndexBuffer) {
928 return;
929 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500930
Chris Daltondbb833b2020-03-17 12:15:46 -0600931 if (!fDesc->fProgramInfo) {
932 this->createProgramInfo(flushState);
933 SkASSERT(fDesc->fProgramInfo);
934 }
935
936 flushState->bindPipelineAndScissorClip(*fDesc->fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400937 flushState->bindBuffers(std::move(fDesc->fIndexBuffer), nullptr,
938 std::move(fDesc->fVertexBuffer));
Chris Daltondbb833b2020-03-17 12:15:46 -0600939
940 int totQuadsSeen = 0;
941 SkDEBUGCODE(int numDraws = 0;)
942 for (const auto& op : ChainRange<TextureOp>(this)) {
943 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
944 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
945 SkASSERT(numDraws < fDesc->fNumProxies);
946 flushState->bindTextures(fDesc->fProgramInfo->primProc(),
947 *op.fViewCountPairs[p].fProxy,
948 fDesc->fProgramInfo->pipeline());
949 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(),
950 fDesc->fVertexSpec, totQuadsSeen, quadCnt,
951 fDesc->totalNumVertices(), fDesc->fBaseVertex);
952 totQuadsSeen += quadCnt;
953 SkDEBUGCODE(++numDraws;)
954 }
955 }
956
957 SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
958 SkASSERT(numDraws == fDesc->fNumProxies);
Brian Salomon34169692017-08-28 15:32:01 -0400959 }
960
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500961 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
962 const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400963 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400964 const auto* that = t->cast<TextureOp>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400965
Chris Daltondbb833b2020-03-17 12:15:46 -0600966 if (fDesc || that->fDesc) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400967 // This should never happen (since only DDL recorded ops should be prePrepared)
968 // but, in any case, we should never combine ops that that been prePrepared
969 return CombineResult::kCannotCombine;
970 }
971
Brian Salomon2432d062020-04-16 20:48:09 -0400972 if (fMetadata.subset() != that->fMetadata.subset()) {
973 // It is technically possible to combine operations across subset modes, but performance
Michael Ludwig2929f512019-04-19 13:05:56 -0400974 // testing suggests it's better to make more draw calls where some take advantage of
975 // the more optimal shader path without coordinate clamping.
976 return CombineResult::kCannotCombine;
977 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400978 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
979 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000980 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400981 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400982
Brian Salomonae7d7702018-10-14 15:05:45 -0400983 bool upgradeToCoverageAAOnMerge = false;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500984 if (fMetadata.aaType() != that->fMetadata.aaType()) {
985 if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400986 return CombineResult::kCannotCombine;
987 }
988 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500989 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400990
Michael Ludwigadb12e72019-12-04 16:19:18 -0500991 if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400992 this->numChainedQuads() + that->numChainedQuads())) {
993 return CombineResult::kCannotCombine;
Robert Phillipsb69001f2019-10-29 12:16:35 -0400994 }
995
Michael Ludwigadb12e72019-12-04 16:19:18 -0500996 if (fMetadata.saturate() != that->fMetadata.saturate()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400997 return CombineResult::kCannotCombine;
998 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500999 if (fMetadata.filter() != that->fMetadata.filter()) {
Brian Salomonf7232642018-09-19 08:58:08 -04001000 return CombineResult::kCannotCombine;
1001 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001002 if (fMetadata.mipmapMode() != that->fMetadata.mipmapMode()) {
1003 return CombineResult::kCannotCombine;
1004 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001005 if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
1006 return CombineResult::kCannotCombine;
1007 }
1008 const auto* thisProxy = fViewCountPairs[0].fProxy.get();
1009 const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
1010 if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
1011 thisProxy != thatProxy) {
Brian Salomon588cec72018-11-14 13:56:37 -05001012 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -05001013 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Michael Ludwigadb12e72019-12-04 16:19:18 -05001014 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
Brian Salomonf7232642018-09-19 08:58:08 -04001015 return CombineResult::kMayChain;
1016 }
Brian Salomon7eae3e02018-08-07 14:02:38 +00001017 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001018 }
Michael Ludwig009b92e2019-02-15 16:03:53 -05001019
Brian Salomon2432d062020-04-16 20:48:09 -04001020 fMetadata.fSubset |= that->fMetadata.fSubset;
Brian Osman788b9162020-02-07 10:36:46 -05001021 fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -04001022 if (upgradeToCoverageAAOnMerge) {
Michael Ludwig4384f042019-12-05 10:30:35 -05001023 fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
Brian Salomonae7d7702018-10-14 15:05:45 -04001024 }
Michael Ludwig009b92e2019-02-15 16:03:53 -05001025
Michael Ludwig425eb452019-06-27 10:13:27 -04001026 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -05001027 fQuads.concat(that->fQuads);
Greg Daniel549325c2019-10-30 16:19:20 -04001028 fViewCountPairs[0].fQuadCnt += that->fQuads.count();
Michael Ludwigadb12e72019-12-04 16:19:18 -05001029 fMetadata.fTotalQuadCount += that->fQuads.count();
Michael Ludwig009b92e2019-02-15 16:03:53 -05001030
Brian Salomon7eae3e02018-08-07 14:02:38 +00001031 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -04001032 }
1033
Brian Salomon2432d062020-04-16 20:48:09 -04001034 GrQuadBuffer<ColorSubsetAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -04001035 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Chris Daltondbb833b2020-03-17 12:15:46 -06001036 // Most state of TextureOp is packed into these two field to minimize the op's size.
Michael Ludwigadb12e72019-12-04 16:19:18 -05001037 // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1038 // consider/measure changes with care.
Chris Daltondbb833b2020-03-17 12:15:46 -06001039 Desc* fDesc;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001040 Metadata fMetadata;
Robert Phillips32803ff2019-10-23 08:26:08 -04001041
1042 // This field must go last. When allocating this op, we will allocate extra space to hold
Greg Daniel549325c2019-10-30 16:19:20 -04001043 // additional ViewCountPairs immediately after the op's allocation so we can treat this
Robert Phillips32803ff2019-10-23 08:26:08 -04001044 // as an fProxyCnt-length array.
Greg Daniel549325c2019-10-30 16:19:20 -04001045 ViewCountPair fViewCountPairs[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -04001046
Brian Salomon34169692017-08-28 15:32:01 -04001047 typedef GrMeshDrawOp INHERITED;
1048};
1049
1050} // anonymous namespace
1051
Robert Phillipse837e612019-11-15 11:02:50 -05001052#if GR_TEST_UTILS
1053uint32_t GrTextureOp::ClassID() {
1054 return TextureOp::ClassID();
1055}
1056#endif
Brian Salomon34169692017-08-28 15:32:01 -04001057
Robert Phillipse837e612019-11-15 11:02:50 -05001058std::unique_ptr<GrDrawOp> GrTextureOp::Make(GrRecordingContext* context,
1059 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -05001060 SkAlphaType alphaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001061 sk_sp<GrColorSpaceXform> textureXform,
1062 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001063 GrSamplerState::MipmapMode mm,
Robert Phillipse837e612019-11-15 11:02:50 -05001064 const SkPMColor4f& color,
1065 Saturate saturate,
1066 SkBlendMode blendMode,
1067 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001068 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -04001069 const SkRect* subset) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001070 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
Brian Salomon2432d062020-04-16 20:48:09 -04001071 if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1072 // No need for a shader-based subset if hardware clamping achieves the same effect
1073 subset = nullptr;
Michael Ludwig22429f92019-06-27 10:44:48 -04001074 }
1075
Brian Salomone69b9ef2020-07-22 11:18:06 -04001076 if (filter != GrSamplerState::Filter::kNearest || mm != GrSamplerState::MipmapMode::kNone) {
1077 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad->fLocal, quad->fDevice);
1078 if (!mustFilter) {
1079 filter = GrSamplerState::Filter::kNearest;
1080 }
1081 if (!mustMM) {
1082 mm = GrSamplerState::MipmapMode::kNone;
1083 }
Michael Ludwig22429f92019-06-27 10:44:48 -04001084 }
1085
1086 if (blendMode == SkBlendMode::kSrcOver) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001087 return TextureOp::Make(context, std::move(proxyView), std::move(textureXform), filter, mm,
Brian Salomon2432d062020-04-16 20:48:09 -04001088 color, saturate, aaType, std::move(quad), subset);
Michael Ludwig22429f92019-06-27 10:44:48 -04001089 } else {
1090 // Emulate complex blending using GrFillRectOp
1091 GrPaint paint;
1092 paint.setColor4f(color);
1093 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1094
1095 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon2432d062020-04-16 20:48:09 -04001096 if (subset) {
Brian Salomonca6b2f42020-01-24 11:31:21 -05001097 const auto& caps = *context->priv().caps();
1098 SkRect localRect;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001099 if (quad->fLocal.asRect(&localRect)) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001100 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
1101 filter, *subset, localRect, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001102 } else {
John Stiles5a2a7b32020-06-04 10:57:21 -04001103 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
1104 filter, *subset, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001105 }
1106 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -05001107 fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), filter);
Michael Ludwig22429f92019-06-27 10:44:48 -04001108 }
Brian Osmanf48f76e2020-07-15 16:04:17 -04001109 fp = GrXfermodeFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Michael Ludwig22429f92019-06-27 10:44:48 -04001110 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001111 if (saturate == GrTextureOp::Saturate::kYes) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001112 fp = GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/false);
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001113 }
John Stiles5933d7d2020-07-21 12:28:35 -04001114 paint.setColorFragmentProcessor(std::move(fp));
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001115 return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
Michael Ludwig22429f92019-06-27 10:44:48 -04001116 }
1117}
1118
Robert Phillipse837e612019-11-15 11:02:50 -05001119// A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1120class GrTextureOp::BatchSizeLimiter {
1121public:
1122 BatchSizeLimiter(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001123 const GrClip* clip,
Robert Phillipse837e612019-11-15 11:02:50 -05001124 GrRecordingContext* context,
1125 int numEntries,
1126 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001127 GrSamplerState::MipmapMode mm,
Robert Phillipse837e612019-11-15 11:02:50 -05001128 GrTextureOp::Saturate saturate,
1129 SkCanvas::SrcRectConstraint constraint,
1130 const SkMatrix& viewMatrix,
1131 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
1132 : fRTC(rtc)
1133 , fClip(clip)
1134 , fContext(context)
1135 , fFilter(filter)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001136 , fMipmapMode(mm)
Robert Phillipse837e612019-11-15 11:02:50 -05001137 , fSaturate(saturate)
1138 , fConstraint(constraint)
1139 , fViewMatrix(viewMatrix)
1140 , fTextureColorSpaceXform(textureColorSpaceXform)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001141 , fNumLeft(numEntries) {}
Brian Salomon34169692017-08-28 15:32:01 -04001142
Michael Ludwigadb12e72019-12-04 16:19:18 -05001143 void createOp(GrRenderTargetContext::TextureSetEntry set[],
Robert Phillipse837e612019-11-15 11:02:50 -05001144 int clumpSize,
1145 GrAAType aaType) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001146 int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
Brian Salomone69b9ef2020-07-22 11:18:06 -04001147 std::unique_ptr<GrDrawOp> op = TextureOp::Make(fContext,
1148 &set[fNumClumped],
1149 clumpSize,
1150 clumpProxyCount,
1151 fFilter,
1152 fMipmapMode,
1153 fSaturate,
1154 aaType,
1155 fConstraint,
1156 fViewMatrix,
Robert Phillipse837e612019-11-15 11:02:50 -05001157 fTextureColorSpaceXform);
1158 fRTC->addDrawOp(fClip, std::move(op));
1159
1160 fNumLeft -= clumpSize;
1161 fNumClumped += clumpSize;
1162 }
1163
1164 int numLeft() const { return fNumLeft; }
1165 int baseIndex() const { return fNumClumped; }
1166
1167private:
1168 GrRenderTargetContext* fRTC;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001169 const GrClip* fClip;
Robert Phillipse837e612019-11-15 11:02:50 -05001170 GrRecordingContext* fContext;
1171 GrSamplerState::Filter fFilter;
Brian Salomone69b9ef2020-07-22 11:18:06 -04001172 GrSamplerState::MipmapMode fMipmapMode;
Robert Phillipse837e612019-11-15 11:02:50 -05001173 GrTextureOp::Saturate fSaturate;
1174 SkCanvas::SrcRectConstraint fConstraint;
1175 const SkMatrix& fViewMatrix;
1176 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
1177
1178 int fNumLeft;
1179 int fNumClumped = 0; // also the offset for the start of the next clump
1180};
1181
1182// Greedily clump quad draws together until the index buffer limit is exceeded.
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001183void GrTextureOp::AddTextureSetOps(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001184 const GrClip* clip,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001185 GrRecordingContext* context,
Michael Ludwigadb12e72019-12-04 16:19:18 -05001186 GrRenderTargetContext::TextureSetEntry set[],
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001187 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -05001188 int proxyRunCnt,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001189 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001190 GrSamplerState::MipmapMode mm,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001191 Saturate saturate,
1192 SkBlendMode blendMode,
1193 GrAAType aaType,
1194 SkCanvas::SrcRectConstraint constraint,
1195 const SkMatrix& viewMatrix,
1196 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001197 // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1198 // the op's metadata so we don't need to worry about overflow.
Michael Ludwig4ef1ca12019-12-19 10:58:52 -05001199 SkDEBUGCODE(TextureOp::ValidateResourceLimits();)
Michael Ludwig379e4962019-12-06 13:21:26 -05001200 SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1201
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001202 // First check if we can support batches as a single op
1203 if (blendMode != SkBlendMode::kSrcOver ||
1204 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1205 // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1206 // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1207 // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1208 SkMatrix ctm;
1209 for (int i = 0; i < cnt; ++i) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001210 ctm = viewMatrix;
1211 if (set[i].fPreViewMatrix) {
1212 ctm.preConcat(*set[i].fPreViewMatrix);
1213 }
Robert Phillipse837e612019-11-15 11:02:50 -05001214
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001215 DrawQuad quad;
1216 quad.fEdgeFlags = set[i].fAAFlags;
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001217 if (set[i].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001218 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001219
1220 SkPoint srcPts[4];
1221 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001222 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001223 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001224 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1225 quad.fLocal = GrQuad(set[i].fSrcRect);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001226 }
1227
Brian Salomon2432d062020-04-16 20:48:09 -04001228 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001229 ? &set[i].fSrcRect : nullptr;
1230
Brian Salomonfc118442019-11-22 19:09:27 -05001231 auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001232 filter, mm, set[i].fColor, saturate, blendMode, aaType, &quad, subset);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001233 rtc->addDrawOp(clip, std::move(op));
1234 }
1235 return;
1236 }
1237
1238 // Second check if we can always just make a single op and avoid the extra iteration
Robert Phillipse837e612019-11-15 11:02:50 -05001239 // needed to clump things together.
Brian Osman788b9162020-02-07 10:36:46 -05001240 if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
Robert Phillipse837e612019-11-15 11:02:50 -05001241 GrResourceProvider::MaxNumAAQuads())) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001242 auto op = TextureOp::Make(context, set, cnt, proxyRunCnt, filter, mm, saturate, aaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001243 constraint, viewMatrix, std::move(textureColorSpaceXform));
1244 rtc->addDrawOp(clip, std::move(op));
1245 return;
1246 }
1247
Brian Salomone69b9ef2020-07-22 11:18:06 -04001248 BatchSizeLimiter state(rtc, clip, context, cnt, filter, mm, saturate, constraint, viewMatrix,
Robert Phillipse837e612019-11-15 11:02:50 -05001249 std::move(textureColorSpaceXform));
1250
1251 // kNone and kMSAA never get altered
1252 if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1253 // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1254 while (state.numLeft() > 0) {
Brian Osman788b9162020-02-07 10:36:46 -05001255 int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
Robert Phillipse837e612019-11-15 11:02:50 -05001256
1257 state.createOp(set, clumpSize, aaType);
1258 }
1259 } else {
1260 // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1261 // can also get downgraded to kNone if all the quads are on integer coordinates and
1262 // axis-aligned.
1263 SkASSERT(aaType == GrAAType::kCoverage);
1264
1265 while (state.numLeft() > 0) {
1266 GrAAType runningAA = GrAAType::kNone;
1267 bool clumped = false;
1268
1269 for (int i = 0; i < state.numLeft(); ++i) {
1270 int absIndex = state.baseIndex() + i;
1271
1272 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone) {
1273
1274 if (i >= GrResourceProvider::MaxNumAAQuads()) {
1275 // Here we either need to boost the AA type to kCoverage, but doing so with
1276 // all the accumulated quads would overflow, or we have a set of AA quads
1277 // that has just gotten too large. In either case, calve off the existing
1278 // quads as their own TextureOp.
1279 state.createOp(
1280 set,
1281 runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1282 runningAA); // maybe downgrading AA here
1283 clumped = true;
1284 break;
1285 }
1286
1287 runningAA = GrAAType::kCoverage;
1288 } else if (runningAA == GrAAType::kNone) {
1289
1290 if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1291 // Here we've found a consistent batch of non-AA quads that has gotten too
1292 // large. Calve it off as its own GrTextureOp.
1293 state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1294 GrAAType::kNone); // definitely downgrading AA here
1295 clumped = true;
1296 break;
1297 }
1298 }
1299 }
1300
1301 if (!clumped) {
1302 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1303 // quads and call it a day.
1304 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1305 }
1306 }
1307 }
1308}
Robert Phillipsae01f622019-11-13 15:56:31 +00001309
Brian Salomon34169692017-08-28 15:32:01 -04001310#if GR_TEST_UTILS
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04001311#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001312#include "src/gpu/GrProxyProvider.h"
1313#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -04001314
1315GR_DRAW_OP_TEST_DEFINE(TextureOp) {
Brian Salomona56a7462020-02-07 14:17:25 -05001316 SkISize dims;
1317 dims.fHeight = random->nextULessThan(90) + 10;
1318 dims.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001319 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001320 GrMipmapped mipMapped = random->nextBool() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel09c94002018-06-08 22:11:51 +00001321 SkBackingFit fit = SkBackingFit::kExact;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001322 if (mipMapped == GrMipmapped::kNo) {
Greg Daniel09c94002018-06-08 22:11:51 +00001323 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1324 }
Greg Daniel4065d452018-11-16 15:43:41 -05001325 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -04001326 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1327 GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -05001328 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -04001329 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001330 format, dims, GrRenderable::kNo, 1, mipMapped, fit, SkBudgeted::kNo, GrProtected::kNo,
1331 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001332
Brian Salomon34169692017-08-28 15:32:01 -04001333 SkRect rect = GrTest::TestRect(random);
1334 SkRect srcRect;
1335 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1336 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1337 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1338 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1339 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -05001340 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001341 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
Brian Salomone69b9ef2020-07-22 11:18:06 -04001342 static_cast<uint32_t>(GrSamplerState::Filter::kLast) + 1);
1343 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
1344 if (mipMapped == GrMipmapped::kYes) {
1345 mm = (GrSamplerState::MipmapMode)random->nextULessThan(
1346 static_cast<uint32_t>(GrSamplerState::MipmapMode::kLast) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +00001347 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001348
Brian Osman3ebd3542018-07-30 14:36:53 -04001349 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001350 GrAAType aaType = GrAAType::kNone;
1351 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06001352 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -05001353 }
Brian Salomon2213ee92018-10-02 10:44:21 -04001354 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1355 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1356 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1357 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1358 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomon2432d062020-04-16 20:48:09 -04001359 bool useSubset = random->nextBool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001360 auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
Greg Daniel549325c2019-10-30 16:19:20 -04001361 GrSurfaceProxyView proxyView(
1362 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -05001363 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Brian Salomonfc118442019-11-22 19:09:27 -05001364 auto alphaType = static_cast<SkAlphaType>(
1365 random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
Greg Daniel549325c2019-10-30 16:19:20 -04001366
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001367 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
Brian Salomonfc118442019-11-22 19:09:27 -05001368 return GrTextureOp::Make(context, std::move(proxyView), alphaType, std::move(texXform), filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001369 mm, color, saturate, SkBlendMode::kSrcOver, aaType, &quad,
1370 useSubset ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -04001371}
1372
1373#endif