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