1 // Unit tests for FlatInput.
6 #include "effect_chain.h"
7 #include "flat_input.h"
8 #include "gtest/gtest.h"
9 #include "resource_pool.h"
10 #include "test_util.h"
15 TEST(FlatInput, SimpleGrayscale) {
24 float expected_data[4 * size] = {
30 float out_data[4 * size];
32 EffectChainTester tester(data, 1, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
33 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
35 expect_equal(expected_data, out_data, 4, size);
38 TEST(FlatInput, RGB) {
41 float data[3 * size] = {
48 float expected_data[4 * size] = {
55 float out_data[4 * size];
57 EffectChainTester tester(data, 1, size, FORMAT_RGB, COLORSPACE_sRGB, GAMMA_LINEAR);
58 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
60 expect_equal(expected_data, out_data, 4, size);
63 TEST(FlatInput, RGBA) {
66 float data[4 * size] = {
73 float expected_data[4 * size] = {
80 float out_data[4 * size];
82 EffectChainTester tester(data, 1, size, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
83 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
85 expect_equal(expected_data, out_data, 4, size);
88 // Note: The sRGB conversion itself is tested in EffectChainTester,
89 // since it also wants to test the chain building itself.
90 // Here, we merely test that alpha is left alone; the test will usually
91 // run using the sRGB OpenGL extension, but might be run with a
92 // GammaExpansionEffect if the card/driver happens not to support that.
93 TEST(FlatInput, AlphaIsNotModifiedBySRGBConversion) {
96 unsigned char data[4 * size] = {
103 float expected_data[4 * size] = {
104 0, 0, 0, 0.0 / 255.0,
105 0, 0, 0, 63.0 / 255.0,
106 0, 0, 0, 127.0 / 255.0,
107 0, 0, 0, 191.0 / 255.0,
108 0, 0, 0, 255.0 / 255.0,
110 float out_data[4 * size];
112 EffectChainTester tester(NULL, 1, size);
113 tester.add_input(data, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
114 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
116 expect_equal(expected_data, out_data, 4, size);
119 TEST(FlatInput, BGR) {
122 float data[3 * size] = {
129 float expected_data[4 * size] = {
136 float out_data[4 * size];
138 EffectChainTester tester(data, 1, size, FORMAT_BGR, COLORSPACE_sRGB, GAMMA_LINEAR);
139 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
141 expect_equal(expected_data, out_data, 4, size);
144 TEST(FlatInput, BGRA) {
147 float data[4 * size] = {
154 float expected_data[4 * size] = {
161 float out_data[4 * size];
163 EffectChainTester tester(data, 1, size, FORMAT_BGRA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
164 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
166 expect_equal(expected_data, out_data, 4, size);
169 TEST(FlatInput, Pitch) {
172 const int height = 4;
174 float data[pitch * height] = {
180 float expected_data[4 * width * height] = {
181 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
182 0.5, 0.5, 0.5, 1.0, 0.5, 0.5, 0.5, 1.0,
183 0.7, 0.7, 0.7, 1.0, 0.2, 0.2, 0.2, 1.0,
184 1.0, 1.0, 1.0, 1.0, 0.6, 0.6, 0.6, 1.0,
186 float out_data[4 * width * height];
188 EffectChainTester tester(NULL, width, height);
191 format.color_space = COLORSPACE_sRGB;
192 format.gamma_curve = GAMMA_LINEAR;
194 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
195 input->set_pitch(pitch);
196 input->set_pixel_data(data);
197 tester.get_chain()->add_input(input);
199 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
200 expect_equal(expected_data, out_data, 4 * width, height);
203 TEST(FlatInput, UpdatedData) {
205 const int height = 4;
207 float data[width * height] = {
213 float out_data[width * height];
215 EffectChainTester tester(NULL, width, height);
218 format.color_space = COLORSPACE_sRGB;
219 format.gamma_curve = GAMMA_LINEAR;
221 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
222 input->set_pixel_data(data);
223 tester.get_chain()->add_input(input);
225 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
226 expect_equal(data, out_data, width, height);
229 input->invalidate_pixel_data();
231 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
232 expect_equal(data, out_data, width, height);
235 TEST(FlatInput, PBO) {
237 const int height = 2;
239 float data[width * height] = {
243 float expected_data[4 * width * height] = {
244 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0,
245 0.5, 0.5, 0.5, 1.0, 0.5, 0.5, 0.5, 1.0, 0.2, 0.2, 0.2, 1.0,
247 float out_data[4 * width * height];
250 glGenBuffers(1, &pbo);
251 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
252 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * sizeof(float), data, GL_STREAM_DRAW);
253 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
255 EffectChainTester tester(NULL, width, height);
258 format.color_space = COLORSPACE_sRGB;
259 format.gamma_curve = GAMMA_LINEAR;
261 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
262 input->set_pixel_data((float *)BUFFER_OFFSET(0), pbo);
263 tester.get_chain()->add_input(input);
265 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
266 expect_equal(expected_data, out_data, 4 * width, height);
268 glDeleteBuffers(1, &pbo);
271 TEST(FlatInput, ExternalTexture) {
274 float data[3 * size] = {
281 float expected_data[4 * size] = {
288 float out_data[4 * size];
290 EffectChainTester tester(NULL, 1, size, FORMAT_RGB, COLORSPACE_sRGB, GAMMA_LINEAR);
293 format.color_space = COLORSPACE_sRGB;
294 format.gamma_curve = GAMMA_LINEAR;
297 GLuint tex = pool.create_2d_texture(GL_RGB8, 1, size);
299 glBindTexture(GL_TEXTURE_2D, tex);
301 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
303 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
305 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, size, GL_RGB, GL_FLOAT, data);
307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
309 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
312 FlatInput *input = new FlatInput(format, FORMAT_RGB, GL_FLOAT, 1, size);
313 input->set_texture_num(tex);
314 tester.get_chain()->add_input(input);
316 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
318 pool.release_2d_texture(tex);
320 expect_equal(expected_data, out_data, 4, size);
323 TEST(FlatInput, NoData) {
325 const int height = 4;
327 float out_data[width * height];
329 EffectChainTester tester(NULL, width, height);
332 format.color_space = COLORSPACE_sRGB;
333 format.gamma_curve = GAMMA_LINEAR;
335 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
336 tester.get_chain()->add_input(input);
338 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
340 // Don't care what the output was, just that it does not crash.