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.
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_scaling.h"
27 #include "content/public/test/unittest_test_suite.h"
28 #include "content/test/content_test_suite.h"
29 #include "gpu/config/gpu_util.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_surface.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"
41 #if defined(TOOLKIT_GTK)
42 #include "ui/gfx/gtk_util.h"
48 using blink::WebGraphicsContext3D;
49 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
51 content::GLHelper::ScalerQuality kQualities[] = {
52 content::GLHelper::SCALER_QUALITY_BEST,
53 content::GLHelper::SCALER_QUALITY_GOOD,
54 content::GLHelper::SCALER_QUALITY_FAST, };
56 const char* kQualityNames[] = {"best", "good", "fast", };
58 class GLHelperTest : public testing::Test {
60 virtual void SetUp() {
61 WebGraphicsContext3D::Attributes attributes;
63 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
65 context_->makeContextCurrent();
66 context_support_ = context_->GetContextSupport();
68 new content::GLHelper(context_->GetGLInterface(), context_support_));
69 helper_scaling_.reset(new content::GLHelperScaling(
70 context_->GetGLInterface(), helper_.get()));
73 virtual void TearDown() {
74 helper_scaling_.reset(NULL);
79 void StartTracing(const std::string& filter) {
80 base::debug::TraceLog::GetInstance()->SetEnabled(
81 base::debug::CategoryFilter(filter),
82 base::debug::TraceLog::RECORDING_MODE,
83 base::debug::TraceLog::RECORD_UNTIL_FULL);
86 static void TraceDataCB(
87 const base::Callback<void()>& callback,
89 const scoped_refptr<base::RefCountedString>& json_events_str,
90 bool has_more_events) {
91 if (output->size() > 1) {
94 output->append(json_events_str->data());
95 if (!has_more_events) {
100 // End tracing, return tracing data in a simple map
101 // of event name->counts.
102 void EndTracing(std::map<std::string, int>* event_counts) {
103 std::string json_data = "[";
104 base::debug::TraceLog::GetInstance()->SetDisabled();
105 base::RunLoop run_loop;
106 base::debug::TraceLog::GetInstance()->Flush(
107 base::Bind(&GLHelperTest::TraceDataCB,
108 run_loop.QuitClosure(),
109 base::Unretained(&json_data)));
111 json_data.append("]");
113 scoped_ptr<base::Value> trace_data(base::JSONReader::Read(json_data));
114 base::ListValue* list;
115 CHECK(trace_data->GetAsList(&list));
116 for (size_t i = 0; i < list->GetSize(); i++) {
117 base::Value* item = NULL;
118 if (list->Get(i, &item)) {
119 base::DictionaryValue* dict;
120 CHECK(item->GetAsDictionary(&dict));
122 CHECK(dict->GetString("name", &name));
123 (*event_counts)[name]++;
124 VLOG(1) << "trace name: " << name;
129 // Bicubic filter kernel function.
130 static float Bicubic(float x) {
131 const float a = -0.5;
136 return (a + 2) * x3 - (a + 3) * x2 + 1;
138 return a * x3 - 5 * a * x2 + 8 * a * x - 4 * a;
144 // Look up a single R/G/B/A value.
146 int Channel(SkBitmap* pixels, int x, int y, int c) {
148 pixels->getAddr32(std::max(0, std::min(x, pixels->width() - 1)),
149 std::max(0, std::min(y, pixels->height() - 1)));
150 return (*data) >> (c * 8) & 0xff;
153 // Set a single R/G/B/A value.
154 void SetChannel(SkBitmap* pixels, int x, int y, int c, int v) {
157 DCHECK_LT(x, pixels->width());
158 DCHECK_LT(y, pixels->height());
159 uint32* data = pixels->getAddr32(x, y);
160 v = std::max(0, std::min(v, 255));
161 *data = (*data & ~(0xffu << (c * 8))) | (v << (c * 8));
164 // Print all the R, G, B or A values from an SkBitmap in a
165 // human-readable format.
166 void PrintChannel(SkBitmap* pixels, int c) {
167 for (int y = 0; y < pixels->height(); y++) {
168 std::string formatted;
169 for (int x = 0; x < pixels->width(); x++) {
170 formatted.append(base::StringPrintf("%3d, ", Channel(pixels, x, y, c)));
172 LOG(ERROR) << formatted;
176 // Print out the individual steps of a scaler pipeline.
177 std::string PrintStages(
178 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages) {
180 for (size_t i = 0; i < scaler_stages.size(); i++) {
181 ret.append(base::StringPrintf("%dx%d -> %dx%d ",
182 scaler_stages[i].src_size.width(),
183 scaler_stages[i].src_size.height(),
184 scaler_stages[i].dst_size.width(),
185 scaler_stages[i].dst_size.height()));
186 bool xy_matters = false;
187 switch (scaler_stages[i].shader) {
188 case GLHelperScaling::SHADER_BILINEAR:
189 ret.append("bilinear");
191 case GLHelperScaling::SHADER_BILINEAR2:
192 ret.append("bilinear2");
195 case GLHelperScaling::SHADER_BILINEAR3:
196 ret.append("bilinear3");
199 case GLHelperScaling::SHADER_BILINEAR4:
200 ret.append("bilinear4");
203 case GLHelperScaling::SHADER_BILINEAR2X2:
204 ret.append("bilinear2x2");
206 case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
207 ret.append("bicubic upscale");
210 case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
211 ret.append("bicubic 1/2");
214 case GLHelperScaling::SHADER_PLANAR:
215 ret.append("planar");
217 case GLHelperScaling::SHADER_YUV_MRT_PASS1:
218 ret.append("rgb2yuv pass 1");
220 case GLHelperScaling::SHADER_YUV_MRT_PASS2:
221 ret.append("rgb2yuv pass 2");
226 if (scaler_stages[i].scale_x) {
237 bool CheckScale(double scale, int samples, bool already_scaled) {
238 // 1:1 is valid if there is one sample.
239 if (samples == 1 && scale == 1.0) {
242 // Is it an exact down-scale (50%, 25%, etc.?)
243 if (scale == 2.0 * samples) {
246 // Upscales, only valid if we haven't already scaled in this dimension.
247 if (!already_scaled) {
248 // Is it a valid bilinear upscale?
249 if (samples == 1 && scale <= 1.0) {
252 // Multi-sample upscale-downscale combination?
253 if (scale > samples / 2.0 && scale < samples) {
260 // Make sure that the stages of the scaler pipeline are sane.
261 void ValidateScalerStages(
262 content::GLHelper::ScalerQuality quality,
263 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages,
264 const std::string& message) {
265 bool previous_error = HasFailure();
266 // First, check that the input size for each stage is equal to
267 // the output size of the previous stage.
268 for (size_t i = 1; i < scaler_stages.size(); i++) {
269 EXPECT_EQ(scaler_stages[i - 1].dst_size.width(),
270 scaler_stages[i].src_size.width());
271 EXPECT_EQ(scaler_stages[i - 1].dst_size.height(),
272 scaler_stages[i].src_size.height());
273 EXPECT_EQ(scaler_stages[i].src_subrect.x(), 0);
274 EXPECT_EQ(scaler_stages[i].src_subrect.y(), 0);
275 EXPECT_EQ(scaler_stages[i].src_subrect.width(),
276 scaler_stages[i].src_size.width());
277 EXPECT_EQ(scaler_stages[i].src_subrect.height(),
278 scaler_stages[i].src_size.height());
281 // Used to verify that up-scales are not attempted after some
283 bool scaled_x = false;
284 bool scaled_y = false;
286 for (size_t i = 0; i < scaler_stages.size(); i++) {
287 // Note: 2.0 means scaling down by 50%
289 static_cast<double>(scaler_stages[i].src_subrect.width()) /
290 static_cast<double>(scaler_stages[i].dst_size.width());
292 static_cast<double>(scaler_stages[i].src_subrect.height()) /
293 static_cast<double>(scaler_stages[i].dst_size.height());
298 // Codify valid scale operations.
299 switch (scaler_stages[i].shader) {
300 case GLHelperScaling::SHADER_PLANAR:
301 case GLHelperScaling::SHADER_YUV_MRT_PASS1:
302 case GLHelperScaling::SHADER_YUV_MRT_PASS2:
303 EXPECT_TRUE(false) << "Invalid shader.";
306 case GLHelperScaling::SHADER_BILINEAR:
307 if (quality != content::GLHelper::SCALER_QUALITY_FAST) {
312 case GLHelperScaling::SHADER_BILINEAR2:
316 case GLHelperScaling::SHADER_BILINEAR3:
320 case GLHelperScaling::SHADER_BILINEAR4:
324 case GLHelperScaling::SHADER_BILINEAR2X2:
328 case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
329 if (scaler_stages[i].scale_x) {
330 EXPECT_LT(x_scale, 1.0);
331 EXPECT_EQ(y_scale, 1.0);
333 EXPECT_EQ(x_scale, 1.0);
334 EXPECT_LT(y_scale, 1.0);
337 case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
338 if (scaler_stages[i].scale_x) {
339 EXPECT_EQ(x_scale, 2.0);
340 EXPECT_EQ(y_scale, 1.0);
342 EXPECT_EQ(x_scale, 1.0);
343 EXPECT_EQ(y_scale, 2.0);
348 if (!scaler_stages[i].scale_x) {
349 std::swap(x_samples, y_samples);
353 EXPECT_TRUE(CheckScale(x_scale, x_samples, scaled_x))
354 << "x_scale = " << x_scale;
357 EXPECT_TRUE(CheckScale(y_scale, y_samples, scaled_y))
358 << "y_scale = " << y_scale;
361 if (x_scale != 1.0) {
364 if (y_scale != 1.0) {
369 if (HasFailure() && !previous_error) {
370 LOG(ERROR) << "Invalid scaler stages: " << message;
371 LOG(ERROR) << "Scaler stages:";
372 LOG(ERROR) << PrintStages(scaler_stages);
376 // Compare two bitmaps, make sure that each component of each pixel
377 // is no more than |maxdiff| apart. If they are not similar enough,
378 // prints out |truth|, |other|, |source|, |scaler_stages| and |message|.
379 void Compare(SkBitmap* truth,
383 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages,
384 std::string message) {
385 EXPECT_EQ(truth->width(), other->width());
386 EXPECT_EQ(truth->height(), other->height());
387 for (int x = 0; x < truth->width(); x++) {
388 for (int y = 0; y < truth->height(); y++) {
389 for (int c = 0; c < 4; c++) {
390 int a = Channel(truth, x, y, c);
391 int b = Channel(other, x, y, c);
392 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " c=" << c
394 if (std::abs(a - b) > maxdiff) {
395 LOG(ERROR) << "-------expected--------";
396 PrintChannel(truth, c);
397 LOG(ERROR) << "-------actual--------";
398 PrintChannel(other, c);
400 LOG(ERROR) << "-------before scaling--------";
401 PrintChannel(source, c);
403 LOG(ERROR) << "-----Scaler stages------";
404 LOG(ERROR) << PrintStages(scaler_stages);
412 // Get a single R, G, B or A value as a float.
413 float ChannelAsFloat(SkBitmap* pixels, int x, int y, int c) {
414 return Channel(pixels, x, y, c) / 255.0;
417 // Works like a GL_LINEAR lookup on an SkBitmap.
418 float Bilinear(SkBitmap* pixels, float x, float y, int c) {
421 int base_x = static_cast<int>(floorf(x));
422 int base_y = static_cast<int>(floorf(y));
425 return (ChannelAsFloat(pixels, base_x, base_y, c) * (1 - x) * (1 - y) +
426 ChannelAsFloat(pixels, base_x + 1, base_y, c) * x * (1 - y) +
427 ChannelAsFloat(pixels, base_x, base_y + 1, c) * (1 - x) * y +
428 ChannelAsFloat(pixels, base_x + 1, base_y + 1, c) * x * y);
431 // Very slow bicubic / bilinear scaler for reference.
432 void ScaleSlow(SkBitmap* input,
434 content::GLHelper::ScalerQuality quality) {
435 float xscale = static_cast<float>(input->width()) / output->width();
436 float yscale = static_cast<float>(input->height()) / output->height();
437 float clamped_xscale = xscale < 1.0 ? 1.0 : 1.0 / xscale;
438 float clamped_yscale = yscale < 1.0 ? 1.0 : 1.0 / yscale;
439 for (int dst_y = 0; dst_y < output->height(); dst_y++) {
440 for (int dst_x = 0; dst_x < output->width(); dst_x++) {
441 for (int channel = 0; channel < 4; channel++) {
442 float dst_x_in_src = (dst_x + 0.5f) * xscale;
443 float dst_y_in_src = (dst_y + 0.5f) * yscale;
448 case content::GLHelper::SCALER_QUALITY_BEST:
449 for (int src_y = -10; src_y < input->height() + 10; ++src_y) {
451 Bicubic((src_y + 0.5f - dst_y_in_src) * clamped_yscale);
452 if (coeff_y == 0.0f) {
455 for (int src_x = -10; src_x < input->width() + 10; ++src_x) {
458 Bicubic((src_x + 0.5f - dst_x_in_src) * clamped_xscale);
463 float c = ChannelAsFloat(input, src_x, src_y, channel);
469 case content::GLHelper::SCALER_QUALITY_GOOD: {
470 int xshift = 0, yshift = 0;
471 while ((output->width() << xshift) < input->width()) {
474 while ((output->height() << yshift) < input->height()) {
477 int xmag = 1 << xshift;
478 int ymag = 1 << yshift;
479 if (xmag == 4 && output->width() * 3 >= input->width()) {
482 if (ymag == 4 && output->height() * 3 >= input->height()) {
485 for (int x = 0; x < xmag; x++) {
486 for (int y = 0; y < ymag; y++) {
487 value += Bilinear(input,
488 (dst_x * xmag + x + 0.5) * xscale / xmag,
489 (dst_y * ymag + y + 0.5) * yscale / ymag,
497 case content::GLHelper::SCALER_QUALITY_FAST:
498 value = Bilinear(input, dst_x_in_src, dst_y_in_src, channel);
506 static_cast<int>(value * 255.0f + 0.5f));
512 void FlipSKBitmap(SkBitmap* bitmap) {
514 int bottom_line = bitmap->height() - 1;
515 while (top_line < bottom_line) {
516 for (int x = 0; x < bitmap->width(); x++) {
517 std::swap(*bitmap->getAddr32(x, top_line),
518 *bitmap->getAddr32(x, bottom_line));
525 // gl_helper scales recursively, so we'll need to do that
526 // in the reference implementation too.
527 void ScaleSlowRecursive(SkBitmap* input,
529 content::GLHelper::ScalerQuality quality) {
530 if (quality == content::GLHelper::SCALER_QUALITY_FAST ||
531 quality == content::GLHelper::SCALER_QUALITY_GOOD) {
532 ScaleSlow(input, output, quality);
536 float xscale = static_cast<float>(output->width()) / input->width();
538 // This corresponds to all the operations we can do directly.
539 float yscale = static_cast<float>(output->height()) / input->height();
540 if ((xscale == 1.0f && yscale == 1.0f) ||
541 (xscale == 0.5f && yscale == 1.0f) ||
542 (xscale == 1.0f && yscale == 0.5f) ||
543 (xscale >= 1.0f && yscale == 1.0f) ||
544 (xscale == 1.0f && yscale >= 1.0f)) {
545 ScaleSlow(input, output, quality);
549 // Now we break the problem down into smaller pieces, using the
550 // operations available.
551 int xtmp = input->width();
552 int ytmp = input->height();
554 if (output->height() != input->height()) {
555 ytmp = output->height();
556 while (ytmp < input->height() && ytmp * 2 != input->height()) {
560 xtmp = output->width();
561 while (xtmp < input->width() && xtmp * 2 != input->width()) {
567 tmp.setConfig(SkBitmap::kARGB_8888_Config, xtmp, ytmp);
569 SkAutoLockPixels lock(tmp);
571 ScaleSlowRecursive(input, &tmp, quality);
572 ScaleSlowRecursive(&tmp, output, quality);
575 // Scaling test: Create a test image, scale it using GLHelperScaling
576 // and a reference implementation and compare the results.
577 void TestScale(int xsize,
584 WebGLId src_texture = context_->createTexture();
585 WebGLId framebuffer = context_->createFramebuffer();
586 SkBitmap input_pixels;
587 input_pixels.setConfig(SkBitmap::kARGB_8888_Config, xsize, ysize);
588 input_pixels.allocPixels();
589 SkAutoLockPixels lock(input_pixels);
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);
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);
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);
616 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
617 context_->bindTexture(GL_TEXTURE_2D, src_texture);
618 context_->texImage2D(GL_TEXTURE_2D,
626 input_pixels.getPixels());
628 std::string message = base::StringPrintf(
630 "output size: %dx%d "
631 "pattern: %d quality: %s",
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),
647 ValidateScalerStages(kQualities[quality], stages, message);
649 WebGLId dst_texture =
650 helper_->CopyAndScaleTexture(src_texture,
651 gfx::Size(xsize, ysize),
652 gfx::Size(scaled_xsize, scaled_ysize),
654 kQualities[quality]);
656 SkBitmap output_pixels;
657 output_pixels.setConfig(
658 SkBitmap::kARGB_8888_Config, scaled_xsize, scaled_ysize);
659 output_pixels.allocPixels();
660 SkAutoLockPixels output_lock(output_pixels);
662 helper_->ReadbackTextureSync(
664 gfx::Rect(0, 0, scaled_xsize, scaled_ysize),
665 static_cast<unsigned char*>(output_pixels.getPixels()),
666 SkBitmap::kARGB_8888_Config);
668 // Flip the pixels back.
669 FlipSKBitmap(&output_pixels);
671 if (xsize == scaled_xsize && ysize == scaled_ysize) {
672 Compare(&input_pixels,
677 message + " comparing against input");
679 SkBitmap truth_pixels;
680 truth_pixels.setConfig(
681 SkBitmap::kARGB_8888_Config, scaled_xsize, scaled_ysize);
682 truth_pixels.allocPixels();
683 SkAutoLockPixels truth_lock(truth_pixels);
685 ScaleSlowRecursive(&input_pixels, &truth_pixels, kQualities[quality]);
686 Compare(&truth_pixels,
691 message + " comparing against scaled");
693 context_->deleteTexture(src_texture);
694 context_->deleteTexture(dst_texture);
695 context_->deleteFramebuffer(framebuffer);
698 // Create a scaling pipeline and check that it is made up of
699 // valid scaling operations.
700 void TestScalerPipeline(size_t quality,
705 std::vector<GLHelperScaling::ScalerStage> stages;
706 helper_scaling_->ComputeScalerStages(kQualities[quality],
707 gfx::Size(xsize, ysize),
708 gfx::Rect(0, 0, xsize, ysize),
709 gfx::Size(dst_xsize, dst_ysize),
713 ValidateScalerStages(kQualities[quality],
717 "output size: %dx%d "
723 kQualityNames[quality]));
726 // Create a scaling pipeline and make sure that the steps
727 // are exactly the steps we expect.
728 void CheckPipeline(content::GLHelper::ScalerQuality quality,
733 const std::string& description) {
734 std::vector<GLHelperScaling::ScalerStage> stages;
735 helper_scaling_->ComputeScalerStages(quality,
736 gfx::Size(xsize, ysize),
737 gfx::Rect(0, 0, xsize, ysize),
738 gfx::Size(dst_xsize, dst_ysize),
742 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, "");
743 EXPECT_EQ(PrintStages(stages), description);
746 // Note: Left/Right means Top/Bottom when used for Y dimension.
754 static Margin NextMargin(Margin m) {
761 return MarginInvalid;
763 return MarginInvalid;
767 int compute_margin(int insize, int outsize, Margin m) {
768 int available = outsize - insize;
771 EXPECT_TRUE(false) << "This should not happen.";
776 return (available / 2) & ~1;
782 // Convert 0.0 - 1.0 to 0 - 255
783 int float_to_byte(float v) {
784 int ret = static_cast<int>(floorf(v * 255.0f + 0.5f));
794 static void callcallback(const base::Callback<void()>& callback,
799 void PrintPlane(unsigned char* plane, int xsize, int stride, int ysize) {
800 for (int y = 0; y < ysize; y++) {
801 std::string formatted;
802 for (int x = 0; x < xsize; x++) {
803 formatted.append(base::StringPrintf("%3d, ", plane[y * stride + x]));
805 LOG(ERROR) << formatted << " (" << (plane + y * stride) << ")";
809 // Compare two planes make sure that each component of each pixel
810 // is no more than |maxdiff| apart.
811 void ComparePlane(unsigned char* truth,
812 unsigned char* other,
818 std::string message) {
819 int truth_stride = stride;
820 for (int x = 0; x < xsize; x++) {
821 for (int y = 0; y < ysize; y++) {
822 int a = other[y * stride + x];
823 int b = truth[y * stride + x];
824 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " "
826 if (std::abs(a - b) > maxdiff) {
827 LOG(ERROR) << "-------expected--------";
828 PrintPlane(truth, xsize, truth_stride, ysize);
829 LOG(ERROR) << "-------actual--------";
830 PrintPlane(other, xsize, stride, ysize);
832 LOG(ERROR) << "-------before yuv conversion: red--------";
833 PrintChannel(source, 0);
834 LOG(ERROR) << "-------before yuv conversion: green------";
835 PrintChannel(source, 1);
836 LOG(ERROR) << "-------before yuv conversion: blue-------";
837 PrintChannel(source, 2);
845 void DrawGridToBitmap(int w, int h,
846 SkColor background_color,
851 ASSERT_GT(grid_pitch, 0);
852 ASSERT_GT(grid_width, 0);
853 ASSERT_NE(background_color, grid_color);
855 for (int y = 0; y < h; ++y) {
856 bool y_on_grid = ((y % grid_pitch) < grid_width);
858 for (int x = 0; x < w; ++x) {
859 bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width));
861 if (bmp.getConfig() == SkBitmap::kARGB_8888_Config) {
862 *bmp.getAddr32(x, y) = (on_grid ? grid_color : background_color);
863 } else if (bmp.getConfig() == SkBitmap::kRGB_565_Config) {
864 *bmp.getAddr16(x, y) = (on_grid ? grid_color : background_color);
870 void DrawCheckerToBitmap(int w, int h,
871 SkColor color1, SkColor color2,
872 int rect_w, int rect_h,
874 ASSERT_GT(rect_w, 0);
875 ASSERT_GT(rect_h, 0);
876 ASSERT_NE(color1, color2);
878 for (int y = 0; y < h; ++y) {
879 bool y_bit = (((y / rect_h) & 0x1) == 0);
881 for (int x = 0; x < w; ++x) {
882 bool x_bit = (((x / rect_w) & 0x1) == 0);
884 bool use_color2 = (x_bit != y_bit); // xor
885 if (bmp.getConfig() == SkBitmap::kARGB_8888_Config) {
886 *bmp.getAddr32(x, y) = (use_color2 ? color2 : color1);
887 } else if (bmp.getConfig() == SkBitmap::kRGB_565_Config) {
888 *bmp.getAddr16(x, y) = (use_color2 ? color2 : color1);
894 bool ColorComponentsClose(SkColor component1,
896 SkBitmap::Config config) {
897 int c1 = static_cast<int>(component1);
898 int c2 = static_cast<int>(component2);
901 case SkBitmap::kARGB_8888_Config:
902 result = (std::abs(c1 - c2) == 0);
904 case SkBitmap::kRGB_565_Config:
905 result = (std::abs(c1 - c2) <= 7);
913 bool ColorsClose(SkColor color1, SkColor color2, SkBitmap::Config config) {
914 bool red = ColorComponentsClose(SkColorGetR(color1),
915 SkColorGetR(color2), config);
916 bool green = ColorComponentsClose(SkColorGetG(color1),
917 SkColorGetG(color2), config);
918 bool blue = ColorComponentsClose(SkColorGetB(color1),
919 SkColorGetB(color2), config);
920 bool alpha = ColorComponentsClose(SkColorGetA(color1),
921 SkColorGetA(color2), config);
922 if (config == SkBitmap::kRGB_565_Config) {
923 return red && blue && green;
925 return red && blue && green && alpha;
928 bool IsEqual(const SkBitmap& bmp1, const SkBitmap& bmp2) {
929 if (bmp1.isNull() && bmp2.isNull())
931 if (bmp1.width() != bmp2.width() ||
932 bmp1.height() != bmp2.height()) {
933 LOG(ERROR) << "Bitmap geometry check failure";
936 if (bmp1.getConfig() != bmp2.getConfig())
939 SkAutoLockPixels lock1(bmp1);
940 SkAutoLockPixels lock2(bmp2);
941 if (!bmp1.getPixels() || !bmp2.getPixels()) {
942 LOG(ERROR) << "Empty Bitmap!";
945 for (int y = 0; y < bmp1.height(); ++y) {
946 for (int x = 0; x < bmp1.width(); ++x) {
947 if (!ColorsClose(bmp1.getColor(x,y),
950 LOG(ERROR) << "Bitmap color comparision failure";
958 void BindAndAttachTextureWithPixels(GLuint src_texture,
959 SkBitmap::Config bitmap_config,
960 const gfx::Size& src_size,
961 const SkBitmap& input_pixels) {
962 context_->bindTexture(GL_TEXTURE_2D, src_texture);
963 GLenum format = (bitmap_config == SkBitmap::kRGB_565_Config) ?
965 GLenum type = (bitmap_config == SkBitmap::kRGB_565_Config) ?
966 GL_UNSIGNED_SHORT_5_6_5 : GL_UNSIGNED_BYTE;
967 context_->texImage2D(GL_TEXTURE_2D,
975 input_pixels.getPixels());
978 void ReadBackTexture(GLuint src_texture,
979 const gfx::Size& src_size,
980 unsigned char* pixels,
981 SkBitmap::Config bitmap_config,
984 base::RunLoop run_loop;
985 helper_->ReadbackTextureAsync(src_texture,
989 base::Bind(&callcallback,
990 run_loop.QuitClosure()));
993 helper_->ReadbackTextureSync(src_texture,
1000 // Test basic format readback.
1001 bool TestTextureFormatReadback(const gfx::Size& src_size,
1002 SkBitmap::Config bitmap_config,
1004 DCHECK((bitmap_config == SkBitmap::kRGB_565_Config) ||
1005 (bitmap_config == SkBitmap::kARGB_8888_Config));
1006 bool rgb565_format = (bitmap_config == SkBitmap::kRGB_565_Config);
1007 if (rgb565_format && !helper_->CanUseRgb565Readback()) {
1008 LOG(INFO) << "RGB565 Format Not supported on this platform";
1009 LOG(INFO) << "Skipping RGB565ReadBackTest";
1012 WebGLId src_texture = context_->createTexture();
1013 SkBitmap input_pixels;
1014 input_pixels.setConfig(bitmap_config, src_size.width(),
1016 input_pixels.allocPixels();
1017 SkAutoLockPixels lock1(input_pixels);
1018 // Test Pattern-1, Fill with Plain color pattern.
1019 // Erase the input bitmap with red color.
1020 input_pixels.eraseColor(SK_ColorRED);
1021 BindAndAttachTextureWithPixels(src_texture,
1025 SkBitmap output_pixels;
1026 output_pixels.setConfig(bitmap_config, src_size.width(),
1028 output_pixels.allocPixels();
1029 SkAutoLockPixels lock2(output_pixels);
1030 // Initialize the output bitmap with Green color.
1031 // When the readback is over output bitmap should have the red color.
1032 output_pixels.eraseColor(SK_ColorGREEN);
1033 uint8* pixels = static_cast<uint8*>(output_pixels.getPixels());
1034 ReadBackTexture(src_texture, src_size, pixels, bitmap_config, async);
1035 bool result = IsEqual(input_pixels, output_pixels);
1037 LOG(ERROR) << "Bitmap comparision failure Pattern-1";
1040 const int rect_w = 10, rect_h = 4, src_grid_pitch = 10, src_grid_width = 4;
1041 const SkColor color1 = SK_ColorRED, color2 = SK_ColorBLUE;
1042 // Test Pattern-2, Fill with Grid Pattern.
1043 DrawGridToBitmap(src_size.width(), src_size.height(),
1045 src_grid_pitch, src_grid_width,
1047 BindAndAttachTextureWithPixels(src_texture,
1051 ReadBackTexture(src_texture, src_size, pixels, bitmap_config, async);
1052 result = IsEqual(input_pixels, output_pixels);
1054 LOG(ERROR) << "Bitmap comparision failure Pattern-2";
1057 // Test Pattern-3, Fill with CheckerBoard Pattern.
1058 DrawCheckerToBitmap(src_size.width(),
1061 color2, rect_w, rect_h, input_pixels);
1062 BindAndAttachTextureWithPixels(src_texture,
1066 ReadBackTexture(src_texture, src_size, pixels, bitmap_config, async);
1067 result = IsEqual(input_pixels, output_pixels);
1069 LOG(ERROR) << "Bitmap comparision failure Pattern-3";
1072 context_->deleteTexture(src_texture);
1079 // YUV readback test. Create a test pattern, convert to YUV
1080 // with reference implementation and compare to what gl_helper
1082 void TestYUVReadback(int xsize,
1091 content::GLHelper::ScalerQuality quality) {
1092 WebGLId src_texture = context_->createTexture();
1093 SkBitmap input_pixels;
1094 input_pixels.setConfig(SkBitmap::kARGB_8888_Config, xsize, ysize);
1095 input_pixels.allocPixels();
1096 SkAutoLockPixels lock(input_pixels);
1098 for (int x = 0; x < xsize; ++x) {
1099 for (int y = 0; y < ysize; ++y) {
1100 switch (test_pattern) {
1101 case 0: // Smooth test pattern
1102 SetChannel(&input_pixels, x, y, 0, x * 10);
1103 SetChannel(&input_pixels, x, y, 1, y * 10);
1104 SetChannel(&input_pixels, x, y, 2, (x + y) * 10);
1105 SetChannel(&input_pixels, x, y, 3, 255);
1107 case 1: // Small blocks
1108 SetChannel(&input_pixels, x, y, 0, x & 1 ? 255 : 0);
1109 SetChannel(&input_pixels, x, y, 1, y & 1 ? 255 : 0);
1110 SetChannel(&input_pixels, x, y, 2, (x + y) & 1 ? 255 : 0);
1111 SetChannel(&input_pixels, x, y, 3, 255);
1113 case 2: // Medium blocks
1114 SetChannel(&input_pixels, x, y, 0, 10 + x / 2 * 50);
1115 SetChannel(&input_pixels, x, y, 1, 10 + y / 3 * 50);
1116 SetChannel(&input_pixels, x, y, 2, (x + y) / 5 * 50 + 5);
1117 SetChannel(&input_pixels, x, y, 3, 255);
1123 context_->bindTexture(GL_TEXTURE_2D, src_texture);
1124 context_->texImage2D(GL_TEXTURE_2D,
1132 input_pixels.getPixels());
1134 gpu::Mailbox mailbox;
1135 context_->genMailboxCHROMIUM(mailbox.name);
1136 EXPECT_FALSE(mailbox.IsZero());
1137 context_->produceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name);
1138 uint32 sync_point = context_->insertSyncPoint();
1140 std::string message = base::StringPrintf(
1141 "input size: %dx%d "
1142 "output size: %dx%d "
1144 "pattern: %d %s %s",
1152 flip ? "flip" : "noflip",
1153 flip ? "mrt" : "nomrt");
1154 scoped_ptr<ReadbackYUVInterface> yuv_reader(
1155 helper_->CreateReadbackPipelineYUV(
1157 gfx::Size(xsize, ysize),
1158 gfx::Rect(0, 0, xsize, ysize),
1159 gfx::Size(output_xsize, output_ysize),
1160 gfx::Rect(xmargin, ymargin, xsize, ysize),
1164 scoped_refptr<media::VideoFrame> output_frame =
1165 media::VideoFrame::CreateFrame(
1166 media::VideoFrame::YV12,
1167 gfx::Size(output_xsize, output_ysize),
1168 gfx::Rect(0, 0, output_xsize, output_ysize),
1169 gfx::Size(output_xsize, output_ysize),
1170 base::TimeDelta::FromSeconds(0));
1171 scoped_refptr<media::VideoFrame> truth_frame =
1172 media::VideoFrame::CreateFrame(
1173 media::VideoFrame::YV12,
1174 gfx::Size(output_xsize, output_ysize),
1175 gfx::Rect(0, 0, output_xsize, output_ysize),
1176 gfx::Size(output_xsize, output_ysize),
1177 base::TimeDelta::FromSeconds(0));
1179 base::RunLoop run_loop;
1180 yuv_reader->ReadbackYUV(mailbox,
1183 base::Bind(&callcallback, run_loop.QuitClosure()));
1187 FlipSKBitmap(&input_pixels);
1190 unsigned char* Y = truth_frame->data(media::VideoFrame::kYPlane);
1191 unsigned char* U = truth_frame->data(media::VideoFrame::kUPlane);
1192 unsigned char* V = truth_frame->data(media::VideoFrame::kVPlane);
1193 int32 y_stride = truth_frame->stride(media::VideoFrame::kYPlane);
1194 int32 u_stride = truth_frame->stride(media::VideoFrame::kUPlane);
1195 int32 v_stride = truth_frame->stride(media::VideoFrame::kVPlane);
1196 memset(Y, 0x00, y_stride * output_ysize);
1197 memset(U, 0x80, u_stride * output_ysize / 2);
1198 memset(V, 0x80, v_stride * output_ysize / 2);
1200 for (int y = 0; y < ysize; y++) {
1201 for (int x = 0; x < xsize; x++) {
1202 Y[(y + ymargin) * y_stride + x + xmargin] = float_to_byte(
1203 ChannelAsFloat(&input_pixels, x, y, 0) * 0.257 +
1204 ChannelAsFloat(&input_pixels, x, y, 1) * 0.504 +
1205 ChannelAsFloat(&input_pixels, x, y, 2) * 0.098 + 0.0625);
1209 for (int y = 0; y < ysize / 2; y++) {
1210 for (int x = 0; x < xsize / 2; x++) {
1211 U[(y + ymargin / 2) * u_stride + x + xmargin / 2] = float_to_byte(
1212 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 0) * -0.148 +
1213 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 1) * -0.291 +
1214 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 2) * 0.439 + 0.5);
1215 V[(y + ymargin / 2) * v_stride + x + xmargin / 2] = float_to_byte(
1216 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 0) * 0.439 +
1217 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 1) * -0.368 +
1218 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 2) * -0.071 +
1224 output_frame->data(media::VideoFrame::kYPlane),
1230 message + " Y plane");
1232 output_frame->data(media::VideoFrame::kUPlane),
1238 message + " U plane");
1240 output_frame->data(media::VideoFrame::kVPlane),
1246 message + " V plane");
1248 context_->deleteTexture(src_texture);
1251 void TestAddOps(int src, int dst, bool scale_x, bool allow3) {
1252 std::deque<GLHelperScaling::ScaleOp> ops;
1253 GLHelperScaling::ScaleOp::AddOps(src, dst, scale_x, allow3, &ops);
1254 // Scale factor 3 is a special case.
1255 // It is currently only allowed by itself.
1256 if (allow3 && dst * 3 >= src && dst * 2 < src) {
1257 EXPECT_EQ(ops[0].scale_factor, 3);
1258 EXPECT_EQ(ops.size(), 1U);
1259 EXPECT_EQ(ops[0].scale_x, scale_x);
1260 EXPECT_EQ(ops[0].scale_size, dst);
1264 for (size_t i = 0; i < ops.size(); i++) {
1265 EXPECT_EQ(ops[i].scale_x, scale_x);
1267 // Only the first op is allowed to be a scale up.
1268 // (Scaling up *after* scaling down would make it fuzzy.)
1269 EXPECT_TRUE(ops[0].scale_factor == 0 || ops[0].scale_factor == 2);
1271 // All other operations must be 50% downscales.
1272 EXPECT_EQ(ops[i].scale_factor, 2);
1275 // Check that the scale factors make sense and add up.
1277 for (int i = static_cast<int>(ops.size() - 1); i >= 0; i--) {
1278 EXPECT_EQ(tmp, ops[i].scale_size);
1279 if (ops[i].scale_factor == 0) {
1281 EXPECT_GT(tmp, src);
1284 tmp *= ops[i].scale_factor;
1287 EXPECT_EQ(tmp, src);
1290 void CheckPipeline2(int xsize,
1294 const std::string& description) {
1295 std::vector<GLHelperScaling::ScalerStage> stages;
1296 helper_scaling_->ConvertScalerOpsToScalerStages(
1297 content::GLHelper::SCALER_QUALITY_GOOD,
1298 gfx::Size(xsize, ysize),
1299 gfx::Rect(0, 0, xsize, ysize),
1300 gfx::Size(dst_xsize, dst_ysize),
1306 EXPECT_EQ(x_ops_.size(), 0U);
1307 EXPECT_EQ(y_ops_.size(), 0U);
1308 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, "");
1309 EXPECT_EQ(PrintStages(stages), description);
1312 void CheckOptimizationsTest() {
1313 // Basic upscale. X and Y should be combined into one pass.
1314 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000));
1315 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000));
1316 CheckPipeline2(1024, 768, 2000, 2000, "1024x768 -> 2000x2000 bilinear\n");
1318 // X scaled 1/2, Y upscaled, should still be one pass.
1319 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512));
1320 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000));
1321 CheckPipeline2(1024, 768, 512, 2000, "1024x768 -> 512x2000 bilinear\n");
1323 // X upscaled, Y scaled 1/2, one bilinear pass
1324 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000));
1325 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384));
1326 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 2000x384 bilinear\n");
1328 // X scaled 1/2, Y scaled 1/2, one bilinear pass
1329 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512));
1330 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384));
1331 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 512x384 bilinear\n");
1333 // X scaled 1/2, Y scaled to 60%, one bilinear2 pass.
1334 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50));
1335 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1336 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1337 CheckPipeline2(100, 100, 50, 60, "100x100 -> 50x60 bilinear2 Y\n");
1339 // X scaled to 60%, Y scaled 1/2, one bilinear2 pass.
1340 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1341 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1342 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 50));
1343 CheckPipeline2(100, 100, 50, 60, "100x100 -> 60x50 bilinear2 X\n");
1345 // X scaled to 60%, Y scaled 60%, one bilinear2x2 pass.
1346 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1347 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1348 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1349 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1350 CheckPipeline2(100, 100, 60, 60, "100x100 -> 60x60 bilinear2x2\n");
1352 // X scaled to 40%, Y scaled 40%, two bilinear3 passes.
1353 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40));
1354 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40));
1359 "100x100 -> 100x40 bilinear3 Y\n"
1360 "100x40 -> 40x40 bilinear3 X\n");
1362 // X scaled to 60%, Y scaled 40%
1363 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1364 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1365 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40));
1370 "100x100 -> 100x40 bilinear3 Y\n"
1371 "100x40 -> 60x40 bilinear2 X\n");
1373 // X scaled to 40%, Y scaled 60%
1374 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40));
1375 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1376 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1381 "100x100 -> 100x60 bilinear2 Y\n"
1382 "100x60 -> 40x60 bilinear3 X\n");
1384 // X scaled to 30%, Y scaled 30%
1385 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1386 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1387 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 30));
1388 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1389 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1390 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1395 "100x100 -> 100x30 bilinear4 Y\n"
1396 "100x30 -> 30x30 bilinear4 X\n");
1398 // X scaled to 50%, Y scaled 30%
1399 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50));
1400 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1401 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1402 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1403 CheckPipeline2(100, 100, 50, 30, "100x100 -> 50x30 bilinear4 Y\n");
1405 // X scaled to 150%, Y scaled 30%
1406 // Note that we avoid combinding X and Y passes
1407 // as that would probably be LESS efficient here.
1408 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 150));
1409 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1410 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1411 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1416 "100x100 -> 100x30 bilinear4 Y\n"
1417 "100x30 -> 150x30 bilinear\n");
1419 // X scaled to 1%, Y scaled 1%
1420 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 128));
1421 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 64));
1422 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 32));
1423 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 16));
1424 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 8));
1425 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 4));
1426 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 2));
1427 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 1));
1428 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 128));
1429 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 64));
1430 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 32));
1431 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 16));
1432 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 8));
1433 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 4));
1434 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 2));
1435 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 1));
1440 "100x100 -> 100x32 bilinear4 Y\n"
1441 "100x32 -> 100x4 bilinear4 Y\n"
1442 "100x4 -> 64x1 bilinear2x2\n"
1443 "64x1 -> 8x1 bilinear4 X\n"
1444 "8x1 -> 1x1 bilinear4 X\n");
1447 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context_;
1448 gpu::ContextSupport* context_support_;
1449 scoped_ptr<content::GLHelper> helper_;
1450 scoped_ptr<content::GLHelperScaling> helper_scaling_;
1451 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_;
1454 TEST_F(GLHelperTest, ARGBSyncReadbackTest) {
1455 const int kTestSize = 64;
1456 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1457 SkBitmap::kARGB_8888_Config,
1459 EXPECT_EQ(result, true);
1462 TEST_F(GLHelperTest, RGB565SyncReadbackTest) {
1463 const int kTestSize = 64;
1464 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1465 SkBitmap::kRGB_565_Config,
1467 EXPECT_EQ(result, true);
1470 TEST_F(GLHelperTest, ARGBASyncReadbackTest) {
1471 const int kTestSize = 64;
1472 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1473 SkBitmap::kARGB_8888_Config,
1475 EXPECT_EQ(result, true);
1478 TEST_F(GLHelperTest, RGB565ASyncReadbackTest) {
1479 const int kTestSize = 64;
1480 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1481 SkBitmap::kRGB_565_Config,
1483 EXPECT_EQ(result, true);
1486 TEST_F(GLHelperTest, YUVReadbackOptTest) {
1487 // This test uses the cb_command tracing events to detect how many
1488 // scaling passes are actually performed by the YUV readback pipeline.
1489 StartTracing(TRACE_DISABLED_BY_DEFAULT("cb_command"));
1491 TestYUVReadback(800,
1500 content::GLHelper::SCALER_QUALITY_FAST);
1502 std::map<std::string, int> event_counts;
1503 EndTracing(&event_counts);
1504 int draw_buffer_calls = event_counts["kDrawBuffersEXTImmediate"];
1505 int draw_arrays_calls = event_counts["kDrawArrays"];
1506 VLOG(1) << "Draw buffer calls: " << draw_buffer_calls;
1507 VLOG(1) << "DrawArrays calls: " << draw_arrays_calls;
1509 if (draw_buffer_calls) {
1510 // When using MRT, the YUV readback code should only
1511 // execute two draw arrays, and scaling should be integrated
1512 // into those two calls since we are using the FAST scalign
1514 EXPECT_EQ(2, draw_arrays_calls);
1516 // When not using MRT, there are three passes for the YUV,
1517 // and one for the scaling.
1518 EXPECT_EQ(4, draw_arrays_calls);
1522 TEST_F(GLHelperTest, YUVReadbackTest) {
1523 int sizes[] = {2, 4, 14};
1524 for (int flip = 0; flip <= 1; flip++) {
1525 for (int use_mrt = 0; use_mrt <= 1; use_mrt++) {
1526 for (unsigned int x = 0; x < arraysize(sizes); x++) {
1527 for (unsigned int y = 0; y < arraysize(sizes); y++) {
1528 for (unsigned int ox = x; ox < arraysize(sizes); ox++) {
1529 for (unsigned int oy = y; oy < arraysize(sizes); oy++) {
1530 // If output is a subsection of the destination frame, (letterbox)
1531 // then try different variations of where the subsection goes.
1532 for (Margin xm = x < ox ? MarginLeft : MarginRight;
1534 xm = NextMargin(xm)) {
1535 for (Margin ym = y < oy ? MarginLeft : MarginRight;
1537 ym = NextMargin(ym)) {
1538 for (int pattern = 0; pattern < 3; pattern++) {
1539 TestYUVReadback(sizes[x],
1543 compute_margin(sizes[x], sizes[ox], xm),
1544 compute_margin(sizes[y], sizes[oy], ym),
1548 content::GLHelper::SCALER_QUALITY_GOOD);
1563 // Per pixel tests, all sizes are small so that we can print
1564 // out the generated bitmaps.
1565 TEST_F(GLHelperTest, ScaleTest) {
1566 int sizes[] = {3, 6, 16};
1567 for (int flip = 0; flip <= 1; flip++) {
1568 for (size_t q = 0; q < arraysize(kQualities); q++) {
1569 for (int x = 0; x < 3; x++) {
1570 for (int y = 0; y < 3; y++) {
1571 for (int dst_x = 0; dst_x < 3; dst_x++) {
1572 for (int dst_y = 0; dst_y < 3; dst_y++) {
1573 for (int pattern = 0; pattern < 3; pattern++) {
1593 // Validate that all scaling generates valid pipelines.
1594 TEST_F(GLHelperTest, ValidateScalerPipelines) {
1595 int sizes[] = {7, 99, 128, 256, 512, 719, 720, 721, 1920, 2011, 3217, 4096};
1596 for (size_t q = 0; q < arraysize(kQualities); q++) {
1597 for (size_t x = 0; x < arraysize(sizes); x++) {
1598 for (size_t y = 0; y < arraysize(sizes); y++) {
1599 for (size_t dst_x = 0; dst_x < arraysize(sizes); dst_x++) {
1600 for (size_t dst_y = 0; dst_y < arraysize(sizes); dst_y++) {
1602 q, sizes[x], sizes[y], sizes[dst_x], sizes[dst_y]);
1613 // Make sure we don't create overly complicated pipelines
1614 // for a few common use cases.
1615 TEST_F(GLHelperTest, CheckSpecificPipelines) {
1616 // Upscale should be single pass.
1617 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1622 "1024x700 -> 1280x720 bilinear\n");
1623 // Slight downscale should use BILINEAR2X2.
1624 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1629 "1280x720 -> 1024x700 bilinear2x2\n");
1630 // Most common tab capture pipeline on the Pixel.
1631 // Should be using two BILINEAR3 passes.
1632 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1637 "2560x1476 -> 2560x720 bilinear3 Y\n"
1638 "2560x720 -> 1249x720 bilinear3 X\n");
1641 TEST_F(GLHelperTest, ScalerOpTest) {
1642 for (int allow3 = 0; allow3 <= 1; allow3++) {
1643 for (int dst = 1; dst < 2049; dst += 1 + (dst >> 3)) {
1644 for (int src = 1; src < 2049; src++) {
1645 TestAddOps(src, dst, allow3 == 1, (src & 1) == 1);
1647 LOG(ERROR) << "Failed for src=" << src << " dst=" << dst
1648 << " allow3=" << allow3;
1656 TEST_F(GLHelperTest, CheckOptimizations) {
1657 // Test in baseclass since it is friends with GLHelperScaling
1658 CheckOptimizationsTest();
1663 // These tests needs to run against a proper GL environment, so we
1664 // need to set it up before we can run the tests.
1665 int main(int argc, char** argv) {
1666 CommandLine::Init(argc, argv);
1667 base::TestSuite* suite = new content::ContentTestSuite(argc, argv);
1668 #if defined(OS_MACOSX)
1669 base::mac::ScopedNSAutoreleasePool pool;
1671 #if defined(TOOLKIT_GTK)
1672 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
1674 gpu::ApplyGpuDriverBugWorkarounds(CommandLine::ForCurrentProcess());
1676 content::UnitTestTestSuite runner(suite);
1677 base::MessageLoop message_loop;
1678 return runner.Run();