blob: 3519b36d53db097575233f4c5f14db8e8e336559 [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 */
Michael Ludwigfe13ca32019-11-21 10:26:41 -05007#include "src/core/SkBlendModePriv.h"
Weston Tracey074414f2020-05-29 12:55:06 +00008#include "src/gpu/GrClip.h"
Robert Phillipse837e612019-11-15 11:02:50 -05009#include "src/gpu/GrContextPriv.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040010#include "src/gpu/GrProxyProvider.h"
Robert Phillipse837e612019-11-15 11:02:50 -050011#include "src/gpu/GrRenderTargetContext.h"
12#include "src/gpu/ops/GrFillRectOp.h"
13#include "src/gpu/ops/GrTextureOp.h"
14#include "tests/Test.h"
15
16static std::unique_ptr<GrRenderTargetContext> new_RTC(GrContext* context) {
Greg Daniele20fcad2020-01-08 11:52:34 -050017 return GrRenderTargetContext::Make(
18 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {128, 128});
Robert Phillipse837e612019-11-15 11:02:50 -050019}
20
Robert Phillipse837e612019-11-15 11:02:50 -050021sk_sp<GrSurfaceProxy> create_proxy(GrContext* context) {
Brian Salomona56a7462020-02-07 14:17:25 -050022 static constexpr SkISize kDimensions = {128, 128};
Robert Phillipse837e612019-11-15 11:02:50 -050023
24 const GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(
25 GrColorType::kRGBA_8888,
26 GrRenderable::kYes);
Robert Phillipse837e612019-11-15 11:02:50 -050027 return context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -040028 format, kDimensions, GrRenderable::kYes, 1, GrMipMapped::kNo, SkBackingFit::kExact,
29 SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillipse837e612019-11-15 11:02:50 -050030}
Robert Phillipse837e612019-11-15 11:02:50 -050031
32typedef GrQuadAAFlags (*PerQuadAAFunc)(int i);
33
34typedef void (*BulkRectTest)(skiatest::Reporter* reporter, GrContext* context,
Michael Ludwigfe13ca32019-11-21 10:26:41 -050035 PerQuadAAFunc perQuadAA, GrAAType overallAA, SkBlendMode blendMode,
Robert Phillipse837e612019-11-15 11:02:50 -050036 int requestedTotNumQuads, int expectedNumOps);
37
38//-------------------------------------------------------------------------------------------------
39static void bulk_fill_rect_create_test(skiatest::Reporter* reporter, GrContext* context,
40 PerQuadAAFunc perQuadAA, GrAAType overallAA,
Michael Ludwigfe13ca32019-11-21 10:26:41 -050041 SkBlendMode blendMode,
Robert Phillipse837e612019-11-15 11:02:50 -050042 int requestedTotNumQuads, int expectedNumOps) {
43
44 std::unique_ptr<GrRenderTargetContext> rtc = new_RTC(context);
45
46 auto quads = new GrRenderTargetContext::QuadSetEntry[requestedTotNumQuads];
47
48 for (int i = 0; i < requestedTotNumQuads; ++i) {
49 quads[i].fRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization
50 quads[i].fColor = SK_PMColor4fWHITE;
51 quads[i].fLocalMatrix = SkMatrix::I();
52 quads[i].fAAFlags = perQuadAA(i);
53 }
54
55 GrPaint paint;
Michael Ludwigfe13ca32019-11-21 10:26:41 -050056 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
Weston Tracey074414f2020-05-29 12:55:06 +000057 GrFillRectOp::AddFillRectOps(rtc.get(), GrNoClip(), context, std::move(paint), overallAA,
Robert Phillipse837e612019-11-15 11:02:50 -050058 SkMatrix::I(), quads, requestedTotNumQuads);
59
60 GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask();
61 int actualNumOps = opsTask->numOpChains();
62
63 int actualTotNumQuads = 0;
64
65 for (int i = 0; i < actualNumOps; ++i) {
66 const GrOp* tmp = opsTask->getChain(i);
67 REPORTER_ASSERT(reporter, tmp->classID() == GrFillRectOp::ClassID());
68 REPORTER_ASSERT(reporter, tmp->isChainTail());
69 actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads();
70 }
71
72 REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
73 REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
74
Greg Daniel0a2464f2020-05-14 15:45:44 -040075 context->flushAndSubmit();
Robert Phillipse837e612019-11-15 11:02:50 -050076
77 delete[] quads;
78}
79
Robert Phillipse837e612019-11-15 11:02:50 -050080//-------------------------------------------------------------------------------------------------
81static void bulk_texture_rect_create_test(skiatest::Reporter* reporter, GrContext* context,
82 PerQuadAAFunc perQuadAA, GrAAType overallAA,
Michael Ludwigfe13ca32019-11-21 10:26:41 -050083 SkBlendMode blendMode,
Robert Phillipse837e612019-11-15 11:02:50 -050084 int requestedTotNumQuads, int expectedNumOps) {
85
86 std::unique_ptr<GrRenderTargetContext> rtc = new_RTC(context);
87
Michael Ludwigfe13ca32019-11-21 10:26:41 -050088 sk_sp<GrSurfaceProxy> proxyA = create_proxy(context);
89 sk_sp<GrSurfaceProxy> proxyB = create_proxy(context);
90 GrSurfaceProxyView proxyViewA(std::move(proxyA), kTopLeft_GrSurfaceOrigin, GrSwizzle::RGBA());
91 GrSurfaceProxyView proxyViewB(std::move(proxyB), kTopLeft_GrSurfaceOrigin, GrSwizzle::RGBA());
Robert Phillipse837e612019-11-15 11:02:50 -050092
93 auto set = new GrRenderTargetContext::TextureSetEntry[requestedTotNumQuads];
94
95 for (int i = 0; i < requestedTotNumQuads; ++i) {
Michael Ludwigfe13ca32019-11-21 10:26:41 -050096 // Alternate between two proxies to prevent op merging if the batch API was forced to submit
97 // one op at a time (to work, this does require that all fDstRects overlap).
98 set[i].fProxyView = i % 2 == 0 ? proxyViewA : proxyViewB;
Brian Salomonfc118442019-11-22 19:09:27 -050099 set[i].fSrcAlphaType = kPremul_SkAlphaType;
Robert Phillipse837e612019-11-15 11:02:50 -0500100 set[i].fSrcRect = SkRect::MakeWH(100.0f, 100.0f);
101 set[i].fDstRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization
102 set[i].fDstClipQuad = nullptr;
103 set[i].fPreViewMatrix = nullptr;
104 set[i].fAlpha = 1.0f;
105 set[i].fAAFlags = perQuadAA(i);
106 }
107
Weston Tracey074414f2020-05-29 12:55:06 +0000108 GrTextureOp::AddTextureSetOps(rtc.get(), GrNoClip(), context, set, requestedTotNumQuads,
Michael Ludwig379e4962019-12-06 13:21:26 -0500109 requestedTotNumQuads, // We alternate so proxyCnt == cnt
110 GrSamplerState::Filter::kNearest,
111 GrTextureOp::Saturate::kYes,
112 blendMode,
113 overallAA,
114 SkCanvas::kStrict_SrcRectConstraint,
115 SkMatrix::I(), nullptr);
Robert Phillipse837e612019-11-15 11:02:50 -0500116
117 GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask();
118 int actualNumOps = opsTask->numOpChains();
119
120 int actualTotNumQuads = 0;
121
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500122 if (blendMode != SkBlendMode::kSrcOver ||
123 !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
124 // In either of these two cases, GrTextureOp creates one op per quad instead. Since
125 // each entry alternates proxies but overlaps geometrically, this will prevent the ops
126 // from being merged back into fewer ops.
127 expectedNumOps = requestedTotNumQuads;
128 }
129 uint32_t expectedOpID = blendMode == SkBlendMode::kSrcOver ? GrTextureOp::ClassID()
130 : GrFillRectOp::ClassID();
Robert Phillipse837e612019-11-15 11:02:50 -0500131 for (int i = 0; i < actualNumOps; ++i) {
132 const GrOp* tmp = opsTask->getChain(i);
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500133 REPORTER_ASSERT(reporter, tmp->classID() == expectedOpID);
Robert Phillipse837e612019-11-15 11:02:50 -0500134 REPORTER_ASSERT(reporter, tmp->isChainTail());
135 actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads();
136 }
137
138 REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
139 REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
140
Greg Daniel0a2464f2020-05-14 15:45:44 -0400141 context->flushAndSubmit();
Robert Phillipse837e612019-11-15 11:02:50 -0500142
143 delete[] set;
144}
Robert Phillipse837e612019-11-15 11:02:50 -0500145
146//-------------------------------------------------------------------------------------------------
147static void run_test(GrContext* context, skiatest::Reporter* reporter, BulkRectTest test) {
Robert Phillipse837e612019-11-15 11:02:50 -0500148 // This is the simple case where there is no AA at all. We expect 2 non-AA clumps of quads.
149 {
150 auto noAA = [](int i) -> GrQuadAAFlags {
151 return GrQuadAAFlags::kNone;
152 };
153
154 static const int kNumExpectedOps = 2;
155
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500156 test(reporter, context, noAA, GrAAType::kNone, SkBlendMode::kSrcOver,
Robert Phillipse837e612019-11-15 11:02:50 -0500157 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
158 }
159
160 // This is the same as the above case except the overall AA is kCoverage. However, since
161 // the per-quad AA is still none, all the quads should be downgraded to non-AA.
162 {
163 auto noAA = [](int i) -> GrQuadAAFlags {
164 return GrQuadAAFlags::kNone;
165 };
166
167 static const int kNumExpectedOps = 2;
168
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500169 test(reporter, context, noAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillipse837e612019-11-15 11:02:50 -0500170 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
171 }
172
173 // This case has an overall AA of kCoverage but the per-quad AA alternates.
174 // We should end up with several aa-sized clumps
175 {
176 auto alternateAA = [](int i) -> GrQuadAAFlags {
177 return (i % 2) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
178 };
179
180 int numExpectedOps = 2*GrResourceProvider::MaxNumNonAAQuads() /
181 GrResourceProvider::MaxNumAAQuads();
182
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500183 test(reporter, context, alternateAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
Robert Phillipse837e612019-11-15 11:02:50 -0500184 2*GrResourceProvider::MaxNumNonAAQuads(), numExpectedOps);
185 }
186
187 // In this case we have a run of MaxNumAAQuads non-AA quads and then AA quads. This
188 // exercises the case where we have a clump of quads that can't be upgraded to AA bc of
189 // its size. We expect one clump of non-AA quads followed by one clump of AA quads.
190 {
191 auto runOfNonAA = [](int i) -> GrQuadAAFlags {
192 return (i < GrResourceProvider::MaxNumAAQuads()) ? GrQuadAAFlags::kNone
193 : GrQuadAAFlags::kAll;
194 };
195
196 static const int kNumExpectedOps = 2;
197
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500198 test(reporter, context, runOfNonAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
199 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
200 }
201
202 // In this case we use a blend mode other than src-over, which hits the GrFillRectOp fallback
203 // code path for GrTextureOp. We pass in the expected results if batching was successful, to
204 // that bulk_fill_rect_create_test batches on all modes; bulk_texture_rect_create_test is
205 // responsible for revising its expectations.
206 {
207 auto fixedAA = [](int i) -> GrQuadAAFlags {
208 return GrQuadAAFlags::kAll;
209 };
210
211 static const int kNumExpectedOps = 2;
212
213 test(reporter, context, fixedAA, GrAAType::kCoverage, SkBlendMode::kSrcATop,
Robert Phillipse837e612019-11-15 11:02:50 -0500214 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
215 }
216}
217
218DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkFillRectTest, reporter, ctxInfo) {
219 run_test(ctxInfo.grContext(), reporter, bulk_fill_rect_create_test);
220}
221
Robert Phillipse837e612019-11-15 11:02:50 -0500222DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkTextureRectTest, reporter, ctxInfo) {
223 run_test(ctxInfo.grContext(), reporter, bulk_texture_rect_create_test);
224}