blob: 08a2e1acacee73a770f8988ac08ec0e55cc6bbd5 [file] [log] [blame]
Robert Phillips4b732282021-05-20 09:45:10 -04001// Copyright 2021 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4#include "experimental/ngatoy/Cmds.h"
5#include "experimental/ngatoy/Fake.h"
6
7#include "include/core/SkCanvas.h"
8#include "include/core/SkGraphics.h"
9#include "include/gpu/GrDirectContext.h"
10#include "src/core/SkOSFile.h"
11#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrDirectContextPriv.h"
13#include "src/utils/SkOSPath.h"
14#include "tools/ToolUtils.h"
15#include "tools/flags/CommandLineFlags.h"
16#include "tools/gpu/GrContextFactory.h"
17
18#include <algorithm>
19
20/*
21 * Questions this is trying to answer:
22 * How to handle saveLayers (in w/ everything or separate)
23 * How to handle blurs & other off screen draws
24 * How to handle clipping
25 * How does sorting stack up against buckets
26 * How does creating batches interact w/ the sorting
27 * How does batching work w/ text
28 * How does text (esp. atlasing) work at all
29 * Batching quality vs. existing
30 * Memory churn/overhead vs existing (esp. wrt batching)
31 * gpu vs cpu boundedness
32 *
33 * Futher Questions:
34 * How can we collect uniforms & not store the fps -- seems complicated
35 * Do all the blend modes (esp. advanced work front-to-back)?
36 * NGA perf vs. OGA perf
37 * Can we prepare any of the saveLayers or off-screen draw render passes in parallel?
38 *
39 * Small potatoes:
40 * Incorporate CTM into the simulator
41 */
42
43/*
44 * How does this all work:
45 *
46 * Each test is specified by a set of RectCmds (which have a unique ID and carry their material
47 * and MC state info) along with the order they are expected to be drawn in with the NGA.
48 *
49 * To generate an expected image, the RectCmds are replayed into an SkCanvas in the order
50 * provided.
51 *
52 * For the actual (NGA) image, the RectCmds are replayed into a FakeCanvas - preserving the
53 * unique ID of the RectCmd. The FakeCanvas creates new RectCmd objects, sorts them using
54 * the SortKey and then performs a kludgey z-buffered rasterization. The FakeCanvas also
55 * preserves the RectCmd order it ultimately used for its rendering and this can be compared
56 * with the expected order from the test.
57 *
58 * The use of the RectCmds to create the tests is a mere convenience to avoid creating a
59 * separate representation of the desired draws.
60 *
61 ***************************
62 * Here are some of the simplifying assumptions of this simulation (and their justification):
63 *
64 * Only SkIRects are used for draws and clips - since MSAA should be taking care of AA for us in
65 * the NGA we don't really need SkRects. This also greatly simplifies the z-buffered rasterization.
66 *
67 **************************
68 * Areas for improvement:
69 * We should add strokes since there are two distinct drawing methods in the NGA (fill v. stroke)
70 */
71
72using sk_gpu_test::GrContextFactory;
73
74static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
75
76static void exitf(const char* format, ...) {
77 va_list args;
78 va_start(args, format);
79 vfprintf(stderr, format, args);
80 va_end(args);
81
82 exit(1);
83}
84
85static void save_files(int testID, const SkBitmap& expected, const SkBitmap& actual) {
86 if (FLAGS_writePath.isEmpty()) {
87 return;
88 }
89
90 const char* dir = FLAGS_writePath[0];
91
92 SkString path = SkOSPath::Join(dir, "expected");
93 path.appendU32(testID);
94 path.append(".png");
95
96 if (!sk_mkdir(dir)) {
97 exitf("failed to create directory for png \"%s\"", path.c_str());
98 }
99 if (!ToolUtils::EncodeImageToFile(path.c_str(), expected, SkEncodedImageFormat::kPNG, 100)) {
100 exitf("failed to save png to \"%s\"", path.c_str());
101 }
102
103 path = SkOSPath::Join(dir, "actual");
104 path.appendU32(testID);
105 path.append(".png");
106
107 if (!ToolUtils::EncodeImageToFile(path.c_str(), actual, SkEncodedImageFormat::kPNG, 100)) {
108 exitf("failed to save png to \"%s\"", path.c_str());
109 }
110}
111
112// Exercise basic SortKey behavior
113static void key_test() {
114 SortKey k;
115 SkASSERT(!k.transparent());
Robert Phillips77ecfc92021-05-24 14:41:24 -0400116 SkASSERT(k.clipID() == 0);
Robert Phillips4b732282021-05-20 09:45:10 -0400117 SkASSERT(k.depth() == 0);
118 SkASSERT(k.material() == 0);
119// k.dump();
120
Robert Phillips77ecfc92021-05-24 14:41:24 -0400121 SortKey k1(false, 4, 1, 3);
Robert Phillips4b732282021-05-20 09:45:10 -0400122 SkASSERT(!k1.transparent());
Robert Phillips77ecfc92021-05-24 14:41:24 -0400123 SkASSERT(k1.clipID() == 4);
Robert Phillips4b732282021-05-20 09:45:10 -0400124 SkASSERT(k1.depth() == 1);
125 SkASSERT(k1.material() == 3);
126// k1.dump();
127
Robert Phillips77ecfc92021-05-24 14:41:24 -0400128 SortKey k2(true, 7, 2, 1);
Robert Phillips4b732282021-05-20 09:45:10 -0400129 SkASSERT(k2.transparent());
Robert Phillips77ecfc92021-05-24 14:41:24 -0400130 SkASSERT(k2.clipID() == 7);
Robert Phillips4b732282021-05-20 09:45:10 -0400131 SkASSERT(k2.depth() == 2);
132 SkASSERT(k2.material() == 1);
133// k2.dump();
134}
135
136static void check_state(FakeMCBlob* actualState,
137 SkIPoint expectedCTM,
138 const std::vector<SkIRect>& expectedClips) {
139 SkASSERT(actualState->ctm() == expectedCTM);
140
141 int i = 0;
142 auto states = actualState->mcStates();
143 for (auto& s : states) {
144 for (auto r : s.rects()) {
145 SkAssertResult(i < (int) expectedClips.size());
146 SkAssertResult(r == expectedClips[i]);
147 i++;
148 }
149 }
150}
151
152// Exercise the FakeMCBlob object
153static void mcstack_test() {
154 const SkIRect r { 0, 0, 10, 10 };
155 const SkIPoint s1Trans { 10, 10 };
156 const SkIPoint s2TransA { -5, -2 };
157 const SkIPoint s2TransB { -3, -1 };
158
159 const std::vector<SkIRect> expectedS0Clips;
160 const std::vector<SkIRect> expectedS1Clips {
161 r.makeOffset(s1Trans)
162 };
163 const std::vector<SkIRect> expectedS2aClips {
164 r.makeOffset(s1Trans),
165 r.makeOffset(s2TransA)
166 };
167 const std::vector<SkIRect> expectedS2bClips {
168 r.makeOffset(s1Trans),
169 r.makeOffset(s2TransA),
170 r.makeOffset(s2TransA + s2TransB)
171 };
172
173 //----------------
174 FakeStateTracker s;
175
176 auto state0 = s.snapState();
177 // The initial state should have no translation & no clip
178 check_state(state0.get(), { 0, 0 }, expectedS0Clips);
179
180 //----------------
181 s.push();
182 s.translate(s1Trans);
183 s.clipRect(r);
184
185 auto state1 = s.snapState();
186 check_state(state1.get(), s1Trans, expectedS1Clips);
187
188 //----------------
189 s.push();
190 s.translate(s2TransA);
191 s.clipRect(r);
192
193 auto state2a = s.snapState();
194 check_state(state2a.get(), s1Trans + s2TransA, expectedS2aClips);
195
196 s.translate(s2TransB);
197 s.clipRect(r);
198
199 auto state2b = s.snapState();
200 check_state(state2b.get(), s1Trans + s2TransA + s2TransB, expectedS2bClips);
201 SkASSERT(state2a != state2b);
202
203 //----------------
204 s.pop();
205 auto state3 = s.snapState();
206 check_state(state3.get(), s1Trans, expectedS1Clips);
207 SkASSERT(state1 == state3);
208
209 //----------------
210 s.pop();
211 auto state4 = s.snapState();
212 check_state(state4.get(), { 0, 0 }, expectedS0Clips);
213 SkASSERT(state0 == state4);
214}
215
216static void check_order(const std::vector<int>& actualOrder,
217 const std::vector<int>& expectedOrder) {
218 if (expectedOrder.size() != actualOrder.size()) {
219 exitf("Op count mismatch. Expected %d - got %d\n",
220 expectedOrder.size(),
221 actualOrder.size());
222 }
223
224 if (expectedOrder != actualOrder) {
225 SkDebugf("order mismatch:\n");
226 SkDebugf("E %d: ", expectedOrder.size());
227 for (auto t : expectedOrder) {
228 SkDebugf("%d", t);
229 }
230 SkDebugf("\n");
231
232 SkDebugf("A %d: ", actualOrder.size());
233 for (auto t : actualOrder) {
234 SkDebugf("%d", t);
235 }
236 SkDebugf("\n");
237 }
238}
239
240typedef int (*PFTest)(std::vector<const Cmd*>* test, std::vector<int>* expectedOrder);
241
242static void sort_test(PFTest testcase) {
243 std::vector<const Cmd*> test;
244 std::vector<int> expectedOrder;
245 int testID = testcase(&test, &expectedOrder);
246
247
248 SkBitmap expectedBM;
249 expectedBM.allocPixels(SkImageInfo::MakeN32Premul(256, 256));
250 expectedBM.eraseColor(SK_ColorBLACK);
251 SkCanvas real(expectedBM);
252
253 SkBitmap actualBM;
254 actualBM.allocPixels(SkImageInfo::MakeN32Premul(256, 256));
255 actualBM.eraseColor(SK_ColorBLACK);
256
257 FakeCanvas fake(actualBM);
258 const FakeMCBlob* prior = nullptr;
259 for (auto c : test) {
260 c->execute(&fake);
261 c->execute(&real, prior);
262 prior = c->state();
263 }
264
265 fake.finalize();
266
267 std::vector<int> actualOrder = fake.getOrder();
268 check_order(actualOrder, expectedOrder);
269
270 save_files(testID, expectedBM, actualBM);
271}
272
273// Simple test - green rect should appear atop the red rect
274static int test1(std::vector<const Cmd*>* test, std::vector<int>* expectedOrder) {
275 // front-to-back order bc all opaque
276 expectedOrder->push_back(1);
277 expectedOrder->push_back(0);
278
279 FakeStateTracker s;
280 sk_sp<FakeMCBlob> state = s.snapState();
281
282 SkIRect r{0, 0, 100, 100};
283 test->push_back(new RectCmd(0, kSolidMat, r.makeOffset(8, 8), SK_ColorRED, SK_ColorUNUSED, state));
284 test->push_back(new RectCmd(1, kSolidMat, r.makeOffset(48, 48), SK_ColorGREEN, SK_ColorUNUSED, state));
285 return 1;
286}
287
288// Simple test - blue rect atop green rect atop red rect
289static int test2(std::vector<const Cmd*>* test, std::vector<int>* expectedOrder) {
290 // front-to-back order bc all opaque
291 expectedOrder->push_back(2);
292 expectedOrder->push_back(1);
293 expectedOrder->push_back(0);
294
295 FakeStateTracker s;
296 sk_sp<FakeMCBlob> state = s.snapState();
297
298 SkIRect r{0, 0, 100, 100};
299 test->push_back(new RectCmd(0, kSolidMat, r.makeOffset(8, 8), SK_ColorRED, SK_ColorUNUSED, state));
300 test->push_back(new RectCmd(1, kSolidMat, r.makeOffset(48, 48), SK_ColorGREEN, SK_ColorUNUSED, state));
301 test->push_back(new RectCmd(2, kSolidMat, r.makeOffset(98, 98), SK_ColorBLUE, SK_ColorUNUSED, state));
302 return 2;
303}
304
305// Transparency test - opaque blue rect atop transparent green rect atop opaque red rect
306static int test3(std::vector<const Cmd*>* test, std::vector<int>* expectedOrder) {
307 // opaque draws are first and are front-to-back. Transparent draw is last.
308 expectedOrder->push_back(2);
309 expectedOrder->push_back(0);
310 expectedOrder->push_back(1);
311
312 FakeStateTracker s;
313 sk_sp<FakeMCBlob> state = s.snapState();
314
315 SkIRect r{0, 0, 100, 100};
316 test->push_back(new RectCmd(0, kSolidMat, r.makeOffset(8, 8), SK_ColorRED, SK_ColorUNUSED, state));
317 test->push_back(new RectCmd(1, kSolidMat, r.makeOffset(48, 48), 0x8000FF00, SK_ColorUNUSED, state));
318 test->push_back(new RectCmd(2, kSolidMat, r.makeOffset(98, 98), SK_ColorBLUE, SK_ColorUNUSED, state));
319 return 3;
320}
321
322// Multi-transparency test - transparent blue rect atop transparent green rect atop
323// transparent red rect
324static int test4(std::vector<const Cmd*>* test, std::vector<int>* expectedOrder) {
325 // All in back-to-front order bc they're all transparent
326 expectedOrder->push_back(0);
327 expectedOrder->push_back(1);
328 expectedOrder->push_back(2);
329
330 FakeStateTracker s;
331 sk_sp<FakeMCBlob> state = s.snapState();
332
333 SkIRect r{0, 0, 100, 100};
334 test->push_back(new RectCmd(0, kSolidMat, r.makeOffset(8, 8), 0x80FF0000, SK_ColorUNUSED, state));
335 test->push_back(new RectCmd(1, kSolidMat, r.makeOffset(48, 48), 0x8000FF00, SK_ColorUNUSED, state));
336 test->push_back(new RectCmd(2, kSolidMat, r.makeOffset(98, 98), 0x800000FF, SK_ColorUNUSED, state));
337 return 4;
338}
339
340// Multiple opaque materials test
341// All opaque:
342// normal1, linear1, radial1, normal2, linear2, radial2
343// Which gets sorted to:
344// normal2, normal1, linear2, linear1, radial2, radial1
345// So, front to back w/in each material type.
346static int test5(std::vector<const Cmd*>* test, std::vector<int>* expectedOrder) {
347 // Note: This pushes sorting by material above sorting by Z. Thus we'll get less front to
348 // back benefit.
349 expectedOrder->push_back(3);
350 expectedOrder->push_back(0);
351 expectedOrder->push_back(4);
352 expectedOrder->push_back(1);
353 expectedOrder->push_back(5);
354 expectedOrder->push_back(2);
355
356 FakeStateTracker s;
357 sk_sp<FakeMCBlob> state = s.snapState();
358
359 SkIRect r{0, 0, 100, 100};
360 test->push_back(new RectCmd(0, kSolidMat, r.makeOffset(8, 8), SK_ColorRED, SK_ColorUNUSED, state));
361 test->push_back(new RectCmd(1, kLinearMat, r.makeOffset(48, 48), SK_ColorGREEN, SK_ColorWHITE, state));
362 test->push_back(new RectCmd(2, kRadialMat, r.makeOffset(98, 98), SK_ColorBLUE, SK_ColorBLACK, state));
363 test->push_back(new RectCmd(3, kSolidMat, r.makeOffset(148, 148), SK_ColorCYAN, SK_ColorUNUSED, state));
364 test->push_back(new RectCmd(4, kLinearMat, r.makeOffset(148, 8), SK_ColorMAGENTA, SK_ColorWHITE, state));
365 test->push_back(new RectCmd(5, kRadialMat, r.makeOffset(8, 148), SK_ColorYELLOW, SK_ColorBLACK, state));
366 return 5;
367}
368
369// simple clipping test - 1 clip rect
370static int test6(std::vector<const Cmd*>* test, std::vector<int>* expectedOrder) {
371 expectedOrder->push_back(1);
372 expectedOrder->push_back(0);
373
374 FakeStateTracker s;
375 s.clipRect(SkIRect::MakeXYWH(28, 28, 40, 40));
376
377 sk_sp<FakeMCBlob> state = s.snapState();
378
379 SkIRect r{0, 0, 100, 100};
Robert Phillips77ecfc92021-05-24 14:41:24 -0400380 test->push_back(new RectCmd(0, kSolidMat, r.makeOffset(8, 8), SK_ColorRED, SK_ColorUNUSED, state));
381 test->push_back(new RectCmd(1, kSolidMat, r.makeOffset(48, 48), SK_ColorGREEN, SK_ColorUNUSED, state));
Robert Phillips4b732282021-05-20 09:45:10 -0400382 return 6;
383}
384
Robert Phillips77ecfc92021-05-24 14:41:24 -0400385// more complicated clipping w/ opaque draws -> should reorder
386static int test7(std::vector<const Cmd*>* test, std::vector<int>* expectedOrder) {
387 // The expected is front to back modulated by the two clip states
388 expectedOrder->push_back(5);
389 expectedOrder->push_back(4);
390 expectedOrder->push_back(1);
391 expectedOrder->push_back(0);
392
393 expectedOrder->push_back(3);
394 expectedOrder->push_back(2);
395
396 FakeStateTracker s;
397 s.clipRect(SkIRect::MakeXYWH(85, 0, 86, 256)); // select the middle third in x
398
399 sk_sp<FakeMCBlob> state = s.snapState();
400
401 SkIRect r{0, 0, 100, 100};
402 test->push_back(new RectCmd(0, kSolidMat, r.makeOffset(8, 8), SK_ColorRED, SK_ColorUNUSED, state));
403 test->push_back(new RectCmd(1, kSolidMat, r.makeOffset(48, 48), SK_ColorGREEN, SK_ColorUNUSED, state));
404
405 s.push();
406 s.clipRect(SkIRect::MakeXYWH(0, 85, 256, 86)); // intersect w/ the middle third in y
407 state = s.snapState();
408
409 test->push_back(new RectCmd(2, kSolidMat, r.makeOffset(98, 98), SK_ColorBLUE, SK_ColorUNUSED, state));
410 test->push_back(new RectCmd(3, kSolidMat, r.makeOffset(148, 148), SK_ColorCYAN, SK_ColorUNUSED, state));
411
412 s.pop();
413 state = s.snapState();
414
415 test->push_back(new RectCmd(4, kSolidMat, r.makeOffset(148, 8), SK_ColorMAGENTA, SK_ColorUNUSED, state));
416 test->push_back(new RectCmd(5, kSolidMat, r.makeOffset(8, 148), SK_ColorYELLOW, SK_ColorUNUSED, state));
417 return 7;
418}
419
420
Robert Phillips4b732282021-05-20 09:45:10 -0400421int main(int argc, char** argv) {
422 CommandLineFlags::Parse(argc, argv);
423
424 SkGraphics::Init();
425
426 key_test();
427 mcstack_test();
428 sort_test(test1);
429 sort_test(test2);
430 sort_test(test3);
431 sort_test(test4);
432 sort_test(test5);
433 sort_test(test6);
Robert Phillips77ecfc92021-05-24 14:41:24 -0400434 sort_test(test7);
Robert Phillips4b732282021-05-20 09:45:10 -0400435
436 return 0;
437}