• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include <new>
9 
10 #include "include/core/SkPoint.h"
11 #include "include/core/SkPoint3.h"
12 #include "include/gpu/GrTexture.h"
13 #include "include/private/GrRecordingContext.h"
14 #include "include/private/SkFloatingPoint.h"
15 #include "include/private/SkTo.h"
16 #include "src/core/SkMathPriv.h"
17 #include "src/core/SkMatrixPriv.h"
18 #include "src/core/SkRectPriv.h"
19 #include "src/gpu/GrAppliedClip.h"
20 #include "src/gpu/GrCaps.h"
21 #include "src/gpu/GrDrawOpTest.h"
22 #include "src/gpu/GrGeometryProcessor.h"
23 #include "src/gpu/GrGpu.h"
24 #include "src/gpu/GrMemoryPool.h"
25 #include "src/gpu/GrOpFlushState.h"
26 #include "src/gpu/GrRecordingContextPriv.h"
27 #include "src/gpu/GrResourceProvider.h"
28 #include "src/gpu/GrResourceProviderPriv.h"
29 #include "src/gpu/GrShaderCaps.h"
30 #include "src/gpu/GrTexturePriv.h"
31 #include "src/gpu/GrTextureProxy.h"
32 #include "src/gpu/SkGr.h"
33 #include "src/gpu/effects/generated/GrClampFragmentProcessor.h"
34 #include "src/gpu/geometry/GrQuad.h"
35 #include "src/gpu/geometry/GrQuadBuffer.h"
36 #include "src/gpu/geometry/GrQuadUtils.h"
37 #include "src/gpu/glsl/GrGLSLVarying.h"
38 #include "src/gpu/ops/GrFillRectOp.h"
39 #include "src/gpu/ops/GrMeshDrawOp.h"
40 #include "src/gpu/ops/GrQuadPerEdgeAA.h"
41 #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
42 #include "src/gpu/ops/GrTextureOp.h"
43 
44 namespace {
45 
46 using Domain = GrQuadPerEdgeAA::Domain;
47 using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
48 using ColorType = GrQuadPerEdgeAA::ColorType;
49 
50 // 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).
axis_aligned_quad_size(const GrQuad & quad)52 static 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 
filter_has_effect(const GrQuad & srcQuad,const GrQuad & dstQuad)60 static bool filter_has_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
61     // If not axis-aligned in src or dst, then always say it has an effect
62     if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
63         dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
64         return true;
65     }
66 
67     SkRect srcRect;
68     SkRect dstRect;
69     if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
70         // Disable filtering when there is no scaling (width and height are the same), and the
71         // top-left corners have the same fraction (so src and dst snap to the pixel grid
72         // identically).
73         SkASSERT(srcRect.isSorted());
74         return srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
75                SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
76                SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
77     } else {
78         // Although the quads are axis-aligned, the local coordinate system is transformed such
79         // that fractionally-aligned sample centers will not align with the device coordinate system
80         // So disable filtering when edges are the same length and both srcQuad and dstQuad
81         // 0th vertex is integer aligned.
82         if (SkScalarIsInt(srcQuad.x(0)) && SkScalarIsInt(srcQuad.y(0)) &&
83             SkScalarIsInt(dstQuad.x(0)) && SkScalarIsInt(dstQuad.y(0))) {
84             // Extract edge lengths
85             SkSize srcSize = axis_aligned_quad_size(srcQuad);
86             SkSize dstSize = axis_aligned_quad_size(dstQuad);
87             return srcSize.fWidth != dstSize.fWidth || srcSize.fHeight != dstSize.fHeight;
88         } else {
89             return true;
90         }
91     }
92 }
93 
94 // Describes function for normalizing src coords: [x * iw, y * ih + yOffset] can represent
95 // regular and rectangular textures, w/ or w/o origin correction.
96 struct NormalizationParams {
97     float fIW; // 1 / width of texture, or 1.0 for texture rectangles
98     float fIH; // 1 / height of texture, or 1.0 for tex rects, X -1 if bottom-left origin
99     float fYOffset; // 0 for top-left origin, height of [normalized] tex if bottom-left
100 };
proxy_normalization_params(const GrSurfaceProxy * proxy,GrSurfaceOrigin origin)101 static NormalizationParams proxy_normalization_params(const GrSurfaceProxy* proxy,
102                                                       GrSurfaceOrigin origin) {
103     // Whether or not the proxy is instantiated, this is the size its texture will be, so we can
104     // normalize the src coordinates up front.
105     SkISize dimensions = proxy->backingStoreDimensions();
106     float iw, ih, h;
107     if (proxy->backendFormat().textureType() == GrTextureType::kRectangle) {
108         iw = ih = 1.f;
109         h = dimensions.height();
110     } else {
111         iw = 1.f / dimensions.width();
112         ih = 1.f / dimensions.height();
113         h = 1.f;
114     }
115 
116     if (origin == kBottomLeft_GrSurfaceOrigin) {
117         return {iw, -ih, h};
118     } else {
119         return {iw, ih, 0.0f};
120     }
121 }
122 
inset_domain_for_bilerp(const NormalizationParams & params,const SkRect & domainRect)123 static SkRect inset_domain_for_bilerp(const NormalizationParams& params, const SkRect& domainRect) {
124     // Normalized pixel size is also equal to iw and ih, so the insets for bilerp are just
125     // in those units and can be applied safely after normalization. However, if the domain is
126     // smaller than a texel, it should clamp to the center of that axis.
127     float dw = domainRect.width() < params.fIW ? domainRect.width() : params.fIW;
128     float dh = domainRect.height() < params.fIH ? domainRect.height() : params.fIH;
129     return domainRect.makeInset(0.5f * dw, 0.5f * dh);
130 }
131 
132 // Normalize the domain. If 'domainRect' is null, it is assumed no domain constraint is desired,
133 // so a sufficiently large rect is returned even if the quad ends up batched with an op that uses
134 // domains overall.
normalize_domain(GrSamplerState::Filter filter,const NormalizationParams & params,const SkRect * domainRect)135 static SkRect normalize_domain(GrSamplerState::Filter filter,
136                                const NormalizationParams& params,
137                                const SkRect* domainRect) {
138     static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
139     if (!domainRect) {
140         // Either the quad has no domain constraint and is batched with a domain constrained op
141         // (in which case we want a domain that doesn't restrict normalized tex coords), or the
142         // entire op doesn't use the domain, in which case the returned value is ignored.
143         return kLargeRect;
144     }
145 
146     auto ltrb = skvx::Vec<4, float>::Load(domainRect);
147     // Normalize and offset
148     ltrb = mad(ltrb, {params.fIW, params.fIH, params.fIW, params.fIH},
149                {0.f, params.fYOffset, 0.f, params.fYOffset});
150     if (params.fIH < 0.f) {
151         // Flip top and bottom to keep the rect sorted when loaded back to SkRect.
152         ltrb = skvx::shuffle<0, 3, 2, 1>(ltrb);
153     }
154 
155     SkRect out;
156     ltrb.store(&out);
157     return out;
158 }
159 
160 // Normalizes logical src coords and corrects for origin
normalize_src_quad(const NormalizationParams & params,GrQuad * srcQuad)161 static void normalize_src_quad(const NormalizationParams& params,
162                                GrQuad* srcQuad) {
163     // The src quad should not have any perspective
164     SkASSERT(!srcQuad->hasPerspective());
165     skvx::Vec<4, float> xs = srcQuad->x4f() * params.fIW;
166     skvx::Vec<4, float> ys = mad(srcQuad->y4f(), params.fIH, params.fYOffset);
167     xs.store(srcQuad->xs());
168     ys.store(srcQuad->ys());
169 }
170 
171 // Count the number of proxy runs in the entry set. This usually is already computed by
172 // SkGpuDevice, but when the BatchLengthLimiter chops the set up it must determine a new proxy count
173 // for each split.
proxy_run_count(const GrRenderTargetContext::TextureSetEntry set[],int count)174 static int proxy_run_count(const GrRenderTargetContext::TextureSetEntry set[], int count) {
175     int actualProxyRunCount = 0;
176     const GrSurfaceProxy* lastProxy = nullptr;
177     for (int i = 0; i < count; ++i) {
178         if (set[i].fProxyView.proxy() != lastProxy) {
179             actualProxyRunCount++;
180             lastProxy = set[i].fProxyView.proxy();
181         }
182     }
183     return actualProxyRunCount;
184 }
185 
186 /**
187  * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
188  * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
189  */
190 class TextureOp final : public GrMeshDrawOp {
191 public:
Make(GrRecordingContext * context,GrSurfaceProxyView proxyView,sk_sp<GrColorSpaceXform> textureXform,GrSamplerState::Filter filter,const SkPMColor4f & color,GrTextureOp::Saturate saturate,GrAAType aaType,DrawQuad * quad,const SkRect * domain)192     static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
193                                           GrSurfaceProxyView proxyView,
194                                           sk_sp<GrColorSpaceXform> textureXform,
195                                           GrSamplerState::Filter filter,
196                                           const SkPMColor4f& color,
197                                           GrTextureOp::Saturate saturate,
198                                           GrAAType aaType,
199                                           DrawQuad* quad,
200                                           const SkRect* domain) {
201         GrOpMemoryPool* pool = context->priv().opMemoryPool();
202         return pool->allocate<TextureOp>(std::move(proxyView), std::move(textureXform), filter,
203                                          color, saturate, aaType, quad, domain);
204     }
205 
Make(GrRecordingContext * context,GrRenderTargetContext::TextureSetEntry set[],int cnt,int proxyRunCnt,GrSamplerState::Filter filter,GrTextureOp::Saturate saturate,GrAAType aaType,SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> textureColorSpaceXform)206     static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
207                                           GrRenderTargetContext::TextureSetEntry set[],
208                                           int cnt,
209                                           int proxyRunCnt,
210                                           GrSamplerState::Filter filter,
211                                           GrTextureOp::Saturate saturate,
212                                           GrAAType aaType,
213                                           SkCanvas::SrcRectConstraint constraint,
214                                           const SkMatrix& viewMatrix,
215                                           sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
216         // Allocate size based on proxyRunCnt, since that determines number of ViewCountPairs.
217         SkASSERT(proxyRunCnt <= cnt);
218 
219         size_t size = sizeof(TextureOp) + sizeof(ViewCountPair) * (proxyRunCnt - 1);
220         GrOpMemoryPool* pool = context->priv().opMemoryPool();
221         void* mem = pool->allocate(size);
222         return std::unique_ptr<GrDrawOp>(
223                 new (mem) TextureOp(set, cnt, proxyRunCnt, filter, saturate, aaType, constraint,
224                                     viewMatrix, std::move(textureColorSpaceXform)));
225     }
226 
~TextureOp()227     ~TextureOp() override {
228         for (unsigned p = 1; p < fMetadata.fProxyCount; ++p) {
229             fViewCountPairs[p].~ViewCountPair();
230         }
231     }
232 
name() const233     const char* name() const override { return "TextureOp"; }
234 
visitProxies(const VisitProxyFunc & func) const235     void visitProxies(const VisitProxyFunc& func) const override {
236         bool mipped = (GrSamplerState::Filter::kMipMap == fMetadata.filter());
237         for (unsigned p = 0; p <  fMetadata.fProxyCount; ++p) {
238             func(fViewCountPairs[p].fProxy.get(), GrMipMapped(mipped));
239         }
240     }
241 
242 #ifdef SK_DEBUG
dumpInfo() const243     SkString dumpInfo() const override {
244         SkString str;
245         str.appendf("# draws: %d\n", fQuads.count());
246         auto iter = fQuads.iterator();
247         for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
248             str.appendf("Proxy ID: %d, Filter: %d\n",
249                         fViewCountPairs[p].fProxy->uniqueID().asUInt(),
250                         static_cast<int>(fMetadata.fFilter));
251             int i = 0;
252             while(i < fViewCountPairs[p].fQuadCnt && iter.next()) {
253                 const GrQuad* quad = iter.deviceQuad();
254                 GrQuad uv = iter.isLocalValid() ? *(iter.localQuad()) : GrQuad();
255                 const ColorDomainAndAA& info = iter.metadata();
256                 str.appendf(
257                         "%d: Color: 0x%08x, Domain(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
258                         "  UVs  [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
259                         "  Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
260                         i, info.fColor.toBytes_RGBA(), fMetadata.fDomain, info.fDomainRect.fLeft,
261                         info.fDomainRect.fTop, info.fDomainRect.fRight, info.fDomainRect.fBottom,
262                         quad->point(0).fX, quad->point(0).fY, quad->point(1).fX, quad->point(1).fY,
263                         quad->point(2).fX, quad->point(2).fY, quad->point(3).fX, quad->point(3).fY,
264                         uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
265                         uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
266 
267                 i++;
268             }
269         }
270         str += INHERITED::dumpInfo();
271         return str;
272     }
273 
ValidateResourceLimits()274     static void ValidateResourceLimits() {
275         // The op implementation has an upper bound on the number of quads that it can represent.
276         // However, the resource manager imposes its own limit on the number of quads, which should
277         // always be lower than the numerical limit this op can hold.
278         using CountStorage = decltype(Metadata::fTotalQuadCount);
279         CountStorage maxQuadCount = std::numeric_limits<CountStorage>::max();
280         // GrResourceProvider::Max...() is typed as int, so don't compare across signed/unsigned.
281         int resourceLimit = SkTo<int>(maxQuadCount);
282         SkASSERT(GrResourceProvider::MaxNumAAQuads() <= resourceLimit &&
283                  GrResourceProvider::MaxNumNonAAQuads() <= resourceLimit);
284     }
285 #endif
286 
finalize(const GrCaps & caps,const GrAppliedClip *,bool hasMixedSampledCoverage,GrClampType clampType)287     GrProcessorSet::Analysis finalize(
288             const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage,
289             GrClampType clampType) override {
290         SkASSERT(fMetadata.colorType() == ColorType::kNone);
291         auto iter = fQuads.metadata();
292         while(iter.next()) {
293             auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor);
294             fMetadata.fColorType = std::max(fMetadata.fColorType, static_cast<uint16_t>(colorType));
295         }
296         return GrProcessorSet::EmptySetAnalysis();
297     }
298 
fixedFunctionFlags() const299     FixedFunctionFlags fixedFunctionFlags() const override {
300         return fMetadata.aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
301                                                      : FixedFunctionFlags::kNone;
302     }
303 
304     DEFINE_OP_CLASS_ID
305 
306 private:
307     friend class ::GrOpMemoryPool;
308 
309     struct ColorDomainAndAA {
ColorDomainAndAA__anon83484da00111::TextureOp::ColorDomainAndAA310         ColorDomainAndAA(const SkPMColor4f& color, const SkRect& domainRect, GrQuadAAFlags aaFlags)
311                 : fColor(color)
312                 , fDomainRect(domainRect)
313                 , fAAFlags(static_cast<uint16_t>(aaFlags)) {
314             SkASSERT(fAAFlags == static_cast<uint16_t>(aaFlags));
315         }
316 
317         SkPMColor4f fColor;
318         // If the op doesn't use domains, this is ignored. If the op uses domains and the specific
319         // entry does not, this rect will equal kLargeRect, so it automatically has no effect.
320         SkRect fDomainRect;
321         unsigned fAAFlags : 4;
322 
aaFlags__anon83484da00111::TextureOp::ColorDomainAndAA323         GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
324     };
325 
326     struct ViewCountPair {
327         // Normally this would be a GrSurfaceProxyView, but GrTextureOp applies the GrOrigin right
328         // away so it doesn't need to be stored, and all ViewCountPairs in an op have the same
329         // swizzle so that is stored in the op metadata.
330         sk_sp<GrSurfaceProxy> fProxy;
331         int fQuadCnt;
332     };
333 
334     // TextureOp and ViewCountPair are 8 byte aligned. This is packed into 8 bytes to minimally
335     // increase the size of the op; increasing the op size can have a surprising impact on
336     // performance (since texture ops are one of the most commonly used in an app).
337     struct Metadata {
338         // AAType must be filled after initialization; ColorType is determined in finalize()
Metadata__anon83484da00111::TextureOp::Metadata339         Metadata(const GrSwizzle& swizzle, GrSamplerState::Filter filter,
340                  GrQuadPerEdgeAA::Domain domain, GrTextureOp::Saturate saturate)
341                 : fSwizzle(swizzle)
342                 , fProxyCount(1)
343                 , fTotalQuadCount(1)
344                 , fFilter(static_cast<uint16_t>(filter))
345                 , fAAType(static_cast<uint16_t>(GrAAType::kNone))
346                 , fColorType(static_cast<uint16_t>(ColorType::kNone))
347                 , fDomain(static_cast<uint16_t>(domain))
348                 , fSaturate(static_cast<uint16_t>(saturate)) {}
349 
350         GrSwizzle fSwizzle; // sizeof(GrSwizzle) == uint16_t
351         uint16_t  fProxyCount;
352         // This will be >= fProxyCount, since a proxy may be drawn multiple times
353         uint16_t  fTotalQuadCount;
354 
355         // These must be based on uint16_t to help MSVC's pack bitfields optimally
356         uint16_t  fFilter     : 2; // GrSamplerState::Filter
357         uint16_t  fAAType     : 2; // GrAAType
358         uint16_t  fColorType  : 2; // GrQuadPerEdgeAA::ColorType
359         uint16_t  fDomain     : 1; // bool
360         uint16_t  fSaturate   : 1; // bool
361         uint16_t  fUnused     : 8; // # of bits left before Metadata exceeds 8 bytes
362 
filter__anon83484da00111::TextureOp::Metadata363         GrSamplerState::Filter filter() const {
364             return static_cast<GrSamplerState::Filter>(fFilter);
365         }
aaType__anon83484da00111::TextureOp::Metadata366         GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
colorType__anon83484da00111::TextureOp::Metadata367         ColorType colorType() const { return static_cast<ColorType>(fColorType); }
domain__anon83484da00111::TextureOp::Metadata368         Domain domain() const { return static_cast<Domain>(fDomain); }
saturate__anon83484da00111::TextureOp::Metadata369         GrTextureOp::Saturate saturate() const {
370             return static_cast<GrTextureOp::Saturate>(fSaturate);
371         }
372 
373         static_assert(GrSamplerState::kFilterCount <= 4);
374         static_assert(kGrAATypeCount <= 4);
375         static_assert(GrQuadPerEdgeAA::kColorTypeCount <= 4);
376     };
377     static_assert(sizeof(Metadata) == 8);
378 
379     // This descriptor is used in both onPrePrepareDraws and onPrepareDraws.
380     //
381     // In the onPrePrepareDraws case it is allocated in the creation-time opData
382     // arena. Both allocateCommon and allocatePrePrepareOnly are called and they also allocate
383     // their memory in the creation-time opData arena.
384     //
385     // In the onPrepareDraws case this descriptor is created on the stack and only
386     // allocateCommon is called. In this case the common memory fields are allocated
387     // in the flush-time arena (i.e., as part of the flushState).
388     struct PrePreparedDesc {
389         VertexSpec                      fVertexSpec;
390         int                             fNumProxies = 0;
391         int                             fNumTotalQuads = 0;
392         GrPipeline::DynamicStateArrays* fDynamicStateArrays = nullptr;
393         GrPipeline::FixedDynamicState*  fFixedDynamicState = nullptr;
394 
395         // This member variable is only used by 'onPrePrepareDraws'. The prior five are also
396         // used by 'onPrepareDraws'
397         char*                           fVertices = nullptr;
398 
399         // How big should 'fVertices' be to hold all the vertex data?
totalSizeInBytes__anon83484da00111::TextureOp::PrePreparedDesc400         size_t totalSizeInBytes() const {
401             return fNumTotalQuads * fVertexSpec.verticesPerQuad() * fVertexSpec.vertexSize();
402         }
403 
totalNumVertices__anon83484da00111::TextureOp::PrePreparedDesc404         int totalNumVertices() const {
405             return fNumTotalQuads * fVertexSpec.verticesPerQuad();
406         }
407 
408         // Helper to fill in the fFixedDynamicState and fDynamicStateArrays. If there is more
409         // than one mesh/proxy they are stored in fDynamicStateArrays but if there is only one
410         // it is stored in fFixedDynamicState.
setMeshProxy__anon83484da00111::TextureOp::PrePreparedDesc411         void setMeshProxy(int index, GrSurfaceProxy* proxy) {
412             SkASSERT(index < fNumProxies);
413 
414             if (fDynamicStateArrays) {
415                 SkASSERT(fDynamicStateArrays->fPrimitiveProcessorTextures);
416                 SkASSERT(fNumProxies > 1);
417 
418                 fDynamicStateArrays->fPrimitiveProcessorTextures[index] = proxy;
419             } else {
420                 SkASSERT(fFixedDynamicState);
421                 SkASSERT(fNumProxies == 1);
422 
423                 fFixedDynamicState->fPrimitiveProcessorTextures[index] = proxy;
424             }
425         }
426 
427         // Allocate the fields required in both onPrePrepareDraws and onPrepareDraws
allocateCommon__anon83484da00111::TextureOp::PrePreparedDesc428         void allocateCommon(SkArenaAlloc* arena, const GrAppliedClip* clip) {
429             // We'll use a dynamic state array for the GP textures when there are multiple ops.
430             // Otherwise, we use fixed dynamic state to specify the single op's proxy.
431             if (fNumProxies > 1) {
432                 fDynamicStateArrays = Target::AllocDynamicStateArrays(arena, fNumProxies, 1, false);
433                 fFixedDynamicState = Target::MakeFixedDynamicState(arena, clip, 0);
434             } else {
435                 fFixedDynamicState = Target::MakeFixedDynamicState(arena, clip, 1);
436             }
437         }
438 
439         // Allocate the fields only needed by onPrePrepareDraws
allocatePrePrepareOnly__anon83484da00111::TextureOp::PrePreparedDesc440         void allocatePrePrepareOnly(SkArenaAlloc* arena) {
441             fVertices = arena->makeArrayDefault<char>(this->totalSizeInBytes());
442         }
443 
444     };
445 
446     // If domainRect is not null it will be used to apply a strict src rect-style constraint.
TextureOp(GrSurfaceProxyView proxyView,sk_sp<GrColorSpaceXform> textureColorSpaceXform,GrSamplerState::Filter filter,const SkPMColor4f & color,GrTextureOp::Saturate saturate,GrAAType aaType,DrawQuad * quad,const SkRect * domainRect)447     TextureOp(GrSurfaceProxyView proxyView,
448               sk_sp<GrColorSpaceXform> textureColorSpaceXform,
449               GrSamplerState::Filter filter,
450               const SkPMColor4f& color,
451               GrTextureOp::Saturate saturate,
452               GrAAType aaType,
453               DrawQuad* quad,
454               const SkRect* domainRect)
455             : INHERITED(ClassID())
456             , fQuads(1, true /* includes locals */)
457             , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
458             , fPrePreparedDesc(nullptr)
459             , fMetadata(proxyView.swizzle(), filter, Domain(!!domainRect), saturate) {
460 
461         // Clean up disparities between the overall aa type and edge configuration and apply
462         // optimizations based on the rect and matrix when appropriate
463         GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
464                                    &aaType, &quad->fEdgeFlags);
465         fMetadata.fAAType = static_cast<uint16_t>(aaType);
466 
467         // We expect our caller to have already caught this optimization.
468         SkASSERT(!domainRect ||
469                  !domainRect->contains(proxyView.proxy()->backingStoreBoundsRect()));
470 
471         // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
472         // If we don't have (or determined we don't need) coverage AA then we can skip using a
473         // domain.
474         if (domainRect && filter == GrSamplerState::Filter::kNearest &&
475             aaType != GrAAType::kCoverage) {
476             domainRect = nullptr;
477             fMetadata.fDomain = static_cast<uint16_t>(Domain::kNo);
478         }
479 
480         // Normalize src coordinates and the domain (if set)
481         NormalizationParams params = proxy_normalization_params(proxyView.proxy(),
482                                                                 proxyView.origin());
483         normalize_src_quad(params, &quad->fLocal);
484         SkRect domain = normalize_domain(filter, params, domainRect);
485 
486         // Set bounds before clipping so we don't have to worry about unioning the bounds of
487         // the two potential quads (GrQuad::bounds() is perspective-safe).
488         this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
489                         IsHairline::kNo);
490 
491         int quadCount = this->appendQuad(quad, color, domain);
492         fViewCountPairs[0] = {proxyView.detachProxy(), quadCount};
493     }
494 
TextureOp(GrRenderTargetContext::TextureSetEntry set[],int cnt,int proxyRunCnt,GrSamplerState::Filter filter,GrTextureOp::Saturate saturate,GrAAType aaType,SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> textureColorSpaceXform)495     TextureOp(GrRenderTargetContext::TextureSetEntry set[],
496               int cnt,
497               int proxyRunCnt,
498               GrSamplerState::Filter filter,
499               GrTextureOp::Saturate saturate,
500               GrAAType aaType,
501               SkCanvas::SrcRectConstraint constraint,
502               const SkMatrix& viewMatrix,
503               sk_sp<GrColorSpaceXform> textureColorSpaceXform)
504             : INHERITED(ClassID())
505             , fQuads(cnt, true /* includes locals */)
506             , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
507             , fPrePreparedDesc(nullptr)
508             , fMetadata(set[0].fProxyView.swizzle(), GrSamplerState::Filter::kNearest,
509                         Domain::kNo, saturate) {
510         // Update counts to reflect the batch op
511         fMetadata.fProxyCount = SkToUInt(proxyRunCnt);
512         fMetadata.fTotalQuadCount = SkToUInt(cnt);
513 
514         SkRect bounds = SkRectPriv::MakeLargestInverted();
515 
516         GrAAType netAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
517         Domain netDomain = Domain::kNo;
518         GrSamplerState::Filter netFilter = GrSamplerState::Filter::kNearest;
519 
520         const GrSurfaceProxy* curProxy = nullptr;
521 
522         // 'q' is the index in 'set' and fQuadBuffer; 'p' is the index in fViewCountPairs and only
523         // increases when set[q]'s proxy changes.
524         int p = 0;
525         for (int q = 0; q < cnt; ++q) {
526             if (q == 0) {
527                 // We do not placement new the first ViewCountPair since that one is allocated and
528                 // initialized as part of the GrTextureOp creation.
529                 fViewCountPairs[0].fProxy = set[0].fProxyView.detachProxy();
530                 fViewCountPairs[0].fQuadCnt = 0;
531                 curProxy = fViewCountPairs[0].fProxy.get();
532             } else if (set[q].fProxyView.proxy() != curProxy) {
533                 // We must placement new the ViewCountPairs here so that the sk_sps in the
534                 // GrSurfaceProxyView get initialized properly.
535                 new(&fViewCountPairs[++p])ViewCountPair({set[q].fProxyView.detachProxy(), 0});
536 
537                 curProxy = fViewCountPairs[p].fProxy.get();
538                 SkASSERT(GrTextureProxy::ProxiesAreCompatibleAsDynamicState(
539                         curProxy, fViewCountPairs[0].fProxy.get()));
540                 SkASSERT(fMetadata.fSwizzle == set[q].fProxyView.swizzle());
541             } // else another quad referencing the same proxy
542 
543             SkMatrix ctm = viewMatrix;
544             if (set[q].fPreViewMatrix) {
545                 ctm.preConcat(*set[q].fPreViewMatrix);
546             }
547 
548             // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
549             // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
550             DrawQuad quad;
551             if (set[q].fDstClipQuad) {
552                 quad.fDevice = GrQuad::MakeFromSkQuad(set[q].fDstClipQuad, ctm);
553 
554                 SkPoint srcPts[4];
555                 GrMapRectPoints(set[q].fDstRect, set[q].fSrcRect, set[q].fDstClipQuad, srcPts, 4);
556                 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
557             } else {
558                 quad.fDevice = GrQuad::MakeFromRect(set[q].fDstRect, ctm);
559                 quad.fLocal = GrQuad(set[q].fSrcRect);
560             }
561 
562             if (netFilter != filter && filter_has_effect(quad.fLocal, quad.fDevice)) {
563                 // The only way netFilter != filter is if bilerp is requested and we haven't yet
564                 // found a quad that requires bilerp (so net is still nearest).
565                 SkASSERT(netFilter == GrSamplerState::Filter::kNearest &&
566                          filter == GrSamplerState::Filter::kBilerp);
567                 netFilter = GrSamplerState::Filter::kBilerp;
568             }
569 
570             // Normalize the src quads and apply origin
571             NormalizationParams proxyParams = proxy_normalization_params(
572                     curProxy, set[q].fProxyView.origin());
573             normalize_src_quad(proxyParams, &quad.fLocal);
574 
575             // Update overall bounds of the op as the union of all quads
576             bounds.joinPossiblyEmptyRect(quad.fDevice.bounds());
577 
578             // Determine the AA type for the quad, then merge with net AA type
579             GrAAType aaForQuad;
580             GrQuadUtils::ResolveAAType(aaType, set[q].fAAFlags, quad.fDevice,
581                                        &aaForQuad, &quad.fEdgeFlags);
582             // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
583             SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
584             if (netAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
585                 netAAType = aaType;
586             }
587 
588             // Calculate metadata for the entry
589             const SkRect* domainForQuad = nullptr;
590             if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
591                 // Check (briefly) if the strict constraint is needed for this set entry
592                 if (!set[q].fSrcRect.contains(curProxy->backingStoreBoundsRect()) &&
593                     (filter == GrSamplerState::Filter::kBilerp ||
594                      aaForQuad == GrAAType::kCoverage)) {
595                     // Can't rely on hardware clamping and the draw will access outer texels
596                     // for AA and/or bilerp. Unlike filter quality, this op still has per-quad
597                     // control over AA so that can check aaForQuad, not netAAType.
598                     netDomain = Domain::kYes;
599                     domainForQuad = &set[q].fSrcRect;
600                 }
601             }
602             // This domain may represent a no-op, otherwise it will have the origin and dimensions
603             // of the texture applied to it. Insetting for bilinear filtering is deferred until
604             // on[Pre]Prepare so that the overall filter can be lazily determined.
605             SkRect domain = normalize_domain(filter, proxyParams, domainForQuad);
606 
607             // Always append a quad (or 2 if perspective clipped), it just may refer back to a prior
608             // ViewCountPair (this frequently happens when Chrome draws 9-patches).
609             float alpha = SkTPin(set[q].fAlpha, 0.f, 1.f);
610             fViewCountPairs[p].fQuadCnt += this->appendQuad(
611                     &quad, {alpha, alpha, alpha, alpha}, domain);
612         }
613         // The # of proxy switches should match what was provided (+1 because we incremented p
614         // when a new proxy was encountered).
615         SkASSERT((p + 1) == fMetadata.fProxyCount);
616         SkASSERT(fQuads.count() == fMetadata.fTotalQuadCount);
617 
618         fMetadata.fAAType = static_cast<uint16_t>(netAAType);
619         fMetadata.fFilter = static_cast<uint16_t>(netFilter);
620         fMetadata.fDomain = static_cast<uint16_t>(netDomain);
621 
622         this->setBounds(bounds, HasAABloat(netAAType == GrAAType::kCoverage), IsHairline::kNo);
623     }
624 
appendQuad(DrawQuad * quad,const SkPMColor4f & color,const SkRect & domain)625     int appendQuad(DrawQuad* quad, const SkPMColor4f& color, const SkRect& domain) {
626         DrawQuad extra;
627         // Only clip when there's anti-aliasing. When non-aa, the GPU clips just fine and there's
628         // no inset/outset math that requires w > 0.
629         int quadCount = quad->fEdgeFlags != GrQuadAAFlags::kNone ?
630                 GrQuadUtils::ClipToW0(quad, &extra) : 1;
631         if (quadCount == 0) {
632             // We can't discard the op at this point, but disable AA flags so it won't go through
633             // inset/outset processing
634             quad->fEdgeFlags = GrQuadAAFlags::kNone;
635             quadCount = 1;
636         }
637         fQuads.append(quad->fDevice, {color, domain, quad->fEdgeFlags},  &quad->fLocal);
638         if (quadCount > 1) {
639             fQuads.append(extra.fDevice, {color, domain, extra.fEdgeFlags}, &extra.fLocal);
640             fMetadata.fTotalQuadCount++;
641         }
642         return quadCount;
643     }
644 
onPrePrepareDraws(GrRecordingContext * context,const GrSurfaceProxyView * dstView,GrAppliedClip * clip,const GrXferProcessor::DstProxyView & dstProxyView)645     void onPrePrepareDraws(GrRecordingContext* context,
646                            const GrSurfaceProxyView* dstView,
647                            GrAppliedClip* clip,
648                            const GrXferProcessor::DstProxyView& dstProxyView) override {
649         TRACE_EVENT0("skia.gpu", TRACE_FUNC);
650 
651         SkDEBUGCODE(this->validate();)
652         SkASSERT(!fPrePreparedDesc);
653 
654         SkArenaAlloc* arena = context->priv().recordTimeAllocator();
655 
656         fPrePreparedDesc = arena->make<PrePreparedDesc>();
657 
658         this->characterize(fPrePreparedDesc);
659 
660         fPrePreparedDesc->allocateCommon(arena, clip);
661 
662         fPrePreparedDesc->allocatePrePrepareOnly(arena);
663 
664         // At this juncture we only fill in the vertex data and state arrays. Filling in of
665         // the meshes is left until onPrepareDraws.
666         SkAssertResult(FillInData(*context->priv().caps(), this, fPrePreparedDesc,
667                                   fPrePreparedDesc->fVertices, nullptr, 0, nullptr, nullptr));
668     }
669 
FillInData(const GrCaps & caps,TextureOp * texOp,PrePreparedDesc * desc,char * pVertexData,GrMesh * meshes,int absBufferOffset,sk_sp<const GrBuffer> vertexBuffer,sk_sp<const GrBuffer> indexBuffer)670     static bool FillInData(const GrCaps& caps, TextureOp* texOp, PrePreparedDesc* desc,
671                            char* pVertexData, GrMesh* meshes, int absBufferOffset,
672                            sk_sp<const GrBuffer> vertexBuffer,
673                            sk_sp<const GrBuffer> indexBuffer) {
674         int totQuadsSeen = 0;
675         SkDEBUGCODE(int totVerticesSeen = 0;)
676         SkDEBUGCODE(const size_t vertexSize = desc->fVertexSpec.vertexSize());
677 
678         GrQuadPerEdgeAA::Tessellator tessellator(desc->fVertexSpec, pVertexData);
679         int meshIndex = 0;
680         for (const auto& op : ChainRange<TextureOp>(texOp)) {
681             auto iter = op.fQuads.iterator();
682             for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
683                 const int quadCnt = op.fViewCountPairs[p].fQuadCnt;
684                 SkDEBUGCODE(int meshVertexCnt = quadCnt * desc->fVertexSpec.verticesPerQuad());
685                 SkASSERT(meshIndex < desc->fNumProxies);
686 
687                 if (pVertexData) {
688                     // Can just use top-left for origin here since we only need the dimensions to
689                     // determine the texel size for insetting.
690                     NormalizationParams params = proxy_normalization_params(
691                             op.fViewCountPairs[p].fProxy.get(), kTopLeft_GrSurfaceOrigin);
692 
693                     bool inset = texOp->fMetadata.filter() != GrSamplerState::Filter::kNearest;
694 
695                     for (int i = 0; i < quadCnt && iter.next(); ++i) {
696                         SkASSERT(iter.isLocalValid());
697                         const ColorDomainAndAA& info = iter.metadata();
698 
699                         tessellator.append(iter.deviceQuad(), iter.localQuad(), info.fColor,
700                                            inset ? inset_domain_for_bilerp(params, info.fDomainRect)
701                                                  : info.fDomainRect,
702                                            info.aaFlags());
703                     }
704                     desc->setMeshProxy(meshIndex, op.fViewCountPairs[p].fProxy.get());
705 
706                     SkASSERT((totVerticesSeen + meshVertexCnt) * vertexSize
707                              == (size_t)(tessellator.vertices() - pVertexData));
708                 }
709 
710                 if (meshes) {
711                     GrQuadPerEdgeAA::ConfigureMesh(caps, &(meshes[meshIndex]), desc->fVertexSpec,
712                                                    totQuadsSeen, quadCnt, desc->totalNumVertices(),
713                                                    vertexBuffer, indexBuffer, absBufferOffset);
714                 }
715 
716                 ++meshIndex;
717 
718                 totQuadsSeen += quadCnt;
719                 SkDEBUGCODE(totVerticesSeen += meshVertexCnt);
720                 SkASSERT(totQuadsSeen * desc->fVertexSpec.verticesPerQuad() == totVerticesSeen);
721             }
722 
723             // If quad counts per proxy were calculated correctly, the entire iterator
724             // should have been consumed.
725             SkASSERT(!pVertexData || !iter.next());
726         }
727 
728         SkASSERT(!pVertexData ||
729                  (desc->totalSizeInBytes() == (size_t)(tessellator.vertices() - pVertexData)));
730         SkASSERT(meshIndex == desc->fNumProxies);
731         SkASSERT(totQuadsSeen == desc->fNumTotalQuads);
732         SkASSERT(totVerticesSeen == desc->totalNumVertices());
733         return true;
734     }
735 
736 #ifdef SK_DEBUG
validate() const737     void validate() const override {
738         // NOTE: Since this is debug-only code, we use the virtual asTextureProxy()
739         auto textureType = fViewCountPairs[0].fProxy->asTextureProxy()->textureType();
740         GrAAType aaType = fMetadata.aaType();
741 
742         int quadCount = 0;
743         for (const auto& op : ChainRange<TextureOp>(this)) {
744             SkASSERT(op.fMetadata.fSwizzle == fMetadata.fSwizzle);
745 
746             for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
747                 auto* proxy = op.fViewCountPairs[p].fProxy->asTextureProxy();
748                 quadCount += op.fViewCountPairs[p].fQuadCnt;
749                 SkASSERT(proxy);
750                 SkASSERT(proxy->textureType() == textureType);
751             }
752 
753             // Each individual op must be a single aaType. kCoverage and kNone ops can chain
754             // together but kMSAA ones do not.
755             if (aaType == GrAAType::kCoverage || aaType == GrAAType::kNone) {
756                 SkASSERT(op.fMetadata.aaType() == GrAAType::kCoverage ||
757                          op.fMetadata.aaType() == GrAAType::kNone);
758             } else {
759                 SkASSERT(aaType == GrAAType::kMSAA && op.fMetadata.aaType() == GrAAType::kMSAA);
760             }
761         }
762 
763         SkASSERT(quadCount == this->numChainedQuads());
764     }
765 #endif
766 
767 #if GR_TEST_UTILS
numQuads() const768     int numQuads() const final { return this->totNumQuads(); }
769 #endif
770 
characterize(PrePreparedDesc * desc) const771     void characterize(PrePreparedDesc* desc) const {
772         GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
773         ColorType colorType = ColorType::kNone;
774         GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
775         Domain domain = Domain::kNo;
776         GrAAType overallAAType = fMetadata.aaType();
777 
778         desc->fNumProxies = 0;
779         desc->fNumTotalQuads = 0;
780         int maxQuadsPerMesh = 0;
781 
782         for (const auto& op : ChainRange<TextureOp>(this)) {
783             if (op.fQuads.deviceQuadType() > quadType) {
784                 quadType = op.fQuads.deviceQuadType();
785             }
786             if (op.fQuads.localQuadType() > srcQuadType) {
787                 srcQuadType = op.fQuads.localQuadType();
788             }
789             if (op.fMetadata.domain() == Domain::kYes) {
790                 domain = Domain::kYes;
791             }
792             colorType = std::max(colorType, op.fMetadata.colorType());
793             desc->fNumProxies += op.fMetadata.fProxyCount;
794 
795             for (unsigned p = 0; p < op.fMetadata.fProxyCount; ++p) {
796                 maxQuadsPerMesh = std::max(op.fViewCountPairs[p].fQuadCnt, maxQuadsPerMesh);
797             }
798             desc->fNumTotalQuads += op.totNumQuads();
799 
800             if (op.fMetadata.aaType() == GrAAType::kCoverage) {
801                 overallAAType = GrAAType::kCoverage;
802             }
803         }
804 
805         SkASSERT(desc->fNumTotalQuads == this->numChainedQuads());
806 
807         SkASSERT(!CombinedQuadCountWillOverflow(overallAAType, false, desc->fNumTotalQuads));
808 
809         auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(overallAAType,
810                                                                         maxQuadsPerMesh);
811 
812         desc->fVertexSpec = VertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true,
813                                        domain, overallAAType, /* alpha as coverage */ true,
814                                        indexBufferOption);
815 
816         SkASSERT(desc->fNumTotalQuads <= GrQuadPerEdgeAA::QuadLimit(indexBufferOption));
817     }
818 
totNumQuads() const819     int totNumQuads() const {
820 #ifdef SK_DEBUG
821         int tmp = 0;
822         for (unsigned p = 0; p < fMetadata.fProxyCount; ++p) {
823             tmp += fViewCountPairs[p].fQuadCnt;
824         }
825         SkASSERT(tmp == fMetadata.fTotalQuadCount);
826 #endif
827 
828         return fMetadata.fTotalQuadCount;
829     }
830 
numChainedQuads() const831     int numChainedQuads() const {
832         int numChainedQuads = this->totNumQuads();
833 
834         for (const GrOp* tmp = this->prevInChain(); tmp; tmp = tmp->prevInChain()) {
835             numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
836         }
837 
838         for (const GrOp* tmp = this->nextInChain(); tmp; tmp = tmp->nextInChain()) {
839             numChainedQuads += ((const TextureOp*)tmp)->totNumQuads();
840         }
841 
842         return numChainedQuads;
843     }
844 
845     // onPrePrepareDraws may or may not have been called at this point
onPrepareDraws(Target * target)846     void onPrepareDraws(Target* target) override {
847         TRACE_EVENT0("skia.gpu", TRACE_FUNC);
848 
849         SkDEBUGCODE(this->validate();)
850 
851         PrePreparedDesc desc;
852 
853         if (fPrePreparedDesc) {
854             desc = *fPrePreparedDesc;
855         } else {
856             SkArenaAlloc* arena = target->allocator();
857 
858             this->characterize(&desc);
859             desc.allocateCommon(arena, target->appliedClip());
860 
861             SkASSERT(!desc.fVertices);
862         }
863 
864         size_t vertexSize = desc.fVertexSpec.vertexSize();
865 
866         sk_sp<const GrBuffer> vbuffer;
867         int vertexOffsetInBuffer = 0;
868 
869         void* vdata = target->makeVertexSpace(vertexSize, desc.totalNumVertices(),
870                                               &vbuffer, &vertexOffsetInBuffer);
871         if (!vdata) {
872             SkDebugf("Could not allocate vertices\n");
873             return;
874         }
875 
876         sk_sp<const GrBuffer> indexBuffer;
877         if (desc.fVertexSpec.needsIndexBuffer()) {
878             indexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(target,
879                                                           desc.fVertexSpec.indexBufferOption());
880             if (!indexBuffer) {
881                 SkDebugf("Could not allocate indices\n");
882                 return;
883             }
884         }
885 
886         // Note: this allocation is always in the flush-time arena (i.e., the flushState)
887         GrMesh* meshes = target->allocMeshes(desc.fNumProxies);
888 
889         bool result;
890         if (fPrePreparedDesc) {
891             memcpy(vdata, desc.fVertices, desc.totalSizeInBytes());
892             // The above memcpy filled in the vertex data - just call FillInData to fill in the
893             // mesh data
894             result = FillInData(target->caps(), this, &desc, nullptr, meshes, vertexOffsetInBuffer,
895                                 std::move(vbuffer), std::move(indexBuffer));
896         } else {
897             // Fills in both vertex data and mesh data
898             result = FillInData(target->caps(), this, &desc, (char*) vdata, meshes,
899                                 vertexOffsetInBuffer, std::move(vbuffer), std::move(indexBuffer));
900         }
901 
902         if (!result) {
903             return;
904         }
905 
906         GrGeometryProcessor* gp;
907 
908         {
909             const GrBackendFormat& backendFormat =
910                     fViewCountPairs[0].fProxy->backendFormat();
911 
912             GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
913                                                          fMetadata.filter());
914 
915             gp = GrQuadPerEdgeAA::MakeTexturedProcessor(target->allocator(),
916                 desc.fVertexSpec, *target->caps().shaderCaps(), backendFormat,
917                 samplerState, fMetadata.fSwizzle, std::move(fTextureColorSpaceXform),
918                 fMetadata.saturate());
919 
920             SkASSERT(vertexSize == gp->vertexStride());
921         }
922 
923         target->recordDraw(gp, meshes, desc.fNumProxies,
924                            desc.fFixedDynamicState, desc.fDynamicStateArrays,
925                            desc.fVertexSpec.primitiveType());
926     }
927 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)928     void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
929         auto pipelineFlags = (GrAAType::kMSAA == fMetadata.aaType())
930                 ? GrPipeline::InputFlags::kHWAntialias
931                 : GrPipeline::InputFlags::kNone;
932 
933         auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
934                                                                  GrProcessorSet::MakeEmptySet(),
935                                                                  pipelineFlags);
936 
937         flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
938     }
939 
onCombineIfPossible(GrOp * t,GrRecordingContext::Arenas *,const GrCaps & caps)940     CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
941                                       const GrCaps& caps) override {
942         TRACE_EVENT0("skia.gpu", TRACE_FUNC);
943         const auto* that = t->cast<TextureOp>();
944 
945         if (fPrePreparedDesc || that->fPrePreparedDesc) {
946             // This should never happen (since only DDL recorded ops should be prePrepared)
947             // but, in any case, we should never combine ops that that been prePrepared
948             return CombineResult::kCannotCombine;
949         }
950 
951         if (fMetadata.domain() != that->fMetadata.domain()) {
952             // It is technically possible to combine operations across domain modes, but performance
953             // testing suggests it's better to make more draw calls where some take advantage of
954             // the more optimal shader path without coordinate clamping.
955             return CombineResult::kCannotCombine;
956         }
957         if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
958                                        that->fTextureColorSpaceXform.get())) {
959             return CombineResult::kCannotCombine;
960         }
961 
962         bool upgradeToCoverageAAOnMerge = false;
963         if (fMetadata.aaType() != that->fMetadata.aaType()) {
964             if (!CanUpgradeAAOnMerge(fMetadata.aaType(), that->fMetadata.aaType())) {
965                 return CombineResult::kCannotCombine;
966             }
967             upgradeToCoverageAAOnMerge = true;
968         }
969 
970         if (CombinedQuadCountWillOverflow(fMetadata.aaType(), upgradeToCoverageAAOnMerge,
971                                           this->numChainedQuads() + that->numChainedQuads())) {
972             return CombineResult::kCannotCombine;
973         }
974 
975         if (fMetadata.saturate() != that->fMetadata.saturate()) {
976             return CombineResult::kCannotCombine;
977         }
978         if (fMetadata.filter() != that->fMetadata.filter()) {
979             return CombineResult::kCannotCombine;
980         }
981         if (fMetadata.fSwizzle != that->fMetadata.fSwizzle) {
982             return CombineResult::kCannotCombine;
983         }
984         const auto* thisProxy = fViewCountPairs[0].fProxy.get();
985         const auto* thatProxy = that->fViewCountPairs[0].fProxy.get();
986         if (fMetadata.fProxyCount > 1 || that->fMetadata.fProxyCount > 1 ||
987             thisProxy != thatProxy) {
988             // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
989             if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
990                 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
991                 return CombineResult::kMayChain;
992             }
993             return CombineResult::kCannotCombine;
994         }
995 
996         fMetadata.fDomain |= that->fMetadata.fDomain;
997         fMetadata.fColorType = std::max(fMetadata.fColorType, that->fMetadata.fColorType);
998         if (upgradeToCoverageAAOnMerge) {
999             fMetadata.fAAType = static_cast<uint16_t>(GrAAType::kCoverage);
1000         }
1001 
1002         // Concatenate quad lists together
1003         fQuads.concat(that->fQuads);
1004         fViewCountPairs[0].fQuadCnt += that->fQuads.count();
1005         fMetadata.fTotalQuadCount += that->fQuads.count();
1006 
1007         return CombineResult::kMerged;
1008     }
1009 
1010     GrQuadBuffer<ColorDomainAndAA> fQuads;
1011     sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
1012     // 'fPrePreparedDesc' is only filled in when this op has been prePrepared. In that case,
1013     // it - and the matching dynamic and fixed state - have been allocated in the opPOD arena
1014     // not in the FlushState arena.
1015     PrePreparedDesc* fPrePreparedDesc;
1016     // All configurable state of TextureOp is packed into one field to minimize the op's size.
1017     // Historically, increasing the size of TextureOp has caused surprising perf regressions, so
1018     // consider/measure changes with care.
1019     Metadata fMetadata;
1020 
1021     // This field must go last. When allocating this op, we will allocate extra space to hold
1022     // additional ViewCountPairs immediately after the op's allocation so we can treat this
1023     // as an fProxyCnt-length array.
1024     ViewCountPair fViewCountPairs[1];
1025 
1026     typedef GrMeshDrawOp INHERITED;
1027 };
1028 
1029 }  // anonymous namespace
1030 
1031 #if GR_TEST_UTILS
ClassID()1032 uint32_t GrTextureOp::ClassID() {
1033     return TextureOp::ClassID();
1034 }
1035 #endif
1036 
Make(GrRecordingContext * context,GrSurfaceProxyView proxyView,SkAlphaType alphaType,sk_sp<GrColorSpaceXform> textureXform,GrSamplerState::Filter filter,const SkPMColor4f & color,Saturate saturate,SkBlendMode blendMode,GrAAType aaType,DrawQuad * quad,const SkRect * domain)1037 std::unique_ptr<GrDrawOp> GrTextureOp::Make(GrRecordingContext* context,
1038                                             GrSurfaceProxyView proxyView,
1039                                             SkAlphaType alphaType,
1040                                             sk_sp<GrColorSpaceXform> textureXform,
1041                                             GrSamplerState::Filter filter,
1042                                             const SkPMColor4f& color,
1043                                             Saturate saturate,
1044                                             SkBlendMode blendMode,
1045                                             GrAAType aaType,
1046                                             DrawQuad* quad,
1047                                             const SkRect* domain) {
1048     // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
1049     if (domain && domain->contains(proxyView.proxy()->backingStoreBoundsRect())) {
1050         // No need for a shader-based domain if hardware clamping achieves the same effect
1051         domain = nullptr;
1052     }
1053 
1054     if (filter != GrSamplerState::Filter::kNearest &&
1055         !filter_has_effect(quad->fLocal, quad->fDevice)) {
1056         filter = GrSamplerState::Filter::kNearest;
1057     }
1058 
1059     if (blendMode == SkBlendMode::kSrcOver) {
1060         return TextureOp::Make(context, std::move(proxyView), std::move(textureXform), filter,
1061                                color, saturate, aaType, std::move(quad), domain);
1062     } else {
1063         // Emulate complex blending using GrFillRectOp
1064         GrPaint paint;
1065         paint.setColor4f(color);
1066         paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
1067 
1068         std::unique_ptr<GrFragmentProcessor> fp;
1069         if (domain) {
1070             const auto& caps = *context->priv().caps();
1071             SkRect localRect;
1072             if (quad->fLocal.asRect(&localRect)) {
1073                 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(), filter,
1074                                                  *domain, localRect, caps);
1075             } else {
1076                 fp = GrTextureEffect::MakeSubset(std::move(proxyView), alphaType, SkMatrix::I(), filter,
1077                                                  *domain, caps);
1078             }
1079         } else {
1080             fp = GrTextureEffect::Make(std::move(proxyView), alphaType, SkMatrix::I(), filter);
1081         }
1082         fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
1083         paint.addColorFragmentProcessor(std::move(fp));
1084         if (saturate == GrTextureOp::Saturate::kYes) {
1085             paint.addColorFragmentProcessor(GrClampFragmentProcessor::Make(false));
1086         }
1087 
1088         return GrFillRectOp::Make(context, std::move(paint), aaType, quad);
1089     }
1090 }
1091 
1092 // A helper class that assists in breaking up bulk API quad draws into manageable chunks.
1093 class GrTextureOp::BatchSizeLimiter {
1094 public:
BatchSizeLimiter(GrRenderTargetContext * rtc,const GrClip & clip,GrRecordingContext * context,int numEntries,GrSamplerState::Filter filter,GrTextureOp::Saturate saturate,SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> textureColorSpaceXform)1095     BatchSizeLimiter(GrRenderTargetContext* rtc,
1096                      const GrClip& clip,
1097                      GrRecordingContext* context,
1098                      int numEntries,
1099                      GrSamplerState::Filter filter,
1100                      GrTextureOp::Saturate saturate,
1101                      SkCanvas::SrcRectConstraint constraint,
1102                      const SkMatrix& viewMatrix,
1103                      sk_sp<GrColorSpaceXform> textureColorSpaceXform)
1104             : fRTC(rtc)
1105             , fClip(clip)
1106             , fContext(context)
1107             , fFilter(filter)
1108             , fSaturate(saturate)
1109             , fConstraint(constraint)
1110             , fViewMatrix(viewMatrix)
1111             , fTextureColorSpaceXform(textureColorSpaceXform)
1112             , fNumLeft(numEntries) {
1113     }
1114 
createOp(GrRenderTargetContext::TextureSetEntry set[],int clumpSize,GrAAType aaType)1115     void createOp(GrRenderTargetContext::TextureSetEntry set[],
1116                   int clumpSize,
1117                   GrAAType aaType) {
1118         int clumpProxyCount = proxy_run_count(&set[fNumClumped], clumpSize);
1119         std::unique_ptr<GrDrawOp> op = TextureOp::Make(fContext, &set[fNumClumped], clumpSize,
1120                                                        clumpProxyCount, fFilter, fSaturate, aaType,
1121                                                        fConstraint, fViewMatrix,
1122                                                        fTextureColorSpaceXform);
1123         fRTC->addDrawOp(fClip, std::move(op));
1124 
1125         fNumLeft -= clumpSize;
1126         fNumClumped += clumpSize;
1127     }
1128 
numLeft() const1129     int numLeft() const { return fNumLeft;  }
baseIndex() const1130     int baseIndex() const { return fNumClumped; }
1131 
1132 private:
1133     GrRenderTargetContext*      fRTC;
1134     const GrClip&               fClip;
1135     GrRecordingContext*         fContext;
1136     GrSamplerState::Filter      fFilter;
1137     GrTextureOp::Saturate       fSaturate;
1138     SkCanvas::SrcRectConstraint fConstraint;
1139     const SkMatrix&             fViewMatrix;
1140     sk_sp<GrColorSpaceXform>    fTextureColorSpaceXform;
1141 
1142     int                         fNumLeft;
1143     int                         fNumClumped = 0; // also the offset for the start of the next clump
1144 };
1145 
1146 // Greedily clump quad draws together until the index buffer limit is exceeded.
AddTextureSetOps(GrRenderTargetContext * rtc,const GrClip & clip,GrRecordingContext * context,GrRenderTargetContext::TextureSetEntry set[],int cnt,int proxyRunCnt,GrSamplerState::Filter filter,Saturate saturate,SkBlendMode blendMode,GrAAType aaType,SkCanvas::SrcRectConstraint constraint,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> textureColorSpaceXform)1147 void GrTextureOp::AddTextureSetOps(GrRenderTargetContext* rtc,
1148                                    const GrClip& clip,
1149                                    GrRecordingContext* context,
1150                                    GrRenderTargetContext::TextureSetEntry set[],
1151                                    int cnt,
1152                                    int proxyRunCnt,
1153                                    GrSamplerState::Filter filter,
1154                                    Saturate saturate,
1155                                    SkBlendMode blendMode,
1156                                    GrAAType aaType,
1157                                    SkCanvas::SrcRectConstraint constraint,
1158                                    const SkMatrix& viewMatrix,
1159                                    sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
1160     // Ensure that the index buffer limits are lower than the proxy and quad count limits of
1161     // the op's metadata so we don't need to worry about overflow.
1162     SkDEBUGCODE(TextureOp::ValidateResourceLimits();)
1163     SkASSERT(proxy_run_count(set, cnt) == proxyRunCnt);
1164 
1165     // First check if we can support batches as a single op
1166     if (blendMode != SkBlendMode::kSrcOver ||
1167         !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
1168         // Append each entry as its own op; these may still be GrTextureOps if the blend mode is
1169         // src-over but the backend doesn't support dynamic state changes. Otherwise Make()
1170         // automatically creates the appropriate GrFillRectOp to emulate GrTextureOp.
1171         SkMatrix ctm;
1172         for (int i = 0; i < cnt; ++i) {
1173             float alpha = set[i].fAlpha;
1174             ctm = viewMatrix;
1175             if (set[i].fPreViewMatrix) {
1176                 ctm.preConcat(*set[i].fPreViewMatrix);
1177             }
1178 
1179             DrawQuad quad;
1180             quad.fEdgeFlags = set[i].fAAFlags;
1181             if (set[i].fDstClipQuad) {
1182                 quad.fDevice = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
1183 
1184                 SkPoint srcPts[4];
1185                 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
1186                 quad.fLocal = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
1187             } else {
1188                 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
1189                 quad.fLocal = GrQuad(set[i].fSrcRect);
1190             }
1191 
1192             const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint
1193                     ? &set[i].fSrcRect : nullptr;
1194 
1195             auto op = Make(context, set[i].fProxyView, set[i].fSrcAlphaType, textureColorSpaceXform,
1196                            filter, {alpha, alpha, alpha, alpha}, saturate, blendMode, aaType,
1197                            &quad, domain);
1198             rtc->addDrawOp(clip, std::move(op));
1199         }
1200         return;
1201     }
1202 
1203     // Second check if we can always just make a single op and avoid the extra iteration
1204     // needed to clump things together.
1205     if (cnt <= std::min(GrResourceProvider::MaxNumNonAAQuads(),
1206                       GrResourceProvider::MaxNumAAQuads())) {
1207         auto op = TextureOp::Make(context, set, cnt, proxyRunCnt, filter, saturate, aaType,
1208                                   constraint, viewMatrix, std::move(textureColorSpaceXform));
1209         rtc->addDrawOp(clip, std::move(op));
1210         return;
1211     }
1212 
1213     BatchSizeLimiter state(rtc, clip, context, cnt, filter, saturate, constraint, viewMatrix,
1214                            std::move(textureColorSpaceXform));
1215 
1216     // kNone and kMSAA never get altered
1217     if (aaType == GrAAType::kNone || aaType == GrAAType::kMSAA) {
1218         // Clump these into series of MaxNumNonAAQuads-sized GrTextureOps
1219         while (state.numLeft() > 0) {
1220             int clumpSize = std::min(state.numLeft(), GrResourceProvider::MaxNumNonAAQuads());
1221 
1222             state.createOp(set, clumpSize, aaType);
1223         }
1224     } else {
1225         // kCoverage can be downgraded to kNone. Note that the following is conservative. kCoverage
1226         // can also get downgraded to kNone if all the quads are on integer coordinates and
1227         // axis-aligned.
1228         SkASSERT(aaType == GrAAType::kCoverage);
1229 
1230         while (state.numLeft() > 0) {
1231             GrAAType runningAA = GrAAType::kNone;
1232             bool clumped = false;
1233 
1234             for (int i = 0; i < state.numLeft(); ++i) {
1235                 int absIndex = state.baseIndex() + i;
1236 
1237                 if (set[absIndex].fAAFlags != GrQuadAAFlags::kNone) {
1238 
1239                     if (i >= GrResourceProvider::MaxNumAAQuads()) {
1240                         // Here we either need to boost the AA type to kCoverage, but doing so with
1241                         // all the accumulated quads would overflow, or we have a set of AA quads
1242                         // that has just gotten too large. In either case, calve off the existing
1243                         // quads as their own TextureOp.
1244                         state.createOp(
1245                             set,
1246                             runningAA == GrAAType::kNone ? i : GrResourceProvider::MaxNumAAQuads(),
1247                             runningAA); // maybe downgrading AA here
1248                         clumped = true;
1249                         break;
1250                     }
1251 
1252                     runningAA = GrAAType::kCoverage;
1253                 } else if (runningAA == GrAAType::kNone) {
1254 
1255                     if (i >= GrResourceProvider::MaxNumNonAAQuads()) {
1256                         // Here we've found a consistent batch of non-AA quads that has gotten too
1257                         // large. Calve it off as its own GrTextureOp.
1258                         state.createOp(set, GrResourceProvider::MaxNumNonAAQuads(),
1259                                        GrAAType::kNone); // definitely downgrading AA here
1260                         clumped = true;
1261                         break;
1262                     }
1263                 }
1264             }
1265 
1266             if (!clumped) {
1267                 // We ran through the above loop w/o hitting a limit. Spit out this last clump of
1268                 // quads and call it a day.
1269                 state.createOp(set, state.numLeft(), runningAA); // maybe downgrading AA here
1270             }
1271         }
1272     }
1273 }
1274 
1275 #if GR_TEST_UTILS
1276 #include "include/private/GrRecordingContext.h"
1277 #include "src/gpu/GrProxyProvider.h"
1278 #include "src/gpu/GrRecordingContextPriv.h"
1279 
GR_DRAW_OP_TEST_DEFINE(TextureOp)1280 GR_DRAW_OP_TEST_DEFINE(TextureOp) {
1281     SkISize dims;
1282     dims.fHeight = random->nextULessThan(90) + 10;
1283     dims.fWidth = random->nextULessThan(90) + 10;
1284     auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
1285     GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
1286     SkBackingFit fit = SkBackingFit::kExact;
1287     if (mipMapped == GrMipMapped::kNo) {
1288         fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
1289     }
1290     const GrBackendFormat format =
1291             context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
1292                                                             GrRenderable::kNo);
1293     GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888);
1294 
1295     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
1296     sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
1297             format, dims, swizzle, GrRenderable::kNo, 1, mipMapped, fit, SkBudgeted::kNo,
1298             GrProtected::kNo, GrInternalSurfaceFlags::kNone);
1299 
1300     SkRect rect = GrTest::TestRect(random);
1301     SkRect srcRect;
1302     srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
1303     srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
1304     srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
1305     srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
1306     SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
1307     SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
1308     GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
1309             static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
1310     while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
1311         filter = (GrSamplerState::Filter)random->nextULessThan(
1312                 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
1313     }
1314     auto texXform = GrTest::TestColorXform(random);
1315     GrAAType aaType = GrAAType::kNone;
1316     if (random->nextBool()) {
1317         aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
1318     }
1319     GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
1320     aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
1321     aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
1322     aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
1323     aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
1324     bool useDomain = random->nextBool();
1325     auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
1326     GrSurfaceProxyView proxyView(
1327             std::move(proxy), origin,
1328             context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
1329     auto alphaType = static_cast<SkAlphaType>(
1330             random->nextRangeU(kUnknown_SkAlphaType + 1, kLastEnum_SkAlphaType));
1331 
1332     DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect), aaFlags};
1333     return GrTextureOp::Make(context, std::move(proxyView), alphaType, std::move(texXform), filter,
1334                              color, saturate, SkBlendMode::kSrcOver, aaType,
1335                              &quad, useDomain ? &srcRect : nullptr);
1336 }
1337 
1338 #endif
1339