OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stddef.h> | |
6 #include <stdint.h> | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include <cmath> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include <GLES2/gl2.h> | |
14 #include <GLES2/gl2ext.h> | |
15 #include <GLES2/gl2extchromium.h> | |
16 | |
17 #include "base/at_exit.h" | |
18 #include "base/bind.h" | |
19 #include "base/command_line.h" | |
20 #include "base/files/file_util.h" | |
21 #include "base/json/json_reader.h" | |
22 #include "base/macros.h" | |
23 #include "base/memory/ref_counted_memory.h" | |
24 #include "base/message_loop/message_loop.h" | |
25 #include "base/run_loop.h" | |
26 #include "base/strings/stringprintf.h" | |
27 #include "base/synchronization/waitable_event.h" | |
28 #include "base/test/launcher/unit_test_launcher.h" | |
29 #include "base/test/test_suite.h" | |
30 #include "base/time/time.h" | |
31 #include "base/trace_event/trace_event.h" | |
32 #include "content/browser/compositor/gl_helper.h" | |
33 #include "content/browser/compositor/gl_helper_readback_support.h" | |
34 #include "content/browser/compositor/gl_helper_scaling.h" | |
35 #include "gpu/command_buffer/client/gl_in_process_context.h" | |
36 #include "gpu/command_buffer/client/gles2_implementation.h" | |
37 #include "gpu/command_buffer/client/shared_memory_limits.h" | |
38 #include "testing/gtest/include/gtest/gtest.h" | |
39 #include "third_party/skia/include/core/SkBitmap.h" | |
40 #include "third_party/skia/include/core/SkTypes.h" | |
41 #include "ui/gl/gl_implementation.h" | |
42 | |
43 namespace content { | |
44 | |
45 content::GLHelper::ScalerQuality kQualities[] = { | |
46 content::GLHelper::SCALER_QUALITY_BEST, | |
47 content::GLHelper::SCALER_QUALITY_GOOD, | |
48 content::GLHelper::SCALER_QUALITY_FAST, | |
49 }; | |
50 | |
51 const char* kQualityNames[] = { | |
52 "best", "good", "fast", | |
53 }; | |
54 | |
55 class GLHelperTest : public testing::Test { | |
56 protected: | |
57 void SetUp() override { | |
58 gpu::gles2::ContextCreationAttribHelper attributes; | |
59 attributes.alpha_size = 8; | |
60 attributes.depth_size = 24; | |
61 attributes.red_size = 8; | |
62 attributes.green_size = 8; | |
63 attributes.blue_size = 8; | |
64 attributes.stencil_size = 8; | |
65 attributes.samples = 4; | |
66 attributes.sample_buffers = 1; | |
67 attributes.bind_generates_resource = false; | |
68 | |
69 context_.reset(gpu::GLInProcessContext::Create( | |
70 nullptr, /* service */ | |
71 nullptr, /* surface */ | |
72 true, /* offscreen */ | |
73 gfx::kNullAcceleratedWidget, /* window */ | |
74 gfx::Size(1, 1), /* size */ | |
75 nullptr, /* share_context */ | |
76 attributes, gfx::PreferDiscreteGpu, gpu::SharedMemoryLimits(), | |
77 nullptr, /* gpu_memory_buffer_manager */ | |
78 nullptr /* image_factory */)); | |
79 gl_ = context_->GetImplementation(); | |
80 gpu::ContextSupport* support = context_->GetImplementation(); | |
81 | |
82 helper_.reset(new content::GLHelper(gl_, support)); | |
83 helper_scaling_.reset(new content::GLHelperScaling(gl_, helper_.get())); | |
84 } | |
85 | |
86 void TearDown() override { | |
87 helper_scaling_.reset(nullptr); | |
88 helper_.reset(nullptr); | |
89 context_.reset(nullptr); | |
90 } | |
91 | |
92 // Bicubic filter kernel function. | |
93 static float Bicubic(float x) { | |
94 const float a = -0.5; | |
95 x = std::abs(x); | |
96 float x2 = x * x; | |
97 float x3 = x2 * x; | |
98 if (x <= 1) { | |
99 return (a + 2) * x3 - (a + 3) * x2 + 1; | |
100 } else if (x < 2) { | |
101 return a * x3 - 5 * a * x2 + 8 * a * x - 4 * a; | |
102 } else { | |
103 return 0.0f; | |
104 } | |
105 } | |
106 | |
107 // Look up a single channel value. Works for 4-channel and single channel | |
108 // bitmaps. Clamp x/y. | |
109 int Channel(SkBitmap* pixels, int x, int y, int c) { | |
110 if (pixels->bytesPerPixel() == 4) { | |
111 uint32_t* data = | |
112 pixels->getAddr32(std::max(0, std::min(x, pixels->width() - 1)), | |
113 std::max(0, std::min(y, pixels->height() - 1))); | |
114 return (*data) >> (c * 8) & 0xff; | |
115 } else { | |
116 DCHECK_EQ(pixels->bytesPerPixel(), 1); | |
117 DCHECK_EQ(c, 0); | |
118 return *pixels->getAddr8(std::max(0, std::min(x, pixels->width() - 1)), | |
119 std::max(0, std::min(y, pixels->height() - 1))); | |
120 } | |
121 } | |
122 | |
123 // Set a single channel value. Works for 4-channel and single channel | |
124 // bitmaps. Clamp x/y. | |
125 void SetChannel(SkBitmap* pixels, int x, int y, int c, int v) { | |
126 DCHECK_GE(x, 0); | |
127 DCHECK_GE(y, 0); | |
128 DCHECK_LT(x, pixels->width()); | |
129 DCHECK_LT(y, pixels->height()); | |
130 if (pixels->bytesPerPixel() == 4) { | |
131 uint32_t* data = pixels->getAddr32(x, y); | |
132 v = std::max(0, std::min(v, 255)); | |
133 *data = (*data & ~(0xffu << (c * 8))) | (v << (c * 8)); | |
134 } else { | |
135 DCHECK_EQ(pixels->bytesPerPixel(), 1); | |
136 DCHECK_EQ(c, 0); | |
137 uint8_t* data = pixels->getAddr8(x, y); | |
138 v = std::max(0, std::min(v, 255)); | |
139 *data = v; | |
140 } | |
141 } | |
142 | |
143 // Print all the R, G, B or A values from an SkBitmap in a | |
144 // human-readable format. | |
145 void PrintChannel(SkBitmap* pixels, int c) { | |
146 for (int y = 0; y < pixels->height(); y++) { | |
147 std::string formatted; | |
148 for (int x = 0; x < pixels->width(); x++) { | |
149 formatted.append(base::StringPrintf("%3d, ", Channel(pixels, x, y, c))); | |
150 } | |
151 LOG(ERROR) << formatted; | |
152 } | |
153 } | |
154 | |
155 // Print out the individual steps of a scaler pipeline. | |
156 std::string PrintStages( | |
157 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages) { | |
158 std::string ret; | |
159 for (size_t i = 0; i < scaler_stages.size(); i++) { | |
160 ret.append(base::StringPrintf( | |
161 "%dx%d -> %dx%d ", scaler_stages[i].src_size.width(), | |
162 scaler_stages[i].src_size.height(), scaler_stages[i].dst_size.width(), | |
163 scaler_stages[i].dst_size.height())); | |
164 bool xy_matters = false; | |
165 switch (scaler_stages[i].shader) { | |
166 case GLHelperScaling::SHADER_BILINEAR: | |
167 ret.append("bilinear"); | |
168 break; | |
169 case GLHelperScaling::SHADER_BILINEAR2: | |
170 ret.append("bilinear2"); | |
171 xy_matters = true; | |
172 break; | |
173 case GLHelperScaling::SHADER_BILINEAR3: | |
174 ret.append("bilinear3"); | |
175 xy_matters = true; | |
176 break; | |
177 case GLHelperScaling::SHADER_BILINEAR4: | |
178 ret.append("bilinear4"); | |
179 xy_matters = true; | |
180 break; | |
181 case GLHelperScaling::SHADER_BILINEAR2X2: | |
182 ret.append("bilinear2x2"); | |
183 break; | |
184 case GLHelperScaling::SHADER_BICUBIC_UPSCALE: | |
185 ret.append("bicubic upscale"); | |
186 xy_matters = true; | |
187 break; | |
188 case GLHelperScaling::SHADER_BICUBIC_HALF_1D: | |
189 ret.append("bicubic 1/2"); | |
190 xy_matters = true; | |
191 break; | |
192 case GLHelperScaling::SHADER_PLANAR: | |
193 ret.append("planar"); | |
194 break; | |
195 case GLHelperScaling::SHADER_YUV_MRT_PASS1: | |
196 ret.append("rgb2yuv pass 1"); | |
197 break; | |
198 case GLHelperScaling::SHADER_YUV_MRT_PASS2: | |
199 ret.append("rgb2yuv pass 2"); | |
200 break; | |
201 } | |
202 | |
203 if (xy_matters) { | |
204 if (scaler_stages[i].scale_x) { | |
205 ret.append(" X"); | |
206 } else { | |
207 ret.append(" Y"); | |
208 } | |
209 } | |
210 ret.append("\n"); | |
211 } | |
212 return ret; | |
213 } | |
214 | |
215 bool CheckScale(double scale, int samples, bool already_scaled) { | |
216 // 1:1 is valid if there is one sample. | |
217 if (samples == 1 && scale == 1.0) { | |
218 return true; | |
219 } | |
220 // Is it an exact down-scale (50%, 25%, etc.?) | |
221 if (scale == 2.0 * samples) { | |
222 return true; | |
223 } | |
224 // Upscales, only valid if we haven't already scaled in this dimension. | |
225 if (!already_scaled) { | |
226 // Is it a valid bilinear upscale? | |
227 if (samples == 1 && scale <= 1.0) { | |
228 return true; | |
229 } | |
230 // Multi-sample upscale-downscale combination? | |
231 if (scale > samples / 2.0 && scale < samples) { | |
232 return true; | |
233 } | |
234 } | |
235 return false; | |
236 } | |
237 | |
238 // Make sure that the stages of the scaler pipeline are sane. | |
239 void ValidateScalerStages( | |
240 content::GLHelper::ScalerQuality quality, | |
241 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages, | |
242 const gfx::Size& dst_size, | |
243 const std::string& message) { | |
244 bool previous_error = HasFailure(); | |
245 // First, check that the input size for each stage is equal to | |
246 // the output size of the previous stage. | |
247 for (size_t i = 1; i < scaler_stages.size(); i++) { | |
248 EXPECT_EQ(scaler_stages[i - 1].dst_size.width(), | |
249 scaler_stages[i].src_size.width()); | |
250 EXPECT_EQ(scaler_stages[i - 1].dst_size.height(), | |
251 scaler_stages[i].src_size.height()); | |
252 EXPECT_EQ(scaler_stages[i].src_subrect.x(), 0); | |
253 EXPECT_EQ(scaler_stages[i].src_subrect.y(), 0); | |
254 EXPECT_EQ(scaler_stages[i].src_subrect.width(), | |
255 scaler_stages[i].src_size.width()); | |
256 EXPECT_EQ(scaler_stages[i].src_subrect.height(), | |
257 scaler_stages[i].src_size.height()); | |
258 } | |
259 | |
260 // Check the output size matches the destination of the last stage | |
261 EXPECT_EQ(scaler_stages[scaler_stages.size() - 1].dst_size.width(), | |
262 dst_size.width()); | |
263 EXPECT_EQ(scaler_stages[scaler_stages.size() - 1].dst_size.height(), | |
264 dst_size.height()); | |
265 | |
266 // Used to verify that up-scales are not attempted after some | |
267 // other scale. | |
268 bool scaled_x = false; | |
269 bool scaled_y = false; | |
270 | |
271 for (size_t i = 0; i < scaler_stages.size(); i++) { | |
272 // Note: 2.0 means scaling down by 50% | |
273 double x_scale = | |
274 static_cast<double>(scaler_stages[i].src_subrect.width()) / | |
275 static_cast<double>(scaler_stages[i].dst_size.width()); | |
276 double y_scale = | |
277 static_cast<double>(scaler_stages[i].src_subrect.height()) / | |
278 static_cast<double>(scaler_stages[i].dst_size.height()); | |
279 | |
280 int x_samples = 0; | |
281 int y_samples = 0; | |
282 | |
283 // Codify valid scale operations. | |
284 switch (scaler_stages[i].shader) { | |
285 case GLHelperScaling::SHADER_PLANAR: | |
286 case GLHelperScaling::SHADER_YUV_MRT_PASS1: | |
287 case GLHelperScaling::SHADER_YUV_MRT_PASS2: | |
288 EXPECT_TRUE(false) << "Invalid shader."; | |
289 break; | |
290 | |
291 case GLHelperScaling::SHADER_BILINEAR: | |
292 if (quality != content::GLHelper::SCALER_QUALITY_FAST) { | |
293 x_samples = 1; | |
294 y_samples = 1; | |
295 } | |
296 break; | |
297 case GLHelperScaling::SHADER_BILINEAR2: | |
298 x_samples = 2; | |
299 y_samples = 1; | |
300 break; | |
301 case GLHelperScaling::SHADER_BILINEAR3: | |
302 x_samples = 3; | |
303 y_samples = 1; | |
304 break; | |
305 case GLHelperScaling::SHADER_BILINEAR4: | |
306 x_samples = 4; | |
307 y_samples = 1; | |
308 break; | |
309 case GLHelperScaling::SHADER_BILINEAR2X2: | |
310 x_samples = 2; | |
311 y_samples = 2; | |
312 break; | |
313 case GLHelperScaling::SHADER_BICUBIC_UPSCALE: | |
314 if (scaler_stages[i].scale_x) { | |
315 EXPECT_LT(x_scale, 1.0); | |
316 EXPECT_EQ(y_scale, 1.0); | |
317 } else { | |
318 EXPECT_EQ(x_scale, 1.0); | |
319 EXPECT_LT(y_scale, 1.0); | |
320 } | |
321 break; | |
322 case GLHelperScaling::SHADER_BICUBIC_HALF_1D: | |
323 if (scaler_stages[i].scale_x) { | |
324 EXPECT_EQ(x_scale, 2.0); | |
325 EXPECT_EQ(y_scale, 1.0); | |
326 } else { | |
327 EXPECT_EQ(x_scale, 1.0); | |
328 EXPECT_EQ(y_scale, 2.0); | |
329 } | |
330 break; | |
331 } | |
332 | |
333 if (!scaler_stages[i].scale_x) { | |
334 std::swap(x_samples, y_samples); | |
335 } | |
336 | |
337 if (x_samples) { | |
338 EXPECT_TRUE(CheckScale(x_scale, x_samples, scaled_x)) << "x_scale = " | |
339 << x_scale; | |
340 } | |
341 if (y_samples) { | |
342 EXPECT_TRUE(CheckScale(y_scale, y_samples, scaled_y)) << "y_scale = " | |
343 << y_scale; | |
344 } | |
345 | |
346 if (x_scale != 1.0) { | |
347 scaled_x = true; | |
348 } | |
349 if (y_scale != 1.0) { | |
350 scaled_y = true; | |
351 } | |
352 } | |
353 | |
354 if (HasFailure() && !previous_error) { | |
355 LOG(ERROR) << "Invalid scaler stages: " << message; | |
356 LOG(ERROR) << "Scaler stages:"; | |
357 LOG(ERROR) << PrintStages(scaler_stages); | |
358 } | |
359 } | |
360 | |
361 // Compares two bitmaps taking color types into account. Checks whether each | |
362 // component of each pixel is no more than |maxdiff| apart. If bitmaps are not | |
363 // similar enough, prints out |truth|, |other|, |source|, |scaler_stages| | |
364 // and |message|. | |
365 void Compare(SkBitmap* truth, | |
366 SkBitmap* other, | |
367 int maxdiff, | |
368 SkBitmap* source, | |
369 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages, | |
370 std::string message) { | |
371 EXPECT_EQ(truth->width(), other->width()); | |
372 EXPECT_EQ(truth->height(), other->height()); | |
373 bool swizzle = (truth->colorType() == kRGBA_8888_SkColorType && | |
374 other->colorType() == kBGRA_8888_SkColorType) || | |
375 (truth->colorType() == kBGRA_8888_SkColorType && | |
376 other->colorType() == kRGBA_8888_SkColorType); | |
377 EXPECT_TRUE(swizzle || truth->colorType() == other->colorType()); | |
378 int bpp = truth->bytesPerPixel(); | |
379 for (int x = 0; x < truth->width(); x++) { | |
380 for (int y = 0; y < truth->height(); y++) { | |
381 for (int c = 0; c < bpp; c++) { | |
382 int a = Channel(truth, x, y, c); | |
383 // swizzle when comparing if needed | |
384 int b = swizzle && (c == 0 || c == 2) | |
385 ? Channel(other, x, y, (c + 2) & 2) | |
386 : Channel(other, x, y, c); | |
387 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " c=" << c | |
388 << " " << message; | |
389 if (std::abs(a - b) > maxdiff) { | |
390 LOG(ERROR) << "-------expected--------"; | |
391 for (int i = 0; i < bpp; i++) { | |
392 LOG(ERROR) << "Channel " << i << ":"; | |
393 PrintChannel(truth, i); | |
394 } | |
395 LOG(ERROR) << "-------actual--------"; | |
396 for (int i = 0; i < bpp; i++) { | |
397 LOG(ERROR) << "Channel " << i << ":"; | |
398 PrintChannel(other, i); | |
399 } | |
400 if (source) { | |
401 LOG(ERROR) << "-------original--------"; | |
402 for (int i = 0; i < source->bytesPerPixel(); i++) { | |
403 LOG(ERROR) << "Channel " << i << ":"; | |
404 PrintChannel(source, i); | |
405 } | |
406 } | |
407 LOG(ERROR) << "-----Scaler stages------"; | |
408 LOG(ERROR) << PrintStages(scaler_stages); | |
409 return; | |
410 } | |
411 } | |
412 } | |
413 } | |
414 } | |
415 | |
416 // Get a single R, G, B or A value as a float. | |
417 float ChannelAsFloat(SkBitmap* pixels, int x, int y, int c) { | |
418 return Channel(pixels, x, y, c) / 255.0; | |
419 } | |
420 | |
421 // Works like a GL_LINEAR lookup on an SkBitmap. | |
422 float Bilinear(SkBitmap* pixels, float x, float y, int c) { | |
423 x -= 0.5; | |
424 y -= 0.5; | |
425 int base_x = static_cast<int>(floorf(x)); | |
426 int base_y = static_cast<int>(floorf(y)); | |
427 x -= base_x; | |
428 y -= base_y; | |
429 return (ChannelAsFloat(pixels, base_x, base_y, c) * (1 - x) * (1 - y) + | |
430 ChannelAsFloat(pixels, base_x + 1, base_y, c) * x * (1 - y) + | |
431 ChannelAsFloat(pixels, base_x, base_y + 1, c) * (1 - x) * y + | |
432 ChannelAsFloat(pixels, base_x + 1, base_y + 1, c) * x * y); | |
433 } | |
434 | |
435 // Encodes an RGBA bitmap to grayscale. | |
436 // Reference implementation for | |
437 // GLHelper::CopyToTextureImpl::EncodeTextureAsGrayscale. | |
438 void EncodeToGrayscaleSlow(SkBitmap* input, SkBitmap* output) { | |
439 const float kRGBtoGrayscaleColorWeights[3] = {0.213f, 0.715f, 0.072f}; | |
440 CHECK_EQ(kAlpha_8_SkColorType, output->colorType()); | |
441 CHECK_EQ(input->width(), output->width()); | |
442 CHECK_EQ(input->height(), output->height()); | |
443 CHECK_EQ(input->colorType(), kRGBA_8888_SkColorType); | |
444 | |
445 for (int dst_y = 0; dst_y < output->height(); dst_y++) { | |
446 for (int dst_x = 0; dst_x < output->width(); dst_x++) { | |
447 float c0 = ChannelAsFloat(input, dst_x, dst_y, 0); | |
448 float c1 = ChannelAsFloat(input, dst_x, dst_y, 1); | |
449 float c2 = ChannelAsFloat(input, dst_x, dst_y, 2); | |
450 float value = c0 * kRGBtoGrayscaleColorWeights[0] + | |
451 c1 * kRGBtoGrayscaleColorWeights[1] + | |
452 c2 * kRGBtoGrayscaleColorWeights[2]; | |
453 SetChannel(output, dst_x, dst_y, 0, | |
454 static_cast<int>(value * 255.0f + 0.5f)); | |
455 } | |
456 } | |
457 } | |
458 | |
459 // Very slow bicubic / bilinear scaler for reference. | |
460 void ScaleSlow(SkBitmap* input, | |
461 SkBitmap* output, | |
462 content::GLHelper::ScalerQuality quality) { | |
463 float xscale = static_cast<float>(input->width()) / output->width(); | |
464 float yscale = static_cast<float>(input->height()) / output->height(); | |
465 float clamped_xscale = xscale < 1.0 ? 1.0 : 1.0 / xscale; | |
466 float clamped_yscale = yscale < 1.0 ? 1.0 : 1.0 / yscale; | |
467 for (int dst_y = 0; dst_y < output->height(); dst_y++) { | |
468 for (int dst_x = 0; dst_x < output->width(); dst_x++) { | |
469 for (int channel = 0; channel < 4; channel++) { | |
470 float dst_x_in_src = (dst_x + 0.5f) * xscale; | |
471 float dst_y_in_src = (dst_y + 0.5f) * yscale; | |
472 | |
473 float value = 0.0f; | |
474 float sum = 0.0f; | |
475 switch (quality) { | |
476 case content::GLHelper::SCALER_QUALITY_BEST: | |
477 for (int src_y = -10; src_y < input->height() + 10; ++src_y) { | |
478 float coeff_y = | |
479 Bicubic((src_y + 0.5f - dst_y_in_src) * clamped_yscale); | |
480 if (coeff_y == 0.0f) { | |
481 continue; | |
482 } | |
483 for (int src_x = -10; src_x < input->width() + 10; ++src_x) { | |
484 float coeff = | |
485 coeff_y * | |
486 Bicubic((src_x + 0.5f - dst_x_in_src) * clamped_xscale); | |
487 if (coeff == 0.0f) { | |
488 continue; | |
489 } | |
490 sum += coeff; | |
491 float c = ChannelAsFloat(input, src_x, src_y, channel); | |
492 value += c * coeff; | |
493 } | |
494 } | |
495 break; | |
496 | |
497 case content::GLHelper::SCALER_QUALITY_GOOD: { | |
498 int xshift = 0, yshift = 0; | |
499 while ((output->width() << xshift) < input->width()) { | |
500 xshift++; | |
501 } | |
502 while ((output->height() << yshift) < input->height()) { | |
503 yshift++; | |
504 } | |
505 int xmag = 1 << xshift; | |
506 int ymag = 1 << yshift; | |
507 if (xmag == 4 && output->width() * 3 >= input->width()) { | |
508 xmag = 3; | |
509 } | |
510 if (ymag == 4 && output->height() * 3 >= input->height()) { | |
511 ymag = 3; | |
512 } | |
513 for (int x = 0; x < xmag; x++) { | |
514 for (int y = 0; y < ymag; y++) { | |
515 value += Bilinear( | |
516 input, (dst_x * xmag + x + 0.5) * xscale / xmag, | |
517 (dst_y * ymag + y + 0.5) * yscale / ymag, channel); | |
518 sum += 1.0; | |
519 } | |
520 } | |
521 break; | |
522 } | |
523 | |
524 case content::GLHelper::SCALER_QUALITY_FAST: | |
525 value = Bilinear(input, dst_x_in_src, dst_y_in_src, channel); | |
526 sum = 1.0; | |
527 } | |
528 value /= sum; | |
529 SetChannel(output, dst_x, dst_y, channel, | |
530 static_cast<int>(value * 255.0f + 0.5f)); | |
531 } | |
532 } | |
533 } | |
534 } | |
535 | |
536 void FlipSKBitmap(SkBitmap* bitmap) { | |
537 int bpp = bitmap->bytesPerPixel(); | |
538 DCHECK(bpp == 4 || bpp == 1); | |
539 int top_line = 0; | |
540 int bottom_line = bitmap->height() - 1; | |
541 while (top_line < bottom_line) { | |
542 for (int x = 0; x < bitmap->width(); x++) { | |
543 bpp == 4 ? std::swap(*bitmap->getAddr32(x, top_line), | |
544 *bitmap->getAddr32(x, bottom_line)) | |
545 : std::swap(*bitmap->getAddr8(x, top_line), | |
546 *bitmap->getAddr8(x, bottom_line)); | |
547 } | |
548 top_line++; | |
549 bottom_line--; | |
550 } | |
551 } | |
552 | |
553 // Swaps red and blue channels in each pixel in a 32-bit bitmap. | |
554 void SwizzleSKBitmap(SkBitmap* bitmap) { | |
555 int bpp = bitmap->bytesPerPixel(); | |
556 DCHECK(bpp == 4); | |
557 for (int y = 0; y < bitmap->height(); y++) { | |
558 for (int x = 0; x < bitmap->width(); x++) { | |
559 // Swap channels 0 and 2 (red and blue) | |
560 int c0 = Channel(bitmap, x, y, 0); | |
561 int c2 = Channel(bitmap, x, y, 2); | |
562 SetChannel(bitmap, x, y, 2, c0); | |
563 SetChannel(bitmap, x, y, 0, c2); | |
564 } | |
565 } | |
566 } | |
567 | |
568 // gl_helper scales recursively, so we'll need to do that | |
569 // in the reference implementation too. | |
570 void ScaleSlowRecursive(SkBitmap* input, | |
571 SkBitmap* output, | |
572 content::GLHelper::ScalerQuality quality) { | |
573 if (quality == content::GLHelper::SCALER_QUALITY_FAST || | |
574 quality == content::GLHelper::SCALER_QUALITY_GOOD) { | |
575 ScaleSlow(input, output, quality); | |
576 return; | |
577 } | |
578 | |
579 float xscale = static_cast<float>(output->width()) / input->width(); | |
580 | |
581 // This corresponds to all the operations we can do directly. | |
582 float yscale = static_cast<float>(output->height()) / input->height(); | |
583 if ((xscale == 1.0f && yscale == 1.0f) || | |
584 (xscale == 0.5f && yscale == 1.0f) || | |
585 (xscale == 1.0f && yscale == 0.5f) || | |
586 (xscale >= 1.0f && yscale == 1.0f) || | |
587 (xscale == 1.0f && yscale >= 1.0f)) { | |
588 ScaleSlow(input, output, quality); | |
589 return; | |
590 } | |
591 | |
592 // Now we break the problem down into smaller pieces, using the | |
593 // operations available. | |
594 int xtmp = input->width(); | |
595 int ytmp = input->height(); | |
596 | |
597 if (output->height() != input->height()) { | |
598 ytmp = output->height(); | |
599 while (ytmp < input->height() && ytmp * 2 != input->height()) { | |
600 ytmp += ytmp; | |
601 } | |
602 } else { | |
603 xtmp = output->width(); | |
604 while (xtmp < input->width() && xtmp * 2 != input->width()) { | |
605 xtmp += xtmp; | |
606 } | |
607 } | |
608 | |
609 SkBitmap tmp; | |
610 tmp.allocN32Pixels(xtmp, ytmp); | |
611 | |
612 ScaleSlowRecursive(input, &tmp, quality); | |
613 ScaleSlowRecursive(&tmp, output, quality); | |
614 } | |
615 | |
616 // Creates an RGBA SkBitmap | |
617 std::unique_ptr<SkBitmap> CreateTestBitmap(int width, | |
618 int height, | |
619 int test_pattern) { | |
620 std::unique_ptr<SkBitmap> bitmap(new SkBitmap); | |
621 bitmap->allocPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, | |
622 kPremul_SkAlphaType)); | |
623 | |
624 for (int x = 0; x < width; ++x) { | |
625 for (int y = 0; y < height; ++y) { | |
626 switch (test_pattern) { | |
627 case 0: // Smooth test pattern | |
628 SetChannel(bitmap.get(), x, y, 0, x * 10); | |
629 SetChannel(bitmap.get(), x, y, 0, y == 0 ? x * 50 : x * 10); | |
630 SetChannel(bitmap.get(), x, y, 1, y * 10); | |
631 SetChannel(bitmap.get(), x, y, 2, (x + y) * 10); | |
632 SetChannel(bitmap.get(), x, y, 3, 255); | |
633 break; | |
634 case 1: // Small blocks | |
635 SetChannel(bitmap.get(), x, y, 0, x & 1 ? 255 : 0); | |
636 SetChannel(bitmap.get(), x, y, 1, y & 1 ? 255 : 0); | |
637 SetChannel(bitmap.get(), x, y, 2, (x + y) & 1 ? 255 : 0); | |
638 SetChannel(bitmap.get(), x, y, 3, 255); | |
639 break; | |
640 case 2: // Medium blocks | |
641 SetChannel(bitmap.get(), x, y, 0, 10 + x / 2 * 50); | |
642 SetChannel(bitmap.get(), x, y, 1, 10 + y / 3 * 50); | |
643 SetChannel(bitmap.get(), x, y, 2, (x + y) / 5 * 50 + 5); | |
644 SetChannel(bitmap.get(), x, y, 3, 255); | |
645 break; | |
646 } | |
647 } | |
648 } | |
649 return bitmap; | |
650 } | |
651 | |
652 // Binds texture and framebuffer and loads the bitmap pixels into the texture. | |
653 void BindTextureAndFrameBuffer(GLuint texture, | |
654 GLuint framebuffer, | |
655 SkBitmap* bitmap, | |
656 int width, | |
657 int height) { | |
658 gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer); | |
659 gl_->BindTexture(GL_TEXTURE_2D, texture); | |
660 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, | |
661 GL_UNSIGNED_BYTE, bitmap->getPixels()); | |
662 } | |
663 | |
664 // Create a test image, transform it using | |
665 // GLHelper::CropScaleReadbackAndCleanTexture and a reference implementation | |
666 // and compare the results. | |
667 void TestCropScaleReadbackAndCleanTexture(int xsize, | |
668 int ysize, | |
669 int scaled_xsize, | |
670 int scaled_ysize, | |
671 int test_pattern, | |
672 SkColorType out_color_type, | |
673 bool swizzle, | |
674 size_t quality_index) { | |
675 DCHECK(out_color_type == kAlpha_8_SkColorType || | |
676 out_color_type == kRGBA_8888_SkColorType || | |
677 out_color_type == kBGRA_8888_SkColorType); | |
678 GLuint src_texture; | |
679 gl_->GenTextures(1, &src_texture); | |
680 GLuint framebuffer; | |
681 gl_->GenFramebuffers(1, &framebuffer); | |
682 std::unique_ptr<SkBitmap> input_pixels = | |
683 CreateTestBitmap(xsize, ysize, test_pattern); | |
684 BindTextureAndFrameBuffer(src_texture, framebuffer, input_pixels.get(), | |
685 xsize, ysize); | |
686 | |
687 std::string message = base::StringPrintf( | |
688 "input size: %dx%d " | |
689 "output size: %dx%d " | |
690 "pattern: %d , quality: %s, " | |
691 "out_color_type: %d", | |
692 xsize, ysize, scaled_xsize, scaled_ysize, test_pattern, | |
693 kQualityNames[quality_index], out_color_type); | |
694 | |
695 // Transform the bitmap using GLHelper::CropScaleReadbackAndCleanTexture. | |
696 SkBitmap output_pixels; | |
697 output_pixels.allocPixels(SkImageInfo::Make( | |
698 scaled_xsize, scaled_ysize, out_color_type, kPremul_SkAlphaType)); | |
699 base::RunLoop run_loop; | |
700 gfx::Size encoded_texture_size; | |
701 helper_->CropScaleReadbackAndCleanTexture( | |
702 src_texture, gfx::Size(xsize, ysize), gfx::Rect(xsize, ysize), | |
703 gfx::Size(scaled_xsize, scaled_ysize), | |
704 static_cast<unsigned char*>(output_pixels.getPixels()), out_color_type, | |
705 base::Bind(&callcallback, run_loop.QuitClosure()), | |
706 kQualities[quality_index]); | |
707 run_loop.Run(); | |
708 // CropScaleReadbackAndCleanTexture flips the pixels. Flip them back. | |
709 FlipSKBitmap(&output_pixels); | |
710 | |
711 // If the bitmap shouldn't have changed - compare against input. | |
712 if (xsize == scaled_xsize && ysize == scaled_ysize && | |
713 out_color_type != kAlpha_8_SkColorType) { | |
714 const std::vector<GLHelperScaling::ScalerStage> dummy_stages; | |
715 Compare(input_pixels.get(), &output_pixels, 0, nullptr, dummy_stages, | |
716 message + " comparing against input"); | |
717 return; | |
718 } | |
719 | |
720 // Now transform the bitmap using the reference implementation. | |
721 SkBitmap scaled_pixels; | |
722 scaled_pixels.allocPixels(SkImageInfo::Make(scaled_xsize, scaled_ysize, | |
723 kRGBA_8888_SkColorType, | |
724 kPremul_SkAlphaType)); | |
725 SkBitmap truth_pixels; | |
726 // Step 1: Scale | |
727 ScaleSlowRecursive(input_pixels.get(), &scaled_pixels, | |
728 kQualities[quality_index]); | |
729 // Step 2: Encode to grayscale if needed. | |
730 if (out_color_type == kAlpha_8_SkColorType) { | |
731 truth_pixels.allocPixels(SkImageInfo::Make( | |
732 scaled_xsize, scaled_ysize, out_color_type, kPremul_SkAlphaType)); | |
733 EncodeToGrayscaleSlow(&scaled_pixels, &truth_pixels); | |
734 } else { | |
735 truth_pixels = scaled_pixels; | |
736 } | |
737 | |
738 // Now compare the results. | |
739 SkAutoLockPixels lock_input(truth_pixels); | |
740 const std::vector<GLHelperScaling::ScalerStage> dummy_stages; | |
741 Compare(&truth_pixels, &output_pixels, 2, input_pixels.get(), dummy_stages, | |
742 message + " comparing against transformed/scaled"); | |
743 | |
744 gl_->DeleteTextures(1, &src_texture); | |
745 gl_->DeleteFramebuffers(1, &framebuffer); | |
746 } | |
747 | |
748 // Scaling test: Create a test image, scale it using GLHelperScaling | |
749 // and a reference implementation and compare the results. | |
750 void TestScale(int xsize, | |
751 int ysize, | |
752 int scaled_xsize, | |
753 int scaled_ysize, | |
754 int test_pattern, | |
755 size_t quality_index, | |
756 bool flip) { | |
757 GLuint src_texture; | |
758 gl_->GenTextures(1, &src_texture); | |
759 GLuint framebuffer; | |
760 gl_->GenFramebuffers(1, &framebuffer); | |
761 std::unique_ptr<SkBitmap> input_pixels = | |
762 CreateTestBitmap(xsize, ysize, test_pattern); | |
763 BindTextureAndFrameBuffer(src_texture, framebuffer, input_pixels.get(), | |
764 xsize, ysize); | |
765 | |
766 std::string message = base::StringPrintf( | |
767 "input size: %dx%d " | |
768 "output size: %dx%d " | |
769 "pattern: %d quality: %s", | |
770 xsize, ysize, scaled_xsize, scaled_ysize, test_pattern, | |
771 kQualityNames[quality_index]); | |
772 | |
773 std::vector<GLHelperScaling::ScalerStage> stages; | |
774 helper_scaling_->ComputeScalerStages(kQualities[quality_index], | |
775 gfx::Size(xsize, ysize), | |
776 gfx::Rect(0, 0, xsize, ysize), | |
777 gfx::Size(scaled_xsize, scaled_ysize), | |
778 flip, | |
779 false, | |
780 &stages); | |
781 ValidateScalerStages(kQualities[quality_index], | |
782 stages, | |
783 gfx::Size(scaled_xsize, scaled_ysize), | |
784 message); | |
785 | |
786 GLuint dst_texture = helper_->CopyAndScaleTexture( | |
787 src_texture, gfx::Size(xsize, ysize), | |
788 gfx::Size(scaled_xsize, scaled_ysize), flip, kQualities[quality_index]); | |
789 | |
790 SkBitmap output_pixels; | |
791 output_pixels.allocPixels(SkImageInfo::Make(scaled_xsize, scaled_ysize, | |
792 kRGBA_8888_SkColorType, | |
793 kPremul_SkAlphaType)); | |
794 | |
795 helper_->ReadbackTextureSync( | |
796 dst_texture, gfx::Rect(0, 0, scaled_xsize, scaled_ysize), | |
797 static_cast<unsigned char*>(output_pixels.getPixels()), | |
798 kRGBA_8888_SkColorType); | |
799 if (flip) { | |
800 // Flip the pixels back. | |
801 FlipSKBitmap(&output_pixels); | |
802 } | |
803 | |
804 // If the bitmap shouldn't have changed - compare against input. | |
805 if (xsize == scaled_xsize && ysize == scaled_ysize) { | |
806 Compare(input_pixels.get(), &output_pixels, 0, nullptr, stages, | |
807 message + " comparing against input"); | |
808 return; | |
809 } | |
810 | |
811 // Now scale the bitmap using the reference implementation. | |
812 SkBitmap truth_pixels; | |
813 truth_pixels.allocPixels(SkImageInfo::Make(scaled_xsize, scaled_ysize, | |
814 kRGBA_8888_SkColorType, | |
815 kPremul_SkAlphaType)); | |
816 ScaleSlowRecursive(input_pixels.get(), &truth_pixels, | |
817 kQualities[quality_index]); | |
818 Compare(&truth_pixels, &output_pixels, 2, input_pixels.get(), stages, | |
819 message + " comparing against scaled"); | |
820 | |
821 gl_->DeleteTextures(1, &src_texture); | |
822 gl_->DeleteTextures(1, &dst_texture); | |
823 gl_->DeleteFramebuffers(1, &framebuffer); | |
824 } | |
825 | |
826 // Create a scaling pipeline and check that it is made up of | |
827 // valid scaling operations. | |
828 void TestScalerPipeline(size_t quality, | |
829 int xsize, | |
830 int ysize, | |
831 int dst_xsize, | |
832 int dst_ysize) { | |
833 std::vector<GLHelperScaling::ScalerStage> stages; | |
834 helper_scaling_->ComputeScalerStages( | |
835 kQualities[quality], gfx::Size(xsize, ysize), | |
836 gfx::Rect(0, 0, xsize, ysize), gfx::Size(dst_xsize, dst_ysize), false, | |
837 false, &stages); | |
838 ValidateScalerStages(kQualities[quality], stages, | |
839 gfx::Size(dst_xsize, dst_ysize), | |
840 base::StringPrintf("input size: %dx%d " | |
841 "output size: %dx%d " | |
842 "quality: %s", | |
843 xsize, ysize, dst_xsize, dst_ysize, | |
844 kQualityNames[quality])); | |
845 } | |
846 | |
847 // Create a scaling pipeline and make sure that the steps | |
848 // are exactly the steps we expect. | |
849 void CheckPipeline(content::GLHelper::ScalerQuality quality, | |
850 int xsize, | |
851 int ysize, | |
852 int dst_xsize, | |
853 int dst_ysize, | |
854 const std::string& description) { | |
855 std::vector<GLHelperScaling::ScalerStage> stages; | |
856 helper_scaling_->ComputeScalerStages( | |
857 quality, gfx::Size(xsize, ysize), gfx::Rect(0, 0, xsize, ysize), | |
858 gfx::Size(dst_xsize, dst_ysize), false, false, &stages); | |
859 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, | |
860 gfx::Size(dst_xsize, dst_ysize), ""); | |
861 EXPECT_EQ(PrintStages(stages), description); | |
862 } | |
863 | |
864 static void callcallback(const base::Callback<void()>& callback, | |
865 bool result) { | |
866 callback.Run(); | |
867 } | |
868 | |
869 void DrawGridToBitmap(int w, | |
870 int h, | |
871 SkColor background_color, | |
872 SkColor grid_color, | |
873 int grid_pitch, | |
874 int grid_width, | |
875 SkBitmap& bmp) { | |
876 ASSERT_GT(grid_pitch, 0); | |
877 ASSERT_GT(grid_width, 0); | |
878 ASSERT_NE(background_color, grid_color); | |
879 | |
880 for (int y = 0; y < h; ++y) { | |
881 bool y_on_grid = ((y % grid_pitch) < grid_width); | |
882 | |
883 for (int x = 0; x < w; ++x) { | |
884 bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width)); | |
885 | |
886 if (bmp.colorType() == kRGBA_8888_SkColorType || | |
887 bmp.colorType() == kBGRA_8888_SkColorType) { | |
888 *bmp.getAddr32(x, y) = (on_grid ? grid_color : background_color); | |
889 } else if (bmp.colorType() == kRGB_565_SkColorType) { | |
890 *bmp.getAddr16(x, y) = (on_grid ? grid_color : background_color); | |
891 } | |
892 } | |
893 } | |
894 } | |
895 | |
896 void DrawCheckerToBitmap(int w, | |
897 int h, | |
898 SkColor color1, | |
899 SkColor color2, | |
900 int rect_w, | |
901 int rect_h, | |
902 SkBitmap& bmp) { | |
903 ASSERT_GT(rect_w, 0); | |
904 ASSERT_GT(rect_h, 0); | |
905 ASSERT_NE(color1, color2); | |
906 | |
907 for (int y = 0; y < h; ++y) { | |
908 bool y_bit = (((y / rect_h) & 0x1) == 0); | |
909 | |
910 for (int x = 0; x < w; ++x) { | |
911 bool x_bit = (((x / rect_w) & 0x1) == 0); | |
912 | |
913 bool use_color2 = (x_bit != y_bit); // xor | |
914 if (bmp.colorType() == kRGBA_8888_SkColorType || | |
915 bmp.colorType() == kBGRA_8888_SkColorType) { | |
916 *bmp.getAddr32(x, y) = (use_color2 ? color2 : color1); | |
917 } else if (bmp.colorType() == kRGB_565_SkColorType) { | |
918 *bmp.getAddr16(x, y) = (use_color2 ? color2 : color1); | |
919 } | |
920 } | |
921 } | |
922 } | |
923 | |
924 bool ColorComponentsClose(SkColor component1, | |
925 SkColor component2, | |
926 SkColorType color_type) { | |
927 int c1 = static_cast<int>(component1); | |
928 int c2 = static_cast<int>(component2); | |
929 bool result = false; | |
930 switch (color_type) { | |
931 case kRGBA_8888_SkColorType: | |
932 case kBGRA_8888_SkColorType: | |
933 result = (std::abs(c1 - c2) == 0); | |
934 break; | |
935 case kRGB_565_SkColorType: | |
936 result = (std::abs(c1 - c2) <= 7); | |
937 break; | |
938 default: | |
939 break; | |
940 } | |
941 return result; | |
942 } | |
943 | |
944 bool ColorsClose(SkColor color1, SkColor color2, SkColorType color_type) { | |
945 bool red = ColorComponentsClose(SkColorGetR(color1), SkColorGetR(color2), | |
946 color_type); | |
947 bool green = ColorComponentsClose(SkColorGetG(color1), SkColorGetG(color2), | |
948 color_type); | |
949 bool blue = ColorComponentsClose(SkColorGetB(color1), SkColorGetB(color2), | |
950 color_type); | |
951 bool alpha = ColorComponentsClose(SkColorGetA(color1), SkColorGetA(color2), | |
952 color_type); | |
953 if (color_type == kRGB_565_SkColorType) { | |
954 return red && blue && green; | |
955 } | |
956 return red && blue && green && alpha; | |
957 } | |
958 | |
959 bool IsEqual(const SkBitmap& bmp1, const SkBitmap& bmp2) { | |
960 if (bmp1.isNull() && bmp2.isNull()) | |
961 return true; | |
962 if (bmp1.width() != bmp2.width() || bmp1.height() != bmp2.height()) { | |
963 LOG(ERROR) << "Bitmap geometry check failure"; | |
964 return false; | |
965 } | |
966 if (bmp1.colorType() != bmp2.colorType()) | |
967 return false; | |
968 | |
969 SkAutoLockPixels lock1(bmp1); | |
970 SkAutoLockPixels lock2(bmp2); | |
971 if (!bmp1.getPixels() || !bmp2.getPixels()) { | |
972 LOG(ERROR) << "Empty Bitmap!"; | |
973 return false; | |
974 } | |
975 for (int y = 0; y < bmp1.height(); ++y) { | |
976 for (int x = 0; x < bmp1.width(); ++x) { | |
977 if (!ColorsClose(bmp1.getColor(x, y), bmp2.getColor(x, y), | |
978 bmp1.colorType())) { | |
979 LOG(ERROR) << "Bitmap color comparision failure"; | |
980 return false; | |
981 } | |
982 } | |
983 } | |
984 return true; | |
985 } | |
986 | |
987 void BindAndAttachTextureWithPixels(GLuint src_texture, | |
988 SkColorType color_type, | |
989 const gfx::Size& src_size, | |
990 const SkBitmap& input_pixels) { | |
991 gl_->BindTexture(GL_TEXTURE_2D, src_texture); | |
992 GLenum format = 0; | |
993 switch (color_type) { | |
994 case kBGRA_8888_SkColorType: | |
995 format = GL_BGRA_EXT; | |
996 break; | |
997 case kRGBA_8888_SkColorType: | |
998 format = GL_RGBA; | |
999 break; | |
1000 case kRGB_565_SkColorType: | |
1001 format = GL_RGB; | |
1002 break; | |
1003 default: | |
1004 NOTREACHED(); | |
1005 } | |
1006 GLenum type = (color_type == kRGB_565_SkColorType) ? | |
1007 GL_UNSIGNED_SHORT_5_6_5 : GL_UNSIGNED_BYTE; | |
1008 gl_->TexImage2D(GL_TEXTURE_2D, 0, format, src_size.width(), | |
1009 src_size.height(), 0, format, type, | |
1010 input_pixels.getPixels()); | |
1011 } | |
1012 | |
1013 void ReadBackTexture(GLuint src_texture, | |
1014 const gfx::Size& src_size, | |
1015 unsigned char* pixels, | |
1016 SkColorType color_type, | |
1017 bool async) { | |
1018 if (async) { | |
1019 base::RunLoop run_loop; | |
1020 helper_->ReadbackTextureAsync( | |
1021 src_texture, src_size, pixels, color_type, | |
1022 base::Bind(&callcallback, run_loop.QuitClosure())); | |
1023 run_loop.Run(); | |
1024 } else { | |
1025 helper_->ReadbackTextureSync(src_texture, gfx::Rect(src_size), pixels, | |
1026 color_type); | |
1027 } | |
1028 } | |
1029 // Test basic format readback. | |
1030 bool TestTextureFormatReadback(const gfx::Size& src_size, | |
1031 SkColorType color_type, | |
1032 bool async) { | |
1033 SkImageInfo info = SkImageInfo::Make(src_size.width(), src_size.height(), | |
1034 color_type, kPremul_SkAlphaType); | |
1035 if (!helper_->IsReadbackConfigSupported(color_type)) { | |
1036 LOG(INFO) << "Skipping test format not supported" << color_type; | |
1037 return true; | |
1038 } | |
1039 GLuint src_texture; | |
1040 gl_->GenTextures(1, &src_texture); | |
1041 SkBitmap input_pixels; | |
1042 input_pixels.allocPixels(info); | |
1043 // Test Pattern-1, Fill with Plain color pattern. | |
1044 // Erase the input bitmap with red color. | |
1045 input_pixels.eraseColor(SK_ColorRED); | |
1046 BindAndAttachTextureWithPixels(src_texture, color_type, src_size, | |
1047 input_pixels); | |
1048 SkBitmap output_pixels; | |
1049 output_pixels.allocPixels(info); | |
1050 // Initialize the output bitmap with Green color. | |
1051 // When the readback is over output bitmap should have the red color. | |
1052 output_pixels.eraseColor(SK_ColorGREEN); | |
1053 uint8_t* pixels = static_cast<uint8_t*>(output_pixels.getPixels()); | |
1054 ReadBackTexture(src_texture, src_size, pixels, color_type, async); | |
1055 bool result = IsEqual(input_pixels, output_pixels); | |
1056 if (!result) { | |
1057 LOG(ERROR) << "Bitmap comparision failure Pattern-1"; | |
1058 return false; | |
1059 } | |
1060 const int rect_w = 10, rect_h = 4, src_grid_pitch = 10, src_grid_width = 4; | |
1061 const SkColor color1 = SK_ColorRED, color2 = SK_ColorBLUE; | |
1062 // Test Pattern-2, Fill with Grid Pattern. | |
1063 DrawGridToBitmap(src_size.width(), src_size.height(), color2, color1, | |
1064 src_grid_pitch, src_grid_width, input_pixels); | |
1065 BindAndAttachTextureWithPixels(src_texture, color_type, src_size, | |
1066 input_pixels); | |
1067 ReadBackTexture(src_texture, src_size, pixels, color_type, async); | |
1068 result = IsEqual(input_pixels, output_pixels); | |
1069 if (!result) { | |
1070 LOG(ERROR) << "Bitmap comparision failure Pattern-2"; | |
1071 return false; | |
1072 } | |
1073 // Test Pattern-3, Fill with CheckerBoard Pattern. | |
1074 DrawCheckerToBitmap(src_size.width(), src_size.height(), color1, color2, | |
1075 rect_w, rect_h, input_pixels); | |
1076 BindAndAttachTextureWithPixels(src_texture, color_type, src_size, | |
1077 input_pixels); | |
1078 ReadBackTexture(src_texture, src_size, pixels, color_type, async); | |
1079 result = IsEqual(input_pixels, output_pixels); | |
1080 if (!result) { | |
1081 LOG(ERROR) << "Bitmap comparision failure Pattern-3"; | |
1082 return false; | |
1083 } | |
1084 gl_->DeleteTextures(1, &src_texture); | |
1085 if (HasFailure()) { | |
1086 return false; | |
1087 } | |
1088 return true; | |
1089 } | |
1090 | |
1091 void TestAddOps(int src, int dst, bool scale_x, bool allow3) { | |
1092 std::deque<GLHelperScaling::ScaleOp> ops; | |
1093 GLHelperScaling::ScaleOp::AddOps(src, dst, scale_x, allow3, &ops); | |
1094 // Scale factor 3 is a special case. | |
1095 // It is currently only allowed by itself. | |
1096 if (allow3 && dst * 3 >= src && dst * 2 < src) { | |
1097 EXPECT_EQ(ops[0].scale_factor, 3); | |
1098 EXPECT_EQ(ops.size(), 1U); | |
1099 EXPECT_EQ(ops[0].scale_x, scale_x); | |
1100 EXPECT_EQ(ops[0].scale_size, dst); | |
1101 return; | |
1102 } | |
1103 | |
1104 for (size_t i = 0; i < ops.size(); i++) { | |
1105 EXPECT_EQ(ops[i].scale_x, scale_x); | |
1106 if (i == 0) { | |
1107 // Only the first op is allowed to be a scale up. | |
1108 // (Scaling up *after* scaling down would make it fuzzy.) | |
1109 EXPECT_TRUE(ops[0].scale_factor == 0 || ops[0].scale_factor == 2); | |
1110 } else { | |
1111 // All other operations must be 50% downscales. | |
1112 EXPECT_EQ(ops[i].scale_factor, 2); | |
1113 } | |
1114 } | |
1115 // Check that the scale factors make sense and add up. | |
1116 int tmp = dst; | |
1117 for (int i = static_cast<int>(ops.size() - 1); i >= 0; i--) { | |
1118 EXPECT_EQ(tmp, ops[i].scale_size); | |
1119 if (ops[i].scale_factor == 0) { | |
1120 EXPECT_EQ(i, 0); | |
1121 EXPECT_GT(tmp, src); | |
1122 tmp = src; | |
1123 } else { | |
1124 tmp *= ops[i].scale_factor; | |
1125 } | |
1126 } | |
1127 EXPECT_EQ(tmp, src); | |
1128 } | |
1129 | |
1130 void CheckPipeline2(int xsize, | |
1131 int ysize, | |
1132 int dst_xsize, | |
1133 int dst_ysize, | |
1134 const std::string& description) { | |
1135 std::vector<GLHelperScaling::ScalerStage> stages; | |
1136 helper_scaling_->ConvertScalerOpsToScalerStages( | |
1137 content::GLHelper::SCALER_QUALITY_GOOD, gfx::Size(xsize, ysize), | |
1138 gfx::Rect(0, 0, xsize, ysize), gfx::Size(dst_xsize, dst_ysize), false, | |
1139 false, &x_ops_, &y_ops_, &stages); | |
1140 EXPECT_EQ(x_ops_.size(), 0U); | |
1141 EXPECT_EQ(y_ops_.size(), 0U); | |
1142 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, | |
1143 gfx::Size(dst_xsize, dst_ysize), ""); | |
1144 EXPECT_EQ(PrintStages(stages), description); | |
1145 } | |
1146 | |
1147 void CheckOptimizationsTest() { | |
1148 // Basic upscale. X and Y should be combined into one pass. | |
1149 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000)); | |
1150 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000)); | |
1151 CheckPipeline2(1024, 768, 2000, 2000, "1024x768 -> 2000x2000 bilinear\n"); | |
1152 | |
1153 // X scaled 1/2, Y upscaled, should still be one pass. | |
1154 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512)); | |
1155 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000)); | |
1156 CheckPipeline2(1024, 768, 512, 2000, "1024x768 -> 512x2000 bilinear\n"); | |
1157 | |
1158 // X upscaled, Y scaled 1/2, one bilinear pass | |
1159 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000)); | |
1160 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384)); | |
1161 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 2000x384 bilinear\n"); | |
1162 | |
1163 // X scaled 1/2, Y scaled 1/2, one bilinear pass | |
1164 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512)); | |
1165 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384)); | |
1166 CheckPipeline2(1024, 768, 512, 384, "1024x768 -> 512x384 bilinear\n"); | |
1167 | |
1168 // X scaled 1/2, Y scaled to 60%, one bilinear2 pass. | |
1169 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50)); | |
1170 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1171 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1172 CheckPipeline2(100, 100, 50, 60, "100x100 -> 50x60 bilinear2 Y\n"); | |
1173 | |
1174 // X scaled to 60%, Y scaled 1/2, one bilinear2 pass. | |
1175 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120)); | |
1176 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60)); | |
1177 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 50)); | |
1178 CheckPipeline2(100, 100, 60, 50, "100x100 -> 60x50 bilinear2 X\n"); | |
1179 | |
1180 // X scaled to 60%, Y scaled 60%, one bilinear2x2 pass. | |
1181 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120)); | |
1182 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60)); | |
1183 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1184 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1185 CheckPipeline2(100, 100, 60, 60, "100x100 -> 60x60 bilinear2x2\n"); | |
1186 | |
1187 // X scaled to 40%, Y scaled 40%, two bilinear3 passes. | |
1188 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40)); | |
1189 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40)); | |
1190 CheckPipeline2(100, 100, 40, 40, | |
1191 "100x100 -> 100x40 bilinear3 Y\n" | |
1192 "100x40 -> 40x40 bilinear3 X\n"); | |
1193 | |
1194 // X scaled to 60%, Y scaled 40% | |
1195 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120)); | |
1196 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60)); | |
1197 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40)); | |
1198 CheckPipeline2(100, 100, 60, 40, | |
1199 "100x100 -> 100x40 bilinear3 Y\n" | |
1200 "100x40 -> 60x40 bilinear2 X\n"); | |
1201 | |
1202 // X scaled to 40%, Y scaled 60% | |
1203 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40)); | |
1204 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1205 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1206 CheckPipeline2(100, 100, 40, 60, | |
1207 "100x100 -> 100x60 bilinear2 Y\n" | |
1208 "100x60 -> 40x60 bilinear3 X\n"); | |
1209 | |
1210 // X scaled to 30%, Y scaled 30% | |
1211 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120)); | |
1212 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60)); | |
1213 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 30)); | |
1214 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1215 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1216 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30)); | |
1217 CheckPipeline2(100, 100, 30, 30, | |
1218 "100x100 -> 100x30 bilinear4 Y\n" | |
1219 "100x30 -> 30x30 bilinear4 X\n"); | |
1220 | |
1221 // X scaled to 50%, Y scaled 30% | |
1222 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50)); | |
1223 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1224 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1225 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30)); | |
1226 CheckPipeline2(100, 100, 50, 30, "100x100 -> 50x30 bilinear4 Y\n"); | |
1227 | |
1228 // X scaled to 150%, Y scaled 30% | |
1229 // Note that we avoid combinding X and Y passes | |
1230 // as that would probably be LESS efficient here. | |
1231 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 150)); | |
1232 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1233 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1234 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30)); | |
1235 CheckPipeline2(100, 100, 150, 30, | |
1236 "100x100 -> 100x30 bilinear4 Y\n" | |
1237 "100x30 -> 150x30 bilinear\n"); | |
1238 | |
1239 // X scaled to 1%, Y scaled 1% | |
1240 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 128)); | |
1241 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 64)); | |
1242 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 32)); | |
1243 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 16)); | |
1244 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 8)); | |
1245 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 4)); | |
1246 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 2)); | |
1247 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 1)); | |
1248 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 128)); | |
1249 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 64)); | |
1250 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 32)); | |
1251 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 16)); | |
1252 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 8)); | |
1253 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 4)); | |
1254 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 2)); | |
1255 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 1)); | |
1256 CheckPipeline2(100, 100, 1, 1, | |
1257 "100x100 -> 100x32 bilinear4 Y\n" | |
1258 "100x32 -> 100x4 bilinear4 Y\n" | |
1259 "100x4 -> 64x1 bilinear2x2\n" | |
1260 "64x1 -> 8x1 bilinear4 X\n" | |
1261 "8x1 -> 1x1 bilinear4 X\n"); | |
1262 } | |
1263 | |
1264 std::unique_ptr<gpu::GLInProcessContext> context_; | |
1265 gpu::gles2::GLES2Interface* gl_; | |
1266 std::unique_ptr<content::GLHelper> helper_; | |
1267 std::unique_ptr<content::GLHelperScaling> helper_scaling_; | |
1268 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; | |
1269 }; | |
1270 | |
1271 class GLHelperPixelTest : public GLHelperTest { | |
1272 private: | |
1273 gfx::DisableNullDrawGLBindings enable_pixel_output_; | |
1274 }; | |
1275 | |
1276 TEST_F(GLHelperTest, RGBASyncReadbackTest) { | |
1277 const int kTestSize = 64; | |
1278 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize), | |
1279 kRGBA_8888_SkColorType, false); | |
1280 EXPECT_EQ(result, true); | |
1281 } | |
1282 | |
1283 TEST_F(GLHelperTest, BGRASyncReadbackTest) { | |
1284 const int kTestSize = 64; | |
1285 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize), | |
1286 kBGRA_8888_SkColorType, false); | |
1287 EXPECT_EQ(result, true); | |
1288 } | |
1289 | |
1290 TEST_F(GLHelperTest, RGB565SyncReadbackTest) { | |
1291 const int kTestSize = 64; | |
1292 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize), | |
1293 kRGB_565_SkColorType, false); | |
1294 EXPECT_EQ(result, true); | |
1295 } | |
1296 | |
1297 TEST_F(GLHelperTest, RGBAASyncReadbackTest) { | |
1298 const int kTestSize = 64; | |
1299 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize), | |
1300 kRGBA_8888_SkColorType, true); | |
1301 EXPECT_EQ(result, true); | |
1302 } | |
1303 | |
1304 TEST_F(GLHelperTest, BGRAASyncReadbackTest) { | |
1305 const int kTestSize = 64; | |
1306 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize), | |
1307 kBGRA_8888_SkColorType, true); | |
1308 EXPECT_EQ(result, true); | |
1309 } | |
1310 | |
1311 TEST_F(GLHelperTest, RGB565ASyncReadbackTest) { | |
1312 const int kTestSize = 64; | |
1313 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize), | |
1314 kRGB_565_SkColorType, true); | |
1315 EXPECT_EQ(result, true); | |
1316 } | |
1317 | |
1318 int kRGBReadBackSizes[] = {3, 6, 16}; | |
1319 | |
1320 class GLHelperPixelReadbackTest | |
1321 : public GLHelperPixelTest, | |
1322 public ::testing::WithParamInterface<std::tr1::tuple<unsigned int, | |
1323 unsigned int, | |
1324 unsigned int, | |
1325 unsigned int, | |
1326 unsigned int>> {}; | |
1327 | |
1328 // Per pixel tests, all sizes are small so that we can print | |
1329 // out the generated bitmaps. | |
1330 TEST_P(GLHelperPixelReadbackTest, ScaleTest) { | |
1331 unsigned int q_index = std::tr1::get<0>(GetParam()); | |
1332 unsigned int x = std::tr1::get<1>(GetParam()); | |
1333 unsigned int y = std::tr1::get<2>(GetParam()); | |
1334 unsigned int dst_x = std::tr1::get<3>(GetParam()); | |
1335 unsigned int dst_y = std::tr1::get<4>(GetParam()); | |
1336 | |
1337 for (int flip = 0; flip <= 1; flip++) { | |
1338 for (int pattern = 0; pattern < 3; pattern++) { | |
1339 TestScale(kRGBReadBackSizes[x], kRGBReadBackSizes[y], | |
1340 kRGBReadBackSizes[dst_x], kRGBReadBackSizes[dst_y], pattern, | |
1341 q_index, flip == 1); | |
1342 if (HasFailure()) { | |
1343 return; | |
1344 } | |
1345 } | |
1346 } | |
1347 } | |
1348 | |
1349 // Per pixel tests, all sizes are small so that we can print | |
1350 // out the generated bitmaps. | |
1351 TEST_P(GLHelperPixelReadbackTest, CropScaleReadbackAndCleanTextureTest) { | |
1352 unsigned int q_index = std::tr1::get<0>(GetParam()); | |
1353 unsigned int x = std::tr1::get<1>(GetParam()); | |
1354 unsigned int y = std::tr1::get<2>(GetParam()); | |
1355 unsigned int dst_x = std::tr1::get<3>(GetParam()); | |
1356 unsigned int dst_y = std::tr1::get<4>(GetParam()); | |
1357 | |
1358 const SkColorType kColorTypes[] = { | |
1359 kAlpha_8_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType}; | |
1360 for (size_t color_type = 0; color_type < arraysize(kColorTypes); | |
1361 color_type++) { | |
1362 for (int pattern = 0; pattern < 3; pattern++) { | |
1363 TestCropScaleReadbackAndCleanTexture( | |
1364 kRGBReadBackSizes[x], kRGBReadBackSizes[y], kRGBReadBackSizes[dst_x], | |
1365 kRGBReadBackSizes[dst_y], pattern, kColorTypes[color_type], false, | |
1366 q_index); | |
1367 if (HasFailure()) | |
1368 return; | |
1369 } | |
1370 } | |
1371 } | |
1372 | |
1373 INSTANTIATE_TEST_CASE_P( | |
1374 , | |
1375 GLHelperPixelReadbackTest, | |
1376 ::testing::Combine( | |
1377 ::testing::Range<unsigned int>(0, arraysize(kQualities)), | |
1378 ::testing::Range<unsigned int>(0, arraysize(kRGBReadBackSizes)), | |
1379 ::testing::Range<unsigned int>(0, arraysize(kRGBReadBackSizes)), | |
1380 ::testing::Range<unsigned int>(0, arraysize(kRGBReadBackSizes)), | |
1381 ::testing::Range<unsigned int>(0, arraysize(kRGBReadBackSizes)))); | |
1382 | |
1383 // Validate that all scaling generates valid pipelines. | |
1384 TEST_F(GLHelperTest, ValidateScalerPipelines) { | |
1385 int sizes[] = {7, 99, 128, 256, 512, 719, 720, 721, 1920, 2011, 3217, 4096}; | |
1386 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
1387 for (size_t x = 0; x < arraysize(sizes); x++) { | |
1388 for (size_t y = 0; y < arraysize(sizes); y++) { | |
1389 for (size_t dst_x = 0; dst_x < arraysize(sizes); dst_x++) { | |
1390 for (size_t dst_y = 0; dst_y < arraysize(sizes); dst_y++) { | |
1391 TestScalerPipeline(q, sizes[x], sizes[y], sizes[dst_x], | |
1392 sizes[dst_y]); | |
1393 if (HasFailure()) { | |
1394 return; | |
1395 } | |
1396 } | |
1397 } | |
1398 } | |
1399 } | |
1400 } | |
1401 } | |
1402 | |
1403 // Make sure we don't create overly complicated pipelines | |
1404 // for a few common use cases. | |
1405 TEST_F(GLHelperTest, CheckSpecificPipelines) { | |
1406 // Upscale should be single pass. | |
1407 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD, 1024, 700, 1280, 720, | |
1408 "1024x700 -> 1280x720 bilinear\n"); | |
1409 // Slight downscale should use BILINEAR2X2. | |
1410 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD, 1280, 720, 1024, 700, | |
1411 "1280x720 -> 1024x700 bilinear2x2\n"); | |
1412 // Most common tab capture pipeline on the Pixel. | |
1413 // Should be using two BILINEAR3 passes. | |
1414 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD, 2560, 1476, 1249, 720, | |
1415 "2560x1476 -> 2560x720 bilinear3 Y\n" | |
1416 "2560x720 -> 1249x720 bilinear3 X\n"); | |
1417 } | |
1418 | |
1419 TEST_F(GLHelperTest, ScalerOpTest) { | |
1420 for (int allow3 = 0; allow3 <= 1; allow3++) { | |
1421 for (int dst = 1; dst < 2049; dst += 1 + (dst >> 3)) { | |
1422 for (int src = 1; src < 2049; src++) { | |
1423 TestAddOps(src, dst, allow3 == 1, (src & 1) == 1); | |
1424 if (HasFailure()) { | |
1425 LOG(ERROR) << "Failed for src=" << src << " dst=" << dst | |
1426 << " allow3=" << allow3; | |
1427 return; | |
1428 } | |
1429 } | |
1430 } | |
1431 } | |
1432 } | |
1433 | |
1434 TEST_F(GLHelperTest, CheckOptimizations) { | |
1435 // Test in baseclass since it is friends with GLHelperScaling | |
1436 CheckOptimizationsTest(); | |
1437 } | |
1438 | |
1439 } // namespace content | |
OLD | NEW |