blob: 56f2fe17bed31b9a145931c997269e033369af07 [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"
12#include "GrDrawContext.h"
13#include "GrGpu.h"
14#include "GrRenderTarget.h"
15#include "GrTexture.h"
16#include "GrTextureProvider.h"
17
18static 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
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.
42static bool reset_dc(SkAutoTUnref<GrDrawContext>* dc, SkAutoTUnref<GrSurface>* rtKeepAlive,
43 GrContext* context, int w, int h) {
44 SkDEBUGCODE(uint32_t oldID = 0;)
45 if (*dc) {
46 SkDEBUGCODE(oldID = (*dc)->accessRenderTarget()->getUniqueID();)
47 rtKeepAlive->reset(nullptr);
48 dc->reset(nullptr);
49 }
50 context->freeGpuResources();
51
52 GrTextureDesc desc;
53 desc.fWidth = w;
54 desc.fHeight = h;
55 desc.fConfig = kRGBA_8888_GrPixelConfig;
56 desc.fFlags = kRenderTarget_GrSurfaceFlag;
57
bsalomon5ec26ae2016-02-25 08:33:02 -080058 rtKeepAlive->reset(context->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomone63ffef2016-02-05 07:17:34 -080059 if (!(*rtKeepAlive)) {
60 return false;
61 }
62 GrRenderTarget* rt = (*rtKeepAlive)->asRenderTarget();
63 SkASSERT(rt->getUniqueID() != oldID);
64 dc->reset(context->drawContext(rt));
mtklein5f939ab2016-03-16 10:28:35 -070065 return *dc != nullptr;
bsalomone63ffef2016-02-05 07:17:34 -080066}
67
bsalomon68d91342016-04-12 09:59:58 -070068DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearBatch, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -070069 GrContext* context = ctxInfo.fGrContext;
bsalomone63ffef2016-02-05 07:17:34 -080070 static const int kW = 10;
71 static const int kH = 10;
72
73 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
74 SkAutoTUnref<GrDrawContext> drawContext;
75 SkAutoTUnref<GrSurface> rtKeepAlive;
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
99 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
100 ERRORF(reporter, "Could not create draw context.");
101 return;
102 }
103 // Check a full clear
104 drawContext->clear(&fullRect, kColor1, false);
105 if (!check_rect(drawContext, fullRect, kColor1, &actualValue, &failX, &failY)) {
106 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
107 failX, failY);
108 }
109
110 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
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);
117 if (!check_rect(drawContext, fullRect, kColor1, &actualValue, &failX, &failY)) {
118 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
119 failX, failY);
120 }
121
122 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
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);
129 if (!check_rect(drawContext, fullRect, kColor2, &actualValue, &failX, &failY)) {
130 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
131 failX, failY);
132 }
133
134 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
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);
141 if (!check_rect(drawContext, fullRect, kColor1, &actualValue, &failX, &failY)) {
142 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
143 failX, failY);
144 }
145
146 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
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);
153 if (!check_rect(drawContext, fullRect, kColor1, &actualValue, &failX, &failY)) {
154 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
155 failX, failY);
156 }
157
158 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
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);
165 if (!check_rect(drawContext, mid1Rect, kColor2, &actualValue, &failX, &failY)) {
166 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
167 failX, failY);
168 }
169 if (!check_rect(drawContext, outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
170 !check_rect(drawContext, outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
171 !check_rect(drawContext, outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
172 !check_rect(drawContext, outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
173 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
174 failX, failY);
175 }
176
177 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
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);
184 if (!check_rect(drawContext, fullRect, kColor1, &actualValue, &failX, &failY)) {
185 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
186 failX, failY);
187 }
188
189 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
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);
198 if (!check_rect(drawContext, mid2Rect, kColor1, &actualValue, &failX, &failY)) {
199 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
200 failX, failY);
201 }
202 if (!check_rect(drawContext, innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
203 !check_rect(drawContext, innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
204 !check_rect(drawContext, innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
205 !check_rect(drawContext, innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
206 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
207 failX, failY);
208 }
209 if (!check_rect(drawContext, outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
210 !check_rect(drawContext, outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
211 !check_rect(drawContext, outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
212 !check_rect(drawContext, outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
213 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
214 failX, failY);
215 }
216
217 if (!reset_dc(&drawContext, &rtKeepAlive, context, kW, kH)) {
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);
225 if (!check_rect(drawContext, mid1Rect, kColor2, &actualValue, &failX, &failY)) {
226 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
227 failX, failY);
228 }
229 if (!check_rect(drawContext, outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
230 !check_rect(drawContext, outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
231 !check_rect(drawContext, outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
232 !check_rect(drawContext, outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
233 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
234 failX, failY);
235 }
236}
237#endif