blob: 1f8e4970a7c04dbd7c1885f3d6efeb13a4cd9284 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2 * Copyright 2015 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// This is a GPU-backend specific test. It relies on static intializers to work
9
10#include "SkTypes.h"
11
12#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_VULKAN)
13
14#include "GrContextFactory.h"
15#include "GrTest.h"
16#include "Test.h"
17#include "vk/GrVkGpu.h"
18
kkinnunen2ae4b2e2016-03-31 08:08:20 -070019using sk_gpu_test::GrContextFactory;
20
Greg Daniel164a9f02016-02-22 09:56:40 -050021bool does_full_buffer_contain_correct_color(GrColor* buffer,
22 GrColor clearColor,
23 GrPixelConfig config,
24 int width,
25 int height) {
26 GrColor matchColor;
27 if (kRGBA_8888_GrPixelConfig == config) {
28 matchColor = clearColor;
29 } else if (kBGRA_8888_GrPixelConfig) {
30 // Hack to flip the R, B componets in the GrColor so that the comparrison will work below
31 matchColor = GrColorPackRGBA(GrColorUnpackB(clearColor),
32 GrColorUnpackG(clearColor),
33 GrColorUnpackR(clearColor),
34 GrColorUnpackA(clearColor));
35 } else {
36 // currently only supporting rgba_8888 and bgra_8888
37 return false;
38 }
39
40 for (int j = 0; j < height; ++j) {
41 for (int i = 0; i < width; ++i) {
42 if (buffer[j * width + i] != matchColor) {
43 return false;
44 }
45 }
46 }
47 return true;
48}
49
50void basic_clear_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config) {
51 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu());
52 gpu->discard(NULL);
53 SkAutoTMalloc<GrColor> buffer(25);
54
55 GrSurfaceDesc surfDesc;
56 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
57 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
58 surfDesc.fWidth = 5;
59 surfDesc.fHeight = 5;
60 surfDesc.fConfig = config;
61 surfDesc.fSampleCnt = 0;
bsalomondc0fcd42016-04-11 14:21:33 -070062 GrTexture* tex = gpu->createTexture(surfDesc, SkBudgeted::kNo);
Greg Daniel164a9f02016-02-22 09:56:40 -050063 SkASSERT(tex);
64 SkASSERT(tex->asRenderTarget());
65 SkIRect rect = SkIRect::MakeWH(5, 5);
66
67 gpu->clear(rect, GrColor_TRANSPARENT_BLACK, tex->asRenderTarget());
68
69 gpu->readPixels(tex, 0, 0, 5, 5, config, (void*)buffer.get(), 0);
70
71 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(buffer.get(),
72 GrColor_TRANSPARENT_BLACK,
73 config,
74 5,
75 5));
76
77 gpu->clear(rect, GrColor_WHITE, tex->asRenderTarget());
78
79 gpu->readPixels(tex, 0, 0, 5, 5, config, (void*)buffer.get(), 0);
80
81 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(buffer.get(),
82 GrColor_WHITE,
83 config,
84 5,
85 5));
86
87 GrColor myColor = GrColorPackRGBA(0xFF, 0x7F, 0x40, 0x20);
88
89 gpu->clear(rect, myColor, tex->asRenderTarget());
90
91 gpu->readPixels(tex, 0, 0, 5, 5, config, (void*)buffer.get(), 0);
92
93 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(buffer.get(),
94 myColor,
95 config,
96 5,
97 5));
98}
99
100void sub_clear_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config) {
101 const int width = 10;
102 const int height = 10;
103 const int subWidth = width/2;
104 const int subHeight = height/2;
105 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu());
106 gpu->discard(NULL);
107 SkAutoTMalloc<GrColor> buffer(width * height);
108 SkAutoTMalloc<GrColor> subBuffer(subWidth * subHeight);
109
110 GrSurfaceDesc surfDesc;
111 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
112 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
113 surfDesc.fWidth = width;
114 surfDesc.fHeight = height;
115 surfDesc.fConfig = config;
116 surfDesc.fSampleCnt = 0;
bsalomondc0fcd42016-04-11 14:21:33 -0700117 GrTexture* tex = gpu->createTexture(surfDesc, SkBudgeted::kNo);
Greg Daniel164a9f02016-02-22 09:56:40 -0500118 SkASSERT(tex);
119 SkASSERT(tex->asRenderTarget());
120
121 SkIRect fullRect = SkIRect::MakeWH(10, 10);
122 gpu->clear(fullRect, GrColor_TRANSPARENT_BLACK, tex->asRenderTarget());
123
124 gpu->readPixels(tex, 0, 0, width, height, config, (void*)buffer.get(), 0);
125
126 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(buffer.get(),
127 GrColor_TRANSPARENT_BLACK,
128 config,
129 width,
130 height));
131 SkIRect rect;
132 rect = SkIRect::MakeXYWH(0, 0, subWidth, subHeight);
133 gpu->clear(rect, GrColor_WHITE, tex->asRenderTarget());
134 rect = SkIRect::MakeXYWH(subWidth, 0, subWidth, subHeight);
135 gpu->clear(rect, GrColor_WHITE, tex->asRenderTarget());
136 rect = SkIRect::MakeXYWH(0, subHeight, subWidth, subHeight);
137 gpu->clear(rect, GrColor_WHITE, tex->asRenderTarget());
138
139 // Should fail since bottom right sub area has not been cleared to white
140 gpu->readPixels(tex, 0, 0, width, height, config, (void*)buffer.get(), 0);
141 REPORTER_ASSERT(reporter, !does_full_buffer_contain_correct_color(buffer.get(),
142 GrColor_WHITE,
143 config,
144 width,
145 height));
146
147 rect = SkIRect::MakeXYWH(subWidth, subHeight, subWidth, subHeight);
148 gpu->clear(rect, GrColor_WHITE, tex->asRenderTarget());
149
150 gpu->readPixels(tex, 0, 0, width, height, config, (void*)buffer.get(), 0);
151 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(buffer.get(),
152 GrColor_WHITE,
153 config,
154 width,
155 height));
156
157 // Try different colors and that each sub area has correct color
158 GrColor subColor1 = GrColorPackRGBA(0xFF, 0x00, 0x00, 0xFF);
159 GrColor subColor2 = GrColorPackRGBA(0x00, 0xFF, 0x00, 0xFF);
160 GrColor subColor3 = GrColorPackRGBA(0x00, 0x00, 0xFF, 0xFF);
161 GrColor subColor4 = GrColorPackRGBA(0xFF, 0xFF, 0x00, 0xFF);
162
163 rect = SkIRect::MakeXYWH(0, 0, subWidth, subHeight);
164 gpu->clear(rect, subColor1, tex->asRenderTarget());
165 rect = SkIRect::MakeXYWH(subWidth, 0, subWidth, subHeight);
166 gpu->clear(rect, subColor2, tex->asRenderTarget());
167 rect = SkIRect::MakeXYWH(0, subHeight, subWidth, subHeight);
168 gpu->clear(rect, subColor3, tex->asRenderTarget());
169 rect = SkIRect::MakeXYWH(subWidth, subHeight, subWidth, subHeight);
170 gpu->clear(rect, subColor4, tex->asRenderTarget());
171
172 gpu->readPixels(tex, 0, 0, subWidth, subHeight, config, (void*)subBuffer.get(), 0);
173 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(subBuffer.get(),
174 subColor1,
175 config,
176 subWidth,
177 subHeight));
178 gpu->readPixels(tex, subWidth, 0, subWidth, subHeight, config, (void*)subBuffer.get(), 0);
179 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(subBuffer.get(),
180 subColor2,
181 config,
182 subWidth,
183 subHeight));
184 gpu->readPixels(tex, 0, subHeight, subWidth, subHeight, config, (void*)subBuffer.get(), 0);
185 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(subBuffer.get(),
186 subColor3,
187 config,
188 subWidth,
189 subHeight));
190 gpu->readPixels(tex, subWidth, subHeight, subWidth, subHeight,
191 config, (void*)subBuffer.get(), 0);
192 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(subBuffer.get(),
193 subColor4,
194 config,
195 subWidth,
196 subHeight));
197}
198
bsalomondc0fcd42016-04-11 14:21:33 -0700199DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkClearTests, reporter, ctxInfo) {
200 basic_clear_test(reporter, ctxInfo.fGrContext, kRGBA_8888_GrPixelConfig);
201 basic_clear_test(reporter, ctxInfo.fGrContext, kBGRA_8888_GrPixelConfig);
202 sub_clear_test(reporter, ctxInfo.fGrContext, kRGBA_8888_GrPixelConfig);
203 sub_clear_test(reporter, ctxInfo.fGrContext, kBGRA_8888_GrPixelConfig);
Greg Daniel164a9f02016-02-22 09:56:40 -0500204}
205
206#endif