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