• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "GrRenderTargetContext.h"
13 #include "GrGpu.h"
14 #include "GrRenderTarget.h"
15 #include "GrResourceProvider.h"
16 #include "GrTexture.h"
17 
check_rect(GrRenderTargetContext * rtc,const SkIRect & rect,uint32_t expectedValue,uint32_t * actualValue,int * failX,int * failY)18 static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
19                        uint32_t* actualValue, int* failX, int* failY) {
20     GrRenderTarget* rt = rtc->accessRenderTarget();
21     int w = rect.width();
22     int h = rect.height();
23     std::unique_ptr<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 // TODO: this test does this thorough purging of the rendertargets b.c. right now
41 // the clear optimizations rely on the rendertarget's uniqueID. It can be
42 // relaxed when we switch that over to using rendertargetcontext ids (although
43 // we probably will want to have more clear values then too)
reset_rtc(sk_sp<GrRenderTargetContext> * rtc,GrContext * context,int w,int h)44 static bool reset_rtc(sk_sp<GrRenderTargetContext>* rtc, GrContext* context, int w, int h) {
45 #ifdef SK_DEBUG
46     GrGpuResource::UniqueID oldID = GrGpuResource::UniqueID::InvalidID();
47 #endif
48 
49     if (*rtc) {
50         SkDEBUGCODE(oldID = (*rtc)->accessRenderTarget()->uniqueID();)
51         rtc->reset(nullptr);
52     }
53     context->freeGpuResources();
54 
55     *rtc = context->makeRenderTargetContext(SkBackingFit::kExact, w, h, kRGBA_8888_GrPixelConfig,
56                                             nullptr);
57 
58     SkASSERT((*rtc)->accessRenderTarget()->uniqueID() != oldID);
59 
60     return *rtc != nullptr;
61 }
62 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp,reporter,ctxInfo)63 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ClearOp, reporter, ctxInfo) {
64     GrContext* context = ctxInfo.grContext();
65     static const int kW = 10;
66     static const int kH = 10;
67 
68     SkIRect fullRect = SkIRect::MakeWH(kW, kH);
69     sk_sp<GrRenderTargetContext> rtContext;
70 
71     // A rectangle that is inset by one on all sides and the 1-pixel wide rectangles that surround
72     // it.
73     SkIRect mid1Rect = SkIRect::MakeXYWH(1, 1, kW-2, kH-2);
74     SkIRect outerLeftEdge = SkIRect::MakeXYWH(0, 0, 1, kH);
75     SkIRect outerTopEdge = SkIRect::MakeXYWH(0, 0, kW, 1);
76     SkIRect outerRightEdge = SkIRect::MakeXYWH(kW-1, 0, 1, kH);
77     SkIRect outerBottomEdge = SkIRect::MakeXYWH(0, kH-1, kW, 1);
78 
79     // A rectangle that is inset by two on all sides and the 1-pixel wide rectangles that surround
80     // it.
81     SkIRect mid2Rect = SkIRect::MakeXYWH(2, 2, kW-4, kH-4);
82     SkIRect innerLeftEdge = SkIRect::MakeXYWH(1, 1, 1, kH-2);
83     SkIRect innerTopEdge = SkIRect::MakeXYWH(1, 1, kW-2, 1);
84     SkIRect innerRightEdge = SkIRect::MakeXYWH(kW-2, 1, 1, kH-2);
85     SkIRect innerBottomEdge = SkIRect::MakeXYWH(1, kH-2, kW-2, 1);
86 
87     uint32_t actualValue;
88     int failX, failY;
89 
90     static const GrColor kColor1 = 0xABCDEF01;
91     static const GrColor kColor2 = ~kColor1;
92 
93     if (!reset_rtc(&rtContext, context, kW, kH)) {
94         ERRORF(reporter, "Could not create render target context.");
95         return;
96     }
97     // Check a full clear
98     rtContext->clear(&fullRect, kColor1, false);
99     if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
100         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
101                failX, failY);
102     }
103 
104     if (!reset_rtc(&rtContext, context, kW, kH)) {
105         ERRORF(reporter, "Could not create render target context.");
106         return;
107     }
108     // Check two full clears, same color
109     rtContext->clear(&fullRect, kColor1, false);
110     rtContext->clear(&fullRect, kColor1, false);
111     if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
112         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
113                failX, failY);
114     }
115 
116     if (!reset_rtc(&rtContext, context, kW, kH)) {
117         ERRORF(reporter, "Could not create render target context.");
118         return;
119     }
120     // Check two full clears, different colors
121     rtContext->clear(&fullRect, kColor1, false);
122     rtContext->clear(&fullRect, kColor2, false);
123     if (!check_rect(rtContext.get(), fullRect, kColor2, &actualValue, &failX, &failY)) {
124         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
125                failX, failY);
126     }
127 
128     if (!reset_rtc(&rtContext, context, kW, kH)) {
129         ERRORF(reporter, "Could not create render target context.");
130         return;
131     }
132     // Test a full clear followed by a same color inset clear
133     rtContext->clear(&fullRect, kColor1, false);
134     rtContext->clear(&mid1Rect, kColor1, false);
135     if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
136         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
137                failX, failY);
138     }
139 
140     if (!reset_rtc(&rtContext, context, kW, kH)) {
141         ERRORF(reporter, "Could not create render target context.");
142         return;
143     }
144     // Test a inset clear followed by same color full clear
145     rtContext->clear(&mid1Rect, kColor1, false);
146     rtContext->clear(&fullRect, kColor1, false);
147     if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
148         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
149                failX, failY);
150     }
151 
152     if (!reset_rtc(&rtContext, context, kW, kH)) {
153         ERRORF(reporter, "Could not create render target context.");
154         return;
155     }
156     // Test a full clear followed by a different color inset clear
157     rtContext->clear(&fullRect, kColor1, false);
158     rtContext->clear(&mid1Rect, kColor2, false);
159     if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
160         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
161                failX, failY);
162     }
163     if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
164         !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
165         !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
166         !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
167         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
168                failX, failY);
169     }
170 
171     if (!reset_rtc(&rtContext, context, kW, kH)) {
172         ERRORF(reporter, "Could not create render target context.");
173         return;
174     }
175     // Test a inset clear followed by a different full clear
176     rtContext->clear(&mid1Rect, kColor2, false);
177     rtContext->clear(&fullRect, kColor1, false);
178     if (!check_rect(rtContext.get(), fullRect, kColor1, &actualValue, &failX, &failY)) {
179         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
180                failX, failY);
181     }
182 
183     if (!reset_rtc(&rtContext, context, kW, kH)) {
184         ERRORF(reporter, "Could not create render target context.");
185         return;
186     }
187     // Check three nested clears from largest to smallest where outermost and innermost are same
188     // color.
189     rtContext->clear(&fullRect, kColor1, false);
190     rtContext->clear(&mid1Rect, kColor2, false);
191     rtContext->clear(&mid2Rect, kColor1, false);
192     if (!check_rect(rtContext.get(), mid2Rect, kColor1, &actualValue, &failX, &failY)) {
193         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
194                failX, failY);
195     }
196     if (!check_rect(rtContext.get(), innerLeftEdge, kColor2, &actualValue, &failX, &failY) ||
197         !check_rect(rtContext.get(), innerTopEdge, kColor2, &actualValue, &failX, &failY) ||
198         !check_rect(rtContext.get(), innerRightEdge, kColor2, &actualValue, &failX, &failY) ||
199         !check_rect(rtContext.get(), innerBottomEdge, kColor2, &actualValue, &failX, &failY)) {
200         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
201                failX, failY);
202     }
203     if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
204         !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
205         !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
206         !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
207         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
208                failX, failY);
209     }
210 
211     if (!reset_rtc(&rtContext, context, kW, kH)) {
212         ERRORF(reporter, "Could not create render target context.");
213         return;
214     }
215     // Swap the order of the second two clears in the above test.
216     rtContext->clear(&fullRect, kColor1, false);
217     rtContext->clear(&mid2Rect, kColor1, false);
218     rtContext->clear(&mid1Rect, kColor2, false);
219     if (!check_rect(rtContext.get(), mid1Rect, kColor2, &actualValue, &failX, &failY)) {
220         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor2, actualValue,
221                failX, failY);
222     }
223     if (!check_rect(rtContext.get(), outerLeftEdge, kColor1, &actualValue, &failX, &failY) ||
224         !check_rect(rtContext.get(), outerTopEdge, kColor1, &actualValue, &failX, &failY) ||
225         !check_rect(rtContext.get(), outerRightEdge, kColor1, &actualValue, &failX, &failY) ||
226         !check_rect(rtContext.get(), outerBottomEdge, kColor1, &actualValue, &failX, &failY)) {
227         ERRORF(reporter, "Expected 0x%08x but got 0x%08x at (%d, %d).", kColor1, actualValue,
228                failX, failY);
229     }
230 }
231 #endif
232