• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 
8 #include "include/gpu/GrDirectContext.h"
9 #include "src/core/SkBlendModePriv.h"
10 #include "src/gpu/GrDirectContextPriv.h"
11 #include "src/gpu/GrProxyProvider.h"
12 #include "src/gpu/GrSurfaceDrawContext.h"
13 #include "src/gpu/ops/GrFillRectOp.h"
14 #include "src/gpu/ops/GrTextureOp.h"
15 #include "tests/Test.h"
16 
new_RTC(GrRecordingContext * rContext)17 static std::unique_ptr<GrSurfaceDrawContext> new_RTC(GrRecordingContext* rContext) {
18     return GrSurfaceDrawContext::Make(
19             rContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {128, 128},
20             SkSurfaceProps());
21 }
22 
create_proxy(GrRecordingContext * rContext)23 static sk_sp<GrSurfaceProxy> create_proxy(GrRecordingContext* rContext) {
24     static constexpr SkISize kDimensions = {128, 128};
25 
26     const GrBackendFormat format = rContext->priv().caps()->getDefaultBackendFormat(
27                                                                            GrColorType::kRGBA_8888,
28                                                                            GrRenderable::kYes);
29     return rContext->priv().proxyProvider()->createProxy(
30             format, kDimensions, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBackingFit::kExact,
31             SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone);
32 }
33 
34 typedef GrQuadAAFlags (*PerQuadAAFunc)(int i);
35 
36 typedef void (*BulkRectTest)(skiatest::Reporter*,
37                              GrDirectContext*,
38                              PerQuadAAFunc,
39                              GrAAType overallAA,
40                              SkBlendMode,
41                              bool addOneByOne,
42                              bool allUniqueProxies,
43                              int requestedTotNumQuads,
44                              int expectedNumOps);
45 
46 //-------------------------------------------------------------------------------------------------
fillrectop_creation_test(skiatest::Reporter * reporter,GrDirectContext * dContext,PerQuadAAFunc perQuadAA,GrAAType overallAA,SkBlendMode blendMode,bool addOneByOne,bool allUniqueProxies,int requestedTotNumQuads,int expectedNumOps)47 static 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     }
56 
57     std::unique_ptr<GrSurfaceDrawContext> rtc = new_RTC(dContext);
58 
59     auto quads = new GrSurfaceDrawContext::QuadSetEntry[requestedTotNumQuads];
60 
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;
69     paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
70 
71     GrFillRectOp::AddFillRectOps(rtc.get(), nullptr, dContext, std::move(paint), overallAA,
72                                  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 
89     dContext->flushAndSubmit();
90 
91     delete[] quads;
92 }
93 
94 //-------------------------------------------------------------------------------------------------
textureop_creation_test(skiatest::Reporter * reporter,GrDirectContext * dContext,PerQuadAAFunc perQuadAA,GrAAType overallAA,SkBlendMode blendMode,bool addOneByOne,bool allUniqueProxies,int requestedTotNumQuads,int expectedNumOps)95 static 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) {
100 
101     std::unique_ptr<GrSurfaceDrawContext> rtc = new_RTC(dContext);
102 
103     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     }
115 
116     auto set = new GrSurfaceDrawContext::TextureSetEntry[requestedTotNumQuads];
117 
118     for (int i = 0; i < requestedTotNumQuads; ++i) {
119         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 
131         set[i].fSrcAlphaType = kPremul_SkAlphaType;
132         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;
136         set[i].fColor = {1.f, 1.f, 1.f, 1.f};
137         set[i].fAAFlags = perQuadAA(i);
138     }
139 
140     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 
148             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);
160             rtc->addDrawOp(nullptr, std::move(op));
161         }
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     }
178 
179     GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask();
180     int actualNumOps = opsTask->numOpChains();
181 
182     int actualTotNumQuads = 0;
183 
184     if (blendMode != SkBlendMode::kSrcOver ||
185         !dContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
186         // 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();
193     for (int i = 0; i < actualNumOps; ++i) {
194         const GrOp* tmp = opsTask->getChain(i);
195         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         }
201     }
202 
203     REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps);
204     REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads);
205 
206     dContext->flushAndSubmit();
207 
208     delete[] set;
209 }
210 
211 //-------------------------------------------------------------------------------------------------
run_test(GrDirectContext * dContext,skiatest::Reporter * reporter,BulkRectTest test)212 static void run_test(GrDirectContext* dContext, skiatest::Reporter* reporter, BulkRectTest test) {
213 
214     // 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 
222         test(reporter, dContext, noAA, GrAAType::kNone, SkBlendMode::kSrcOver,
223              false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
224     }
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 
235         test(reporter, dContext, noAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
236              false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps);
237     }
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 
249         test(reporter, dContext, alternateAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
250              false, false, 2*GrResourceProvider::MaxNumNonAAQuads(), numExpectedOps);
251     }
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 
264         test(reporter, dContext, runOfNonAA, GrAAType::kCoverage, SkBlendMode::kSrcOver,
265              false, false, 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
266     }
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 
279         test(reporter, dContext, fixedAA, GrAAType::kCoverage, SkBlendMode::kSrcATop,
280              false, false, 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps);
281     }
282 
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 
310 }
311 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkFillRectTest,reporter,ctxInfo)312 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkFillRectTest, reporter, ctxInfo) {
313     run_test(ctxInfo.directContext(), reporter, fillrectop_creation_test);
314 }
315 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkTextureRectTest,reporter,ctxInfo)316 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkTextureRectTest, reporter, ctxInfo) {
317     run_test(ctxInfo.directContext(), reporter, textureop_creation_test);
318 }
319