xref: /aosp_15_r20/external/skia/tests/Skbug6653.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2017 Google Inc.
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/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSurface.h"
18 #include "include/core/SkTypes.h"
19 #include "include/gpu/GpuTypes.h"
20 #include "include/gpu/ganesh/GrDirectContext.h"
21 #include "include/gpu/ganesh/GrTypes.h"
22 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
23 #include "include/private/base/SkDebug.h"
24 #include "tests/CtsEnforcement.h"
25 #include "tests/Test.h"
26 
27 #include <cstdint>
28 
29 class GrRecordingContext;
30 struct GrContextOptions;
31 
read_pixels(sk_sp<SkSurface> surface,SkColor initColor)32 static SkBitmap read_pixels(sk_sp<SkSurface> surface, SkColor initColor) {
33     SkBitmap bmp;
34     bmp.allocN32Pixels(surface->width(), surface->height());
35     bmp.eraseColor(initColor);
36     if (!surface->readPixels(bmp, 0, 0)) {
37         SkDebugf("readPixels failed\n");
38     }
39     return bmp;
40 }
41 
make_surface(GrRecordingContext * rContext)42 static sk_sp<SkSurface> make_surface(GrRecordingContext* rContext) {
43     SkImageInfo info = SkImageInfo::Make(50, 50, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
44     return SkSurfaces::RenderTarget(
45             rContext, skgpu::Budgeted::kNo, info, 4, kBottomLeft_GrSurfaceOrigin, nullptr);
46 }
47 
test_bug_6653(GrDirectContext * dContext,skiatest::Reporter * reporter,const char * label)48 static void test_bug_6653(GrDirectContext* dContext,
49                           skiatest::Reporter* reporter,
50                           const char* label) {
51     SkRect rect = SkRect::MakeWH(50, 50);
52 
53     SkPaint paint;
54     paint.setColor(SK_ColorWHITE);
55     paint.setStrokeWidth(5);
56     paint.setStyle(SkPaint::kStroke_Style);
57 
58     // The one device that fails this test (Galaxy S6) does so in a flaky fashion. Trying many
59     // times makes it more likely to fail. Also, interacting with the phone (eg swiping between
60     // different home screens) while the test is running makes it fail close to 100%.
61     static const int kNumIterations = 50;
62 
63     for (int i = 0; i < kNumIterations; ++i) {
64         auto s0 = make_surface(dContext);
65         if (!s0) {
66             // MSAA may not be supported
67             return;
68         }
69 
70         auto s1 = make_surface(dContext);
71         s1->getCanvas()->clear(SK_ColorBLACK);
72         s1->getCanvas()->drawOval(rect, paint);
73         SkBitmap b1 = read_pixels(s1, SK_ColorBLACK);
74         s1 = nullptr;
75 
76         // The bug requires that all three of the following surfaces are cleared to the same color
77         auto s2 = make_surface(dContext);
78         s2->getCanvas()->clear(SK_ColorBLUE);
79         SkBitmap b2 = read_pixels(s2, SK_ColorBLACK);
80         s2 = nullptr;
81 
82         auto s3 = make_surface(dContext);
83         s3->getCanvas()->clear(SK_ColorBLUE);
84         s0->getCanvas()->drawImage(read_pixels(s3, SK_ColorBLACK).asImage(), 0, 0);
85         s3 = nullptr;
86 
87         auto s4 = make_surface(dContext);
88         s4->getCanvas()->clear(SK_ColorBLUE);
89         s4->getCanvas()->drawOval(rect, paint);
90 
91         // When this fails, b4 will "succeed", but return an empty bitmap (containing just the
92         // clear color). Regardless, b5 will contain the oval that was just drawn, so diffing the
93         // two bitmaps tests for the failure case. Initialize the bitmaps to different colors so
94         // that if the readPixels doesn't work, this test will always fail.
95         SkBitmap b4 = read_pixels(s4, SK_ColorRED);
96         SkBitmap b5 = read_pixels(s4, SK_ColorGREEN);
97 
98         bool match = true;
99         for (int y = 0; y < b4.height() && match; ++y) {
100             for (int x = 0; x < b4.width() && match; ++x) {
101                 uint32_t pixelA = *b4.getAddr32(x, y);
102                 uint32_t pixelB = *b5.getAddr32(x, y);
103                 if (pixelA != pixelB) {
104                     match = false;
105                 }
106             }
107         }
108 
109         REPORTER_ASSERT(reporter, match, "%s", label);
110     }
111 }
112 
113 // Tests that readPixels returns up-to-date results. This has failed on several GPUs,
114 // from multiple vendors, in MSAA mode.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(skbug6653,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)115 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(skbug6653, reporter, ctxInfo, CtsEnforcement::kApiLevel_T) {
116     auto ctx = ctxInfo.directContext();
117     test_bug_6653(ctx, reporter, "Default");
118 }
119