• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 "tests/Test.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/gpu/graphite/Context.h"
12 #include "include/gpu/graphite/Recording.h"
13 #include "src/gpu/graphite/ContextPriv.h"
14 #include "src/gpu/graphite/Surface_Graphite.h"
15 #include "tests/TestUtils.h"
16 #include "tools/ToolUtils.h"
17 
18 using namespace skgpu::graphite;
19 
20 namespace {
21 
22 const SkISize kSurfaceSize = { 64, 64 };
23 
24 constexpr SkColor4f kBackgroundColor = SkColors::kWhite;
25 
run_test(skiatest::Reporter * reporter,Context * context,Recorder * recorder)26 bool run_test(skiatest::Reporter* reporter,
27               Context* context,
28               Recorder* recorder) {
29     SkImageInfo ii = SkImageInfo::Make(kSurfaceSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
30 
31     SkBitmap result0, result1;
32     result0.allocPixels(ii);
33     result1.allocPixels(ii);
34     SkPixmap pm0, pm1;
35 
36     SkAssertResult(result0.peekPixels(&pm0));
37     SkAssertResult(result1.peekPixels(&pm1));
38 
39     sk_sp<SkSurface> surface = SkSurface::MakeGraphite(recorder, ii);
40     if (!surface) {
41         ERRORF(reporter, "Surface creation failed");
42         return false;
43     }
44 
45     SkCanvas* canvas = surface->getCanvas();
46 
47     // Set up a recording to clear the surface
48     canvas->clear(kBackgroundColor);
49     std::unique_ptr<Recording> clearRecording = recorder->snap();
50     if (!clearRecording) {
51         ERRORF(reporter, "Recording creation failed");
52         return false;
53     }
54 
55     // Draw some text and get recording
56     SkPaint paint;
57     paint.setAntiAlias(true);
58 
59     SkFont font(ToolUtils::create_portable_typeface("serif", SkFontStyle()));
60     font.setSubpixel(true);
61     font.setSize(12);
62 
63     const char* text0 = "Hamburge";
64     const size_t text0Len = strlen(text0);
65 
66     canvas->drawSimpleText(text0, text0Len, SkTextEncoding::kUTF8, 3, 20, font, paint);
67     std::unique_ptr<Recording> text0Recording = recorder->snap();
68 
69     // Draw some more text and get recording
70     const char* text1 = "burgefons";
71     const size_t text1Len = strlen(text1);
72 
73     canvas->drawSimpleText(text1, text1Len, SkTextEncoding::kUTF8, 3, 40, font, paint);
74     std::unique_ptr<Recording> text1Recording = recorder->snap();
75 
76     // Playback 0, then 1, and read pixels
77     InsertRecordingInfo info;
78     info.fRecording = clearRecording.get();
79     context->insertRecording(info);
80     info.fRecording = text0Recording.get();
81     context->insertRecording(info);
82     info.fRecording = text1Recording.get();
83     context->insertRecording(info);
84     context->submit();
85 
86     if (!surface->readPixels(pm0, 0, 0)) {
87         ERRORF(reporter, "readPixels failed");
88         return false;
89     }
90 
91     // Playback 1, then 0, and read pixels
92     info.fRecording = clearRecording.get();
93     context->insertRecording(info);
94     info.fRecording = text1Recording.get();
95     context->insertRecording(info);
96     info.fRecording = text0Recording.get();
97     context->insertRecording(info);
98     context->submit();
99 
100     if (!surface->readPixels(pm1, 0, 0)) {
101         ERRORF(reporter, "readPixels failed");
102         return false;
103     }
104 
105     // Compare and contrast
106     float tol = 1.f/256;
107     const float tols[4] = {tol, tol, tol, tol};
108     auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y,
109                                                                 const float diffs[4]) {
110         SkASSERT(x >= 0 && y >= 0);
111         ERRORF(reporter,
112                "Error at %d, %d. Diff in floats: (%f, %f, %f, %f)",
113                x, y, diffs[0], diffs[1], diffs[2], diffs[3]);
114     });
115     ComparePixels(pm0, pm1, tols, error);
116 
117     return true;
118 }
119 
120 } // anonymous namespace
121 
122 // This test captures two recordings A and B, plays them back as A then B, and B then A,
123 // and verifies that the result is the same.
DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(RecordingOrderTest_Graphite,reporter,context)124 DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(RecordingOrderTest_Graphite, reporter, context) {
125     std::unique_ptr<Recorder> recorder = context->makeRecorder();
126 
127     (void) run_test(reporter, context, recorder.get());
128 }
129