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