xref: /aosp_15_r20/external/skia/src/gpu/ganesh/ops/TextureOp.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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 #include "src/gpu/ganesh/ops/TextureOp.h"
8 
9 #include "include/core/SkAlphaType.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkSamplingOptions.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/gpu/GpuTypes.h"
20 #include "include/gpu/ganesh/GrBackendSurface.h"
21 #include "include/gpu/ganesh/GrRecordingContext.h"
22 #include "include/gpu/ganesh/GrTypes.h"
23 #include "include/private/base/SkAssert.h"
24 #include "include/private/base/SkDebug.h"
25 #include "include/private/base/SkTo.h"
26 #include "include/private/gpu/ganesh/GrTypesPriv.h"
27 #include "src/base/SkArenaAlloc.h"
28 #include "src/base/SkVx.h"
29 #include "src/core/SkRectPriv.h"
30 #include "src/core/SkTraceEvent.h"
31 #include "src/gpu/SkBackingFit.h"
32 #include "src/gpu/Swizzle.h"
33 #include "src/gpu/ganesh/GrAppliedClip.h"
34 #include "src/gpu/ganesh/GrBuffer.h"
35 #include "src/gpu/ganesh/GrCaps.h"
36 #include "src/gpu/ganesh/GrColorSpaceXform.h"
37 #include "src/gpu/ganesh/GrDrawOpTest.h"
38 #include "src/gpu/ganesh/GrFragmentProcessor.h"
39 #include "src/gpu/ganesh/GrGeometryProcessor.h"
40 #include "src/gpu/ganesh/GrMeshDrawTarget.h"
41 #include "src/gpu/ganesh/GrOpFlushState.h"
42 #include "src/gpu/ganesh/GrOpsTypes.h"
43 #include "src/gpu/ganesh/GrPaint.h"
44 #include "src/gpu/ganesh/GrPipeline.h"
45 #include "src/gpu/ganesh/GrProcessorSet.h"
46 #include "src/gpu/ganesh/GrProgramInfo.h"
47 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
48 #include "src/gpu/ganesh/GrResourceProvider.h"
49 #include "src/gpu/ganesh/GrSurfaceProxy.h"
50 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
51 #include "src/gpu/ganesh/GrTextureProxy.h"
52 #include "src/gpu/ganesh/GrXferProcessor.h"
53 #include "src/gpu/ganesh/SkGr.h"
54 #include "src/gpu/ganesh/SurfaceDrawContext.h"
55 #include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h"
56 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
57 #include "src/gpu/ganesh/geometry/GrQuad.h"
58 #include "src/gpu/ganesh/geometry/GrQuadBuffer.h"
59 #include "src/gpu/ganesh/geometry/GrQuadUtils.h"
60 #include "src/gpu/ganesh/geometry/GrRect.h"
61 #include "src/gpu/ganesh/ops/FillRectOp.h"
62 #include "src/gpu/ganesh/ops/GrMeshDrawOp.h"
63 #include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h"
64 #include "src/gpu/ganesh/ops/QuadPerEdgeAA.h"
65 
66 #if defined(GPU_TEST_UTILS)
67 #include "src/base/SkRandom.h"
68 #include "src/gpu/ganesh/GrProxyProvider.h"
69 #include "src/gpu/ganesh/GrTestUtils.h"
70 #endif
71 
72 #include <algorithm>
73 #include <cmath>
74 #include <cstring>
75 #include <limits>
76 #include <memory>
77 #include <new>
78 #include <utility>
79 
80 class GrDstProxyView;
81 
82 using namespace skgpu::ganesh;
83 
84 namespace {
85 
86 using Subset = skgpu::ganesh::QuadPerEdgeAA::Subset;
87 using VertexSpec = skgpu::ganesh::QuadPerEdgeAA::VertexSpec;
88 using ColorType = skgpu::ganesh::QuadPerEdgeAA::ColorType;
89 
90 // Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
91 // between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
axis_aligned_quad_size(const GrQuad & quad)92 SkSize axis_aligned_quad_size(const GrQuad& quad) {
93     SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
94     // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
95     float dw = std::fabs(quad.x(2) - quad.x(0)) + std::fabs(quad.y(2) - quad.y(0));
96     float dh = std::fabs(quad.x(1) - quad.x(0)) + std::fabs(quad.y(1) - quad.y(0));
97     return {dw, dh};
98 }
99 
100 // Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
101 // regular and rectangular textures, w/ or w/o origin correction.
102 struct NormalizationParams {
103     float fIW; // 1 / width of texture, or 1.0 for texture rectangles
104     float fInvH; // 1 / height of texture, or 1.0 for tex rects, X -1 if bottom-left origin
105     float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
106 };
proxy_normalization_params(const GrSurfaceProxy * proxy,GrSurfaceOrigin origin)107 NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
108                                                GrSurfaceOrigin origin) {
109     // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
110     // normalize the src coordinates up front.
111     SkISize dimensions = proxy->backingStoreDimensions();
112     float iw, ih, h;
113     if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
114         iw = ih = 1.f;
115         h = dimensions.height();
116     } else {
117         iw = 1.f / dimensions.width();
118         ih = 1.f / dimensions.height();
119         h = 1.f;
120     }
121 
122     if (origin == kBottomLeft_GrSurfaceOrigin) {
123         return {iw, -ih, h};
124     } else {
125         return {iw, ih, 0.0f};
126     }
127 }
128 
129 // Normalize the subset. If 'subsetRect' is null, it is assumed no subset constraint is desired,
130 // so a sufficiently large rect is returned even if the quad ends up batched with an op that uses
131 // subsets overall. When there is a subset it will be inset based on the filter mode. Normalization
132 // and y-flipping are applied as indicated by NormalizationParams.
normalize_and_inset_subset(GrSamplerState::Filter filter,const NormalizationParams & params,const SkRect * subsetRect)133 SkRect normalize_and_inset_subset(GrSamplerState::Filter filter,
134                                   const NormalizationParams& params,
135                                   const SkRect* subsetRect) {
136     static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
137     if (!subsetRect) {
138         // Either the quad has no subset constraint and is batched with a subset constrained op
139         // (in which case we want a subset that doesn't restrict normalized tex coords), or the
140         // entire op doesn't use the subset, in which case the returned value is ignored.
141         return kLargeRect;
142     }
143 
144     auto ltrb = skvx::Vec<4, float>::Load(subsetRect);
145     auto flipHi = skvx::Vec<4, float>({1.f, 1.f, -1.f, -1.f});
146     if (filter == GrSamplerState::Filter::kNearest) {
147         // Make sure our insetting puts us at pixel centers.
148         ltrb = skvx::floor(ltrb*flipHi)*flipHi;
149     }
150     // Inset with pin to the rect center.
151     ltrb += skvx::Vec<4, float>({ GrTextureEffect::kLinearInset,  GrTextureEffect::kLinearInset,
152                                  -GrTextureEffect::kLinearInset, -GrTextureEffect::kLinearInset});
153     auto mid = (skvx::shuffle<2, 3, 0, 1>(ltrb) + ltrb)*0.5f;
154     ltrb = skvx::min(ltrb*flipHi, mid*flipHi)*flipHi;
155 
156     // Normalize and offset
157     ltrb = ltrb * skvx::Vec<4, float>{params.fIW, params.fInvH, params.fIW, params.fInvH} +
158                skvx::Vec<4, float>{0.f, params.fYOffset, 0.f, params.fYOffset};
159     if (params.fInvH < 0.f) {
160         // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
161         ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
162     }
163 
164     SkRect out;
165     ltrb.store(&out);
166     return out;
167 }
168 
169 // Normalizes logical src coords and corrects for origin
normalize_src_quad(const NormalizationParams & params,GrQuad * srcQuad)170 void normalize_src_quad(const NormalizationParams& params,
171                         GrQuad* srcQuad) {
172     // The src quad should not have any perspective
173     SkASSERT(!srcQuad->hasPerspective());
174     skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
175     skvx::Vec<4, float> ys = srcQuad->y4f() * params.fInvH + params.fYOffset;
176     xs.store(srcQuad->xs());
177     ys.store(srcQuad->ys());
178 }
179 
180 // Count the number of proxy runs in the entry set. This usually is already computed by
181 // SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
182 // for each split.
proxy_run_count(const GrTextureSetEntry set[],int count)183 int proxy_run_count(const GrTextureSetEntry set[], int count) {
184     int actualProxyRunCount = 0;
185     const GrSurfaceProxy* lastProxy = nullptr;
186     for (int i = 0; i < count; ++i) {
187         if (set[i].fProxyView.proxy() != lastProxy) {
188             actualProxyRunCount++;
189             lastProxy = set[i].fProxyView.proxy();
190         }
191     }
192     return actualProxyRunCount;
193 }
194 
safe_to_ignore_subset_rect(GrAAType aaType,GrSamplerState::Filter filter,const DrawQuad & quad,const SkRect & subsetRect)195 bool safe_to_ignore_subset_rect(GrAAType aaType, GrSamplerState::Filter filter,
196                                 const DrawQuad& quad, const SkRect& subsetRect) {
197     // If both the device and local quad are both axis-aligned, and filtering is off, the local quad
198     // can push all the way up to the edges of the the subset rect and the sampler shouldn't
199     // overshoot. Unfortunately, antialiasing adds enough jitter that we can only rely on this in
200     // the non-antialiased case.
201     SkRect localBounds = quad.fLocal.bounds();
202     if (aaType == GrAAType::kNone &&
203         filter == GrSamplerState::Filter::kNearest &&
204         quad.fDevice.quadType() == GrQuad::Type::kAxisAligned &&
205         quad.fLocal.quadType() == GrQuad::Type::kAxisAligned &&
206         subsetRect.contains(localBounds)) {
207 
208         return true;
209     }
210 
211     // If the local quad is inset by at least 0.5 pixels into the subset rect's bounds, the
212     // sampler shouldn't overshoot, even when antialiasing and filtering is taken into account.
213     if (subsetRect.makeInset(GrTextureEffect::kLinearInset,
214                              GrTextureEffect::kLinearInset)
215                   .contains(localBounds)) {
216         return true;
217     }
218 
219     // The subset rect cannot be ignored safely.
220     return false;
221 }
222 
223 /**
224  * Op that implements TextureOp::Make. It draws textured quads. Each quad can modulate against a
225  * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
226  */
227 class TextureOpImpl final : public GrMeshDrawOp {
228 public:
229     using Saturate = TextureOp::Saturate;
230 
Make(GrRecordingContext * context,GrSurfaceProxyView proxyView,sk_sp<GrColorSpaceXform> textureXform,GrSamplerState::Filter filter,GrSamplerState::MipmapMode mm,const SkPMColor4f & color,Saturate saturate,GrAAType aaType,DrawQuad * quad,const SkRect * subset)231     static GrOp::Owner Make(GrRecordingContext* context,
232                             GrSurfaceProxyView proxyView,
233                             sk_sp<GrColorSpaceXform> textureXform,
234                             GrSamplerState::Filter filter,
235                             GrSamplerState::MipmapMode mm,
236                             const SkPMColor4f& color,
237                             Saturate saturate,
238                             GrAAType aaType,
239                             DrawQuad* quad,
240                             const SkRect* subset) {
241 
242         return GrOp::Make<TextureOpImpl>(context, std::move(proxyView), std::move(textureXform),
243                                          filter, mm, color, saturate, aaType, quad, subset);
244     }
245 
Make(GrRecordingContext * context,GrTextureSetEntry set[],int cnt,int proxyRunCnt,GrSamplerState::Filter filter,GrSamplerState::MipmapMode mm,Saturate saturate,GrAAType aaType,SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> textureColorSpaceXform)246     static GrOp::Owner Make(GrRecordingContext* context,
247                             GrTextureSetEntry set[],
248                             int cnt,
249                             int proxyRunCnt,
250                             GrSamplerState::Filter filter,
251                             GrSamplerState::MipmapMode mm,
252                             Saturate saturate,
253                             GrAAType aaType,
254                             SkCanvas::SrcRectConstraint constraint,
255                             const SkMatrix& viewMatrix,
256                             sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
257         // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
258         SkASSERT(proxyRunCnt <= cnt);
259         return GrOp::MakeWithExtraMemory<TextureOpImpl>(
260                 context, sizeof(ViewCountPair) * (proxyRunCnt - 1),
261                 set, cnt, proxyRunCnt, filter, mm, saturate, aaType, constraint,
262                 viewMatrix, std::move(textureColorSpaceXform));
263     }
264 
~TextureOpImpl()265     ~TextureOpImpl() override {
266         for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
267             fViewCountPairs[p].~ViewCountPair();
268         }
269     }
270 
name() const271     const char* name() const override { return "TextureOp"; }
272 
visitProxies(const GrVisitProxyFunc & func) const273     void visitProxies(const GrVisitProxyFunc& func) const override {
274         bool mipped = (fMetadata.mipmapMode() != GrSamplerState::MipmapMode::kNone);
275         for (unsigned p = 0; p <  fMetadata.fProxyCount; ++p) {
276             func(fViewCountPairs[p].fProxy.get(), skgpu::Mipmapped(mipped));
277         }
278         if (fDesc && fDesc->fProgramInfo) {
279             fDesc->fProgramInfo->visitFPProxies(func);
280         }
281     }
282 
283 #ifdef SK_DEBUG
ValidateResourceLimits()284     static void ValidateResourceLimits() {
285         // The op implementation has an upper bound on the number of quads that it can represent.
286         // However, the resource manager imposes its own limit on the number of quads, which should
287         // always be lower than the numerical limit this op can hold.
288         using CountStorage = decltype(Metadata::fTotalQuadCount);
289         CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
290         // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
291         int resourceLimit = SkTo<int>(maxQuadCount);
292         SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
293                  GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
294     }
295 #endif
296 
finalize(const GrCaps & caps,const GrAppliedClip *,GrClampType clampType)297     GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip*,
298                                       GrClampType clampType) override {
299         SkASSERT(fMetadata.colorType() == ColorType::kNone);
300         auto iter = fQuads.metadata();
301         while(iter.next()) {
302             auto colorType = skgpu::ganesh::QuadPerEdgeAA::MinColorType(iter->fColor);
303             colorType = std::max(static_cast<ColorType>(fMetadata.fColorType),
304                                  colorType);
305             if (caps.reducedShaderMode()) {
306                 colorType = std::max(colorType, ColorType::kByte);
307             }
308             fMetadata.fColorType = static_cast<uint16_t>(colorType);
309         }
310         return GrProcessorSet::EmptySetAnalysis();
311     }
312 
fixedFunctionFlags() const313     FixedFunctionFlags fixedFunctionFlags() const override {
314         return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
315                                                      : FixedFunctionFlags::kNone;
316     }
317 
318     DEFINE_OP_CLASS_ID
319 
320 private:
321     friend class ::GrOp;
322 
323     struct ColorSubsetAndAA {
ColorSubsetAndAA__anon00ec69f70111::TextureOpImpl::ColorSubsetAndAA324         ColorSubsetAndAA(const SkPMColor4f& color, const SkRect& subsetRect, GrQuadAAFlags aaFlags)
325                 : fColor(color)
326                 , fSubsetRect(subsetRect)
327                 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
328             SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
329         }
330 
331         SkPMColor4f fColor;
332         // If the op doesn't use subsets, this is ignored. If the op uses subsets and the specific
333         // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
334         SkRect fSubsetRect;
335         unsigned fAAFlags : 4;
336 
aaFlags__anon00ec69f70111::TextureOpImpl::ColorSubsetAndAA337         GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
338     };
339 
340     struct ViewCountPair {
341         // Normally this would be a GrSurfaceProxyView, but TextureOp applies the GrOrigin right
342         // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
343         // swizzle so that is stored in the op metadata.
344         sk_sp<GrSurfaceProxy> fProxy;
345         int fQuadCnt;
346     };
347 
348     // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
349     // increase the size of the op; increasing the op size can have a surprising impact on
350     // performance (since texture ops are one of the most commonly used in an app).
351     struct Metadata {
352         // AAType must be filled after initialization; ColorType is determined in finalize()
Metadata__anon00ec69f70111::TextureOpImpl::Metadata353         Metadata(const skgpu::Swizzle& swizzle,
354                  GrSamplerState::Filter filter,
355                  GrSamplerState::MipmapMode mm,
356                  Subset subset,
357                  Saturate saturate)
358             : fSwizzle(swizzle)
359             , fProxyCount(1)
360             , fTotalQuadCount(1)
361             , fFilter(static_cast<uint16_t>(filter))
362             , fMipmapMode(static_cast<uint16_t>(mm))
363             , fAAType(static_cast<uint16_t>(GrAAType::kNone))
364             , fColorType(static_cast<uint16_t>(ColorType::kNone))
365             , fSubset(static_cast<uint16_t>(subset))
366             , fSaturate(static_cast<uint16_t>(saturate)) {}
367 
368         skgpu::Swizzle fSwizzle; // sizeof(skgpu::Swizzle) == uint16_t
369         uint16_t  fProxyCount;
370         // This will be >= fProxyCount, since a proxy may be drawn multiple times
371         uint16_t  fTotalQuadCount;
372 
373         // These must be based on uint16_t to help MSVC's pack bitfields optimally
374         uint16_t  fFilter     : 2; // GrSamplerState::Filter
375         uint16_t  fMipmapMode : 2; // GrSamplerState::MipmapMode
376         uint16_t  fAAType     : 2; // GrAAType
377         uint16_t  fColorType  : 2; // GrQuadPerEdgeAA::ColorType
378         uint16_t  fSubset     : 1; // bool
379         uint16_t  fSaturate   : 1; // bool
380         uint16_t  fUnused     : 6; // # of bits left before Metadata exceeds 8 bytes
381 
filter__anon00ec69f70111::TextureOpImpl::Metadata382         GrSamplerState::Filter filter() const {
383             return static_cast<GrSamplerState::Filter>(fFilter);
384         }
mipmapMode__anon00ec69f70111::TextureOpImpl::Metadata385         GrSamplerState::MipmapMode mipmapMode() const {
386             return static_cast<GrSamplerState::MipmapMode>(fMipmapMode);
387         }
aaType__anon00ec69f70111::TextureOpImpl::Metadata388         GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
colorType__anon00ec69f70111::TextureOpImpl::Metadata389         ColorType colorType() const { return static_cast<ColorType>(fColorType); }
subset__anon00ec69f70111::TextureOpImpl::Metadata390         Subset subset() const { return static_cast<Subset>(fSubset); }
saturate__anon00ec69f70111::TextureOpImpl::Metadata391         Saturate saturate() const { return static_cast<Saturate>(fSaturate); }
392 
393         static_assert(GrSamplerState::kFilterCount <= 4);
394         static_assert(kGrAATypeCount <= 4);
395         static_assert(skgpu::ganesh::QuadPerEdgeAA::kColorTypeCount <= 4);
396     };
397     static_assert(sizeof(Metadata) == 8);
398 
399     // This descriptor is used to store the draw info we decide on during on(Pre)PrepareDraws. We
400     // store the data in a separate struct in order to minimize the size of the TextureOp.
401     // Historically, increasing the TextureOp's size has caused surprising perf regressions, but we
402     // may want to re-evaluate whether this is still necessary.
403     //
404     // In the onPrePrepareDraws case it is allocated in the creation-time opData arena, and
405     // allocatePrePreparedVertices is also called.
406     //
407     // In the onPrepareDraws case this descriptor is allocated in the flush-time arena (i.e., as
408     // part of the flushState).
409     struct Desc {
410         VertexSpec fVertexSpec;
411         int fNumProxies = 0;
412         int fNumTotalQuads = 0;
413 
414         // This member variable is only used by 'onPrePrepareDraws'.
415         char* fPrePreparedVertices = nullptr;
416 
417         GrProgramInfo* fProgramInfo = nullptr;
418 
419         sk_sp<const GrBuffer> fIndexBuffer;
420         sk_sp<const GrBuffer> fVertexBuffer;
421         int fBaseVertex;
422 
423         // How big should 'fVertices' be to hold all the vertex data?
totalSizeInBytes__anon00ec69f70111::TextureOpImpl::Desc424         size_t totalSizeInBytes() const {
425             return this->totalNumVertices() * fVertexSpec.vertexSize();
426         }
427 
totalNumVertices__anon00ec69f70111::TextureOpImpl::Desc428         int totalNumVertices() const {
429             return fNumTotalQuads * fVertexSpec.verticesPerQuad();
430         }
431 
allocatePrePreparedVertices__anon00ec69f70111::TextureOpImpl::Desc432         void allocatePrePreparedVertices(SkArenaAlloc* arena) {
433             fPrePreparedVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
434         }
435     };
436     // If subsetRect is not null it will be used to apply a strict src rect-style constraint.
TextureOpImpl(GrSurfaceProxyView proxyView,sk_sp<GrColorSpaceXform> textureColorSpaceXform,GrSamplerState::Filter filter,GrSamplerState::MipmapMode mm,const SkPMColor4f & color,Saturate saturate,GrAAType aaType,DrawQuad * quad,const SkRect * subsetRect)437     TextureOpImpl(GrSurfaceProxyView proxyView,
438                   sk_sp<GrColorSpaceXform> textureColorSpaceXform,
439                   GrSamplerState::Filter filter,
440                   GrSamplerState::MipmapMode mm,
441                   const SkPMColor4f& color,
442                   Saturate saturate,
443                   GrAAType aaType,
444                   DrawQuad* quad,
445                   const SkRect* subsetRect)
446             : INHERITED(ClassID())
447             , fQuads(1, true /* includes locals */)
448             , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
449             , fDesc(nullptr)
450             , fMetadata(proxyView.swizzle(), filter, mm, Subset(!!subsetRect), saturate) {
451         // Clean up disparities between the overall aa type and edge configuration and apply
452         // optimizations based on the rect and matrix when appropriate
453         GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
454                                    &aaType, &quad->fEdgeFlags);
455         fMetadata.fAAType = static_cast<uint16_t>(aaType);
456 
457         // We expect our caller to have already caught this optimization.
458         SkASSERT(!subsetRect ||
459                  !subsetRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
460 
461         // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
462         // Try to identify cases where the subsetting isn't actually necessary, and skip it.
463         if (subsetRect) {
464             if (safe_to_ignore_subset_rect(aaType, filter, *quad, *subsetRect)) {
465                 subsetRect = nullptr;
466                 fMetadata.fSubset = static_cast<uint16_t>(Subset::kNo);
467             }
468         }
469 
470         // Normalize src coordinates and the subset (if set)
471         NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
472                                                                 proxyView.origin());
473         normalize_src_quad(params, &quad->fLocal);
474         SkRect subset = normalize_and_inset_subset(filter, params, subsetRect);
475 
476         // Set bounds before clipping so we don't have to worry about unioning the bounds of
477         // the two potential quads (GrQuad::bounds() is perspective-safe).
478         bool hairline = GrQuadUtils::WillUseHairline(quad->fDevice, aaType, quad->fEdgeFlags);
479         this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
480                         hairline ? IsHairline::kYes : IsHairline::kNo);
481         int quadCount = this->appendQuad(quad, color, subset);
482         fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
483     }
484 
TextureOpImpl(GrTextureSetEntry set[],int cnt,int proxyRunCnt,const GrSamplerState::Filter filter,const GrSamplerState::MipmapMode mm,const Saturate saturate,const GrAAType aaType,const SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> textureColorSpaceXform)485     TextureOpImpl(GrTextureSetEntry set[],
486                   int cnt,
487                   int proxyRunCnt,
488                   const GrSamplerState::Filter filter,
489                   const GrSamplerState::MipmapMode mm,
490                   const Saturate saturate,
491                   const GrAAType aaType,
492                   const SkCanvas::SrcRectConstraint constraint,
493                   const SkMatrix& viewMatrix,
494                   sk_sp<GrColorSpaceXform> textureColorSpaceXform)
495             : INHERITED(ClassID())
496             , fQuads(cnt, true /* includes locals */)
497             , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
498             , fDesc(nullptr)
499             , fMetadata(set[0].fProxyView.swizzle(),
500                         GrSamplerState::Filter::kNearest,
501                         GrSamplerState::MipmapMode::kNone,
502                         Subset::kNo,
503                         saturate) {
504         // Update counts to reflect the batch op
505         fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
506         fMetadata.fTotalQuadCount = SkToUInt(cnt);
507 
508         SkRect bounds = SkRectPriv::MakeLargestInverted();
509 
510         GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
511         Subset netSubset = Subset::kNo;
512         GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
513         GrSamplerState::MipmapMode netMM = GrSamplerState::MipmapMode::kNone;
514         bool hasSubpixel = false;
515 
516         const GrSurfaceProxy* curProxy = nullptr;
517 
518         // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
519         // increases when set[q]'s proxy changes.
520         int p = 0;
521         for (int q = 0; q < cnt; ++q) {
522             SkASSERT(mm == GrSamplerState::MipmapMode::kNone ||
523                      (set[0].fProxyView.proxy()->asTextureProxy()->mipmapped() ==
524                       skgpu::Mipmapped::kYes));
525             if (q == 0) {
526                 // We do not placement new the first ViewCountPair since that one is allocated and
527                 // initialized as part of the TextureOp creation.
528                 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
529                 fViewCountPairs[0].fQuadCnt = 0;
530                 curProxy = fViewCountPairs[0].fProxy.get();
531             } else if (set[q].fProxyView.proxy() != curProxy) {
532                 // We must placement new the ViewCountPairs here so that the sk_sps in the
533                 // GrSurfaceProxyView get initialized properly.
534                 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
535 
536                 curProxy = fViewCountPairs[p].fProxy.get();
537                 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
538                         curProxy, fViewCountPairs[0].fProxy.get()));
539                 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
540             } // else another quad referencing the same proxy
541 
542             SkMatrix ctm = viewMatrix;
543             if (set[q].fPreViewMatrix) {
544                 ctm.preConcat(*set[q].fPreViewMatrix);
545             }
546 
547             // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
548             // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
549             DrawQuad quad;
550             if (set[q].fDstClipQuad) {
551                 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
552 
553                 SkPoint srcPts[4];
554                 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
555                 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
556             } else {
557                 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
558                 quad.fLocal = GrQuad(set[q].fSrcRect);
559             }
560 
561             // This may be reduced per-quad from the requested aggregate filtering level, and used
562             // to determine if the subset is needed for the entry as well.
563             GrSamplerState::Filter filterForQuad = filter;
564             if (netFilter != filter || netMM != mm) {
565                 // The only way netFilter != filter is if linear is requested and we haven't yet
566                 // found a quad that requires linear (so net is still nearest). Similar for mip
567                 // mapping.
568                 SkASSERT(filter == netFilter ||
569                          (netFilter == GrSamplerState::Filter::kNearest && filter > netFilter));
570                 SkASSERT(mm == netMM ||
571                          (netMM == GrSamplerState::MipmapMode::kNone && mm > netMM));
572                 auto [mustFilter, mustMM] = FilterAndMipmapHaveNoEffect(quad.fLocal, quad.fDevice);
573                 if (filter != GrSamplerState::Filter::kNearest) {
574                     if (mustFilter) {
575                         netFilter = filter; // upgrade batch to higher filter level
576                     } else {
577                         filterForQuad = GrSamplerState::Filter::kNearest; // downgrade entry
578                     }
579                 }
580                 if (mustMM && mm != GrSamplerState::MipmapMode::kNone) {
581                     netMM = mm;
582                 }
583             }
584 
585             // Determine the AA type for the quad, then merge with net AA type
586             GrAAType aaForQuad;
587             GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
588                                        &aaForQuad, &quad.fEdgeFlags);
589             // Update overall bounds of the op as the union of all quads
590             bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
591             hasSubpixel |= GrQuadUtils::WillUseHairline(quad.fDevice, aaForQuad, quad.fEdgeFlags);
592 
593             // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
594             SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
595             if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
596                 netAAType = aaType;
597             }
598 
599             // Calculate metadata for the entry
600             const SkRect* subsetForQuad = nullptr;
601             if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
602                 // Check (briefly) if the subset rect is actually needed for this set entry.
603                 SkRect* subsetRect = &set[q].fSrcRect;
604                 if (!subsetRect->contains(curProxy->backingStoreBoundsRect())) {
605                     if (!safe_to_ignore_subset_rect(aaForQuad, filterForQuad, quad, *subsetRect)) {
606                         netSubset = Subset::kYes;
607                         subsetForQuad = subsetRect;
608                     }
609                 }
610             }
611 
612             // Normalize the src quads and apply origin
613             NormalizationParams proxyParams = proxy_normalization_params(
614                     curProxy, set[q].fProxyView.origin());
615             normalize_src_quad(proxyParams, &quad.fLocal);
616 
617             // This subset may represent a no-op, otherwise it will have the origin and dimensions
618             // of the texture applied to it.
619             SkRect subset = normalize_and_inset_subset(filter, proxyParams, subsetForQuad);
620 
621             // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
622             // ViewCountPair (this frequently happens when Chrome draws 9-patches).
623             fViewCountPairs[p].fQuadCnt += this->appendQuad(&quad, set[q].fColor, subset);
624         }
625         // The # of proxy switches should match what was provided (+1 because we incremented p
626         // when a new proxy was encountered).
627         SkASSERT((p + 1) == fMetadata.fProxyCount);
628         SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
629 
630         fMetadata.fAAType = static_cast<uint16_t>(netAAType);
631         fMetadata.fFilter = static_cast<uint16_t>(netFilter);
632         fMetadata.fSubset = static_cast<uint16_t>(netSubset);
633 
634         this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage),
635                         hasSubpixel ? IsHairline::kYes : IsHairline::kNo);
636     }
637 
appendQuad(DrawQuad * quad,const SkPMColor4f & color,const SkRect & subset)638     int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& subset) {
639         DrawQuad extra;
640         // Always clip to W0 to stay consistent with GrQuad::bounds
641         int quadCount = GrQuadUtils::ClipToW0(quad, &extra);
642         if (quadCount == 0) {
643             // We can't discard the op at this point, but disable AA flags so it won't go through
644             // inset/outset processing
645             quad->fEdgeFlags = GrQuadAAFlags::kNone;
646             quadCount = 1;
647         }
648         fQuads.append(quad->fDevice, {color, subset, quad->fEdgeFlags},  &quad->fLocal);
649         if (quadCount > 1) {
650             fQuads.append(extra.fDevice, {color, subset, extra.fEdgeFlags}, &extra.fLocal);
651             fMetadata.fTotalQuadCount++;
652         }
653         return quadCount;
654     }
655 
programInfo()656     GrProgramInfo* programInfo() override {
657         // Although this Op implements its own onPrePrepareDraws it calls GrMeshDrawOps' version so
658         // this entry point will be called.
659         return (fDesc) ? fDesc->fProgramInfo : nullptr;
660     }
661 
onCreateProgramInfo(const GrCaps * caps,SkArenaAlloc * arena,const GrSurfaceProxyView & writeView,bool usesMSAASurface,GrAppliedClip && appliedClip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)662     void onCreateProgramInfo(const GrCaps* caps,
663                              SkArenaAlloc* arena,
664                              const GrSurfaceProxyView& writeView,
665                              bool usesMSAASurface,
666                              GrAppliedClip&& appliedClip,
667                              const GrDstProxyView& dstProxyView,
668                              GrXferBarrierFlags renderPassXferBarriers,
669                              GrLoadOp colorLoadOp) override {
670         SkASSERT(fDesc);
671 
672         GrGeometryProcessor* gp;
673 
674         {
675             const GrBackendFormat& backendFormat =
676                     fViewCountPairs[0].fProxy->backendFormat();
677 
678             GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
679                                                          fMetadata.filter());
680 
681             gp = skgpu::ganesh::QuadPerEdgeAA::MakeTexturedProcessor(
682                     arena,
683                     fDesc->fVertexSpec,
684                     *caps->shaderCaps(),
685                     backendFormat,
686                     samplerState,
687                     fMetadata.fSwizzle,
688                     std::move(fTextureColorSpaceXform),
689                     fMetadata.saturate());
690 
691             SkASSERT(fDesc->fVertexSpec.vertexSize() == gp->vertexStride());
692         }
693 
694         fDesc->fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
695                 caps, arena, writeView, usesMSAASurface, std::move(appliedClip), dstProxyView, gp,
696                 GrProcessorSet::MakeEmptySet(), fDesc->fVertexSpec.primitiveType(),
697                 renderPassXferBarriers, colorLoadOp, GrPipeline::InputFlags::kNone);
698     }
699 
onPrePrepareDraws(GrRecordingContext * context,const GrSurfaceProxyView & writeView,GrAppliedClip * clip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)700     void onPrePrepareDraws(GrRecordingContext* context,
701                            const GrSurfaceProxyView& writeView,
702                            GrAppliedClip* clip,
703                            const GrDstProxyView& dstProxyView,
704                            GrXferBarrierFlags renderPassXferBarriers,
705                            GrLoadOp colorLoadOp) override {
706         TRACE_EVENT0("skia.gpu", TRACE_FUNC);
707 
708         SkDEBUGCODE(this->validate();)
709         SkASSERT(!fDesc);
710 
711         SkArenaAlloc* arena = context->priv().recordTimeAllocator();
712 
713         fDesc = arena->make<Desc>();
714         this->characterize(fDesc);
715         fDesc->allocatePrePreparedVertices(arena);
716         FillInVertices(*context->priv().caps(), this, fDesc, fDesc->fPrePreparedVertices);
717 
718         // This will call onCreateProgramInfo and register the created program with the DDL.
719         this->INHERITED::onPrePrepareDraws(context, writeView, clip, dstProxyView,
720                                            renderPassXferBarriers, colorLoadOp);
721     }
722 
FillInVertices(const GrCaps & caps,TextureOpImpl * texOp,Desc * desc,char * vertexData)723     static void FillInVertices(const GrCaps& caps,
724                                TextureOpImpl* texOp,
725                                Desc* desc,
726                                char* vertexData) {
727         SkASSERT(vertexData);
728 
729         SkDEBUGCODE(int totQuadsSeen = 0;) SkDEBUGCODE(int totVerticesSeen = 0;)
730                 SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize();)
731                         SkDEBUGCODE(auto startMark{vertexData};)
732 
733                                 skgpu::ganesh::QuadPerEdgeAA::Tessellator tessellator(
734                                         desc->fVertexSpec, vertexData);
735         for (const auto& op : ChainRange<TextureOpImpl>(texOp)) {
736             auto iter = op.fQuads.iterator();
737             for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
738                 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
739                 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
740 
741                 for (int i = 0; i < quadCnt && iter.next(); ++i) {
742                     SkASSERT(iter.isLocalValid());
743                     const ColorSubsetAndAA& info = iter.metadata();
744 
745                     tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
746                                        info.fSubsetRect, info.aaFlags());
747                 }
748 
749                 SkASSERT((totVerticesSeen + meshVertexCnt) * vertexSize
750                          == (size_t)(tessellator.vertexMark() - startMark));
751 
752                 SkDEBUGCODE(totQuadsSeen += quadCnt;)
753                 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
754                 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
755             }
756 
757             // If quad counts per proxy were calculated correctly, the entire iterator
758             // should have been consumed.
759             SkASSERT(!iter.next());
760         }
761 
762         SkASSERT(desc->totalSizeInBytes() == (size_t)(tessellator.vertexMark() - startMark));
763         SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
764         SkASSERT(totVerticesSeen == desc->totalNumVertices());
765     }
766 
767 #ifdef SK_DEBUG
validate_op(GrTextureType textureType,GrAAType aaType,skgpu::Swizzle swizzle,const TextureOpImpl * op)768     static int validate_op(GrTextureType textureType,
769                            GrAAType aaType,
770                            skgpu::Swizzle swizzle,
771                            const TextureOpImpl* op) {
772         SkASSERT(op->fMetadata.fSwizzle == swizzle);
773 
774         int quadCount = 0;
775         for (unsigned p = 0; p < op->fMetadata.fProxyCount; ++p) {
776             auto* proxy = op->fViewCountPairs[p].fProxy->asTextureProxy();
777             quadCount += op->fViewCountPairs[p].fQuadCnt;
778             SkASSERT(proxy);
779             SkASSERT(proxy->textureType() == textureType);
780         }
781 
782         SkASSERT(aaType == op->fMetadata.aaType());
783         return quadCount;
784     }
785 
validate() const786     void validate() const override {
787         // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
788         auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
789         GrAAType aaType = fMetadata.aaType();
790         skgpu::Swizzle swizzle = fMetadata.fSwizzle;
791 
792         int quadCount = validate_op(textureType, aaType, swizzle, this);
793 
794         for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
795             quadCount += validate_op(textureType, aaType, swizzle,
796                                      static_cast<const TextureOpImpl*>(tmp));
797         }
798 
799         for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
800             quadCount += validate_op(textureType, aaType, swizzle,
801                                      static_cast<const TextureOpImpl*>(tmp));
802         }
803 
804         SkASSERT(quadCount == this->numChainedQuads());
805     }
806 
807 #endif
808 
809 #if defined(GPU_TEST_UTILS)
numQuads() const810     int numQuads() const final { return this->totNumQuads(); }
811 #endif
812 
characterize(Desc * desc) const813     void characterize(Desc* desc) const {
814         SkDEBUGCODE(this->validate();)
815 
816         GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
817         ColorType colorType = ColorType::kNone;
818         GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
819         Subset subset = Subset::kNo;
820         GrAAType overallAAType = fMetadata.aaType();
821 
822         desc->fNumProxies = 0;
823         desc->fNumTotalQuads = 0;
824         int maxQuadsPerMesh = 0;
825 
826         for (const auto& op : ChainRange<TextureOpImpl>(this)) {
827             if (op.fQuads.deviceQuadType() > quadType) {
828                 quadType = op.fQuads.deviceQuadType();
829             }
830             if (op.fQuads.localQuadType() > srcQuadType) {
831                 srcQuadType = op.fQuads.localQuadType();
832             }
833             if (op.fMetadata.subset() == Subset::kYes) {
834                 subset = Subset::kYes;
835             }
836             colorType = std::max(colorType, op.fMetadata.colorType());
837             desc->fNumProxies += op.fMetadata.fProxyCount;
838 
839             for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
840                 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
841             }
842             desc->fNumTotalQuads += op.totNumQuads();
843 
844             if (op.fMetadata.aaType() == GrAAType::kCoverage) {
845                 overallAAType = GrAAType::kCoverage;
846             }
847         }
848 
849         SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
850 
851         SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
852 
853         auto indexBufferOption =
854                 skgpu::ganesh::QuadPerEdgeAA::CalcIndexBufferOption(overallAAType, maxQuadsPerMesh);
855 
856         desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
857                                        subset, overallAAType, /* alpha as coverage */ true,
858                                        indexBufferOption);
859 
860         SkASSERT(desc->fNumTotalQuads <=
861                  skgpu::ganesh::QuadPerEdgeAA::QuadLimit(indexBufferOption));
862     }
863 
totNumQuads() const864     int totNumQuads() const {
865 #ifdef SK_DEBUG
866         int tmp = 0;
867         for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
868             tmp += fViewCountPairs[p].fQuadCnt;
869         }
870         SkASSERT(tmp == fMetadata.fTotalQuadCount);
871 #endif
872 
873         return fMetadata.fTotalQuadCount;
874     }
875 
numChainedQuads() const876     int numChainedQuads() const {
877         int numChainedQuads = this->totNumQuads();
878 
879         for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
880             numChainedQuads += ((const TextureOpImpl*)tmp)->totNumQuads();
881         }
882 
883         for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
884             numChainedQuads += ((const TextureOpImpl*)tmp)->totNumQuads();
885         }
886 
887         return numChainedQuads;
888     }
889 
890     // onPrePrepareDraws may or may not have been called at this point
onPrepareDraws(GrMeshDrawTarget * target)891     void onPrepareDraws(GrMeshDrawTarget* target) override {
892         TRACE_EVENT0("skia.gpu", TRACE_FUNC);
893 
894         SkDEBUGCODE(this->validate();)
895 
896         SkASSERT(!fDesc || fDesc->fPrePreparedVertices);
897 
898         if (!fDesc) {
899             SkArenaAlloc* arena = target->allocator();
900             fDesc = arena->make<Desc>();
901             this->characterize(fDesc);
902             SkASSERT(!fDesc->fPrePreparedVertices);
903         }
904 
905         size_t vertexSize = fDesc->fVertexSpec.vertexSize();
906 
907         void* vdata = target->makeVertexSpace(vertexSize, fDesc->totalNumVertices(),
908                                               &fDesc->fVertexBuffer, &fDesc->fBaseVertex);
909         if (!vdata) {
910             SkDebugf("Could not allocate vertices\n");
911             return;
912         }
913 
914         if (fDesc->fVertexSpec.needsIndexBuffer()) {
915             fDesc->fIndexBuffer = skgpu::ganesh::QuadPerEdgeAA::GetIndexBuffer(
916                     target, fDesc->fVertexSpec.indexBufferOption());
917             if (!fDesc->fIndexBuffer) {
918                 SkDebugf("Could not allocate indices\n");
919                 return;
920             }
921         }
922 
923         if (fDesc->fPrePreparedVertices) {
924             memcpy(vdata, fDesc->fPrePreparedVertices, fDesc->totalSizeInBytes());
925         } else {
926             FillInVertices(target->caps(), this, fDesc, (char*) vdata);
927         }
928     }
929 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)930     void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
931         if (!fDesc->fVertexBuffer) {
932             return;
933         }
934 
935         if (fDesc->fVertexSpec.needsIndexBuffer() && !fDesc->fIndexBuffer) {
936             return;
937         }
938 
939         if (!fDesc->fProgramInfo) {
940             this->createProgramInfo(flushState);
941             SkASSERT(fDesc->fProgramInfo);
942         }
943 
944         flushState->bindPipelineAndScissorClip(*fDesc->fProgramInfo, chainBounds);
945         flushState->bindBuffers(std::move(fDesc->fIndexBuffer), nullptr,
946                                 std::move(fDesc->fVertexBuffer));
947 
948         int totQuadsSeen = 0;
949         SkDEBUGCODE(int numDraws = 0;)
950         for (const auto& op : ChainRange<TextureOpImpl>(this)) {
951             for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
952                 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
953                 SkASSERT(numDraws < fDesc->fNumProxies);
954                 flushState->bindTextures(fDesc->fProgramInfo->geomProc(),
955                                          *op.fViewCountPairs[p].fProxy,
956                                          fDesc->fProgramInfo->pipeline());
957                 skgpu::ganesh::QuadPerEdgeAA::IssueDraw(flushState->caps(),
958                                                         flushState->opsRenderPass(),
959                                                         fDesc->fVertexSpec,
960                                                         totQuadsSeen,
961                                                         quadCnt,
962                                                         fDesc->totalNumVertices(),
963                                                         fDesc->fBaseVertex);
964                 totQuadsSeen += quadCnt;
965                 SkDEBUGCODE(++numDraws;)
966             }
967         }
968 
969         SkASSERT(totQuadsSeen == fDesc->fNumTotalQuads);
970         SkASSERT(numDraws == fDesc->fNumProxies);
971     }
972 
propagateCoverageAAThroughoutChain()973     void propagateCoverageAAThroughoutChain() {
974         fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
975 
976         for (GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
977             auto tex = static_cast<TextureOpImpl*>(tmp);
978             SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
979                      tex->fMetadata.aaType() == GrAAType::kNone);
980             tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
981         }
982 
983         for (GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
984             auto tex = static_cast<TextureOpImpl*>(tmp);
985             SkASSERT(tex->fMetadata.aaType() == GrAAType::kCoverage ||
986                      tex->fMetadata.aaType() == GrAAType::kNone);
987             tex->fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
988         }
989     }
990 
onCombineIfPossible(GrOp * t,SkArenaAlloc *,const GrCaps & caps)991     CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
992         TRACE_EVENT0("skia.gpu", TRACE_FUNC);
993         auto that = t->cast<TextureOpImpl>();
994 
995         SkDEBUGCODE(this->validate();)
996         SkDEBUGCODE(that->validate();)
997 
998         if (fDesc || that->fDesc) {
999             // This should never happen (since only DDL recorded ops should be prePrepared)
1000             // but, in any case, we should never combine ops that that been prePrepared
1001             return CombineResult::kCannotCombine;
1002         }
1003 
1004         if (fMetadata.subset() != that->fMetadata.subset()) {
1005             // It is technically possible to combine operations across subset modes, but performance
1006             // testing suggests it's better to make more draw calls where some take advantage of
1007             // the more optimal shader path without coordinate clamping.
1008             return CombineResult::kCannotCombine;
1009         }
1010         if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
1011                                        that->fTextureColorSpaceXform.get())) {
1012             return CombineResult::kCannotCombine;
1013         }
1014 
1015         bool upgradeToCoverageAAOnMerge = false;
1016         if (fMetadata.aaType() != that->fMetadata.aaType()) {
1017             if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
1018                 return CombineResult::kCannotCombine;
1019             }
1020             upgradeToCoverageAAOnMerge = true;
1021         }
1022 
1023         if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
1024                                           this->numChainedQuads() + that->numChainedQuads())) {
1025             return CombineResult::kCannotCombine;
1026         }
1027 
1028         if (fMetadata.saturate() != that->fMetadata.saturate()) {
1029             return CombineResult::kCannotCombine;
1030         }
1031         if (fMetadata.filter() != that->fMetadata.filter()) {
1032             return CombineResult::kCannotCombine;
1033         }
1034         if (fMetadata.mipmapMode() != that->fMetadata.mipmapMode()) {
1035             return CombineResult::kCannotCombine;
1036         }
1037         if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
1038             return CombineResult::kCannotCombine;
1039         }
1040         const auto* thisProxy = fViewCountPairs[0].fProxy.get();
1041         const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
1042         if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
1043             thisProxy != thatProxy) {
1044             // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
1045             if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
1046                 caps.dynamicStateArrayGeometryProcessorTextureSupport() &&
1047                 fMetadata.aaType() == that->fMetadata.aaType()) {
1048                 // We only allow chaining when the aaTypes match bc otherwise the AA type
1049                 // reported by the chain can be inconsistent. That is, since chaining doesn't
1050                 // propagate revised AA information throughout the chain, the head of the chain
1051                 // could have an AA setting of kNone while the chain as a whole could have a
1052                 // setting of kCoverage. This inconsistency would then interfere with the validity
1053                 // of the CombinedQuadCountWillOverflow calls.
1054                 // This problem doesn't occur w/ merging bc we do propagate the AA information
1055                 // (in propagateCoverageAAThroughoutChain) below.
1056                 return CombineResult::kMayChain;
1057             }
1058             return CombineResult::kCannotCombine;
1059         }
1060 
1061         fMetadata.fSubset |= that->fMetadata.fSubset;
1062         fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
1063 
1064         // Concatenate quad lists together
1065         fQuads.concat(that->fQuads);
1066         fViewCountPairs[0].fQuadCnt += that->fQuads.count();
1067         fMetadata.fTotalQuadCount += that->fQuads.count();
1068 
1069         if (upgradeToCoverageAAOnMerge) {
1070             // This merger may be the start of a concatenation of two chains. When one
1071             // of the chains mutates its AA the other must follow suit or else the above AA
1072             // check may prevent later ops from chaining together. A specific example of this is
1073             // when chain2 is prepended onto chain1:
1074             //  chain1 (that): opA (non-AA/mergeable) opB (non-AA/non-mergeable)
1075             //  chain2 (this): opC (cov-AA/non-mergeable) opD (cov-AA/mergeable)
1076             // W/o this propagation, after opD & opA merge, opB and opC would say they couldn't
1077             // chain - which would stop the concatenation process.
1078             this->propagateCoverageAAThroughoutChain();
1079             that->propagateCoverageAAThroughoutChain();
1080         }
1081 
1082         SkDEBUGCODE(this->validate();)
1083 
1084         return CombineResult::kMerged;
1085     }
1086 
1087 #if defined(GPU_TEST_UTILS)
onDumpInfo() const1088     SkString onDumpInfo() const override {
1089         SkString str = SkStringPrintf("# draws: %d\n", fQuads.count());
1090         auto iter = fQuads.iterator();
1091         for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
1092             SkString proxyStr = fViewCountPairs[p].fProxy->dump();
1093             str.append(proxyStr);
1094             str.appendf(", Filter: %d, MM: %d\n",
1095                         static_cast<int>(fMetadata.fFilter),
1096                         static_cast<int>(fMetadata.fMipmapMode));
1097             for (int i = 0; i < fViewCountPairs[p].fQuadCnt && iter.next(); ++i) {
1098                 const GrQuad* quad = iter.deviceQuad();
1099                 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
1100                 const ColorSubsetAndAA& info = iter.metadata();
1101                 str.appendf(
1102                         "%d: Color: 0x%08x, Subset(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
1103                         "  UVs  [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
1104                         "  Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
1105                         i, info.fColor.toBytes_RGBA(), fMetadata.fSubset, info.fSubsetRect.fLeft,
1106                         info.fSubsetRect.fTop, info.fSubsetRect.fRight, info.fSubsetRect.fBottom,
1107                         quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
1108                         quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
1109                         uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
1110                         uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
1111             }
1112         }
1113         return str;
1114     }
1115 #endif
1116 
1117     GrQuadBuffer<ColorSubsetAndAA> fQuads;
1118     sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
1119     // Most state of TextureOp is packed into these two field to minimize the op's size.
1120     // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1121     // consider/measure changes with care.
1122     Desc* fDesc;
1123     Metadata fMetadata;
1124 
1125     // This field must go last. When allocating this op, we will allocate extra space to hold
1126     // additional ViewCountPairs immediately after the op's allocation so we can treat this
1127     // as an fProxyCnt-length array.
1128     ViewCountPair fViewCountPairs[1];
1129 
1130     using INHERITED = GrMeshDrawOp;
1131 };
1132 
1133 }  // anonymous namespace
1134 
1135 namespace skgpu::ganesh {
1136 
1137 #if defined(GPU_TEST_UTILS)
ClassID()1138 uint32_t TextureOp::ClassID() {
1139     return TextureOpImpl::ClassID();
1140 }
1141 #endif
1142 
FilterAndMipmapHaveNoEffect(const GrQuad & srcQuad,const GrQuad & dstQuad)1143 std::tuple<bool /* filter */, bool /* mipmap */> FilterAndMipmapHaveNoEffect(
1144         const GrQuad& srcQuad, const GrQuad& dstQuad) {
1145     // If not axis-aligned in src or dst, then always say it has an effect
1146     if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
1147         dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
1148         return {true, true};
1149     }
1150 
1151     SkRect srcRect;
1152     SkRect dstRect;
1153     if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
1154         // Disable filtering when there is no scaling (width and height are the same), and the
1155         // top-left corners have the same fraction (so src and dst snap to the pixel grid
1156         // identically).
1157         SkASSERT(srcRect.isSorted());
1158         bool filter = srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
1159                       SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
1160                       SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
1161         bool mm = srcRect.width() > dstRect.width() || srcRect.height() > dstRect.height();
1162         return {filter, mm};
1163     }
1164     // Extract edge lengths
1165     SkSize srcSize = axis_aligned_quad_size(srcQuad);
1166     SkSize dstSize = axis_aligned_quad_size(dstQuad);
1167     // Although the quads are axis-aligned, the local coordinate system is transformed such
1168     // that fractionally-aligned sample centers will not align with the device coordinate system
1169     // So disable filtering when edges are the same length and both srcQuad and dstQuad
1170     // 0th vertex is integer aligned.
1171     bool filter = srcSize != dstSize || !SkScalarIsInt(srcQuad.x(0)) ||
1172                   !SkScalarIsInt(srcQuad.y(0)) || !SkScalarIsInt(dstQuad.x(0)) ||
1173                   !SkScalarIsInt(dstQuad.y(0));
1174     bool mm = srcSize.fWidth > dstSize.fWidth || srcSize.fHeight > dstSize.fHeight;
1175     return {filter, mm};
1176 }
1177 
Make(GrRecordingContext * context,GrSurfaceProxyView proxyView,SkAlphaType alphaType,sk_sp<GrColorSpaceXform> textureXform,GrSamplerState::Filter filter,GrSamplerState::MipmapMode mm,const SkPMColor4f & color,Saturate saturate,SkBlendMode blendMode,GrAAType aaType,DrawQuad * quad,const SkRect * subset)1178 GrOp::Owner TextureOp::Make(GrRecordingContext* context,
1179                             GrSurfaceProxyView proxyView,
1180                             SkAlphaType alphaType,
1181                             sk_sp<GrColorSpaceXform> textureXform,
1182                             GrSamplerState::Filter filter,
1183                             GrSamplerState::MipmapMode mm,
1184                             const SkPMColor4f& color,
1185                             Saturate saturate,
1186                             SkBlendMode blendMode,
1187                             GrAAType aaType,
1188                             DrawQuad* quad,
1189                             const SkRect* subset) {
1190     // Apply optimizations that are valid whether or not using TextureOp or FillRectOp
1191     if (subset && subset->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1192         // No need for a shader-based subset if hardware clamping achieves the same effect
1193         subset = nullptr;
1194     }
1195 
1196     if (filter != GrSamplerState::Filter::kNearest || mm != GrSamplerState::MipmapMode::kNone) {
1197         auto [mustFilter, mustMM] = FilterAndMipmapHaveNoEffect(quad->fLocal, quad->fDevice);
1198         if (!mustFilter) {
1199             filter = GrSamplerState::Filter::kNearest;
1200         }
1201         if (!mustMM) {
1202             mm = GrSamplerState::MipmapMode::kNone;
1203         }
1204     }
1205 
1206     if (blendMode == SkBlendMode::kSrcOver) {
1207         return TextureOpImpl::Make(context, std::move(proxyView), std::move(textureXform), filter,
1208                                    mm, color, saturate, aaType, std::move(quad), subset);
1209     } else {
1210         // Emulate complex blending using FillRectOp
1211         GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, filter, mm);
1212         GrPaint paint;
1213         paint.setColor4f(color);
1214         paint.setXPFactory(GrXPFactory::FromBlendMode(blendMode));
1215 
1216         std::unique_ptr<GrFragmentProcessor> fp;
1217         const auto& caps = *context->priv().caps();
1218         if (subset) {
1219             SkRect localRect;
1220             if (quad->fLocal.asRect(&localRect)) {
1221                 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
1222                                                  samplerState, *subset, localRect, caps);
1223             } else {
1224                 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(),
1225                                                  samplerState, *subset, caps);
1226             }
1227         } else {
1228             fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), samplerState,
1229                                        caps);
1230         }
1231         fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
1232         fp = GrBlendFragmentProcessor::Make<SkBlendMode::kModulate>(std::move(fp), nullptr);
1233         if (saturate == Saturate::kYes) {
1234             fp = GrFragmentProcessor::ClampOutput(std::move(fp));
1235         }
1236         paint.setColorFragmentProcessor(std::move(fp));
1237         return ganesh::FillRectOp::Make(context, std::move(paint), aaType, quad);
1238     }
1239 }
1240 
1241 // A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1242 class TextureOp::BatchSizeLimiter {
1243 public:
BatchSizeLimiter(ganesh::SurfaceDrawContext * sdc,const GrClip * clip,GrRecordingContext * rContext,int numEntries,GrSamplerState::Filter filter,GrSamplerState::MipmapMode mm,Saturate saturate,SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> textureColorSpaceXform)1244     BatchSizeLimiter(ganesh::SurfaceDrawContext* sdc,
1245                      const GrClip* clip,
1246                      GrRecordingContext* rContext,
1247                      int numEntries,
1248                      GrSamplerState::Filter filter,
1249                      GrSamplerState::MipmapMode mm,
1250                      Saturate saturate,
1251                      SkCanvas::SrcRectConstraint constraint,
1252                      const SkMatrix& viewMatrix,
1253                      sk_sp<GrColorSpaceXform> textureColorSpaceXform)
1254             : fSDC(sdc)
1255             , fClip(clip)
1256             , fContext(rContext)
1257             , fFilter(filter)
1258             , fMipmapMode(mm)
1259             , fSaturate(saturate)
1260             , fConstraint(constraint)
1261             , fViewMatrix(viewMatrix)
1262             , fTextureColorSpaceXform(textureColorSpaceXform)
1263             , fNumLeft(numEntries) {}
1264 
createOp(GrTextureSetEntry set[],int clumpSize,GrAAType aaType)1265     void createOp(GrTextureSetEntry set[], int clumpSize, GrAAType aaType) {
1266 
1267         int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
1268         GrOp::Owner op = TextureOpImpl::Make(fContext,
1269                                              &set[fNumClumped],
1270                                              clumpSize,
1271                                              clumpProxyCount,
1272                                              fFilter,
1273                                              fMipmapMode,
1274                                              fSaturate,
1275                                              aaType,
1276                                              fConstraint,
1277                                              fViewMatrix,
1278                                              fTextureColorSpaceXform);
1279         fSDC->addDrawOp(fClip, std::move(op));
1280 
1281         fNumLeft -= clumpSize;
1282         fNumClumped += clumpSize;
1283     }
1284 
numLeft() const1285     int numLeft() const { return fNumLeft;  }
baseIndex() const1286     int baseIndex() const { return fNumClumped; }
1287 
1288 private:
1289     ganesh::SurfaceDrawContext* fSDC;
1290     const GrClip*               fClip;
1291     GrRecordingContext*         fContext;
1292     GrSamplerState::Filter      fFilter;
1293     GrSamplerState::MipmapMode  fMipmapMode;
1294     Saturate                    fSaturate;
1295     SkCanvas::SrcRectConstraint fConstraint;
1296     const SkMatrix&             fViewMatrix;
1297     sk_sp<GrColorSpaceXform>    fTextureColorSpaceXform;
1298 
1299     int                         fNumLeft;
1300     int                         fNumClumped = 0; // also the offset for the start of the next clump
1301 };
1302 
1303 // Greedily clump quad draws together until the index buffer limit is exceeded.
AddTextureSetOps(ganesh::SurfaceDrawContext * sdc,const GrClip * clip,GrRecordingContext * context,GrTextureSetEntry set[],int cnt,int proxyRunCnt,GrSamplerState::Filter filter,GrSamplerState::MipmapMode mm,Saturate saturate,SkBlendMode blendMode,GrAAType aaType,SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> textureColorSpaceXform)1304 void TextureOp::AddTextureSetOps(ganesh::SurfaceDrawContext* sdc,
1305                                  const GrClip* clip,
1306                                  GrRecordingContext* context,
1307                                  GrTextureSetEntry set[],
1308                                  int cnt,
1309                                  int proxyRunCnt,
1310                                  GrSamplerState::Filter filter,
1311                                  GrSamplerState::MipmapMode mm,
1312                                  Saturate saturate,
1313                                  SkBlendMode blendMode,
1314                                  GrAAType aaType,
1315                                  SkCanvas::SrcRectConstraint constraint,
1316                                  const SkMatrix& viewMatrix,
1317                                  sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
1318     // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1319     // the op's metadata so we don't need to worry about overflow.
1320     SkDEBUGCODE(TextureOpImpl::ValidateResourceLimits();)
1321     SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1322 
1323     // First check if we can support batches as a single op
1324     if (blendMode != SkBlendMode::kSrcOver ||
1325         !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1326         // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1327         // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1328         // automatically creates the appropriate FillRectOp to emulate TextureOp.
1329         SkMatrix ctm;
1330         for (int i = 0; i < cnt; ++i) {
1331             ctm = viewMatrix;
1332             if (set[i].fPreViewMatrix) {
1333                 ctm.preConcat(*set[i].fPreViewMatrix);
1334             }
1335 
1336             DrawQuad quad;
1337             quad.fEdgeFlags = set[i].fAAFlags;
1338             if (set[i].fDstClipQuad) {
1339                 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
1340 
1341                 SkPoint srcPts[4];
1342                 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
1343                 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
1344             } else {
1345                 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1346                 quad.fLocal = GrQuad(set[i].fSrcRect);
1347             }
1348 
1349             const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint
1350                     ? &set[i].fSrcRect : nullptr;
1351 
1352             auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
1353                            filter, mm, set[i].fColor, saturate, blendMode, aaType, &quad, subset);
1354             sdc->addDrawOp(clip, std::move(op));
1355         }
1356         return;
1357     }
1358 
1359     // Second check if we can always just make a single op and avoid the extra iteration
1360     // needed to clump things together.
1361     if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
1362                       GrResourceProvider::MaxNumAAQuads())) {
1363         auto op = TextureOpImpl::Make(context, set, cnt, proxyRunCnt, filter, mm, saturate, aaType,
1364                                       constraint, viewMatrix, std::move(textureColorSpaceXform));
1365         sdc->addDrawOp(clip, std::move(op));
1366         return;
1367     }
1368 
1369     BatchSizeLimiter state(sdc, clip, context, cnt, filter, mm, saturate, constraint, viewMatrix,
1370                            std::move(textureColorSpaceXform));
1371 
1372     // kNone and kMSAA never get altered
1373     if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1374         // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1375         while (state.numLeft() > 0) {
1376             int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
1377 
1378             state.createOp(set, clumpSize, aaType);
1379         }
1380     } else {
1381         // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1382         // can also get downgraded to kNone if all the quads are on integer coordinates and
1383         // axis-aligned.
1384         SkASSERT(aaType == GrAAType::kCoverage);
1385 
1386         while (state.numLeft() > 0) {
1387             GrAAType runningAA = GrAAType::kNone;
1388             bool clumped = false;
1389 
1390             for (int i = 0; i < state.numLeft(); ++i) {
1391                 int absIndex = state.baseIndex() + i;
1392 
1393                 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone ||
1394                     runningAA == GrAAType::kCoverage) {
1395 
1396                     if (i >= GrResourceProvider::MaxNumAAQuads()) {
1397                         // Here we either need to boost the AA type to kCoverage, but doing so with
1398                         // all the accumulated quads would overflow, or we have a set of AA quads
1399                         // that has just gotten too large. In either case, calve off the existing
1400                         // quads as their own TextureOp.
1401                         state.createOp(
1402                             set,
1403                             runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1404                             runningAA); // maybe downgrading AA here
1405                         clumped = true;
1406                         break;
1407                     }
1408 
1409                     runningAA = GrAAType::kCoverage;
1410                 } else if (runningAA == GrAAType::kNone) {
1411 
1412                     if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1413                         // Here we've found a consistent batch of non-AA quads that has gotten too
1414                         // large. Calve it off as its own TextureOp.
1415                         state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1416                                        GrAAType::kNone); // definitely downgrading AA here
1417                         clumped = true;
1418                         break;
1419                     }
1420                 }
1421             }
1422 
1423             if (!clumped) {
1424                 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1425                 // quads and call it a day.
1426                 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1427             }
1428         }
1429     }
1430 }
1431 
1432 } // namespace skgpu::ganesh
1433 
1434 #if defined(GPU_TEST_UTILS)
GR_DRAW_OP_TEST_DEFINE(TextureOpImpl)1435 GR_DRAW_OP_TEST_DEFINE(TextureOpImpl) {
1436     SkISize dims;
1437     dims.fHeight = random->nextULessThan(90) + 10;
1438     dims.fWidth = random->nextULessThan(90) + 10;
1439     auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
1440     skgpu::Mipmapped mipmapped =
1441             random->nextBool() ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo;
1442     SkBackingFit fit = SkBackingFit::kExact;
1443     if (mipmapped == skgpu::Mipmapped::kNo) {
1444         fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1445     }
1446     const GrBackendFormat format =
1447             context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1448                                                             GrRenderable::kNo);
1449     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
1450     sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format,
1451                                                              dims,
1452                                                              GrRenderable::kNo,
1453                                                              1,
1454                                                              mipmapped,
1455                                                              fit,
1456                                                              skgpu::Budgeted::kNo,
1457                                                              GrProtected::kNo,
1458                                                              /*label=*/"TextureOp",
1459                                                              GrInternalSurfaceFlags::kNone);
1460 
1461     SkRect rect = GrTest::TestRect(random);
1462     SkRect srcRect;
1463     srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1464     srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1465     srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1466     srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1467     SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
1468     SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
1469     GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
1470             static_cast<uint32_t>(GrSamplerState::Filter::kLast) + 1);
1471     GrSamplerState::MipmapMode mm = GrSamplerState::MipmapMode::kNone;
1472     if (mipmapped == skgpu::Mipmapped::kYes) {
1473         mm = (GrSamplerState::MipmapMode)random->nextULessThan(
1474                 static_cast<uint32_t>(GrSamplerState::MipmapMode::kLast) + 1);
1475     }
1476 
1477     auto texXform = GrTest::TestColorXform(random);
1478     GrAAType aaType = GrAAType::kNone;
1479     if (random->nextBool()) {
1480         aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
1481     }
1482     GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1483     aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1484     aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1485     aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1486     aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
1487     bool useSubset = random->nextBool();
1488     auto saturate = random->nextBool() ? TextureOp::Saturate::kYes
1489                                        : TextureOp::Saturate::kNo;
1490     GrSurfaceProxyView proxyView(
1491             std::move(proxy), origin,
1492             context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
1493     auto alphaType = static_cast<SkAlphaType>(
1494             random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
1495 
1496     DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
1497     return TextureOp::Make(context, std::move(proxyView), alphaType,
1498                            std::move(texXform), filter, mm, color, saturate,
1499                            SkBlendMode::kSrcOver, aaType, &quad,
1500                            useSubset ? &srcRect : nullptr);
1501 }
1502 
1503 #endif // defined(GPU_TEST_UTILS)
1504