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(); |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 23 | std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 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 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame^] | 40 | // TODO: this test does this thorough purging of the rendertargets b.c. right now |
| 41 | // the clear optimizations rely on the rendertarget's uniqueID. It can be |
| 42 | // relaxed when we switch that over to using rendertargetcontext ids (although |
| 43 | // we probably will want to have more clear values then too) |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 44 | static bool reset_rtc(sk_sp<GrRenderTargetContext>* rtc, GrContext* context, int w, int h) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame^] | 45 | #ifdef SK_DEBUG |
| 46 | GrGpuResource::UniqueID oldID = GrGpuResource::UniqueID::InvalidID(); |
| 47 | #endif |
| 48 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 49 | if (*rtc) { |
| 50 | SkDEBUGCODE(oldID = (*rtc)->accessRenderTarget()->uniqueID();) |
| 51 | rtc->reset(nullptr); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 52 | } |
| 53 | context->freeGpuResources(); |
| 54 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 55 | *rtc = context->makeRenderTargetContext(SkBackingFit::kExact, w, h, kRGBA_8888_GrPixelConfig, |
| 56 | nullptr); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 57 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 58 | SkASSERT((*rtc)->accessRenderTarget()->uniqueID() != oldID); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 59 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 60 | return *rtc != nullptr; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 61 | } |
| 62 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 63 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearBatch, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 64 | GrContext* context = ctxInfo.grContext(); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 65 | static const int kW = 10; |
| 66 | static const int kH = 10; |
| 67 | |
| 68 | SkIRect fullRect = SkIRect::MakeWH(kW, kH); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 69 | sk_sp<GrRenderTargetContext> rtContext; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 70 | |
| 71 | // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround |
| 72 | // it. |
| 73 | SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2); |
| 74 | SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH); |
| 75 | SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1); |
| 76 | SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH); |
| 77 | SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1); |
| 78 | |
| 79 | // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround |
| 80 | // it. |
| 81 | SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4); |
| 82 | SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2); |
| 83 | SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1); |
| 84 | SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2); |
| 85 | SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1); |
| 86 | |
| 87 | uint32_t actualValue; |
| 88 | int failX, failY; |
| 89 | |
| 90 | static const GrColor kColor1 = 0xABCDEF01; |
| 91 | static const GrColor kColor2 = ~kColor1; |
| 92 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 93 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 94 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | // Check a full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 98 | rtContext->clear(&fullRect, kColor1, false); |
| 99 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 100 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 101 | failX, failY); |
| 102 | } |
| 103 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 104 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 105 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | // Check two full clears, same color |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 109 | rtContext->clear(&fullRect, kColor1, false); |
| 110 | rtContext->clear(&fullRect, kColor1, false); |
| 111 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 112 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 113 | failX, failY); |
| 114 | } |
| 115 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 116 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 117 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | // Check two full clears, different colors |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 121 | rtContext->clear(&fullRect, kColor1, false); |
| 122 | rtContext->clear(&fullRect, kColor2, false); |
| 123 | if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 124 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 125 | failX, failY); |
| 126 | } |
| 127 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 128 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 129 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | // Test a full clear followed by a same color inset clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 133 | rtContext->clear(&fullRect, kColor1, false); |
| 134 | rtContext->clear(&mid1Rect, kColor1, false); |
| 135 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 136 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 137 | failX, failY); |
| 138 | } |
| 139 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 140 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 141 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | // Test a inset clear followed by same color full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 145 | rtContext->clear(&mid1Rect, kColor1, false); |
| 146 | rtContext->clear(&fullRect, kColor1, false); |
| 147 | if (!check_rect(rtContext.get(), fullRect, 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 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 152 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 153 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | // Test a full clear followed by a different color inset clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 157 | rtContext->clear(&fullRect, kColor1, false); |
| 158 | rtContext->clear(&mid1Rect, kColor2, false); |
| 159 | if (!check_rect(rtContext.get(), mid1Rect, kColor2, &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).", kColor2, actualValue, |
| 161 | failX, failY); |
| 162 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 163 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 164 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 165 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 166 | !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 167 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 168 | failX, failY); |
| 169 | } |
| 170 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 171 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 172 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | // Test a inset clear followed by a different full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 176 | rtContext->clear(&mid1Rect, kColor2, false); |
| 177 | rtContext->clear(&fullRect, kColor1, false); |
| 178 | if (!check_rect(rtContext.get(), fullRect, kColor1, &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).", kColor1, actualValue, |
| 180 | failX, failY); |
| 181 | } |
| 182 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 183 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 184 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | // Check three nested clears from largest to smallest where outermost and innermost are same |
| 188 | // color. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 189 | rtContext->clear(&fullRect, kColor1, false); |
| 190 | rtContext->clear(&mid1Rect, kColor2, false); |
| 191 | rtContext->clear(&mid2Rect, kColor1, false); |
| 192 | if (!check_rect(rtContext.get(), mid2Rect, kColor1, &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).", kColor1, actualValue, |
| 194 | failX, failY); |
| 195 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 196 | if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) || |
| 197 | !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) || |
| 198 | !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) || |
| 199 | !check_rect(rtContext.get(), innerBottomEdge, kColor2, &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).", kColor2, actualValue, |
| 201 | failX, failY); |
| 202 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 203 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 204 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 205 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 206 | !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 207 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 208 | failX, failY); |
| 209 | } |
| 210 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 211 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 212 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | // Swap the order of the second two clears in the above test. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 216 | rtContext->clear(&fullRect, kColor1, false); |
| 217 | rtContext->clear(&mid2Rect, kColor1, false); |
| 218 | rtContext->clear(&mid1Rect, kColor2, false); |
| 219 | if (!check_rect(rtContext.get(), mid1Rect, kColor2, &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).", kColor2, actualValue, |
| 221 | failX, failY); |
| 222 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 223 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 224 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 225 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 226 | !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 227 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 228 | failX, failY); |
| 229 | } |
| 230 | } |
| 231 | #endif |