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