blob: e7bf602f7898319f406bdb3781d8dd1338da4cdc [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"
Michael Ludwig22429f92019-06-27 10:44:48 -040014#include "include/private/SkFloatingPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/SkGr.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040033#include "src/gpu/effects/GrTextureDomain.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040034#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040035#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040036#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/glsl/GrGLSLVarying.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040038#include "src/gpu/ops/GrFillRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/ops/GrMeshDrawOp.h"
40#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Brian Salomon34169692017-08-28 15:32:01 -040041
42namespace {
43
Michael Ludwig460eb5e2018-10-29 11:09:29 -040044using Domain = GrQuadPerEdgeAA::Domain;
Michael Ludwigc182b942018-11-16 10:27:51 -050045using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050046using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040047
Michael Ludwig22429f92019-06-27 10:44:48 -040048// Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
49// between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
50static SkSize axis_aligned_quad_size(const GrQuad& quad) {
51 SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
52 // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
53 float dw = sk_float_abs(quad.x(2) - quad.x(0)) + sk_float_abs(quad.y(2) - quad.y(0));
54 float dh = sk_float_abs(quad.x(1) - quad.x(0)) + sk_float_abs(quad.y(1) - quad.y(0));
55 return {dw, dh};
56}
57
58static bool filter_has_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
59 // If not axis-aligned in src or dst, then always say it has an effect
60 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
61 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
62 return true;
63 }
64
65 SkRect srcRect;
66 SkRect dstRect;
67 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
68 // Disable filtering when there is no scaling (width and height are the same), and the
69 // top-left corners have the same fraction (so src and dst snap to the pixel grid
70 // identically).
71 SkASSERT(srcRect.isSorted());
72 return srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
73 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
74 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
75 } else {
76 // Although the quads are axis-aligned, the local coordinate system is transformed such
77 // that fractionally-aligned sample centers will not align with the device coordinate system
78 // So disable filtering when edges are the same length and both srcQuad and dstQuad
79 // 0th vertex is integer aligned.
80 if (SkScalarIsInt(srcQuad.x(0)) && SkScalarIsInt(srcQuad.y(0)) &&
81 SkScalarIsInt(dstQuad.x(0)) && SkScalarIsInt(dstQuad.y(0))) {
82 // Extract edge lengths
83 SkSize srcSize = axis_aligned_quad_size(srcQuad);
84 SkSize dstSize = axis_aligned_quad_size(dstQuad);
85 return srcSize.fWidth != dstSize.fWidth || srcSize.fHeight != dstSize.fHeight;
86 } else {
87 return true;
88 }
89 }
90}
91
Brian Salomon246bc3d2018-12-06 15:33:02 -050092// if normalizing the domain then pass 1/width, 1/height, 1 for iw, ih, h. Otherwise pass
93// 1, 1, and height.
Michael Ludwigf339dfe2019-06-27 10:41:28 -040094static void compute_domain(Domain domain, GrSamplerState::Filter filter, GrSurfaceOrigin origin,
95 const SkRect& domainRect, float iw, float ih, float h, SkRect* out) {
Brian Salomon246bc3d2018-12-06 15:33:02 -050096 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Michael Ludwig460eb5e2018-10-29 11:09:29 -040097 if (domain == Domain::kNo) {
98 // Either the quad has no domain constraint and is batched with a domain constrained op
99 // (in which case we want a domain that doesn't restrict normalized tex coords), or the
100 // entire op doesn't use the domain, in which case the returned value is ignored.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400101 *out = kLargeRect;
102 return;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400103 }
104
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400105 auto ltrb = Sk4f::Load(&domainRect);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400106 if (filter == GrSamplerState::Filter::kBilerp) {
107 auto rblt = SkNx_shuffle<2, 3, 0, 1>(ltrb);
108 auto whwh = (rblt - ltrb).abs();
109 auto c = (rblt + ltrb) * 0.5f;
110 static const Sk4f kOffsets = {0.5f, 0.5f, -0.5f, -0.5f};
111 ltrb = (whwh < 1.f).thenElse(c, ltrb + kOffsets);
112 }
113 ltrb *= Sk4f(iw, ih, iw, ih);
114 if (origin == kBottomLeft_GrSurfaceOrigin) {
115 static const Sk4f kMul = {1.f, -1.f, 1.f, -1.f};
Brian Salomon246bc3d2018-12-06 15:33:02 -0500116 const Sk4f kAdd = {0.f, h, 0.f, h};
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400117 ltrb = SkNx_shuffle<0, 3, 2, 1>(kMul * ltrb + kAdd);
118 }
119
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400120 ltrb.store(out);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400121}
122
Michael Ludwig009b92e2019-02-15 16:03:53 -0500123// Normalizes logical src coords and corrects for origin
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400124static void compute_src_quad(GrSurfaceOrigin origin, const GrQuad& srcQuad,
125 float iw, float ih, float h, GrQuad* out) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500126 // The src quad should not have any perspective
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400127 SkASSERT(!srcQuad.hasPerspective() && !out->hasPerspective());
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400128 skvx::Vec<4, float> xs = srcQuad.x4f() * iw;
129 skvx::Vec<4, float> ys = srcQuad.y4f() * ih;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500130 if (origin == kBottomLeft_GrSurfaceOrigin) {
131 ys = h - ys;
132 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400133 xs.store(out->xs());
134 ys.store(out->ys());
135 out->setQuadType(srcQuad.quadType());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500136}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400137
Brian Salomon34169692017-08-28 15:32:01 -0400138/**
139 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
140 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
141 */
142class TextureOp final : public GrMeshDrawOp {
143public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500144 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400145 sk_sp<GrTextureProxy> proxy,
Michael Ludwig22429f92019-06-27 10:44:48 -0400146 sk_sp<GrColorSpaceXform> textureXform,
Robert Phillips7c525e62018-06-12 10:11:12 -0400147 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500148 const SkPMColor4f& color,
Robert Phillips7c525e62018-06-12 10:11:12 -0400149 GrAAType aaType,
Brian Salomon2213ee92018-10-02 10:44:21 -0400150 GrQuadAAFlags aaFlags,
Michael Ludwig22429f92019-06-27 10:44:48 -0400151 const GrQuad& deviceQuad,
152 const GrQuad& localQuad,
153 const SkRect* domain) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500154 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomon2213ee92018-10-02 10:44:21 -0400155 return pool->allocate<TextureOp>(
Michael Ludwig22429f92019-06-27 10:44:48 -0400156 std::move(proxy), std::move(textureXform), filter, color, aaType, aaFlags,
157 deviceQuad, localQuad, domain);
Brian Salomon34169692017-08-28 15:32:01 -0400158 }
Robert Phillipsb97da532019-02-12 15:24:12 -0500159 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Brian Salomond7065e72018-10-12 11:42:02 -0400160 const GrRenderTargetContext::TextureSetEntry set[],
Brian Salomond003d222018-11-26 13:25:05 -0500161 int cnt, GrSamplerState::Filter filter, GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400162 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500163 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500164 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400165 size_t size = sizeof(TextureOp) + sizeof(Proxy) * (cnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500166 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400167 void* mem = pool->allocate(size);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400168 return std::unique_ptr<GrDrawOp>(new (mem) TextureOp(
169 set, cnt, filter, aaType, constraint, viewMatrix,
170 std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400171 }
Brian Salomon34169692017-08-28 15:32:01 -0400172
Brian Salomon336ce7b2017-09-08 08:23:58 -0400173 ~TextureOp() override {
Brian Salomond7065e72018-10-12 11:42:02 -0400174 for (unsigned p = 0; p < fProxyCnt; ++p) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400175 fProxies[p].fProxy->unref();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400176 }
177 }
Brian Salomon34169692017-08-28 15:32:01 -0400178
179 const char* name() const override { return "TextureOp"; }
180
Chris Dalton1706cbf2019-05-21 19:35:29 -0600181 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomond7065e72018-10-12 11:42:02 -0400182 for (unsigned p = 0; p < fProxyCnt; ++p) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600183 bool mipped = (GrSamplerState::Filter::kMipMap == this->filter());
184 func(fProxies[p].fProxy, GrMipMapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400185 }
186 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400187
Brian Osman9a390ac2018-11-12 09:47:48 -0500188#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400189 SkString dumpInfo() const override {
190 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400191 str.appendf("# draws: %d\n", fQuads.count());
Michael Ludwig425eb452019-06-27 10:13:27 -0400192 auto iter = fQuads.iterator();
Brian Salomond7065e72018-10-12 11:42:02 -0400193 for (unsigned p = 0; p < fProxyCnt; ++p) {
194 str.appendf("Proxy ID: %d, Filter: %d\n", fProxies[p].fProxy->uniqueID().asUInt(),
195 static_cast<int>(fFilter));
Michael Ludwig425eb452019-06-27 10:13:27 -0400196 int i = 0;
197 while(i < fProxies[p].fQuadCnt && iter.next()) {
198 const GrQuad& quad = iter.deviceQuad();
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400199 const GrQuad& uv = iter.localQuad();
Michael Ludwig425eb452019-06-27 10:13:27 -0400200 const ColorDomainAndAA& info = iter.metadata();
Brian Salomond7065e72018-10-12 11:42:02 -0400201 str.appendf(
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400202 "%d: Color: 0x%08x, Domain(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
203 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
204 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
205 i, info.fColor.toBytes_RGBA(), info.fHasDomain, info.fDomainRect.fLeft,
206 info.fDomainRect.fTop, info.fDomainRect.fRight, info.fDomainRect.fBottom,
207 quad.point(0).fX, quad.point(0).fY, quad.point(1).fX, quad.point(1).fY,
208 quad.point(2).fX, quad.point(2).fY, quad.point(3).fX, quad.point(3).fY,
209 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
210 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
211
Michael Ludwig425eb452019-06-27 10:13:27 -0400212 i++;
Brian Salomond7065e72018-10-12 11:42:02 -0400213 }
Brian Salomon34169692017-08-28 15:32:01 -0400214 }
215 str += INHERITED::dumpInfo();
216 return str;
217 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500218#endif
Brian Salomon34169692017-08-28 15:32:01 -0400219
Brian Osman5ced0bf2019-03-15 10:15:29 -0400220 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600221 const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage,
222 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400223 fColorType = static_cast<unsigned>(ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400224 auto iter = fQuads.metadata();
225 while(iter.next()) {
226 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor, clampType, caps);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400227 fColorType = SkTMax(fColorType, static_cast<unsigned>(colorType));
228 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700229 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400230 }
231
Brian Salomon485b8c62018-01-12 15:11:06 -0500232 FixedFunctionFlags fixedFunctionFlags() const override {
233 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
234 : FixedFunctionFlags::kNone;
235 }
Brian Salomon34169692017-08-28 15:32:01 -0400236
237 DEFINE_OP_CLASS_ID
238
239private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400240 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500241
Michael Ludwig425eb452019-06-27 10:13:27 -0400242 struct ColorDomainAndAA {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400243 ColorDomainAndAA(const SkPMColor4f& color, const SkRect* domainRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400244 : fColor(color)
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400245 , fDomainRect(domainRect ? *domainRect : SkRect::MakeEmpty())
246 , fHasDomain(static_cast<unsigned>(domainRect ? Domain::kYes : Domain::kNo))
Michael Ludwig425eb452019-06-27 10:13:27 -0400247 , fAAFlags(static_cast<unsigned>(aaFlags)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400248 SkASSERT(fAAFlags == static_cast<unsigned>(aaFlags));
249 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400250
251 SkPMColor4f fColor;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400252 SkRect fDomainRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400253 unsigned fHasDomain : 1;
254 unsigned fAAFlags : 4;
255
256 Domain domain() const { return Domain(fHasDomain); }
257 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
258 };
259 struct Proxy {
260 GrTextureProxy* fProxy;
261 int fQuadCnt;
262 };
263
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400264 // dstQuad should be the geometry transformed by the view matrix. If domainRect
265 // is not null it will be used to apply the strict src rect constraint.
Michael Ludwig22429f92019-06-27 10:44:48 -0400266 TextureOp(sk_sp<GrTextureProxy> proxy, sk_sp<GrColorSpaceXform> textureColorSpaceXform,
267 GrSamplerState::Filter filter, const SkPMColor4f& color,
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400268 GrAAType aaType, GrQuadAAFlags aaFlags,
Michael Ludwig22429f92019-06-27 10:44:48 -0400269 const GrQuad& dstQuad, const GrQuad& srcQuad, const SkRect* domainRect)
Brian Salomon34169692017-08-28 15:32:01 -0400270 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400271 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400272 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips3d4cac52019-06-11 08:08:08 -0400273 , fFilter(static_cast<unsigned>(filter)) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400274 // Clean up disparities between the overall aa type and edge configuration and apply
275 // optimizations based on the rect and matrix when appropriate
Michael Ludwig0f809022019-06-04 09:14:37 -0400276 GrQuadUtils::ResolveAAType(aaType, aaFlags, dstQuad, &aaType, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400277 fAAType = static_cast<unsigned>(aaType);
278
Brian Salomonf1709042018-10-03 11:57:00 -0400279 // We expect our caller to have already caught this optimization.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400280 SkASSERT(!domainRect || !domainRect->contains(proxy->getWorstCaseBoundsRect()));
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.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400285 if (domainRect && this->filter() == GrSamplerState::Filter::kNearest &&
Michael Ludwig6bee7762018-10-19 09:50:36 -0400286 aaType != GrAAType::kCoverage) {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400287 domainRect = nullptr;
Brian Salomonf09abc52018-10-03 15:59:04 -0400288 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500289
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400290 fQuads.append(dstQuad, {color, domainRect, aaFlags}, &srcQuad);
Michael Ludwig425eb452019-06-27 10:13:27 -0400291
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 Ludwigf339dfe2019-06-27 10:41:28 -0400296 fDomain = static_cast<unsigned>(domainRect != nullptr);
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())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400303 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400304 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips3d4cac52019-06-11 08:08:08 -0400305 , fFilter(static_cast<unsigned>(filter)) {
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;
Brian Salomon1d835422019-03-13 16:11:44 -0400310 bool allOpaque = true;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400311 Domain netDomain = Domain::kNo;
Brian Salomond7065e72018-10-12 11:42:02 -0400312 for (unsigned p = 0; p < fProxyCnt; ++p) {
313 fProxies[p].fProxy = SkRef(set[p].fProxy.get());
314 fProxies[p].fQuadCnt = 1;
315 SkASSERT(fProxies[p].fProxy->textureType() == fProxies[0].fProxy->textureType());
316 SkASSERT(fProxies[p].fProxy->config() == fProxies[0].fProxy->config());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500317
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500318 SkMatrix ctm = viewMatrix;
319 if (set[p].fPreViewMatrix) {
320 ctm.preConcat(*set[p].fPreViewMatrix);
321 }
322
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400323 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
324 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
325 GrQuad quad, srcQuad;
326 if (set[p].fDstClipQuad) {
327 quad = GrQuad::MakeFromSkQuad(set[p].fDstClipQuad, ctm);
328
329 SkPoint srcPts[4];
330 GrMapRectPoints(set[p].fDstRect, set[p].fSrcRect, set[p].fDstClipQuad, srcPts, 4);
331 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
332 } else {
333 quad = GrQuad::MakeFromRect(set[p].fDstRect, ctm);
334 srcQuad = GrQuad(set[p].fSrcRect);
335 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500336
Michael Ludwig22429f92019-06-27 10:44:48 -0400337 if (!mustFilter && this->filter() != GrSamplerState::Filter::kNearest) {
338 mustFilter = filter_has_effect(srcQuad, quad);
339 }
340
Michael Ludwig41f395d2019-05-23 13:59:45 -0400341 bounds.joinPossiblyEmptyRect(quad.bounds());
Michael Ludwig6bee7762018-10-19 09:50:36 -0400342 GrQuadAAFlags aaFlags;
343 // Don't update the overall aaType, might be inappropriate for some of the quads
344 GrAAType aaForQuad;
Michael Ludwig0f809022019-06-04 09:14:37 -0400345 GrQuadUtils::ResolveAAType(aaType, set[p].fAAFlags, quad, &aaForQuad, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400346 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
347 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
348 if (overallAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
349 overallAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400350 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400351
352 // Calculate metadata for the entry
353 const SkRect* domainForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400354 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
355 // Check (briefly) if the strict constraint is needed for this set entry
356 if (!set[p].fSrcRect.contains(fProxies[p].fProxy->getWorstCaseBoundsRect()) &&
357 (mustFilter || aaForQuad == GrAAType::kCoverage)) {
358 // Can't rely on hardware clamping and the draw will access outer texels
359 // for AA and/or bilerp
360 netDomain = Domain::kYes;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400361 domainForQuad = &set[p].fSrcRect;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400362 }
363 }
Brian Salomond003d222018-11-26 13:25:05 -0500364 float alpha = SkTPin(set[p].fAlpha, 0.f, 1.f);
Brian Salomon1d835422019-03-13 16:11:44 -0400365 allOpaque &= (1.f == alpha);
Brian Salomond003d222018-11-26 13:25:05 -0500366 SkPMColor4f color{alpha, alpha, alpha, alpha};
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400367 fQuads.append(quad, {color, domainForQuad, aaFlags}, &srcQuad);
Brian Salomond7065e72018-10-12 11:42:02 -0400368 }
Michael Ludwig6bee7762018-10-19 09:50:36 -0400369 fAAType = static_cast<unsigned>(overallAAType);
Brian Salomon0087c832018-10-15 14:48:20 -0400370 if (!mustFilter) {
371 fFilter = static_cast<unsigned>(GrSamplerState::Filter::kNearest);
372 }
Brian Salomond7065e72018-10-12 11:42:02 -0400373 this->setBounds(bounds, HasAABloat(this->aaType() == GrAAType::kCoverage), IsZeroArea::kNo);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400374 fDomain = static_cast<unsigned>(netDomain);
Brian Salomon34169692017-08-28 15:32:01 -0400375 }
376
Michael Ludwig425eb452019-06-27 10:13:27 -0400377 void tess(void* v, const VertexSpec& spec, const GrTextureProxy* proxy,
378 GrQuadBuffer<ColorDomainAndAA>::Iter* iter, int cnt) const {
Brian Salomon5f394272019-07-02 14:07:49 -0400379 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond7065e72018-10-12 11:42:02 -0400380 auto origin = proxy->origin();
381 const auto* texture = proxy->peekTexture();
Brian Salomon246bc3d2018-12-06 15:33:02 -0500382 float iw, ih, h;
383 if (proxy->textureType() == GrTextureType::kRectangle) {
384 iw = ih = 1.f;
385 h = texture->height();
386 } else {
387 iw = 1.f / texture->width();
388 ih = 1.f / texture->height();
389 h = 1.f;
390 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000391
Michael Ludwig425eb452019-06-27 10:13:27 -0400392 int i = 0;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400393 // Explicit ctor ensures ws are 1s, which compute_src_quad requires
394 GrQuad srcQuad(SkRect::MakeEmpty());
395 SkRect domain;
Michael Ludwig425eb452019-06-27 10:13:27 -0400396 while(i < cnt && iter->next()) {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400397 SkASSERT(iter->isLocalValid());
Michael Ludwig425eb452019-06-27 10:13:27 -0400398 const ColorDomainAndAA& info = iter->metadata();
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400399 // Must correct the texture coordinates and domain now that the real texture size
400 // is known
401 compute_src_quad(origin, iter->localQuad(), iw, ih, h, &srcQuad);
402 compute_domain(info.domain(), this->filter(), origin, info.fDomainRect, iw, ih, h,
403 &domain);
Michael Ludwig425eb452019-06-27 10:13:27 -0400404 v = GrQuadPerEdgeAA::Tessellate(v, spec, iter->deviceQuad(), info.fColor, srcQuad,
405 domain, info.aaFlags());
406 i++;
Brian Salomon17031a72018-05-22 14:14:07 -0400407 }
408 }
409
Brian Salomon34169692017-08-28 15:32:01 -0400410 void onPrepareDraws(Target* target) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400411 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400412 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
413 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
Brian Salomonf7232642018-09-19 08:58:08 -0400414 Domain domain = Domain::kNo;
Brian Salomon1d835422019-03-13 16:11:44 -0400415 ColorType colorType = ColorType::kNone;
Brian Salomond7065e72018-10-12 11:42:02 -0400416 int numProxies = 0;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400417 int numTotalQuads = 0;
Brian Salomond7065e72018-10-12 11:42:02 -0400418 auto textureType = fProxies[0].fProxy->textureType();
419 auto config = fProxies[0].fProxy->config();
Greg Daniel2c3398d2019-06-19 11:58:01 -0400420 const GrSwizzle& swizzle = fProxies[0].fProxy->textureSwizzle();
Brian Salomonae7d7702018-10-14 15:05:45 -0400421 GrAAType aaType = this->aaType();
Brian Salomonf7232642018-09-19 08:58:08 -0400422 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400423 if (op.fQuads.deviceQuadType() > quadType) {
424 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500425 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400426 if (op.fQuads.localQuadType() > srcQuadType) {
427 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500428 }
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);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400442 SkASSERT(proxy->textureSwizzle() == swizzle);
Brian Salomonf7232642018-09-19 08:58:08 -0400443 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400444 if (op.aaType() == GrAAType::kCoverage) {
445 SkASSERT(aaType == GrAAType::kCoverage || aaType == GrAAType::kNone);
446 aaType = GrAAType::kCoverage;
447 }
Brian Salomon34169692017-08-28 15:32:01 -0400448 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400449
Brian Salomon1d835422019-03-13 16:11:44 -0400450 VertexSpec vertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true, domain, aaType,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500451 /* alpha as coverage */ true);
Michael Ludwigc182b942018-11-16 10:27:51 -0500452
Greg Daniel7a82edf2018-12-04 10:54:34 -0500453 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
454 this->filter());
455 GrGpu* gpu = target->resourceProvider()->priv().gpu();
456 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
457 samplerState, fProxies[0].fProxy->backendFormat());
458
Michael Ludwig467994d2018-12-03 14:58:31 +0000459 sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
460 vertexSpec, *target->caps().shaderCaps(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400461 textureType, config, samplerState, swizzle, extraSamplerKey,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500462 std::move(fTextureColorSpaceXform));
463
Brian Salomonf7232642018-09-19 08:58:08 -0400464 // We'll use a dynamic state array for the GP textures when there are multiple ops.
465 // Otherwise, we use fixed dynamic state to specify the single op's proxy.
466 GrPipeline::DynamicStateArrays* dynamicStateArrays = nullptr;
467 GrPipeline::FixedDynamicState* fixedDynamicState;
Brian Salomond7065e72018-10-12 11:42:02 -0400468 if (numProxies > 1) {
469 dynamicStateArrays = target->allocDynamicStateArrays(numProxies, 1, false);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700470 fixedDynamicState = target->makeFixedDynamicState(0);
Brian Salomonf7232642018-09-19 08:58:08 -0400471 } else {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700472 fixedDynamicState = target->makeFixedDynamicState(1);
Brian Salomond7065e72018-10-12 11:42:02 -0400473 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxies[0].fProxy;
Brian Salomonf7232642018-09-19 08:58:08 -0400474 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400475
Michael Ludwigc182b942018-11-16 10:27:51 -0500476 size_t vertexSize = gp->vertexStride();
Brian Salomon92be2f72018-06-19 14:33:47 -0400477
Brian Salomond7065e72018-10-12 11:42:02 -0400478 GrMesh* meshes = target->allocMeshes(numProxies);
Brian Salomon12d22642019-01-29 14:38:50 -0500479 sk_sp<const GrBuffer> vbuffer;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400480 int vertexOffsetInBuffer = 0;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500481 int numQuadVerticesLeft = numTotalQuads * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400482 int numAllocatedVertices = 0;
483 void* vdata = nullptr;
484
Brian Salomond7065e72018-10-12 11:42:02 -0400485 int m = 0;
Brian Salomonf7232642018-09-19 08:58:08 -0400486 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400487 auto iter = op.fQuads.iterator();
Brian Salomond7065e72018-10-12 11:42:02 -0400488 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
489 int quadCnt = op.fProxies[p].fQuadCnt;
490 auto* proxy = op.fProxies[p].fProxy;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500491 int meshVertexCnt = quadCnt * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400492 if (numAllocatedVertices < meshVertexCnt) {
493 vdata = target->makeVertexSpaceAtLeast(
494 vertexSize, meshVertexCnt, numQuadVerticesLeft, &vbuffer,
495 &vertexOffsetInBuffer, &numAllocatedVertices);
496 SkASSERT(numAllocatedVertices <= numQuadVerticesLeft);
497 if (!vdata) {
498 SkDebugf("Could not allocate vertices\n");
499 return;
500 }
Brian Salomonf7232642018-09-19 08:58:08 -0400501 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400502 SkASSERT(numAllocatedVertices >= meshVertexCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400503
Michael Ludwig425eb452019-06-27 10:13:27 -0400504 op.tess(vdata, vertexSpec, proxy, &iter, quadCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400505
Michael Ludwig93aeba02018-12-21 09:50:31 -0500506 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, &(meshes[m]), vertexSpec,
507 quadCnt)) {
508 SkDebugf("Could not allocate indices");
509 return;
Brian Salomond7065e72018-10-12 11:42:02 -0400510 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400511 meshes[m].setVertexData(vbuffer, vertexOffsetInBuffer);
Brian Salomond7065e72018-10-12 11:42:02 -0400512 if (dynamicStateArrays) {
513 dynamicStateArrays->fPrimitiveProcessorTextures[m] = proxy;
514 }
515 ++m;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400516 numAllocatedVertices -= meshVertexCnt;
517 numQuadVerticesLeft -= meshVertexCnt;
518 vertexOffsetInBuffer += meshVertexCnt;
519 vdata = reinterpret_cast<char*>(vdata) + vertexSize * meshVertexCnt;
Brian Salomonf7232642018-09-19 08:58:08 -0400520 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400521 // If quad counts per proxy were calculated correctly, the entire iterator should have
522 // been consumed.
523 SkASSERT(!iter.next());
Brian Salomon34169692017-08-28 15:32:01 -0400524 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400525 SkASSERT(!numQuadVerticesLeft);
526 SkASSERT(!numAllocatedVertices);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700527 target->recordDraw(
528 std::move(gp), meshes, numProxies, fixedDynamicState, dynamicStateArrays);
529 }
530
531 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
532 auto pipelineFlags = (GrAAType::kMSAA == this->aaType())
Chris Daltonbaa1b352019-04-03 12:03:00 -0600533 ? GrPipeline::InputFlags::kHWAntialias
534 : GrPipeline::InputFlags::kNone;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700535 flushState->executeDrawsAndUploadsForMeshDrawOp(
536 this, chainBounds, GrProcessorSet::MakeEmptySet(), pipelineFlags);
Brian Salomon34169692017-08-28 15:32:01 -0400537 }
538
Brian Salomonf7232642018-09-19 08:58:08 -0400539 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400540 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400541 const auto* that = t->cast<TextureOp>();
Michael Ludwig2929f512019-04-19 13:05:56 -0400542 if (fDomain != that->fDomain) {
543 // It is technically possible to combine operations across domain modes, but performance
544 // testing suggests it's better to make more draw calls where some take advantage of
545 // the more optimal shader path without coordinate clamping.
546 return CombineResult::kCannotCombine;
547 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400548 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
549 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000550 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400551 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400552 bool upgradeToCoverageAAOnMerge = false;
Brian Salomon485b8c62018-01-12 15:11:06 -0500553 if (this->aaType() != that->aaType()) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400554 if (!((this->aaType() == GrAAType::kCoverage && that->aaType() == GrAAType::kNone) ||
555 (that->aaType() == GrAAType::kCoverage && this->aaType() == GrAAType::kNone))) {
556 return CombineResult::kCannotCombine;
557 }
558 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500559 }
Brian Salomonf7232642018-09-19 08:58:08 -0400560 if (fFilter != that->fFilter) {
561 return CombineResult::kCannotCombine;
562 }
Brian Salomond7065e72018-10-12 11:42:02 -0400563 auto thisProxy = fProxies[0].fProxy;
564 auto thatProxy = that->fProxies[0].fProxy;
565 if (fProxyCnt > 1 || that->fProxyCnt > 1 ||
Brian Salomon588cec72018-11-14 13:56:37 -0500566 thisProxy->uniqueID() != thatProxy->uniqueID()) {
567 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -0500568 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Brian Salomonf7232642018-09-19 08:58:08 -0400569 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
570 return CombineResult::kMayChain;
571 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000572 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400573 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500574
Brian Salomonb80ffee2018-05-23 16:39:39 -0400575 fDomain |= that->fDomain;
Brian Salomon1d835422019-03-13 16:11:44 -0400576 fColorType = SkTMax(fColorType, that->fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -0400577 if (upgradeToCoverageAAOnMerge) {
578 fAAType = static_cast<unsigned>(GrAAType::kCoverage);
579 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500580
Michael Ludwig425eb452019-06-27 10:13:27 -0400581 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -0500582 fQuads.concat(that->fQuads);
583 fProxies[0].fQuadCnt += that->fQuads.count();
584
Brian Salomon7eae3e02018-08-07 14:02:38 +0000585 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -0400586 }
587
Brian Salomon485b8c62018-01-12 15:11:06 -0500588 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomon0087c832018-10-15 14:48:20 -0400589 GrSamplerState::Filter filter() const { return static_cast<GrSamplerState::Filter>(fFilter); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500590
Michael Ludwig425eb452019-06-27 10:13:27 -0400591 GrQuadBuffer<ColorDomainAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -0400592 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Brian Salomon0087c832018-10-15 14:48:20 -0400593 unsigned fFilter : 2;
Brian Salomon485b8c62018-01-12 15:11:06 -0500594 unsigned fAAType : 2;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400595 unsigned fDomain : 1;
Brian Salomon1d835422019-03-13 16:11:44 -0400596 unsigned fColorType : 2;
597 GR_STATIC_ASSERT(GrQuadPerEdgeAA::kColorTypeCount <= 4);
Robert Phillips3d4cac52019-06-11 08:08:08 -0400598 unsigned fProxyCnt : 32 - 7;
Brian Salomond7065e72018-10-12 11:42:02 -0400599 Proxy fProxies[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -0400600
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400601 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type does not fit in 2 bits");
Michael Ludwigf995c052018-11-26 15:24:29 -0500602
Brian Salomon34169692017-08-28 15:32:01 -0400603 typedef GrMeshDrawOp INHERITED;
604};
605
606} // anonymous namespace
607
608namespace GrTextureOp {
609
Michael Ludwig205224f2019-06-27 10:47:42 -0400610std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
611 sk_sp<GrTextureProxy> proxy,
612 sk_sp<GrColorSpaceXform> textureXform,
613 GrSamplerState::Filter filter,
614 const SkPMColor4f& color,
615 SkBlendMode blendMode,
616 GrAAType aaType,
617 GrQuadAAFlags aaFlags,
618 const GrQuad& deviceQuad,
619 const GrQuad& localQuad,
620 const SkRect* domain) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400621 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
622 if (domain && domain->contains(proxy->getWorstCaseBoundsRect())) {
623 // No need for a shader-based domain if hardware clamping achieves the same effect
624 domain = nullptr;
625 }
626
627 if (filter != GrSamplerState::Filter::kNearest && !filter_has_effect(localQuad, deviceQuad)) {
628 filter = GrSamplerState::Filter::kNearest;
629 }
630
631 if (blendMode == SkBlendMode::kSrcOver) {
632 return TextureOp::Make(context, std::move(proxy), std::move(textureXform), filter, color,
633 aaType, aaFlags, deviceQuad, localQuad, domain);
634 } else {
635 // Emulate complex blending using GrFillRectOp
636 GrPaint paint;
637 paint.setColor4f(color);
638 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
639
640 std::unique_ptr<GrFragmentProcessor> fp;
641 if (domain) {
642 // Update domain to match what GrTextureOp computes during tessellation, using top-left
643 // as the origin so that it doesn't depend on final texture size (which the FP handles
644 // later, as well as accounting for the true origin).
645 SkRect correctedDomain;
646 compute_domain(Domain::kYes, filter, kTopLeft_GrSurfaceOrigin, *domain,
647 1.f, 1.f, proxy->height(), &correctedDomain);
648 fp = GrTextureDomainEffect::Make(std::move(proxy), SkMatrix::I(), correctedDomain,
649 GrTextureDomain::kClamp_Mode, filter);
650 } else {
651 fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I(), filter);
652 }
653 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
654 paint.addColorFragmentProcessor(std::move(fp));
655
656 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
657 deviceQuad, localQuad);
658 }
659}
660
Michael Ludwig009b92e2019-02-15 16:03:53 -0500661std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
662 const GrRenderTargetContext::TextureSetEntry set[],
663 int cnt,
664 GrSamplerState::Filter filter,
665 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400666 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500667 const SkMatrix& viewMatrix,
668 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Michael Ludwig31ba7182019-04-03 10:38:06 -0400669 return TextureOp::Make(context, set, cnt, filter, aaType, constraint, viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500670 std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400671}
672
Brian Salomon34169692017-08-28 15:32:01 -0400673} // namespace GrTextureOp
674
675#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500676#include "include/private/GrRecordingContext.h"
677#include "src/gpu/GrProxyProvider.h"
678#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -0400679
680GR_DRAW_OP_TEST_DEFINE(TextureOp) {
681 GrSurfaceDesc desc;
682 desc.fConfig = kRGBA_8888_GrPixelConfig;
683 desc.fHeight = random->nextULessThan(90) + 10;
684 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500685 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel09c94002018-06-08 22:11:51 +0000686 GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
687 SkBackingFit fit = SkBackingFit::kExact;
688 if (mipMapped == GrMipMapped::kNo) {
689 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
690 }
Greg Daniel4065d452018-11-16 15:43:41 -0500691 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400692 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
693 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500694
Robert Phillips9da87e02019-02-04 13:26:26 -0500695 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -0400696 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400697 format, desc, GrRenderable::kNo, 1, origin, mipMapped, fit, SkBudgeted::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400698 GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500699
Brian Salomon34169692017-08-28 15:32:01 -0400700 SkRect rect = GrTest::TestRect(random);
701 SkRect srcRect;
702 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
703 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
704 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
705 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
706 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -0500707 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400708 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
709 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +0000710 while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
711 filter = (GrSamplerState::Filter)random->nextULessThan(
712 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
713 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400714 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -0500715 GrAAType aaType = GrAAType::kNone;
716 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600717 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -0500718 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400719 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
720 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
721 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
722 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
723 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Michael Ludwig205224f2019-06-27 10:47:42 -0400724 bool useDomain = random->nextBool();
725 return GrTextureOp::Make(context, std::move(proxy), std::move(texXform), filter, color,
726 SkBlendMode::kSrcOver, aaType, aaFlags,
727 GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect),
728 useDomain ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -0400729}
730
731#endif