blob: 46a93da8d5112485914a7ff963bc7040e4685751 [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040030#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/SkGr.h"
John Stilesf743d4e2020-07-23 11:35:08 -040032#include "src/gpu/effects/GrBlendFragmentProcessor.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
Brian Salomone69b9ef2020-07-22 11:18:06 -040060static std::tuple<bool /* filter */,
61 bool /* mipmap */>
62filter_and_mm_have_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
Michael Ludwig22429f92019-06-27 10:44:48 -040063 // If not axis-aligned in src or dst, then always say it has an effect
64 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
65 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040066 return {true, true};
Michael Ludwig22429f92019-06-27 10:44:48 -040067 }
68
69 SkRect srcRect;
70 SkRect dstRect;
71 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
72 // Disable filtering when there is no scaling (width and height are the same), and the
73 // top-left corners have the same fraction (so src and dst snap to the pixel grid
74 // identically).
75 SkASSERT(srcRect.isSorted());
Brian Salomone69b9ef2020-07-22 11:18:06 -040076 bool filter = srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
77 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
78 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
79 bool mm = srcRect.width() > dstRect.width() || srcRect.height() > dstRect.height();
80 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040081 }
Brian Salomone69b9ef2020-07-22 11:18:06 -040082 // Extract edge lengths
83 SkSize srcSize = axis_aligned_quad_size(srcQuad);
84 SkSize dstSize = axis_aligned_quad_size(dstQuad);
85 // Although the quads are axis-aligned, the local coordinate system is transformed such
86 // that fractionally-aligned sample centers will not align with the device coordinate system
87 // So disable filtering when edges are the same length and both srcQuad and dstQuad
88 // 0th vertex is integer aligned.
89 bool filter = srcSize != dstSize ||
90 !SkScalarIsInt(srcQuad.x(0)) ||
91 !SkScalarIsInt(srcQuad.y(0)) ||
92 !SkScalarIsInt(dstQuad.x(0)) ||
93 !SkScalarIsInt(dstQuad.y(0));
94 bool mm = srcSize.fWidth > dstSize.fWidth || srcSize.fHeight > dstSize.fHeight;
95 return {filter, mm};
Michael Ludwig22429f92019-06-27 10:44:48 -040096}
97
Michael Ludwig119ac6d2019-11-21 09:26:46 -050098// Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
99// regular and rectangular textures, w/ or w/o origin correction.
100struct NormalizationParams {
101 float fIW; // 1 / width of texture, or 1.0 for texture rectangles
Michael Ludwigc453a502020-05-29 12:29:12 -0400102 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 -0500103 float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
104};
Michael Ludwigadb12e72019-12-04 16:19:18 -0500105static NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
106 GrSurfaceOrigin origin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500107 // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
108 // normalize the src coordinates up front.
Michael Ludwigadb12e72019-12-04 16:19:18 -0500109 SkISize dimensions = proxy->backingStoreDimensions();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500110 float iw, ih, h;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500111 if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500112 iw = ih = 1.f;
113 h = dimensions.height();
114 } else {
115 iw = 1.f / dimensions.width();
116 ih = 1.f / dimensions.height();
117 h = 1.f;
118 }
119
Michael Ludwigadb12e72019-12-04 16:19:18 -0500120 if (origin == kBottomLeft_GrSurfaceOrigin) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500121 return {iw, -ih, h};
122 } else {
123 return {iw, ih, 0.0f};
124 }
125}
126
Brian Salomon2432d062020-04-16 20:48:09 -0400127// Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500128// 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 -0400129// subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
130// and y-flipping are applied as indicated by NormalizationParams.
131static SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
132 const NormalizationParams& params,
133 const SkRect* subsetRect) {
Brian Salomon246bc3d2018-12-06 15:33:02 -0500134 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Brian Salomon2432d062020-04-16 20:48:09 -0400135 if (!subsetRect) {
136 // Either the quad has no subset constraint and is batched with a subset constrained op
137 // (in which case we want a subset that doesn't restrict normalized tex coords), or the
138 // entire op doesn't use the subset, in which case the returned value is ignored.
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500139 return kLargeRect;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400140 }
141
Brian Salomon2432d062020-04-16 20:48:09 -0400142 auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400143 auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
144 if (filter == GrSamplerState::Filter::kNearest) {
145 // Make sure our insetting puts us at pixel centers.
146 ltrb = skvx::floor(ltrb*flipHi)*flipHi;
147 }
148 // Inset with pin to the rect center.
149 ltrb += skvx::Vec<4, float>({.5f, .5f, -.5f, -.5f});
150 auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
151 ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
152
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500153 // Normalize and offset
Michael Ludwigc453a502020-05-29 12:29:12 -0400154 ltrb = mad(ltrb, {params.fIW, params.fInvH, params.fIW, params.fInvH},
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500155 {0.f, params.fYOffset, 0.f, params.fYOffset});
Michael Ludwigc453a502020-05-29 12:29:12 -0400156 if (params.fInvH < 0.f) {
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500157 // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
158 ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400159 }
160
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500161 SkRect out;
162 ltrb.store(&out);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500163 return out;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400164}
165
Michael Ludwig009b92e2019-02-15 16:03:53 -0500166// Normalizes logical src coords and corrects for origin
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500167static void normalize_src_quad(const NormalizationParams& params,
168 GrQuad* srcQuad) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500169 // The src quad should not have any perspective
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500170 SkASSERT(!srcQuad->hasPerspective());
171 skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
Michael Ludwigc453a502020-05-29 12:29:12 -0400172 skvx::Vec<4, float> ys = mad(srcQuad->y4f(), params.fInvH, params.fYOffset);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500173 xs.store(srcQuad->xs());
174 ys.store(srcQuad->ys());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500175}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400176
Michael Ludwig379e4962019-12-06 13:21:26 -0500177// Count the number of proxy runs in the entry set. This usually is already computed by
178// SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
179// for each split.
180static int proxy_run_count(const GrRenderTargetContext::TextureSetEntry set[], int count) {
181 int actualProxyRunCount = 0;
182 const GrSurfaceProxy* lastProxy = nullptr;
183 for (int i = 0; i < count; ++i) {
184 if (set[i].fProxyView.proxy() != lastProxy) {
185 actualProxyRunCount++;
186 lastProxy = set[i].fProxyView.proxy();
187 }
188 }
189 return actualProxyRunCount;
190}
191
John Stilescbe4e282020-06-01 10:38:31 -0400192static bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
193 const DrawQuad& quad, const SkRect& subsetRect) {
194 // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
195 // can push all the way up to the edges of the the subset rect and the sampler shouldn't
196 // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
197 // the non-antialiased case.
198 SkRect localBounds = quad.fLocal.bounds();
199 if (aaType == GrAAType::kNone &&
200 filter == GrSamplerState::Filter::kNearest &&
201 quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
202 quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
203 subsetRect.contains(localBounds)) {
204
205 return true;
206 }
207
208 // If the subset rect is inset by at least 0.5 pixels into the local quad's bounds, the
209 // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
210 if (subsetRect.makeInset(0.5f, 0.5f).contains(localBounds)) {
211 return true;
212 }
213
214 // The subset rect cannot be ignored safely.
215 return false;
216}
217
Brian Salomon34169692017-08-28 15:32:01 -0400218/**
219 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
220 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
221 */
222class TextureOp final : public GrMeshDrawOp {
223public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500224 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Greg Daniel549325c2019-10-30 16:19:20 -0400225 GrSurfaceProxyView proxyView,
Michael Ludwig22429f92019-06-27 10:44:48 -0400226 sk_sp<GrColorSpaceXform> textureXform,
Robert Phillips7c525e62018-06-12 10:11:12 -0400227 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400228 GrSamplerState::MipmapMode mm,
Brian Osman3d139a42018-11-19 10:42:10 -0500229 const SkPMColor4f& color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400230 GrTextureOp::Saturate saturate,
Robert Phillips7c525e62018-06-12 10:11:12 -0400231 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500232 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400233 const SkRect* subset) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500234 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomone69b9ef2020-07-22 11:18:06 -0400235 return pool->allocate<TextureOp>(std::move(proxyView), std::move(textureXform), filter, mm,
Brian Salomon2432d062020-04-16 20:48:09 -0400236 color, saturate, aaType, quad, subset);
Brian Salomon34169692017-08-28 15:32:01 -0400237 }
Robert Phillipse837e612019-11-15 11:02:50 -0500238
Robert Phillipsb97da532019-02-12 15:24:12 -0500239 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwigadb12e72019-12-04 16:19:18 -0500240 GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400241 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500242 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400243 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400244 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400245 GrTextureOp::Saturate saturate,
246 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400247 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500248 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500249 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -0500250 // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
251 SkASSERT(proxyRunCnt <= cnt);
252
253 size_t size = sizeof(TextureOp) + sizeof(ViewCountPair) * (proxyRunCnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500254 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400255 void* mem = pool->allocate(size);
Michael Ludwig379e4962019-12-06 13:21:26 -0500256 return std::unique_ptr<GrDrawOp>(
Brian Salomone69b9ef2020-07-22 11:18:06 -0400257 new (mem) TextureOp(set, cnt, proxyRunCnt, filter, mm, saturate, aaType, constraint,
Michael Ludwig379e4962019-12-06 13:21:26 -0500258 viewMatrix, std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400259 }
Brian Salomon34169692017-08-28 15:32:01 -0400260
Brian Salomon336ce7b2017-09-08 08:23:58 -0400261 ~TextureOp() override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500262 for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400263 fViewCountPairs[p].~ViewCountPair();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400264 }
265 }
Brian Salomon34169692017-08-28 15:32:01 -0400266
267 const char* name() const override { return "TextureOp"; }
268
Chris Dalton1706cbf2019-05-21 19:35:29 -0600269 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400270 bool mipped = (fMetadata.mipmapMode() != GrSamplerState::MipmapMode::kNone);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500271 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400272 func(fViewCountPairs[p].fProxy.get(), GrMipmapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400273 }
Chris Daltondbb833b2020-03-17 12:15:46 -0600274 if (fDesc && fDesc->fProgramInfo) {
275 fDesc->fProgramInfo->visitFPProxies(func);
276 }
Brian Salomond7065e72018-10-12 11:42:02 -0400277 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400278
Brian Osman9a390ac2018-11-12 09:47:48 -0500279#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400280 SkString dumpInfo() const override {
281 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400282 str.appendf("# draws: %d\n", fQuads.count());
Michael Ludwig425eb452019-06-27 10:13:27 -0400283 auto iter = fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500284 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400285 str.appendf("Proxy ID: %d, Filter: %d, MM: %d\n",
Michael Ludwigadb12e72019-12-04 16:19:18 -0500286 fViewCountPairs[p].fProxy->uniqueID().asUInt(),
Brian Salomone69b9ef2020-07-22 11:18:06 -0400287 static_cast<int>(fMetadata.fFilter),
288 static_cast<int>(fMetadata.fMipmapMode));
Michael Ludwig425eb452019-06-27 10:13:27 -0400289 int i = 0;
Greg Daniel549325c2019-10-30 16:19:20 -0400290 while(i < fViewCountPairs[p].fQuadCnt && iter.next()) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500291 const GrQuad* quad = iter.deviceQuad();
292 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
Brian Salomon2432d062020-04-16 20:48:09 -0400293 const ColorSubsetAndAA& info = iter.metadata();
Brian Salomond7065e72018-10-12 11:42:02 -0400294 str.appendf(
Brian Salomon2432d062020-04-16 20:48:09 -0400295 "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400296 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
297 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
Brian Salomon2432d062020-04-16 20:48:09 -0400298 i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
299 info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
Michael Ludwig704d5402019-11-25 09:43:37 -0500300 quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
301 quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400302 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
303 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
304
Michael Ludwig425eb452019-06-27 10:13:27 -0400305 i++;
Brian Salomond7065e72018-10-12 11:42:02 -0400306 }
Brian Salomon34169692017-08-28 15:32:01 -0400307 }
308 str += INHERITED::dumpInfo();
309 return str;
310 }
Michael Ludwig4ef1ca12019-12-19 10:58:52 -0500311
312 static void ValidateResourceLimits() {
313 // The op implementation has an upper bound on the number of quads that it can represent.
314 // However, the resource manager imposes its own limit on the number of quads, which should
315 // always be lower than the numerical limit this op can hold.
316 using CountStorage = decltype(Metadata::fTotalQuadCount);
317 CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
318 // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
319 int resourceLimit = SkTo<int>(maxQuadCount);
320 SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
321 GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
322 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500323#endif
Brian Salomon34169692017-08-28 15:32:01 -0400324
Brian Osman5ced0bf2019-03-15 10:15:29 -0400325 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600326 const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage,
327 GrClampType clampType) override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500328 SkASSERT(fMetadata.colorType() == ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400329 auto iter = fQuads.metadata();
330 while(iter.next()) {
Brian Osman2715bf52019-12-06 14:38:47 -0500331 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
Brian Osman788b9162020-02-07 10:36:46 -0500332 fMetadata.fColorType = std::max(fMetadata.fColorType, static_cast<uint16_t>(colorType));
Brian Osman8fa7ab42019-03-18 10:22:42 -0400333 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700334 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400335 }
336
Brian Salomon485b8c62018-01-12 15:11:06 -0500337 FixedFunctionFlags fixedFunctionFlags() const override {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500338 return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
339 : FixedFunctionFlags::kNone;
Brian Salomon485b8c62018-01-12 15:11:06 -0500340 }
Brian Salomon34169692017-08-28 15:32:01 -0400341
342 DEFINE_OP_CLASS_ID
343
344private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400345 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500346
Brian Salomon2432d062020-04-16 20:48:09 -0400347 struct ColorSubsetAndAA {
348 ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400349 : fColor(color)
Brian Salomon2432d062020-04-16 20:48:09 -0400350 , fSubsetRect(subsetRect)
Michael Ludwig4384f042019-12-05 10:30:35 -0500351 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
352 SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
Michael Ludwig425eb452019-06-27 10:13:27 -0400353 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400354
355 SkPMColor4f fColor;
Brian Salomon2432d062020-04-16 20:48:09 -0400356 // 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 -0500357 // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
Brian Salomon2432d062020-04-16 20:48:09 -0400358 SkRect fSubsetRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400359 unsigned fAAFlags : 4;
360
Michael Ludwig425eb452019-06-27 10:13:27 -0400361 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
362 };
Michael Ludwigadb12e72019-12-04 16:19:18 -0500363
Greg Daniel549325c2019-10-30 16:19:20 -0400364 struct ViewCountPair {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500365 // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
366 // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
367 // swizzle so that is stored in the op metadata.
368 sk_sp<GrSurfaceProxy> fProxy;
Michael Ludwig425eb452019-06-27 10:13:27 -0400369 int fQuadCnt;
370 };
371
Michael Ludwigadb12e72019-12-04 16:19:18 -0500372 // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
373 // increase the size of the op; increasing the op size can have a surprising impact on
374 // performance (since texture ops are one of the most commonly used in an app).
375 struct Metadata {
376 // AAType must be filled after initialization; ColorType is determined in finalize()
Brian Salomone69b9ef2020-07-22 11:18:06 -0400377 Metadata(const GrSwizzle& swizzle,
378 GrSamplerState::Filter filter,
379 GrSamplerState::MipmapMode mm,
380 GrQuadPerEdgeAA::Subset subset,
381 GrTextureOp::Saturate saturate)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500382 : fSwizzle(swizzle)
383 , fProxyCount(1)
384 , fTotalQuadCount(1)
Michael Ludwig4384f042019-12-05 10:30:35 -0500385 , fFilter(static_cast<uint16_t>(filter))
Brian Salomone69b9ef2020-07-22 11:18:06 -0400386 , fMipmapMode(static_cast<uint16_t>(mm))
Michael Ludwig4384f042019-12-05 10:30:35 -0500387 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
388 , fColorType(static_cast<uint16_t>(ColorType::kNone))
Brian Salomon2432d062020-04-16 20:48:09 -0400389 , fSubset(static_cast<uint16_t>(subset))
Michael Ludwig4384f042019-12-05 10:30:35 -0500390 , fSaturate(static_cast<uint16_t>(saturate)) {}
Michael Ludwigadb12e72019-12-04 16:19:18 -0500391
Michael Ludwig4384f042019-12-05 10:30:35 -0500392 GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
Michael Ludwigadb12e72019-12-04 16:19:18 -0500393 uint16_t fProxyCount;
394 // This will be >= fProxyCount, since a proxy may be drawn multiple times
395 uint16_t fTotalQuadCount;
396
Michael Ludwig4384f042019-12-05 10:30:35 -0500397 // These must be based on uint16_t to help MSVC's pack bitfields optimally
398 uint16_t fFilter : 2; // GrSamplerState::Filter
Brian Salomone69b9ef2020-07-22 11:18:06 -0400399 uint16_t fMipmapMode : 2; // GrSamplerState::MipmapMode
Michael Ludwig4384f042019-12-05 10:30:35 -0500400 uint16_t fAAType : 2; // GrAAType
401 uint16_t fColorType : 2; // GrQuadPerEdgeAA::ColorType
Brian Salomon2432d062020-04-16 20:48:09 -0400402 uint16_t fSubset : 1; // bool
Michael Ludwig4384f042019-12-05 10:30:35 -0500403 uint16_t fSaturate : 1; // bool
Brian Salomone69b9ef2020-07-22 11:18:06 -0400404 uint16_t fUnused : 6; // # of bits left before Metadata exceeds 8 bytes
Michael Ludwigadb12e72019-12-04 16:19:18 -0500405
406 GrSamplerState::Filter filter() const {
407 return static_cast<GrSamplerState::Filter>(fFilter);
408 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400409 GrSamplerState::MipmapMode mipmapMode() const {
410 return static_cast<GrSamplerState::MipmapMode>(fMipmapMode);
411 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500412 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
413 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
Brian Salomon2432d062020-04-16 20:48:09 -0400414 Subset subset() const { return static_cast<Subset>(fSubset); }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500415 GrTextureOp::Saturate saturate() const {
416 return static_cast<GrTextureOp::Saturate>(fSaturate);
417 }
418
419 static_assert(GrSamplerState::kFilterCount <= 4);
420 static_assert(kGrAATypeCount <= 4);
421 static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
422 };
Michael Ludwig4384f042019-12-05 10:30:35 -0500423 static_assert(sizeof(Metadata) == 8);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500424
Chris Daltondbb833b2020-03-17 12:15:46 -0600425 // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
426 // store the data in a separate struct in order to minimize the size of the TextureOp.
427 // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
428 // may want to re-evaluate whether this is still necessary.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400429 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600430 // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
431 // allocatePrePreparedVertices is also called.
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400432 //
Chris Daltondbb833b2020-03-17 12:15:46 -0600433 // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
434 // part of the flushState).
435 struct Desc {
436 VertexSpec fVertexSpec;
437 int fNumProxies = 0;
438 int fNumTotalQuads = 0;
Robert Phillips32803ff2019-10-23 08:26:08 -0400439
Chris Daltondbb833b2020-03-17 12:15:46 -0600440 // This member variable is only used by 'onPrePrepareDraws'.
441 char* fPrePreparedVertices = nullptr;
442
443 GrProgramInfo* fProgramInfo = nullptr;
444
445 sk_sp<const GrBuffer> fIndexBuffer;
446 sk_sp<const GrBuffer> fVertexBuffer;
447 int fBaseVertex;
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400448
449 // How big should 'fVertices' be to hold all the vertex data?
450 size_t totalSizeInBytes() const {
Chris Daltondbb833b2020-03-17 12:15:46 -0600451 return this->totalNumVertices() * fVertexSpec.vertexSize();
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400452 }
453
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400454 int totalNumVertices() const {
455 return fNumTotalQuads * fVertexSpec.verticesPerQuad();
456 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400457
Chris Daltondbb833b2020-03-17 12:15:46 -0600458 void allocatePrePreparedVertices(SkArenaAlloc* arena) {
459 fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400460 }
Robert Phillips32803ff2019-10-23 08:26:08 -0400461 };
Brian Salomon2432d062020-04-16 20:48:09 -0400462 // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
Greg Daniel549325c2019-10-30 16:19:20 -0400463 TextureOp(GrSurfaceProxyView proxyView,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400464 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
465 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400466 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400467 const SkPMColor4f& color,
468 GrTextureOp::Saturate saturate,
469 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500470 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400471 const SkRect* subsetRect)
Brian Salomon34169692017-08-28 15:32:01 -0400472 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400473 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400474 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600475 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400476 , fMetadata(proxyView.swizzle(), filter, mm, Subset(!!subsetRect), saturate) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400477 // Clean up disparities between the overall aa type and edge configuration and apply
478 // optimizations based on the rect and matrix when appropriate
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500479 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
480 &aaType, &quad->fEdgeFlags);
Michael Ludwig4384f042019-12-05 10:30:35 -0500481 fMetadata.fAAType = static_cast<uint16_t>(aaType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400482
Brian Salomonf1709042018-10-03 11:57:00 -0400483 // We expect our caller to have already caught this optimization.
Brian Salomon2432d062020-04-16 20:48:09 -0400484 SkASSERT(!subsetRect ||
485 !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500486
Brian Salomonf09abc52018-10-03 15:59:04 -0400487 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
John Stilescbe4e282020-06-01 10:38:31 -0400488 // Try to identify cases where the subsetting isn't actually necessary, and skip it.
489 if (subsetRect) {
490 if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
491 subsetRect = nullptr;
492 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
493 }
Brian Salomonf09abc52018-10-03 15:59:04 -0400494 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500495
Brian Salomon2432d062020-04-16 20:48:09 -0400496 // Normalize src coordinates and the subset (if set)
Michael Ludwigadb12e72019-12-04 16:19:18 -0500497 NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
498 proxyView.origin());
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500499 normalize_src_quad(params, &quad->fLocal);
Brian Salomon75cebbe2020-05-18 14:08:14 -0400500 SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500501
Michael Ludwig949ceb22020-02-07 10:14:45 -0500502 // Set bounds before clipping so we don't have to worry about unioning the bounds of
503 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500504 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -0400505 IsHairline::kNo);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500506
Brian Salomon2432d062020-04-16 20:48:09 -0400507 int quadCount = this->appendQuad(quad, color, subset);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500508 fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
Brian Salomond7065e72018-10-12 11:42:02 -0400509 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400510
Michael Ludwigadb12e72019-12-04 16:19:18 -0500511 TextureOp(GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400512 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -0500513 int proxyRunCnt,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400514 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400515 GrSamplerState::MipmapMode mm,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400516 GrTextureOp::Saturate saturate,
517 GrAAType aaType,
518 SkCanvas::SrcRectConstraint constraint,
519 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500520 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400521 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400522 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400523 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Chris Daltondbb833b2020-03-17 12:15:46 -0600524 , fDesc(nullptr)
Brian Salomone69b9ef2020-07-22 11:18:06 -0400525 , fMetadata(set[0].fProxyView.swizzle(),
526 GrSamplerState::Filter::kNearest,
527 GrSamplerState::MipmapMode::kNone,
528 Subset::kNo,
529 saturate) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500530 // Update counts to reflect the batch op
Michael Ludwig379e4962019-12-06 13:21:26 -0500531 fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
Michael Ludwigadb12e72019-12-04 16:19:18 -0500532 fMetadata.fTotalQuadCount = SkToUInt(cnt);
533
Brian Salomond7065e72018-10-12 11:42:02 -0400534 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500535
536 GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon2432d062020-04-16 20:48:09 -0400537 Subset netSubset = Subset::kNo;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500538 GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400539 GrSamplerState::MipmapMode netMM = GrSamplerState::MipmapMode::kNone;
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500540
Michael Ludwig379e4962019-12-06 13:21:26 -0500541 const GrSurfaceProxy* curProxy = nullptr;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500542
Michael Ludwig379e4962019-12-06 13:21:26 -0500543 // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
544 // increases when set[q]'s proxy changes.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500545 int p = 0;
546 for (int q = 0; q < cnt; ++q) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400547 SkASSERT(mm == GrSamplerState::MipmapMode::kNone ||
548 (set[0].fProxyView.proxy()->asTextureProxy()->mipmapped() ==
549 GrMipmapped::kYes));
Michael Ludwig379e4962019-12-06 13:21:26 -0500550 if (q == 0) {
Greg Daniel549325c2019-10-30 16:19:20 -0400551 // We do not placement new the first ViewCountPair since that one is allocated and
552 // initialized as part of the GrTextureOp creation.
Michael Ludwig379e4962019-12-06 13:21:26 -0500553 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
554 fViewCountPairs[0].fQuadCnt = 0;
555 curProxy = fViewCountPairs[0].fProxy.get();
556 } else if (set[q].fProxyView.proxy() != curProxy) {
Greg Daniel549325c2019-10-30 16:19:20 -0400557 // We must placement new the ViewCountPairs here so that the sk_sps in the
558 // GrSurfaceProxyView get initialized properly.
Michael Ludwig379e4962019-12-06 13:21:26 -0500559 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
Michael Ludwigadb12e72019-12-04 16:19:18 -0500560
Michael Ludwig379e4962019-12-06 13:21:26 -0500561 curProxy = fViewCountPairs[p].fProxy.get();
Greg Danielc71c7962020-01-14 16:44:18 -0500562 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
563 curProxy, fViewCountPairs[0].fProxy.get()));
Michael Ludwig379e4962019-12-06 13:21:26 -0500564 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
Michael Ludwig379e4962019-12-06 13:21:26 -0500565 } // else another quad referencing the same proxy
Michael Ludwigce62dec2019-02-19 11:48:46 -0500566
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500567 SkMatrix ctm = viewMatrix;
Michael Ludwig379e4962019-12-06 13:21:26 -0500568 if (set[q].fPreViewMatrix) {
569 ctm.preConcat(*set[q].fPreViewMatrix);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500570 }
571
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400572 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
573 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500574 DrawQuad quad;
Michael Ludwig379e4962019-12-06 13:21:26 -0500575 if (set[q].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500576 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400577
578 SkPoint srcPts[4];
Michael Ludwig379e4962019-12-06 13:21:26 -0500579 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500580 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400581 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500582 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
583 quad.fLocal = GrQuad(set[q].fSrcRect);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400584 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500585
Brian Salomone69b9ef2020-07-22 11:18:06 -0400586 if (netFilter != filter || netMM != mm) {
587 // The only way netFilter != filter is if linear is requested and we haven't yet
588 // found a quad that requires linear (so net is still nearest). Similar for mip
589 // mapping.
590 SkASSERT(netFilter <= filter);
591 SkASSERT(netMM <= mm);
592 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad.fLocal, quad.fDevice);
593 if (mustFilter && filter != GrSamplerState::Filter::kNearest) {
594 netFilter = GrSamplerState::Filter::kLinear;
595 }
596 if (mustMM && mm != GrSamplerState::MipmapMode::kNone) {
597 netMM = GrSamplerState::MipmapMode::kLinear;
598 }
Michael Ludwig22429f92019-06-27 10:44:48 -0400599 }
600
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500601 // Update overall bounds of the op as the union of all quads
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500602 bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500603
604 // Determine the AA type for the quad, then merge with net AA type
Michael Ludwig6bee7762018-10-19 09:50:36 -0400605 GrAAType aaForQuad;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500606 GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
607 &aaForQuad, &quad.fEdgeFlags);
John Stilescbe4e282020-06-01 10:38:31 -0400608
Michael Ludwig6bee7762018-10-19 09:50:36 -0400609 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
610 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500611 if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
612 netAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400613 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400614
615 // Calculate metadata for the entry
Brian Salomon2432d062020-04-16 20:48:09 -0400616 const SkRect* subsetForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400617 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
John Stilescbe4e282020-06-01 10:38:31 -0400618 // Check (briefly) if the subset rect is actually needed for this set entry.
619 SkRect* subsetRect = &set[q].fSrcRect;
620 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
621 if (!safe_to_ignore_subset_rect(aaForQuad, filter, quad, *subsetRect)) {
622 netSubset = Subset::kYes;
623 subsetForQuad = subsetRect;
624 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400625 }
626 }
John Stilescbe4e282020-06-01 10:38:31 -0400627
628 // Normalize the src quads and apply origin
629 NormalizationParams proxyParams = proxy_normalization_params(
630 curProxy, set[q].fProxyView.origin());
631 normalize_src_quad(proxyParams, &quad.fLocal);
632
Brian Salomon2432d062020-04-16 20:48:09 -0400633 // This subset may represent a no-op, otherwise it will have the origin and dimensions
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500634 // of the texture applied to it. Insetting for bilinear filtering is deferred until
635 // on[Pre]Prepare so that the overall filter can be lazily determined.
Brian Salomon75cebbe2020-05-18 14:08:14 -0400636 SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500637
Michael Ludwig949ceb22020-02-07 10:14:45 -0500638 // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
639 // ViewCountPair (this frequently happens when Chrome draws 9-patches).
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400640 fViewCountPairs[p].fQuadCnt += this->appendQuad(&quad, set[q].fColor, subset);
Brian Salomond7065e72018-10-12 11:42:02 -0400641 }
Michael Ludwig406172a2019-12-06 14:05:19 -0500642 // The # of proxy switches should match what was provided (+1 because we incremented p
Michael Ludwig379e4962019-12-06 13:21:26 -0500643 // when a new proxy was encountered).
Michael Ludwig406172a2019-12-06 14:05:19 -0500644 SkASSERT((p + 1) == fMetadata.fProxyCount);
Michael Ludwig379e4962019-12-06 13:21:26 -0500645 SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500646
Michael Ludwig4384f042019-12-05 10:30:35 -0500647 fMetadata.fAAType = static_cast<uint16_t>(netAAType);
648 fMetadata.fFilter = static_cast<uint16_t>(netFilter);
Brian Salomon2432d062020-04-16 20:48:09 -0400649 fMetadata.fSubset = static_cast<uint16_t>(netSubset);
Brian Salomon34169692017-08-28 15:32:01 -0400650
Michael Ludwig119ac6d2019-11-21 09:26:46 -0500651 this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage), IsHairline::kNo);
Brian Salomon17031a72018-05-22 14:14:07 -0400652 }
653
Brian Salomon2432d062020-04-16 20:48:09 -0400654 int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& subset) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500655 DrawQuad extra;
Michael Ludwig465864c2020-02-10 09:30:04 -0500656 // Only clip when there's anti-aliasing. When non-aa, the GPU clips just fine and there's
657 // no inset/outset math that requires w > 0.
658 int quadCount = quad->fEdgeFlags != GrQuadAAFlags::kNone ?
659 GrQuadUtils::ClipToW0(quad, &extra) : 1;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500660 if (quadCount == 0) {
661 // We can't discard the op at this point, but disable AA flags so it won't go through
662 // inset/outset processing
663 quad->fEdgeFlags = GrQuadAAFlags::kNone;
664 quadCount = 1;
665 }
Brian Salomon2432d062020-04-16 20:48:09 -0400666 fQuads.append(quad->fDevice, {color, subset, quad->fEdgeFlags}, &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500667 if (quadCount > 1) {
Brian Salomon2432d062020-04-16 20:48:09 -0400668 fQuads.append(extra.fDevice, {color, subset, extra.fEdgeFlags}, &extra.fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500669 fMetadata.fTotalQuadCount++;
670 }
671 return quadCount;
672 }
673
Robert Phillips2669a7b2020-03-12 12:07:19 -0400674 GrProgramInfo* programInfo() override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600675 // Although this Op implements its own onPrePrepareDraws it calls GrMeshDrawOps' version so
676 // this entry point will be called.
677 return (fDesc) ? fDesc->fProgramInfo : nullptr;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400678 }
679
Chris Daltondbb833b2020-03-17 12:15:46 -0600680 void onCreateProgramInfo(const GrCaps* caps,
681 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400682 const GrSurfaceProxyView* writeView,
Chris Daltondbb833b2020-03-17 12:15:46 -0600683 GrAppliedClip&& appliedClip,
684 const GrXferProcessor::DstProxyView& dstProxyView) override {
685 SkASSERT(fDesc);
686
687 GrGeometryProcessor* gp;
688
689 {
690 const GrBackendFormat& backendFormat =
691 fViewCountPairs[0].fProxy->backendFormat();
692
693 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
694 fMetadata.filter());
695
696 gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
697 arena, fDesc->fVertexSpec, *caps->shaderCaps(), backendFormat, samplerState,
698 fMetadata.fSwizzle, std::move(fTextureColorSpaceXform), fMetadata.saturate());
699
700 SkASSERT(fDesc->fVertexSpec.vertexSize() == gp->vertexStride());
701 }
702
703 auto pipelineFlags = (GrAAType::kMSAA == fMetadata.aaType()) ?
704 GrPipeline::InputFlags::kHWAntialias : GrPipeline::InputFlags::kNone;
705
706 fDesc->fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Brian Salomon8afde5f2020-04-01 16:22:00 -0400707 caps, arena, writeView, std::move(appliedClip), dstProxyView, gp,
Chris Daltondbb833b2020-03-17 12:15:46 -0600708 GrProcessorSet::MakeEmptySet(), fDesc->fVertexSpec.primitiveType(),
709 pipelineFlags);
Robert Phillips4133dc42020-03-11 15:55:55 -0400710 }
711
Robert Phillipsdf70f152019-11-15 14:57:05 -0500712 void onPrePrepareDraws(GrRecordingContext* context,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400713 const GrSurfaceProxyView* writeView,
Robert Phillips8053c972019-11-21 10:44:53 -0500714 GrAppliedClip* clip,
715 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips61fc7992019-10-22 11:58:17 -0400716 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips29f38542019-10-16 09:20:25 -0400717
Robert Phillips61fc7992019-10-22 11:58:17 -0400718 SkDEBUGCODE(this->validate();)
Chris Daltondbb833b2020-03-17 12:15:46 -0600719 SkASSERT(!fDesc);
Robert Phillips61fc7992019-10-22 11:58:17 -0400720
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500721 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillips61fc7992019-10-22 11:58:17 -0400722
Chris Daltondbb833b2020-03-17 12:15:46 -0600723 fDesc = arena->make<Desc>();
724 this->characterize(fDesc);
725 fDesc->allocatePrePreparedVertices(arena);
726 FillInVertices(*context->priv().caps(), this, fDesc, fDesc->fPrePreparedVertices);
Robert Phillips61fc7992019-10-22 11:58:17 -0400727
Chris Daltondbb833b2020-03-17 12:15:46 -0600728 // This will call onCreateProgramInfo and register the created program with the DDL.
Brian Salomon8afde5f2020-04-01 16:22:00 -0400729 this->INHERITED::onPrePrepareDraws(context, writeView, clip, dstProxyView);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400730 }
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400731
Chris Daltondbb833b2020-03-17 12:15:46 -0600732 static void FillInVertices(const GrCaps& caps, TextureOp* texOp, Desc* desc, char* vertexData) {
733 SkASSERT(vertexData);
734
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400735 int totQuadsSeen = 0;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400736 SkDEBUGCODE(int totVerticesSeen = 0;)
Michael Ludwig189c9802019-11-21 11:21:12 -0500737 SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400738
Chris Daltondbb833b2020-03-17 12:15:46 -0600739 GrQuadPerEdgeAA::Tessellator tessellator(desc->fVertexSpec, vertexData);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400740 for (const auto& op : ChainRange<TextureOp>(texOp)) {
741 auto iter = op.fQuads.iterator();
Michael Ludwigadb12e72019-12-04 16:19:18 -0500742 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500743 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
744 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400745
Chris Daltondbb833b2020-03-17 12:15:46 -0600746 for (int i = 0; i < quadCnt && iter.next(); ++i) {
747 SkASSERT(iter.isLocalValid());
Brian Salomon2432d062020-04-16 20:48:09 -0400748 const ColorSubsetAndAA& info = iter.metadata();
Michael Ludwig7c6a4a82020-02-07 10:14:26 -0500749
Chris Daltondbb833b2020-03-17 12:15:46 -0600750 tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
Brian Salomon75cebbe2020-05-18 14:08:14 -0400751 info.fSubsetRect, info.aaFlags());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400752 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400753
Chris Daltondbb833b2020-03-17 12:15:46 -0600754 SkASSERT((totVerticesSeen + meshVertexCnt) * vertexSize
755 == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400756
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400757 totQuadsSeen += quadCnt;
758 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
759 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400760 }
761
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400762 // If quad counts per proxy were calculated correctly, the entire iterator
763 // should have been consumed.
Chris Daltondbb833b2020-03-17 12:15:46 -0600764 SkASSERT(!iter.next());
Robert Phillipsc5a2c752019-10-24 13:11:45 -0400765 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400766
Chris Daltondbb833b2020-03-17 12:15:46 -0600767 SkASSERT(desc->totalSizeInBytes() == (size_t)(tessellator.vertices() - vertexData));
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400768 SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
769 SkASSERT(totVerticesSeen == desc->totalNumVertices());
Robert Phillips7327c9d2019-10-08 16:32:56 -0400770 }
771
Robert Phillips29f38542019-10-16 09:20:25 -0400772#ifdef SK_DEBUG
773 void validate() const override {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500774 // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
Michael Ludwigadb12e72019-12-04 16:19:18 -0500775 auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
776 GrAAType aaType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400777
Robert Phillipse837e612019-11-15 11:02:50 -0500778 int quadCount = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400779 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500780 SkASSERT(op.fMetadata.fSwizzle == fMetadata.fSwizzle);
781
782 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
783 auto* proxy = op.fViewCountPairs[p].fProxy->asTextureProxy();
Robert Phillipse837e612019-11-15 11:02:50 -0500784 quadCount += op.fViewCountPairs[p].fQuadCnt;
Robert Phillips29f38542019-10-16 09:20:25 -0400785 SkASSERT(proxy);
786 SkASSERT(proxy->textureType() == textureType);
Robert Phillips29f38542019-10-16 09:20:25 -0400787 }
788
789 // Each individual op must be a single aaType. kCoverage and kNone ops can chain
790 // together but kMSAA ones do not.
791 if (aaType == GrAAType::kCoverage || aaType == GrAAType::kNone) {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500792 SkASSERT(op.fMetadata.aaType() == GrAAType::kCoverage ||
793 op.fMetadata.aaType() == GrAAType::kNone);
Robert Phillips29f38542019-10-16 09:20:25 -0400794 } else {
Michael Ludwigadb12e72019-12-04 16:19:18 -0500795 SkASSERT(aaType == GrAAType::kMSAA && op.fMetadata.aaType() == GrAAType::kMSAA);
Robert Phillips29f38542019-10-16 09:20:25 -0400796 }
797 }
Robert Phillipse837e612019-11-15 11:02:50 -0500798
799 SkASSERT(quadCount == this->numChainedQuads());
Robert Phillips29f38542019-10-16 09:20:25 -0400800 }
801#endif
802
Robert Phillipse837e612019-11-15 11:02:50 -0500803#if GR_TEST_UTILS
804 int numQuads() const final { return this->totNumQuads(); }
805#endif
806
Chris Daltondbb833b2020-03-17 12:15:46 -0600807 void characterize(Desc* desc) const {
Robert Phillips29f38542019-10-16 09:20:25 -0400808 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
809 ColorType colorType = ColorType::kNone;
810 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomon2432d062020-04-16 20:48:09 -0400811 Subset subset = Subset::kNo;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500812 GrAAType overallAAType = fMetadata.aaType();
Robert Phillips29f38542019-10-16 09:20:25 -0400813
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400814 desc->fNumProxies = 0;
815 desc->fNumTotalQuads = 0;
816 int maxQuadsPerMesh = 0;
Robert Phillips29f38542019-10-16 09:20:25 -0400817
Brian Salomonf7232642018-09-19 08:58:08 -0400818 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400819 if (op.fQuads.deviceQuadType() > quadType) {
820 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500821 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400822 if (op.fQuads.localQuadType() > srcQuadType) {
823 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500824 }
Brian Salomon2432d062020-04-16 20:48:09 -0400825 if (op.fMetadata.subset() == Subset::kYes) {
826 subset = Subset::kYes;
Brian Salomonf7232642018-09-19 08:58:08 -0400827 }
Brian Osman788b9162020-02-07 10:36:46 -0500828 colorType = std::max(colorType, op.fMetadata.colorType());
Michael Ludwigadb12e72019-12-04 16:19:18 -0500829 desc->fNumProxies += op.fMetadata.fProxyCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400830
Michael Ludwigadb12e72019-12-04 16:19:18 -0500831 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
Brian Osman788b9162020-02-07 10:36:46 -0500832 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
Brian Salomonf7232642018-09-19 08:58:08 -0400833 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400834 desc->fNumTotalQuads += op.totNumQuads();
835
Michael Ludwigadb12e72019-12-04 16:19:18 -0500836 if (op.fMetadata.aaType() == GrAAType::kCoverage) {
Robert Phillips29f38542019-10-16 09:20:25 -0400837 overallAAType = GrAAType::kCoverage;
Brian Salomonae7d7702018-10-14 15:05:45 -0400838 }
Brian Salomon34169692017-08-28 15:32:01 -0400839 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400840
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400841 SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
842
843 SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
844
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400845 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(overallAAType,
846 maxQuadsPerMesh);
847
848 desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
Brian Salomon2432d062020-04-16 20:48:09 -0400849 subset, overallAAType, /* alpha as coverage */ true,
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400850 indexBufferOption);
Robert Phillipse837e612019-11-15 11:02:50 -0500851
852 SkASSERT(desc->fNumTotalQuads <= GrQuadPerEdgeAA::QuadLimit(indexBufferOption));
Robert Phillips29f38542019-10-16 09:20:25 -0400853 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500854
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400855 int totNumQuads() const {
856#ifdef SK_DEBUG
857 int tmp = 0;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500858 for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
Greg Daniel549325c2019-10-30 16:19:20 -0400859 tmp += fViewCountPairs[p].fQuadCnt;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400860 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500861 SkASSERT(tmp == fMetadata.fTotalQuadCount);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400862#endif
863
Michael Ludwigadb12e72019-12-04 16:19:18 -0500864 return fMetadata.fTotalQuadCount;
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400865 }
866
867 int numChainedQuads() const {
868 int numChainedQuads = this->totNumQuads();
869
870 for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
871 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
872 }
873
874 for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
875 numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
876 }
877
878 return numChainedQuads;
879 }
880
Robert Phillips29f38542019-10-16 09:20:25 -0400881 // onPrePrepareDraws may or may not have been called at this point
882 void onPrepareDraws(Target* target) override {
883 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500884
Robert Phillips29f38542019-10-16 09:20:25 -0400885 SkDEBUGCODE(this->validate();)
886
Chris Daltondbb833b2020-03-17 12:15:46 -0600887 SkASSERT(!fDesc || fDesc->fPrePreparedVertices);
Robert Phillips29f38542019-10-16 09:20:25 -0400888
Chris Daltondbb833b2020-03-17 12:15:46 -0600889 if (!fDesc) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400890 SkArenaAlloc* arena = target->allocator();
Chris Daltondbb833b2020-03-17 12:15:46 -0600891 fDesc = arena->make<Desc>();
892 this->characterize(fDesc);
893 SkASSERT(!fDesc->fPrePreparedVertices);
Brian Salomonf7232642018-09-19 08:58:08 -0400894 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400895
Chris Daltondbb833b2020-03-17 12:15:46 -0600896 size_t vertexSize = fDesc->fVertexSpec.vertexSize();
Brian Salomon92be2f72018-06-19 14:33:47 -0400897
Chris Daltondbb833b2020-03-17 12:15:46 -0600898 void* vdata = target->makeVertexSpace(vertexSize, fDesc->totalNumVertices(),
899 &fDesc->fVertexBuffer, &fDesc->fBaseVertex);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400900 if (!vdata) {
901 SkDebugf("Could not allocate vertices\n");
902 return;
Brian Salomon34169692017-08-28 15:32:01 -0400903 }
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400904
Chris Daltondbb833b2020-03-17 12:15:46 -0600905 if (fDesc->fVertexSpec.needsIndexBuffer()) {
906 fDesc->fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(
907 target, fDesc->fVertexSpec.indexBufferOption());
908 if (!fDesc->fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400909 SkDebugf("Could not allocate indices\n");
910 return;
911 }
912 }
913
Chris Daltondbb833b2020-03-17 12:15:46 -0600914 if (fDesc->fPrePreparedVertices) {
915 memcpy(vdata, fDesc->fPrePreparedVertices, fDesc->totalSizeInBytes());
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400916 } else {
Chris Daltondbb833b2020-03-17 12:15:46 -0600917 FillInVertices(target->caps(), this, fDesc, (char*) vdata);
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400918 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700919 }
920
921 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600922 if (!fDesc->fVertexBuffer) {
923 return;
924 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500925
Chris Daltondbb833b2020-03-17 12:15:46 -0600926 if (fDesc->fVertexSpec.needsIndexBuffer() && !fDesc->fIndexBuffer) {
927 return;
928 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500929
Chris Daltondbb833b2020-03-17 12:15:46 -0600930 if (!fDesc->fProgramInfo) {
931 this->createProgramInfo(flushState);
932 SkASSERT(fDesc->fProgramInfo);
933 }
934
935 flushState->bindPipelineAndScissorClip(*fDesc->fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400936 flushState->bindBuffers(std::move(fDesc->fIndexBuffer), nullptr,
937 std::move(fDesc->fVertexBuffer));
Chris Daltondbb833b2020-03-17 12:15:46 -0600938
939 int totQuadsSeen = 0;
940 SkDEBUGCODE(int numDraws = 0;)
941 for (const auto& op : ChainRange<TextureOp>(this)) {
942 for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
943 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
944 SkASSERT(numDraws < fDesc->fNumProxies);
945 flushState->bindTextures(fDesc->fProgramInfo->primProc(),
946 *op.fViewCountPairs[p].fProxy,
947 fDesc->fProgramInfo->pipeline());
948 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(),
949 fDesc->fVertexSpec, totQuadsSeen, quadCnt,
950 fDesc->totalNumVertices(), fDesc->fBaseVertex);
951 totQuadsSeen += quadCnt;
952 SkDEBUGCODE(++numDraws;)
953 }
954 }
955
956 SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
957 SkASSERT(numDraws == fDesc->fNumProxies);
Brian Salomon34169692017-08-28 15:32:01 -0400958 }
959
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500960 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
961 const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400962 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400963 const auto* that = t->cast<TextureOp>();
Robert Phillips7327c9d2019-10-08 16:32:56 -0400964
Chris Daltondbb833b2020-03-17 12:15:46 -0600965 if (fDesc || that->fDesc) {
Robert Phillips7327c9d2019-10-08 16:32:56 -0400966 // This should never happen (since only DDL recorded ops should be prePrepared)
967 // but, in any case, we should never combine ops that that been prePrepared
968 return CombineResult::kCannotCombine;
969 }
970
Brian Salomon2432d062020-04-16 20:48:09 -0400971 if (fMetadata.subset() != that->fMetadata.subset()) {
972 // It is technically possible to combine operations across subset modes, but performance
Michael Ludwig2929f512019-04-19 13:05:56 -0400973 // testing suggests it's better to make more draw calls where some take advantage of
974 // the more optimal shader path without coordinate clamping.
975 return CombineResult::kCannotCombine;
976 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400977 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
978 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000979 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400980 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400981
Brian Salomonae7d7702018-10-14 15:05:45 -0400982 bool upgradeToCoverageAAOnMerge = false;
Michael Ludwigadb12e72019-12-04 16:19:18 -0500983 if (fMetadata.aaType() != that->fMetadata.aaType()) {
984 if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400985 return CombineResult::kCannotCombine;
986 }
987 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500988 }
Robert Phillipsb69001f2019-10-29 12:16:35 -0400989
Michael Ludwigadb12e72019-12-04 16:19:18 -0500990 if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400991 this->numChainedQuads() + that->numChainedQuads())) {
992 return CombineResult::kCannotCombine;
Robert Phillipsb69001f2019-10-29 12:16:35 -0400993 }
994
Michael Ludwigadb12e72019-12-04 16:19:18 -0500995 if (fMetadata.saturate() != that->fMetadata.saturate()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400996 return CombineResult::kCannotCombine;
997 }
Michael Ludwigadb12e72019-12-04 16:19:18 -0500998 if (fMetadata.filter() != that->fMetadata.filter()) {
Brian Salomonf7232642018-09-19 08:58:08 -0400999 return CombineResult::kCannotCombine;
1000 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001001 if (fMetadata.mipmapMode() != that->fMetadata.mipmapMode()) {
1002 return CombineResult::kCannotCombine;
1003 }
Michael Ludwigadb12e72019-12-04 16:19:18 -05001004 if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
1005 return CombineResult::kCannotCombine;
1006 }
1007 const auto* thisProxy = fViewCountPairs[0].fProxy.get();
1008 const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
1009 if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
1010 thisProxy != thatProxy) {
Brian Salomon588cec72018-11-14 13:56:37 -05001011 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -05001012 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Michael Ludwigadb12e72019-12-04 16:19:18 -05001013 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
Brian Salomonf7232642018-09-19 08:58:08 -04001014 return CombineResult::kMayChain;
1015 }
Brian Salomon7eae3e02018-08-07 14:02:38 +00001016 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -04001017 }
Michael Ludwig009b92e2019-02-15 16:03:53 -05001018
Brian Salomon2432d062020-04-16 20:48:09 -04001019 fMetadata.fSubset |= that->fMetadata.fSubset;
Brian Osman788b9162020-02-07 10:36:46 -05001020 fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -04001021 if (upgradeToCoverageAAOnMerge) {
Michael Ludwig4384f042019-12-05 10:30:35 -05001022 fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
Brian Salomonae7d7702018-10-14 15:05:45 -04001023 }
Michael Ludwig009b92e2019-02-15 16:03:53 -05001024
Michael Ludwig425eb452019-06-27 10:13:27 -04001025 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -05001026 fQuads.concat(that->fQuads);
Greg Daniel549325c2019-10-30 16:19:20 -04001027 fViewCountPairs[0].fQuadCnt += that->fQuads.count();
Michael Ludwigadb12e72019-12-04 16:19:18 -05001028 fMetadata.fTotalQuadCount += that->fQuads.count();
Michael Ludwig009b92e2019-02-15 16:03:53 -05001029
Brian Salomon7eae3e02018-08-07 14:02:38 +00001030 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -04001031 }
1032
Brian Salomon2432d062020-04-16 20:48:09 -04001033 GrQuadBuffer<ColorSubsetAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -04001034 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Chris Daltondbb833b2020-03-17 12:15:46 -06001035 // Most state of TextureOp is packed into these two field to minimize the op's size.
Michael Ludwigadb12e72019-12-04 16:19:18 -05001036 // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1037 // consider/measure changes with care.
Chris Daltondbb833b2020-03-17 12:15:46 -06001038 Desc* fDesc;
Michael Ludwigadb12e72019-12-04 16:19:18 -05001039 Metadata fMetadata;
Robert Phillips32803ff2019-10-23 08:26:08 -04001040
1041 // This field must go last. When allocating this op, we will allocate extra space to hold
Greg Daniel549325c2019-10-30 16:19:20 -04001042 // additional ViewCountPairs immediately after the op's allocation so we can treat this
Robert Phillips32803ff2019-10-23 08:26:08 -04001043 // as an fProxyCnt-length array.
Greg Daniel549325c2019-10-30 16:19:20 -04001044 ViewCountPair fViewCountPairs[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -04001045
Brian Salomon34169692017-08-28 15:32:01 -04001046 typedef GrMeshDrawOp INHERITED;
1047};
1048
1049} // anonymous namespace
1050
Robert Phillipse837e612019-11-15 11:02:50 -05001051#if GR_TEST_UTILS
1052uint32_t GrTextureOp::ClassID() {
1053 return TextureOp::ClassID();
1054}
1055#endif
Brian Salomon34169692017-08-28 15:32:01 -04001056
Robert Phillipse837e612019-11-15 11:02:50 -05001057std::unique_ptr<GrDrawOp> GrTextureOp::Make(GrRecordingContext* context,
1058 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -05001059 SkAlphaType alphaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001060 sk_sp<GrColorSpaceXform> textureXform,
1061 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001062 GrSamplerState::MipmapMode mm,
Robert Phillipse837e612019-11-15 11:02:50 -05001063 const SkPMColor4f& color,
1064 Saturate saturate,
1065 SkBlendMode blendMode,
1066 GrAAType aaType,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001067 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -04001068 const SkRect* subset) {
Michael Ludwig22429f92019-06-27 10:44:48 -04001069 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
Brian Salomon2432d062020-04-16 20:48:09 -04001070 if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1071 // No need for a shader-based subset if hardware clamping achieves the same effect
1072 subset = nullptr;
Michael Ludwig22429f92019-06-27 10:44:48 -04001073 }
1074
Brian Salomone69b9ef2020-07-22 11:18:06 -04001075 if (filter != GrSamplerState::Filter::kNearest || mm != GrSamplerState::MipmapMode::kNone) {
1076 auto [mustFilter, mustMM] = filter_and_mm_have_effect(quad->fLocal, quad->fDevice);
1077 if (!mustFilter) {
1078 filter = GrSamplerState::Filter::kNearest;
1079 }
1080 if (!mustMM) {
1081 mm = GrSamplerState::MipmapMode::kNone;
1082 }
Michael Ludwig22429f92019-06-27 10:44:48 -04001083 }
1084
1085 if (blendMode == SkBlendMode::kSrcOver) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001086 return TextureOp::Make(context, std::move(proxyView), std::move(textureXform), filter, mm,
Brian Salomon2432d062020-04-16 20:48:09 -04001087 color, saturate, aaType, std::move(quad), subset);
Michael Ludwig22429f92019-06-27 10:44:48 -04001088 } else {
1089 // Emulate complex blending using GrFillRectOp
1090 GrPaint paint;
1091 paint.setColor4f(color);
1092 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1093
1094 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomon2432d062020-04-16 20:48:09 -04001095 if (subset) {
Brian Salomonca6b2f42020-01-24 11:31:21 -05001096 const auto& caps = *context->priv().caps();
1097 SkRect localRect;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001098 if (quad->fLocal.asRect(&localRect)) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001099 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
1100 filter, *subset, localRect, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001101 } else {
John Stiles5a2a7b32020-06-04 10:57:21 -04001102 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
1103 filter, *subset, caps);
Brian Salomonca6b2f42020-01-24 11:31:21 -05001104 }
1105 } else {
Greg Danield2ccbb52020-02-05 10:45:39 -05001106 fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), filter);
Michael Ludwig22429f92019-06-27 10:44:48 -04001107 }
John Stilesf743d4e2020-07-23 11:35:08 -04001108 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
Michael Ludwig22429f92019-06-27 10:44:48 -04001109 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001110 if (saturate == GrTextureOp::Saturate::kYes) {
John Stiles5a2a7b32020-06-04 10:57:21 -04001111 fp = GrClampFragmentProcessor::Make(std::move(fp), /*clampToPremul=*/false);
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001112 }
John Stiles5933d7d2020-07-21 12:28:35 -04001113 paint.setColorFragmentProcessor(std::move(fp));
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001114 return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
Michael Ludwig22429f92019-06-27 10:44:48 -04001115 }
1116}
1117
Robert Phillipse837e612019-11-15 11:02:50 -05001118// A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1119class GrTextureOp::BatchSizeLimiter {
1120public:
1121 BatchSizeLimiter(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001122 const GrClip* clip,
Robert Phillipse837e612019-11-15 11:02:50 -05001123 GrRecordingContext* context,
1124 int numEntries,
1125 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001126 GrSamplerState::MipmapMode mm,
Robert Phillipse837e612019-11-15 11:02:50 -05001127 GrTextureOp::Saturate saturate,
1128 SkCanvas::SrcRectConstraint constraint,
1129 const SkMatrix& viewMatrix,
1130 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
1131 : fRTC(rtc)
1132 , fClip(clip)
1133 , fContext(context)
1134 , fFilter(filter)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001135 , fMipmapMode(mm)
Robert Phillipse837e612019-11-15 11:02:50 -05001136 , fSaturate(saturate)
1137 , fConstraint(constraint)
1138 , fViewMatrix(viewMatrix)
1139 , fTextureColorSpaceXform(textureColorSpaceXform)
Brian Salomone69b9ef2020-07-22 11:18:06 -04001140 , fNumLeft(numEntries) {}
Brian Salomon34169692017-08-28 15:32:01 -04001141
Michael Ludwigadb12e72019-12-04 16:19:18 -05001142 void createOp(GrRenderTargetContext::TextureSetEntry set[],
Robert Phillipse837e612019-11-15 11:02:50 -05001143 int clumpSize,
1144 GrAAType aaType) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001145 int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
Brian Salomone69b9ef2020-07-22 11:18:06 -04001146 std::unique_ptr<GrDrawOp> op = TextureOp::Make(fContext,
1147 &set[fNumClumped],
1148 clumpSize,
1149 clumpProxyCount,
1150 fFilter,
1151 fMipmapMode,
1152 fSaturate,
1153 aaType,
1154 fConstraint,
1155 fViewMatrix,
Robert Phillipse837e612019-11-15 11:02:50 -05001156 fTextureColorSpaceXform);
1157 fRTC->addDrawOp(fClip, std::move(op));
1158
1159 fNumLeft -= clumpSize;
1160 fNumClumped += clumpSize;
1161 }
1162
1163 int numLeft() const { return fNumLeft; }
1164 int baseIndex() const { return fNumClumped; }
1165
1166private:
1167 GrRenderTargetContext* fRTC;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001168 const GrClip* fClip;
Robert Phillipse837e612019-11-15 11:02:50 -05001169 GrRecordingContext* fContext;
1170 GrSamplerState::Filter fFilter;
Brian Salomone69b9ef2020-07-22 11:18:06 -04001171 GrSamplerState::MipmapMode fMipmapMode;
Robert Phillipse837e612019-11-15 11:02:50 -05001172 GrTextureOp::Saturate fSaturate;
1173 SkCanvas::SrcRectConstraint fConstraint;
1174 const SkMatrix& fViewMatrix;
1175 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
1176
1177 int fNumLeft;
1178 int fNumClumped = 0; // also the offset for the start of the next clump
1179};
1180
1181// Greedily clump quad draws together until the index buffer limit is exceeded.
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001182void GrTextureOp::AddTextureSetOps(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001183 const GrClip* clip,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001184 GrRecordingContext* context,
Michael Ludwigadb12e72019-12-04 16:19:18 -05001185 GrRenderTargetContext::TextureSetEntry set[],
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001186 int cnt,
Michael Ludwig379e4962019-12-06 13:21:26 -05001187 int proxyRunCnt,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001188 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001189 GrSamplerState::MipmapMode mm,
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001190 Saturate saturate,
1191 SkBlendMode blendMode,
1192 GrAAType aaType,
1193 SkCanvas::SrcRectConstraint constraint,
1194 const SkMatrix& viewMatrix,
1195 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig379e4962019-12-06 13:21:26 -05001196 // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1197 // the op's metadata so we don't need to worry about overflow.
Michael Ludwig4ef1ca12019-12-19 10:58:52 -05001198 SkDEBUGCODE(TextureOp::ValidateResourceLimits();)
Michael Ludwig379e4962019-12-06 13:21:26 -05001199 SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1200
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001201 // First check if we can support batches as a single op
1202 if (blendMode != SkBlendMode::kSrcOver ||
1203 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1204 // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1205 // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1206 // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1207 SkMatrix ctm;
1208 for (int i = 0; i < cnt; ++i) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001209 ctm = viewMatrix;
1210 if (set[i].fPreViewMatrix) {
1211 ctm.preConcat(*set[i].fPreViewMatrix);
1212 }
Robert Phillipse837e612019-11-15 11:02:50 -05001213
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001214 DrawQuad quad;
1215 quad.fEdgeFlags = set[i].fAAFlags;
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001216 if (set[i].fDstClipQuad) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001217 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001218
1219 SkPoint srcPts[4];
1220 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001221 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001222 } else {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001223 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1224 quad.fLocal = GrQuad(set[i].fSrcRect);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001225 }
1226
Brian Salomon2432d062020-04-16 20:48:09 -04001227 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001228 ? &set[i].fSrcRect : nullptr;
1229
Brian Salomonfc118442019-11-22 19:09:27 -05001230 auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001231 filter, mm, set[i].fColor, saturate, blendMode, aaType, &quad, subset);
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001232 rtc->addDrawOp(clip, std::move(op));
1233 }
1234 return;
1235 }
1236
1237 // Second check if we can always just make a single op and avoid the extra iteration
Robert Phillipse837e612019-11-15 11:02:50 -05001238 // needed to clump things together.
Brian Osman788b9162020-02-07 10:36:46 -05001239 if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
Robert Phillipse837e612019-11-15 11:02:50 -05001240 GrResourceProvider::MaxNumAAQuads())) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04001241 auto op = TextureOp::Make(context, set, cnt, proxyRunCnt, filter, mm, saturate, aaType,
Robert Phillipse837e612019-11-15 11:02:50 -05001242 constraint, viewMatrix, std::move(textureColorSpaceXform));
1243 rtc->addDrawOp(clip, std::move(op));
1244 return;
1245 }
1246
Brian Salomone69b9ef2020-07-22 11:18:06 -04001247 BatchSizeLimiter state(rtc, clip, context, cnt, filter, mm, saturate, constraint, viewMatrix,
Robert Phillipse837e612019-11-15 11:02:50 -05001248 std::move(textureColorSpaceXform));
1249
1250 // kNone and kMSAA never get altered
1251 if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1252 // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1253 while (state.numLeft() > 0) {
Brian Osman788b9162020-02-07 10:36:46 -05001254 int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
Robert Phillipse837e612019-11-15 11:02:50 -05001255
1256 state.createOp(set, clumpSize, aaType);
1257 }
1258 } else {
1259 // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1260 // can also get downgraded to kNone if all the quads are on integer coordinates and
1261 // axis-aligned.
1262 SkASSERT(aaType == GrAAType::kCoverage);
1263
1264 while (state.numLeft() > 0) {
1265 GrAAType runningAA = GrAAType::kNone;
1266 bool clumped = false;
1267
1268 for (int i = 0; i < state.numLeft(); ++i) {
1269 int absIndex = state.baseIndex() + i;
1270
1271 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone) {
1272
1273 if (i >= GrResourceProvider::MaxNumAAQuads()) {
1274 // Here we either need to boost the AA type to kCoverage, but doing so with
1275 // all the accumulated quads would overflow, or we have a set of AA quads
1276 // that has just gotten too large. In either case, calve off the existing
1277 // quads as their own TextureOp.
1278 state.createOp(
1279 set,
1280 runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1281 runningAA); // maybe downgrading AA here
1282 clumped = true;
1283 break;
1284 }
1285
1286 runningAA = GrAAType::kCoverage;
1287 } else if (runningAA == GrAAType::kNone) {
1288
1289 if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1290 // Here we've found a consistent batch of non-AA quads that has gotten too
1291 // large. Calve it off as its own GrTextureOp.
1292 state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1293 GrAAType::kNone); // definitely downgrading AA here
1294 clumped = true;
1295 break;
1296 }
1297 }
1298 }
1299
1300 if (!clumped) {
1301 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1302 // quads and call it a day.
1303 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1304 }
1305 }
1306 }
1307}
Robert Phillipsae01f622019-11-13 15:56:31 +00001308
Brian Salomon34169692017-08-28 15:32:01 -04001309#if GR_TEST_UTILS
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04001310#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05001311#include "src/gpu/GrProxyProvider.h"
1312#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -04001313
1314GR_DRAW_OP_TEST_DEFINE(TextureOp) {
Brian Salomona56a7462020-02-07 14:17:25 -05001315 SkISize dims;
1316 dims.fHeight = random->nextULessThan(90) + 10;
1317 dims.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001318 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001319 GrMipmapped mipMapped = random->nextBool() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel09c94002018-06-08 22:11:51 +00001320 SkBackingFit fit = SkBackingFit::kExact;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001321 if (mipMapped == GrMipmapped::kNo) {
Greg Daniel09c94002018-06-08 22:11:51 +00001322 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1323 }
Greg Daniel4065d452018-11-16 15:43:41 -05001324 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -04001325 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1326 GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -05001327 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -04001328 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -04001329 format, dims, GrRenderable::kNo, 1, mipMapped, fit, SkBudgeted::kNo, GrProtected::kNo,
1330 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001331
Brian Salomon34169692017-08-28 15:32:01 -04001332 SkRect rect = GrTest::TestRect(random);
1333 SkRect srcRect;
1334 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1335 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1336 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1337 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1338 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -05001339 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001340 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
Brian Salomone69b9ef2020-07-22 11:18:06 -04001341 static_cast<uint32_t>(GrSamplerState::Filter::kLast) + 1);
1342 GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
1343 if (mipMapped == GrMipmapped::kYes) {
1344 mm = (GrSamplerState::MipmapMode)random->nextULessThan(
1345 static_cast<uint32_t>(GrSamplerState::MipmapMode::kLast) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +00001346 }
Brian Salomone69b9ef2020-07-22 11:18:06 -04001347
Brian Osman3ebd3542018-07-30 14:36:53 -04001348 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -05001349 GrAAType aaType = GrAAType::kNone;
1350 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06001351 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -05001352 }
Brian Salomon2213ee92018-10-02 10:44:21 -04001353 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1354 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1355 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1356 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1357 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomon2432d062020-04-16 20:48:09 -04001358 bool useSubset = random->nextBool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -04001359 auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
Greg Daniel549325c2019-10-30 16:19:20 -04001360 GrSurfaceProxyView proxyView(
1361 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -05001362 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Brian Salomonfc118442019-11-22 19:09:27 -05001363 auto alphaType = static_cast<SkAlphaType>(
1364 random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
Greg Daniel549325c2019-10-30 16:19:20 -04001365
Michael Ludwig6b45c5d2020-02-07 09:56:38 -05001366 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
Brian Salomonfc118442019-11-22 19:09:27 -05001367 return GrTextureOp::Make(context, std::move(proxyView), alphaType, std::move(texXform), filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -04001368 mm, color, saturate, SkBlendMode::kSrcOver, aaType, &quad,
1369 useSubset ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -04001370}
1371
1372#endif