blob: 45eb2afa2d6b8e6eea980b203eb3bc1ab5236b6f [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 Phillips294870f2016-11-11 12:38:40 -050045// TODO: this test does this thorough purging of the rendertargets b.c. right now
46// the clear optimizations rely on the rendertarget's uniqueID. It can be
47// relaxed when we switch that over to using rendertargetcontext ids (although
48// we probably will want to have more clear values then too)
Brian Osman11052242016-10-27 14:47:55 -040049static bool reset_rtc(sk_sp<GrRenderTargetContext>* rtc, GrContext* context, int w, int h) {
Robert Phillips294870f2016-11-11 12:38:40 -050050#ifdef SK_DEBUG
51 GrGpuResource::UniqueID oldID = GrGpuResource::UniqueID::InvalidID();
52#endif
53
Brian Osman11052242016-10-27 14:47:55 -040054 if (*rtc) {
55 SkDEBUGCODE(oldID = (*rtc)->accessRenderTarget()->uniqueID();)
56 rtc->reset(nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080057 }
58 context->freeGpuResources();
59
Robert Phillipsdd3b3f42017-04-24 10:57:28 -040060 *rtc = context->makeDeferredRenderTargetContext(SkBackingFit::kExact, w, h,
61 kRGBA_8888_GrPixelConfig, nullptr);
bsalomone63ffef2016-02-05 07:17:34 -080062
Brian Osman11052242016-10-27 14:47:55 -040063 SkASSERT((*rtc)->accessRenderTarget()->uniqueID() != oldID);
robertphillipsd4c741e2016-04-28 09:55:15 -070064
Brian Osman11052242016-10-27 14:47:55 -040065 return *rtc != nullptr;
bsalomone63ffef2016-02-05 07:17:34 -080066}
67
Brian Salomon09d994e2016-12-21 11:14:46 -050068DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070069 GrContext* context = ctxInfo.grContext();
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);
Brian Osman11052242016-10-27 14:47:55 -040074 sk_sp<GrRenderTargetContext> rtContext;
bsalomone63ffef2016-02-05 07:17:34 -080075
76 // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
77 // it.
78 SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
79 SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
80 SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
81 SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
82 SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
83
84 // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
85 // it.
86 SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
87 SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
88 SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
89 SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
90 SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
91
92 uint32_t actualValue;
93 int failX, failY;
94
95 static const GrColor kColor1 = 0xABCDEF01;
96 static const GrColor kColor2 = ~kColor1;
97
Brian Osman11052242016-10-27 14:47:55 -040098 if (!reset_rtc(&rtContext, context, kW, kH)) {
99 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800100 return;
101 }
102 // Check a full clear
Brian Osman11052242016-10-27 14:47:55 -0400103 rtContext->clear(&fullRect, kColor1, false);
104 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800105 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
106 failX, failY);
107 }
108
Brian Osman11052242016-10-27 14:47:55 -0400109 if (!reset_rtc(&rtContext, context, kW, kH)) {
110 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800111 return;
112 }
113 // Check two full clears, same color
Brian Osman11052242016-10-27 14:47:55 -0400114 rtContext->clear(&fullRect, kColor1, false);
115 rtContext->clear(&fullRect, kColor1, false);
116 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800117 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
118 failX, failY);
119 }
120
Brian Osman11052242016-10-27 14:47:55 -0400121 if (!reset_rtc(&rtContext, context, kW, kH)) {
122 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800123 return;
124 }
125 // Check two full clears, different colors
Brian Osman11052242016-10-27 14:47:55 -0400126 rtContext->clear(&fullRect, kColor1, false);
127 rtContext->clear(&fullRect, kColor2, false);
128 if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800129 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
130 failX, failY);
131 }
132
Brian Osman11052242016-10-27 14:47:55 -0400133 if (!reset_rtc(&rtContext, context, kW, kH)) {
134 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800135 return;
136 }
137 // Test a full clear followed by a same color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400138 rtContext->clear(&fullRect, kColor1, false);
139 rtContext->clear(&mid1Rect, kColor1, false);
140 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800141 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
142 failX, failY);
143 }
144
Brian Osman11052242016-10-27 14:47:55 -0400145 if (!reset_rtc(&rtContext, context, kW, kH)) {
146 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800147 return;
148 }
149 // Test a inset clear followed by same color full clear
Brian Osman11052242016-10-27 14:47:55 -0400150 rtContext->clear(&mid1Rect, kColor1, false);
151 rtContext->clear(&fullRect, kColor1, false);
152 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800153 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
154 failX, failY);
155 }
156
Brian Osman11052242016-10-27 14:47:55 -0400157 if (!reset_rtc(&rtContext, context, kW, kH)) {
158 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800159 return;
160 }
161 // Test a full clear followed by a different color inset clear
Brian Osman11052242016-10-27 14:47:55 -0400162 rtContext->clear(&fullRect, kColor1, false);
163 rtContext->clear(&mid1Rect, kColor2, false);
164 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800165 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
166 failX, failY);
167 }
Brian Osman11052242016-10-27 14:47:55 -0400168 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
169 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
170 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
171 !check_rect(rtContext.get(), outerBottomEdge, 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 }
175
Brian Osman11052242016-10-27 14:47:55 -0400176 if (!reset_rtc(&rtContext, context, kW, kH)) {
177 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800178 return;
179 }
180 // Test a inset clear followed by a different full clear
Brian Osman11052242016-10-27 14:47:55 -0400181 rtContext->clear(&mid1Rect, kColor2, false);
182 rtContext->clear(&fullRect, kColor1, false);
183 if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800184 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
185 failX, failY);
186 }
187
Brian Osman11052242016-10-27 14:47:55 -0400188 if (!reset_rtc(&rtContext, context, kW, kH)) {
189 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800190 return;
191 }
192 // Check three nested clears from largest to smallest where outermost and innermost are same
193 // color.
Brian Osman11052242016-10-27 14:47:55 -0400194 rtContext->clear(&fullRect, kColor1, false);
195 rtContext->clear(&mid1Rect, kColor2, false);
196 rtContext->clear(&mid2Rect, kColor1, false);
197 if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800198 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
199 failX, failY);
200 }
Brian Osman11052242016-10-27 14:47:55 -0400201 if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
202 !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
203 !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
204 !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800205 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
206 failX, failY);
207 }
Brian Osman11052242016-10-27 14:47:55 -0400208 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
209 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
210 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
211 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800212 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
213 failX, failY);
214 }
215
Brian Osman11052242016-10-27 14:47:55 -0400216 if (!reset_rtc(&rtContext, context, kW, kH)) {
217 ERRORF(reporter, "Could not create render target context.");
bsalomone63ffef2016-02-05 07:17:34 -0800218 return;
219 }
220 // Swap the order of the second two clears in the above test.
Brian Osman11052242016-10-27 14:47:55 -0400221 rtContext->clear(&fullRect, kColor1, false);
222 rtContext->clear(&mid2Rect, kColor1, false);
223 rtContext->clear(&mid1Rect, kColor2, false);
224 if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800225 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
226 failX, failY);
227 }
Brian Osman11052242016-10-27 14:47:55 -0400228 if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
229 !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
230 !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
231 !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
bsalomone63ffef2016-02-05 07:17:34 -0800232 ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
233 failX, failY);
234 }
235}
236#endif