bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "Test.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | #include "GrContext.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 12 | #include "GrRenderTargetContext.h" |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 13 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 14 | static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue, |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 15 | uint32_t* actualValue, int* failX, int* failY) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 16 | int w = rect.width(); |
| 17 | int h = rect.height(); |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 18 | std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 19 | memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 20 | |
| 21 | SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 22 | |
| 23 | if (!rtc->readPixels(dstInfo, pixels.get(), 0, rect.fLeft, rect.fTop)) { |
| 24 | return false; |
| 25 | } |
| 26 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 27 | for (int y = 0; y < h; ++y) { |
| 28 | for (int x = 0; x < w; ++x) { |
| 29 | uint32_t pixel = pixels.get()[y * w + x]; |
| 30 | if (pixel != expectedValue) { |
| 31 | *actualValue = pixel; |
| 32 | *failX = x + rect.fLeft; |
| 33 | *failY = y + rect.fTop; |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | return true; |
| 39 | } |
| 40 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 41 | sk_sp<GrRenderTargetContext> newRTC(GrContext* context, int w, int h) { |
| 42 | return context->makeDeferredRenderTargetContext(SkBackingFit::kExact, w, h, |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 43 | kRGBA_8888_GrPixelConfig, nullptr); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 46 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 47 | GrContext* context = ctxInfo.grContext(); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 48 | static const int kW = 10; |
| 49 | static const int kH = 10; |
| 50 | |
| 51 | SkIRect fullRect = SkIRect::MakeWH(kW, kH); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 52 | sk_sp<GrRenderTargetContext> rtContext; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 53 | |
| 54 | // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround |
| 55 | // it. |
| 56 | SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2); |
| 57 | SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH); |
| 58 | SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1); |
| 59 | SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH); |
| 60 | SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1); |
| 61 | |
| 62 | // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround |
| 63 | // it. |
| 64 | SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4); |
| 65 | SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2); |
| 66 | SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1); |
| 67 | SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2); |
| 68 | SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1); |
| 69 | |
| 70 | uint32_t actualValue; |
| 71 | int failX, failY; |
| 72 | |
| 73 | static const GrColor kColor1 = 0xABCDEF01; |
| 74 | static const GrColor kColor2 = ~kColor1; |
| 75 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 76 | rtContext = newRTC(context, kW, kH); |
| 77 | SkASSERT(rtContext); |
| 78 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 79 | // Check a full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 80 | rtContext->clear(&fullRect, kColor1, false); |
| 81 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 82 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 83 | failX, failY); |
| 84 | } |
| 85 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 86 | rtContext = newRTC(context, kW, kH); |
| 87 | SkASSERT(rtContext); |
| 88 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 89 | // Check two full clears, same color |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 90 | rtContext->clear(&fullRect, kColor1, false); |
| 91 | rtContext->clear(&fullRect, kColor1, false); |
| 92 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 93 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 94 | failX, failY); |
| 95 | } |
| 96 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 97 | rtContext = newRTC(context, kW, kH); |
| 98 | SkASSERT(rtContext); |
| 99 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 100 | // Check two full clears, different colors |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 101 | rtContext->clear(&fullRect, kColor1, false); |
| 102 | rtContext->clear(&fullRect, kColor2, false); |
| 103 | if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 104 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 105 | failX, failY); |
| 106 | } |
| 107 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 108 | rtContext = newRTC(context, kW, kH); |
| 109 | SkASSERT(rtContext); |
| 110 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 111 | // Test a full clear followed by a same color inset clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 112 | rtContext->clear(&fullRect, kColor1, false); |
| 113 | rtContext->clear(&mid1Rect, kColor1, false); |
| 114 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 115 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 116 | failX, failY); |
| 117 | } |
| 118 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 119 | rtContext = newRTC(context, kW, kH); |
| 120 | SkASSERT(rtContext); |
| 121 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 122 | // Test a inset clear followed by same color full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 123 | rtContext->clear(&mid1Rect, kColor1, false); |
| 124 | rtContext->clear(&fullRect, kColor1, false); |
| 125 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 126 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 127 | failX, failY); |
| 128 | } |
| 129 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 130 | rtContext = newRTC(context, kW, kH); |
| 131 | SkASSERT(rtContext); |
| 132 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 133 | // Test a full clear followed by a different color inset clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 134 | rtContext->clear(&fullRect, kColor1, false); |
| 135 | rtContext->clear(&mid1Rect, kColor2, false); |
| 136 | if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 137 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 138 | failX, failY); |
| 139 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 140 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 141 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 142 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 143 | !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 144 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 145 | failX, failY); |
| 146 | } |
| 147 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 148 | rtContext = newRTC(context, kW, kH); |
| 149 | SkASSERT(rtContext); |
| 150 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 151 | // Test a inset clear followed by a different full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 152 | rtContext->clear(&mid1Rect, kColor2, false); |
| 153 | rtContext->clear(&fullRect, kColor1, false); |
| 154 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 155 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 156 | failX, failY); |
| 157 | } |
| 158 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 159 | rtContext = newRTC(context, kW, kH); |
| 160 | SkASSERT(rtContext); |
| 161 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 162 | // Check three nested clears from largest to smallest where outermost and innermost are same |
| 163 | // color. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 164 | rtContext->clear(&fullRect, kColor1, false); |
| 165 | rtContext->clear(&mid1Rect, kColor2, false); |
| 166 | rtContext->clear(&mid2Rect, kColor1, false); |
| 167 | if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 168 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 169 | failX, failY); |
| 170 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 171 | if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) || |
| 172 | !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) || |
| 173 | !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) || |
| 174 | !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 175 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 176 | failX, failY); |
| 177 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 178 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 179 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 180 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 181 | !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 182 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 183 | failX, failY); |
| 184 | } |
| 185 | |
Robert Phillips | e4d45bf | 2017-06-02 14:53:40 -0400 | [diff] [blame] | 186 | rtContext = newRTC(context, kW, kH); |
| 187 | SkASSERT(rtContext); |
| 188 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 189 | // Swap the order of the second two clears in the above test. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 190 | rtContext->clear(&fullRect, kColor1, false); |
| 191 | rtContext->clear(&mid2Rect, kColor1, false); |
| 192 | rtContext->clear(&mid1Rect, kColor2, false); |
| 193 | if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 194 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 195 | failX, failY); |
| 196 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 197 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 198 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 199 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 200 | !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 201 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 202 | failX, failY); |
| 203 | } |
| 204 | } |
| 205 | #endif |