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