blob: 3530b88cead5b725e8e7317164b15755dd802c03 [file] [log] [blame]
bsalomone63ffef2016-02-05 07:17:34 -08001/*
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 Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
bsalomone63ffef2016-02-05 07:17:34 -080013#include "GrGpu.h"
14#include "GrRenderTarget.h"
15#include "GrTexture.h"
16#include "GrTextureProvider.h"
17
Brian Osman11052242016-10-27 14:47:55 -040018static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
bsalomone63ffef2016-02-05 07:17:34 -080019 uint32_t* actualValue, int* failX, int* failY) {
Brian Osman11052242016-10-27 14:47:55 -040020 GrRenderTarget* rt = rtc->accessRenderTarget();
bsalomone63ffef2016-02-05 07:17:34 -080021 int w = rect.width();
22 int h = rect.height();
Ben Wagner7ecc5962016-11-02 17:07:33 -040023 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
bsalomone63ffef2016-02-05 07:17:34 -080024 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 Phillips294870f2016-11-11 12:38:40 -050040// 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 Osman11052242016-10-27 14:47:55 -040044static bool reset_rtc(sk_sp<GrRenderTargetContext>* rtc, GrContext* context, int w, int h) {
Robert Phillips294870f2016-11-11 12:38:40 -050045#ifdef SK_DEBUG
46 GrGpuResource::UniqueID oldID = GrGpuResource::UniqueID::InvalidID();
47#endif
48
Brian Osman11052242016-10-27 14:47:55 -040049 if (*rtc) {
50 SkDEBUGCODE(oldID = (*rtc)->accessRenderTarget()->uniqueID();)
51 rtc->reset(nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080052 }
53 context->freeGpuResources();
54
Brian Osman11052242016-10-27 14:47:55 -040055 *rtc = context->makeRenderTargetContext(SkBackingFit::kExact, w, h, kRGBA_8888_GrPixelConfig,
56 nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080057
Brian Osman11052242016-10-27 14:47:55 -040058 SkASSERT((*rtc)->accessRenderTarget()->uniqueID() != oldID);
robertphillipsd4c741e2016-04-28 09:55:15 -070059
Brian Osman11052242016-10-27 14:47:55 -040060 return *rtc != nullptr;
bsalomone63ffef2016-02-05 07:17:34 -080061}
62
Brian Salomon09d994e2016-12-21 11:14:46 -050063DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070064 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -080065 static const int kW = 10;
66 static const int kH = 10;
67
68 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Osman11052242016-10-27 14:47:55 -040069 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080070
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 Osman11052242016-10-27 14:47:55 -040093 if (!reset_rtc(&rtContext, context, kW, kH)) {
94 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -080095 return;
96 }
97 // Check a full clear
Brian Osman11052242016-10-27 14:47:55 -040098 rtContext->clear(&fullRect, kColor1, false);
99 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800100 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
101 failX, failY);
102 }
103
Brian Osman11052242016-10-27 14:47:55 -0400104 if (!reset_rtc(&rtContext, context, kW, kH)) {
105 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800106 return;
107 }
108 // Check two full clears, same color
Brian Osman11052242016-10-27 14:47:55 -0400109 rtContext->clear(&fullRect, kColor1, false);
110 rtContext->clear(&fullRect, kColor1, false);
111 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800112 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
113 failX, failY);
114 }
115
Brian Osman11052242016-10-27 14:47:55 -0400116 if (!reset_rtc(&rtContext, context, kW, kH)) {
117 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800118 return;
119 }
120 // Check two full clears, different colors
Brian Osman11052242016-10-27 14:47:55 -0400121 rtContext->clear(&fullRect, kColor1, false);
122 rtContext->clear(&fullRect, kColor2, false);
123 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800124 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
125 failX, failY);
126 }
127
Brian Osman11052242016-10-27 14:47:55 -0400128 if (!reset_rtc(&rtContext, context, kW, kH)) {
129 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800130 return;
131 }
132 // Test a full clear followed by a same color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400133 rtContext->clear(&fullRect, kColor1, false);
134 rtContext->clear(&mid1Rect, kColor1, false);
135 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800136 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
137 failX, failY);
138 }
139
Brian Osman11052242016-10-27 14:47:55 -0400140 if (!reset_rtc(&rtContext, context, kW, kH)) {
141 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800142 return;
143 }
144 // Test a inset clear followed by same color full clear
Brian Osman11052242016-10-27 14:47:55 -0400145 rtContext->clear(&mid1Rect, kColor1, false);
146 rtContext->clear(&fullRect, kColor1, false);
147 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800148 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
149 failX, failY);
150 }
151
Brian Osman11052242016-10-27 14:47:55 -0400152 if (!reset_rtc(&rtContext, context, kW, kH)) {
153 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800154 return;
155 }
156 // Test a full clear followed by a different color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400157 rtContext->clear(&fullRect, kColor1, false);
158 rtContext->clear(&mid1Rect, kColor2, false);
159 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800160 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
161 failX, failY);
162 }
Brian Osman11052242016-10-27 14:47:55 -0400163 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)) {
bsalomone63ffef2016-02-05 07:17:34 -0800167 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
168 failX, failY);
169 }
170
Brian Osman11052242016-10-27 14:47:55 -0400171 if (!reset_rtc(&rtContext, context, kW, kH)) {
172 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800173 return;
174 }
175 // Test a inset clear followed by a different full clear
Brian Osman11052242016-10-27 14:47:55 -0400176 rtContext->clear(&mid1Rect, kColor2, false);
177 rtContext->clear(&fullRect, kColor1, false);
178 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800179 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
180 failX, failY);
181 }
182
Brian Osman11052242016-10-27 14:47:55 -0400183 if (!reset_rtc(&rtContext, context, kW, kH)) {
184 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800185 return;
186 }
187 // Check three nested clears from largest to smallest where outermost and innermost are same
188 // color.
Brian Osman11052242016-10-27 14:47:55 -0400189 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)) {
bsalomone63ffef2016-02-05 07:17:34 -0800193 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
194 failX, failY);
195 }
Brian Osman11052242016-10-27 14:47:55 -0400196 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)) {
bsalomone63ffef2016-02-05 07:17:34 -0800200 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
201 failX, failY);
202 }
Brian Osman11052242016-10-27 14:47:55 -0400203 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)) {
bsalomone63ffef2016-02-05 07:17:34 -0800207 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
208 failX, failY);
209 }
210
Brian Osman11052242016-10-27 14:47:55 -0400211 if (!reset_rtc(&rtContext, context, kW, kH)) {
212 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800213 return;
214 }
215 // Swap the order of the second two clears in the above test.
Brian Osman11052242016-10-27 14:47:55 -0400216 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)) {
bsalomone63ffef2016-02-05 07:17:34 -0800220 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
221 failX, failY);
222 }
Brian Osman11052242016-10-27 14:47:55 -0400223 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)) {
bsalomone63ffef2016-02-05 07:17:34 -0800227 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
228 failX, failY);
229 }
230}
231#endif