Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 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 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
| 12 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tests/Test.h" |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 15 | |
Brian Osman | ea30810 | 2018-09-11 16:39:11 -0400 | [diff] [blame] | 16 | static SkBitmap read_pixels(sk_sp<SkSurface> surface, SkColor initColor) { |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 17 | SkBitmap bmp; |
| 18 | bmp.allocN32Pixels(surface->width(), surface->height()); |
Brian Osman | ea30810 | 2018-09-11 16:39:11 -0400 | [diff] [blame] | 19 | bmp.eraseColor(initColor); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 20 | if (!surface->readPixels(bmp, 0, 0)) { |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 21 | SkDebugf("readPixels failed\n"); |
| 22 | } |
| 23 | return bmp; |
| 24 | } |
| 25 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 26 | static sk_sp<SkSurface> make_surface(GrRecordingContext* rContext) { |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 27 | SkImageInfo info = SkImageInfo::Make(50, 50, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 28 | return SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info, 4, |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 29 | kBottomLeft_GrSurfaceOrigin, nullptr); |
| 30 | } |
| 31 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 32 | static void test_bug_6653(GrDirectContext* dContext, |
| 33 | skiatest::Reporter* reporter, |
| 34 | const char* label) { |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 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) { |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 48 | auto s0 = make_surface(dContext); |
Brian Osman | bcf65ed | 2017-06-15 14:16:08 -0400 | [diff] [blame] | 49 | if (!s0) { |
| 50 | // MSAA may not be supported |
| 51 | return; |
| 52 | } |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 53 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 54 | auto s1 = make_surface(dContext); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 55 | s1->getCanvas()->clear(SK_ColorBLACK); |
| 56 | s1->getCanvas()->drawOval(rect, paint); |
Brian Osman | ea30810 | 2018-09-11 16:39:11 -0400 | [diff] [blame] | 57 | SkBitmap b1 = read_pixels(s1, SK_ColorBLACK); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 58 | s1 = nullptr; |
| 59 | |
| 60 | // The bug requires that all three of the following surfaces are cleared to the same color |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 61 | auto s2 = make_surface(dContext); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 62 | s2->getCanvas()->clear(SK_ColorBLUE); |
Brian Osman | ea30810 | 2018-09-11 16:39:11 -0400 | [diff] [blame] | 63 | SkBitmap b2 = read_pixels(s2, SK_ColorBLACK); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 64 | s2 = nullptr; |
| 65 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 66 | auto s3 = make_surface(dContext); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 67 | s3->getCanvas()->clear(SK_ColorBLUE); |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 68 | s0->getCanvas()->drawImage(read_pixels(s3, SK_ColorBLACK).asImage(), 0, 0); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 69 | s3 = nullptr; |
| 70 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 71 | auto s4 = make_surface(dContext); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 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 |
Brian Osman | ea30810 | 2018-09-11 16:39:11 -0400 | [diff] [blame] | 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); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 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 | |
Brian Osman | da01be3 | 2018-08-28 09:44:45 -0400 | [diff] [blame] | 93 | REPORTER_ASSERT(reporter, match, label); |
Brian Osman | eee3c09 | 2017-06-15 13:25:10 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
Brian Osman | dd04bec | 2018-08-27 10:02:00 -0400 | [diff] [blame] | 96 | |
| 97 | // Tests that readPixels returns up-to-date results. This has failed on several GPUs, |
| 98 | // from multiple vendors, in MSAA mode. |
| 99 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(skbug6653, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 100 | auto ctx = ctxInfo.directContext(); |
Brian Osman | da01be3 | 2018-08-28 09:44:45 -0400 | [diff] [blame] | 101 | test_bug_6653(ctx, reporter, "Default"); |
Brian Osman | dd04bec | 2018-08-27 10:02:00 -0400 | [diff] [blame] | 102 | } |
| 103 | |