blob: 3f9f0f144b9761d07572dbaa71919660f1a65260 [file] [log] [blame]
Brian Salomon34169692017-08-28 15:32:01 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrTextureOp.h"
Brian Salomond7065e72018-10-12 11:42:02 -04009#include <new>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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/GrTextureProxy.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#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/SkGr.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040032#include "src/gpu/geometry/GrQuad.h"
Michael Ludwigd17e05a2019-06-04 09:10:34 -040033#include "src/gpu/geometry/GrQuadList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/glsl/GrGLSLVarying.h"
35#include "src/gpu/ops/GrMeshDrawOp.h"
36#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Brian Salomon34169692017-08-28 15:32:01 -040037
38namespace {
39
Michael Ludwig460eb5e2018-10-29 11:09:29 -040040using Domain = GrQuadPerEdgeAA::Domain;
Michael Ludwigc182b942018-11-16 10:27:51 -050041using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050042using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040043
Brian Salomon246bc3d2018-12-06 15:33:02 -050044// if normalizing the domain then pass 1/width, 1/height, 1 for iw, ih, h. Otherwise pass
45// 1, 1, and height.
46static SkRect compute_domain(Domain domain, GrSamplerState::Filter filter, GrSurfaceOrigin origin,
47 const SkRect& srcRect, float iw, float ih, float h) {
48 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Michael Ludwig460eb5e2018-10-29 11:09:29 -040049 if (domain == Domain::kNo) {
50 // Either the quad has no domain constraint and is batched with a domain constrained op
51 // (in which case we want a domain that doesn't restrict normalized tex coords), or the
52 // entire op doesn't use the domain, in which case the returned value is ignored.
53 return kLargeRect;
54 }
55
56 auto ltrb = Sk4f::Load(&srcRect);
57 if (filter == GrSamplerState::Filter::kBilerp) {
58 auto rblt = SkNx_shuffle<2, 3, 0, 1>(ltrb);
59 auto whwh = (rblt - ltrb).abs();
60 auto c = (rblt + ltrb) * 0.5f;
61 static const Sk4f kOffsets = {0.5f, 0.5f, -0.5f, -0.5f};
62 ltrb = (whwh < 1.f).thenElse(c, ltrb + kOffsets);
63 }
64 ltrb *= Sk4f(iw, ih, iw, ih);
65 if (origin == kBottomLeft_GrSurfaceOrigin) {
66 static const Sk4f kMul = {1.f, -1.f, 1.f, -1.f};
Brian Salomon246bc3d2018-12-06 15:33:02 -050067 const Sk4f kAdd = {0.f, h, 0.f, h};
Michael Ludwig460eb5e2018-10-29 11:09:29 -040068 ltrb = SkNx_shuffle<0, 3, 2, 1>(kMul * ltrb + kAdd);
69 }
70
71 SkRect domainRect;
72 ltrb.store(&domainRect);
73 return domainRect;
74}
75
Brian Salomon246bc3d2018-12-06 15:33:02 -050076// If normalizing the src quad then pass 1/width, 1/height, 1 for iw, ih, h. Otherwise pass
77// 1, 1, and height.
Michael Ludwigde4c58c2019-06-04 09:12:59 -040078static GrQuad compute_src_quad_from_rect(GrSurfaceOrigin origin, const SkRect& srcRect,
79 float iw, float ih, float h) {
Michael Ludwig460eb5e2018-10-29 11:09:29 -040080 // Convert the pixel-space src rectangle into normalized texture coordinates
81 SkRect texRect = {
82 iw * srcRect.fLeft,
83 ih * srcRect.fTop,
84 iw * srcRect.fRight,
85 ih * srcRect.fBottom
86 };
87 if (origin == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon246bc3d2018-12-06 15:33:02 -050088 texRect.fTop = h - texRect.fTop;
89 texRect.fBottom = h - texRect.fBottom;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040090 }
Michael Ludwigde4c58c2019-06-04 09:12:59 -040091 return GrQuad(texRect);
Michael Ludwig460eb5e2018-10-29 11:09:29 -040092}
Michael Ludwig009b92e2019-02-15 16:03:53 -050093// Normalizes logical src coords and corrects for origin
Michael Ludwigde4c58c2019-06-04 09:12:59 -040094static GrQuad compute_src_quad(GrSurfaceOrigin origin, const GrQuad& srcQuad,
95 float iw, float ih, float h) {
Michael Ludwig009b92e2019-02-15 16:03:53 -050096 // The src quad should not have any perspective
97 SkASSERT(!srcQuad.hasPerspective());
Michael Ludwigb3461fa2019-04-30 11:50:55 -040098 skvx::Vec<4, float> xs = srcQuad.x4f() * iw;
99 skvx::Vec<4, float> ys = srcQuad.y4f() * ih;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500100 if (origin == kBottomLeft_GrSurfaceOrigin) {
101 ys = h - ys;
102 }
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400103 return GrQuad(xs, ys, srcQuad.quadType());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500104}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400105
Brian Salomon34169692017-08-28 15:32:01 -0400106/**
107 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
108 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
109 */
110class TextureOp final : public GrMeshDrawOp {
111public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500112 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400113 sk_sp<GrTextureProxy> proxy,
114 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500115 const SkPMColor4f& color,
Robert Phillips7c525e62018-06-12 10:11:12 -0400116 const SkRect& srcRect,
117 const SkRect& dstRect,
118 GrAAType aaType,
Brian Salomon2213ee92018-10-02 10:44:21 -0400119 GrQuadAAFlags aaFlags,
Robert Phillips7c525e62018-06-12 10:11:12 -0400120 SkCanvas::SrcRectConstraint constraint,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400121 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500122 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400123 GrQuad dstQuad = GrQuad::MakeFromRect(dstRect, viewMatrix);
Robert Phillipsc994a932018-06-19 13:09:54 -0400124
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400125 if (dstQuad.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500126 // Disable filtering if possible (note AA optimizations for rects are automatically
127 // handled above in GrResolveAATypeForQuad).
128 if (filter != GrSamplerState::Filter::kNearest &&
129 !GrTextureOp::GetFilterHasEffect(viewMatrix, srcRect, dstRect)) {
130 filter = GrSamplerState::Filter::kNearest;
131 }
132 }
133
134 GrOpMemoryPool* pool = context->priv().opMemoryPool();
135 // srcRect provides both local coords and domain (if needed), so use nullptr for srcQuad
Brian Salomon2213ee92018-10-02 10:44:21 -0400136 return pool->allocate<TextureOp>(
Michael Ludwig41f395d2019-05-23 13:59:45 -0400137 std::move(proxy), filter, color, dstQuad, srcRect, constraint,
138 nullptr, aaType, aaFlags, std::move(textureColorSpaceXform));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500139 }
140 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
141 sk_sp<GrTextureProxy> proxy,
142 GrSamplerState::Filter filter,
143 const SkPMColor4f& color,
144 const SkPoint srcQuad[4],
145 const SkPoint dstQuad[4],
146 GrAAType aaType,
147 GrQuadAAFlags aaFlags,
148 const SkRect* domain,
149 const SkMatrix& viewMatrix,
150 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400151 GrQuad grDstQuad = GrQuad::MakeFromSkQuad(dstQuad, viewMatrix);
152 GrQuad grSrcQuad = GrQuad::MakeFromSkQuad(srcQuad, SkMatrix::I());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500153
154 // If constraint remains fast, the value in srcRect will be ignored since srcQuads provides
155 // the local coordinates and a domain won't be used.
156 SkRect srcRect = SkRect::MakeEmpty();
157 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
158 if (domain) {
159 srcRect = *domain;
160 constraint = SkCanvas::kStrict_SrcRectConstraint;
161 }
162
163 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400164 // Pass domain as srcRect if provided, but send srcQuad as a GrQuad for local coords
Michael Ludwig009b92e2019-02-15 16:03:53 -0500165 return pool->allocate<TextureOp>(
Michael Ludwig41f395d2019-05-23 13:59:45 -0400166 std::move(proxy), filter, color, grDstQuad, srcRect, constraint, &grSrcQuad,
167 aaType, aaFlags, std::move(textureColorSpaceXform));
Brian Salomon34169692017-08-28 15:32:01 -0400168 }
Robert Phillipsb97da532019-02-12 15:24:12 -0500169 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Brian Salomond7065e72018-10-12 11:42:02 -0400170 const GrRenderTargetContext::TextureSetEntry set[],
Brian Salomond003d222018-11-26 13:25:05 -0500171 int cnt, GrSamplerState::Filter filter, GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400172 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500173 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500174 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400175 size_t size = sizeof(TextureOp) + sizeof(Proxy) * (cnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500176 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400177 void* mem = pool->allocate(size);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400178 return std::unique_ptr<GrDrawOp>(new (mem) TextureOp(
179 set, cnt, filter, aaType, constraint, viewMatrix,
180 std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400181 }
Brian Salomon34169692017-08-28 15:32:01 -0400182
Brian Salomon336ce7b2017-09-08 08:23:58 -0400183 ~TextureOp() override {
Brian Salomond7065e72018-10-12 11:42:02 -0400184 for (unsigned p = 0; p < fProxyCnt; ++p) {
185 if (fFinalized) {
186 fProxies[p].fProxy->completedRead();
187 } else {
188 fProxies[p].fProxy->unref();
189 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400190 }
191 }
Brian Salomon34169692017-08-28 15:32:01 -0400192
193 const char* name() const override { return "TextureOp"; }
194
Chris Dalton1706cbf2019-05-21 19:35:29 -0600195 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomond7065e72018-10-12 11:42:02 -0400196 for (unsigned p = 0; p < fProxyCnt; ++p) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600197 bool mipped = (GrSamplerState::Filter::kMipMap == this->filter());
198 func(fProxies[p].fProxy, GrMipMapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400199 }
200 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400201
Brian Osman9a390ac2018-11-12 09:47:48 -0500202#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400203 SkString dumpInfo() const override {
204 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400205 str.appendf("# draws: %d\n", fQuads.count());
206 int q = 0;
207 for (unsigned p = 0; p < fProxyCnt; ++p) {
208 str.appendf("Proxy ID: %d, Filter: %d\n", fProxies[p].fProxy->uniqueID().asUInt(),
209 static_cast<int>(fFilter));
210 for (int i = 0; i < fProxies[p].fQuadCnt; ++i, ++q) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400211 GrQuad quad = fQuads[q];
Michael Ludwigc96fc372019-01-08 15:46:15 -0500212 const ColorDomainAndAA& info = fQuads.metadata(i);
Brian Salomond7065e72018-10-12 11:42:02 -0400213 str.appendf(
214 "%d: Color: 0x%08x, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
215 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
Michael Ludwigc96fc372019-01-08 15:46:15 -0500216 i, info.fColor.toBytes_RGBA(), info.fSrcRect.fLeft, info.fSrcRect.fTop,
217 info.fSrcRect.fRight, info.fSrcRect.fBottom, quad.point(0).fX,
218 quad.point(0).fY, quad.point(1).fX, quad.point(1).fY,
219 quad.point(2).fX, quad.point(2).fY, quad.point(3).fX,
220 quad.point(3).fY);
Brian Salomond7065e72018-10-12 11:42:02 -0400221 }
Brian Salomon34169692017-08-28 15:32:01 -0400222 }
223 str += INHERITED::dumpInfo();
224 return str;
225 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500226#endif
Brian Salomon34169692017-08-28 15:32:01 -0400227
Brian Osman5ced0bf2019-03-15 10:15:29 -0400228 GrProcessorSet::Analysis finalize(
Brian Osman8fa7ab42019-03-18 10:22:42 -0400229 const GrCaps& caps, const GrAppliedClip*, GrFSAAType, GrClampType clampType) override {
Brian Salomon34169692017-08-28 15:32:01 -0400230 SkASSERT(!fFinalized);
231 fFinalized = true;
Brian Salomond7065e72018-10-12 11:42:02 -0400232 for (unsigned p = 0; p < fProxyCnt; ++p) {
233 fProxies[p].fProxy->addPendingRead();
234 fProxies[p].fProxy->unref();
235 }
Brian Osman8fa7ab42019-03-18 10:22:42 -0400236 fColorType = static_cast<unsigned>(ColorType::kNone);
237 for (int q = 0; q < fQuads.count(); ++q) {
238 const ColorDomainAndAA& info = fQuads.metadata(q);
239 auto colorType = GrQuadPerEdgeAA::MinColorType(info.fColor, clampType, caps);
240 fColorType = SkTMax(fColorType, static_cast<unsigned>(colorType));
241 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700242 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400243 }
244
Brian Salomon485b8c62018-01-12 15:11:06 -0500245 FixedFunctionFlags fixedFunctionFlags() const override {
246 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
247 : FixedFunctionFlags::kNone;
248 }
Brian Salomon34169692017-08-28 15:32:01 -0400249
250 DEFINE_OP_CLASS_ID
251
252private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400253 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500254
Michael Ludwig009b92e2019-02-15 16:03:53 -0500255 // dstQuad and dstQuadType should be the geometry transformed by the view matrix.
256 // srcRect represents original src rect and will be used as the domain when constraint is strict
257 // If srcQuad is provided, it will be used for the local coords instead of srcRect, although
258 // srcRect will still specify the domain constraint if needed.
Brian Osman3d139a42018-11-19 10:42:10 -0500259 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, const SkPMColor4f& color,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400260 const GrQuad& dstQuad, const SkRect& srcRect,
261 SkCanvas::SrcRectConstraint constraint, const GrQuad* srcQuad, GrAAType aaType,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500262 GrQuadAAFlags aaFlags, sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomon34169692017-08-28 15:32:01 -0400263 : INHERITED(ClassID())
Brian Osman3ebd3542018-07-30 14:36:53 -0400264 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Brian Salomon0087c832018-10-15 14:48:20 -0400265 , fFilter(static_cast<unsigned>(filter))
Brian Osman2b23c4b2018-06-01 12:25:08 -0400266 , fFinalized(0) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400267 // Clean up disparities between the overall aa type and edge configuration and apply
268 // optimizations based on the rect and matrix when appropriate
Michael Ludwig41f395d2019-05-23 13:59:45 -0400269 GrResolveAATypeForQuad(aaType, aaFlags, dstQuad, &aaType, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400270 fAAType = static_cast<unsigned>(aaType);
271
Brian Salomonf1709042018-10-03 11:57:00 -0400272 // We expect our caller to have already caught this optimization.
Brian Salomond7065e72018-10-12 11:42:02 -0400273 SkASSERT(!srcRect.contains(proxy->getWorstCaseBoundsRect()) ||
Brian Salomonf1709042018-10-03 11:57:00 -0400274 constraint == SkCanvas::kFast_SrcRectConstraint);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500275
Brian Salomonf09abc52018-10-03 15:59:04 -0400276 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
277 // If we don't have (or determined we don't need) coverage AA then we can skip using a
278 // domain.
279 if (constraint == SkCanvas::kStrict_SrcRectConstraint &&
Brian Salomon0087c832018-10-15 14:48:20 -0400280 this->filter() == GrSamplerState::Filter::kNearest &&
Michael Ludwig6bee7762018-10-19 09:50:36 -0400281 aaType != GrAAType::kCoverage) {
Brian Salomonf09abc52018-10-03 15:59:04 -0400282 constraint = SkCanvas::kFast_SrcRectConstraint;
283 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500284
285 Domain domain = constraint == SkCanvas::kStrict_SrcRectConstraint ? Domain::kYes
286 : Domain::kNo;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500287 // Initially, if srcQuad is provided it will always be at index 0 of fSrcQuads
Michael Ludwig41f395d2019-05-23 13:59:45 -0400288 fQuads.push_back(dstQuad, {color, srcRect, srcQuad ? 0 : -1, domain, aaFlags});
Michael Ludwig009b92e2019-02-15 16:03:53 -0500289 if (srcQuad) {
Michael Ludwig41f395d2019-05-23 13:59:45 -0400290 fSrcQuads.push_back(*srcQuad);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500291 }
Brian Salomond7065e72018-10-12 11:42:02 -0400292 fProxyCnt = 1;
293 fProxies[0] = {proxy.release(), 1};
Michael Ludwig41f395d2019-05-23 13:59:45 -0400294 this->setBounds(dstQuad.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
295 IsZeroArea::kNo);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500296 fDomain = static_cast<unsigned>(domain);
Brian Salomond7065e72018-10-12 11:42:02 -0400297 }
298 TextureOp(const GrRenderTargetContext::TextureSetEntry set[], int cnt,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400299 GrSamplerState::Filter filter, GrAAType aaType,
300 SkCanvas::SrcRectConstraint constraint, const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500301 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400302 : INHERITED(ClassID())
303 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Brian Salomon0087c832018-10-15 14:48:20 -0400304 , fFilter(static_cast<unsigned>(filter))
Brian Salomond7065e72018-10-12 11:42:02 -0400305 , fFinalized(0) {
Brian Salomond7065e72018-10-12 11:42:02 -0400306 fProxyCnt = SkToUInt(cnt);
307 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig6bee7762018-10-19 09:50:36 -0400308 GrAAType overallAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon0087c832018-10-15 14:48:20 -0400309 bool mustFilter = false;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500310 // Most dst rects are transformed by the same view matrix, so their quad types start
311 // identical, unless an entry provides a dstClip or additional transform that changes it.
312 // The quad list will automatically adapt to that.
Michael Ludwig41f395d2019-05-23 13:59:45 -0400313 fQuads.reserve(cnt, viewMatrix.hasPerspective());
Brian Salomon1d835422019-03-13 16:11:44 -0400314 bool allOpaque = true;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400315 Domain netDomain = Domain::kNo;
Brian Salomond7065e72018-10-12 11:42:02 -0400316 for (unsigned p = 0; p < fProxyCnt; ++p) {
317 fProxies[p].fProxy = SkRef(set[p].fProxy.get());
318 fProxies[p].fQuadCnt = 1;
319 SkASSERT(fProxies[p].fProxy->textureType() == fProxies[0].fProxy->textureType());
320 SkASSERT(fProxies[p].fProxy->config() == fProxies[0].fProxy->config());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500321
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500322 SkMatrix ctm = viewMatrix;
323 if (set[p].fPreViewMatrix) {
324 ctm.preConcat(*set[p].fPreViewMatrix);
325 }
326
Michael Ludwigce62dec2019-02-19 11:48:46 -0500327 // Use dstRect unless dstClip is provided, which is assumed to be a quad
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500328 auto quad = set[p].fDstClipQuad == nullptr ?
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400329 GrQuad::MakeFromRect(set[p].fDstRect, ctm) :
330 GrQuad::MakeFromSkQuad(set[p].fDstClipQuad, ctm);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500331
Michael Ludwig41f395d2019-05-23 13:59:45 -0400332 bounds.joinPossiblyEmptyRect(quad.bounds());
Michael Ludwig6bee7762018-10-19 09:50:36 -0400333 GrQuadAAFlags aaFlags;
334 // Don't update the overall aaType, might be inappropriate for some of the quads
335 GrAAType aaForQuad;
Michael Ludwig41f395d2019-05-23 13:59:45 -0400336 GrResolveAATypeForQuad(aaType, set[p].fAAFlags, quad, &aaForQuad, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400337 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
338 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
339 if (overallAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
340 overallAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400341 }
Brian Salomon0087c832018-10-15 14:48:20 -0400342 if (!mustFilter && this->filter() != GrSamplerState::Filter::kNearest) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400343 mustFilter = quad.quadType() != GrQuad::Type::kAxisAligned ||
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500344 GrTextureOp::GetFilterHasEffect(ctm, set[p].fSrcRect,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500345 set[p].fDstRect);
Brian Salomon0087c832018-10-15 14:48:20 -0400346 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400347 Domain domainForQuad = Domain::kNo;
348 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
349 // Check (briefly) if the strict constraint is needed for this set entry
350 if (!set[p].fSrcRect.contains(fProxies[p].fProxy->getWorstCaseBoundsRect()) &&
351 (mustFilter || aaForQuad == GrAAType::kCoverage)) {
352 // Can't rely on hardware clamping and the draw will access outer texels
353 // for AA and/or bilerp
354 netDomain = Domain::kYes;
355 domainForQuad = Domain::kYes;
356 }
357 }
Brian Salomond003d222018-11-26 13:25:05 -0500358 float alpha = SkTPin(set[p].fAlpha, 0.f, 1.f);
Brian Salomon1d835422019-03-13 16:11:44 -0400359 allOpaque &= (1.f == alpha);
Brian Salomond003d222018-11-26 13:25:05 -0500360 SkPMColor4f color{alpha, alpha, alpha, alpha};
Michael Ludwigce62dec2019-02-19 11:48:46 -0500361 int srcQuadIndex = -1;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500362 if (set[p].fDstClipQuad) {
Michael Ludwigce62dec2019-02-19 11:48:46 -0500363 // Derive new source coordinates that match dstClip's relative locations in dstRect,
364 // but with respect to srcRect
365 SkPoint srcQuad[4];
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500366 GrMapRectPoints(set[p].fDstRect, set[p].fSrcRect, set[p].fDstClipQuad, srcQuad, 4);
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400367 fSrcQuads.push_back(GrQuad::MakeFromSkQuad(srcQuad, SkMatrix::I()));
Michael Ludwigce62dec2019-02-19 11:48:46 -0500368 srcQuadIndex = fSrcQuads.count() - 1;
369 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400370 fQuads.push_back(quad, {color, set[p].fSrcRect, srcQuadIndex, domainForQuad, aaFlags});
Brian Salomond7065e72018-10-12 11:42:02 -0400371 }
Michael Ludwig6bee7762018-10-19 09:50:36 -0400372 fAAType = static_cast<unsigned>(overallAAType);
Brian Salomon0087c832018-10-15 14:48:20 -0400373 if (!mustFilter) {
374 fFilter = static_cast<unsigned>(GrSamplerState::Filter::kNearest);
375 }
Brian Salomond7065e72018-10-12 11:42:02 -0400376 this->setBounds(bounds, HasAABloat(this->aaType() == GrAAType::kCoverage), IsZeroArea::kNo);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400377 fDomain = static_cast<unsigned>(netDomain);
Brian Salomon34169692017-08-28 15:32:01 -0400378 }
379
Brian Salomon574d6162018-11-19 16:57:25 -0500380 void tess(void* v, const VertexSpec& spec, const GrTextureProxy* proxy, int start,
381 int cnt) const {
Brian Salomond7065e72018-10-12 11:42:02 -0400382 TRACE_EVENT0("skia", TRACE_FUNC);
Brian Salomond7065e72018-10-12 11:42:02 -0400383 auto origin = proxy->origin();
384 const auto* texture = proxy->peekTexture();
Brian Salomon246bc3d2018-12-06 15:33:02 -0500385 float iw, ih, h;
386 if (proxy->textureType() == GrTextureType::kRectangle) {
387 iw = ih = 1.f;
388 h = texture->height();
389 } else {
390 iw = 1.f / texture->width();
391 ih = 1.f / texture->height();
392 h = 1.f;
393 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000394
Brian Salomond7065e72018-10-12 11:42:02 -0400395 for (int i = start; i < start + cnt; ++i) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400396 const GrQuad& device = fQuads[i];
Michael Ludwigc96fc372019-01-08 15:46:15 -0500397 const ColorDomainAndAA& info = fQuads.metadata(i);
398
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400399 GrQuad srcQuad = info.fSrcQuadIndex >= 0 ?
Michael Ludwig009b92e2019-02-15 16:03:53 -0500400 compute_src_quad(origin, fSrcQuads[info.fSrcQuadIndex], iw, ih, h) :
401 compute_src_quad_from_rect(origin, info.fSrcRect, iw, ih, h);
Brian Salomon246bc3d2018-12-06 15:33:02 -0500402 SkRect domain =
Michael Ludwigc96fc372019-01-08 15:46:15 -0500403 compute_domain(info.domain(), this->filter(), origin, info.fSrcRect, iw, ih, h);
404 v = GrQuadPerEdgeAA::Tessellate(v, spec, device, info.fColor, srcQuad, domain,
405 info.aaFlags());
Brian Salomon17031a72018-05-22 14:14:07 -0400406 }
407 }
408
Brian Salomon34169692017-08-28 15:32:01 -0400409 void onPrepareDraws(Target* target) override {
Brian Salomond7065e72018-10-12 11:42:02 -0400410 TRACE_EVENT0("skia", TRACE_FUNC);
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400411 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
412 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomonf7232642018-09-19 08:58:08 -0400413 Domain domain = Domain::kNo;
Brian Salomon1d835422019-03-13 16:11:44 -0400414 ColorType colorType = ColorType::kNone;
Brian Salomond7065e72018-10-12 11:42:02 -0400415 int numProxies = 0;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400416 int numTotalQuads = 0;
Brian Salomond7065e72018-10-12 11:42:02 -0400417 auto textureType = fProxies[0].fProxy->textureType();
418 auto config = fProxies[0].fProxy->config();
Brian Salomonae7d7702018-10-14 15:05:45 -0400419 GrAAType aaType = this->aaType();
Brian Salomonf7232642018-09-19 08:58:08 -0400420 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwigc96fc372019-01-08 15:46:15 -0500421 if (op.fQuads.quadType() > quadType) {
422 quadType = op.fQuads.quadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500423 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500424 if (op.fSrcQuads.quadType() > srcQuadType) {
425 // Should only become more general if there are quads to use instead of fSrcRect
426 SkASSERT(op.fSrcQuads.count() > 0);
427 srcQuadType = op.fSrcQuads.quadType();
428 }
Brian Salomonf7232642018-09-19 08:58:08 -0400429 if (op.fDomain) {
430 domain = Domain::kYes;
431 }
Brian Salomon1d835422019-03-13 16:11:44 -0400432 colorType = SkTMax(colorType, static_cast<ColorType>(op.fColorType));
Brian Salomond7065e72018-10-12 11:42:02 -0400433 numProxies += op.fProxyCnt;
434 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
Brian Salomon4b8178f2018-10-12 13:18:27 -0400435 numTotalQuads += op.fProxies[p].fQuadCnt;
Brian Salomond7065e72018-10-12 11:42:02 -0400436 auto* proxy = op.fProxies[p].fProxy;
Robert Phillips12c46292019-04-23 07:36:17 -0400437 if (!proxy->isInstantiated()) {
Brian Salomond7065e72018-10-12 11:42:02 -0400438 return;
439 }
440 SkASSERT(proxy->config() == config);
441 SkASSERT(proxy->textureType() == textureType);
Brian Salomonf7232642018-09-19 08:58:08 -0400442 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400443 if (op.aaType() == GrAAType::kCoverage) {
444 SkASSERT(aaType == GrAAType::kCoverage || aaType == GrAAType::kNone);
445 aaType = GrAAType::kCoverage;
446 }
Brian Salomon34169692017-08-28 15:32:01 -0400447 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400448
Brian Salomon1d835422019-03-13 16:11:44 -0400449 VertexSpec vertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true, domain, aaType,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500450 /* alpha as coverage */ true);
Michael Ludwigc182b942018-11-16 10:27:51 -0500451
Greg Daniel7a82edf2018-12-04 10:54:34 -0500452 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
453 this->filter());
454 GrGpu* gpu = target->resourceProvider()->priv().gpu();
455 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
456 samplerState, fProxies[0].fProxy->backendFormat());
457
Michael Ludwig467994d2018-12-03 14:58:31 +0000458 sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
459 vertexSpec, *target->caps().shaderCaps(),
Greg Daniel7a82edf2018-12-04 10:54:34 -0500460 textureType, config, samplerState, extraSamplerKey,
461 std::move(fTextureColorSpaceXform));
462
Brian Salomonf7232642018-09-19 08:58:08 -0400463 // We'll use a dynamic state array for the GP textures when there are multiple ops.
464 // Otherwise, we use fixed dynamic state to specify the single op's proxy.
465 GrPipeline::DynamicStateArrays* dynamicStateArrays = nullptr;
466 GrPipeline::FixedDynamicState* fixedDynamicState;
Brian Salomond7065e72018-10-12 11:42:02 -0400467 if (numProxies > 1) {
468 dynamicStateArrays = target->allocDynamicStateArrays(numProxies, 1, false);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700469 fixedDynamicState = target->makeFixedDynamicState(0);
Brian Salomonf7232642018-09-19 08:58:08 -0400470 } else {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700471 fixedDynamicState = target->makeFixedDynamicState(1);
Brian Salomond7065e72018-10-12 11:42:02 -0400472 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxies[0].fProxy;
Brian Salomonf7232642018-09-19 08:58:08 -0400473 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400474
Michael Ludwigc182b942018-11-16 10:27:51 -0500475 size_t vertexSize = gp->vertexStride();
Brian Salomon92be2f72018-06-19 14:33:47 -0400476
Brian Salomond7065e72018-10-12 11:42:02 -0400477 GrMesh* meshes = target->allocMeshes(numProxies);
Brian Salomon12d22642019-01-29 14:38:50 -0500478 sk_sp<const GrBuffer> vbuffer;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400479 int vertexOffsetInBuffer = 0;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500480 int numQuadVerticesLeft = numTotalQuads * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400481 int numAllocatedVertices = 0;
482 void* vdata = nullptr;
483
Brian Salomond7065e72018-10-12 11:42:02 -0400484 int m = 0;
Brian Salomonf7232642018-09-19 08:58:08 -0400485 for (const auto& op : ChainRange<TextureOp>(this)) {
Brian Salomond7065e72018-10-12 11:42:02 -0400486 int q = 0;
487 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
488 int quadCnt = op.fProxies[p].fQuadCnt;
489 auto* proxy = op.fProxies[p].fProxy;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500490 int meshVertexCnt = quadCnt * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400491 if (numAllocatedVertices < meshVertexCnt) {
492 vdata = target->makeVertexSpaceAtLeast(
493 vertexSize, meshVertexCnt, numQuadVerticesLeft, &vbuffer,
494 &vertexOffsetInBuffer, &numAllocatedVertices);
495 SkASSERT(numAllocatedVertices <= numQuadVerticesLeft);
496 if (!vdata) {
497 SkDebugf("Could not allocate vertices\n");
498 return;
499 }
Brian Salomonf7232642018-09-19 08:58:08 -0400500 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400501 SkASSERT(numAllocatedVertices >= meshVertexCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400502
Michael Ludwigc182b942018-11-16 10:27:51 -0500503 op.tess(vdata, vertexSpec, proxy, q, quadCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400504
Michael Ludwig93aeba02018-12-21 09:50:31 -0500505 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, &(meshes[m]), vertexSpec,
506 quadCnt)) {
507 SkDebugf("Could not allocate indices");
508 return;
Brian Salomond7065e72018-10-12 11:42:02 -0400509 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400510 meshes[m].setVertexData(vbuffer, vertexOffsetInBuffer);
Brian Salomond7065e72018-10-12 11:42:02 -0400511 if (dynamicStateArrays) {
512 dynamicStateArrays->fPrimitiveProcessorTextures[m] = proxy;
513 }
514 ++m;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400515 numAllocatedVertices -= meshVertexCnt;
516 numQuadVerticesLeft -= meshVertexCnt;
517 vertexOffsetInBuffer += meshVertexCnt;
518 vdata = reinterpret_cast<char*>(vdata) + vertexSize * meshVertexCnt;
Brian Salomond7065e72018-10-12 11:42:02 -0400519 q += quadCnt;
Brian Salomonf7232642018-09-19 08:58:08 -0400520 }
Brian Salomon34169692017-08-28 15:32:01 -0400521 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400522 SkASSERT(!numQuadVerticesLeft);
523 SkASSERT(!numAllocatedVertices);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700524 target->recordDraw(
525 std::move(gp), meshes, numProxies, fixedDynamicState, dynamicStateArrays);
526 }
527
528 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
529 auto pipelineFlags = (GrAAType::kMSAA == this->aaType())
Chris Daltonbaa1b352019-04-03 12:03:00 -0600530 ? GrPipeline::InputFlags::kHWAntialias
531 : GrPipeline::InputFlags::kNone;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700532 flushState->executeDrawsAndUploadsForMeshDrawOp(
533 this, chainBounds, GrProcessorSet::MakeEmptySet(), pipelineFlags);
Brian Salomon34169692017-08-28 15:32:01 -0400534 }
535
Brian Salomonf7232642018-09-19 08:58:08 -0400536 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomond7065e72018-10-12 11:42:02 -0400537 TRACE_EVENT0("skia", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400538 const auto* that = t->cast<TextureOp>();
Michael Ludwig2929f512019-04-19 13:05:56 -0400539 if (fDomain != that->fDomain) {
540 // It is technically possible to combine operations across domain modes, but performance
541 // testing suggests it's better to make more draw calls where some take advantage of
542 // the more optimal shader path without coordinate clamping.
543 return CombineResult::kCannotCombine;
544 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400545 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
546 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000547 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400548 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400549 bool upgradeToCoverageAAOnMerge = false;
Brian Salomon485b8c62018-01-12 15:11:06 -0500550 if (this->aaType() != that->aaType()) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400551 if (!((this->aaType() == GrAAType::kCoverage && that->aaType() == GrAAType::kNone) ||
552 (that->aaType() == GrAAType::kCoverage && this->aaType() == GrAAType::kNone))) {
553 return CombineResult::kCannotCombine;
554 }
555 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500556 }
Brian Salomonf7232642018-09-19 08:58:08 -0400557 if (fFilter != that->fFilter) {
558 return CombineResult::kCannotCombine;
559 }
Brian Salomond7065e72018-10-12 11:42:02 -0400560 auto thisProxy = fProxies[0].fProxy;
561 auto thatProxy = that->fProxies[0].fProxy;
562 if (fProxyCnt > 1 || that->fProxyCnt > 1 ||
Brian Salomon588cec72018-11-14 13:56:37 -0500563 thisProxy->uniqueID() != thatProxy->uniqueID()) {
564 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -0500565 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Brian Salomonf7232642018-09-19 08:58:08 -0400566 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
567 return CombineResult::kMayChain;
568 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000569 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400570 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500571
Brian Salomonb80ffee2018-05-23 16:39:39 -0400572 fDomain |= that->fDomain;
Brian Salomon1d835422019-03-13 16:11:44 -0400573 fColorType = SkTMax(fColorType, that->fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -0400574 if (upgradeToCoverageAAOnMerge) {
575 fAAType = static_cast<unsigned>(GrAAType::kCoverage);
576 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500577
578 // Concatenate quad lists together, updating the fSrcQuadIndex in the appended quads
579 // to account for the new starting index in fSrcQuads
580 int srcQuadOffset = fSrcQuads.count();
581 int oldQuadCount = fQuads.count();
582
583 fSrcQuads.concat(that->fSrcQuads);
584 fQuads.concat(that->fQuads);
585 fProxies[0].fQuadCnt += that->fQuads.count();
586
587 if (that->fSrcQuads.count() > 0) {
588 // Some of the concatenated quads pointed to fSrcQuads, so adjust the indices for the
589 // newly appended quads
590 for (int i = oldQuadCount; i < fQuads.count(); ++i) {
591 if (fQuads.metadata(i).fSrcQuadIndex >= 0) {
592 fQuads.metadata(i).fSrcQuadIndex += srcQuadOffset;
593 }
594 }
595 }
596
597 // Confirm all tracked state makes sense when in debug builds
598#ifdef SK_DEBUG
599 SkASSERT(fSrcQuads.count() <= fQuads.count());
600 for (int i = 0; i < fQuads.count(); ++i) {
601 int srcIndex = fQuads.metadata(i).fSrcQuadIndex;
602 if (srcIndex >= 0) {
603 // Make sure it points to a valid index, in the right region of the list
604 SkASSERT(srcIndex < fSrcQuads.count());
605 SkASSERT((i < oldQuadCount && srcIndex < srcQuadOffset) ||
606 (i >= oldQuadCount && srcIndex >= srcQuadOffset));
607 }
608 }
609#endif
Brian Salomon7eae3e02018-08-07 14:02:38 +0000610 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -0400611 }
612
Brian Salomon485b8c62018-01-12 15:11:06 -0500613 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomon0087c832018-10-15 14:48:20 -0400614 GrSamplerState::Filter filter() const { return static_cast<GrSamplerState::Filter>(fFilter); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500615
Michael Ludwigc96fc372019-01-08 15:46:15 -0500616 struct ColorDomainAndAA {
617 // Special constructor to convert enums into the packed bits, which should not delete
618 // the implicit move constructor (but it does require us to declare an empty ctor for
619 // use with the GrTQuadList).
Michael Ludwig009b92e2019-02-15 16:03:53 -0500620 ColorDomainAndAA(const SkPMColor4f& color, const SkRect& srcRect, int srcQuadIndex,
Michael Ludwigc96fc372019-01-08 15:46:15 -0500621 Domain hasDomain, GrQuadAAFlags aaFlags)
622 : fColor(color)
623 , fSrcRect(srcRect)
Michael Ludwig009b92e2019-02-15 16:03:53 -0500624 , fSrcQuadIndex(srcQuadIndex)
Michael Ludwigc96fc372019-01-08 15:46:15 -0500625 , fHasDomain(static_cast<unsigned>(hasDomain))
Brian Salomon2213ee92018-10-02 10:44:21 -0400626 , fAAFlags(static_cast<unsigned>(aaFlags)) {
Michael Ludwigc96fc372019-01-08 15:46:15 -0500627 SkASSERT(fHasDomain == static_cast<unsigned>(hasDomain));
Brian Salomon2213ee92018-10-02 10:44:21 -0400628 SkASSERT(fAAFlags == static_cast<unsigned>(aaFlags));
629 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500630 ColorDomainAndAA() = default;
Michael Ludwig23d8943e2019-01-04 14:51:40 +0000631
Michael Ludwig23d8943e2019-01-04 14:51:40 +0000632 SkPMColor4f fColor;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500633 // Even if fSrcQuadIndex provides source coords, use fSrcRect for domain constraint
Michael Ludwigc96fc372019-01-08 15:46:15 -0500634 SkRect fSrcRect;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500635 // If >= 0, use to access fSrcQuads instead of fSrcRect for the source coordinates
636 int fSrcQuadIndex;
Michael Ludwig23d8943e2019-01-04 14:51:40 +0000637 unsigned fHasDomain : 1;
638 unsigned fAAFlags : 4;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500639
640 Domain domain() const { return Domain(fHasDomain); }
641 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
Brian Salomon34169692017-08-28 15:32:01 -0400642 };
Brian Salomond7065e72018-10-12 11:42:02 -0400643 struct Proxy {
644 GrTextureProxy* fProxy;
645 int fQuadCnt;
646 };
Michael Ludwigc96fc372019-01-08 15:46:15 -0500647 GrTQuadList<ColorDomainAndAA> fQuads;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500648 // The majority of texture ops will not track a complete src quad so this is indexed separately
649 // and may be of different size to fQuads.
650 GrQuadList fSrcQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -0400651 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Brian Salomon0087c832018-10-15 14:48:20 -0400652 unsigned fFilter : 2;
Brian Salomon485b8c62018-01-12 15:11:06 -0500653 unsigned fAAType : 2;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400654 unsigned fDomain : 1;
Brian Salomon1d835422019-03-13 16:11:44 -0400655 unsigned fColorType : 2;
656 GR_STATIC_ASSERT(GrQuadPerEdgeAA::kColorTypeCount <= 4);
Brian Salomon34169692017-08-28 15:32:01 -0400657 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500658 unsigned fFinalized : 1;
Robert Phillips5f78adf2019-04-22 12:41:39 -0400659 unsigned fProxyCnt : 32 - 8;
Brian Salomond7065e72018-10-12 11:42:02 -0400660 Proxy fProxies[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -0400661
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400662 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type does not fit in 2 bits");
Michael Ludwigf995c052018-11-26 15:24:29 -0500663
Brian Salomon34169692017-08-28 15:32:01 -0400664 typedef GrMeshDrawOp INHERITED;
665};
666
667} // anonymous namespace
668
669namespace GrTextureOp {
670
Robert Phillipsb97da532019-02-12 15:24:12 -0500671std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400672 sk_sp<GrTextureProxy> proxy,
673 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500674 const SkPMColor4f& color,
Robert Phillips7c525e62018-06-12 10:11:12 -0400675 const SkRect& srcRect,
676 const SkRect& dstRect,
677 GrAAType aaType,
Brian Salomon2213ee92018-10-02 10:44:21 -0400678 GrQuadAAFlags aaFlags,
Robert Phillips7c525e62018-06-12 10:11:12 -0400679 SkCanvas::SrcRectConstraint constraint,
680 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500681 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400682 return TextureOp::Make(context, std::move(proxy), filter, color, srcRect, dstRect, aaType,
Brian Osman3d139a42018-11-19 10:42:10 -0500683 aaFlags, constraint, viewMatrix, std::move(textureColorSpaceXform));
Brian Salomon34169692017-08-28 15:32:01 -0400684}
685
Michael Ludwig009b92e2019-02-15 16:03:53 -0500686std::unique_ptr<GrDrawOp> MakeQuad(GrRecordingContext* context,
687 sk_sp<GrTextureProxy> proxy,
688 GrSamplerState::Filter filter,
689 const SkPMColor4f& color,
690 const SkPoint srcQuad[4],
691 const SkPoint dstQuad[4],
692 GrAAType aaType,
693 GrQuadAAFlags aaFlags,
694 const SkRect* domain,
695 const SkMatrix& viewMatrix,
696 sk_sp<GrColorSpaceXform> textureXform) {
697 return TextureOp::Make(context, std::move(proxy), filter, color, srcQuad, dstQuad, aaType,
698 aaFlags, domain, viewMatrix, std::move(textureXform));
699}
700
701std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
702 const GrRenderTargetContext::TextureSetEntry set[],
703 int cnt,
704 GrSamplerState::Filter filter,
705 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400706 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500707 const SkMatrix& viewMatrix,
708 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig31ba7182019-04-03 10:38:06 -0400709 return TextureOp::Make(context, set, cnt, filter, aaType, constraint, viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500710 std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400711}
712
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500713bool GetFilterHasEffect(const SkMatrix& viewMatrix, const SkRect& srcRect, const SkRect& dstRect) {
714 // Hypothetically we could disable bilerp filtering when flipping or rotating 90 degrees, but
715 // that makes the math harder and we don't want to increase the overhead of the checks
716 if (!viewMatrix.isScaleTranslate() ||
717 viewMatrix.getScaleX() < 0.0f || viewMatrix.getScaleY() < 0.0f) {
718 return true;
719 }
720
721 // Given the matrix conditions ensured above, this computes the device space coordinates for
722 // the top left corner of dstRect and its size.
723 SkScalar dw = viewMatrix.getScaleX() * dstRect.width();
724 SkScalar dh = viewMatrix.getScaleY() * dstRect.height();
725 SkScalar dl = viewMatrix.getScaleX() * dstRect.fLeft + viewMatrix.getTranslateX();
726 SkScalar dt = viewMatrix.getScaleY() * dstRect.fTop + viewMatrix.getTranslateY();
727
728 // Disable filtering when there is no scaling of the src rect and the src rect and dst rect
729 // align fractionally. If we allow inverted src rects this logic needs to consider that.
730 SkASSERT(srcRect.isSorted());
731 return dw != srcRect.width() || dh != srcRect.height() ||
732 SkScalarFraction(dl) != SkScalarFraction(srcRect.fLeft) ||
733 SkScalarFraction(dt) != SkScalarFraction(srcRect.fTop);
734}
735
Brian Salomon34169692017-08-28 15:32:01 -0400736} // namespace GrTextureOp
737
738#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500739#include "include/private/GrRecordingContext.h"
740#include "src/gpu/GrProxyProvider.h"
741#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -0400742
743GR_DRAW_OP_TEST_DEFINE(TextureOp) {
744 GrSurfaceDesc desc;
745 desc.fConfig = kRGBA_8888_GrPixelConfig;
746 desc.fHeight = random->nextULessThan(90) + 10;
747 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500748 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel09c94002018-06-08 22:11:51 +0000749 GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
750 SkBackingFit fit = SkBackingFit::kExact;
751 if (mipMapped == GrMipMapped::kNo) {
752 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
753 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500754
Greg Daniel4065d452018-11-16 15:43:41 -0500755 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500756 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500757
Robert Phillips9da87e02019-02-04 13:26:26 -0500758 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Greg Daniel4065d452018-11-16 15:43:41 -0500759 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format, desc, origin, mipMapped, fit,
Greg Daniel09c94002018-06-08 22:11:51 +0000760 SkBudgeted::kNo,
761 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500762
Brian Salomon34169692017-08-28 15:32:01 -0400763 SkRect rect = GrTest::TestRect(random);
764 SkRect srcRect;
765 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
766 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
767 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
768 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
769 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -0500770 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400771 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
772 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +0000773 while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
774 filter = (GrSamplerState::Filter)random->nextULessThan(
775 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
776 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400777 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -0500778 GrAAType aaType = GrAAType::kNone;
779 if (random->nextBool()) {
780 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
781 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400782 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
783 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
784 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
785 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
786 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400787 auto constraint = random->nextBool() ? SkCanvas::kStrict_SrcRectConstraint
788 : SkCanvas::kFast_SrcRectConstraint;
Robert Phillips7c525e62018-06-12 10:11:12 -0400789 return GrTextureOp::Make(context, std::move(proxy), filter, color, srcRect, rect, aaType,
Brian Osman3d139a42018-11-19 10:42:10 -0500790 aaFlags, constraint, viewMatrix, std::move(texXform));
Brian Salomon34169692017-08-28 15:32:01 -0400791}
792
793#endif