blob: 7a8c035b8999ddc9e44f33383b2f36de94896626 [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 Phillipse40495d2021-07-20 09:40:13 -040011#include "src/gpu/GrOpsTypes.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040012#include "src/gpu/GrProxyProvider.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040013#include "src/gpu/GrResourceProvider.h"
Robert Phillipse837e612019-11-15 11:02:50 -050014#include "src/gpu/ops/GrFillRectOp.h"
15#include "src/gpu/ops/GrTextureOp.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040016#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Robert Phillipse837e612019-11-15 11:02:50 -050017#include "tests/Test.h"
18
Robert Phillips4dca8312021-07-28 15:13:20 -040019static std::unique_ptr<skgpu::v1::SurfaceDrawContext> new_SDC(GrRecordingContext* rContext) {
20 return skgpu::v1::SurfaceDrawContext::Make(
Chris Daltonf5b87f92021-04-19 17:27:09 -060021 rContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {128, 128},
22 SkSurfaceProps());
Robert Phillipse837e612019-11-15 11:02:50 -050023}
24
Robert Phillips089b7c92020-08-12 11:57:07 -040025static sk_sp<GrSurfaceProxy> create_proxy(GrRecordingContext* rContext) {
Brian Salomona56a7462020-02-07 14:17:25 -050026 static constexpr SkISize kDimensions = {128, 128};
Robert Phillipse837e612019-11-15 11:02:50 -050027
Robert Phillipsfe4b4812020-07-17 14:15:51 -040028 const GrBackendFormat format = rContext->priv().caps()->getDefaultBackendFormat(
Robert Phillipse837e612019-11-15 11:02:50 -050029 GrColorType::kRGBA_8888,
30 GrRenderable::kYes);
Robert Phillipsfe4b4812020-07-17 14:15:51 -040031 return rContext->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -040032 format, kDimensions, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -040033 SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillipse837e612019-11-15 11:02:50 -050034}
Robert Phillipse837e612019-11-15 11:02:50 -050035
36typedef GrQuadAAFlags (*PerQuadAAFunc)(int i);
37
Robert Phillipsfe4b4812020-07-17 14:15:51 -040038typedef void (*BulkRectTest)(skiatest::Reporter*,
39 GrDirectContext*,
Robert Phillips6bf11252020-07-31 12:15:00 -040040 PerQuadAAFunc,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040041 GrAAType overallAA,
42 SkBlendMode,
Robert Phillips6bf11252020-07-31 12:15:00 -040043 bool addOneByOne,
44 bool allUniqueProxies,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040045 int requestedTotNumQuads,
46 int expectedNumOps);
Robert Phillipse837e612019-11-15 11:02:50 -050047
48//-------------------------------------------------------------------------------------------------
Robert Phillips6bf11252020-07-31 12:15:00 -040049static void fillrectop_creation_test(skiatest::Reporter* reporter, GrDirectContext* dContext,
50 PerQuadAAFunc perQuadAA, GrAAType overallAA,
51 SkBlendMode blendMode, bool addOneByOne,
52 bool allUniqueProxies,
53 int requestedTotNumQuads, int expectedNumOps) {
54
55 if (addOneByOne || allUniqueProxies) {
56 return;
57 }
Robert Phillipse837e612019-11-15 11:02:50 -050058
Robert Phillips4dca8312021-07-28 15:13:20 -040059 std::unique_ptr<skgpu::v1::SurfaceDrawContext> sdc = new_SDC(dContext);
Robert Phillipse837e612019-11-15 11:02:50 -050060
Robert Phillipse40495d2021-07-20 09:40:13 -040061 auto quads = new GrQuadSetEntry[requestedTotNumQuads];
Robert Phillipse837e612019-11-15 11:02:50 -050062
63 for (int i = 0; i < requestedTotNumQuads; ++i) {
64 quads[i].fRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization
65 quads[i].fColor = SK_PMColor4fWHITE;
66 quads[i].fLocalMatrix = SkMatrix::I();
67 quads[i].fAAFlags = perQuadAA(i);
68 }
69
70 GrPaint paint;
Michael Ludwigfe13ca32019-11-21 10:26:41 -050071 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
Robert Phillips6bf11252020-07-31 12:15:00 -040072
Robert Phillips4dca8312021-07-28 15:13:20 -040073 GrFillRectOp::AddFillRectOps(sdc.get(), nullptr, dContext, std::move(paint), overallAA,
Robert Phillipse837e612019-11-15 11:02:50 -050074 SkMatrix::I(), quads, requestedTotNumQuads);
75
Robert Phillips3e87a8e2021-08-25 13:22:24 -040076 auto opsTask = sdc->testingOnly_PeekLastOpsTask();
Robert Phillipse837e612019-11-15 11:02:50 -050077 int actualNumOps = opsTask->numOpChains();
78
79 int actualTotNumQuads = 0;
80
81 for (int i = 0; i < actualNumOps; ++i) {
82 const GrOp* tmp = opsTask->getChain(i);
83 REPORTER_ASSERT(reporter, tmp->classID() == GrFillRectOp::ClassID());
84 REPORTER_ASSERT(reporter, tmp->isChainTail());
85 actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads();
86 }
87
88 REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
89 REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
90
Robert Phillipsfe4b4812020-07-17 14:15:51 -040091 dContext->flushAndSubmit();
Robert Phillipse837e612019-11-15 11:02:50 -050092
93 delete[] quads;
94}
95
Robert Phillipse837e612019-11-15 11:02:50 -050096//-------------------------------------------------------------------------------------------------
Robert Phillips6bf11252020-07-31 12:15:00 -040097static void textureop_creation_test(skiatest::Reporter* reporter, GrDirectContext* dContext,
98 PerQuadAAFunc perQuadAA, GrAAType overallAA,
99 SkBlendMode blendMode, bool addOneByOne,
100 bool allUniqueProxies,
101 int requestedTotNumQuads, int expectedNumOps) {
Robert Phillipse837e612019-11-15 11:02:50 -0500102
Robert Phillips4dca8312021-07-28 15:13:20 -0400103 std::unique_ptr<skgpu::v1::SurfaceDrawContext> sdc = new_SDC(dContext);
Robert Phillipse837e612019-11-15 11:02:50 -0500104
Robert Phillips6bf11252020-07-31 12:15:00 -0400105 GrSurfaceProxyView proxyViewA, proxyViewB;
106
107 if (!allUniqueProxies) {
108 sk_sp<GrSurfaceProxy> proxyA = create_proxy(dContext);
109 sk_sp<GrSurfaceProxy> proxyB = create_proxy(dContext);
110 proxyViewA = GrSurfaceProxyView(std::move(proxyA),
111 kTopLeft_GrSurfaceOrigin,
112 GrSwizzle::RGBA());
113 proxyViewB = GrSurfaceProxyView(std::move(proxyB),
114 kTopLeft_GrSurfaceOrigin,
115 GrSwizzle::RGBA());
116 }
Robert Phillipse837e612019-11-15 11:02:50 -0500117
Robert Phillipse40495d2021-07-20 09:40:13 -0400118 auto set = new GrTextureSetEntry[requestedTotNumQuads];
Robert Phillipse837e612019-11-15 11:02:50 -0500119
120 for (int i = 0; i < requestedTotNumQuads; ++i) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400121 if (!allUniqueProxies) {
122 // Alternate between two proxies to prevent op merging if the batch API was forced to
123 // submit one op at a time (to work, this does require that all fDstRects overlap).
124 set[i].fProxyView = i % 2 == 0 ? proxyViewA : proxyViewB;
125 } else {
126 // Each op gets its own proxy to force chaining only
127 sk_sp<GrSurfaceProxy> proxyA = create_proxy(dContext);
128 set[i].fProxyView = GrSurfaceProxyView(std::move(proxyA),
129 kTopLeft_GrSurfaceOrigin,
130 GrSwizzle::RGBA());
131 }
132
Brian Salomonfc118442019-11-22 19:09:27 -0500133 set[i].fSrcAlphaType = kPremul_SkAlphaType;
Robert Phillipse837e612019-11-15 11:02:50 -0500134 set[i].fSrcRect = SkRect::MakeWH(100.0f, 100.0f);
135 set[i].fDstRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization
136 set[i].fDstClipQuad = nullptr;
137 set[i].fPreViewMatrix = nullptr;
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400138 set[i].fColor = {1.f, 1.f, 1.f, 1.f};
Robert Phillipse837e612019-11-15 11:02:50 -0500139 set[i].fAAFlags = perQuadAA(i);
140 }
141
Robert Phillips6bf11252020-07-31 12:15:00 -0400142 if (addOneByOne) {
143 for (int i = 0; i < requestedTotNumQuads; ++i) {
144 DrawQuad quad;
145
146 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, SkMatrix::I());
147 quad.fLocal = GrQuad(set[i].fSrcRect);
148 quad.fEdgeFlags = set[i].fAAFlags;
149
Herb Derbyc76d4092020-10-07 16:46:15 -0400150 GrOp::Owner op = GrTextureOp::Make(dContext,
151 set[i].fProxyView,
152 set[i].fSrcAlphaType,
153 nullptr,
154 GrSamplerState::Filter::kNearest,
155 GrSamplerState::MipmapMode::kNone,
156 set[i].fColor,
157 GrTextureOp::Saturate::kYes,
158 blendMode,
159 overallAA,
160 &quad,
161 nullptr);
Robert Phillips4dca8312021-07-28 15:13:20 -0400162 sdc->addDrawOp(nullptr, std::move(op));
Robert Phillips6bf11252020-07-31 12:15:00 -0400163 }
164 } else {
Robert Phillips4dca8312021-07-28 15:13:20 -0400165 GrTextureOp::AddTextureSetOps(sdc.get(),
Robert Phillips6bf11252020-07-31 12:15:00 -0400166 nullptr,
167 dContext,
168 set,
169 requestedTotNumQuads,
170 requestedTotNumQuads, // We alternate so proxyCnt == cnt
171 GrSamplerState::Filter::kNearest,
172 GrSamplerState::MipmapMode::kNone,
173 GrTextureOp::Saturate::kYes,
174 blendMode,
175 overallAA,
176 SkCanvas::kStrict_SrcRectConstraint,
177 SkMatrix::I(),
178 nullptr);
179 }
Robert Phillipse837e612019-11-15 11:02:50 -0500180
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400181 auto opsTask = sdc->testingOnly_PeekLastOpsTask();
Robert Phillipse837e612019-11-15 11:02:50 -0500182 int actualNumOps = opsTask->numOpChains();
183
184 int actualTotNumQuads = 0;
185
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500186 if (blendMode != SkBlendMode::kSrcOver ||
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400187 !dContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500188 // In either of these two cases, GrTextureOp creates one op per quad instead. Since
189 // each entry alternates proxies but overlaps geometrically, this will prevent the ops
190 // from being merged back into fewer ops.
191 expectedNumOps = requestedTotNumQuads;
192 }
193 uint32_t expectedOpID = blendMode == SkBlendMode::kSrcOver ? GrTextureOp::ClassID()
194 : GrFillRectOp::ClassID();
Robert Phillipse837e612019-11-15 11:02:50 -0500195 for (int i = 0; i < actualNumOps; ++i) {
196 const GrOp* tmp = opsTask->getChain(i);
Robert Phillips6bf11252020-07-31 12:15:00 -0400197 REPORTER_ASSERT(reporter, allUniqueProxies || tmp->isChainTail());
198 while (tmp) {
199 REPORTER_ASSERT(reporter, tmp->classID() == expectedOpID);
200 actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads();
201 tmp = tmp->nextInChain();
202 }
Robert Phillipse837e612019-11-15 11:02:50 -0500203 }
204
205 REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
206 REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
207
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400208 dContext->flushAndSubmit();
Robert Phillipse837e612019-11-15 11:02:50 -0500209
210 delete[] set;
211}
Robert Phillipse837e612019-11-15 11:02:50 -0500212
213//-------------------------------------------------------------------------------------------------
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400214static void run_test(GrDirectContext* dContext, skiatest::Reporter* reporter, BulkRectTest test) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400215
Robert Phillipse837e612019-11-15 11:02:50 -0500216 // This is the simple case where there is no AA at all. We expect 2 non-AA clumps of quads.
217 {
218 auto noAA = [](int i) -> GrQuadAAFlags {
219 return GrQuadAAFlags::kNone;
220 };
221
222 static const int kNumExpectedOps = 2;
223
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400224 test(reporter, dContext, noAA, GrAAType::kNone, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400225 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500226 }
227
228 // This is the same as the above case except the overall AA is kCoverage. However, since
229 // the per-quad AA is still none, all the quads should be downgraded to non-AA.
230 {
231 auto noAA = [](int i) -> GrQuadAAFlags {
232 return GrQuadAAFlags::kNone;
233 };
234
235 static const int kNumExpectedOps = 2;
236
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400237 test(reporter, dContext, noAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400238 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500239 }
240
241 // This case has an overall AA of kCoverage but the per-quad AA alternates.
242 // We should end up with several aa-sized clumps
243 {
244 auto alternateAA = [](int i) -> GrQuadAAFlags {
245 return (i % 2) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
246 };
247
248 int numExpectedOps = 2*GrResourceProvider::MaxNumNonAAQuads() /
249 GrResourceProvider::MaxNumAAQuads();
250
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400251 test(reporter, dContext, alternateAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400252 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), numExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500253 }
254
255 // In this case we have a run of MaxNumAAQuads non-AA quads and then AA quads. This
256 // exercises the case where we have a clump of quads that can't be upgraded to AA bc of
257 // its size. We expect one clump of non-AA quads followed by one clump of AA quads.
258 {
259 auto runOfNonAA = [](int i) -> GrQuadAAFlags {
260 return (i < GrResourceProvider::MaxNumAAQuads()) ? GrQuadAAFlags::kNone
261 : GrQuadAAFlags::kAll;
262 };
263
264 static const int kNumExpectedOps = 2;
265
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400266 test(reporter, dContext, runOfNonAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400267 false, false, 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500268 }
269
270 // In this case we use a blend mode other than src-over, which hits the GrFillRectOp fallback
271 // code path for GrTextureOp. We pass in the expected results if batching was successful, to
272 // that bulk_fill_rect_create_test batches on all modes; bulk_texture_rect_create_test is
273 // responsible for revising its expectations.
274 {
275 auto fixedAA = [](int i) -> GrQuadAAFlags {
276 return GrQuadAAFlags::kAll;
277 };
278
279 static const int kNumExpectedOps = 2;
280
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400281 test(reporter, dContext, fixedAA, GrAAType::kCoverage, SkBlendMode::kSrcATop,
Robert Phillips6bf11252020-07-31 12:15:00 -0400282 false, false, 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500283 }
Robert Phillips6bf11252020-07-31 12:15:00 -0400284
285 // This repros crbug.com/1108475, where we create 1024 non-AA texture ops w/ one coverage-AA
286 // texture op in the middle. Because each op has its own texture, all the texture ops
287 // get chained together so the quad count can exceed the AA maximum.
288 {
289 auto onlyOneAA = [](int i) -> GrQuadAAFlags {
290 return i == 256 ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
291 };
292
293 static const int kNumExpectedOps = 3;
294
295 test(reporter, dContext, onlyOneAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
296 true, true, 1024, kNumExpectedOps);
297 }
298
299 // This repros a problem related to crbug.com/1108475. In this case, the bulk creation
300 // method had no way to break up the set of texture ops at the AA quad limit.
301 {
302 auto onlyOneAA = [](int i) -> GrQuadAAFlags {
303 return i == 256 ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
304 };
305
306 static const int kNumExpectedOps = 2;
307
308 test(reporter, dContext, onlyOneAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
309 false, true, 1024, kNumExpectedOps);
310 }
311
Robert Phillipse837e612019-11-15 11:02:50 -0500312}
313
314DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkFillRectTest, reporter, ctxInfo) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400315 run_test(ctxInfo.directContext(), reporter, fillrectop_creation_test);
Robert Phillipse837e612019-11-15 11:02:50 -0500316}
317
Robert Phillipse837e612019-11-15 11:02:50 -0500318DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkTextureRectTest, reporter, ctxInfo) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400319 run_test(ctxInfo.directContext(), reporter, textureop_creation_test);
Robert Phillipse837e612019-11-15 11:02:50 -0500320}