blob: 1371db4fd54aef2bc2870f0356e9b048daf148ee [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
8#include "SkTypes.h"
9
10#include "SkCanvas.h"
11#include "SkSurface.h"
12
Brian Osmaneee3c092017-06-15 13:25:10 -040013#include "GrContext.h"
Brian Osmandd04bec2018-08-27 10:02:00 -040014#include "GrContextPriv.h"
Brian Osmaneee3c092017-06-15 13:25:10 -040015#include "GrTest.h"
16#include "Test.h"
17
18static SkBitmap read_pixels(sk_sp<SkSurface> surface) {
19 SkBitmap bmp;
20 bmp.allocN32Pixels(surface->width(), surface->height());
Mike Reedf1942192017-07-21 14:24:29 -040021 if (!surface->readPixels(bmp, 0, 0)) {
Brian Osmaneee3c092017-06-15 13:25:10 -040022 SkDebugf("readPixels failed\n");
23 }
24 return bmp;
25}
26
27static sk_sp<SkSurface> make_surface(GrContext* context) {
28 SkImageInfo info = SkImageInfo::Make(50, 50, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
29 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 4,
30 kBottomLeft_GrSurfaceOrigin, nullptr);
31}
32
Brian Osmandd04bec2018-08-27 10:02:00 -040033static void test_bug_6653(GrContext* ctx, skiatest::Reporter* reporter) {
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) {
47 auto s0 = make_surface(ctx);
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
53 auto s1 = make_surface(ctx);
54 s1->getCanvas()->clear(SK_ColorBLACK);
55 s1->getCanvas()->drawOval(rect, paint);
56 SkBitmap b1 = read_pixels(s1);
57 s1 = nullptr;
58
59 // The bug requires that all three of the following surfaces are cleared to the same color
60 auto s2 = make_surface(ctx);
61 s2->getCanvas()->clear(SK_ColorBLUE);
62 SkBitmap b2 = read_pixels(s2);
63 s2 = nullptr;
64
65 auto s3 = make_surface(ctx);
66 s3->getCanvas()->clear(SK_ColorBLUE);
67 SkBitmap b3 = read_pixels(s3);
68 s0->getCanvas()->drawBitmap(b3, 0, 0);
69 s3 = nullptr;
70
71 auto s4 = make_surface(ctx);
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.
78 SkBitmap b4 = read_pixels(s4);
79 SkBitmap b5 = read_pixels(s4);
80
81 bool match = true;
82 for (int y = 0; y < b4.height() && match; ++y) {
83 for (int x = 0; x < b4.width() && match; ++x) {
84 uint32_t pixelA = *b4.getAddr32(x, y);
85 uint32_t pixelB = *b5.getAddr32(x, y);
86 if (pixelA != pixelB) {
87 match = false;
88 }
89 }
90 }
91
92 REPORTER_ASSERT(reporter, match);
93 }
94}
Brian Osmandd04bec2018-08-27 10:02:00 -040095
96// Tests that readPixels returns up-to-date results. This has failed on several GPUs,
97// from multiple vendors, in MSAA mode.
98DEF_GPUTEST_FOR_RENDERING_CONTEXTS(skbug6653, reporter, ctxInfo) {
99 GrContext* ctx = ctxInfo.grContext();
100 test_bug_6653(ctx, reporter);
101}
102
103// Same as above, but without explicit resource allocation.
104DEF_GPUTEST_FOR_RENDERING_CONTEXTS(skbug6653_noExplicitResourceAllocation, reporter, ctxInfo) {
105 GrContext* ctx = ctxInfo.grContext();
106 ctx->flush();
107 ctx->contextPriv().resourceProvider()->testingOnly_setExplicitlyAllocateGPUResources(false);
108 test_bug_6653(ctx, reporter);
109}