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 | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 45 | // TODO: this test does this thorough purging of the rendertargets b.c. right now |
| 46 | // the clear optimizations rely on the rendertarget's uniqueID. It can be |
| 47 | // relaxed when we switch that over to using rendertargetcontext ids (although |
| 48 | // we probably will want to have more clear values then too) |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 49 | 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] | 50 | #ifdef SK_DEBUG |
| 51 | GrGpuResource::UniqueID oldID = GrGpuResource::UniqueID::InvalidID(); |
| 52 | #endif |
| 53 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 54 | if (*rtc) { |
| 55 | SkDEBUGCODE(oldID = (*rtc)->accessRenderTarget()->uniqueID();) |
| 56 | rtc->reset(nullptr); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 57 | } |
| 58 | context->freeGpuResources(); |
| 59 | |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 60 | *rtc = context->makeDeferredRenderTargetContext(SkBackingFit::kExact, w, h, |
| 61 | kRGBA_8888_GrPixelConfig, nullptr); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 62 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 63 | SkASSERT((*rtc)->accessRenderTarget()->uniqueID() != oldID); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 64 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 65 | return *rtc != nullptr; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 68 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 69 | GrContext* context = ctxInfo.grContext(); |
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); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 74 | sk_sp<GrRenderTargetContext> rtContext; |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 75 | |
| 76 | // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround |
| 77 | // it. |
| 78 | SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2); |
| 79 | SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH); |
| 80 | SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1); |
| 81 | SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH); |
| 82 | SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1); |
| 83 | |
| 84 | // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround |
| 85 | // it. |
| 86 | SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4); |
| 87 | SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2); |
| 88 | SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1); |
| 89 | SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2); |
| 90 | SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1); |
| 91 | |
| 92 | uint32_t actualValue; |
| 93 | int failX, failY; |
| 94 | |
| 95 | static const GrColor kColor1 = 0xABCDEF01; |
| 96 | static const GrColor kColor2 = ~kColor1; |
| 97 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 98 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 99 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | // Check a full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 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, same color |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 114 | rtContext->clear(&fullRect, kColor1, false); |
| 115 | rtContext->clear(&fullRect, kColor1, false); |
| 116 | if (!check_rect(rtContext.get(), fullRect, kColor1, &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).", kColor1, 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 | // Check two full clears, different colors |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 126 | rtContext->clear(&fullRect, kColor1, false); |
| 127 | rtContext->clear(&fullRect, kColor2, false); |
| 128 | if (!check_rect(rtContext.get(), fullRect, kColor2, &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).", kColor2, 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 full clear followed by a same 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, 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 inset clear followed by same color full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 150 | rtContext->clear(&mid1Rect, kColor1, false); |
| 151 | rtContext->clear(&fullRect, kColor1, false); |
| 152 | if (!check_rect(rtContext.get(), fullRect, kColor1, &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).", kColor1, actualValue, |
| 154 | failX, failY); |
| 155 | } |
| 156 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 157 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 158 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | // Test a full clear followed by a different color inset clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 162 | rtContext->clear(&fullRect, kColor1, false); |
| 163 | rtContext->clear(&mid1Rect, kColor2, false); |
| 164 | if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 165 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 166 | failX, failY); |
| 167 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 168 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 169 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 170 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 171 | !check_rect(rtContext.get(), outerBottomEdge, 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 | // Test a inset clear followed by a different full clear |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 181 | rtContext->clear(&mid1Rect, kColor2, false); |
| 182 | rtContext->clear(&fullRect, kColor1, false); |
| 183 | if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 184 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 185 | failX, failY); |
| 186 | } |
| 187 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 188 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 189 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 190 | return; |
| 191 | } |
| 192 | // Check three nested clears from largest to smallest where outermost and innermost are same |
| 193 | // color. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 194 | rtContext->clear(&fullRect, kColor1, false); |
| 195 | rtContext->clear(&mid1Rect, kColor2, false); |
| 196 | rtContext->clear(&mid2Rect, kColor1, false); |
| 197 | if (!check_rect(rtContext.get(), mid2Rect, kColor1, &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).", kColor1, actualValue, |
| 199 | failX, failY); |
| 200 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 201 | if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) || |
| 202 | !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) || |
| 203 | !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) || |
| 204 | !check_rect(rtContext.get(), innerBottomEdge, kColor2, &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).", kColor2, actualValue, |
| 206 | failX, failY); |
| 207 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 208 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 209 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 210 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 211 | !check_rect(rtContext.get(), outerBottomEdge, kColor1, &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).", kColor1, actualValue, |
| 213 | failX, failY); |
| 214 | } |
| 215 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 216 | if (!reset_rtc(&rtContext, context, kW, kH)) { |
| 217 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | // Swap the order of the second two clears in the above test. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 221 | rtContext->clear(&fullRect, kColor1, false); |
| 222 | rtContext->clear(&mid2Rect, kColor1, false); |
| 223 | rtContext->clear(&mid1Rect, kColor2, false); |
| 224 | if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 225 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue, |
| 226 | failX, failY); |
| 227 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 228 | if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) || |
| 229 | !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) || |
| 230 | !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) || |
| 231 | !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) { |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 232 | ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue, |
| 233 | failX, failY); |
| 234 | } |
| 235 | } |
| 236 | #endif |