Do not glReadPixels() with type GL_RED.
[movit] / test_util.cpp
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <algorithm>
5 #include <epoxy/gl.h>
6 #include <gtest/gtest.h>
7 #include <gtest/gtest-message.h>
8
9 #include "flat_input.h"
10 #include "init.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
13 #include "util.h"
14
15 using namespace std;
16
17 namespace movit {
18
19 class Input;
20
21 namespace {
22
23 // Not thread-safe, but this isn't a big problem for testing.
24 ResourcePool *get_static_pool()
25 {
26         static ResourcePool *resource_pool = NULL;
27         if (!resource_pool) {
28                 resource_pool = new ResourcePool();
29         }
30         return resource_pool;
31 }
32
33 // Flip upside-down to compensate for different origin.
34 template<class T>
35 void vertical_flip(T *data, unsigned width, unsigned height)
36 {
37         for (unsigned y = 0; y < height / 2; ++y) {
38                 unsigned flip_y = height - y - 1;
39                 for (unsigned x = 0; x < width; ++x) {
40                         swap(data[y * width + x], data[flip_y * width + x]);
41                 }
42         }
43 }
44
45 }  // namespace
46
47 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
48                                      MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
49                                      GLenum framebuffer_format)
50         : chain(width, height, get_static_pool()), width(width), height(height), finalized(false)
51 {
52         CHECK(init_movit(".", MOVIT_DEBUG_OFF));
53
54         if (data != NULL) {
55                 add_input(data, pixel_format, color_space, gamma_curve);
56         }
57
58         glGenTextures(1, &texnum);
59         check_error();
60         glBindTexture(GL_TEXTURE_2D, texnum);
61         check_error();
62         glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
63         check_error();
64
65         glGenFramebuffers(1, &fbo);
66         check_error();
67         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
68         check_error();
69         glFramebufferTexture2D(
70                 GL_FRAMEBUFFER,
71                 GL_COLOR_ATTACHMENT0,
72                 GL_TEXTURE_2D,
73                 texnum,
74                 0);
75         check_error();
76         glBindFramebuffer(GL_FRAMEBUFFER, 0);
77         check_error();
78 }
79
80 EffectChainTester::~EffectChainTester()
81 {
82         glDeleteFramebuffers(1, &fbo);
83         check_error();
84         glDeleteTextures(1, &texnum);
85         check_error();
86 }
87
88 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
89 {
90         ImageFormat format;
91         format.color_space = color_space;
92         format.gamma_curve = gamma_curve;
93
94         if (input_width == -1) {
95                 input_width = width;
96         }
97         if (input_height == -1) {
98                 input_height = height;
99         }
100
101         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, input_width, input_height);
102         input->set_pixel_data(data);
103         chain.add_input(input);
104         return input;
105 }
106
107 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
108 {
109         ImageFormat format;
110         format.color_space = color_space;
111         format.gamma_curve = gamma_curve;
112
113         if (input_width == -1) {
114                 input_width = width;
115         }
116         if (input_height == -1) {
117                 input_height = height;
118         }
119
120         FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, input_width, input_height);
121         input->set_pixel_data(data);
122         chain.add_input(input);
123         return input;
124 }
125
126 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
127 {
128         if (!finalized) {
129                 finalize_chain(color_space, gamma_curve, alpha_format);
130         }
131
132         chain.render_to_fbo(fbo, width, height);
133
134         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
135         check_error();
136         if (!epoxy_is_desktop_gl() && format == GL_RED) {
137                 // GLES will only read GL_RGBA.
138                 float *temp = new float[width * height * 4];
139                 glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, temp);
140                 check_error();
141                 for (int i = 0; i < width * height; ++i) {
142                         out_data[i] = temp[i * 4];
143                 }
144                 delete[] temp;
145         } else {
146                 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
147                 check_error();
148         }
149
150         if (format == GL_RGBA) {
151                 width *= 4;
152         }
153
154         vertical_flip(out_data, width, height);
155 }
156
157 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
158 {
159         if (!finalized) {
160                 finalize_chain(color_space, gamma_curve, alpha_format);
161         }
162
163         chain.render_to_fbo(fbo, width, height);
164
165         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
166         check_error();
167         if (!epoxy_is_desktop_gl() && format == GL_RED) {
168                 // GLES will only read GL_RGBA.
169                 unsigned char *temp = new unsigned char[width * height * 4];
170                 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, temp);
171                 check_error();
172                 for (int i = 0; i < width * height; ++i) {
173                         out_data[i] = temp[i * 4];
174                 }
175                 delete[] temp;
176         } else {
177                 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
178                 check_error();
179         }
180
181         if (format == GL_RGBA) {
182                 width *= 4;
183         }
184
185         vertical_flip(out_data, width, height);
186 }
187
188 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
189 {
190         assert(!finalized);
191         ImageFormat image_format;
192         image_format.color_space = color_space;
193         image_format.gamma_curve = gamma_curve;
194         chain.add_output(image_format, alpha_format);
195         chain.finalize();
196         finalized = true;
197 }
198
199 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
200 {
201         float largest_difference = -1.0f;
202         float squared_difference = 0.0f;
203         int largest_diff_x = -1, largest_diff_y = -1;
204
205         for (unsigned y = 0; y < height; ++y) {
206                 for (unsigned x = 0; x < width; ++x) {
207                         float diff = ref[y * width + x] - result[y * width + x];
208                         if (fabs(diff) > largest_difference) {
209                                 largest_difference = fabs(diff);
210                                 largest_diff_x = x;
211                                 largest_diff_y = y;
212                         }
213                         squared_difference += diff * diff;
214                 }
215         }
216
217         EXPECT_LT(largest_difference, largest_difference_limit)
218                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
219                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
220                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
221
222         float rms = sqrt(squared_difference) / (width * height);
223         EXPECT_LT(rms, rms_limit);
224
225         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
226                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
227
228                 fprintf(stderr, "Reference:\n");
229                 for (unsigned y = 0; y < height; ++y) {
230                         for (unsigned x = 0; x < width; ++x) {
231                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
232                         }
233                         fprintf(stderr, "\n");
234                 }
235
236                 fprintf(stderr, "\nResult:\n");
237                 for (unsigned y = 0; y < height; ++y) {
238                         for (unsigned x = 0; x < width; ++x) {
239                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
240                         }
241                         fprintf(stderr, "\n");
242                 }
243         }
244 }
245
246 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
247 {
248         assert(width > 0);
249         assert(height > 0);
250
251         float *ref_float = new float[width * height];
252         float *result_float = new float[width * height];
253
254         for (unsigned y = 0; y < height; ++y) {
255                 for (unsigned x = 0; x < width; ++x) {
256                         ref_float[y * width + x] = ref[y * width + x];
257                         result_float[y * width + x] = result[y * width + x];
258                 }
259         }
260
261         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
262
263         delete[] ref_float;
264         delete[] result_float;
265 }
266
267 void test_accuracy(const float *expected, const float *result, unsigned num_values, double absolute_error_limit, double relative_error_limit, double local_relative_error_limit, double rms_limit)
268 {
269         double squared_difference = 0.0;
270         for (unsigned i = 0; i < num_values; ++i) {
271                 double absolute_error = fabs(expected[i] - result[i]);
272                 squared_difference += absolute_error * absolute_error;
273                 EXPECT_LT(absolute_error, absolute_error_limit);
274
275                 if (expected[i] > 0.0) {
276                         double relative_error = fabs(absolute_error / expected[i]);
277
278                         EXPECT_LT(relative_error, relative_error_limit);
279                 }
280                 if (i < num_values - 1) {
281                         double delta = expected[i + 1] - expected[i];
282                         double local_relative_error = fabs(absolute_error / delta);
283                         EXPECT_LT(local_relative_error, local_relative_error_limit);
284                 }
285         }
286         double rms = sqrt(squared_difference) / num_values;
287         EXPECT_LT(rms, rms_limit);
288 }
289
290 }  // namespace movit