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 | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 40 | // We only really need the DC, but currently the DC doesn't own the RT so we also ref it, but that |
| 41 | // could be dropped when DC is a proper owner of its RT. |
| 42 | static bool reset_dc(sk_sp<GrDrawContext>* dc, SkAutoTUnref<GrSurface>* rtKeepAlive, |
| 43 | GrContext* context, int w, int h) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 44 | SkDEBUGCODE(uint32_t oldID = 0;) |
| 45 | if (*dc) { |
| 46 | SkDEBUGCODE(oldID = (*dc)->accessRenderTarget()->getUniqueID();) |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 47 | rtKeepAlive->reset(nullptr); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 48 | dc->reset(nullptr); |
| 49 | } |
| 50 | context->freeGpuResources(); |
| 51 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 52 | GrTextureDesc desc; |
| 53 | desc.fWidth = w; |
| 54 | desc.fHeight = h; |
| 55 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 56 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 57 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 58 | rtKeepAlive->reset(context->textureProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 59 | if (!(*rtKeepAlive)) { |
| 60 | return false; |
| 61 | } |
| 62 | GrRenderTarget* rt = (*rtKeepAlive)->asRenderTarget(); |
| 63 | SkASSERT(rt->getUniqueID() != oldID); |
| 64 | *dc = context->drawContext(sk_ref_sp(rt)); |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 65 | return *dc != nullptr; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 66 | } |
| 67 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 68 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearBatch, reporter, ctxInfo) { |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 69 | GrContext* context = ctxInfo.fGrContext; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 70 | static const int kW = 10; |
| 71 | static const int kH = 10; |
| 72 | |
| 73 | SkIRect fullRect = SkIRect::MakeWH(kW, kH); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 74 | sk_sp<GrDrawContext> drawContext; |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 75 | SkAutoTUnref<GrSurface> rtKeepAlive; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 76 | |
| 77 | // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround |
| 78 | // it. |
| 79 | SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2); |
| 80 | SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH); |
| 81 | SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1); |
| 82 | SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH); |
| 83 | SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1); |
| 84 | |
| 85 | // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround |
| 86 | // it. |
| 87 | SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4); |
| 88 | SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2); |
| 89 | SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1); |
| 90 | SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2); |
| 91 | SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1); |
| 92 | |
| 93 | uint32_t actualValue; |
| 94 | int failX, failY; |
| 95 | |
| 96 | static const GrColor kColor1 = 0xABCDEF01; |
| 97 | static const GrColor kColor2 = ~kColor1; |
| 98 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 99 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 100 | ERRORF(reporter, "Could not create draw context."); |
| 101 | return; |
| 102 | } |
| 103 | // Check a full clear |
| 104 | drawContext->clear(&fullRect, kColor1, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 105 | if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 106 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 107 | failX, failY); |
| 108 | } |
| 109 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 110 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 111 | ERRORF(reporter, "Could not create draw context."); |
| 112 | return; |
| 113 | } |
| 114 | // Check two full clears, same color |
| 115 | drawContext->clear(&fullRect, kColor1, false); |
| 116 | drawContext->clear(&fullRect, kColor1, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 117 | if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 118 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 119 | failX, failY); |
| 120 | } |
| 121 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 122 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 123 | ERRORF(reporter, "Could not create draw context."); |
| 124 | return; |
| 125 | } |
| 126 | // Check two full clears, different colors |
| 127 | drawContext->clear(&fullRect, kColor1, false); |
| 128 | drawContext->clear(&fullRect, kColor2, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 129 | if (!check_rect(drawContext.get(), fullRect, kColor2, &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).", kColor2, actualValue, |
| 131 | failX, failY); |
| 132 | } |
| 133 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 134 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 135 | ERRORF(reporter, "Could not create draw context."); |
| 136 | return; |
| 137 | } |
| 138 | // Test a full clear followed by a same color inset clear |
| 139 | drawContext->clear(&fullRect, kColor1, false); |
| 140 | drawContext->clear(&mid1Rect, kColor1, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 141 | if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 142 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 143 | failX, failY); |
| 144 | } |
| 145 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 146 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 147 | ERRORF(reporter, "Could not create draw context."); |
| 148 | return; |
| 149 | } |
| 150 | // Test a inset clear followed by same color full clear |
| 151 | drawContext->clear(&mid1Rect, kColor1, false); |
| 152 | drawContext->clear(&fullRect, kColor1, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 153 | if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 154 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 155 | failX, failY); |
| 156 | } |
| 157 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 158 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 159 | ERRORF(reporter, "Could not create draw context."); |
| 160 | return; |
| 161 | } |
| 162 | // Test a full clear followed by a different color inset clear |
| 163 | drawContext->clear(&fullRect, kColor1, false); |
| 164 | drawContext->clear(&mid1Rect, kColor2, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 165 | if (!check_rect(drawContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 166 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 167 | failX, failY); |
| 168 | } |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 169 | if (!check_rect(drawContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 170 | !check_rect(drawContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 171 | !check_rect(drawContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 172 | !check_rect(drawContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 173 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 174 | failX, failY); |
| 175 | } |
| 176 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 177 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 178 | ERRORF(reporter, "Could not create draw context."); |
| 179 | return; |
| 180 | } |
| 181 | // Test a inset clear followed by a different full clear |
| 182 | drawContext->clear(&mid1Rect, kColor2, false); |
| 183 | drawContext->clear(&fullRect, kColor1, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 184 | if (!check_rect(drawContext.get(), fullRect, 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 | } |
| 188 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 189 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 190 | ERRORF(reporter, "Could not create draw context."); |
| 191 | return; |
| 192 | } |
| 193 | // Check three nested clears from largest to smallest where outermost and innermost are same |
| 194 | // color. |
| 195 | drawContext->clear(&fullRect, kColor1, false); |
| 196 | drawContext->clear(&mid1Rect, kColor2, false); |
| 197 | drawContext->clear(&mid2Rect, kColor1, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 198 | if (!check_rect(drawContext.get(), mid2Rect, 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 | } |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 202 | if (!check_rect(drawContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) || |
| 203 | !check_rect(drawContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) || |
| 204 | !check_rect(drawContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) || |
| 205 | !check_rect(drawContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 206 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 207 | failX, failY); |
| 208 | } |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 209 | if (!check_rect(drawContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 210 | !check_rect(drawContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 211 | !check_rect(drawContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 212 | !check_rect(drawContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 213 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 214 | failX, failY); |
| 215 | } |
| 216 | |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 217 | if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 218 | ERRORF(reporter, "Could not create draw context."); |
| 219 | return; |
| 220 | } |
| 221 | // Swap the order of the second two clears in the above test. |
| 222 | drawContext->clear(&fullRect, kColor1, false); |
| 223 | drawContext->clear(&mid2Rect, kColor1, false); |
| 224 | drawContext->clear(&mid1Rect, kColor2, false); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 225 | if (!check_rect(drawContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 226 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 227 | failX, failY); |
| 228 | } |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 229 | if (!check_rect(drawContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 230 | !check_rect(drawContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 231 | !check_rect(drawContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 232 | !check_rect(drawContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 233 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 234 | failX, failY); |
| 235 | } |
| 236 | } |
| 237 | #endif |