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