[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / content / common / gpu / client / gl_helper_unittest.cc
blob9a4f2395af8fb5f28833655778ff0bdb025ffab7
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.
5 #include <stdio.h>
6 #include <cmath>
7 #include <string>
8 #include <vector>
10 #include <GLES2/gl2.h>
11 #include <GLES2/gl2ext.h>
12 #include <GLES2/gl2extchromium.h>
14 #include "base/at_exit.h"
15 #include "base/bind.h"
16 #include "base/command_line.h"
17 #include "base/debug/trace_event.h"
18 #include "base/file_util.h"
19 #include "base/json/json_reader.h"
20 #include "base/message_loop/message_loop.h"
21 #include "base/run_loop.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/synchronization/waitable_event.h"
24 #include "base/time/time.h"
25 #include "content/common/gpu/client/gl_helper.h"
26 #include "content/common/gpu/client/gl_helper_readback_support.h"
27 #include "content/common/gpu/client/gl_helper_scaling.h"
28 #include "content/public/test/unittest_test_suite.h"
29 #include "content/test/content_test_suite.h"
30 #include "media/base/video_frame.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "third_party/skia/include/core/SkBitmap.h"
33 #include "third_party/skia/include/core/SkTypes.h"
34 #include "ui/gl/gl_implementation.h"
35 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
37 #if defined(OS_MACOSX)
38 #include "base/mac/scoped_nsautorelease_pool.h"
39 #endif
41 namespace content {
43 using blink::WebGLId;
44 using blink::WebGraphicsContext3D;
45 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
47 content::GLHelper::ScalerQuality kQualities[] = {
48 content::GLHelper::SCALER_QUALITY_BEST,
49 content::GLHelper::SCALER_QUALITY_GOOD,
50 content::GLHelper::SCALER_QUALITY_FAST, };
52 const char* kQualityNames[] = {"best", "good", "fast", };
54 class GLHelperTest : public testing::Test {
55 protected:
56 virtual void SetUp() {
57 WebGraphicsContext3D::Attributes attributes;
58 bool lose_context_when_out_of_memory = false;
59 context_ =
60 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
61 attributes, lose_context_when_out_of_memory);
62 context_->makeContextCurrent();
63 context_support_ = context_->GetContextSupport();
64 helper_.reset(
65 new content::GLHelper(context_->GetGLInterface(), context_support_));
66 helper_scaling_.reset(new content::GLHelperScaling(
67 context_->GetGLInterface(), helper_.get()));
70 virtual void TearDown() {
71 helper_scaling_.reset(NULL);
72 helper_.reset(NULL);
73 context_.reset(NULL);
76 void StartTracing(const std::string& filter) {
77 base::debug::TraceLog::GetInstance()->SetEnabled(
78 base::debug::CategoryFilter(filter),
79 base::debug::TraceLog::RECORDING_MODE,
80 base::debug::TraceLog::RECORD_UNTIL_FULL);
83 static void TraceDataCB(
84 const base::Callback<void()>& callback,
85 std::string* output,
86 const scoped_refptr<base::RefCountedString>& json_events_str,
87 bool has_more_events) {
88 if (output->size() > 1) {
89 output->append(",");
91 output->append(json_events_str->data());
92 if (!has_more_events) {
93 callback.Run();
97 // End tracing, return tracing data in a simple map
98 // of event name->counts.
99 void EndTracing(std::map<std::string, int>* event_counts) {
100 std::string json_data = "[";
101 base::debug::TraceLog::GetInstance()->SetDisabled();
102 base::RunLoop run_loop;
103 base::debug::TraceLog::GetInstance()->Flush(
104 base::Bind(&GLHelperTest::TraceDataCB,
105 run_loop.QuitClosure(),
106 base::Unretained(&json_data)));
107 run_loop.Run();
108 json_data.append("]");
110 scoped_ptr<base::Value> trace_data(base::JSONReader::Read(json_data));
111 base::ListValue* list;
112 CHECK(trace_data->GetAsList(&list));
113 for (size_t i = 0; i < list->GetSize(); i++) {
114 base::Value* item = NULL;
115 if (list->Get(i, &item)) {
116 base::DictionaryValue* dict;
117 CHECK(item->GetAsDictionary(&dict));
118 std::string name;
119 CHECK(dict->GetString("name", &name));
120 (*event_counts)[name]++;
121 VLOG(1) << "trace name: " << name;
126 // Bicubic filter kernel function.
127 static float Bicubic(float x) {
128 const float a = -0.5;
129 x = std::abs(x);
130 float x2 = x * x;
131 float x3 = x2 * x;
132 if (x <= 1) {
133 return (a + 2) * x3 - (a + 3) * x2 + 1;
134 } else if (x < 2) {
135 return a * x3 - 5 * a * x2 + 8 * a * x - 4 * a;
136 } else {
137 return 0.0f;
141 // Look up a single R/G/B/A value.
142 // Clamp x/y.
143 int Channel(SkBitmap* pixels, int x, int y, int c) {
144 uint32* data =
145 pixels->getAddr32(std::max(0, std::min(x, pixels->width() - 1)),
146 std::max(0, std::min(y, pixels->height() - 1)));
147 return (*data) >> (c * 8) & 0xff;
150 // Set a single R/G/B/A value.
151 void SetChannel(SkBitmap* pixels, int x, int y, int c, int v) {
152 DCHECK_GE(x, 0);
153 DCHECK_GE(y, 0);
154 DCHECK_LT(x, pixels->width());
155 DCHECK_LT(y, pixels->height());
156 uint32* data = pixels->getAddr32(x, y);
157 v = std::max(0, std::min(v, 255));
158 *data = (*data & ~(0xffu << (c * 8))) | (v << (c * 8));
161 // Print all the R, G, B or A values from an SkBitmap in a
162 // human-readable format.
163 void PrintChannel(SkBitmap* pixels, int c) {
164 for (int y = 0; y < pixels->height(); y++) {
165 std::string formatted;
166 for (int x = 0; x < pixels->width(); x++) {
167 formatted.append(base::StringPrintf("%3d, ", Channel(pixels, x, y, c)));
169 LOG(ERROR) << formatted;
173 // Print out the individual steps of a scaler pipeline.
174 std::string PrintStages(
175 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages) {
176 std::string ret;
177 for (size_t i = 0; i < scaler_stages.size(); i++) {
178 ret.append(base::StringPrintf("%dx%d -> %dx%d ",
179 scaler_stages[i].src_size.width(),
180 scaler_stages[i].src_size.height(),
181 scaler_stages[i].dst_size.width(),
182 scaler_stages[i].dst_size.height()));
183 bool xy_matters = false;
184 switch (scaler_stages[i].shader) {
185 case GLHelperScaling::SHADER_BILINEAR:
186 ret.append("bilinear");
187 break;
188 case GLHelperScaling::SHADER_BILINEAR2:
189 ret.append("bilinear2");
190 xy_matters = true;
191 break;
192 case GLHelperScaling::SHADER_BILINEAR3:
193 ret.append("bilinear3");
194 xy_matters = true;
195 break;
196 case GLHelperScaling::SHADER_BILINEAR4:
197 ret.append("bilinear4");
198 xy_matters = true;
199 break;
200 case GLHelperScaling::SHADER_BILINEAR2X2:
201 ret.append("bilinear2x2");
202 break;
203 case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
204 ret.append("bicubic upscale");
205 xy_matters = true;
206 break;
207 case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
208 ret.append("bicubic 1/2");
209 xy_matters = true;
210 break;
211 case GLHelperScaling::SHADER_PLANAR:
212 ret.append("planar");
213 break;
214 case GLHelperScaling::SHADER_YUV_MRT_PASS1:
215 ret.append("rgb2yuv pass 1");
216 break;
217 case GLHelperScaling::SHADER_YUV_MRT_PASS2:
218 ret.append("rgb2yuv pass 2");
219 break;
222 if (xy_matters) {
223 if (scaler_stages[i].scale_x) {
224 ret.append(" X");
225 } else {
226 ret.append(" Y");
229 ret.append("\n");
231 return ret;
234 bool CheckScale(double scale, int samples, bool already_scaled) {
235 // 1:1 is valid if there is one sample.
236 if (samples == 1 && scale == 1.0) {
237 return true;
239 // Is it an exact down-scale (50%, 25%, etc.?)
240 if (scale == 2.0 * samples) {
241 return true;
243 // Upscales, only valid if we haven't already scaled in this dimension.
244 if (!already_scaled) {
245 // Is it a valid bilinear upscale?
246 if (samples == 1 && scale <= 1.0) {
247 return true;
249 // Multi-sample upscale-downscale combination?
250 if (scale > samples / 2.0 && scale < samples) {
251 return true;
254 return false;
257 // Make sure that the stages of the scaler pipeline are sane.
258 void ValidateScalerStages(
259 content::GLHelper::ScalerQuality quality,
260 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages,
261 const gfx::Size& dst_size,
262 const std::string& message) {
263 bool previous_error = HasFailure();
264 // First, check that the input size for each stage is equal to
265 // the output size of the previous stage.
266 for (size_t i = 1; i < scaler_stages.size(); i++) {
267 EXPECT_EQ(scaler_stages[i - 1].dst_size.width(),
268 scaler_stages[i].src_size.width());
269 EXPECT_EQ(scaler_stages[i - 1].dst_size.height(),
270 scaler_stages[i].src_size.height());
271 EXPECT_EQ(scaler_stages[i].src_subrect.x(), 0);
272 EXPECT_EQ(scaler_stages[i].src_subrect.y(), 0);
273 EXPECT_EQ(scaler_stages[i].src_subrect.width(),
274 scaler_stages[i].src_size.width());
275 EXPECT_EQ(scaler_stages[i].src_subrect.height(),
276 scaler_stages[i].src_size.height());
279 // Check the output size matches the destination of the last stage
280 EXPECT_EQ(scaler_stages[scaler_stages.size() - 1].dst_size.width(),
281 dst_size.width());
282 EXPECT_EQ(scaler_stages[scaler_stages.size() - 1].dst_size.height(),
283 dst_size.height());
285 // Used to verify that up-scales are not attempted after some
286 // other scale.
287 bool scaled_x = false;
288 bool scaled_y = false;
290 for (size_t i = 0; i < scaler_stages.size(); i++) {
291 // Note: 2.0 means scaling down by 50%
292 double x_scale =
293 static_cast<double>(scaler_stages[i].src_subrect.width()) /
294 static_cast<double>(scaler_stages[i].dst_size.width());
295 double y_scale =
296 static_cast<double>(scaler_stages[i].src_subrect.height()) /
297 static_cast<double>(scaler_stages[i].dst_size.height());
299 int x_samples = 0;
300 int y_samples = 0;
302 // Codify valid scale operations.
303 switch (scaler_stages[i].shader) {
304 case GLHelperScaling::SHADER_PLANAR:
305 case GLHelperScaling::SHADER_YUV_MRT_PASS1:
306 case GLHelperScaling::SHADER_YUV_MRT_PASS2:
307 EXPECT_TRUE(false) << "Invalid shader.";
308 break;
310 case GLHelperScaling::SHADER_BILINEAR:
311 if (quality != content::GLHelper::SCALER_QUALITY_FAST) {
312 x_samples = 1;
313 y_samples = 1;
315 break;
316 case GLHelperScaling::SHADER_BILINEAR2:
317 x_samples = 2;
318 y_samples = 1;
319 break;
320 case GLHelperScaling::SHADER_BILINEAR3:
321 x_samples = 3;
322 y_samples = 1;
323 break;
324 case GLHelperScaling::SHADER_BILINEAR4:
325 x_samples = 4;
326 y_samples = 1;
327 break;
328 case GLHelperScaling::SHADER_BILINEAR2X2:
329 x_samples = 2;
330 y_samples = 2;
331 break;
332 case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
333 if (scaler_stages[i].scale_x) {
334 EXPECT_LT(x_scale, 1.0);
335 EXPECT_EQ(y_scale, 1.0);
336 } else {
337 EXPECT_EQ(x_scale, 1.0);
338 EXPECT_LT(y_scale, 1.0);
340 break;
341 case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
342 if (scaler_stages[i].scale_x) {
343 EXPECT_EQ(x_scale, 2.0);
344 EXPECT_EQ(y_scale, 1.0);
345 } else {
346 EXPECT_EQ(x_scale, 1.0);
347 EXPECT_EQ(y_scale, 2.0);
349 break;
352 if (!scaler_stages[i].scale_x) {
353 std::swap(x_samples, y_samples);
356 if (x_samples) {
357 EXPECT_TRUE(CheckScale(x_scale, x_samples, scaled_x))
358 << "x_scale = " << x_scale;
360 if (y_samples) {
361 EXPECT_TRUE(CheckScale(y_scale, y_samples, scaled_y))
362 << "y_scale = " << y_scale;
365 if (x_scale != 1.0) {
366 scaled_x = true;
368 if (y_scale != 1.0) {
369 scaled_y = true;
373 if (HasFailure() && !previous_error) {
374 LOG(ERROR) << "Invalid scaler stages: " << message;
375 LOG(ERROR) << "Scaler stages:";
376 LOG(ERROR) << PrintStages(scaler_stages);
380 // Compare two bitmaps, make sure that each component of each pixel
381 // is no more than |maxdiff| apart. If they are not similar enough,
382 // prints out |truth|, |other|, |source|, |scaler_stages| and |message|.
383 void Compare(SkBitmap* truth,
384 SkBitmap* other,
385 int maxdiff,
386 SkBitmap* source,
387 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages,
388 std::string message) {
389 EXPECT_EQ(truth->width(), other->width());
390 EXPECT_EQ(truth->height(), other->height());
391 for (int x = 0; x < truth->width(); x++) {
392 for (int y = 0; y < truth->height(); y++) {
393 for (int c = 0; c < 4; c++) {
394 int a = Channel(truth, x, y, c);
395 int b = Channel(other, x, y, c);
396 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " c=" << c
397 << " " << message;
398 if (std::abs(a - b) > maxdiff) {
399 LOG(ERROR) << "-------expected--------";
400 PrintChannel(truth, c);
401 LOG(ERROR) << "-------actual--------";
402 PrintChannel(other, c);
403 if (source) {
404 LOG(ERROR) << "-------before scaling--------";
405 PrintChannel(source, c);
407 LOG(ERROR) << "-----Scaler stages------";
408 LOG(ERROR) << PrintStages(scaler_stages);
409 return;
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;
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);
435 // Very slow bicubic / bilinear scaler for reference.
436 void ScaleSlow(SkBitmap* input,
437 SkBitmap* output,
438 content::GLHelper::ScalerQuality quality) {
439 float xscale = static_cast<float>(input->width()) / output->width();
440 float yscale = static_cast<float>(input->height()) / output->height();
441 float clamped_xscale = xscale < 1.0 ? 1.0 : 1.0 / xscale;
442 float clamped_yscale = yscale < 1.0 ? 1.0 : 1.0 / yscale;
443 for (int dst_y = 0; dst_y < output->height(); dst_y++) {
444 for (int dst_x = 0; dst_x < output->width(); dst_x++) {
445 for (int channel = 0; channel < 4; channel++) {
446 float dst_x_in_src = (dst_x + 0.5f) * xscale;
447 float dst_y_in_src = (dst_y + 0.5f) * yscale;
449 float value = 0.0f;
450 float sum = 0.0f;
451 switch (quality) {
452 case content::GLHelper::SCALER_QUALITY_BEST:
453 for (int src_y = -10; src_y < input->height() + 10; ++src_y) {
454 float coeff_y =
455 Bicubic((src_y + 0.5f - dst_y_in_src) * clamped_yscale);
456 if (coeff_y == 0.0f) {
457 continue;
459 for (int src_x = -10; src_x < input->width() + 10; ++src_x) {
460 float coeff =
461 coeff_y *
462 Bicubic((src_x + 0.5f - dst_x_in_src) * clamped_xscale);
463 if (coeff == 0.0f) {
464 continue;
466 sum += coeff;
467 float c = ChannelAsFloat(input, src_x, src_y, channel);
468 value += c * coeff;
471 break;
473 case content::GLHelper::SCALER_QUALITY_GOOD: {
474 int xshift = 0, yshift = 0;
475 while ((output->width() << xshift) < input->width()) {
476 xshift++;
478 while ((output->height() << yshift) < input->height()) {
479 yshift++;
481 int xmag = 1 << xshift;
482 int ymag = 1 << yshift;
483 if (xmag == 4 && output->width() * 3 >= input->width()) {
484 xmag = 3;
486 if (ymag == 4 && output->height() * 3 >= input->height()) {
487 ymag = 3;
489 for (int x = 0; x < xmag; x++) {
490 for (int y = 0; y < ymag; y++) {
491 value += Bilinear(input,
492 (dst_x * xmag + x + 0.5) * xscale / xmag,
493 (dst_y * ymag + y + 0.5) * yscale / ymag,
494 channel);
495 sum += 1.0;
498 break;
501 case content::GLHelper::SCALER_QUALITY_FAST:
502 value = Bilinear(input, dst_x_in_src, dst_y_in_src, channel);
503 sum = 1.0;
505 value /= sum;
506 SetChannel(output,
507 dst_x,
508 dst_y,
509 channel,
510 static_cast<int>(value * 255.0f + 0.5f));
516 void FlipSKBitmap(SkBitmap* bitmap) {
517 int top_line = 0;
518 int bottom_line = bitmap->height() - 1;
519 while (top_line < bottom_line) {
520 for (int x = 0; x < bitmap->width(); x++) {
521 std::swap(*bitmap->getAddr32(x, top_line),
522 *bitmap->getAddr32(x, bottom_line));
524 top_line++;
525 bottom_line--;
529 // gl_helper scales recursively, so we'll need to do that
530 // in the reference implementation too.
531 void ScaleSlowRecursive(SkBitmap* input,
532 SkBitmap* output,
533 content::GLHelper::ScalerQuality quality) {
534 if (quality == content::GLHelper::SCALER_QUALITY_FAST ||
535 quality == content::GLHelper::SCALER_QUALITY_GOOD) {
536 ScaleSlow(input, output, quality);
537 return;
540 float xscale = static_cast<float>(output->width()) / input->width();
542 // This corresponds to all the operations we can do directly.
543 float yscale = static_cast<float>(output->height()) / input->height();
544 if ((xscale == 1.0f && yscale == 1.0f) ||
545 (xscale == 0.5f && yscale == 1.0f) ||
546 (xscale == 1.0f && yscale == 0.5f) ||
547 (xscale >= 1.0f && yscale == 1.0f) ||
548 (xscale == 1.0f && yscale >= 1.0f)) {
549 ScaleSlow(input, output, quality);
550 return;
553 // Now we break the problem down into smaller pieces, using the
554 // operations available.
555 int xtmp = input->width();
556 int ytmp = input->height();
558 if (output->height() != input->height()) {
559 ytmp = output->height();
560 while (ytmp < input->height() && ytmp * 2 != input->height()) {
561 ytmp += ytmp;
563 } else {
564 xtmp = output->width();
565 while (xtmp < input->width() && xtmp * 2 != input->width()) {
566 xtmp += xtmp;
570 SkBitmap tmp;
571 tmp.allocN32Pixels(xtmp, ytmp);
573 ScaleSlowRecursive(input, &tmp, quality);
574 ScaleSlowRecursive(&tmp, output, quality);
577 // Scaling test: Create a test image, scale it using GLHelperScaling
578 // and a reference implementation and compare the results.
579 void TestScale(int xsize,
580 int ysize,
581 int scaled_xsize,
582 int scaled_ysize,
583 int test_pattern,
584 size_t quality,
585 bool flip) {
586 WebGLId src_texture = context_->createTexture();
587 WebGLId framebuffer = context_->createFramebuffer();
588 SkBitmap input_pixels;
589 input_pixels.allocN32Pixels(xsize, ysize);
591 for (int x = 0; x < xsize; ++x) {
592 for (int y = 0; y < ysize; ++y) {
593 switch (test_pattern) {
594 case 0: // Smooth test pattern
595 SetChannel(&input_pixels, x, y, 0, x * 10);
596 SetChannel(&input_pixels, x, y, 1, y * 10);
597 SetChannel(&input_pixels, x, y, 2, (x + y) * 10);
598 SetChannel(&input_pixels, x, y, 3, 255);
599 break;
600 case 1: // Small blocks
601 SetChannel(&input_pixels, x, y, 0, x & 1 ? 255 : 0);
602 SetChannel(&input_pixels, x, y, 1, y & 1 ? 255 : 0);
603 SetChannel(&input_pixels, x, y, 2, (x + y) & 1 ? 255 : 0);
604 SetChannel(&input_pixels, x, y, 3, 255);
605 break;
606 case 2: // Medium blocks
607 SetChannel(&input_pixels, x, y, 0, 10 + x / 2 * 50);
608 SetChannel(&input_pixels, x, y, 1, 10 + y / 3 * 50);
609 SetChannel(&input_pixels, x, y, 2, (x + y) / 5 * 50 + 5);
610 SetChannel(&input_pixels, x, y, 3, 255);
611 break;
616 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
617 context_->bindTexture(GL_TEXTURE_2D, src_texture);
618 context_->texImage2D(GL_TEXTURE_2D,
620 GL_RGBA,
621 xsize,
622 ysize,
624 GL_RGBA,
625 GL_UNSIGNED_BYTE,
626 input_pixels.getPixels());
628 std::string message = base::StringPrintf(
629 "input size: %dx%d "
630 "output size: %dx%d "
631 "pattern: %d quality: %s",
632 xsize,
633 ysize,
634 scaled_xsize,
635 scaled_ysize,
636 test_pattern,
637 kQualityNames[quality]);
639 std::vector<GLHelperScaling::ScalerStage> stages;
640 helper_scaling_->ComputeScalerStages(kQualities[quality],
641 gfx::Size(xsize, ysize),
642 gfx::Rect(0, 0, xsize, ysize),
643 gfx::Size(scaled_xsize, scaled_ysize),
644 flip,
645 false,
646 &stages);
647 ValidateScalerStages(kQualities[quality],
648 stages,
649 gfx::Size(scaled_xsize, scaled_ysize),
650 message);
652 WebGLId dst_texture =
653 helper_->CopyAndScaleTexture(src_texture,
654 gfx::Size(xsize, ysize),
655 gfx::Size(scaled_xsize, scaled_ysize),
656 flip,
657 kQualities[quality]);
659 SkBitmap output_pixels;
660 output_pixels.allocN32Pixels(scaled_xsize, scaled_ysize);
662 helper_->ReadbackTextureSync(
663 dst_texture,
664 gfx::Rect(0, 0, scaled_xsize, scaled_ysize),
665 static_cast<unsigned char*>(output_pixels.getPixels()),
666 kN32_SkColorType);
667 if (flip) {
668 // Flip the pixels back.
669 FlipSKBitmap(&output_pixels);
671 if (xsize == scaled_xsize && ysize == scaled_ysize) {
672 Compare(&input_pixels,
673 &output_pixels,
675 NULL,
676 stages,
677 message + " comparing against input");
679 SkBitmap truth_pixels;
680 truth_pixels.allocN32Pixels(scaled_xsize, scaled_ysize);
682 ScaleSlowRecursive(&input_pixels, &truth_pixels, kQualities[quality]);
683 Compare(&truth_pixels,
684 &output_pixels,
686 &input_pixels,
687 stages,
688 message + " comparing against scaled");
690 context_->deleteTexture(src_texture);
691 context_->deleteTexture(dst_texture);
692 context_->deleteFramebuffer(framebuffer);
695 // Create a scaling pipeline and check that it is made up of
696 // valid scaling operations.
697 void TestScalerPipeline(size_t quality,
698 int xsize,
699 int ysize,
700 int dst_xsize,
701 int dst_ysize) {
702 std::vector<GLHelperScaling::ScalerStage> stages;
703 helper_scaling_->ComputeScalerStages(kQualities[quality],
704 gfx::Size(xsize, ysize),
705 gfx::Rect(0, 0, xsize, ysize),
706 gfx::Size(dst_xsize, dst_ysize),
707 false,
708 false,
709 &stages);
710 ValidateScalerStages(kQualities[quality],
711 stages,
712 gfx::Size(dst_xsize, dst_ysize),
713 base::StringPrintf(
714 "input size: %dx%d "
715 "output size: %dx%d "
716 "quality: %s",
717 xsize,
718 ysize,
719 dst_xsize,
720 dst_ysize,
721 kQualityNames[quality]));
724 // Create a scaling pipeline and make sure that the steps
725 // are exactly the steps we expect.
726 void CheckPipeline(content::GLHelper::ScalerQuality quality,
727 int xsize,
728 int ysize,
729 int dst_xsize,
730 int dst_ysize,
731 const std::string& description) {
732 std::vector<GLHelperScaling::ScalerStage> stages;
733 helper_scaling_->ComputeScalerStages(quality,
734 gfx::Size(xsize, ysize),
735 gfx::Rect(0, 0, xsize, ysize),
736 gfx::Size(dst_xsize, dst_ysize),
737 false,
738 false,
739 &stages);
740 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD,
741 stages,
742 gfx::Size(dst_xsize, dst_ysize),
743 "");
744 EXPECT_EQ(PrintStages(stages), description);
747 // Note: Left/Right means Top/Bottom when used for Y dimension.
748 enum Margin {
749 MarginLeft,
750 MarginMiddle,
751 MarginRight,
752 MarginInvalid,
755 static Margin NextMargin(Margin m) {
756 switch (m) {
757 case MarginLeft:
758 return MarginMiddle;
759 case MarginMiddle:
760 return MarginRight;
761 case MarginRight:
762 return MarginInvalid;
763 default:
764 return MarginInvalid;
768 int compute_margin(int insize, int outsize, Margin m) {
769 int available = outsize - insize;
770 switch (m) {
771 default:
772 EXPECT_TRUE(false) << "This should not happen.";
773 return 0;
774 case MarginLeft:
775 return 0;
776 case MarginMiddle:
777 return (available / 2) & ~1;
778 case MarginRight:
779 return available;
783 // Convert 0.0 - 1.0 to 0 - 255
784 int float_to_byte(float v) {
785 int ret = static_cast<int>(floorf(v * 255.0f + 0.5f));
786 if (ret < 0) {
787 return 0;
789 if (ret > 255) {
790 return 255;
792 return ret;
795 static void callcallback(const base::Callback<void()>& callback,
796 bool result) {
797 callback.Run();
800 void PrintPlane(unsigned char* plane, int xsize, int stride, int ysize) {
801 for (int y = 0; y < ysize; y++) {
802 std::string formatted;
803 for (int x = 0; x < xsize; x++) {
804 formatted.append(base::StringPrintf("%3d, ", plane[y * stride + x]));
806 LOG(ERROR) << formatted << " (" << (plane + y * stride) << ")";
810 // Compare two planes make sure that each component of each pixel
811 // is no more than |maxdiff| apart.
812 void ComparePlane(unsigned char* truth,
813 unsigned char* other,
814 int maxdiff,
815 int xsize,
816 int stride,
817 int ysize,
818 SkBitmap* source,
819 std::string message) {
820 int truth_stride = stride;
821 for (int x = 0; x < xsize; x++) {
822 for (int y = 0; y < ysize; y++) {
823 int a = other[y * stride + x];
824 int b = truth[y * stride + x];
825 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " "
826 << message;
827 if (std::abs(a - b) > maxdiff) {
828 LOG(ERROR) << "-------expected--------";
829 PrintPlane(truth, xsize, truth_stride, ysize);
830 LOG(ERROR) << "-------actual--------";
831 PrintPlane(other, xsize, stride, ysize);
832 if (source) {
833 LOG(ERROR) << "-------before yuv conversion: red--------";
834 PrintChannel(source, 0);
835 LOG(ERROR) << "-------before yuv conversion: green------";
836 PrintChannel(source, 1);
837 LOG(ERROR) << "-------before yuv conversion: blue-------";
838 PrintChannel(source, 2);
840 return;
846 void DrawGridToBitmap(int w, int h,
847 SkColor background_color,
848 SkColor grid_color,
849 int grid_pitch,
850 int grid_width,
851 SkBitmap& bmp) {
852 ASSERT_GT(grid_pitch, 0);
853 ASSERT_GT(grid_width, 0);
854 ASSERT_NE(background_color, grid_color);
856 for (int y = 0; y < h; ++y) {
857 bool y_on_grid = ((y % grid_pitch) < grid_width);
859 for (int x = 0; x < w; ++x) {
860 bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width));
862 if (bmp.colorType() == kN32_SkColorType) {
863 *bmp.getAddr32(x, y) = (on_grid ? grid_color : background_color);
864 } else if (bmp.colorType() == kRGB_565_SkColorType) {
865 *bmp.getAddr16(x, y) = (on_grid ? grid_color : background_color);
871 void DrawCheckerToBitmap(int w, int h,
872 SkColor color1, SkColor color2,
873 int rect_w, int rect_h,
874 SkBitmap& bmp) {
875 ASSERT_GT(rect_w, 0);
876 ASSERT_GT(rect_h, 0);
877 ASSERT_NE(color1, color2);
879 for (int y = 0; y < h; ++y) {
880 bool y_bit = (((y / rect_h) & 0x1) == 0);
882 for (int x = 0; x < w; ++x) {
883 bool x_bit = (((x / rect_w) & 0x1) == 0);
885 bool use_color2 = (x_bit != y_bit); // xor
886 if (bmp.colorType() == kN32_SkColorType) {
887 *bmp.getAddr32(x, y) = (use_color2 ? color2 : color1);
888 } else if (bmp.colorType() == kRGB_565_SkColorType) {
889 *bmp.getAddr16(x, y) = (use_color2 ? color2 : color1);
895 bool ColorComponentsClose(SkColor component1,
896 SkColor component2,
897 SkColorType color_type) {
898 int c1 = static_cast<int>(component1);
899 int c2 = static_cast<int>(component2);
900 bool result = false;
901 switch (color_type) {
902 case kN32_SkColorType:
903 result = (std::abs(c1 - c2) == 0);
904 break;
905 case kRGB_565_SkColorType:
906 result = (std::abs(c1 - c2) <= 7);
907 break;
908 default:
909 break;
911 return result;
914 bool ColorsClose(SkColor color1, SkColor color2, SkColorType color_type) {
915 bool red = ColorComponentsClose(SkColorGetR(color1),
916 SkColorGetR(color2), color_type);
917 bool green = ColorComponentsClose(SkColorGetG(color1),
918 SkColorGetG(color2), color_type);
919 bool blue = ColorComponentsClose(SkColorGetB(color1),
920 SkColorGetB(color2), color_type);
921 bool alpha = ColorComponentsClose(SkColorGetA(color1),
922 SkColorGetA(color2), color_type);
923 if (color_type == kRGB_565_SkColorType) {
924 return red && blue && green;
926 return red && blue && green && alpha;
929 bool IsEqual(const SkBitmap& bmp1, const SkBitmap& bmp2) {
930 if (bmp1.isNull() && bmp2.isNull())
931 return true;
932 if (bmp1.width() != bmp2.width() ||
933 bmp1.height() != bmp2.height()) {
934 LOG(ERROR) << "Bitmap geometry check failure";
935 return false;
937 if (bmp1.colorType() != bmp2.colorType())
938 return false;
940 SkAutoLockPixels lock1(bmp1);
941 SkAutoLockPixels lock2(bmp2);
942 if (!bmp1.getPixels() || !bmp2.getPixels()) {
943 LOG(ERROR) << "Empty Bitmap!";
944 return false;
946 for (int y = 0; y < bmp1.height(); ++y) {
947 for (int x = 0; x < bmp1.width(); ++x) {
948 if (!ColorsClose(bmp1.getColor(x,y),
949 bmp2.getColor(x,y),
950 bmp1.colorType())) {
951 LOG(ERROR) << "Bitmap color comparision failure";
952 return false;
956 return true;
959 void BindAndAttachTextureWithPixels(GLuint src_texture,
960 SkColorType color_type,
961 const gfx::Size& src_size,
962 const SkBitmap& input_pixels) {
963 context_->bindTexture(GL_TEXTURE_2D, src_texture);
964 GLenum format = (color_type == kRGB_565_SkColorType) ?
965 GL_RGB : GL_RGBA;
966 GLenum type = (color_type == kRGB_565_SkColorType) ?
967 GL_UNSIGNED_SHORT_5_6_5 : GL_UNSIGNED_BYTE;
968 context_->texImage2D(GL_TEXTURE_2D,
970 format,
971 src_size.width(),
972 src_size.height(),
974 format,
975 type,
976 input_pixels.getPixels());
979 void ReadBackTexture(GLuint src_texture,
980 const gfx::Size& src_size,
981 unsigned char* pixels,
982 SkColorType color_type,
983 bool async) {
984 if (async) {
985 base::RunLoop run_loop;
986 helper_->ReadbackTextureAsync(src_texture,
987 src_size,
988 pixels,
989 color_type,
990 base::Bind(&callcallback,
991 run_loop.QuitClosure()));
992 run_loop.Run();
993 } else {
994 helper_->ReadbackTextureSync(src_texture,
995 gfx::Rect(src_size),
996 pixels,
997 color_type);
1001 // Test basic format readback.
1002 bool TestTextureFormatReadback(const gfx::Size& src_size,
1003 SkColorType color_type,
1004 bool async) {
1005 SkImageInfo info =
1006 SkImageInfo::Make(src_size.width(),
1007 src_size.height(),
1008 color_type,
1009 kPremul_SkAlphaType);
1010 if (!helper_->IsReadbackConfigSupported(color_type)) {
1011 LOG(INFO) << "Skipping test format not supported" << color_type;
1012 return true;
1014 WebGLId src_texture = context_->createTexture();
1015 SkBitmap input_pixels;
1016 input_pixels.allocPixels(info);
1017 // Test Pattern-1, Fill with Plain color pattern.
1018 // Erase the input bitmap with red color.
1019 input_pixels.eraseColor(SK_ColorRED);
1020 BindAndAttachTextureWithPixels(src_texture,
1021 color_type,
1022 src_size,
1023 input_pixels);
1024 SkBitmap output_pixels;
1025 output_pixels.allocPixels(info);
1026 // Initialize the output bitmap with Green color.
1027 // When the readback is over output bitmap should have the red color.
1028 output_pixels.eraseColor(SK_ColorGREEN);
1029 uint8* pixels = static_cast<uint8*>(output_pixels.getPixels());
1030 ReadBackTexture(src_texture, src_size, pixels, color_type, async);
1031 bool result = IsEqual(input_pixels, output_pixels);
1032 if (!result) {
1033 LOG(ERROR) << "Bitmap comparision failure Pattern-1";
1034 return false;
1036 const int rect_w = 10, rect_h = 4, src_grid_pitch = 10, src_grid_width = 4;
1037 const SkColor color1 = SK_ColorRED, color2 = SK_ColorBLUE;
1038 // Test Pattern-2, Fill with Grid Pattern.
1039 DrawGridToBitmap(src_size.width(), src_size.height(),
1040 color2, color1,
1041 src_grid_pitch, src_grid_width,
1042 input_pixels);
1043 BindAndAttachTextureWithPixels(src_texture,
1044 color_type,
1045 src_size,
1046 input_pixels);
1047 ReadBackTexture(src_texture, src_size, pixels, color_type, async);
1048 result = IsEqual(input_pixels, output_pixels);
1049 if (!result) {
1050 LOG(ERROR) << "Bitmap comparision failure Pattern-2";
1051 return false;
1053 // Test Pattern-3, Fill with CheckerBoard Pattern.
1054 DrawCheckerToBitmap(src_size.width(),
1055 src_size.height(),
1056 color1,
1057 color2, rect_w, rect_h, input_pixels);
1058 BindAndAttachTextureWithPixels(src_texture,
1059 color_type,
1060 src_size,
1061 input_pixels);
1062 ReadBackTexture(src_texture, src_size, pixels, color_type, async);
1063 result = IsEqual(input_pixels, output_pixels);
1064 if (!result) {
1065 LOG(ERROR) << "Bitmap comparision failure Pattern-3";
1066 return false;
1068 context_->deleteTexture(src_texture);
1069 if (HasFailure()) {
1070 return false;
1072 return true;
1075 // YUV readback test. Create a test pattern, convert to YUV
1076 // with reference implementation and compare to what gl_helper
1077 // returns.
1078 void TestYUVReadback(int xsize,
1079 int ysize,
1080 int output_xsize,
1081 int output_ysize,
1082 int xmargin,
1083 int ymargin,
1084 int test_pattern,
1085 bool flip,
1086 bool use_mrt,
1087 content::GLHelper::ScalerQuality quality) {
1088 WebGLId src_texture = context_->createTexture();
1089 SkBitmap input_pixels;
1090 input_pixels.allocN32Pixels(xsize, ysize);
1092 for (int x = 0; x < xsize; ++x) {
1093 for (int y = 0; y < ysize; ++y) {
1094 switch (test_pattern) {
1095 case 0: // Smooth test pattern
1096 SetChannel(&input_pixels, x, y, 0, x * 10);
1097 SetChannel(&input_pixels, x, y, 1, y * 10);
1098 SetChannel(&input_pixels, x, y, 2, (x + y) * 10);
1099 SetChannel(&input_pixels, x, y, 3, 255);
1100 break;
1101 case 1: // Small blocks
1102 SetChannel(&input_pixels, x, y, 0, x & 1 ? 255 : 0);
1103 SetChannel(&input_pixels, x, y, 1, y & 1 ? 255 : 0);
1104 SetChannel(&input_pixels, x, y, 2, (x + y) & 1 ? 255 : 0);
1105 SetChannel(&input_pixels, x, y, 3, 255);
1106 break;
1107 case 2: // Medium blocks
1108 SetChannel(&input_pixels, x, y, 0, 10 + x / 2 * 50);
1109 SetChannel(&input_pixels, x, y, 1, 10 + y / 3 * 50);
1110 SetChannel(&input_pixels, x, y, 2, (x + y) / 5 * 50 + 5);
1111 SetChannel(&input_pixels, x, y, 3, 255);
1112 break;
1117 context_->bindTexture(GL_TEXTURE_2D, src_texture);
1118 context_->texImage2D(GL_TEXTURE_2D,
1120 GL_RGBA,
1121 xsize,
1122 ysize,
1124 GL_RGBA,
1125 GL_UNSIGNED_BYTE,
1126 input_pixels.getPixels());
1128 gpu::Mailbox mailbox;
1129 context_->genMailboxCHROMIUM(mailbox.name);
1130 EXPECT_FALSE(mailbox.IsZero());
1131 context_->produceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name);
1132 uint32 sync_point = context_->insertSyncPoint();
1134 std::string message = base::StringPrintf(
1135 "input size: %dx%d "
1136 "output size: %dx%d "
1137 "margin: %dx%d "
1138 "pattern: %d %s %s",
1139 xsize,
1140 ysize,
1141 output_xsize,
1142 output_ysize,
1143 xmargin,
1144 ymargin,
1145 test_pattern,
1146 flip ? "flip" : "noflip",
1147 flip ? "mrt" : "nomrt");
1148 scoped_ptr<ReadbackYUVInterface> yuv_reader(
1149 helper_->CreateReadbackPipelineYUV(
1150 quality,
1151 gfx::Size(xsize, ysize),
1152 gfx::Rect(0, 0, xsize, ysize),
1153 gfx::Size(output_xsize, output_ysize),
1154 gfx::Rect(xmargin, ymargin, xsize, ysize),
1155 flip,
1156 use_mrt));
1158 scoped_refptr<media::VideoFrame> output_frame =
1159 media::VideoFrame::CreateFrame(
1160 media::VideoFrame::YV12,
1161 gfx::Size(output_xsize, output_ysize),
1162 gfx::Rect(0, 0, output_xsize, output_ysize),
1163 gfx::Size(output_xsize, output_ysize),
1164 base::TimeDelta::FromSeconds(0));
1165 scoped_refptr<media::VideoFrame> truth_frame =
1166 media::VideoFrame::CreateFrame(
1167 media::VideoFrame::YV12,
1168 gfx::Size(output_xsize, output_ysize),
1169 gfx::Rect(0, 0, output_xsize, output_ysize),
1170 gfx::Size(output_xsize, output_ysize),
1171 base::TimeDelta::FromSeconds(0));
1173 base::RunLoop run_loop;
1174 yuv_reader->ReadbackYUV(mailbox,
1175 sync_point,
1176 output_frame.get(),
1177 base::Bind(&callcallback, run_loop.QuitClosure()));
1178 run_loop.Run();
1180 if (flip) {
1181 FlipSKBitmap(&input_pixels);
1184 unsigned char* Y = truth_frame->data(media::VideoFrame::kYPlane);
1185 unsigned char* U = truth_frame->data(media::VideoFrame::kUPlane);
1186 unsigned char* V = truth_frame->data(media::VideoFrame::kVPlane);
1187 int32 y_stride = truth_frame->stride(media::VideoFrame::kYPlane);
1188 int32 u_stride = truth_frame->stride(media::VideoFrame::kUPlane);
1189 int32 v_stride = truth_frame->stride(media::VideoFrame::kVPlane);
1190 memset(Y, 0x00, y_stride * output_ysize);
1191 memset(U, 0x80, u_stride * output_ysize / 2);
1192 memset(V, 0x80, v_stride * output_ysize / 2);
1194 for (int y = 0; y < ysize; y++) {
1195 for (int x = 0; x < xsize; x++) {
1196 Y[(y + ymargin) * y_stride + x + xmargin] = float_to_byte(
1197 ChannelAsFloat(&input_pixels, x, y, 0) * 0.257 +
1198 ChannelAsFloat(&input_pixels, x, y, 1) * 0.504 +
1199 ChannelAsFloat(&input_pixels, x, y, 2) * 0.098 + 0.0625);
1203 for (int y = 0; y < ysize / 2; y++) {
1204 for (int x = 0; x < xsize / 2; x++) {
1205 U[(y + ymargin / 2) * u_stride + x + xmargin / 2] = float_to_byte(
1206 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 0) * -0.148 +
1207 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 1) * -0.291 +
1208 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 2) * 0.439 + 0.5);
1209 V[(y + ymargin / 2) * v_stride + x + xmargin / 2] = float_to_byte(
1210 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 0) * 0.439 +
1211 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 1) * -0.368 +
1212 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 2) * -0.071 +
1213 0.5);
1217 ComparePlane(Y,
1218 output_frame->data(media::VideoFrame::kYPlane),
1220 output_xsize,
1221 y_stride,
1222 output_ysize,
1223 &input_pixels,
1224 message + " Y plane");
1225 ComparePlane(U,
1226 output_frame->data(media::VideoFrame::kUPlane),
1228 output_xsize / 2,
1229 u_stride,
1230 output_ysize / 2,
1231 &input_pixels,
1232 message + " U plane");
1233 ComparePlane(V,
1234 output_frame->data(media::VideoFrame::kVPlane),
1236 output_xsize / 2,
1237 v_stride,
1238 output_ysize / 2,
1239 &input_pixels,
1240 message + " V plane");
1242 context_->deleteTexture(src_texture);
1245 void TestAddOps(int src, int dst, bool scale_x, bool allow3) {
1246 std::deque<GLHelperScaling::ScaleOp> ops;
1247 GLHelperScaling::ScaleOp::AddOps(src, dst, scale_x, allow3, &ops);
1248 // Scale factor 3 is a special case.
1249 // It is currently only allowed by itself.
1250 if (allow3 && dst * 3 >= src && dst * 2 < src) {
1251 EXPECT_EQ(ops[0].scale_factor, 3);
1252 EXPECT_EQ(ops.size(), 1U);
1253 EXPECT_EQ(ops[0].scale_x, scale_x);
1254 EXPECT_EQ(ops[0].scale_size, dst);
1255 return;
1258 for (size_t i = 0; i < ops.size(); i++) {
1259 EXPECT_EQ(ops[i].scale_x, scale_x);
1260 if (i == 0) {
1261 // Only the first op is allowed to be a scale up.
1262 // (Scaling up *after* scaling down would make it fuzzy.)
1263 EXPECT_TRUE(ops[0].scale_factor == 0 || ops[0].scale_factor == 2);
1264 } else {
1265 // All other operations must be 50% downscales.
1266 EXPECT_EQ(ops[i].scale_factor, 2);
1269 // Check that the scale factors make sense and add up.
1270 int tmp = dst;
1271 for (int i = static_cast<int>(ops.size() - 1); i >= 0; i--) {
1272 EXPECT_EQ(tmp, ops[i].scale_size);
1273 if (ops[i].scale_factor == 0) {
1274 EXPECT_EQ(i, 0);
1275 EXPECT_GT(tmp, src);
1276 tmp = src;
1277 } else {
1278 tmp *= ops[i].scale_factor;
1281 EXPECT_EQ(tmp, src);
1284 void CheckPipeline2(int xsize,
1285 int ysize,
1286 int dst_xsize,
1287 int dst_ysize,
1288 const std::string& description) {
1289 std::vector<GLHelperScaling::ScalerStage> stages;
1290 helper_scaling_->ConvertScalerOpsToScalerStages(
1291 content::GLHelper::SCALER_QUALITY_GOOD,
1292 gfx::Size(xsize, ysize),
1293 gfx::Rect(0, 0, xsize, ysize),
1294 gfx::Size(dst_xsize, dst_ysize),
1295 false,
1296 false,
1297 &x_ops_,
1298 &y_ops_,
1299 &stages);
1300 EXPECT_EQ(x_ops_.size(), 0U);
1301 EXPECT_EQ(y_ops_.size(), 0U);
1302 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD,
1303 stages,
1304 gfx::Size(dst_xsize, dst_ysize),
1305 "");
1306 EXPECT_EQ(PrintStages(stages), description);
1309 void CheckOptimizationsTest() {
1310 // Basic upscale. X and Y should be combined into one pass.
1311 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000));
1312 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000));
1313 CheckPipeline2(1024, 768, 2000, 2000, "1024x768 -> 2000x2000 bilinear\n");
1315 // X scaled 1/2, Y upscaled, should still be one pass.
1316 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512));
1317 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000));
1318 CheckPipeline2(1024, 768, 512, 2000, "1024x768 -> 512x2000 bilinear\n");
1320 // X upscaled, Y scaled 1/2, one bilinear pass
1321 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000));
1322 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384));
1323 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 2000x384 bilinear\n");
1325 // X scaled 1/2, Y scaled 1/2, one bilinear pass
1326 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512));
1327 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384));
1328 CheckPipeline2(1024, 768, 512, 384, "1024x768 -> 512x384 bilinear\n");
1330 // X scaled 1/2, Y scaled to 60%, one bilinear2 pass.
1331 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50));
1332 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1333 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1334 CheckPipeline2(100, 100, 50, 60, "100x100 -> 50x60 bilinear2 Y\n");
1336 // X scaled to 60%, Y scaled 1/2, one bilinear2 pass.
1337 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1338 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1339 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 50));
1340 CheckPipeline2(100, 100, 60, 50, "100x100 -> 60x50 bilinear2 X\n");
1342 // X scaled to 60%, Y scaled 60%, one bilinear2x2 pass.
1343 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1344 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1345 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1346 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1347 CheckPipeline2(100, 100, 60, 60, "100x100 -> 60x60 bilinear2x2\n");
1349 // X scaled to 40%, Y scaled 40%, two bilinear3 passes.
1350 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40));
1351 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40));
1352 CheckPipeline2(100,
1353 100,
1356 "100x100 -> 100x40 bilinear3 Y\n"
1357 "100x40 -> 40x40 bilinear3 X\n");
1359 // X scaled to 60%, Y scaled 40%
1360 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1361 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1362 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40));
1363 CheckPipeline2(100,
1364 100,
1367 "100x100 -> 100x40 bilinear3 Y\n"
1368 "100x40 -> 60x40 bilinear2 X\n");
1370 // X scaled to 40%, Y scaled 60%
1371 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40));
1372 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1373 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1374 CheckPipeline2(100,
1375 100,
1378 "100x100 -> 100x60 bilinear2 Y\n"
1379 "100x60 -> 40x60 bilinear3 X\n");
1381 // X scaled to 30%, Y scaled 30%
1382 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1383 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1384 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 30));
1385 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1386 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1387 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1388 CheckPipeline2(100,
1389 100,
1392 "100x100 -> 100x30 bilinear4 Y\n"
1393 "100x30 -> 30x30 bilinear4 X\n");
1395 // X scaled to 50%, Y scaled 30%
1396 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50));
1397 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1398 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1399 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1400 CheckPipeline2(100, 100, 50, 30, "100x100 -> 50x30 bilinear4 Y\n");
1402 // X scaled to 150%, Y scaled 30%
1403 // Note that we avoid combinding X and Y passes
1404 // as that would probably be LESS efficient here.
1405 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 150));
1406 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1407 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1408 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1409 CheckPipeline2(100,
1410 100,
1411 150,
1413 "100x100 -> 100x30 bilinear4 Y\n"
1414 "100x30 -> 150x30 bilinear\n");
1416 // X scaled to 1%, Y scaled 1%
1417 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 128));
1418 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 64));
1419 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 32));
1420 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 16));
1421 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 8));
1422 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 4));
1423 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 2));
1424 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 1));
1425 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 128));
1426 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 64));
1427 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 32));
1428 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 16));
1429 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 8));
1430 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 4));
1431 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 2));
1432 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 1));
1433 CheckPipeline2(100,
1434 100,
1437 "100x100 -> 100x32 bilinear4 Y\n"
1438 "100x32 -> 100x4 bilinear4 Y\n"
1439 "100x4 -> 64x1 bilinear2x2\n"
1440 "64x1 -> 8x1 bilinear4 X\n"
1441 "8x1 -> 1x1 bilinear4 X\n");
1444 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context_;
1445 gpu::ContextSupport* context_support_;
1446 scoped_ptr<content::GLHelper> helper_;
1447 scoped_ptr<content::GLHelperScaling> helper_scaling_;
1448 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_;
1451 class GLHelperPixelTest : public GLHelperTest {
1452 private:
1453 gfx::DisableNullDrawGLBindings enable_pixel_output_;
1456 TEST_F(GLHelperTest, ARGBSyncReadbackTest) {
1457 const int kTestSize = 64;
1458 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1459 kN32_SkColorType,
1460 false);
1461 EXPECT_EQ(result, true);
1464 TEST_F(GLHelperTest, RGB565SyncReadbackTest) {
1465 const int kTestSize = 64;
1466 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1467 kRGB_565_SkColorType,
1468 false);
1469 EXPECT_EQ(result, true);
1472 TEST_F(GLHelperTest, ARGBASyncReadbackTest) {
1473 const int kTestSize = 64;
1474 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1475 kN32_SkColorType,
1476 true);
1477 EXPECT_EQ(result, true);
1480 TEST_F(GLHelperTest, RGB565ASyncReadbackTest) {
1481 const int kTestSize = 64;
1482 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1483 kRGB_565_SkColorType,
1484 true);
1485 EXPECT_EQ(result, true);
1488 TEST_F(GLHelperPixelTest, YUVReadbackOptTest) {
1489 // This test uses the cb_command tracing events to detect how many
1490 // scaling passes are actually performed by the YUV readback pipeline.
1491 StartTracing(TRACE_DISABLED_BY_DEFAULT("cb_command"));
1493 TestYUVReadback(800,
1494 400,
1495 800,
1496 400,
1500 false,
1501 true,
1502 content::GLHelper::SCALER_QUALITY_FAST);
1504 std::map<std::string, int> event_counts;
1505 EndTracing(&event_counts);
1506 int draw_buffer_calls = event_counts["kDrawBuffersEXTImmediate"];
1507 int draw_arrays_calls = event_counts["kDrawArrays"];
1508 VLOG(1) << "Draw buffer calls: " << draw_buffer_calls;
1509 VLOG(1) << "DrawArrays calls: " << draw_arrays_calls;
1511 if (draw_buffer_calls) {
1512 // When using MRT, the YUV readback code should only
1513 // execute two draw arrays, and scaling should be integrated
1514 // into those two calls since we are using the FAST scalign
1515 // quality.
1516 EXPECT_EQ(2, draw_arrays_calls);
1517 } else {
1518 // When not using MRT, there are three passes for the YUV,
1519 // and one for the scaling.
1520 EXPECT_EQ(4, draw_arrays_calls);
1524 TEST_F(GLHelperPixelTest, YUVReadbackTest) {
1525 int sizes[] = {2, 4, 14};
1526 for (int flip = 0; flip <= 1; flip++) {
1527 for (int use_mrt = 0; use_mrt <= 1; use_mrt++) {
1528 for (unsigned int x = 0; x < arraysize(sizes); x++) {
1529 for (unsigned int y = 0; y < arraysize(sizes); y++) {
1530 for (unsigned int ox = x; ox < arraysize(sizes); ox++) {
1531 for (unsigned int oy = y; oy < arraysize(sizes); oy++) {
1532 // If output is a subsection of the destination frame, (letterbox)
1533 // then try different variations of where the subsection goes.
1534 for (Margin xm = x < ox ? MarginLeft : MarginRight;
1535 xm <= MarginRight;
1536 xm = NextMargin(xm)) {
1537 for (Margin ym = y < oy ? MarginLeft : MarginRight;
1538 ym <= MarginRight;
1539 ym = NextMargin(ym)) {
1540 for (int pattern = 0; pattern < 3; pattern++) {
1541 TestYUVReadback(sizes[x],
1542 sizes[y],
1543 sizes[ox],
1544 sizes[oy],
1545 compute_margin(sizes[x], sizes[ox], xm),
1546 compute_margin(sizes[y], sizes[oy], ym),
1547 pattern,
1548 flip == 1,
1549 use_mrt == 1,
1550 content::GLHelper::SCALER_QUALITY_GOOD);
1551 if (HasFailure()) {
1552 return;
1565 // Per pixel tests, all sizes are small so that we can print
1566 // out the generated bitmaps.
1567 TEST_F(GLHelperPixelTest, ScaleTest) {
1568 int sizes[] = {3, 6, 16};
1569 for (int flip = 0; flip <= 1; flip++) {
1570 for (size_t q = 0; q < arraysize(kQualities); q++) {
1571 for (int x = 0; x < 3; x++) {
1572 for (int y = 0; y < 3; y++) {
1573 for (int dst_x = 0; dst_x < 3; dst_x++) {
1574 for (int dst_y = 0; dst_y < 3; dst_y++) {
1575 for (int pattern = 0; pattern < 3; pattern++) {
1576 TestScale(sizes[x],
1577 sizes[y],
1578 sizes[dst_x],
1579 sizes[dst_y],
1580 pattern,
1582 flip == 1);
1583 if (HasFailure()) {
1584 return;
1595 // Validate that all scaling generates valid pipelines.
1596 TEST_F(GLHelperTest, ValidateScalerPipelines) {
1597 int sizes[] = {7, 99, 128, 256, 512, 719, 720, 721, 1920, 2011, 3217, 4096};
1598 for (size_t q = 0; q < arraysize(kQualities); q++) {
1599 for (size_t x = 0; x < arraysize(sizes); x++) {
1600 for (size_t y = 0; y < arraysize(sizes); y++) {
1601 for (size_t dst_x = 0; dst_x < arraysize(sizes); dst_x++) {
1602 for (size_t dst_y = 0; dst_y < arraysize(sizes); dst_y++) {
1603 TestScalerPipeline(
1604 q, sizes[x], sizes[y], sizes[dst_x], sizes[dst_y]);
1605 if (HasFailure()) {
1606 return;
1615 // Make sure we don't create overly complicated pipelines
1616 // for a few common use cases.
1617 TEST_F(GLHelperTest, CheckSpecificPipelines) {
1618 // Upscale should be single pass.
1619 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1620 1024,
1621 700,
1622 1280,
1623 720,
1624 "1024x700 -> 1280x720 bilinear\n");
1625 // Slight downscale should use BILINEAR2X2.
1626 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1627 1280,
1628 720,
1629 1024,
1630 700,
1631 "1280x720 -> 1024x700 bilinear2x2\n");
1632 // Most common tab capture pipeline on the Pixel.
1633 // Should be using two BILINEAR3 passes.
1634 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1635 2560,
1636 1476,
1637 1249,
1638 720,
1639 "2560x1476 -> 2560x720 bilinear3 Y\n"
1640 "2560x720 -> 1249x720 bilinear3 X\n");
1643 TEST_F(GLHelperTest, ScalerOpTest) {
1644 for (int allow3 = 0; allow3 <= 1; allow3++) {
1645 for (int dst = 1; dst < 2049; dst += 1 + (dst >> 3)) {
1646 for (int src = 1; src < 2049; src++) {
1647 TestAddOps(src, dst, allow3 == 1, (src & 1) == 1);
1648 if (HasFailure()) {
1649 LOG(ERROR) << "Failed for src=" << src << " dst=" << dst
1650 << " allow3=" << allow3;
1651 return;
1658 TEST_F(GLHelperTest, CheckOptimizations) {
1659 // Test in baseclass since it is friends with GLHelperScaling
1660 CheckOptimizationsTest();
1663 } // namespace
1665 // These tests needs to run against a proper GL environment, so we
1666 // need to set it up before we can run the tests.
1667 int main(int argc, char** argv) {
1668 CommandLine::Init(argc, argv);
1669 base::TestSuite* suite = new content::ContentTestSuite(argc, argv);
1670 #if defined(OS_MACOSX)
1671 base::mac::ScopedNSAutoreleasePool pool;
1672 #endif
1674 content::UnitTestTestSuite runner(suite);
1675 base::MessageLoop message_loop;
1676 return runner.Run();