blob: b6ae6858314b869b395f6592c1596879879cdac4 [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"
Brian Osman32342f02017-03-04 08:12:46 -050015#include "GrResourceProvider.h"
bsalomone63ffef2016-02-05 07:17:34 -080016#include "GrTexture.h"
bsalomone63ffef2016-02-05 07:17:34 -080017
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) {
bsalomone63ffef2016-02-05 07:17:34 -080020 int w = rect.width();
21 int h = rect.height();
Ben Wagner7ecc5962016-11-02 17:07:33 -040022 std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
bsalomone63ffef2016-02-05 07:17:34 -080023 memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -040024
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
bsalomone63ffef2016-02-05 07:17:34 -080031 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 Phillipse4d45bf2017-06-02 14:53:40 -040045sk_sp<GrRenderTargetContext> newRTC(GrContext* context, int w, int h) {
46 return context->makeDeferredRenderTargetContext(SkBackingFit::kExact, w, h,
Robert Phillipsdd3b3f42017-04-24 10:57:28 -040047 kRGBA_8888_GrPixelConfig, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080048}
49
Brian Salomon09d994e2016-12-21 11:14:46 -050050DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070051 GrContext* context = ctxInfo.grContext();
bsalomone63ffef2016-02-05 07:17:34 -080052 static const int kW = 10;
53 static const int kH = 10;
54
55 SkIRect fullRect = SkIRect::MakeWH(kW, kH);
Brian Osman11052242016-10-27 14:47:55 -040056 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080057
58 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
59 // it.
60 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
61 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
62 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
63 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
64 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
65
66 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
67 // it.
68 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
69 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
70 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
71 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
72 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
73
74 uint32_t actualValue;
75 int failX, failY;
76
77 static const GrColor kColor1 = 0xABCDEF01;
78 static const GrColor kColor2 = ~kColor1;
79
Robert Phillipse4d45bf2017-06-02 14:53:40 -040080 rtContext = newRTC(context, kW, kH);
81 SkASSERT(rtContext);
82
bsalomone63ffef2016-02-05 07:17:34 -080083 // Check a full clear
Brian Osman11052242016-10-27 14:47:55 -040084 rtContext->clear(&fullRect, kColor1, false);
85 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080086 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
87 failX, failY);
88 }
89
Robert Phillipse4d45bf2017-06-02 14:53:40 -040090 rtContext = newRTC(context, kW, kH);
91 SkASSERT(rtContext);
92
bsalomone63ffef2016-02-05 07:17:34 -080093 // Check two full clears, same color
Brian Osman11052242016-10-27 14:47:55 -040094 rtContext->clear(&fullRect, kColor1, false);
95 rtContext->clear(&fullRect, kColor1, false);
96 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -080097 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
98 failX, failY);
99 }
100
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400101 rtContext = newRTC(context, kW, kH);
102 SkASSERT(rtContext);
103
bsalomone63ffef2016-02-05 07:17:34 -0800104 // Check two full clears, different colors
Brian Osman11052242016-10-27 14:47:55 -0400105 rtContext->clear(&fullRect, kColor1, false);
106 rtContext->clear(&fullRect, kColor2, false);
107 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800108 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
109 failX, failY);
110 }
111
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400112 rtContext = newRTC(context, kW, kH);
113 SkASSERT(rtContext);
114
bsalomone63ffef2016-02-05 07:17:34 -0800115 // Test a full clear followed by a same color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400116 rtContext->clear(&fullRect, kColor1, false);
117 rtContext->clear(&mid1Rect, kColor1, false);
118 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800119 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
120 failX, failY);
121 }
122
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400123 rtContext = newRTC(context, kW, kH);
124 SkASSERT(rtContext);
125
bsalomone63ffef2016-02-05 07:17:34 -0800126 // Test a inset clear followed by same color full clear
Brian Osman11052242016-10-27 14:47:55 -0400127 rtContext->clear(&mid1Rect, kColor1, false);
128 rtContext->clear(&fullRect, kColor1, false);
129 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800130 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
131 failX, failY);
132 }
133
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400134 rtContext = newRTC(context, kW, kH);
135 SkASSERT(rtContext);
136
bsalomone63ffef2016-02-05 07:17:34 -0800137 // Test a full clear followed by a different color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400138 rtContext->clear(&fullRect, kColor1, false);
139 rtContext->clear(&mid1Rect, kColor2, false);
140 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800141 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
142 failX, failY);
143 }
Brian Osman11052242016-10-27 14:47:55 -0400144 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
145 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
146 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
147 !check_rect(rtContext.get(), outerBottomEdge, 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
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400152 rtContext = newRTC(context, kW, kH);
153 SkASSERT(rtContext);
154
bsalomone63ffef2016-02-05 07:17:34 -0800155 // Test a inset clear followed by a different full clear
Brian Osman11052242016-10-27 14:47:55 -0400156 rtContext->clear(&mid1Rect, kColor2, false);
157 rtContext->clear(&fullRect, kColor1, false);
158 if (!check_rect(rtContext.get(), fullRect, 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
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400163 rtContext = newRTC(context, kW, kH);
164 SkASSERT(rtContext);
165
bsalomone63ffef2016-02-05 07:17:34 -0800166 // Check three nested clears from largest to smallest where outermost and innermost are same
167 // color.
Brian Osman11052242016-10-27 14:47:55 -0400168 rtContext->clear(&fullRect, kColor1, false);
169 rtContext->clear(&mid1Rect, kColor2, false);
170 rtContext->clear(&mid2Rect, kColor1, false);
171 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800172 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
173 failX, failY);
174 }
Brian Osman11052242016-10-27 14:47:55 -0400175 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
176 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
177 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
178 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800179 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
180 failX, failY);
181 }
Brian Osman11052242016-10-27 14:47:55 -0400182 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
183 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
184 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
185 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800186 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
187 failX, failY);
188 }
189
Robert Phillipse4d45bf2017-06-02 14:53:40 -0400190 rtContext = newRTC(context, kW, kH);
191 SkASSERT(rtContext);
192
bsalomone63ffef2016-02-05 07:17:34 -0800193 // Swap the order of the second two clears in the above test.
Brian Osman11052242016-10-27 14:47:55 -0400194 rtContext->clear(&fullRect, kColor1, false);
195 rtContext->clear(&mid2Rect, kColor1, false);
196 rtContext->clear(&mid1Rect, kColor2, false);
197 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800198 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
199 failX, failY);
200 }
Brian Osman11052242016-10-27 14:47:55 -0400201 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
202 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
203 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
204 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800205 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
206 failX, failY);
207 }
208}
209#endif