blob: 8e37aa7b0d368eba4126c5c40e676a9e3d7ab83a [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(
Robert Phillipsfe4b4812020-07-17 14:15:51 -040019 rContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {128, 128});
Robert Phillipse837e612019-11-15 11:02:50 -050020}
21
Robert Phillips089b7c92020-08-12 11:57:07 -040022static sk_sp<GrSurfaceProxy> create_proxy(GrRecordingContext* rContext) {
Brian Salomona56a7462020-02-07 14:17:25 -050023 static constexpr SkISize kDimensions = {128, 128};
Robert Phillipse837e612019-11-15 11:02:50 -050024
Robert Phillipsfe4b4812020-07-17 14:15:51 -040025 const GrBackendFormat format = rContext->priv().caps()->getDefaultBackendFormat(
Robert Phillipse837e612019-11-15 11:02:50 -050026 GrColorType::kRGBA_8888,
27 GrRenderable::kYes);
Robert Phillipsfe4b4812020-07-17 14:15:51 -040028 return rContext->priv().proxyProvider()->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -040029 format, kDimensions, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -040030 SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillipse837e612019-11-15 11:02:50 -050031}
Robert Phillipse837e612019-11-15 11:02:50 -050032
33typedef GrQuadAAFlags (*PerQuadAAFunc)(int i);
34
Robert Phillipsfe4b4812020-07-17 14:15:51 -040035typedef void (*BulkRectTest)(skiatest::Reporter*,
36 GrDirectContext*,
Robert Phillips6bf11252020-07-31 12:15:00 -040037 PerQuadAAFunc,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040038 GrAAType overallAA,
39 SkBlendMode,
Robert Phillips6bf11252020-07-31 12:15:00 -040040 bool addOneByOne,
41 bool allUniqueProxies,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040042 int requestedTotNumQuads,
43 int expectedNumOps);
Robert Phillipse837e612019-11-15 11:02:50 -050044
45//-------------------------------------------------------------------------------------------------
Robert Phillips6bf11252020-07-31 12:15:00 -040046static void fillrectop_creation_test(skiatest::Reporter* reporter, GrDirectContext* dContext,
47 PerQuadAAFunc perQuadAA, GrAAType overallAA,
48 SkBlendMode blendMode, bool addOneByOne,
49 bool allUniqueProxies,
50 int requestedTotNumQuads, int expectedNumOps) {
51
52 if (addOneByOne || allUniqueProxies) {
53 return;
54 }
Robert Phillipse837e612019-11-15 11:02:50 -050055
Brian Salomoneebe7352020-12-09 16:37:04 -050056 std::unique_ptr<GrSurfaceDrawContext> rtc = new_RTC(dContext);
Robert Phillipse837e612019-11-15 11:02:50 -050057
Brian Salomoneebe7352020-12-09 16:37:04 -050058 auto quads = new GrSurfaceDrawContext::QuadSetEntry[requestedTotNumQuads];
Robert Phillipse837e612019-11-15 11:02:50 -050059
60 for (int i = 0; i < requestedTotNumQuads; ++i) {
61 quads[i].fRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization
62 quads[i].fColor = SK_PMColor4fWHITE;
63 quads[i].fLocalMatrix = SkMatrix::I();
64 quads[i].fAAFlags = perQuadAA(i);
65 }
66
67 GrPaint paint;
Michael Ludwigfe13ca32019-11-21 10:26:41 -050068 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
Robert Phillips6bf11252020-07-31 12:15:00 -040069
Robert Phillipsfe4b4812020-07-17 14:15:51 -040070 GrFillRectOp::AddFillRectOps(rtc.get(), nullptr, dContext, std::move(paint), overallAA,
Robert Phillipse837e612019-11-15 11:02:50 -050071 SkMatrix::I(), quads, requestedTotNumQuads);
72
73 GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask();
74 int actualNumOps = opsTask->numOpChains();
75
76 int actualTotNumQuads = 0;
77
78 for (int i = 0; i < actualNumOps; ++i) {
79 const GrOp* tmp = opsTask->getChain(i);
80 REPORTER_ASSERT(reporter, tmp->classID() == GrFillRectOp::ClassID());
81 REPORTER_ASSERT(reporter, tmp->isChainTail());
82 actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads();
83 }
84
85 REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
86 REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
87
Robert Phillipsfe4b4812020-07-17 14:15:51 -040088 dContext->flushAndSubmit();
Robert Phillipse837e612019-11-15 11:02:50 -050089
90 delete[] quads;
91}
92
Robert Phillipse837e612019-11-15 11:02:50 -050093//-------------------------------------------------------------------------------------------------
Robert Phillips6bf11252020-07-31 12:15:00 -040094static void textureop_creation_test(skiatest::Reporter* reporter, GrDirectContext* dContext,
95 PerQuadAAFunc perQuadAA, GrAAType overallAA,
96 SkBlendMode blendMode, bool addOneByOne,
97 bool allUniqueProxies,
98 int requestedTotNumQuads, int expectedNumOps) {
Robert Phillipse837e612019-11-15 11:02:50 -050099
Brian Salomoneebe7352020-12-09 16:37:04 -0500100 std::unique_ptr<GrSurfaceDrawContext> rtc = new_RTC(dContext);
Robert Phillipse837e612019-11-15 11:02:50 -0500101
Robert Phillips6bf11252020-07-31 12:15:00 -0400102 GrSurfaceProxyView proxyViewA, proxyViewB;
103
104 if (!allUniqueProxies) {
105 sk_sp<GrSurfaceProxy> proxyA = create_proxy(dContext);
106 sk_sp<GrSurfaceProxy> proxyB = create_proxy(dContext);
107 proxyViewA = GrSurfaceProxyView(std::move(proxyA),
108 kTopLeft_GrSurfaceOrigin,
109 GrSwizzle::RGBA());
110 proxyViewB = GrSurfaceProxyView(std::move(proxyB),
111 kTopLeft_GrSurfaceOrigin,
112 GrSwizzle::RGBA());
113 }
Robert Phillipse837e612019-11-15 11:02:50 -0500114
Brian Salomoneebe7352020-12-09 16:37:04 -0500115 auto set = new GrSurfaceDrawContext::TextureSetEntry[requestedTotNumQuads];
Robert Phillipse837e612019-11-15 11:02:50 -0500116
117 for (int i = 0; i < requestedTotNumQuads; ++i) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400118 if (!allUniqueProxies) {
119 // Alternate between two proxies to prevent op merging if the batch API was forced to
120 // submit one op at a time (to work, this does require that all fDstRects overlap).
121 set[i].fProxyView = i % 2 == 0 ? proxyViewA : proxyViewB;
122 } else {
123 // Each op gets its own proxy to force chaining only
124 sk_sp<GrSurfaceProxy> proxyA = create_proxy(dContext);
125 set[i].fProxyView = GrSurfaceProxyView(std::move(proxyA),
126 kTopLeft_GrSurfaceOrigin,
127 GrSwizzle::RGBA());
128 }
129
Brian Salomonfc118442019-11-22 19:09:27 -0500130 set[i].fSrcAlphaType = kPremul_SkAlphaType;
Robert Phillipse837e612019-11-15 11:02:50 -0500131 set[i].fSrcRect = SkRect::MakeWH(100.0f, 100.0f);
132 set[i].fDstRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization
133 set[i].fDstClipQuad = nullptr;
134 set[i].fPreViewMatrix = nullptr;
Michael Ludwig1c66ad92020-07-10 08:59:44 -0400135 set[i].fColor = {1.f, 1.f, 1.f, 1.f};
Robert Phillipse837e612019-11-15 11:02:50 -0500136 set[i].fAAFlags = perQuadAA(i);
137 }
138
Robert Phillips6bf11252020-07-31 12:15:00 -0400139 if (addOneByOne) {
140 for (int i = 0; i < requestedTotNumQuads; ++i) {
141 DrawQuad quad;
142
143 quad.fDevice = GrQuad::MakeFromRect(set[i].fDstRect, SkMatrix::I());
144 quad.fLocal = GrQuad(set[i].fSrcRect);
145 quad.fEdgeFlags = set[i].fAAFlags;
146
Herb Derbyc76d4092020-10-07 16:46:15 -0400147 GrOp::Owner op = GrTextureOp::Make(dContext,
148 set[i].fProxyView,
149 set[i].fSrcAlphaType,
150 nullptr,
151 GrSamplerState::Filter::kNearest,
152 GrSamplerState::MipmapMode::kNone,
153 set[i].fColor,
154 GrTextureOp::Saturate::kYes,
155 blendMode,
156 overallAA,
157 &quad,
158 nullptr);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500159 rtc->addDrawOp(nullptr, std::move(op));
Robert Phillips6bf11252020-07-31 12:15:00 -0400160 }
161 } else {
162 GrTextureOp::AddTextureSetOps(rtc.get(),
163 nullptr,
164 dContext,
165 set,
166 requestedTotNumQuads,
167 requestedTotNumQuads, // We alternate so proxyCnt == cnt
168 GrSamplerState::Filter::kNearest,
169 GrSamplerState::MipmapMode::kNone,
170 GrTextureOp::Saturate::kYes,
171 blendMode,
172 overallAA,
173 SkCanvas::kStrict_SrcRectConstraint,
174 SkMatrix::I(),
175 nullptr);
176 }
Robert Phillipse837e612019-11-15 11:02:50 -0500177
178 GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask();
179 int actualNumOps = opsTask->numOpChains();
180
181 int actualTotNumQuads = 0;
182
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500183 if (blendMode != SkBlendMode::kSrcOver ||
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400184 !dContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500185 // In either of these two cases, GrTextureOp creates one op per quad instead. Since
186 // each entry alternates proxies but overlaps geometrically, this will prevent the ops
187 // from being merged back into fewer ops.
188 expectedNumOps = requestedTotNumQuads;
189 }
190 uint32_t expectedOpID = blendMode == SkBlendMode::kSrcOver ? GrTextureOp::ClassID()
191 : GrFillRectOp::ClassID();
Robert Phillipse837e612019-11-15 11:02:50 -0500192 for (int i = 0; i < actualNumOps; ++i) {
193 const GrOp* tmp = opsTask->getChain(i);
Robert Phillips6bf11252020-07-31 12:15:00 -0400194 REPORTER_ASSERT(reporter, allUniqueProxies || tmp->isChainTail());
195 while (tmp) {
196 REPORTER_ASSERT(reporter, tmp->classID() == expectedOpID);
197 actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads();
198 tmp = tmp->nextInChain();
199 }
Robert Phillipse837e612019-11-15 11:02:50 -0500200 }
201
202 REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
203 REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
204
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400205 dContext->flushAndSubmit();
Robert Phillipse837e612019-11-15 11:02:50 -0500206
207 delete[] set;
208}
Robert Phillipse837e612019-11-15 11:02:50 -0500209
210//-------------------------------------------------------------------------------------------------
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400211static void run_test(GrDirectContext* dContext, skiatest::Reporter* reporter, BulkRectTest test) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400212
Robert Phillipse837e612019-11-15 11:02:50 -0500213 // This is the simple case where there is no AA at all. We expect 2 non-AA clumps of quads.
214 {
215 auto noAA = [](int i) -> GrQuadAAFlags {
216 return GrQuadAAFlags::kNone;
217 };
218
219 static const int kNumExpectedOps = 2;
220
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400221 test(reporter, dContext, noAA, GrAAType::kNone, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400222 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500223 }
224
225 // This is the same as the above case except the overall AA is kCoverage. However, since
226 // the per-quad AA is still none, all the quads should be downgraded to non-AA.
227 {
228 auto noAA = [](int i) -> GrQuadAAFlags {
229 return GrQuadAAFlags::kNone;
230 };
231
232 static const int kNumExpectedOps = 2;
233
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400234 test(reporter, dContext, noAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400235 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500236 }
237
238 // This case has an overall AA of kCoverage but the per-quad AA alternates.
239 // We should end up with several aa-sized clumps
240 {
241 auto alternateAA = [](int i) -> GrQuadAAFlags {
242 return (i % 2) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
243 };
244
245 int numExpectedOps = 2*GrResourceProvider::MaxNumNonAAQuads() /
246 GrResourceProvider::MaxNumAAQuads();
247
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400248 test(reporter, dContext, alternateAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400249 false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), numExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500250 }
251
252 // In this case we have a run of MaxNumAAQuads non-AA quads and then AA quads. This
253 // exercises the case where we have a clump of quads that can't be upgraded to AA bc of
254 // its size. We expect one clump of non-AA quads followed by one clump of AA quads.
255 {
256 auto runOfNonAA = [](int i) -> GrQuadAAFlags {
257 return (i < GrResourceProvider::MaxNumAAQuads()) ? GrQuadAAFlags::kNone
258 : GrQuadAAFlags::kAll;
259 };
260
261 static const int kNumExpectedOps = 2;
262
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400263 test(reporter, dContext, runOfNonAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillips6bf11252020-07-31 12:15:00 -0400264 false, false, 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500265 }
266
267 // In this case we use a blend mode other than src-over, which hits the GrFillRectOp fallback
268 // code path for GrTextureOp. We pass in the expected results if batching was successful, to
269 // that bulk_fill_rect_create_test batches on all modes; bulk_texture_rect_create_test is
270 // responsible for revising its expectations.
271 {
272 auto fixedAA = [](int i) -> GrQuadAAFlags {
273 return GrQuadAAFlags::kAll;
274 };
275
276 static const int kNumExpectedOps = 2;
277
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400278 test(reporter, dContext, fixedAA, GrAAType::kCoverage, SkBlendMode::kSrcATop,
Robert Phillips6bf11252020-07-31 12:15:00 -0400279 false, false, 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
Robert Phillipse837e612019-11-15 11:02:50 -0500280 }
Robert Phillips6bf11252020-07-31 12:15:00 -0400281
282 // This repros crbug.com/1108475, where we create 1024 non-AA texture ops w/ one coverage-AA
283 // texture op in the middle. Because each op has its own texture, all the texture ops
284 // get chained together so the quad count can exceed the AA maximum.
285 {
286 auto onlyOneAA = [](int i) -> GrQuadAAFlags {
287 return i == 256 ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
288 };
289
290 static const int kNumExpectedOps = 3;
291
292 test(reporter, dContext, onlyOneAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
293 true, true, 1024, kNumExpectedOps);
294 }
295
296 // This repros a problem related to crbug.com/1108475. In this case, the bulk creation
297 // method had no way to break up the set of texture ops at the AA quad limit.
298 {
299 auto onlyOneAA = [](int i) -> GrQuadAAFlags {
300 return i == 256 ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
301 };
302
303 static const int kNumExpectedOps = 2;
304
305 test(reporter, dContext, onlyOneAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
306 false, true, 1024, kNumExpectedOps);
307 }
308
Robert Phillipse837e612019-11-15 11:02:50 -0500309}
310
311DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkFillRectTest, reporter, ctxInfo) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400312 run_test(ctxInfo.directContext(), reporter, fillrectop_creation_test);
Robert Phillipse837e612019-11-15 11:02:50 -0500313}
314
Robert Phillipse837e612019-11-15 11:02:50 -0500315DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkTextureRectTest, reporter, ctxInfo) {
Robert Phillips6bf11252020-07-31 12:15:00 -0400316 run_test(ctxInfo.directContext(), reporter, textureop_creation_test);
Robert Phillipse837e612019-11-15 11:02:50 -0500317}