blob: bcd08998673559f64a55d2ef2ca6a9581fa6c89b [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
Brian Osman11052242016-10-27 14:47:55 -040014static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
bsalomone63ffef2016-02-05 07:17:34 -080015 uint32_t* actualValue, int* failX, int* failY) {
bsalomone63ffef2016-02-05 07:17:34 -080016 int w = rect.width();
17 int h = rect.height();
Ben Wagner7ecc5962016-11-02 17:07:33 -040018 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
bsalomone63ffef2016-02-05 07:17:34 -080019 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -040020
21 SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
22
23 if (!rtc->readPixels(dstInfo, pixels.get(), 0, rect.fLeft, rect.fTop)) {
24 return false;
25 }
26
bsalomone63ffef2016-02-05 07:17:34 -080027 for (int y = 0; y < h; ++y) {
28 for (int x = 0; x < w; ++x) {
29 uint32_t pixel = pixels.get()[y * w + x];
30 if (pixel != expectedValue) {
31 *actualValue = pixel;
32 *failX = x + rect.fLeft;
33 *failY = y + rect.fTop;
34 return false;
35 }
36 }
37 }
38 return true;
39}
40
Robert Phillipse4d45bf2017-06-02 14:53:40 -040041sk_sp<GrRenderTargetContext> newRTC(GrContext* context, int w, int h) {
42 return context->makeDeferredRenderTargetContext(SkBackingFit::kExact, w, h,
Robert Phillipsdd3b3f42017-04-24 10:57:28 -040043 kRGBA_8888_GrPixelConfig, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080044}
45
Brian Salomon09d994e2016-12-21 11:14:46 -050046DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070047 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -080048 static const int kW = 10;
49 static const int kH = 10;
50
51 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Osman11052242016-10-27 14:47:55 -040052 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080053
54 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
55 // it.
56 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
57 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
58 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
59 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
60 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
61
62 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
63 // it.
64 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
65 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
66 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
67 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
68 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
69
70 uint32_t actualValue;
71 int failX, failY;
72
73 static const GrColor kColor1 = 0xABCDEF01;
74 static const GrColor kColor2 = ~kColor1;
75
Robert Phillipse4d45bf2017-06-02 14:53:40 -040076 rtContext = newRTC(context, kW, kH);
77 SkASSERT(rtContext);
78
bsalomone63ffef2016-02-05 07:17:34 -080079 // Check a full clear
Brian Osman11052242016-10-27 14:47:55 -040080 rtContext->clear(&fullRect, kColor1, false);
81 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080082 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
83 failX, failY);
84 }
85
Robert Phillipse4d45bf2017-06-02 14:53:40 -040086 rtContext = newRTC(context, kW, kH);
87 SkASSERT(rtContext);
88
bsalomone63ffef2016-02-05 07:17:34 -080089 // Check two full clears, same color
Brian Osman11052242016-10-27 14:47:55 -040090 rtContext->clear(&fullRect, kColor1, false);
91 rtContext->clear(&fullRect, kColor1, false);
92 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080093 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
94 failX, failY);
95 }
96
Robert Phillipse4d45bf2017-06-02 14:53:40 -040097 rtContext = newRTC(context, kW, kH);
98 SkASSERT(rtContext);
99
bsalomone63ffef2016-02-05 07:17:34 -0800100 // Check two full clears, different colors
Brian Osman11052242016-10-27 14:47:55 -0400101 rtContext->clear(&fullRect, kColor1, false);
102 rtContext->clear(&fullRect, kColor2, false);
103 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800104 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
105 failX, failY);
106 }
107
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400108 rtContext = newRTC(context, kW, kH);
109 SkASSERT(rtContext);
110
bsalomone63ffef2016-02-05 07:17:34 -0800111 // Test a full clear followed by a same color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400112 rtContext->clear(&fullRect, kColor1, false);
113 rtContext->clear(&mid1Rect, kColor1, false);
114 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800115 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
116 failX, failY);
117 }
118
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400119 rtContext = newRTC(context, kW, kH);
120 SkASSERT(rtContext);
121
bsalomone63ffef2016-02-05 07:17:34 -0800122 // Test a inset clear followed by same color full clear
Brian Osman11052242016-10-27 14:47:55 -0400123 rtContext->clear(&mid1Rect, kColor1, false);
124 rtContext->clear(&fullRect, kColor1, false);
125 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800126 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
127 failX, failY);
128 }
129
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400130 rtContext = newRTC(context, kW, kH);
131 SkASSERT(rtContext);
132
bsalomone63ffef2016-02-05 07:17:34 -0800133 // Test a full clear followed by a different color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400134 rtContext->clear(&fullRect, kColor1, false);
135 rtContext->clear(&mid1Rect, kColor2, false);
136 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800137 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
138 failX, failY);
139 }
Brian Osman11052242016-10-27 14:47:55 -0400140 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
141 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
142 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
143 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800144 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
145 failX, failY);
146 }
147
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400148 rtContext = newRTC(context, kW, kH);
149 SkASSERT(rtContext);
150
bsalomone63ffef2016-02-05 07:17:34 -0800151 // Test a inset clear followed by a different full clear
Brian Osman11052242016-10-27 14:47:55 -0400152 rtContext->clear(&mid1Rect, kColor2, false);
153 rtContext->clear(&fullRect, kColor1, false);
154 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800155 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
156 failX, failY);
157 }
158
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400159 rtContext = newRTC(context, kW, kH);
160 SkASSERT(rtContext);
161
bsalomone63ffef2016-02-05 07:17:34 -0800162 // Check three nested clears from largest to smallest where outermost and innermost are same
163 // color.
Brian Osman11052242016-10-27 14:47:55 -0400164 rtContext->clear(&fullRect, kColor1, false);
165 rtContext->clear(&mid1Rect, kColor2, false);
166 rtContext->clear(&mid2Rect, kColor1, false);
167 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800168 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
169 failX, failY);
170 }
Brian Osman11052242016-10-27 14:47:55 -0400171 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
172 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
173 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
174 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800175 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
176 failX, failY);
177 }
Brian Osman11052242016-10-27 14:47:55 -0400178 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
179 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
180 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
181 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800182 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
183 failX, failY);
184 }
185
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400186 rtContext = newRTC(context, kW, kH);
187 SkASSERT(rtContext);
188
bsalomone63ffef2016-02-05 07:17:34 -0800189 // Swap the order of the second two clears in the above test.
Brian Osman11052242016-10-27 14:47:55 -0400190 rtContext->clear(&fullRect, kColor1, false);
191 rtContext->clear(&mid2Rect, kColor1, false);
192 rtContext->clear(&mid1Rect, kColor2, false);
193 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800194 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
195 failX, failY);
196 }
Brian Osman11052242016-10-27 14:47:55 -0400197 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
198 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
199 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
200 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800201 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
202 failX, failY);
203 }
204}
205#endif