blob: d25f5427d4db9ee34365a17f390c38521ac214ef [file] [log] [blame]
Robert Phillipse837e612019-11-15 11:02:50 -05001/*
2 * Copyright 2019 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Robert Phillips6d344c32020-07-06 10:56:46 -04007
8#include "include/gpu/GrDirectContext.h"
Michael Ludwigfe13ca32019-11-21 10:26:41 -05009#include "src/core/SkBlendModePriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040010#include "src/gpu/GrDirectContextPriv.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040011#include "src/gpu/GrProxyProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050012#include "src/gpu/GrSurfaceDrawContext.h"
Robert Phillipse837e612019-11-15 11:02:50 -050013#include "src/gpu/ops/GrFillRectOp.h"
14#include "src/gpu/ops/GrTextureOp.h"
15#include "tests/Test.h"
16
Brian Salomoneebe7352020-12-09 16:37:04 -050017static std::unique_ptr<GrSurfaceDrawContext> new_RTC(GrRecordingContext* rContext) {
18 return GrSurfaceDrawContext::Make(
Chris Daltonf5b87f92021-04-19 17:27:09 -060019 rContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {128, 128},
20 SkSurfaceProps());
Robert Phillipse837e612019-11-15 11:02:50 -050021}
22
Robert Phillips089b7c92020-08-12 11:57:07 -040023static sk_sp<GrSurfaceProxy> create_proxy(GrRecordingContext* rContext) {
Brian Salomona56a7462020-02-07 14:17:25 -050024 static constexpr SkISize kDimensions = {128, 128};
Robert Phillipse837e612019-11-15 11:02:50 -050025
Robert Phillipsfe4b4812020-07-17 14:15:51 -040026 const GrBackendFormat format = rContext->priv().caps()->getDefaultBackendFormat(
Robert Phillipse837e612019-11-15 11:02:50 -050027 GrColorType::kRGBA_8888,
28 GrRenderable::kYes);
Robert Phillipsfe4b4812020-07-17 14:15:51 -040029 return rContext->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -040030 format, kDimensions, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -040031 SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillipse837e612019-11-15 11:02:50 -050032}
Robert Phillipse837e612019-11-15 11:02:50 -050033
34typedef GrQuadAAFlags (*PerQuadAAFunc)(int i);
35
Robert Phillipsfe4b4812020-07-17 14:15:51 -040036typedef void (*BulkRectTest)(skiatest::Reporter*,
37 GrDirectContext*,
Robert Phillips6bf11252020-07-31 12:15:00 -040038 PerQuadAAFunc,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040039 GrAAType overallAA,
40 SkBlendMode,
Robert Phillips6bf11252020-07-31 12:15:00 -040041 bool addOneByOne,
42 bool allUniqueProxies,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040043 int requestedTotNumQuads,
44 int expectedNumOps);
Robert Phillipse837e612019-11-15 11:02:50 -050045
46//-------------------------------------------------------------------------------------------------
Robert Phillips6bf11252020-07-31 12:15:00 -040047static void fillrectop_creation_test(skiatest::Reporter* reporter, GrDirectContext* dContext,
48 PerQuadAAFunc perQuadAA, GrAAType overallAA,
49 SkBlendMode blendMode, bool addOneByOne,
50 bool allUniqueProxies,
51 int requestedTotNumQuads, int expectedNumOps) {
52
53 if (addOneByOne || allUniqueProxies) {
54 return;
55 }
Robert Phillipse837e612019-11-15 11:02:50 -050056
Brian Salomoneebe7352020-12-09 16:37:04 -050057 std::unique_ptr<GrSurfaceDrawContext> rtc = new_RTC(dContext);
Robert Phillipse837e612019-11-15 11:02:50 -050058
Brian Salomoneebe7352020-12-09 16:37:04 -050059 auto quads = new GrSurfaceDrawContext::QuadSetEntry[requestedTotNumQuads];
Robert Phillipse837e612019-11-15 11:02:50 -050060
61 for (int i = 0; i < requestedTotNumQuads; ++i) {
62 quads[i].fRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization
63 quads[i].fColor = SK_PMColor4fWHITE;
64 quads[i].fLocalMatrix = SkMatrix::I();
65 quads[i].fAAFlags = perQuadAA(i);
66 }
67
68 GrPaint paint;
Michael Ludwigfe13ca32019-11-21 10:26:41 -050069 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
Robert Phillips6bf11252020-07-31 12:15:00 -040070
Robert Phillipsfe4b4812020-07-17 14:15:51 -040071 GrFillRectOp::AddFillRectOps(rtc.get(), nullptr, dContext, std::move(paint), overallAA,
Robert Phillipse837e612019-11-15 11:02:50 -050072 SkMatrix::I(), quads, requestedTotNumQuads);
73
74 GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask();
75 int actualNumOps = opsTask->numOpChains();
76
77 int actualTotNumQuads = 0;
78
79 for (int i = 0; i < actualNumOps; ++i) {
80 const GrOp* tmp = opsTask->getChain(i);
81 REPORTER_ASSERT(reporter, tmp->classID() == GrFillRectOp::ClassID());
82 REPORTER_ASSERT(reporter, tmp->isChainTail());
83 actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads();
84 }
85
86 REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
87 REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
88
Robert Phillipsfe4b4812020-07-17 14:15:51 -040089 dContext->flushAndSubmit();
Robert Phillipse837e612019-11-15 11:02:50 -050090
91 delete[] quads;
92}
93
Robert Phillipse837e612019-11-15 11:02:50 -050094//-------------------------------------------------------------------------------------------------
Robert Phillips6bf11252020-07-31 12:15:00 -040095static void textureop_creation_test(skiatest::Reporter* reporter, GrDirectContext* dContext,
96 PerQuadAAFunc perQuadAA, GrAAType overallAA,
97 SkBlendMode blendMode, bool addOneByOne,
98 bool allUniqueProxies,
99 int requestedTotNumQuads, int expectedNumOps) {
Robert Phillipse837e612019-11-15 11:02:50 -0500100
Brian Salomoneebe7352020-12-09 16:37:04 -0500101 std::unique_ptr<GrSurfaceDrawContext> rtc = new_RTC(dContext);
Robert Phillipse837e612019-11-15 11:02:50 -0500102
Robert Phillips6bf11252020-07-31 12:15:00 -0400103 GrSurfaceProxyView proxyViewA, proxyViewB;
104
105 if (!allUniqueProxies) {
106 sk_sp<GrSurfaceProxy> proxyA = create_proxy(dContext);
107 sk_sp<GrSurfaceProxy> proxyB = create_proxy(dContext);
108 proxyViewA = GrSurfaceProxyView(std::move(proxyA),
109 kTopLeft_GrSurfaceOrigin,
110 GrSwizzle::RGBA());
111 proxyViewB = GrSurfaceProxyView(std::move(proxyB),
112 kTopLeft_GrSurfaceOrigin,
113 GrSwizzle::RGBA());
114 }
Robert Phillipse837e612019-11-15 11:02:50 -0500115
Brian Salomoneebe7352020-12-09 16:37:04 -0500116 auto set = new GrSurfaceDrawContext::TextureSetEntry[requestedTotNumQuads];
Robert Phillipse837e612019-11-15 11:02:50 -0500117
118 for (int i = 0; i < requestedTotNumQuads; ++i) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400119 if (!allUniqueProxies) {
120 // Alternate between two proxies to prevent op merging if the batch API was forced to
121 // submit one op at a time (to work, this does require that all fDstRects overlap).
122 set[i].fProxyView = i % 2 == 0 ? proxyViewA : proxyViewB;
123 } else {
124 // Each op gets its own proxy to force chaining only
125 sk_sp<GrSurfaceProxy> proxyA = create_proxy(dContext);
126 set[i].fProxyView = GrSurfaceProxyView(std::move(proxyA),
127 kTopLeft_GrSurfaceOrigin,
128 GrSwizzle::RGBA());
129 }
130
Brian Salomonfc118442019-11-22 19:09:27 -0500131 set[i].fSrcAlphaType = kPremul_SkAlphaType;
Robert Phillipse837e612019-11-15 11:02:50 -0500132 set[i].fSrcRect = SkRect::MakeWH(100.0f, 100.0f);
133 set[i].fDstRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization
134 set[i].fDstClipQuad = nullptr;
135 set[i].fPreViewMatrix = nullptr;
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400136 set[i].fColor = {1.f, 1.f, 1.f, 1.f};
Robert Phillipse837e612019-11-15 11:02:50 -0500137 set[i].fAAFlags = perQuadAA(i);
138 }
139
Robert Phillips6bf11252020-07-31 12:15:00 -0400140 if (addOneByOne) {
141 for (int i = 0; i < requestedTotNumQuads; ++i) {
142 DrawQuad quad;
143
144 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, SkMatrix::I());
145 quad.fLocal = GrQuad(set[i].fSrcRect);
146 quad.fEdgeFlags = set[i].fAAFlags;
147
Herb Derbyc76d4092020-10-07 16:46:15 -0400148 GrOp::Owner op = GrTextureOp::Make(dContext,
149 set[i].fProxyView,
150 set[i].fSrcAlphaType,
151 nullptr,
152 GrSamplerState::Filter::kNearest,
153 GrSamplerState::MipmapMode::kNone,
154 set[i].fColor,
155 GrTextureOp::Saturate::kYes,
156 blendMode,
157 overallAA,
158 &quad,
159 nullptr);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500160 rtc->addDrawOp(nullptr, std::move(op));
Robert Phillips6bf11252020-07-31 12:15:00 -0400161 }
162 } else {
163 GrTextureOp::AddTextureSetOps(rtc.get(),
164 nullptr,
165 dContext,
166 set,
167 requestedTotNumQuads,
168 requestedTotNumQuads, // We alternate so proxyCnt == cnt
169 GrSamplerState::Filter::kNearest,
170 GrSamplerState::MipmapMode::kNone,
171 GrTextureOp::Saturate::kYes,
172 blendMode,
173 overallAA,
174 SkCanvas::kStrict_SrcRectConstraint,
175 SkMatrix::I(),
176 nullptr);
177 }
Robert Phillipse837e612019-11-15 11:02:50 -0500178
179 GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask();
180 int actualNumOps = opsTask->numOpChains();
181
182 int actualTotNumQuads = 0;
183
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500184 if (blendMode != SkBlendMode::kSrcOver ||
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400185 !dContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500186 // In either of these two cases, GrTextureOp creates one op per quad instead. Since
187 // each entry alternates proxies but overlaps geometrically, this will prevent the ops
188 // from being merged back into fewer ops.
189 expectedNumOps = requestedTotNumQuads;
190 }
191 uint32_t expectedOpID = blendMode == SkBlendMode::kSrcOver ? GrTextureOp::ClassID()
192 : GrFillRectOp::ClassID();
Robert Phillipse837e612019-11-15 11:02:50 -0500193 for (int i = 0; i < actualNumOps; ++i) {
194 const GrOp* tmp = opsTask->getChain(i);
Robert Phillips6bf11252020-07-31 12:15:00 -0400195 REPORTER_ASSERT(reporter, allUniqueProxies || tmp->isChainTail());
196 while (tmp) {
197 REPORTER_ASSERT(reporter, tmp->classID() == expectedOpID);
198 actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads();
199 tmp = tmp->nextInChain();
200 }
Robert Phillipse837e612019-11-15 11:02:50 -0500201 }
202
203 REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
204 REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
205
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400206 dContext->flushAndSubmit();
Robert Phillipse837e612019-11-15 11:02:50 -0500207
208 delete[] set;
209}
Robert Phillipse837e612019-11-15 11:02:50 -0500210
211//-------------------------------------------------------------------------------------------------
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400212static void run_test(GrDirectContext* dContext, skiatest::Reporter* reporter, BulkRectTest test) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400213
Robert Phillipse837e612019-11-15 11:02:50 -0500214 // This is the simple case where there is no AA at all. We expect 2 non-AA clumps of quads.
215 {
216 auto noAA = [](int i) -> GrQuadAAFlags {
217 return GrQuadAAFlags::kNone;
218 };
219
220 static const int kNumExpectedOps = 2;
221
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400222 test(reporter, dContext, noAA, GrAAType::kNone, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400223 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500224 }
225
226 // This is the same as the above case except the overall AA is kCoverage. However, since
227 // the per-quad AA is still none, all the quads should be downgraded to non-AA.
228 {
229 auto noAA = [](int i) -> GrQuadAAFlags {
230 return GrQuadAAFlags::kNone;
231 };
232
233 static const int kNumExpectedOps = 2;
234
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400235 test(reporter, dContext, noAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400236 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500237 }
238
239 // This case has an overall AA of kCoverage but the per-quad AA alternates.
240 // We should end up with several aa-sized clumps
241 {
242 auto alternateAA = [](int i) -> GrQuadAAFlags {
243 return (i % 2) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
244 };
245
246 int numExpectedOps = 2*GrResourceProvider::MaxNumNonAAQuads() /
247 GrResourceProvider::MaxNumAAQuads();
248
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400249 test(reporter, dContext, alternateAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400250 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), numExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500251 }
252
253 // In this case we have a run of MaxNumAAQuads non-AA quads and then AA quads. This
254 // exercises the case where we have a clump of quads that can't be upgraded to AA bc of
255 // its size. We expect one clump of non-AA quads followed by one clump of AA quads.
256 {
257 auto runOfNonAA = [](int i) -> GrQuadAAFlags {
258 return (i < GrResourceProvider::MaxNumAAQuads()) ? GrQuadAAFlags::kNone
259 : GrQuadAAFlags::kAll;
260 };
261
262 static const int kNumExpectedOps = 2;
263
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400264 test(reporter, dContext, runOfNonAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400265 false, false, 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500266 }
267
268 // In this case we use a blend mode other than src-over, which hits the GrFillRectOp fallback
269 // code path for GrTextureOp. We pass in the expected results if batching was successful, to
270 // that bulk_fill_rect_create_test batches on all modes; bulk_texture_rect_create_test is
271 // responsible for revising its expectations.
272 {
273 auto fixedAA = [](int i) -> GrQuadAAFlags {
274 return GrQuadAAFlags::kAll;
275 };
276
277 static const int kNumExpectedOps = 2;
278
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400279 test(reporter, dContext, fixedAA, GrAAType::kCoverage, SkBlendMode::kSrcATop,
Robert Phillips6bf11252020-07-31 12:15:00 -0400280 false, false, 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500281 }
Robert Phillips6bf11252020-07-31 12:15:00 -0400282
283 // This repros crbug.com/1108475, where we create 1024 non-AA texture ops w/ one coverage-AA
284 // texture op in the middle. Because each op has its own texture, all the texture ops
285 // get chained together so the quad count can exceed the AA maximum.
286 {
287 auto onlyOneAA = [](int i) -> GrQuadAAFlags {
288 return i == 256 ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
289 };
290
291 static const int kNumExpectedOps = 3;
292
293 test(reporter, dContext, onlyOneAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
294 true, true, 1024, kNumExpectedOps);
295 }
296
297 // This repros a problem related to crbug.com/1108475. In this case, the bulk creation
298 // method had no way to break up the set of texture ops at the AA quad limit.
299 {
300 auto onlyOneAA = [](int i) -> GrQuadAAFlags {
301 return i == 256 ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
302 };
303
304 static const int kNumExpectedOps = 2;
305
306 test(reporter, dContext, onlyOneAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
307 false, true, 1024, kNumExpectedOps);
308 }
309
Robert Phillipse837e612019-11-15 11:02:50 -0500310}
311
312DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkFillRectTest, reporter, ctxInfo) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400313 run_test(ctxInfo.directContext(), reporter, fillrectop_creation_test);
Robert Phillipse837e612019-11-15 11:02:50 -0500314}
315
Robert Phillipse837e612019-11-15 11:02:50 -0500316DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkTextureRectTest, reporter, ctxInfo) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400317 run_test(ctxInfo.directContext(), reporter, textureop_creation_test);
Robert Phillipse837e612019-11-15 11:02:50 -0500318}