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