blob: b9ece6bce361a5d81235aa96a24a5599e27a87cd [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
robertphillipsd4c741e2016-04-28 09:55:15 -070040static bool reset_dc(sk_sp<GrDrawContext>* dc, GrContext* context, int w, int h) {
bsalomone63ffef2016-02-05 07:17:34 -080041 SkDEBUGCODE(uint32_t oldID = 0;)
42 if (*dc) {
robertphillips8abb3702016-08-31 14:04:06 -070043 SkDEBUGCODE(oldID = (*dc)->accessRenderTarget()->uniqueID();)
bsalomone63ffef2016-02-05 07:17:34 -080044 dc->reset(nullptr);
45 }
46 context->freeGpuResources();
47
robertphillips6738c702016-07-27 12:13:51 -070048 *dc = context->makeDrawContext(SkBackingFit::kExact, w, h, kRGBA_8888_GrPixelConfig, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080049
robertphillips8abb3702016-08-31 14:04:06 -070050 SkASSERT((*dc)->accessRenderTarget()->uniqueID() != oldID);
robertphillipsd4c741e2016-04-28 09:55:15 -070051
mtklein5f939ab2016-03-16 10:28:35 -070052 return *dc != nullptr;
bsalomone63ffef2016-02-05 07:17:34 -080053}
54
bsalomon68d91342016-04-12 09:59:58 -070055DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearBatch, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070056 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -080057 static const int kW = 10;
58 static const int kH = 10;
59
60 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
robertphillips6c7e3252016-04-27 10:47:51 -070061 sk_sp<GrDrawContext> drawContext;
bsalomone63ffef2016-02-05 07:17:34 -080062
63 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
64 // it.
65 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
66 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
67 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
68 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
69 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
70
71 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
72 // it.
73 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
74 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
75 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
76 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
77 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
78
79 uint32_t actualValue;
80 int failX, failY;
81
82 static const GrColor kColor1 = 0xABCDEF01;
83 static const GrColor kColor2 = ~kColor1;
84
robertphillipsd4c741e2016-04-28 09:55:15 -070085 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -080086 ERRORF(reporter, "Could not create draw context.");
87 return;
88 }
89 // Check a full clear
90 drawContext->clear(&fullRect, kColor1, false);
robertphillips6c7e3252016-04-27 10:47:51 -070091 if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080092 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
93 failX, failY);
94 }
95
robertphillipsd4c741e2016-04-28 09:55:15 -070096 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -080097 ERRORF(reporter, "Could not create draw context.");
98 return;
99 }
100 // Check two full clears, same color
101 drawContext->clear(&fullRect, kColor1, false);
102 drawContext->clear(&fullRect, kColor1, false);
robertphillips6c7e3252016-04-27 10:47:51 -0700103 if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800104 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
105 failX, failY);
106 }
107
robertphillipsd4c741e2016-04-28 09:55:15 -0700108 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -0800109 ERRORF(reporter, "Could not create draw context.");
110 return;
111 }
112 // Check two full clears, different colors
113 drawContext->clear(&fullRect, kColor1, false);
114 drawContext->clear(&fullRect, kColor2, false);
robertphillips6c7e3252016-04-27 10:47:51 -0700115 if (!check_rect(drawContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800116 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
117 failX, failY);
118 }
119
robertphillipsd4c741e2016-04-28 09:55:15 -0700120 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -0800121 ERRORF(reporter, "Could not create draw context.");
122 return;
123 }
124 // Test a full clear followed by a same color inset clear
125 drawContext->clear(&fullRect, kColor1, false);
126 drawContext->clear(&mid1Rect, kColor1, false);
robertphillips6c7e3252016-04-27 10:47:51 -0700127 if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800128 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
129 failX, failY);
130 }
131
robertphillipsd4c741e2016-04-28 09:55:15 -0700132 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -0800133 ERRORF(reporter, "Could not create draw context.");
134 return;
135 }
136 // Test a inset clear followed by same color full clear
137 drawContext->clear(&mid1Rect, kColor1, false);
138 drawContext->clear(&fullRect, kColor1, false);
robertphillips6c7e3252016-04-27 10:47:51 -0700139 if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800140 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
141 failX, failY);
142 }
143
robertphillipsd4c741e2016-04-28 09:55:15 -0700144 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -0800145 ERRORF(reporter, "Could not create draw context.");
146 return;
147 }
148 // Test a full clear followed by a different color inset clear
149 drawContext->clear(&fullRect, kColor1, false);
150 drawContext->clear(&mid1Rect, kColor2, false);
robertphillips6c7e3252016-04-27 10:47:51 -0700151 if (!check_rect(drawContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800152 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
153 failX, failY);
154 }
robertphillips6c7e3252016-04-27 10:47:51 -0700155 if (!check_rect(drawContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
156 !check_rect(drawContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
157 !check_rect(drawContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
158 !check_rect(drawContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800159 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
160 failX, failY);
161 }
162
robertphillipsd4c741e2016-04-28 09:55:15 -0700163 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -0800164 ERRORF(reporter, "Could not create draw context.");
165 return;
166 }
167 // Test a inset clear followed by a different full clear
168 drawContext->clear(&mid1Rect, kColor2, false);
169 drawContext->clear(&fullRect, kColor1, false);
robertphillips6c7e3252016-04-27 10:47:51 -0700170 if (!check_rect(drawContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800171 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
172 failX, failY);
173 }
174
robertphillipsd4c741e2016-04-28 09:55:15 -0700175 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -0800176 ERRORF(reporter, "Could not create draw context.");
177 return;
178 }
179 // Check three nested clears from largest to smallest where outermost and innermost are same
180 // color.
181 drawContext->clear(&fullRect, kColor1, false);
182 drawContext->clear(&mid1Rect, kColor2, false);
183 drawContext->clear(&mid2Rect, kColor1, false);
robertphillips6c7e3252016-04-27 10:47:51 -0700184 if (!check_rect(drawContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800185 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
186 failX, failY);
187 }
robertphillips6c7e3252016-04-27 10:47:51 -0700188 if (!check_rect(drawContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
189 !check_rect(drawContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
190 !check_rect(drawContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
191 !check_rect(drawContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800192 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
193 failX, failY);
194 }
robertphillips6c7e3252016-04-27 10:47:51 -0700195 if (!check_rect(drawContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
196 !check_rect(drawContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
197 !check_rect(drawContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
198 !check_rect(drawContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800199 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
200 failX, failY);
201 }
202
robertphillipsd4c741e2016-04-28 09:55:15 -0700203 if (!reset_dc(&drawContext, context, kW, kH)) {
bsalomone63ffef2016-02-05 07:17:34 -0800204 ERRORF(reporter, "Could not create draw context.");
205 return;
206 }
207 // Swap the order of the second two clears in the above test.
208 drawContext->clear(&fullRect, kColor1, false);
209 drawContext->clear(&mid2Rect, kColor1, false);
210 drawContext->clear(&mid1Rect, kColor2, false);
robertphillips6c7e3252016-04-27 10:47:51 -0700211 if (!check_rect(drawContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800212 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
213 failX, failY);
214 }
robertphillips6c7e3252016-04-27 10:47:51 -0700215 if (!check_rect(drawContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
216 !check_rect(drawContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
217 !check_rect(drawContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
218 !check_rect(drawContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800219 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
220 failX, failY);
221 }
222}
223#endif