blob: c77b250c3e54cd85b8735aee9ebc0a25e4ceb95a [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
Brian Salomond7065e72018-10-12 11:42:02 -04008#include <new>
Brian Salomonf19f9ca2019-09-18 15:54:26 -04009
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"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040034#include "src/gpu/effects/generated/GrSaturateProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040035#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040036#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040037#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/glsl/GrGLSLVarying.h"
Michael Ludwig22429f92019-06-27 10:44:48 -040039#include "src/gpu/ops/GrFillRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/ops/GrMeshDrawOp.h"
41#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040042#include "src/gpu/ops/GrTextureOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040043
44namespace {
45
Michael Ludwig460eb5e2018-10-29 11:09:29 -040046using Domain = GrQuadPerEdgeAA::Domain;
Michael Ludwigc182b942018-11-16 10:27:51 -050047using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
Brian Osman3d139a42018-11-19 10:42:10 -050048using ColorType = GrQuadPerEdgeAA::ColorType;
Brian Salomonb80ffee2018-05-23 16:39:39 -040049
Michael Ludwig22429f92019-06-27 10:44:48 -040050// Extracts lengths of vertical and horizontal edges of axis-aligned quad. "width" is the edge
51// between v0 and v2 (or v1 and v3), "height" is the edge between v0 and v1 (or v2 and v3).
52static SkSize axis_aligned_quad_size(const GrQuad& quad) {
53 SkASSERT(quad.quadType() == GrQuad::Type::kAxisAligned);
54 // Simplification of regular edge length equation, since it's axis aligned and can avoid sqrt
55 float dw = sk_float_abs(quad.x(2) - quad.x(0)) + sk_float_abs(quad.y(2) - quad.y(0));
56 float dh = sk_float_abs(quad.x(1) - quad.x(0)) + sk_float_abs(quad.y(1) - quad.y(0));
57 return {dw, dh};
58}
59
60static bool filter_has_effect(const GrQuad& srcQuad, const GrQuad& dstQuad) {
61 // If not axis-aligned in src or dst, then always say it has an effect
62 if (srcQuad.quadType() != GrQuad::Type::kAxisAligned ||
63 dstQuad.quadType() != GrQuad::Type::kAxisAligned) {
64 return true;
65 }
66
67 SkRect srcRect;
68 SkRect dstRect;
69 if (srcQuad.asRect(&srcRect) && dstQuad.asRect(&dstRect)) {
70 // Disable filtering when there is no scaling (width and height are the same), and the
71 // top-left corners have the same fraction (so src and dst snap to the pixel grid
72 // identically).
73 SkASSERT(srcRect.isSorted());
74 return srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height() ||
75 SkScalarFraction(srcRect.fLeft) != SkScalarFraction(dstRect.fLeft) ||
76 SkScalarFraction(srcRect.fTop) != SkScalarFraction(dstRect.fTop);
77 } else {
78 // Although the quads are axis-aligned, the local coordinate system is transformed such
79 // that fractionally-aligned sample centers will not align with the device coordinate system
80 // So disable filtering when edges are the same length and both srcQuad and dstQuad
81 // 0th vertex is integer aligned.
82 if (SkScalarIsInt(srcQuad.x(0)) && SkScalarIsInt(srcQuad.y(0)) &&
83 SkScalarIsInt(dstQuad.x(0)) && SkScalarIsInt(dstQuad.y(0))) {
84 // Extract edge lengths
85 SkSize srcSize = axis_aligned_quad_size(srcQuad);
86 SkSize dstSize = axis_aligned_quad_size(dstQuad);
87 return srcSize.fWidth != dstSize.fWidth || srcSize.fHeight != dstSize.fHeight;
88 } else {
89 return true;
90 }
91 }
92}
93
Brian Salomon246bc3d2018-12-06 15:33:02 -050094// if normalizing the domain then pass 1/width, 1/height, 1 for iw, ih, h. Otherwise pass
95// 1, 1, and height.
Michael Ludwigf339dfe2019-06-27 10:41:28 -040096static void compute_domain(Domain domain, GrSamplerState::Filter filter, GrSurfaceOrigin origin,
97 const SkRect& domainRect, float iw, float ih, float h, SkRect* out) {
Brian Salomon246bc3d2018-12-06 15:33:02 -050098 static constexpr SkRect kLargeRect = {-100000, -100000, 1000000, 1000000};
Michael Ludwig460eb5e2018-10-29 11:09:29 -040099 if (domain == Domain::kNo) {
100 // Either the quad has no domain constraint and is batched with a domain constrained op
101 // (in which case we want a domain that doesn't restrict normalized tex coords), or the
102 // entire op doesn't use the domain, in which case the returned value is ignored.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400103 *out = kLargeRect;
104 return;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400105 }
106
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400107 auto ltrb = Sk4f::Load(&domainRect);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400108 if (filter == GrSamplerState::Filter::kBilerp) {
109 auto rblt = SkNx_shuffle<2, 3, 0, 1>(ltrb);
110 auto whwh = (rblt - ltrb).abs();
111 auto c = (rblt + ltrb) * 0.5f;
112 static const Sk4f kOffsets = {0.5f, 0.5f, -0.5f, -0.5f};
113 ltrb = (whwh < 1.f).thenElse(c, ltrb + kOffsets);
114 }
115 ltrb *= Sk4f(iw, ih, iw, ih);
116 if (origin == kBottomLeft_GrSurfaceOrigin) {
117 static const Sk4f kMul = {1.f, -1.f, 1.f, -1.f};
Brian Salomon246bc3d2018-12-06 15:33:02 -0500118 const Sk4f kAdd = {0.f, h, 0.f, h};
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400119 ltrb = SkNx_shuffle<0, 3, 2, 1>(kMul * ltrb + kAdd);
120 }
121
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400122 ltrb.store(out);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400123}
124
Michael Ludwig009b92e2019-02-15 16:03:53 -0500125// Normalizes logical src coords and corrects for origin
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400126static void compute_src_quad(GrSurfaceOrigin origin, const GrQuad& srcQuad,
127 float iw, float ih, float h, GrQuad* out) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500128 // The src quad should not have any perspective
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400129 SkASSERT(!srcQuad.hasPerspective() && !out->hasPerspective());
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400130 skvx::Vec<4, float> xs = srcQuad.x4f() * iw;
131 skvx::Vec<4, float> ys = srcQuad.y4f() * ih;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500132 if (origin == kBottomLeft_GrSurfaceOrigin) {
133 ys = h - ys;
134 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400135 xs.store(out->xs());
136 ys.store(out->ys());
137 out->setQuadType(srcQuad.quadType());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500138}
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400139
Brian Salomon34169692017-08-28 15:32:01 -0400140/**
141 * Op that implements GrTextureOp::Make. It draws textured quads. Each quad can modulate against a
142 * the texture by color. The blend with the destination is always src-over. The edges are non-AA.
143 */
144class TextureOp final : public GrMeshDrawOp {
145public:
Robert Phillipsb97da532019-02-12 15:24:12 -0500146 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400147 sk_sp<GrTextureProxy> proxy,
Michael Ludwig22429f92019-06-27 10:44:48 -0400148 sk_sp<GrColorSpaceXform> textureXform,
Robert Phillips7c525e62018-06-12 10:11:12 -0400149 GrSamplerState::Filter filter,
Brian Osman3d139a42018-11-19 10:42:10 -0500150 const SkPMColor4f& color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400151 GrTextureOp::Saturate saturate,
Robert Phillips7c525e62018-06-12 10:11:12 -0400152 GrAAType aaType,
Brian Salomon2213ee92018-10-02 10:44:21 -0400153 GrQuadAAFlags aaFlags,
Michael Ludwig22429f92019-06-27 10:44:48 -0400154 const GrQuad& deviceQuad,
155 const GrQuad& localQuad,
156 const SkRect* domain) {
Michael Ludwig009b92e2019-02-15 16:03:53 -0500157 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400158 return pool->allocate<TextureOp>(std::move(proxy), std::move(textureXform), filter, color,
159 saturate, aaType, aaFlags, deviceQuad, localQuad, domain);
Brian Salomon34169692017-08-28 15:32:01 -0400160 }
Robert Phillipsb97da532019-02-12 15:24:12 -0500161 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Brian Salomond7065e72018-10-12 11:42:02 -0400162 const GrRenderTargetContext::TextureSetEntry set[],
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400163 int cnt,
164 GrSamplerState::Filter filter,
165 GrTextureOp::Saturate saturate,
166 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400167 SkCanvas::SrcRectConstraint constraint,
Brian Salomond003d222018-11-26 13:25:05 -0500168 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500169 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400170 size_t size = sizeof(TextureOp) + sizeof(Proxy) * (cnt - 1);
Robert Phillips9da87e02019-02-04 13:26:26 -0500171 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Brian Salomond7065e72018-10-12 11:42:02 -0400172 void* mem = pool->allocate(size);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400173 return std::unique_ptr<GrDrawOp>(new (mem) TextureOp(set, cnt, filter, saturate, aaType,
174 constraint, viewMatrix,
175 std::move(textureColorSpaceXform)));
Brian Salomond7065e72018-10-12 11:42:02 -0400176 }
Brian Salomon34169692017-08-28 15:32:01 -0400177
Brian Salomon336ce7b2017-09-08 08:23:58 -0400178 ~TextureOp() override {
Brian Salomond7065e72018-10-12 11:42:02 -0400179 for (unsigned p = 0; p < fProxyCnt; ++p) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400180 fProxies[p].fProxy->unref();
Brian Salomon336ce7b2017-09-08 08:23:58 -0400181 }
182 }
Brian Salomon34169692017-08-28 15:32:01 -0400183
184 const char* name() const override { return "TextureOp"; }
185
Chris Dalton1706cbf2019-05-21 19:35:29 -0600186 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomond7065e72018-10-12 11:42:02 -0400187 for (unsigned p = 0; p < fProxyCnt; ++p) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600188 bool mipped = (GrSamplerState::Filter::kMipMap == this->filter());
189 func(fProxies[p].fProxy, GrMipMapped(mipped));
Brian Salomond7065e72018-10-12 11:42:02 -0400190 }
191 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400192
Brian Osman9a390ac2018-11-12 09:47:48 -0500193#ifdef SK_DEBUG
Brian Salomon34169692017-08-28 15:32:01 -0400194 SkString dumpInfo() const override {
195 SkString str;
Brian Salomond7065e72018-10-12 11:42:02 -0400196 str.appendf("# draws: %d\n", fQuads.count());
Michael Ludwig425eb452019-06-27 10:13:27 -0400197 auto iter = fQuads.iterator();
Brian Salomond7065e72018-10-12 11:42:02 -0400198 for (unsigned p = 0; p < fProxyCnt; ++p) {
199 str.appendf("Proxy ID: %d, Filter: %d\n", fProxies[p].fProxy->uniqueID().asUInt(),
200 static_cast<int>(fFilter));
Michael Ludwig425eb452019-06-27 10:13:27 -0400201 int i = 0;
202 while(i < fProxies[p].fQuadCnt && iter.next()) {
203 const GrQuad& quad = iter.deviceQuad();
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400204 const GrQuad& uv = iter.localQuad();
Michael Ludwig425eb452019-06-27 10:13:27 -0400205 const ColorDomainAndAA& info = iter.metadata();
Brian Salomond7065e72018-10-12 11:42:02 -0400206 str.appendf(
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400207 "%d: Color: 0x%08x, Domain(%d): [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n"
208 " UVs [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n"
209 " Quad [(%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)]\n",
210 i, info.fColor.toBytes_RGBA(), info.fHasDomain, info.fDomainRect.fLeft,
211 info.fDomainRect.fTop, info.fDomainRect.fRight, info.fDomainRect.fBottom,
212 quad.point(0).fX, quad.point(0).fY, quad.point(1).fX, quad.point(1).fY,
213 quad.point(2).fX, quad.point(2).fY, quad.point(3).fX, quad.point(3).fY,
214 uv.point(0).fX, uv.point(0).fY, uv.point(1).fX, uv.point(1).fY,
215 uv.point(2).fX, uv.point(2).fY, uv.point(3).fX, uv.point(3).fY);
216
Michael Ludwig425eb452019-06-27 10:13:27 -0400217 i++;
Brian Salomond7065e72018-10-12 11:42:02 -0400218 }
Brian Salomon34169692017-08-28 15:32:01 -0400219 }
220 str += INHERITED::dumpInfo();
221 return str;
222 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500223#endif
Brian Salomon34169692017-08-28 15:32:01 -0400224
Brian Osman5ced0bf2019-03-15 10:15:29 -0400225 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600226 const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage,
227 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400228 fColorType = static_cast<unsigned>(ColorType::kNone);
Michael Ludwig425eb452019-06-27 10:13:27 -0400229 auto iter = fQuads.metadata();
230 while(iter.next()) {
231 auto colorType = GrQuadPerEdgeAA::MinColorType(iter->fColor, clampType, caps);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400232 fColorType = SkTMax(fColorType, static_cast<unsigned>(colorType));
233 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700234 return GrProcessorSet::EmptySetAnalysis();
Brian Salomon34169692017-08-28 15:32:01 -0400235 }
236
Brian Salomon485b8c62018-01-12 15:11:06 -0500237 FixedFunctionFlags fixedFunctionFlags() const override {
238 return this->aaType() == GrAAType::kMSAA ? FixedFunctionFlags::kUsesHWAA
239 : FixedFunctionFlags::kNone;
240 }
Brian Salomon34169692017-08-28 15:32:01 -0400241
242 DEFINE_OP_CLASS_ID
243
244private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400245 friend class ::GrOpMemoryPool;
Brian Salomon762d5e72017-12-01 10:25:08 -0500246
Michael Ludwig425eb452019-06-27 10:13:27 -0400247 struct ColorDomainAndAA {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400248 ColorDomainAndAA(const SkPMColor4f& color, const SkRect* domainRect, GrQuadAAFlags aaFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -0400249 : fColor(color)
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400250 , fDomainRect(domainRect ? *domainRect : SkRect::MakeEmpty())
251 , fHasDomain(static_cast<unsigned>(domainRect ? Domain::kYes : Domain::kNo))
Michael Ludwig425eb452019-06-27 10:13:27 -0400252 , fAAFlags(static_cast<unsigned>(aaFlags)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400253 SkASSERT(fAAFlags == static_cast<unsigned>(aaFlags));
254 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400255
256 SkPMColor4f fColor;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400257 SkRect fDomainRect;
Michael Ludwig425eb452019-06-27 10:13:27 -0400258 unsigned fHasDomain : 1;
259 unsigned fAAFlags : 4;
260
261 Domain domain() const { return Domain(fHasDomain); }
262 GrQuadAAFlags aaFlags() const { return static_cast<GrQuadAAFlags>(fAAFlags); }
263 };
264 struct Proxy {
265 GrTextureProxy* fProxy;
266 int fQuadCnt;
267 };
268
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400269 // dstQuad should be the geometry transformed by the view matrix. If domainRect
270 // is not null it will be used to apply the strict src rect constraint.
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400271 TextureOp(sk_sp<GrTextureProxy> proxy,
272 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
273 GrSamplerState::Filter filter,
274 const SkPMColor4f& color,
275 GrTextureOp::Saturate saturate,
276 GrAAType aaType,
277 GrQuadAAFlags aaFlags,
278 const GrQuad& dstQuad,
279 const GrQuad& srcQuad,
280 const SkRect* domainRect)
Brian Salomon34169692017-08-28 15:32:01 -0400281 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400282 , fQuads(1, true /* includes locals */)
Brian Osman3ebd3542018-07-30 14:36:53 -0400283 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400284 , fSaturate(static_cast<unsigned>(saturate))
Robert Phillips3d4cac52019-06-11 08:08:08 -0400285 , fFilter(static_cast<unsigned>(filter)) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400286 // Clean up disparities between the overall aa type and edge configuration and apply
287 // optimizations based on the rect and matrix when appropriate
Michael Ludwig0f809022019-06-04 09:14:37 -0400288 GrQuadUtils::ResolveAAType(aaType, aaFlags, dstQuad, &aaType, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400289 fAAType = static_cast<unsigned>(aaType);
290
Brian Salomonf1709042018-10-03 11:57:00 -0400291 // We expect our caller to have already caught this optimization.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400292 SkASSERT(!domainRect || !domainRect->contains(proxy->getWorstCaseBoundsRect()));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500293
Brian Salomonf09abc52018-10-03 15:59:04 -0400294 // We may have had a strict constraint with nearest filter solely due to possible AA bloat.
295 // If we don't have (or determined we don't need) coverage AA then we can skip using a
296 // domain.
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400297 if (domainRect && this->filter() == GrSamplerState::Filter::kNearest &&
Michael Ludwig6bee7762018-10-19 09:50:36 -0400298 aaType != GrAAType::kCoverage) {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400299 domainRect = nullptr;
Brian Salomonf09abc52018-10-03 15:59:04 -0400300 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500301
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400302 fQuads.append(dstQuad, {color, domainRect, aaFlags}, &srcQuad);
Michael Ludwig425eb452019-06-27 10:13:27 -0400303
Brian Salomond7065e72018-10-12 11:42:02 -0400304 fProxyCnt = 1;
305 fProxies[0] = {proxy.release(), 1};
Michael Ludwig41f395d2019-05-23 13:59:45 -0400306 this->setBounds(dstQuad.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
307 IsZeroArea::kNo);
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400308 fDomain = static_cast<unsigned>(domainRect != nullptr);
Brian Salomond7065e72018-10-12 11:42:02 -0400309 }
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400310 TextureOp(const GrRenderTargetContext::TextureSetEntry set[],
311 int cnt,
312 GrSamplerState::Filter filter,
313 GrTextureOp::Saturate saturate,
314 GrAAType aaType,
315 SkCanvas::SrcRectConstraint constraint,
316 const SkMatrix& viewMatrix,
Brian Salomond003d222018-11-26 13:25:05 -0500317 sk_sp<GrColorSpaceXform> textureColorSpaceXform)
Brian Salomond7065e72018-10-12 11:42:02 -0400318 : INHERITED(ClassID())
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400319 , fQuads(cnt, true /* includes locals */)
Brian Salomond7065e72018-10-12 11:42:02 -0400320 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400321 , fSaturate(static_cast<unsigned>(saturate))
Robert Phillips3d4cac52019-06-11 08:08:08 -0400322 , fFilter(static_cast<unsigned>(filter)) {
Brian Salomond7065e72018-10-12 11:42:02 -0400323 fProxyCnt = SkToUInt(cnt);
324 SkRect bounds = SkRectPriv::MakeLargestInverted();
Michael Ludwig6bee7762018-10-19 09:50:36 -0400325 GrAAType overallAAType = GrAAType::kNone; // aa type maximally compatible with all dst rects
Brian Salomon0087c832018-10-15 14:48:20 -0400326 bool mustFilter = false;
Brian Salomon1d835422019-03-13 16:11:44 -0400327 bool allOpaque = true;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400328 Domain netDomain = Domain::kNo;
Brian Salomond7065e72018-10-12 11:42:02 -0400329 for (unsigned p = 0; p < fProxyCnt; ++p) {
330 fProxies[p].fProxy = SkRef(set[p].fProxy.get());
331 fProxies[p].fQuadCnt = 1;
332 SkASSERT(fProxies[p].fProxy->textureType() == fProxies[0].fProxy->textureType());
333 SkASSERT(fProxies[p].fProxy->config() == fProxies[0].fProxy->config());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500334
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500335 SkMatrix ctm = viewMatrix;
336 if (set[p].fPreViewMatrix) {
337 ctm.preConcat(*set[p].fPreViewMatrix);
338 }
339
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400340 // Use dstRect/srcRect unless dstClip is provided, in which case derive new source
341 // coordinates by mapping dstClipQuad by the dstRect to srcRect transform.
342 GrQuad quad, srcQuad;
343 if (set[p].fDstClipQuad) {
344 quad = GrQuad::MakeFromSkQuad(set[p].fDstClipQuad, ctm);
345
346 SkPoint srcPts[4];
347 GrMapRectPoints(set[p].fDstRect, set[p].fSrcRect, set[p].fDstClipQuad, srcPts, 4);
348 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
349 } else {
350 quad = GrQuad::MakeFromRect(set[p].fDstRect, ctm);
351 srcQuad = GrQuad(set[p].fSrcRect);
352 }
Michael Ludwigce62dec2019-02-19 11:48:46 -0500353
Michael Ludwig22429f92019-06-27 10:44:48 -0400354 if (!mustFilter && this->filter() != GrSamplerState::Filter::kNearest) {
355 mustFilter = filter_has_effect(srcQuad, quad);
356 }
357
Michael Ludwig41f395d2019-05-23 13:59:45 -0400358 bounds.joinPossiblyEmptyRect(quad.bounds());
Michael Ludwig6bee7762018-10-19 09:50:36 -0400359 GrQuadAAFlags aaFlags;
360 // Don't update the overall aaType, might be inappropriate for some of the quads
361 GrAAType aaForQuad;
Michael Ludwig0f809022019-06-04 09:14:37 -0400362 GrQuadUtils::ResolveAAType(aaType, set[p].fAAFlags, quad, &aaForQuad, &aaFlags);
Michael Ludwig6bee7762018-10-19 09:50:36 -0400363 // Resolve sets aaForQuad to aaType or None, there is never a change between aa methods
364 SkASSERT(aaForQuad == GrAAType::kNone || aaForQuad == aaType);
365 if (overallAAType == GrAAType::kNone && aaForQuad != GrAAType::kNone) {
366 overallAAType = aaType;
Brian Salomond7065e72018-10-12 11:42:02 -0400367 }
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400368
369 // Calculate metadata for the entry
370 const SkRect* domainForQuad = nullptr;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400371 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
372 // Check (briefly) if the strict constraint is needed for this set entry
373 if (!set[p].fSrcRect.contains(fProxies[p].fProxy->getWorstCaseBoundsRect()) &&
374 (mustFilter || aaForQuad == GrAAType::kCoverage)) {
375 // Can't rely on hardware clamping and the draw will access outer texels
376 // for AA and/or bilerp
377 netDomain = Domain::kYes;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400378 domainForQuad = &set[p].fSrcRect;
Michael Ludwig31ba7182019-04-03 10:38:06 -0400379 }
380 }
Brian Salomond003d222018-11-26 13:25:05 -0500381 float alpha = SkTPin(set[p].fAlpha, 0.f, 1.f);
Brian Salomon1d835422019-03-13 16:11:44 -0400382 allOpaque &= (1.f == alpha);
Brian Salomond003d222018-11-26 13:25:05 -0500383 SkPMColor4f color{alpha, alpha, alpha, alpha};
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400384 fQuads.append(quad, {color, domainForQuad, aaFlags}, &srcQuad);
Brian Salomond7065e72018-10-12 11:42:02 -0400385 }
Michael Ludwig6bee7762018-10-19 09:50:36 -0400386 fAAType = static_cast<unsigned>(overallAAType);
Brian Salomon0087c832018-10-15 14:48:20 -0400387 if (!mustFilter) {
388 fFilter = static_cast<unsigned>(GrSamplerState::Filter::kNearest);
389 }
Brian Salomond7065e72018-10-12 11:42:02 -0400390 this->setBounds(bounds, HasAABloat(this->aaType() == GrAAType::kCoverage), IsZeroArea::kNo);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400391 fDomain = static_cast<unsigned>(netDomain);
Brian Salomon34169692017-08-28 15:32:01 -0400392 }
393
Michael Ludwig425eb452019-06-27 10:13:27 -0400394 void tess(void* v, const VertexSpec& spec, const GrTextureProxy* proxy,
395 GrQuadBuffer<ColorDomainAndAA>::Iter* iter, int cnt) const {
Brian Salomon5f394272019-07-02 14:07:49 -0400396 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond7065e72018-10-12 11:42:02 -0400397 auto origin = proxy->origin();
398 const auto* texture = proxy->peekTexture();
Brian Salomon246bc3d2018-12-06 15:33:02 -0500399 float iw, ih, h;
400 if (proxy->textureType() == GrTextureType::kRectangle) {
401 iw = ih = 1.f;
402 h = texture->height();
403 } else {
404 iw = 1.f / texture->width();
405 ih = 1.f / texture->height();
406 h = 1.f;
407 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000408
Michael Ludwig425eb452019-06-27 10:13:27 -0400409 int i = 0;
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400410 // Explicit ctor ensures ws are 1s, which compute_src_quad requires
411 GrQuad srcQuad(SkRect::MakeEmpty());
412 SkRect domain;
Michael Ludwig425eb452019-06-27 10:13:27 -0400413 while(i < cnt && iter->next()) {
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400414 SkASSERT(iter->isLocalValid());
Michael Ludwig425eb452019-06-27 10:13:27 -0400415 const ColorDomainAndAA& info = iter->metadata();
Michael Ludwigf339dfe2019-06-27 10:41:28 -0400416 // Must correct the texture coordinates and domain now that the real texture size
417 // is known
418 compute_src_quad(origin, iter->localQuad(), iw, ih, h, &srcQuad);
419 compute_domain(info.domain(), this->filter(), origin, info.fDomainRect, iw, ih, h,
420 &domain);
Michael Ludwig425eb452019-06-27 10:13:27 -0400421 v = GrQuadPerEdgeAA::Tessellate(v, spec, iter->deviceQuad(), info.fColor, srcQuad,
422 domain, info.aaFlags());
423 i++;
Brian Salomon17031a72018-05-22 14:14:07 -0400424 }
425 }
426
Brian Salomon34169692017-08-28 15:32:01 -0400427 void onPrepareDraws(Target* target) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400428 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400429 GrQuad::Type quadType = GrQuad::Type::kAxisAligned;
430 GrQuad::Type srcQuadType = GrQuad::Type::kAxisAligned;
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();
Greg Daniel2c3398d2019-06-19 11:58:01 -0400436 const GrSwizzle& swizzle = fProxies[0].fProxy->textureSwizzle();
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 Ludwig425eb452019-06-27 10:13:27 -0400439 if (op.fQuads.deviceQuadType() > quadType) {
440 quadType = op.fQuads.deviceQuadType();
Michael Ludwigf995c052018-11-26 15:24:29 -0500441 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400442 if (op.fQuads.localQuadType() > srcQuadType) {
443 srcQuadType = op.fQuads.localQuadType();
Michael Ludwig009b92e2019-02-15 16:03:53 -0500444 }
Brian Salomonf7232642018-09-19 08:58:08 -0400445 if (op.fDomain) {
446 domain = Domain::kYes;
447 }
Brian Salomon1d835422019-03-13 16:11:44 -0400448 colorType = SkTMax(colorType, static_cast<ColorType>(op.fColorType));
Brian Salomond7065e72018-10-12 11:42:02 -0400449 numProxies += op.fProxyCnt;
450 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
Brian Salomon4b8178f2018-10-12 13:18:27 -0400451 numTotalQuads += op.fProxies[p].fQuadCnt;
Brian Salomond7065e72018-10-12 11:42:02 -0400452 auto* proxy = op.fProxies[p].fProxy;
Robert Phillips12c46292019-04-23 07:36:17 -0400453 if (!proxy->isInstantiated()) {
Brian Salomond7065e72018-10-12 11:42:02 -0400454 return;
455 }
Brian Salomond7065e72018-10-12 11:42:02 -0400456 SkASSERT(proxy->textureType() == textureType);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400457 SkASSERT(proxy->textureSwizzle() == swizzle);
Brian Salomonf7232642018-09-19 08:58:08 -0400458 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400459 if (op.aaType() == GrAAType::kCoverage) {
460 SkASSERT(aaType == GrAAType::kCoverage || aaType == GrAAType::kNone);
461 aaType = GrAAType::kCoverage;
462 }
Brian Salomon34169692017-08-28 15:32:01 -0400463 }
Brian Salomon336ce7b2017-09-08 08:23:58 -0400464
Brian Salomon1d835422019-03-13 16:11:44 -0400465 VertexSpec vertexSpec(quadType, colorType, srcQuadType, /* hasLocal */ true, domain, aaType,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500466 /* alpha as coverage */ true);
Michael Ludwigc182b942018-11-16 10:27:51 -0500467
Greg Daniel7a82edf2018-12-04 10:54:34 -0500468 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
469 this->filter());
470 GrGpu* gpu = target->resourceProvider()->priv().gpu();
471 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
472 samplerState, fProxies[0].fProxy->backendFormat());
473
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400474 auto saturate = static_cast<GrTextureOp::Saturate>(fSaturate);
Michael Ludwig467994d2018-12-03 14:58:31 +0000475 sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeTexturedProcessor(
Brian Salomon67529b22019-08-13 15:31:04 -0400476 vertexSpec, *target->caps().shaderCaps(), textureType, samplerState, swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400477 extraSamplerKey, std::move(fTextureColorSpaceXform), saturate);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500478
Brian Salomonf7232642018-09-19 08:58:08 -0400479 // We'll use a dynamic state array for the GP textures when there are multiple ops.
480 // Otherwise, we use fixed dynamic state to specify the single op's proxy.
481 GrPipeline::DynamicStateArrays* dynamicStateArrays = nullptr;
482 GrPipeline::FixedDynamicState* fixedDynamicState;
Brian Salomond7065e72018-10-12 11:42:02 -0400483 if (numProxies > 1) {
484 dynamicStateArrays = target->allocDynamicStateArrays(numProxies, 1, false);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700485 fixedDynamicState = target->makeFixedDynamicState(0);
Brian Salomonf7232642018-09-19 08:58:08 -0400486 } else {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700487 fixedDynamicState = target->makeFixedDynamicState(1);
Brian Salomond7065e72018-10-12 11:42:02 -0400488 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxies[0].fProxy;
Brian Salomonf7232642018-09-19 08:58:08 -0400489 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400490
Michael Ludwigc182b942018-11-16 10:27:51 -0500491 size_t vertexSize = gp->vertexStride();
Brian Salomon92be2f72018-06-19 14:33:47 -0400492
Brian Salomond7065e72018-10-12 11:42:02 -0400493 GrMesh* meshes = target->allocMeshes(numProxies);
Brian Salomon12d22642019-01-29 14:38:50 -0500494 sk_sp<const GrBuffer> vbuffer;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400495 int vertexOffsetInBuffer = 0;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500496 int numQuadVerticesLeft = numTotalQuads * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400497 int numAllocatedVertices = 0;
498 void* vdata = nullptr;
499
Brian Salomond7065e72018-10-12 11:42:02 -0400500 int m = 0;
Brian Salomonf7232642018-09-19 08:58:08 -0400501 for (const auto& op : ChainRange<TextureOp>(this)) {
Michael Ludwig425eb452019-06-27 10:13:27 -0400502 auto iter = op.fQuads.iterator();
Brian Salomond7065e72018-10-12 11:42:02 -0400503 for (unsigned p = 0; p < op.fProxyCnt; ++p) {
504 int quadCnt = op.fProxies[p].fQuadCnt;
505 auto* proxy = op.fProxies[p].fProxy;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500506 int meshVertexCnt = quadCnt * vertexSpec.verticesPerQuad();
Brian Salomon4b8178f2018-10-12 13:18:27 -0400507 if (numAllocatedVertices < meshVertexCnt) {
508 vdata = target->makeVertexSpaceAtLeast(
509 vertexSize, meshVertexCnt, numQuadVerticesLeft, &vbuffer,
510 &vertexOffsetInBuffer, &numAllocatedVertices);
511 SkASSERT(numAllocatedVertices <= numQuadVerticesLeft);
512 if (!vdata) {
513 SkDebugf("Could not allocate vertices\n");
514 return;
515 }
Brian Salomonf7232642018-09-19 08:58:08 -0400516 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400517 SkASSERT(numAllocatedVertices >= meshVertexCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400518
Michael Ludwig425eb452019-06-27 10:13:27 -0400519 op.tess(vdata, vertexSpec, proxy, &iter, quadCnt);
Brian Salomond7065e72018-10-12 11:42:02 -0400520
Michael Ludwig93aeba02018-12-21 09:50:31 -0500521 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, &(meshes[m]), vertexSpec,
522 quadCnt)) {
523 SkDebugf("Could not allocate indices");
524 return;
Brian Salomond7065e72018-10-12 11:42:02 -0400525 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400526 meshes[m].setVertexData(vbuffer, vertexOffsetInBuffer);
Brian Salomond7065e72018-10-12 11:42:02 -0400527 if (dynamicStateArrays) {
528 dynamicStateArrays->fPrimitiveProcessorTextures[m] = proxy;
529 }
530 ++m;
Brian Salomon4b8178f2018-10-12 13:18:27 -0400531 numAllocatedVertices -= meshVertexCnt;
532 numQuadVerticesLeft -= meshVertexCnt;
533 vertexOffsetInBuffer += meshVertexCnt;
534 vdata = reinterpret_cast<char*>(vdata) + vertexSize * meshVertexCnt;
Brian Salomonf7232642018-09-19 08:58:08 -0400535 }
Michael Ludwig425eb452019-06-27 10:13:27 -0400536 // If quad counts per proxy were calculated correctly, the entire iterator should have
537 // been consumed.
538 SkASSERT(!iter.next());
Brian Salomon34169692017-08-28 15:32:01 -0400539 }
Brian Salomon4b8178f2018-10-12 13:18:27 -0400540 SkASSERT(!numQuadVerticesLeft);
541 SkASSERT(!numAllocatedVertices);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700542 target->recordDraw(
543 std::move(gp), meshes, numProxies, fixedDynamicState, dynamicStateArrays);
544 }
545
546 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
547 auto pipelineFlags = (GrAAType::kMSAA == this->aaType())
Chris Daltonbaa1b352019-04-03 12:03:00 -0600548 ? GrPipeline::InputFlags::kHWAntialias
549 : GrPipeline::InputFlags::kNone;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700550 flushState->executeDrawsAndUploadsForMeshDrawOp(
551 this, chainBounds, GrProcessorSet::MakeEmptySet(), pipelineFlags);
Brian Salomon34169692017-08-28 15:32:01 -0400552 }
553
Brian Salomonf7232642018-09-19 08:58:08 -0400554 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400555 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon34169692017-08-28 15:32:01 -0400556 const auto* that = t->cast<TextureOp>();
Michael Ludwig2929f512019-04-19 13:05:56 -0400557 if (fDomain != that->fDomain) {
558 // It is technically possible to combine operations across domain modes, but performance
559 // testing suggests it's better to make more draw calls where some take advantage of
560 // the more optimal shader path without coordinate clamping.
561 return CombineResult::kCannotCombine;
562 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400563 if (!GrColorSpaceXform::Equals(fTextureColorSpaceXform.get(),
564 that->fTextureColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000565 return CombineResult::kCannotCombine;
Brian Osman3ebd3542018-07-30 14:36:53 -0400566 }
Brian Salomonae7d7702018-10-14 15:05:45 -0400567 bool upgradeToCoverageAAOnMerge = false;
Brian Salomon485b8c62018-01-12 15:11:06 -0500568 if (this->aaType() != that->aaType()) {
Brian Salomonae7d7702018-10-14 15:05:45 -0400569 if (!((this->aaType() == GrAAType::kCoverage && that->aaType() == GrAAType::kNone) ||
570 (that->aaType() == GrAAType::kCoverage && this->aaType() == GrAAType::kNone))) {
571 return CombineResult::kCannotCombine;
572 }
573 upgradeToCoverageAAOnMerge = true;
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500574 }
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400575 if (fSaturate != that->fSaturate) {
576 return CombineResult::kCannotCombine;
577 }
Brian Salomonf7232642018-09-19 08:58:08 -0400578 if (fFilter != that->fFilter) {
579 return CombineResult::kCannotCombine;
580 }
Brian Salomond7065e72018-10-12 11:42:02 -0400581 auto thisProxy = fProxies[0].fProxy;
582 auto thatProxy = that->fProxies[0].fProxy;
583 if (fProxyCnt > 1 || that->fProxyCnt > 1 ||
Brian Salomon588cec72018-11-14 13:56:37 -0500584 thisProxy->uniqueID() != thatProxy->uniqueID()) {
585 // We can't merge across different proxies. Check if 'this' can be chained with 'that'.
Greg Daniel45723ac2018-11-30 10:12:43 -0500586 if (GrTextureProxy::ProxiesAreCompatibleAsDynamicState(thisProxy, thatProxy) &&
Brian Salomonf7232642018-09-19 08:58:08 -0400587 caps.dynamicStateArrayGeometryProcessorTextureSupport()) {
588 return CombineResult::kMayChain;
589 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000590 return CombineResult::kCannotCombine;
Brian Salomon336ce7b2017-09-08 08:23:58 -0400591 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500592
Brian Salomonb80ffee2018-05-23 16:39:39 -0400593 fDomain |= that->fDomain;
Brian Salomon1d835422019-03-13 16:11:44 -0400594 fColorType = SkTMax(fColorType, that->fColorType);
Brian Salomonae7d7702018-10-14 15:05:45 -0400595 if (upgradeToCoverageAAOnMerge) {
596 fAAType = static_cast<unsigned>(GrAAType::kCoverage);
597 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500598
Michael Ludwig425eb452019-06-27 10:13:27 -0400599 // Concatenate quad lists together
Michael Ludwig009b92e2019-02-15 16:03:53 -0500600 fQuads.concat(that->fQuads);
601 fProxies[0].fQuadCnt += that->fQuads.count();
602
Brian Salomon7eae3e02018-08-07 14:02:38 +0000603 return CombineResult::kMerged;
Brian Salomon34169692017-08-28 15:32:01 -0400604 }
605
Brian Salomon485b8c62018-01-12 15:11:06 -0500606 GrAAType aaType() const { return static_cast<GrAAType>(fAAType); }
Brian Salomon0087c832018-10-15 14:48:20 -0400607 GrSamplerState::Filter filter() const { return static_cast<GrSamplerState::Filter>(fFilter); }
Brian Salomonb5ef1f92018-01-11 11:46:21 -0500608
Michael Ludwig425eb452019-06-27 10:13:27 -0400609 GrQuadBuffer<ColorDomainAndAA> fQuads;
Brian Osman3ebd3542018-07-30 14:36:53 -0400610 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400611 unsigned fSaturate : 1;
Brian Salomon0087c832018-10-15 14:48:20 -0400612 unsigned fFilter : 2;
Brian Salomon485b8c62018-01-12 15:11:06 -0500613 unsigned fAAType : 2;
Brian Salomonb80ffee2018-05-23 16:39:39 -0400614 unsigned fDomain : 1;
Brian Salomon1d835422019-03-13 16:11:44 -0400615 unsigned fColorType : 2;
616 GR_STATIC_ASSERT(GrQuadPerEdgeAA::kColorTypeCount <= 4);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400617 unsigned fProxyCnt : 32 - 8;
Brian Salomond7065e72018-10-12 11:42:02 -0400618 Proxy fProxies[1];
Brian Salomon336ce7b2017-09-08 08:23:58 -0400619
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400620 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type does not fit in 2 bits");
Michael Ludwigf995c052018-11-26 15:24:29 -0500621
Brian Salomon34169692017-08-28 15:32:01 -0400622 typedef GrMeshDrawOp INHERITED;
623};
624
625} // anonymous namespace
626
627namespace GrTextureOp {
628
Michael Ludwig205224f2019-06-27 10:47:42 -0400629std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
630 sk_sp<GrTextureProxy> proxy,
631 sk_sp<GrColorSpaceXform> textureXform,
632 GrSamplerState::Filter filter,
633 const SkPMColor4f& color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400634 Saturate saturate,
Michael Ludwig205224f2019-06-27 10:47:42 -0400635 SkBlendMode blendMode,
636 GrAAType aaType,
637 GrQuadAAFlags aaFlags,
638 const GrQuad& deviceQuad,
639 const GrQuad& localQuad,
640 const SkRect* domain) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400641 // Apply optimizations that are valid whether or not using GrTextureOp or GrFillRectOp
642 if (domain && domain->contains(proxy->getWorstCaseBoundsRect())) {
643 // No need for a shader-based domain if hardware clamping achieves the same effect
644 domain = nullptr;
645 }
646
647 if (filter != GrSamplerState::Filter::kNearest && !filter_has_effect(localQuad, deviceQuad)) {
648 filter = GrSamplerState::Filter::kNearest;
649 }
650
651 if (blendMode == SkBlendMode::kSrcOver) {
652 return TextureOp::Make(context, std::move(proxy), std::move(textureXform), filter, color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400653 saturate, aaType, aaFlags, deviceQuad, localQuad, domain);
Michael Ludwig22429f92019-06-27 10:44:48 -0400654 } else {
655 // Emulate complex blending using GrFillRectOp
656 GrPaint paint;
657 paint.setColor4f(color);
658 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
659
660 std::unique_ptr<GrFragmentProcessor> fp;
661 if (domain) {
662 // Update domain to match what GrTextureOp computes during tessellation, using top-left
663 // as the origin so that it doesn't depend on final texture size (which the FP handles
664 // later, as well as accounting for the true origin).
665 SkRect correctedDomain;
666 compute_domain(Domain::kYes, filter, kTopLeft_GrSurfaceOrigin, *domain,
667 1.f, 1.f, proxy->height(), &correctedDomain);
668 fp = GrTextureDomainEffect::Make(std::move(proxy), SkMatrix::I(), correctedDomain,
669 GrTextureDomain::kClamp_Mode, filter);
670 } else {
671 fp = GrSimpleTextureEffect::Make(std::move(proxy), SkMatrix::I(), filter);
672 }
673 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(textureXform));
674 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400675 if (saturate == GrTextureOp::Saturate::kYes) {
676 paint.addColorFragmentProcessor(GrSaturateProcessor::Make());
677 }
Michael Ludwig22429f92019-06-27 10:44:48 -0400678
679 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
680 deviceQuad, localQuad);
681 }
682}
683
Michael Ludwig009b92e2019-02-15 16:03:53 -0500684std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
685 const GrRenderTargetContext::TextureSetEntry set[],
686 int cnt,
687 GrSamplerState::Filter filter,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400688 Saturate saturate,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500689 GrAAType aaType,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400690 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500691 const SkMatrix& viewMatrix,
692 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400693 return TextureOp::Make(context, set, cnt, filter, saturate, aaType, constraint, viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500694 std::move(textureColorSpaceXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400695}
696
Brian Salomon34169692017-08-28 15:32:01 -0400697} // namespace GrTextureOp
698
699#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500700#include "include/private/GrRecordingContext.h"
701#include "src/gpu/GrProxyProvider.h"
702#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon34169692017-08-28 15:32:01 -0400703
704GR_DRAW_OP_TEST_DEFINE(TextureOp) {
705 GrSurfaceDesc desc;
706 desc.fConfig = kRGBA_8888_GrPixelConfig;
707 desc.fHeight = random->nextULessThan(90) + 10;
708 desc.fWidth = random->nextULessThan(90) + 10;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500709 auto origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel09c94002018-06-08 22:11:51 +0000710 GrMipMapped mipMapped = random->nextBool() ? GrMipMapped::kYes : GrMipMapped::kNo;
711 SkBackingFit fit = SkBackingFit::kExact;
712 if (mipMapped == GrMipMapped::kNo) {
713 fit = random->nextBool() ? SkBackingFit::kApprox : SkBackingFit::kExact;
714 }
Greg Daniel4065d452018-11-16 15:43:41 -0500715 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400716 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
717 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500718
Robert Phillips9da87e02019-02-04 13:26:26 -0500719 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomone8a766b2019-07-19 14:24:36 -0400720 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400721 format, desc, GrRenderable::kNo, 1, origin, mipMapped, fit, SkBudgeted::kNo,
Brian Salomone8a766b2019-07-19 14:24:36 -0400722 GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500723
Brian Salomon34169692017-08-28 15:32:01 -0400724 SkRect rect = GrTest::TestRect(random);
725 SkRect srcRect;
726 srcRect.fLeft = random->nextRangeScalar(0.f, proxy->width() / 2.f);
727 srcRect.fRight = random->nextRangeScalar(0.f, proxy->width()) + proxy->width() / 2.f;
728 srcRect.fTop = random->nextRangeScalar(0.f, proxy->height() / 2.f);
729 srcRect.fBottom = random->nextRangeScalar(0.f, proxy->height()) + proxy->height() / 2.f;
730 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Osman3d139a42018-11-19 10:42:10 -0500731 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU()));
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400732 GrSamplerState::Filter filter = (GrSamplerState::Filter)random->nextULessThan(
733 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
Greg Daniel09c94002018-06-08 22:11:51 +0000734 while (mipMapped == GrMipMapped::kNo && filter == GrSamplerState::Filter::kMipMap) {
735 filter = (GrSamplerState::Filter)random->nextULessThan(
736 static_cast<uint32_t>(GrSamplerState::Filter::kMipMap) + 1);
737 }
Brian Osman3ebd3542018-07-30 14:36:53 -0400738 auto texXform = GrTest::TestColorXform(random);
Brian Salomon485b8c62018-01-12 15:11:06 -0500739 GrAAType aaType = GrAAType::kNone;
740 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600741 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Brian Salomon485b8c62018-01-12 15:11:06 -0500742 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400743 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
744 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
745 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
746 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
747 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
Michael Ludwig205224f2019-06-27 10:47:42 -0400748 bool useDomain = random->nextBool();
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400749 auto saturate = random->nextBool() ? GrTextureOp::Saturate::kYes : GrTextureOp::Saturate::kNo;
Michael Ludwig205224f2019-06-27 10:47:42 -0400750 return GrTextureOp::Make(context, std::move(proxy), std::move(texXform), filter, color,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400751 saturate, SkBlendMode::kSrcOver, aaType, aaFlags,
Michael Ludwig205224f2019-06-27 10:47:42 -0400752 GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(srcRect),
753 useDomain ? &srcRect : nullptr);
Brian Salomon34169692017-08-28 15:32:01 -0400754}
755
756#endif