blob: 2aae1db498e248ea9d324d747984d78c13b57fe5 [file] [log] [blame]
Brian Osmaneee3c092017-06-15 13:25:10 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040010#include "include/core/SkTypes.h"
11#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
13#include "tests/Test.h"
Brian Osmaneee3c092017-06-15 13:25:10 -040014
Brian Osmanea308102018-09-11 16:39:11 -040015static SkBitmap read_pixels(sk_sp<SkSurface> surface, SkColor initColor) {
Brian Osmaneee3c092017-06-15 13:25:10 -040016 SkBitmap bmp;
17 bmp.allocN32Pixels(surface->width(), surface->height());
Brian Osmanea308102018-09-11 16:39:11 -040018 bmp.eraseColor(initColor);
Mike Reedf1942192017-07-21 14:24:29 -040019 if (!surface->readPixels(bmp, 0, 0)) {
Brian Osmaneee3c092017-06-15 13:25:10 -040020 SkDebugf("readPixels failed\n");
21 }
22 return bmp;
23}
24
Robert Phillipse94b4e12020-07-23 13:54:35 -040025static sk_sp<SkSurface> make_surface(GrRecordingContext* rContext) {
Brian Osmaneee3c092017-06-15 13:25:10 -040026 SkImageInfo info = SkImageInfo::Make(50, 50, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -040027 return SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info, 4,
Brian Osmaneee3c092017-06-15 13:25:10 -040028 kBottomLeft_GrSurfaceOrigin, nullptr);
29}
30
Robert Phillipse94b4e12020-07-23 13:54:35 -040031static void test_bug_6653(GrDirectContext* dContext,
32 skiatest::Reporter* reporter,
33 const char* label) {
Brian Osmaneee3c092017-06-15 13:25:10 -040034 SkRect rect = SkRect::MakeWH(50, 50);
35
36 SkPaint paint;
37 paint.setColor(SK_ColorWHITE);
38 paint.setStrokeWidth(5);
39 paint.setStyle(SkPaint::kStroke_Style);
40
41 // The one device that fails this test (Galaxy S6) does so in a flaky fashion. Trying many
42 // times makes it more likely to fail. Also, interacting with the phone (eg swiping between
43 // different home screens) while the test is running makes it fail close to 100%.
44 static const int kNumIterations = 50;
45
46 for (int i = 0; i < kNumIterations; ++i) {
Robert Phillipse94b4e12020-07-23 13:54:35 -040047 auto s0 = make_surface(dContext);
Brian Osmanbcf65ed2017-06-15 14:16:08 -040048 if (!s0) {
49 // MSAA may not be supported
50 return;
51 }
Brian Osmaneee3c092017-06-15 13:25:10 -040052
Robert Phillipse94b4e12020-07-23 13:54:35 -040053 auto s1 = make_surface(dContext);
Brian Osmaneee3c092017-06-15 13:25:10 -040054 s1->getCanvas()->clear(SK_ColorBLACK);
55 s1->getCanvas()->drawOval(rect, paint);
Brian Osmanea308102018-09-11 16:39:11 -040056 SkBitmap b1 = read_pixels(s1, SK_ColorBLACK);
Brian Osmaneee3c092017-06-15 13:25:10 -040057 s1 = nullptr;
58
59 // The bug requires that all three of the following surfaces are cleared to the same color
Robert Phillipse94b4e12020-07-23 13:54:35 -040060 auto s2 = make_surface(dContext);
Brian Osmaneee3c092017-06-15 13:25:10 -040061 s2->getCanvas()->clear(SK_ColorBLUE);
Brian Osmanea308102018-09-11 16:39:11 -040062 SkBitmap b2 = read_pixels(s2, SK_ColorBLACK);
Brian Osmaneee3c092017-06-15 13:25:10 -040063 s2 = nullptr;
64
Robert Phillipse94b4e12020-07-23 13:54:35 -040065 auto s3 = make_surface(dContext);
Brian Osmaneee3c092017-06-15 13:25:10 -040066 s3->getCanvas()->clear(SK_ColorBLUE);
Brian Osmanea308102018-09-11 16:39:11 -040067 SkBitmap b3 = read_pixels(s3, SK_ColorBLACK);
Brian Osmaneee3c092017-06-15 13:25:10 -040068 s0->getCanvas()->drawBitmap(b3, 0, 0);
69 s3 = nullptr;
70
Robert Phillipse94b4e12020-07-23 13:54:35 -040071 auto s4 = make_surface(dContext);
Brian Osmaneee3c092017-06-15 13:25:10 -040072 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
Brian Osmanea308102018-09-11 16:39:11 -040077 // 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);
Brian Osmaneee3c092017-06-15 13:25:10 -040081
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
Brian Osmanda01be32018-08-28 09:44:45 -040093 REPORTER_ASSERT(reporter, match, label);
Brian Osmaneee3c092017-06-15 13:25:10 -040094 }
95}
Brian Osmandd04bec2018-08-27 10:02:00 -040096
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) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400100 auto ctx = ctxInfo.directContext();
Brian Osmanda01be32018-08-28 09:44:45 -0400101 test_bug_6653(ctx, reporter, "Default");
Brian Osmandd04bec2018-08-27 10:02:00 -0400102}
103