blob: a3a11531f28e506eddafc5c2a8f8b0be8acd5f6b [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
8#include "GrTextureOp.h"
Brian Salomond7065e72018-10-12 11:42:02 -04009#include <new>
Brian Salomon34169692017-08-28 15:32:01 -040010#include "GrAppliedClip.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040011#include "GrCaps.h"
Brian Salomon34169692017-08-28 15:32:01 -040012#include "GrDrawOpTest.h"
13#include "GrGeometryProcessor.h"
Greg Daniel7a82edf2018-12-04 10:54:34 -050014#include "GrGpu.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040015#include "GrMemoryPool.h"
Brian Salomon34169692017-08-28 15:32:01 -040016#include "GrMeshDrawOp.h"
17#include "GrOpFlushState.h"
18#include "GrQuad.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040019#include "GrQuadPerEdgeAA.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050020#include "GrRecordingContext.h"
21#include "GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -040022#include "GrResourceProvider.h"
Greg Daniel7a82edf2018-12-04 10:54:34 -050023#include "GrResourceProviderPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -040024#include "GrShaderCaps.h"
25#include "GrTexture.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040026#include "GrTexturePriv.h"
Brian Salomon34169692017-08-28 15:32:01 -040027#include "GrTextureProxy.h"
28#include "SkGr.h"
Brian Salomon336ce7b2017-09-08 08:23:58 -040029#include "SkMathPriv.h"
Brian Salomona33b67c2018-05-17 10:42:14 -040030#include "SkMatrixPriv.h"
Brian Salomonb5ef1f92018-01-11 11:46:21 -050031#include "SkPoint.h"
32#include "SkPoint3.h"
Brian Salomond7065e72018-10-12 11:42:02 -040033#include "SkRectPriv.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040034#include "SkTo.h"
Brian Salomon34169692017-08-28 15:32:01 -040035#include "glsl/GrGLSLVarying.h"
36
37namespace {
38
Michael Ludwig460eb5e2018-10-29 11:09:29 -040039using Domain = GrQuadPerEdgeAA::Domain;
Michael Ludwigc182b942018-11-16 10:27:51 -050040using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050041using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040042
Brian Salomon246bc3d2018-12-06 15:33:02 -050043// if normalizing the domain then pass 1/width, 1/height, 1 for iw, ih, h. Otherwise pass
44// 1, 1, and height.
45static SkRect compute_domain(Domain domain, GrSamplerState::Filter filter, GrSurfaceOrigin origin,
46 const SkRect& srcRect, float iw, float ih, float h) {
47 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Michael Ludwig460eb5e2018-10-29 11:09:29 -040048 if (domain == Domain::kNo) {
49 // Either the quad has no domain constraint and is batched with a domain constrained op
50 // (in which case we want a domain that doesn't restrict normalized tex coords), or the
51 // entire op doesn't use the domain, in which case the returned value is ignored.
52 return kLargeRect;
53 }
54
55 auto ltrb = Sk4f::Load(&srcRect);
56 if (filter == GrSamplerState::Filter::kBilerp) {
57 auto rblt = SkNx_shuffle<2, 3, 0, 1>(ltrb);
58 auto whwh = (rblt - ltrb).abs();
59 auto c = (rblt + ltrb) * 0.5f;
60 static const Sk4f kOffsets = {0.5f, 0.5f, -0.5f, -0.5f};
61 ltrb = (whwh < 1.f).thenElse(c, ltrb + kOffsets);
62 }
63 ltrb *= Sk4f(iw, ih, iw, ih);
64 if (origin == kBottomLeft_GrSurfaceOrigin) {
65 static const Sk4f kMul = {1.f, -1.f, 1.f, -1.f};
Brian Salomon246bc3d2018-12-06 15:33:02 -050066 const Sk4f kAdd = {0.f, h, 0.f, h};
Michael Ludwig460eb5e2018-10-29 11:09:29 -040067 ltrb = SkNx_shuffle<0, 3, 2, 1>(kMul * ltrb + kAdd);
68 }
69
70 SkRect domainRect;
71 ltrb.store(&domainRect);
72 return domainRect;
73}
74
Brian Salomon246bc3d2018-12-06 15:33:02 -050075// If normalizing the src quad then pass 1/width, 1/height, 1 for iw, ih, h. Otherwise pass
76// 1, 1, and height.
Michael Ludwig009b92e2019-02-15 16:03:53 -050077static GrPerspQuad compute_src_quad_from_rect(GrSurfaceOrigin origin, const SkRect& srcRect,
78 float iw, float ih, float h) {
Michael Ludwig460eb5e2018-10-29 11:09:29 -040079 // Convert the pixel-space src rectangle into normalized texture coordinates
80 SkRect texRect = {
81 iw * srcRect.fLeft,
82 ih * srcRect.fTop,
83 iw * srcRect.fRight,
84 ih * srcRect.fBottom
85 };
86 if (origin == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon246bc3d2018-12-06 15:33:02 -050087 texRect.fTop = h - texRect.fTop;
88 texRect.fBottom = h - texRect.fBottom;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040089 }
Michael Ludwige9c57d32019-02-13 13:39:39 -050090 return GrPerspQuad(texRect);
Michael Ludwig460eb5e2018-10-29 11:09:29 -040091}
Michael Ludwig009b92e2019-02-15 16:03:53 -050092// Normalizes logical src coords and corrects for origin
93static GrPerspQuad compute_src_quad(GrSurfaceOrigin origin, const GrPerspQuad& srcQuad,
94 float iw, float ih, float h) {
95 // The src quad should not have any perspective
96 SkASSERT(!srcQuad.hasPerspective());
97 Sk4f xs = srcQuad.x4f() * iw;
98 Sk4f ys = srcQuad.y4f() * ih;
99 if (origin == kBottomLeft_GrSurfaceOrigin) {
100 ys = h - ys;
101 }
102 return GrPerspQuad(xs, ys);
103}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400104
Brian Salomon34169692017-08-28 15:32:01 -0400105/**
106 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
107 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
108 */
109class TextureOp final : public GrMeshDrawOp {
110public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500111 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400112 sk_sp<GrTextureProxy> proxy,
113 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500114 const SkPMColor4f& color,
Robert Phillips7c525e62018-06-12 10:11:12 -0400115 const SkRect& srcRect,
116 const SkRect& dstRect,
117 GrAAType aaType,
Brian Salomon2213ee92018-10-02 10:44:21 -0400118 GrQuadAAFlags aaFlags,
Robert Phillips7c525e62018-06-12 10:11:12 -0400119 SkCanvas::SrcRectConstraint constraint,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400120 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500121 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500122 GrPerspQuad dstQuad = GrPerspQuad::MakeFromRect(dstRect, viewMatrix);
123 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Robert Phillipsc994a932018-06-19 13:09:54 -0400124
Michael Ludwig009b92e2019-02-15 16:03:53 -0500125 if (dstQuadType == GrQuadType::kRect) {
126 // 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 Ludwig009b92e2019-02-15 16:03:53 -0500137 std::move(proxy), filter, color, dstQuad, dstQuadType, srcRect, constraint,
138 nullptr, GrQuadType::kRect, aaType, aaFlags, std::move(textureColorSpaceXform));
139 }
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) {
151 GrPerspQuad grDstQuad = GrPerspQuad::MakeFromSkQuad(dstQuad, viewMatrix);
152 GrQuadType dstQuadType = viewMatrix.hasPerspective() ? GrQuadType::kPerspective
153 : GrQuadType::kStandard;
154 GrPerspQuad grSrcQuad = GrPerspQuad::MakeFromSkQuad(srcQuad, SkMatrix::I());
155
156 // If constraint remains fast, the value in srcRect will be ignored since srcQuads provides
157 // the local coordinates and a domain won't be used.
158 SkRect srcRect = SkRect::MakeEmpty();
159 SkCanvas::SrcRectConstraint constraint = SkCanvas::kFast_SrcRectConstraint;
160 if (domain) {
161 srcRect = *domain;
162 constraint = SkCanvas::kStrict_SrcRectConstraint;
163 }
164
165 GrOpMemoryPool* pool = context->priv().opMemoryPool();
166 // Pass domain as srcRect if provided, but send srcQuad as a GrPerspQuad for local coords
167 return pool->allocate<TextureOp>(
168 std::move(proxy), filter, color, grDstQuad, dstQuadType, srcRect, constraint,
169 &grSrcQuad, GrQuadType::kStandard, aaType, aaFlags,
170 std::move(textureColorSpaceXform));
Brian Salomon34169692017-08-28 15:32:01 -0400171 }
Robert Phillipsb97da532019-02-12 15:24:12 -0500172 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Brian Salomond7065e72018-10-12 11:42:02 -0400173 const GrRenderTargetContext::TextureSetEntry set[],
Brian Salomond003d222018-11-26 13:25:05 -0500174 int cnt, GrSamplerState::Filter filter, GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400175 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500176 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500177 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400178 size_t size = sizeof(TextureOp) + sizeof(Proxy) * (cnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500179 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400180 void* mem = pool->allocate(size);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400181 return std::unique_ptr<GrDrawOp>(new (mem) TextureOp(
182 set, cnt, filter, aaType, constraint, viewMatrix,
183 std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400184 }
Brian Salomon34169692017-08-28 15:32:01 -0400185
Brian Salomon336ce7b2017-09-08 08:23:58 -0400186 ~TextureOp() override {
Brian Salomond7065e72018-10-12 11:42:02 -0400187 for (unsigned p = 0; p < fProxyCnt; ++p) {
188 if (fFinalized) {
189 fProxies[p].fProxy->completedRead();
190 } else {
191 fProxies[p].fProxy->unref();
192 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400193 }
194 }
Brian Salomon34169692017-08-28 15:32:01 -0400195
196 const char* name() const override { return "TextureOp"; }
197
Brian Salomon7d94bb52018-10-12 14:37:19 -0400198 void visitProxies(const VisitProxyFunc& func, VisitorType visitor) const override {
199 if (visitor == VisitorType::kAllocatorGather && fCanSkipAllocatorGather) {
200 return;
201 }
Brian Salomond7065e72018-10-12 11:42:02 -0400202 for (unsigned p = 0; p < fProxyCnt; ++p) {
203 func(fProxies[p].fProxy);
204 }
205 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400206
Brian Osman9a390ac2018-11-12 09:47:48 -0500207#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400208 SkString dumpInfo() const override {
209 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400210 str.appendf("# draws: %d\n", fQuads.count());
211 int q = 0;
212 for (unsigned p = 0; p < fProxyCnt; ++p) {
213 str.appendf("Proxy ID: %d, Filter: %d\n", fProxies[p].fProxy->uniqueID().asUInt(),
214 static_cast<int>(fFilter));
215 for (int i = 0; i < fProxies[p].fQuadCnt; ++i, ++q) {
Michael Ludwigc96fc372019-01-08 15:46:15 -0500216 GrPerspQuad quad = fQuads[q];
217 const ColorDomainAndAA& info = fQuads.metadata(i);
Brian Salomond7065e72018-10-12 11:42:02 -0400218 str.appendf(
219 "%d: Color: 0x%08x, TexRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f] "
220 "Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
Michael Ludwigc96fc372019-01-08 15:46:15 -0500221 i, info.fColor.toBytes_RGBA(), info.fSrcRect.fLeft, info.fSrcRect.fTop,
222 info.fSrcRect.fRight, info.fSrcRect.fBottom, quad.point(0).fX,
223 quad.point(0).fY, quad.point(1).fX, quad.point(1).fY,
224 quad.point(2).fX, quad.point(2).fY, quad.point(3).fX,
225 quad.point(3).fY);
Brian Salomond7065e72018-10-12 11:42:02 -0400226 }
Brian Salomon34169692017-08-28 15:32:01 -0400227 }
228 str += INHERITED::dumpInfo();
229 return str;
230 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500231#endif
Brian Salomon34169692017-08-28 15:32:01 -0400232
Brian Osman5ced0bf2019-03-15 10:15:29 -0400233 GrProcessorSet::Analysis finalize(
Brian Osman8fa7ab42019-03-18 10:22:42 -0400234 const GrCaps& caps, const GrAppliedClip*, GrFSAAType, GrClampType clampType) override {
Brian Salomon34169692017-08-28 15:32:01 -0400235 SkASSERT(!fFinalized);
236 fFinalized = true;
Brian Salomond7065e72018-10-12 11:42:02 -0400237 for (unsigned p = 0; p < fProxyCnt; ++p) {
238 fProxies[p].fProxy->addPendingRead();
239 fProxies[p].fProxy->unref();
240 }
Brian Osman8fa7ab42019-03-18 10:22:42 -0400241 fColorType = static_cast<unsigned>(ColorType::kNone);
242 for (int q = 0; q < fQuads.count(); ++q) {
243 const ColorDomainAndAA& info = fQuads.metadata(q);
244 auto colorType = GrQuadPerEdgeAA::MinColorType(info.fColor, clampType, caps);
245 fColorType = SkTMax(fColorType, static_cast<unsigned>(colorType));
246 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700247 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400248 }
249
Brian Salomon485b8c62018-01-12 15:11:06 -0500250 FixedFunctionFlags fixedFunctionFlags() const override {
251 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
252 : FixedFunctionFlags::kNone;
253 }
Brian Salomon34169692017-08-28 15:32:01 -0400254
255 DEFINE_OP_CLASS_ID
256
257private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400258 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500259
Michael Ludwig009b92e2019-02-15 16:03:53 -0500260 // dstQuad and dstQuadType should be the geometry transformed by the view matrix.
261 // srcRect represents original src rect and will be used as the domain when constraint is strict
262 // If srcQuad is provided, it will be used for the local coords instead of srcRect, although
263 // srcRect will still specify the domain constraint if needed.
Brian Osman3d139a42018-11-19 10:42:10 -0500264 TextureOp(sk_sp<GrTextureProxy> proxy, GrSamplerState::Filter filter, const SkPMColor4f& color,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500265 const GrPerspQuad& dstQuad, GrQuadType dstQuadType,
266 const SkRect& srcRect, SkCanvas::SrcRectConstraint constraint,
267 const GrPerspQuad* srcQuad, GrQuadType srcQuadType, GrAAType aaType,
268 GrQuadAAFlags aaFlags, sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomon34169692017-08-28 15:32:01 -0400269 : INHERITED(ClassID())
Brian Osman3ebd3542018-07-30 14:36:53 -0400270 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Brian Salomon0087c832018-10-15 14:48:20 -0400271 , fFilter(static_cast<unsigned>(filter))
Brian Osman2b23c4b2018-06-01 12:25:08 -0400272 , fFinalized(0) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400273 // Clean up disparities between the overall aa type and edge configuration and apply
274 // optimizations based on the rect and matrix when appropriate
Michael Ludwig009b92e2019-02-15 16:03:53 -0500275 GrResolveAATypeForQuad(aaType, aaFlags, dstQuad, dstQuadType, &aaType, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400276 fAAType = static_cast<unsigned>(aaType);
277
Brian Salomonf1709042018-10-03 11:57:00 -0400278 // We expect our caller to have already caught this optimization.
Brian Salomond7065e72018-10-12 11:42:02 -0400279 SkASSERT(!srcRect.contains(proxy->getWorstCaseBoundsRect()) ||
Brian Salomonf1709042018-10-03 11:57:00 -0400280 constraint == SkCanvas::kFast_SrcRectConstraint);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500281
Brian Salomonf09abc52018-10-03 15:59:04 -0400282 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
283 // If we don't have (or determined we don't need) coverage AA then we can skip using a
284 // domain.
285 if (constraint == SkCanvas::kStrict_SrcRectConstraint &&
Brian Salomon0087c832018-10-15 14:48:20 -0400286 this->filter() == GrSamplerState::Filter::kNearest &&
Michael Ludwig6bee7762018-10-19 09:50:36 -0400287 aaType != GrAAType::kCoverage) {
Brian Salomonf09abc52018-10-03 15:59:04 -0400288 constraint = SkCanvas::kFast_SrcRectConstraint;
289 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500290
291 Domain domain = constraint == SkCanvas::kStrict_SrcRectConstraint ? Domain::kYes
292 : Domain::kNo;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500293 // Initially, if srcQuad is provided it will always be at index 0 of fSrcQuads
294 fQuads.push_back(dstQuad, dstQuadType, {color, srcRect, srcQuad ? 0 : -1, domain, aaFlags});
295 if (srcQuad) {
296 fSrcQuads.push_back(*srcQuad, srcQuadType);
297 }
Brian Salomond7065e72018-10-12 11:42:02 -0400298 fProxyCnt = 1;
299 fProxies[0] = {proxy.release(), 1};
Michael Ludwig009b92e2019-02-15 16:03:53 -0500300 auto bounds = dstQuad.bounds(dstQuadType);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400301 this->setBounds(bounds, HasAABloat(aaType == GrAAType::kCoverage), IsZeroArea::kNo);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500302 fDomain = static_cast<unsigned>(domain);
Brian Salomon7d94bb52018-10-12 14:37:19 -0400303 fCanSkipAllocatorGather =
304 static_cast<unsigned>(fProxies[0].fProxy->canSkipResourceAllocator());
Brian Salomond7065e72018-10-12 11:42:02 -0400305 }
306 TextureOp(const GrRenderTargetContext::TextureSetEntry set[], int cnt,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400307 GrSamplerState::Filter filter, GrAAType aaType,
308 SkCanvas::SrcRectConstraint constraint, const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500309 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400310 : INHERITED(ClassID())
311 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Brian Salomon0087c832018-10-15 14:48:20 -0400312 , fFilter(static_cast<unsigned>(filter))
Brian Salomond7065e72018-10-12 11:42:02 -0400313 , fFinalized(0) {
Brian Salomond7065e72018-10-12 11:42:02 -0400314 fProxyCnt = SkToUInt(cnt);
315 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig6bee7762018-10-19 09:50:36 -0400316 GrAAType overallAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon0087c832018-10-15 14:48:20 -0400317 bool mustFilter = false;
Brian Salomon7d94bb52018-10-12 14:37:19 -0400318 fCanSkipAllocatorGather = static_cast<unsigned>(true);
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500319 // Most dst rects are transformed by the same view matrix, so their quad types start
320 // identical, unless an entry provides a dstClip or additional transform that changes it.
321 // The quad list will automatically adapt to that.
322 fQuads.reserve(cnt, GrQuadTypeForTransformedRect(viewMatrix));
Brian Salomon1d835422019-03-13 16:11:44 -0400323 bool allOpaque = true;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400324 Domain netDomain = Domain::kNo;
Brian Salomond7065e72018-10-12 11:42:02 -0400325 for (unsigned p = 0; p < fProxyCnt; ++p) {
326 fProxies[p].fProxy = SkRef(set[p].fProxy.get());
327 fProxies[p].fQuadCnt = 1;
328 SkASSERT(fProxies[p].fProxy->textureType() == fProxies[0].fProxy->textureType());
329 SkASSERT(fProxies[p].fProxy->config() == fProxies[0].fProxy->config());
Brian Salomon7d94bb52018-10-12 14:37:19 -0400330 if (!fProxies[p].fProxy->canSkipResourceAllocator()) {
331 fCanSkipAllocatorGather = static_cast<unsigned>(false);
332 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500333
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500334 SkMatrix ctm = viewMatrix;
335 if (set[p].fPreViewMatrix) {
336 ctm.preConcat(*set[p].fPreViewMatrix);
337 }
338
Michael Ludwigce62dec2019-02-19 11:48:46 -0500339 // Use dstRect unless dstClip is provided, which is assumed to be a quad
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500340 auto quad = set[p].fDstClipQuad == nullptr ?
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500341 GrPerspQuad::MakeFromRect(set[p].fDstRect, ctm) :
342 GrPerspQuad::MakeFromSkQuad(set[p].fDstClipQuad, ctm);
343 GrQuadType quadType = GrQuadTypeForTransformedRect(ctm);
344 if (set[p].fDstClipQuad && quadType != GrQuadType::kPerspective) {
Michael Ludwigce62dec2019-02-19 11:48:46 -0500345 quadType = GrQuadType::kStandard;
346 }
347
Michael Ludwigc96fc372019-01-08 15:46:15 -0500348 bounds.joinPossiblyEmptyRect(quad.bounds(quadType));
Michael Ludwig6bee7762018-10-19 09:50:36 -0400349 GrQuadAAFlags aaFlags;
350 // Don't update the overall aaType, might be inappropriate for some of the quads
351 GrAAType aaForQuad;
352 GrResolveAATypeForQuad(aaType, set[p].fAAFlags, quad, quadType, &aaForQuad, &aaFlags);
353 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
354 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
355 if (overallAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
356 overallAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400357 }
Brian Salomon0087c832018-10-15 14:48:20 -0400358 if (!mustFilter && this->filter() != GrSamplerState::Filter::kNearest) {
Michael Ludwigc182b942018-11-16 10:27:51 -0500359 mustFilter = quadType != GrQuadType::kRect ||
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500360 GrTextureOp::GetFilterHasEffect(ctm, set[p].fSrcRect,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500361 set[p].fDstRect);
Brian Salomon0087c832018-10-15 14:48:20 -0400362 }
Michael Ludwig31ba7182019-04-03 10:38:06 -0400363 Domain domainForQuad = Domain::kNo;
364 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
365 // Check (briefly) if the strict constraint is needed for this set entry
366 if (!set[p].fSrcRect.contains(fProxies[p].fProxy->getWorstCaseBoundsRect()) &&
367 (mustFilter || aaForQuad == GrAAType::kCoverage)) {
368 // Can't rely on hardware clamping and the draw will access outer texels
369 // for AA and/or bilerp
370 netDomain = Domain::kYes;
371 domainForQuad = Domain::kYes;
372 }
373 }
Brian Salomond003d222018-11-26 13:25:05 -0500374 float alpha = SkTPin(set[p].fAlpha, 0.f, 1.f);
Brian Salomon1d835422019-03-13 16:11:44 -0400375 allOpaque &= (1.f == alpha);
Brian Salomond003d222018-11-26 13:25:05 -0500376 SkPMColor4f color{alpha, alpha, alpha, alpha};
Michael Ludwigce62dec2019-02-19 11:48:46 -0500377 int srcQuadIndex = -1;
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500378 if (set[p].fDstClipQuad) {
Michael Ludwigce62dec2019-02-19 11:48:46 -0500379 // Derive new source coordinates that match dstClip's relative locations in dstRect,
380 // but with respect to srcRect
381 SkPoint srcQuad[4];
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500382 GrMapRectPoints(set[p].fDstRect, set[p].fSrcRect, set[p].fDstClipQuad, srcQuad, 4);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500383 fSrcQuads.push_back(GrPerspQuad::MakeFromSkQuad(srcQuad, SkMatrix::I()),
384 GrQuadType::kStandard);
385 srcQuadIndex = fSrcQuads.count() - 1;
386 }
387 fQuads.push_back(quad, quadType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400388 {color, set[p].fSrcRect, srcQuadIndex, domainForQuad, aaFlags});
Brian Salomond7065e72018-10-12 11:42:02 -0400389 }
Michael Ludwig6bee7762018-10-19 09:50:36 -0400390 fAAType = static_cast<unsigned>(overallAAType);
Brian Salomon0087c832018-10-15 14:48:20 -0400391 if (!mustFilter) {
392 fFilter = static_cast<unsigned>(GrSamplerState::Filter::kNearest);
393 }
Brian Salomond7065e72018-10-12 11:42:02 -0400394 this->setBounds(bounds, HasAABloat(this->aaType() == GrAAType::kCoverage), IsZeroArea::kNo);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400395 fDomain = static_cast<unsigned>(netDomain);
Brian Salomon34169692017-08-28 15:32:01 -0400396 }
397
Brian Salomon574d6162018-11-19 16:57:25 -0500398 void tess(void* v, const VertexSpec& spec, const GrTextureProxy* proxy, int start,
399 int cnt) const {
Brian Salomond7065e72018-10-12 11:42:02 -0400400 TRACE_EVENT0("skia", TRACE_FUNC);
Brian Salomond7065e72018-10-12 11:42:02 -0400401 auto origin = proxy->origin();
402 const auto* texture = proxy->peekTexture();
Brian Salomon246bc3d2018-12-06 15:33:02 -0500403 float iw, ih, h;
404 if (proxy->textureType() == GrTextureType::kRectangle) {
405 iw = ih = 1.f;
406 h = texture->height();
407 } else {
408 iw = 1.f / texture->width();
409 ih = 1.f / texture->height();
410 h = 1.f;
411 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000412
Brian Salomond7065e72018-10-12 11:42:02 -0400413 for (int i = start; i < start + cnt; ++i) {
Michael Ludwigc96fc372019-01-08 15:46:15 -0500414 const GrPerspQuad& device = fQuads[i];
415 const ColorDomainAndAA& info = fQuads.metadata(i);
416
Michael Ludwig009b92e2019-02-15 16:03:53 -0500417 GrPerspQuad srcQuad = info.fSrcQuadIndex >= 0 ?
418 compute_src_quad(origin, fSrcQuads[info.fSrcQuadIndex], iw, ih, h) :
419 compute_src_quad_from_rect(origin, info.fSrcRect, iw, ih, h);
Brian Salomon246bc3d2018-12-06 15:33:02 -0500420 SkRect domain =
Michael Ludwigc96fc372019-01-08 15:46:15 -0500421 compute_domain(info.domain(), this->filter(), origin, info.fSrcRect, iw, ih, h);
422 v = GrQuadPerEdgeAA::Tessellate(v, spec, device, info.fColor, srcQuad, domain,
423 info.aaFlags());
Brian Salomon17031a72018-05-22 14:14:07 -0400424 }
425 }
426
Brian Salomon34169692017-08-28 15:32:01 -0400427 void onPrepareDraws(Target* target) override {
Brian Salomond7065e72018-10-12 11:42:02 -0400428 TRACE_EVENT0("skia", TRACE_FUNC);
Michael Ludwigf995c052018-11-26 15:24:29 -0500429 GrQuadType quadType = GrQuadType::kRect;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500430 GrQuadType srcQuadType = GrQuadType::kRect;
Brian Salomonf7232642018-09-19 08:58:08 -0400431 Domain domain = Domain::kNo;
Brian Salomon1d835422019-03-13 16:11:44 -0400432 ColorType colorType = ColorType::kNone;
Brian Salomond7065e72018-10-12 11:42:02 -0400433 int numProxies = 0;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400434 int numTotalQuads = 0;
Brian Salomond7065e72018-10-12 11:42:02 -0400435 auto textureType = fProxies[0].fProxy->textureType();
436 auto config = fProxies[0].fProxy->config();
Brian Salomonae7d7702018-10-14 15:05:45 -0400437 GrAAType aaType = this->aaType();
Brian Salomonf7232642018-09-19 08:58:08 -0400438 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwigc96fc372019-01-08 15:46:15 -0500439 if (op.fQuads.quadType() > quadType) {
440 quadType = op.fQuads.quadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500441 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500442 if (op.fSrcQuads.quadType() > srcQuadType) {
443 // Should only become more general if there are quads to use instead of fSrcRect
444 SkASSERT(op.fSrcQuads.count() > 0);
445 srcQuadType = op.fSrcQuads.quadType();
446 }
Brian Salomonf7232642018-09-19 08:58:08 -0400447 if (op.fDomain) {
448 domain = Domain::kYes;
449 }
Brian Salomon1d835422019-03-13 16:11:44 -0400450 colorType = SkTMax(colorType, static_cast<ColorType>(op.fColorType));
Brian Salomond7065e72018-10-12 11:42:02 -0400451 numProxies += op.fProxyCnt;
452 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
Brian Salomon4b8178f2018-10-12 13:18:27 -0400453 numTotalQuads += op.fProxies[p].fQuadCnt;
Brian Salomond7065e72018-10-12 11:42:02 -0400454 auto* proxy = op.fProxies[p].fProxy;
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400455 if (target->resourceProvider()->explicitlyAllocateGPUResources()) {
456 SkASSERT(proxy->isInstantiated());
457 } else if (!proxy->instantiate(target->resourceProvider())) {
Brian Salomond7065e72018-10-12 11:42:02 -0400458 return;
459 }
460 SkASSERT(proxy->config() == config);
461 SkASSERT(proxy->textureType() == textureType);
Brian Salomonf7232642018-09-19 08:58:08 -0400462 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400463 if (op.aaType() == GrAAType::kCoverage) {
464 SkASSERT(aaType == GrAAType::kCoverage || aaType == GrAAType::kNone);
465 aaType = GrAAType::kCoverage;
466 }
Brian Salomon34169692017-08-28 15:32:01 -0400467 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400468
Brian Salomon1d835422019-03-13 16:11:44 -0400469 VertexSpec vertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true, domain, aaType,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500470 /* alpha as coverage */ true);
Michael Ludwigc182b942018-11-16 10:27:51 -0500471
Greg Daniel7a82edf2018-12-04 10:54:34 -0500472 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
473 this->filter());
474 GrGpu* gpu = target->resourceProvider()->priv().gpu();
475 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
476 samplerState, fProxies[0].fProxy->backendFormat());
477
Michael Ludwig467994d2018-12-03 14:58:31 +0000478 sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
479 vertexSpec, *target->caps().shaderCaps(),
Greg Daniel7a82edf2018-12-04 10:54:34 -0500480 textureType, config, samplerState, extraSamplerKey,
481 std::move(fTextureColorSpaceXform));
482
Brian Salomonf7232642018-09-19 08:58:08 -0400483 // We'll use a dynamic state array for the GP textures when there are multiple ops.
484 // Otherwise, we use fixed dynamic state to specify the single op's proxy.
485 GrPipeline::DynamicStateArrays* dynamicStateArrays = nullptr;
486 GrPipeline::FixedDynamicState* fixedDynamicState;
Brian Salomond7065e72018-10-12 11:42:02 -0400487 if (numProxies > 1) {
488 dynamicStateArrays = target->allocDynamicStateArrays(numProxies, 1, false);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700489 fixedDynamicState = target->makeFixedDynamicState(0);
Brian Salomonf7232642018-09-19 08:58:08 -0400490 } else {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700491 fixedDynamicState = target->makeFixedDynamicState(1);
Brian Salomond7065e72018-10-12 11:42:02 -0400492 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxies[0].fProxy;
Brian Salomonf7232642018-09-19 08:58:08 -0400493 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400494
Michael Ludwigc182b942018-11-16 10:27:51 -0500495 size_t vertexSize = gp->vertexStride();
Brian Salomon92be2f72018-06-19 14:33:47 -0400496
Brian Salomond7065e72018-10-12 11:42:02 -0400497 GrMesh* meshes = target->allocMeshes(numProxies);
Brian Salomon12d22642019-01-29 14:38:50 -0500498 sk_sp<const GrBuffer> vbuffer;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400499 int vertexOffsetInBuffer = 0;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500500 int numQuadVerticesLeft = numTotalQuads * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400501 int numAllocatedVertices = 0;
502 void* vdata = nullptr;
503
Brian Salomond7065e72018-10-12 11:42:02 -0400504 int m = 0;
Brian Salomonf7232642018-09-19 08:58:08 -0400505 for (const auto& op : ChainRange<TextureOp>(this)) {
Brian Salomond7065e72018-10-12 11:42:02 -0400506 int q = 0;
507 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
508 int quadCnt = op.fProxies[p].fQuadCnt;
509 auto* proxy = op.fProxies[p].fProxy;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500510 int meshVertexCnt = quadCnt * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400511 if (numAllocatedVertices < meshVertexCnt) {
512 vdata = target->makeVertexSpaceAtLeast(
513 vertexSize, meshVertexCnt, numQuadVerticesLeft, &vbuffer,
514 &vertexOffsetInBuffer, &numAllocatedVertices);
515 SkASSERT(numAllocatedVertices <= numQuadVerticesLeft);
516 if (!vdata) {
517 SkDebugf("Could not allocate vertices\n");
518 return;
519 }
Brian Salomonf7232642018-09-19 08:58:08 -0400520 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400521 SkASSERT(numAllocatedVertices >= meshVertexCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400522
Michael Ludwigc182b942018-11-16 10:27:51 -0500523 op.tess(vdata, vertexSpec, proxy, q, quadCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400524
Michael Ludwig93aeba02018-12-21 09:50:31 -0500525 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, &(meshes[m]), vertexSpec,
526 quadCnt)) {
527 SkDebugf("Could not allocate indices");
528 return;
Brian Salomond7065e72018-10-12 11:42:02 -0400529 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400530 meshes[m].setVertexData(vbuffer, vertexOffsetInBuffer);
Brian Salomond7065e72018-10-12 11:42:02 -0400531 if (dynamicStateArrays) {
532 dynamicStateArrays->fPrimitiveProcessorTextures[m] = proxy;
533 }
534 ++m;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400535 numAllocatedVertices -= meshVertexCnt;
536 numQuadVerticesLeft -= meshVertexCnt;
537 vertexOffsetInBuffer += meshVertexCnt;
538 vdata = reinterpret_cast<char*>(vdata) + vertexSize * meshVertexCnt;
Brian Salomond7065e72018-10-12 11:42:02 -0400539 q += quadCnt;
Brian Salomonf7232642018-09-19 08:58:08 -0400540 }
Brian Salomon34169692017-08-28 15:32:01 -0400541 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400542 SkASSERT(!numQuadVerticesLeft);
543 SkASSERT(!numAllocatedVertices);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700544 target->recordDraw(
545 std::move(gp), meshes, numProxies, fixedDynamicState, dynamicStateArrays);
546 }
547
548 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
549 auto pipelineFlags = (GrAAType::kMSAA == this->aaType())
Chris Daltonbaa1b352019-04-03 12:03:00 -0600550 ? GrPipeline::InputFlags::kHWAntialias
551 : GrPipeline::InputFlags::kNone;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700552 flushState->executeDrawsAndUploadsForMeshDrawOp(
553 this, chainBounds, GrProcessorSet::MakeEmptySet(), pipelineFlags);
Brian Salomon34169692017-08-28 15:32:01 -0400554 }
555
Brian Salomonf7232642018-09-19 08:58:08 -0400556 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomond7065e72018-10-12 11:42:02 -0400557 TRACE_EVENT0("skia", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400558 const auto* that = t->cast<TextureOp>();
Brian Osman3ebd3542018-07-30 14:36:53 -0400559 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
560 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000561 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400562 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400563 bool upgradeToCoverageAAOnMerge = false;
Brian Salomon485b8c62018-01-12 15:11:06 -0500564 if (this->aaType() != that->aaType()) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400565 if (!((this->aaType() == GrAAType::kCoverage && that->aaType() == GrAAType::kNone) ||
566 (that->aaType() == GrAAType::kCoverage && this->aaType() == GrAAType::kNone))) {
567 return CombineResult::kCannotCombine;
568 }
569 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500570 }
Brian Salomonf7232642018-09-19 08:58:08 -0400571 if (fFilter != that->fFilter) {
572 return CombineResult::kCannotCombine;
573 }
Brian Salomond7065e72018-10-12 11:42:02 -0400574 auto thisProxy = fProxies[0].fProxy;
575 auto thatProxy = that->fProxies[0].fProxy;
576 if (fProxyCnt > 1 || that->fProxyCnt > 1 ||
Brian Salomon588cec72018-11-14 13:56:37 -0500577 thisProxy->uniqueID() != thatProxy->uniqueID()) {
578 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -0500579 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Brian Salomonf7232642018-09-19 08:58:08 -0400580 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
581 return CombineResult::kMayChain;
582 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000583 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400584 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500585
Brian Salomonb80ffee2018-05-23 16:39:39 -0400586 fDomain |= that->fDomain;
Brian Salomon1d835422019-03-13 16:11:44 -0400587 fColorType = SkTMax(fColorType, that->fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -0400588 if (upgradeToCoverageAAOnMerge) {
589 fAAType = static_cast<unsigned>(GrAAType::kCoverage);
590 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500591
592 // Concatenate quad lists together, updating the fSrcQuadIndex in the appended quads
593 // to account for the new starting index in fSrcQuads
594 int srcQuadOffset = fSrcQuads.count();
595 int oldQuadCount = fQuads.count();
596
597 fSrcQuads.concat(that->fSrcQuads);
598 fQuads.concat(that->fQuads);
599 fProxies[0].fQuadCnt += that->fQuads.count();
600
601 if (that->fSrcQuads.count() > 0) {
602 // Some of the concatenated quads pointed to fSrcQuads, so adjust the indices for the
603 // newly appended quads
604 for (int i = oldQuadCount; i < fQuads.count(); ++i) {
605 if (fQuads.metadata(i).fSrcQuadIndex >= 0) {
606 fQuads.metadata(i).fSrcQuadIndex += srcQuadOffset;
607 }
608 }
609 }
610
611 // Confirm all tracked state makes sense when in debug builds
612#ifdef SK_DEBUG
613 SkASSERT(fSrcQuads.count() <= fQuads.count());
614 for (int i = 0; i < fQuads.count(); ++i) {
615 int srcIndex = fQuads.metadata(i).fSrcQuadIndex;
616 if (srcIndex >= 0) {
617 // Make sure it points to a valid index, in the right region of the list
618 SkASSERT(srcIndex < fSrcQuads.count());
619 SkASSERT((i < oldQuadCount && srcIndex < srcQuadOffset) ||
620 (i >= oldQuadCount && srcIndex >= srcQuadOffset));
621 }
622 }
623#endif
Brian Salomon7eae3e02018-08-07 14:02:38 +0000624 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -0400625 }
626
Brian Salomon485b8c62018-01-12 15:11:06 -0500627 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomon0087c832018-10-15 14:48:20 -0400628 GrSamplerState::Filter filter() const { return static_cast<GrSamplerState::Filter>(fFilter); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500629
Michael Ludwigc96fc372019-01-08 15:46:15 -0500630 struct ColorDomainAndAA {
631 // Special constructor to convert enums into the packed bits, which should not delete
632 // the implicit move constructor (but it does require us to declare an empty ctor for
633 // use with the GrTQuadList).
Michael Ludwig009b92e2019-02-15 16:03:53 -0500634 ColorDomainAndAA(const SkPMColor4f& color, const SkRect& srcRect, int srcQuadIndex,
Michael Ludwigc96fc372019-01-08 15:46:15 -0500635 Domain hasDomain, GrQuadAAFlags aaFlags)
636 : fColor(color)
637 , fSrcRect(srcRect)
Michael Ludwig009b92e2019-02-15 16:03:53 -0500638 , fSrcQuadIndex(srcQuadIndex)
Michael Ludwigc96fc372019-01-08 15:46:15 -0500639 , fHasDomain(static_cast<unsigned>(hasDomain))
Brian Salomon2213ee92018-10-02 10:44:21 -0400640 , fAAFlags(static_cast<unsigned>(aaFlags)) {
Michael Ludwigc96fc372019-01-08 15:46:15 -0500641 SkASSERT(fHasDomain == static_cast<unsigned>(hasDomain));
Brian Salomon2213ee92018-10-02 10:44:21 -0400642 SkASSERT(fAAFlags == static_cast<unsigned>(aaFlags));
643 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500644 ColorDomainAndAA() = default;
Michael Ludwig23d8943e2019-01-04 14:51:40 +0000645
Michael Ludwig23d8943e2019-01-04 14:51:40 +0000646 SkPMColor4f fColor;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500647 // Even if fSrcQuadIndex provides source coords, use fSrcRect for domain constraint
Michael Ludwigc96fc372019-01-08 15:46:15 -0500648 SkRect fSrcRect;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500649 // If >= 0, use to access fSrcQuads instead of fSrcRect for the source coordinates
650 int fSrcQuadIndex;
Michael Ludwig23d8943e2019-01-04 14:51:40 +0000651 unsigned fHasDomain : 1;
652 unsigned fAAFlags : 4;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500653
654 Domain domain() const { return Domain(fHasDomain); }
655 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
Brian Salomon34169692017-08-28 15:32:01 -0400656 };
Brian Salomond7065e72018-10-12 11:42:02 -0400657 struct Proxy {
658 GrTextureProxy* fProxy;
659 int fQuadCnt;
660 };
Michael Ludwigc96fc372019-01-08 15:46:15 -0500661 GrTQuadList<ColorDomainAndAA> fQuads;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500662 // The majority of texture ops will not track a complete src quad so this is indexed separately
663 // and may be of different size to fQuads.
664 GrQuadList fSrcQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -0400665 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Brian Salomon0087c832018-10-15 14:48:20 -0400666 unsigned fFilter : 2;
Brian Salomon485b8c62018-01-12 15:11:06 -0500667 unsigned fAAType : 2;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400668 unsigned fDomain : 1;
Brian Salomon1d835422019-03-13 16:11:44 -0400669 unsigned fColorType : 2;
670 GR_STATIC_ASSERT(GrQuadPerEdgeAA::kColorTypeCount <= 4);
Brian Salomon34169692017-08-28 15:32:01 -0400671 // Used to track whether fProxy is ref'ed or has a pending IO after finalize() is called.
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500672 unsigned fFinalized : 1;
Brian Salomon7d94bb52018-10-12 14:37:19 -0400673 unsigned fCanSkipAllocatorGather : 1;
Brian Salomon1d835422019-03-13 16:11:44 -0400674 unsigned fProxyCnt : 32 - 9;
Brian Salomond7065e72018-10-12 11:42:02 -0400675 Proxy fProxies[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -0400676
Michael Ludwigf995c052018-11-26 15:24:29 -0500677 static_assert(kGrQuadTypeCount <= 4, "GrQuadType does not fit in 2 bits");
678
Brian Salomon34169692017-08-28 15:32:01 -0400679 typedef GrMeshDrawOp INHERITED;
680};
681
682} // anonymous namespace
683
684namespace GrTextureOp {
685
Robert Phillipsb97da532019-02-12 15:24:12 -0500686std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400687 sk_sp<GrTextureProxy> proxy,
688 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500689 const SkPMColor4f& color,
Robert Phillips7c525e62018-06-12 10:11:12 -0400690 const SkRect& srcRect,
691 const SkRect& dstRect,
692 GrAAType aaType,
Brian Salomon2213ee92018-10-02 10:44:21 -0400693 GrQuadAAFlags aaFlags,
Robert Phillips7c525e62018-06-12 10:11:12 -0400694 SkCanvas::SrcRectConstraint constraint,
695 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500696 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400697 return TextureOp::Make(context, std::move(proxy), filter, color, srcRect, dstRect, aaType,
Brian Osman3d139a42018-11-19 10:42:10 -0500698 aaFlags, constraint, viewMatrix, std::move(textureColorSpaceXform));
Brian Salomon34169692017-08-28 15:32:01 -0400699}
700
Michael Ludwig009b92e2019-02-15 16:03:53 -0500701std::unique_ptr<GrDrawOp> MakeQuad(GrRecordingContext* context,
702 sk_sp<GrTextureProxy> proxy,
703 GrSamplerState::Filter filter,
704 const SkPMColor4f& color,
705 const SkPoint srcQuad[4],
706 const SkPoint dstQuad[4],
707 GrAAType aaType,
708 GrQuadAAFlags aaFlags,
709 const SkRect* domain,
710 const SkMatrix& viewMatrix,
711 sk_sp<GrColorSpaceXform> textureXform) {
712 return TextureOp::Make(context, std::move(proxy), filter, color, srcQuad, dstQuad, aaType,
713 aaFlags, domain, viewMatrix, std::move(textureXform));
714}
715
716std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
717 const GrRenderTargetContext::TextureSetEntry set[],
718 int cnt,
719 GrSamplerState::Filter filter,
720 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400721 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500722 const SkMatrix& viewMatrix,
723 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig31ba7182019-04-03 10:38:06 -0400724 return TextureOp::Make(context, set, cnt, filter, aaType, constraint, viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500725 std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400726}
727
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500728bool GetFilterHasEffect(const SkMatrix& viewMatrix, const SkRect& srcRect, const SkRect& dstRect) {
729 // Hypothetically we could disable bilerp filtering when flipping or rotating 90 degrees, but
730 // that makes the math harder and we don't want to increase the overhead of the checks
731 if (!viewMatrix.isScaleTranslate() ||
732 viewMatrix.getScaleX() < 0.0f || viewMatrix.getScaleY() < 0.0f) {
733 return true;
734 }
735
736 // Given the matrix conditions ensured above, this computes the device space coordinates for
737 // the top left corner of dstRect and its size.
738 SkScalar dw = viewMatrix.getScaleX() * dstRect.width();
739 SkScalar dh = viewMatrix.getScaleY() * dstRect.height();
740 SkScalar dl = viewMatrix.getScaleX() * dstRect.fLeft + viewMatrix.getTranslateX();
741 SkScalar dt = viewMatrix.getScaleY() * dstRect.fTop + viewMatrix.getTranslateY();
742
743 // Disable filtering when there is no scaling of the src rect and the src rect and dst rect
744 // align fractionally. If we allow inverted src rects this logic needs to consider that.
745 SkASSERT(srcRect.isSorted());
746 return dw != srcRect.width() || dh != srcRect.height() ||
747 SkScalarFraction(dl) != SkScalarFraction(srcRect.fLeft) ||
748 SkScalarFraction(dt) != SkScalarFraction(srcRect.fTop);
749}
750
Brian Salomon34169692017-08-28 15:32:01 -0400751} // namespace GrTextureOp
752
753#if GR_TEST_UTILS
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500754#include "GrProxyProvider.h"
Robert Phillipsb97da532019-02-12 15:24:12 -0500755#include "GrRecordingContext.h"
756#include "GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -0400757
758GR_DRAW_OP_TEST_DEFINE(TextureOp) {
759 GrSurfaceDesc desc;
760 desc.fConfig = kRGBA_8888_GrPixelConfig;
761 desc.fHeight = random->nextULessThan(90) + 10;
762 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500763 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel09c94002018-06-08 22:11:51 +0000764 GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
765 SkBackingFit fit = SkBackingFit::kExact;
766 if (mipMapped == GrMipMapped::kNo) {
767 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
768 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500769
Greg Daniel4065d452018-11-16 15:43:41 -0500770 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500771 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500772
Robert Phillips9da87e02019-02-04 13:26:26 -0500773 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Greg Daniel4065d452018-11-16 15:43:41 -0500774 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format, desc, origin, mipMapped, fit,
Greg Daniel09c94002018-06-08 22:11:51 +0000775 SkBudgeted::kNo,
776 GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500777
Brian Salomon34169692017-08-28 15:32:01 -0400778 SkRect rect = GrTest::TestRect(random);
779 SkRect srcRect;
780 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
781 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
782 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
783 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
784 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -0500785 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400786 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
787 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +0000788 while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
789 filter = (GrSamplerState::Filter)random->nextULessThan(
790 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
791 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400792 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -0500793 GrAAType aaType = GrAAType::kNone;
794 if (random->nextBool()) {
795 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
796 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400797 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
798 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
799 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
800 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
801 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400802 auto constraint = random->nextBool() ? SkCanvas::kStrict_SrcRectConstraint
803 : SkCanvas::kFast_SrcRectConstraint;
Robert Phillips7c525e62018-06-12 10:11:12 -0400804 return GrTextureOp::Make(context, std::move(proxy), filter, color, srcRect, rect, aaType,
Brian Osman3d139a42018-11-19 10:42:10 -0500805 aaFlags, constraint, viewMatrix, std::move(texXform));
Brian Salomon34169692017-08-28 15:32:01 -0400806}
807
808#endif