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/core/SkCanvas.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/gpu/graphite/Context.h"
15 #include "include/gpu/graphite/Recorder.h"
16 #include "include/gpu/graphite/Recording.h"
17 #include "src/gpu/graphite/Surface_Graphite.h"
18
19 namespace skgpu::graphite {
20
21 // Tests that a drawing with MSAA will have contents retained between recordings.
22 // This is for testing MSAA load from resolve feature.
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(MultisampleRetainTest,reporter,context)23 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(MultisampleRetainTest, reporter, context) {
24 const SkImageInfo surfaceImageInfo = SkImageInfo::Make(
25 16, 16, SkColorType::kRGBA_8888_SkColorType, SkAlphaType::kPremul_SkAlphaType);
26
27 std::unique_ptr<Recorder> surfaceRecorder = context->makeRecorder();
28 sk_sp<SkSurface> surface = SkSurface::MakeGraphite(surfaceRecorder.get(), surfaceImageInfo);
29
30 // Clear entire surface to red
31 SkCanvas* surfaceCanvas = surface->getCanvas();
32 surfaceCanvas->clear(SkColors::kRed);
33 std::unique_ptr<Recording> surfaceRecording = surfaceRecorder->snap();
34 // Flush the clearing
35 context->insertRecording({surfaceRecording.get()});
36
37 // Draw a blue path. The old red background should be retained between recordings.
38 SkPaint paint;
39 paint.setStrokeWidth(3);
40 paint.setColor(SkColors::kBlue);
41 paint.setStyle(SkPaint::Style::kStroke_Style);
42
43 SkPath path;
44 constexpr int kPathPoints[][2] = {
45 {3, 2},
46 {3, 4},
47 {6, 8},
48 {3, 15},
49 };
50
51 for (size_t i = 0; i < std::size(kPathPoints); ++i) {
52 path.lineTo(kPathPoints[i][0], kPathPoints[i][1]);
53 }
54
55 surfaceCanvas->drawPath(path, paint);
56
57 std::unique_ptr<Recording> surfaceRecording2 = surfaceRecorder->snap();
58 // Play back recording.
59 context->insertRecording({surfaceRecording2.get()});
60
61 // Read pixels.
62 SkBitmap bitmap;
63 bitmap.allocPixels(surfaceImageInfo);
64 if (!surface->readPixels(bitmap, 0, 0)) {
65 ERRORF(reporter, "readPixels failed");
66 return;
67 }
68
69 // Verify recording was replayed.
70 REPORTER_ASSERT(reporter, bitmap.getColor4f(8, 0) == SkColors::kRed);
71 REPORTER_ASSERT(reporter, bitmap.getColor4f(0, 8) == SkColors::kRed);
72 REPORTER_ASSERT(reporter, bitmap.getColor4f(15, 14) == SkColors::kRed);
73
74 // Verify points on the path have blue color. We don't verify last point because it is on the
75 // edge of the path thus might have blurry color.
76 for (size_t i = 0; i < std::size(kPathPoints) - 1; ++i) {
77 REPORTER_ASSERT(reporter,
78 bitmap.getColor4f(kPathPoints[i][0], kPathPoints[i][1]) == SkColors::kBlue);
79 }
80 }
81
82 } // namespace skgpu::graphite
83