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_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 "gpu/config/gpu_util.h"
31 #include "media/base/video_frame.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "third_party/skia/include/core/SkBitmap.h"
34 #include "third_party/skia/include/core/SkTypes.h"
35 #include "ui/gl/gl_implementation.h"
36 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
38 #if defined(OS_MACOSX)
39 #include "base/mac/scoped_nsautorelease_pool.h"
42 #if defined(TOOLKIT_GTK)
43 #include "ui/gfx/gtk_util.h"
49 using blink::WebGraphicsContext3D;
50 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
52 content::GLHelper::ScalerQuality kQualities[] = {
53 content::GLHelper::SCALER_QUALITY_BEST,
54 content::GLHelper::SCALER_QUALITY_GOOD,
55 content::GLHelper::SCALER_QUALITY_FAST, };
57 const char* kQualityNames[] = {"best", "good", "fast", };
59 class GLHelperTest : public testing::Test {
61 virtual void SetUp() {
62 WebGraphicsContext3D::Attributes attributes;
63 bool lose_context_when_out_of_memory = false;
65 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
66 attributes, lose_context_when_out_of_memory);
67 context_->makeContextCurrent();
68 context_support_ = context_->GetContextSupport();
70 new content::GLHelper(context_->GetGLInterface(), context_support_));
71 helper_scaling_.reset(new content::GLHelperScaling(
72 context_->GetGLInterface(), helper_.get()));
75 virtual void TearDown() {
76 helper_scaling_.reset(NULL);
81 void StartTracing(const std::string& filter) {
82 base::debug::TraceLog::GetInstance()->SetEnabled(
83 base::debug::CategoryFilter(filter),
84 base::debug::TraceLog::RECORDING_MODE,
85 base::debug::TraceLog::RECORD_UNTIL_FULL);
88 static void TraceDataCB(
89 const base::Callback<void()>& callback,
91 const scoped_refptr<base::RefCountedString>& json_events_str,
92 bool has_more_events) {
93 if (output->size() > 1) {
96 output->append(json_events_str->data());
97 if (!has_more_events) {
102 // End tracing, return tracing data in a simple map
103 // of event name->counts.
104 void EndTracing(std::map<std::string, int>* event_counts) {
105 std::string json_data = "[";
106 base::debug::TraceLog::GetInstance()->SetDisabled();
107 base::RunLoop run_loop;
108 base::debug::TraceLog::GetInstance()->Flush(
109 base::Bind(&GLHelperTest::TraceDataCB,
110 run_loop.QuitClosure(),
111 base::Unretained(&json_data)));
113 json_data.append("]");
115 scoped_ptr<base::Value> trace_data(base::JSONReader::Read(json_data));
116 base::ListValue* list;
117 CHECK(trace_data->GetAsList(&list));
118 for (size_t i = 0; i < list->GetSize(); i++) {
119 base::Value* item = NULL;
120 if (list->Get(i, &item)) {
121 base::DictionaryValue* dict;
122 CHECK(item->GetAsDictionary(&dict));
124 CHECK(dict->GetString("name", &name));
125 (*event_counts)[name]++;
126 VLOG(1) << "trace name: " << name;
131 // Bicubic filter kernel function.
132 static float Bicubic(float x) {
133 const float a = -0.5;
138 return (a + 2) * x3 - (a + 3) * x2 + 1;
140 return a * x3 - 5 * a * x2 + 8 * a * x - 4 * a;
146 // Look up a single R/G/B/A value.
148 int Channel(SkBitmap* pixels, int x, int y, int c) {
150 pixels->getAddr32(std::max(0, std::min(x, pixels->width() - 1)),
151 std::max(0, std::min(y, pixels->height() - 1)));
152 return (*data) >> (c * 8) & 0xff;
155 // Set a single R/G/B/A value.
156 void SetChannel(SkBitmap* pixels, int x, int y, int c, int v) {
159 DCHECK_LT(x, pixels->width());
160 DCHECK_LT(y, pixels->height());
161 uint32* data = pixels->getAddr32(x, y);
162 v = std::max(0, std::min(v, 255));
163 *data = (*data & ~(0xffu << (c * 8))) | (v << (c * 8));
166 // Print all the R, G, B or A values from an SkBitmap in a
167 // human-readable format.
168 void PrintChannel(SkBitmap* pixels, int c) {
169 for (int y = 0; y < pixels->height(); y++) {
170 std::string formatted;
171 for (int x = 0; x < pixels->width(); x++) {
172 formatted.append(base::StringPrintf("%3d, ", Channel(pixels, x, y, c)));
174 LOG(ERROR) << formatted;
178 // Print out the individual steps of a scaler pipeline.
179 std::string PrintStages(
180 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages) {
182 for (size_t i = 0; i < scaler_stages.size(); i++) {
183 ret.append(base::StringPrintf("%dx%d -> %dx%d ",
184 scaler_stages[i].src_size.width(),
185 scaler_stages[i].src_size.height(),
186 scaler_stages[i].dst_size.width(),
187 scaler_stages[i].dst_size.height()));
188 bool xy_matters = false;
189 switch (scaler_stages[i].shader) {
190 case GLHelperScaling::SHADER_BILINEAR:
191 ret.append("bilinear");
193 case GLHelperScaling::SHADER_BILINEAR2:
194 ret.append("bilinear2");
197 case GLHelperScaling::SHADER_BILINEAR3:
198 ret.append("bilinear3");
201 case GLHelperScaling::SHADER_BILINEAR4:
202 ret.append("bilinear4");
205 case GLHelperScaling::SHADER_BILINEAR2X2:
206 ret.append("bilinear2x2");
208 case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
209 ret.append("bicubic upscale");
212 case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
213 ret.append("bicubic 1/2");
216 case GLHelperScaling::SHADER_PLANAR:
217 ret.append("planar");
219 case GLHelperScaling::SHADER_YUV_MRT_PASS1:
220 ret.append("rgb2yuv pass 1");
222 case GLHelperScaling::SHADER_YUV_MRT_PASS2:
223 ret.append("rgb2yuv pass 2");
228 if (scaler_stages[i].scale_x) {
239 bool CheckScale(double scale, int samples, bool already_scaled) {
240 // 1:1 is valid if there is one sample.
241 if (samples == 1 && scale == 1.0) {
244 // Is it an exact down-scale (50%, 25%, etc.?)
245 if (scale == 2.0 * samples) {
248 // Upscales, only valid if we haven't already scaled in this dimension.
249 if (!already_scaled) {
250 // Is it a valid bilinear upscale?
251 if (samples == 1 && scale <= 1.0) {
254 // Multi-sample upscale-downscale combination?
255 if (scale > samples / 2.0 && scale < samples) {
262 // Make sure that the stages of the scaler pipeline are sane.
263 void ValidateScalerStages(
264 content::GLHelper::ScalerQuality quality,
265 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages,
266 const std::string& message) {
267 bool previous_error = HasFailure();
268 // First, check that the input size for each stage is equal to
269 // the output size of the previous stage.
270 for (size_t i = 1; i < scaler_stages.size(); i++) {
271 EXPECT_EQ(scaler_stages[i - 1].dst_size.width(),
272 scaler_stages[i].src_size.width());
273 EXPECT_EQ(scaler_stages[i - 1].dst_size.height(),
274 scaler_stages[i].src_size.height());
275 EXPECT_EQ(scaler_stages[i].src_subrect.x(), 0);
276 EXPECT_EQ(scaler_stages[i].src_subrect.y(), 0);
277 EXPECT_EQ(scaler_stages[i].src_subrect.width(),
278 scaler_stages[i].src_size.width());
279 EXPECT_EQ(scaler_stages[i].src_subrect.height(),
280 scaler_stages[i].src_size.height());
283 // Used to verify that up-scales are not attempted after some
285 bool scaled_x = false;
286 bool scaled_y = false;
288 for (size_t i = 0; i < scaler_stages.size(); i++) {
289 // Note: 2.0 means scaling down by 50%
291 static_cast<double>(scaler_stages[i].src_subrect.width()) /
292 static_cast<double>(scaler_stages[i].dst_size.width());
294 static_cast<double>(scaler_stages[i].src_subrect.height()) /
295 static_cast<double>(scaler_stages[i].dst_size.height());
300 // Codify valid scale operations.
301 switch (scaler_stages[i].shader) {
302 case GLHelperScaling::SHADER_PLANAR:
303 case GLHelperScaling::SHADER_YUV_MRT_PASS1:
304 case GLHelperScaling::SHADER_YUV_MRT_PASS2:
305 EXPECT_TRUE(false) << "Invalid shader.";
308 case GLHelperScaling::SHADER_BILINEAR:
309 if (quality != content::GLHelper::SCALER_QUALITY_FAST) {
314 case GLHelperScaling::SHADER_BILINEAR2:
318 case GLHelperScaling::SHADER_BILINEAR3:
322 case GLHelperScaling::SHADER_BILINEAR4:
326 case GLHelperScaling::SHADER_BILINEAR2X2:
330 case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
331 if (scaler_stages[i].scale_x) {
332 EXPECT_LT(x_scale, 1.0);
333 EXPECT_EQ(y_scale, 1.0);
335 EXPECT_EQ(x_scale, 1.0);
336 EXPECT_LT(y_scale, 1.0);
339 case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
340 if (scaler_stages[i].scale_x) {
341 EXPECT_EQ(x_scale, 2.0);
342 EXPECT_EQ(y_scale, 1.0);
344 EXPECT_EQ(x_scale, 1.0);
345 EXPECT_EQ(y_scale, 2.0);
350 if (!scaler_stages[i].scale_x) {
351 std::swap(x_samples, y_samples);
355 EXPECT_TRUE(CheckScale(x_scale, x_samples, scaled_x))
356 << "x_scale = " << x_scale;
359 EXPECT_TRUE(CheckScale(y_scale, y_samples, scaled_y))
360 << "y_scale = " << y_scale;
363 if (x_scale != 1.0) {
366 if (y_scale != 1.0) {
371 if (HasFailure() && !previous_error) {
372 LOG(ERROR) << "Invalid scaler stages: " << message;
373 LOG(ERROR) << "Scaler stages:";
374 LOG(ERROR) << PrintStages(scaler_stages);
378 // Compare two bitmaps, make sure that each component of each pixel
379 // is no more than |maxdiff| apart. If they are not similar enough,
380 // prints out |truth|, |other|, |source|, |scaler_stages| and |message|.
381 void Compare(SkBitmap* truth,
385 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages,
386 std::string message) {
387 EXPECT_EQ(truth->width(), other->width());
388 EXPECT_EQ(truth->height(), other->height());
389 for (int x = 0; x < truth->width(); x++) {
390 for (int y = 0; y < truth->height(); y++) {
391 for (int c = 0; c < 4; c++) {
392 int a = Channel(truth, x, y, c);
393 int b = Channel(other, x, y, c);
394 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " c=" << c
396 if (std::abs(a - b) > maxdiff) {
397 LOG(ERROR) << "-------expected--------";
398 PrintChannel(truth, c);
399 LOG(ERROR) << "-------actual--------";
400 PrintChannel(other, c);
402 LOG(ERROR) << "-------before scaling--------";
403 PrintChannel(source, c);
405 LOG(ERROR) << "-----Scaler stages------";
406 LOG(ERROR) << PrintStages(scaler_stages);
414 // Get a single R, G, B or A value as a float.
415 float ChannelAsFloat(SkBitmap* pixels, int x, int y, int c) {
416 return Channel(pixels, x, y, c) / 255.0;
419 // Works like a GL_LINEAR lookup on an SkBitmap.
420 float Bilinear(SkBitmap* pixels, float x, float y, int c) {
423 int base_x = static_cast<int>(floorf(x));
424 int base_y = static_cast<int>(floorf(y));
427 return (ChannelAsFloat(pixels, base_x, base_y, c) * (1 - x) * (1 - y) +
428 ChannelAsFloat(pixels, base_x + 1, base_y, c) * x * (1 - y) +
429 ChannelAsFloat(pixels, base_x, base_y + 1, c) * (1 - x) * y +
430 ChannelAsFloat(pixels, base_x + 1, base_y + 1, c) * x * y);
433 // Very slow bicubic / bilinear scaler for reference.
434 void ScaleSlow(SkBitmap* input,
436 content::GLHelper::ScalerQuality quality) {
437 float xscale = static_cast<float>(input->width()) / output->width();
438 float yscale = static_cast<float>(input->height()) / output->height();
439 float clamped_xscale = xscale < 1.0 ? 1.0 : 1.0 / xscale;
440 float clamped_yscale = yscale < 1.0 ? 1.0 : 1.0 / yscale;
441 for (int dst_y = 0; dst_y < output->height(); dst_y++) {
442 for (int dst_x = 0; dst_x < output->width(); dst_x++) {
443 for (int channel = 0; channel < 4; channel++) {
444 float dst_x_in_src = (dst_x + 0.5f) * xscale;
445 float dst_y_in_src = (dst_y + 0.5f) * yscale;
450 case content::GLHelper::SCALER_QUALITY_BEST:
451 for (int src_y = -10; src_y < input->height() + 10; ++src_y) {
453 Bicubic((src_y + 0.5f - dst_y_in_src) * clamped_yscale);
454 if (coeff_y == 0.0f) {
457 for (int src_x = -10; src_x < input->width() + 10; ++src_x) {
460 Bicubic((src_x + 0.5f - dst_x_in_src) * clamped_xscale);
465 float c = ChannelAsFloat(input, src_x, src_y, channel);
471 case content::GLHelper::SCALER_QUALITY_GOOD: {
472 int xshift = 0, yshift = 0;
473 while ((output->width() << xshift) < input->width()) {
476 while ((output->height() << yshift) < input->height()) {
479 int xmag = 1 << xshift;
480 int ymag = 1 << yshift;
481 if (xmag == 4 && output->width() * 3 >= input->width()) {
484 if (ymag == 4 && output->height() * 3 >= input->height()) {
487 for (int x = 0; x < xmag; x++) {
488 for (int y = 0; y < ymag; y++) {
489 value += Bilinear(input,
490 (dst_x * xmag + x + 0.5) * xscale / xmag,
491 (dst_y * ymag + y + 0.5) * yscale / ymag,
499 case content::GLHelper::SCALER_QUALITY_FAST:
500 value = Bilinear(input, dst_x_in_src, dst_y_in_src, channel);
508 static_cast<int>(value * 255.0f + 0.5f));
514 void FlipSKBitmap(SkBitmap* bitmap) {
516 int bottom_line = bitmap->height() - 1;
517 while (top_line < bottom_line) {
518 for (int x = 0; x < bitmap->width(); x++) {
519 std::swap(*bitmap->getAddr32(x, top_line),
520 *bitmap->getAddr32(x, bottom_line));
527 // gl_helper scales recursively, so we'll need to do that
528 // in the reference implementation too.
529 void ScaleSlowRecursive(SkBitmap* input,
531 content::GLHelper::ScalerQuality quality) {
532 if (quality == content::GLHelper::SCALER_QUALITY_FAST ||
533 quality == content::GLHelper::SCALER_QUALITY_GOOD) {
534 ScaleSlow(input, output, quality);
538 float xscale = static_cast<float>(output->width()) / input->width();
540 // This corresponds to all the operations we can do directly.
541 float yscale = static_cast<float>(output->height()) / input->height();
542 if ((xscale == 1.0f && yscale == 1.0f) ||
543 (xscale == 0.5f && yscale == 1.0f) ||
544 (xscale == 1.0f && yscale == 0.5f) ||
545 (xscale >= 1.0f && yscale == 1.0f) ||
546 (xscale == 1.0f && yscale >= 1.0f)) {
547 ScaleSlow(input, output, quality);
551 // Now we break the problem down into smaller pieces, using the
552 // operations available.
553 int xtmp = input->width();
554 int ytmp = input->height();
556 if (output->height() != input->height()) {
557 ytmp = output->height();
558 while (ytmp < input->height() && ytmp * 2 != input->height()) {
562 xtmp = output->width();
563 while (xtmp < input->width() && xtmp * 2 != input->width()) {
569 tmp.setConfig(SkBitmap::kARGB_8888_Config, xtmp, ytmp);
571 SkAutoLockPixels lock(tmp);
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,
586 WebGLId src_texture = context_->createTexture();
587 WebGLId framebuffer = context_->createFramebuffer();
588 SkBitmap input_pixels;
589 input_pixels.setConfig(SkBitmap::kARGB_8888_Config, xsize, ysize);
590 input_pixels.allocPixels();
591 SkAutoLockPixels lock(input_pixels);
593 for (int x = 0; x < xsize; ++x) {
594 for (int y = 0; y < ysize; ++y) {
595 switch (test_pattern) {
596 case 0: // Smooth test pattern
597 SetChannel(&input_pixels, x, y, 0, x * 10);
598 SetChannel(&input_pixels, x, y, 1, y * 10);
599 SetChannel(&input_pixels, x, y, 2, (x + y) * 10);
600 SetChannel(&input_pixels, x, y, 3, 255);
602 case 1: // Small blocks
603 SetChannel(&input_pixels, x, y, 0, x & 1 ? 255 : 0);
604 SetChannel(&input_pixels, x, y, 1, y & 1 ? 255 : 0);
605 SetChannel(&input_pixels, x, y, 2, (x + y) & 1 ? 255 : 0);
606 SetChannel(&input_pixels, x, y, 3, 255);
608 case 2: // Medium blocks
609 SetChannel(&input_pixels, x, y, 0, 10 + x / 2 * 50);
610 SetChannel(&input_pixels, x, y, 1, 10 + y / 3 * 50);
611 SetChannel(&input_pixels, x, y, 2, (x + y) / 5 * 50 + 5);
612 SetChannel(&input_pixels, x, y, 3, 255);
618 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
619 context_->bindTexture(GL_TEXTURE_2D, src_texture);
620 context_->texImage2D(GL_TEXTURE_2D,
628 input_pixels.getPixels());
630 std::string message = base::StringPrintf(
632 "output size: %dx%d "
633 "pattern: %d quality: %s",
639 kQualityNames[quality]);
641 std::vector<GLHelperScaling::ScalerStage> stages;
642 helper_scaling_->ComputeScalerStages(kQualities[quality],
643 gfx::Size(xsize, ysize),
644 gfx::Rect(0, 0, xsize, ysize),
645 gfx::Size(scaled_xsize, scaled_ysize),
649 ValidateScalerStages(kQualities[quality], stages, message);
651 WebGLId dst_texture =
652 helper_->CopyAndScaleTexture(src_texture,
653 gfx::Size(xsize, ysize),
654 gfx::Size(scaled_xsize, scaled_ysize),
656 kQualities[quality]);
658 SkBitmap output_pixels;
659 output_pixels.setConfig(
660 SkBitmap::kARGB_8888_Config, scaled_xsize, scaled_ysize);
661 output_pixels.allocPixels();
662 SkAutoLockPixels output_lock(output_pixels);
664 helper_->ReadbackTextureSync(
666 gfx::Rect(0, 0, scaled_xsize, scaled_ysize),
667 static_cast<unsigned char*>(output_pixels.getPixels()),
668 SkBitmap::kARGB_8888_Config);
670 // Flip the pixels back.
671 FlipSKBitmap(&output_pixels);
673 if (xsize == scaled_xsize && ysize == scaled_ysize) {
674 Compare(&input_pixels,
679 message + " comparing against input");
681 SkBitmap truth_pixels;
682 truth_pixels.setConfig(
683 SkBitmap::kARGB_8888_Config, scaled_xsize, scaled_ysize);
684 truth_pixels.allocPixels();
685 SkAutoLockPixels truth_lock(truth_pixels);
687 ScaleSlowRecursive(&input_pixels, &truth_pixels, kQualities[quality]);
688 Compare(&truth_pixels,
693 message + " comparing against scaled");
695 context_->deleteTexture(src_texture);
696 context_->deleteTexture(dst_texture);
697 context_->deleteFramebuffer(framebuffer);
700 // Create a scaling pipeline and check that it is made up of
701 // valid scaling operations.
702 void TestScalerPipeline(size_t quality,
707 std::vector<GLHelperScaling::ScalerStage> stages;
708 helper_scaling_->ComputeScalerStages(kQualities[quality],
709 gfx::Size(xsize, ysize),
710 gfx::Rect(0, 0, xsize, ysize),
711 gfx::Size(dst_xsize, dst_ysize),
715 ValidateScalerStages(kQualities[quality],
719 "output size: %dx%d "
725 kQualityNames[quality]));
728 // Create a scaling pipeline and make sure that the steps
729 // are exactly the steps we expect.
730 void CheckPipeline(content::GLHelper::ScalerQuality quality,
735 const std::string& description) {
736 std::vector<GLHelperScaling::ScalerStage> stages;
737 helper_scaling_->ComputeScalerStages(quality,
738 gfx::Size(xsize, ysize),
739 gfx::Rect(0, 0, xsize, ysize),
740 gfx::Size(dst_xsize, dst_ysize),
744 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, "");
745 EXPECT_EQ(PrintStages(stages), description);
748 // Note: Left/Right means Top/Bottom when used for Y dimension.
756 static Margin NextMargin(Margin m) {
763 return MarginInvalid;
765 return MarginInvalid;
769 int compute_margin(int insize, int outsize, Margin m) {
770 int available = outsize - insize;
773 EXPECT_TRUE(false) << "This should not happen.";
778 return (available / 2) & ~1;
784 // Convert 0.0 - 1.0 to 0 - 255
785 int float_to_byte(float v) {
786 int ret = static_cast<int>(floorf(v * 255.0f + 0.5f));
796 static void callcallback(const base::Callback<void()>& callback,
801 void PrintPlane(unsigned char* plane, int xsize, int stride, int ysize) {
802 for (int y = 0; y < ysize; y++) {
803 std::string formatted;
804 for (int x = 0; x < xsize; x++) {
805 formatted.append(base::StringPrintf("%3d, ", plane[y * stride + x]));
807 LOG(ERROR) << formatted << " (" << (plane + y * stride) << ")";
811 // Compare two planes make sure that each component of each pixel
812 // is no more than |maxdiff| apart.
813 void ComparePlane(unsigned char* truth,
814 unsigned char* other,
820 std::string message) {
821 int truth_stride = stride;
822 for (int x = 0; x < xsize; x++) {
823 for (int y = 0; y < ysize; y++) {
824 int a = other[y * stride + x];
825 int b = truth[y * stride + x];
826 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " "
828 if (std::abs(a - b) > maxdiff) {
829 LOG(ERROR) << "-------expected--------";
830 PrintPlane(truth, xsize, truth_stride, ysize);
831 LOG(ERROR) << "-------actual--------";
832 PrintPlane(other, xsize, stride, ysize);
834 LOG(ERROR) << "-------before yuv conversion: red--------";
835 PrintChannel(source, 0);
836 LOG(ERROR) << "-------before yuv conversion: green------";
837 PrintChannel(source, 1);
838 LOG(ERROR) << "-------before yuv conversion: blue-------";
839 PrintChannel(source, 2);
847 void DrawGridToBitmap(int w, int h,
848 SkColor background_color,
853 ASSERT_GT(grid_pitch, 0);
854 ASSERT_GT(grid_width, 0);
855 ASSERT_NE(background_color, grid_color);
857 for (int y = 0; y < h; ++y) {
858 bool y_on_grid = ((y % grid_pitch) < grid_width);
860 for (int x = 0; x < w; ++x) {
861 bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width));
863 if (bmp.getConfig() == SkBitmap::kARGB_8888_Config) {
864 *bmp.getAddr32(x, y) = (on_grid ? grid_color : background_color);
865 } else if (bmp.getConfig() == SkBitmap::kRGB_565_Config) {
866 *bmp.getAddr16(x, y) = (on_grid ? grid_color : background_color);
872 void DrawCheckerToBitmap(int w, int h,
873 SkColor color1, SkColor color2,
874 int rect_w, int rect_h,
876 ASSERT_GT(rect_w, 0);
877 ASSERT_GT(rect_h, 0);
878 ASSERT_NE(color1, color2);
880 for (int y = 0; y < h; ++y) {
881 bool y_bit = (((y / rect_h) & 0x1) == 0);
883 for (int x = 0; x < w; ++x) {
884 bool x_bit = (((x / rect_w) & 0x1) == 0);
886 bool use_color2 = (x_bit != y_bit); // xor
887 if (bmp.getConfig() == SkBitmap::kARGB_8888_Config) {
888 *bmp.getAddr32(x, y) = (use_color2 ? color2 : color1);
889 } else if (bmp.getConfig() == SkBitmap::kRGB_565_Config) {
890 *bmp.getAddr16(x, y) = (use_color2 ? color2 : color1);
896 bool ColorComponentsClose(SkColor component1,
898 SkBitmap::Config config) {
899 int c1 = static_cast<int>(component1);
900 int c2 = static_cast<int>(component2);
903 case SkBitmap::kARGB_8888_Config:
904 result = (std::abs(c1 - c2) == 0);
906 case SkBitmap::kRGB_565_Config:
907 result = (std::abs(c1 - c2) <= 7);
915 bool ColorsClose(SkColor color1, SkColor color2, SkBitmap::Config config) {
916 bool red = ColorComponentsClose(SkColorGetR(color1),
917 SkColorGetR(color2), config);
918 bool green = ColorComponentsClose(SkColorGetG(color1),
919 SkColorGetG(color2), config);
920 bool blue = ColorComponentsClose(SkColorGetB(color1),
921 SkColorGetB(color2), config);
922 bool alpha = ColorComponentsClose(SkColorGetA(color1),
923 SkColorGetA(color2), config);
924 if (config == SkBitmap::kRGB_565_Config) {
925 return red && blue && green;
927 return red && blue && green && alpha;
930 bool IsEqual(const SkBitmap& bmp1, const SkBitmap& bmp2) {
931 if (bmp1.isNull() && bmp2.isNull())
933 if (bmp1.width() != bmp2.width() ||
934 bmp1.height() != bmp2.height()) {
935 LOG(ERROR) << "Bitmap geometry check failure";
938 if (bmp1.getConfig() != bmp2.getConfig())
941 SkAutoLockPixels lock1(bmp1);
942 SkAutoLockPixels lock2(bmp2);
943 if (!bmp1.getPixels() || !bmp2.getPixels()) {
944 LOG(ERROR) << "Empty Bitmap!";
947 for (int y = 0; y < bmp1.height(); ++y) {
948 for (int x = 0; x < bmp1.width(); ++x) {
949 if (!ColorsClose(bmp1.getColor(x,y),
952 LOG(ERROR) << "Bitmap color comparision failure";
960 void BindAndAttachTextureWithPixels(GLuint src_texture,
961 SkBitmap::Config bitmap_config,
962 const gfx::Size& src_size,
963 const SkBitmap& input_pixels) {
964 context_->bindTexture(GL_TEXTURE_2D, src_texture);
965 GLenum format = (bitmap_config == SkBitmap::kRGB_565_Config) ?
967 GLenum type = (bitmap_config == SkBitmap::kRGB_565_Config) ?
968 GL_UNSIGNED_SHORT_5_6_5 : GL_UNSIGNED_BYTE;
969 context_->texImage2D(GL_TEXTURE_2D,
977 input_pixels.getPixels());
980 void ReadBackTexture(GLuint src_texture,
981 const gfx::Size& src_size,
982 unsigned char* pixels,
983 SkBitmap::Config bitmap_config,
986 base::RunLoop run_loop;
987 helper_->ReadbackTextureAsync(src_texture,
991 base::Bind(&callcallback,
992 run_loop.QuitClosure()));
995 helper_->ReadbackTextureSync(src_texture,
1002 // Test basic format readback.
1003 bool TestTextureFormatReadback(const gfx::Size& src_size,
1004 SkBitmap::Config bitmap_config,
1006 if (!helper_->IsReadbackConfigSupported(bitmap_config)) {
1007 LOG(INFO) << "Skipping test format not supported" << bitmap_config;
1010 WebGLId src_texture = context_->createTexture();
1011 SkBitmap input_pixels;
1012 input_pixels.setConfig(bitmap_config, src_size.width(),
1014 input_pixels.allocPixels();
1015 SkAutoLockPixels lock1(input_pixels);
1016 // Test Pattern-1, Fill with Plain color pattern.
1017 // Erase the input bitmap with red color.
1018 input_pixels.eraseColor(SK_ColorRED);
1019 BindAndAttachTextureWithPixels(src_texture,
1023 SkBitmap output_pixels;
1024 output_pixels.setConfig(bitmap_config, src_size.width(),
1026 output_pixels.allocPixels();
1027 SkAutoLockPixels lock2(output_pixels);
1028 // Initialize the output bitmap with Green color.
1029 // When the readback is over output bitmap should have the red color.
1030 output_pixels.eraseColor(SK_ColorGREEN);
1031 uint8* pixels = static_cast<uint8*>(output_pixels.getPixels());
1032 ReadBackTexture(src_texture, src_size, pixels, bitmap_config, async);
1033 bool result = IsEqual(input_pixels, output_pixels);
1035 LOG(ERROR) << "Bitmap comparision failure Pattern-1";
1038 const int rect_w = 10, rect_h = 4, src_grid_pitch = 10, src_grid_width = 4;
1039 const SkColor color1 = SK_ColorRED, color2 = SK_ColorBLUE;
1040 // Test Pattern-2, Fill with Grid Pattern.
1041 DrawGridToBitmap(src_size.width(), src_size.height(),
1043 src_grid_pitch, src_grid_width,
1045 BindAndAttachTextureWithPixels(src_texture,
1049 ReadBackTexture(src_texture, src_size, pixels, bitmap_config, async);
1050 result = IsEqual(input_pixels, output_pixels);
1052 LOG(ERROR) << "Bitmap comparision failure Pattern-2";
1055 // Test Pattern-3, Fill with CheckerBoard Pattern.
1056 DrawCheckerToBitmap(src_size.width(),
1059 color2, rect_w, rect_h, input_pixels);
1060 BindAndAttachTextureWithPixels(src_texture,
1064 ReadBackTexture(src_texture, src_size, pixels, bitmap_config, async);
1065 result = IsEqual(input_pixels, output_pixels);
1067 LOG(ERROR) << "Bitmap comparision failure Pattern-3";
1070 context_->deleteTexture(src_texture);
1077 // YUV readback test. Create a test pattern, convert to YUV
1078 // with reference implementation and compare to what gl_helper
1080 void TestYUVReadback(int xsize,
1089 content::GLHelper::ScalerQuality quality) {
1090 WebGLId src_texture = context_->createTexture();
1091 SkBitmap input_pixels;
1092 input_pixels.setConfig(SkBitmap::kARGB_8888_Config, xsize, ysize);
1093 input_pixels.allocPixels();
1094 SkAutoLockPixels lock(input_pixels);
1096 for (int x = 0; x < xsize; ++x) {
1097 for (int y = 0; y < ysize; ++y) {
1098 switch (test_pattern) {
1099 case 0: // Smooth test pattern
1100 SetChannel(&input_pixels, x, y, 0, x * 10);
1101 SetChannel(&input_pixels, x, y, 1, y * 10);
1102 SetChannel(&input_pixels, x, y, 2, (x + y) * 10);
1103 SetChannel(&input_pixels, x, y, 3, 255);
1105 case 1: // Small blocks
1106 SetChannel(&input_pixels, x, y, 0, x & 1 ? 255 : 0);
1107 SetChannel(&input_pixels, x, y, 1, y & 1 ? 255 : 0);
1108 SetChannel(&input_pixels, x, y, 2, (x + y) & 1 ? 255 : 0);
1109 SetChannel(&input_pixels, x, y, 3, 255);
1111 case 2: // Medium blocks
1112 SetChannel(&input_pixels, x, y, 0, 10 + x / 2 * 50);
1113 SetChannel(&input_pixels, x, y, 1, 10 + y / 3 * 50);
1114 SetChannel(&input_pixels, x, y, 2, (x + y) / 5 * 50 + 5);
1115 SetChannel(&input_pixels, x, y, 3, 255);
1121 context_->bindTexture(GL_TEXTURE_2D, src_texture);
1122 context_->texImage2D(GL_TEXTURE_2D,
1130 input_pixels.getPixels());
1132 gpu::Mailbox mailbox;
1133 context_->genMailboxCHROMIUM(mailbox.name);
1134 EXPECT_FALSE(mailbox.IsZero());
1135 context_->produceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name);
1136 uint32 sync_point = context_->insertSyncPoint();
1138 std::string message = base::StringPrintf(
1139 "input size: %dx%d "
1140 "output size: %dx%d "
1142 "pattern: %d %s %s",
1150 flip ? "flip" : "noflip",
1151 flip ? "mrt" : "nomrt");
1152 scoped_ptr<ReadbackYUVInterface> yuv_reader(
1153 helper_->CreateReadbackPipelineYUV(
1155 gfx::Size(xsize, ysize),
1156 gfx::Rect(0, 0, xsize, ysize),
1157 gfx::Size(output_xsize, output_ysize),
1158 gfx::Rect(xmargin, ymargin, xsize, ysize),
1162 scoped_refptr<media::VideoFrame> output_frame =
1163 media::VideoFrame::CreateFrame(
1164 media::VideoFrame::YV12,
1165 gfx::Size(output_xsize, output_ysize),
1166 gfx::Rect(0, 0, output_xsize, output_ysize),
1167 gfx::Size(output_xsize, output_ysize),
1168 base::TimeDelta::FromSeconds(0));
1169 scoped_refptr<media::VideoFrame> truth_frame =
1170 media::VideoFrame::CreateFrame(
1171 media::VideoFrame::YV12,
1172 gfx::Size(output_xsize, output_ysize),
1173 gfx::Rect(0, 0, output_xsize, output_ysize),
1174 gfx::Size(output_xsize, output_ysize),
1175 base::TimeDelta::FromSeconds(0));
1177 base::RunLoop run_loop;
1178 yuv_reader->ReadbackYUV(mailbox,
1181 base::Bind(&callcallback, run_loop.QuitClosure()));
1185 FlipSKBitmap(&input_pixels);
1188 unsigned char* Y = truth_frame->data(media::VideoFrame::kYPlane);
1189 unsigned char* U = truth_frame->data(media::VideoFrame::kUPlane);
1190 unsigned char* V = truth_frame->data(media::VideoFrame::kVPlane);
1191 int32 y_stride = truth_frame->stride(media::VideoFrame::kYPlane);
1192 int32 u_stride = truth_frame->stride(media::VideoFrame::kUPlane);
1193 int32 v_stride = truth_frame->stride(media::VideoFrame::kVPlane);
1194 memset(Y, 0x00, y_stride * output_ysize);
1195 memset(U, 0x80, u_stride * output_ysize / 2);
1196 memset(V, 0x80, v_stride * output_ysize / 2);
1198 for (int y = 0; y < ysize; y++) {
1199 for (int x = 0; x < xsize; x++) {
1200 Y[(y + ymargin) * y_stride + x + xmargin] = float_to_byte(
1201 ChannelAsFloat(&input_pixels, x, y, 0) * 0.257 +
1202 ChannelAsFloat(&input_pixels, x, y, 1) * 0.504 +
1203 ChannelAsFloat(&input_pixels, x, y, 2) * 0.098 + 0.0625);
1207 for (int y = 0; y < ysize / 2; y++) {
1208 for (int x = 0; x < xsize / 2; x++) {
1209 U[(y + ymargin / 2) * u_stride + x + xmargin / 2] = float_to_byte(
1210 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 0) * -0.148 +
1211 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 1) * -0.291 +
1212 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 2) * 0.439 + 0.5);
1213 V[(y + ymargin / 2) * v_stride + x + xmargin / 2] = float_to_byte(
1214 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 0) * 0.439 +
1215 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 1) * -0.368 +
1216 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 2) * -0.071 +
1222 output_frame->data(media::VideoFrame::kYPlane),
1228 message + " Y plane");
1230 output_frame->data(media::VideoFrame::kUPlane),
1236 message + " U plane");
1238 output_frame->data(media::VideoFrame::kVPlane),
1244 message + " V plane");
1246 context_->deleteTexture(src_texture);
1249 void TestAddOps(int src, int dst, bool scale_x, bool allow3) {
1250 std::deque<GLHelperScaling::ScaleOp> ops;
1251 GLHelperScaling::ScaleOp::AddOps(src, dst, scale_x, allow3, &ops);
1252 // Scale factor 3 is a special case.
1253 // It is currently only allowed by itself.
1254 if (allow3 && dst * 3 >= src && dst * 2 < src) {
1255 EXPECT_EQ(ops[0].scale_factor, 3);
1256 EXPECT_EQ(ops.size(), 1U);
1257 EXPECT_EQ(ops[0].scale_x, scale_x);
1258 EXPECT_EQ(ops[0].scale_size, dst);
1262 for (size_t i = 0; i < ops.size(); i++) {
1263 EXPECT_EQ(ops[i].scale_x, scale_x);
1265 // Only the first op is allowed to be a scale up.
1266 // (Scaling up *after* scaling down would make it fuzzy.)
1267 EXPECT_TRUE(ops[0].scale_factor == 0 || ops[0].scale_factor == 2);
1269 // All other operations must be 50% downscales.
1270 EXPECT_EQ(ops[i].scale_factor, 2);
1273 // Check that the scale factors make sense and add up.
1275 for (int i = static_cast<int>(ops.size() - 1); i >= 0; i--) {
1276 EXPECT_EQ(tmp, ops[i].scale_size);
1277 if (ops[i].scale_factor == 0) {
1279 EXPECT_GT(tmp, src);
1282 tmp *= ops[i].scale_factor;
1285 EXPECT_EQ(tmp, src);
1288 void CheckPipeline2(int xsize,
1292 const std::string& description) {
1293 std::vector<GLHelperScaling::ScalerStage> stages;
1294 helper_scaling_->ConvertScalerOpsToScalerStages(
1295 content::GLHelper::SCALER_QUALITY_GOOD,
1296 gfx::Size(xsize, ysize),
1297 gfx::Rect(0, 0, xsize, ysize),
1298 gfx::Size(dst_xsize, dst_ysize),
1304 EXPECT_EQ(x_ops_.size(), 0U);
1305 EXPECT_EQ(y_ops_.size(), 0U);
1306 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, "");
1307 EXPECT_EQ(PrintStages(stages), description);
1310 void CheckOptimizationsTest() {
1311 // Basic upscale. X and Y should be combined into one pass.
1312 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000));
1313 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000));
1314 CheckPipeline2(1024, 768, 2000, 2000, "1024x768 -> 2000x2000 bilinear\n");
1316 // X scaled 1/2, Y upscaled, should still be one pass.
1317 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512));
1318 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000));
1319 CheckPipeline2(1024, 768, 512, 2000, "1024x768 -> 512x2000 bilinear\n");
1321 // X upscaled, Y scaled 1/2, one bilinear pass
1322 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000));
1323 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384));
1324 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 2000x384 bilinear\n");
1326 // X scaled 1/2, Y scaled 1/2, one bilinear pass
1327 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512));
1328 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384));
1329 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 512x384 bilinear\n");
1331 // X scaled 1/2, Y scaled to 60%, one bilinear2 pass.
1332 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50));
1333 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1334 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1335 CheckPipeline2(100, 100, 50, 60, "100x100 -> 50x60 bilinear2 Y\n");
1337 // X scaled to 60%, Y scaled 1/2, one bilinear2 pass.
1338 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1339 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1340 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 50));
1341 CheckPipeline2(100, 100, 50, 60, "100x100 -> 60x50 bilinear2 X\n");
1343 // X scaled to 60%, Y scaled 60%, one bilinear2x2 pass.
1344 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1345 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1346 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1347 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1348 CheckPipeline2(100, 100, 60, 60, "100x100 -> 60x60 bilinear2x2\n");
1350 // X scaled to 40%, Y scaled 40%, two bilinear3 passes.
1351 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40));
1352 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40));
1357 "100x100 -> 100x40 bilinear3 Y\n"
1358 "100x40 -> 40x40 bilinear3 X\n");
1360 // X scaled to 60%, Y scaled 40%
1361 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1362 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1363 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40));
1368 "100x100 -> 100x40 bilinear3 Y\n"
1369 "100x40 -> 60x40 bilinear2 X\n");
1371 // X scaled to 40%, Y scaled 60%
1372 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40));
1373 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1374 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1379 "100x100 -> 100x60 bilinear2 Y\n"
1380 "100x60 -> 40x60 bilinear3 X\n");
1382 // X scaled to 30%, Y scaled 30%
1383 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1384 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1385 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 30));
1386 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1387 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1388 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1393 "100x100 -> 100x30 bilinear4 Y\n"
1394 "100x30 -> 30x30 bilinear4 X\n");
1396 // X scaled to 50%, Y scaled 30%
1397 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50));
1398 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1399 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1400 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1401 CheckPipeline2(100, 100, 50, 30, "100x100 -> 50x30 bilinear4 Y\n");
1403 // X scaled to 150%, Y scaled 30%
1404 // Note that we avoid combinding X and Y passes
1405 // as that would probably be LESS efficient here.
1406 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 150));
1407 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1408 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1409 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1414 "100x100 -> 100x30 bilinear4 Y\n"
1415 "100x30 -> 150x30 bilinear\n");
1417 // X scaled to 1%, Y scaled 1%
1418 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 128));
1419 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 64));
1420 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 32));
1421 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 16));
1422 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 8));
1423 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 4));
1424 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 2));
1425 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 1));
1426 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 128));
1427 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 64));
1428 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 32));
1429 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 16));
1430 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 8));
1431 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 4));
1432 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 2));
1433 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 1));
1438 "100x100 -> 100x32 bilinear4 Y\n"
1439 "100x32 -> 100x4 bilinear4 Y\n"
1440 "100x4 -> 64x1 bilinear2x2\n"
1441 "64x1 -> 8x1 bilinear4 X\n"
1442 "8x1 -> 1x1 bilinear4 X\n");
1445 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context_;
1446 gpu::ContextSupport* context_support_;
1447 scoped_ptr<content::GLHelper> helper_;
1448 scoped_ptr<content::GLHelperScaling> helper_scaling_;
1449 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_;
1452 class GLHelperPixelTest : public GLHelperTest {
1454 gfx::DisableNullDrawGLBindings enable_pixel_output_;
1457 TEST_F(GLHelperTest, ARGBSyncReadbackTest) {
1458 const int kTestSize = 64;
1459 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1460 SkBitmap::kARGB_8888_Config,
1462 EXPECT_EQ(result, true);
1465 TEST_F(GLHelperTest, RGB565SyncReadbackTest) {
1466 const int kTestSize = 64;
1467 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1468 SkBitmap::kRGB_565_Config,
1470 EXPECT_EQ(result, true);
1473 TEST_F(GLHelperTest, ARGBASyncReadbackTest) {
1474 const int kTestSize = 64;
1475 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1476 SkBitmap::kARGB_8888_Config,
1478 EXPECT_EQ(result, true);
1481 TEST_F(GLHelperTest, RGB565ASyncReadbackTest) {
1482 const int kTestSize = 64;
1483 bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1484 SkBitmap::kRGB_565_Config,
1486 EXPECT_EQ(result, true);
1489 TEST_F(GLHelperPixelTest, YUVReadbackOptTest) {
1490 // This test uses the cb_command tracing events to detect how many
1491 // scaling passes are actually performed by the YUV readback pipeline.
1492 StartTracing(TRACE_DISABLED_BY_DEFAULT("cb_command"));
1494 TestYUVReadback(800,
1503 content::GLHelper::SCALER_QUALITY_FAST);
1505 std::map<std::string, int> event_counts;
1506 EndTracing(&event_counts);
1507 int draw_buffer_calls = event_counts["kDrawBuffersEXTImmediate"];
1508 int draw_arrays_calls = event_counts["kDrawArrays"];
1509 VLOG(1) << "Draw buffer calls: " << draw_buffer_calls;
1510 VLOG(1) << "DrawArrays calls: " << draw_arrays_calls;
1512 if (draw_buffer_calls) {
1513 // When using MRT, the YUV readback code should only
1514 // execute two draw arrays, and scaling should be integrated
1515 // into those two calls since we are using the FAST scalign
1517 EXPECT_EQ(2, draw_arrays_calls);
1519 // When not using MRT, there are three passes for the YUV,
1520 // and one for the scaling.
1521 EXPECT_EQ(4, draw_arrays_calls);
1525 TEST_F(GLHelperPixelTest, YUVReadbackTest) {
1526 int sizes[] = {2, 4, 14};
1527 for (int flip = 0; flip <= 1; flip++) {
1528 for (int use_mrt = 0; use_mrt <= 1; use_mrt++) {
1529 for (unsigned int x = 0; x < arraysize(sizes); x++) {
1530 for (unsigned int y = 0; y < arraysize(sizes); y++) {
1531 for (unsigned int ox = x; ox < arraysize(sizes); ox++) {
1532 for (unsigned int oy = y; oy < arraysize(sizes); oy++) {
1533 // If output is a subsection of the destination frame, (letterbox)
1534 // then try different variations of where the subsection goes.
1535 for (Margin xm = x < ox ? MarginLeft : MarginRight;
1537 xm = NextMargin(xm)) {
1538 for (Margin ym = y < oy ? MarginLeft : MarginRight;
1540 ym = NextMargin(ym)) {
1541 for (int pattern = 0; pattern < 3; pattern++) {
1542 TestYUVReadback(sizes[x],
1546 compute_margin(sizes[x], sizes[ox], xm),
1547 compute_margin(sizes[y], sizes[oy], ym),
1551 content::GLHelper::SCALER_QUALITY_GOOD);
1566 // Per pixel tests, all sizes are small so that we can print
1567 // out the generated bitmaps.
1568 TEST_F(GLHelperPixelTest, ScaleTest) {
1569 int sizes[] = {3, 6, 16};
1570 for (int flip = 0; flip <= 1; flip++) {
1571 for (size_t q = 0; q < arraysize(kQualities); q++) {
1572 for (int x = 0; x < 3; x++) {
1573 for (int y = 0; y < 3; y++) {
1574 for (int dst_x = 0; dst_x < 3; dst_x++) {
1575 for (int dst_y = 0; dst_y < 3; dst_y++) {
1576 for (int pattern = 0; pattern < 3; pattern++) {
1596 // Validate that all scaling generates valid pipelines.
1597 TEST_F(GLHelperTest, ValidateScalerPipelines) {
1598 int sizes[] = {7, 99, 128, 256, 512, 719, 720, 721, 1920, 2011, 3217, 4096};
1599 for (size_t q = 0; q < arraysize(kQualities); q++) {
1600 for (size_t x = 0; x < arraysize(sizes); x++) {
1601 for (size_t y = 0; y < arraysize(sizes); y++) {
1602 for (size_t dst_x = 0; dst_x < arraysize(sizes); dst_x++) {
1603 for (size_t dst_y = 0; dst_y < arraysize(sizes); dst_y++) {
1605 q, sizes[x], sizes[y], sizes[dst_x], sizes[dst_y]);
1616 // Make sure we don't create overly complicated pipelines
1617 // for a few common use cases.
1618 TEST_F(GLHelperTest, CheckSpecificPipelines) {
1619 // Upscale should be single pass.
1620 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1625 "1024x700 -> 1280x720 bilinear\n");
1626 // Slight downscale should use BILINEAR2X2.
1627 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1632 "1280x720 -> 1024x700 bilinear2x2\n");
1633 // Most common tab capture pipeline on the Pixel.
1634 // Should be using two BILINEAR3 passes.
1635 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1640 "2560x1476 -> 2560x720 bilinear3 Y\n"
1641 "2560x720 -> 1249x720 bilinear3 X\n");
1644 TEST_F(GLHelperTest, ScalerOpTest) {
1645 for (int allow3 = 0; allow3 <= 1; allow3++) {
1646 for (int dst = 1; dst < 2049; dst += 1 + (dst >> 3)) {
1647 for (int src = 1; src < 2049; src++) {
1648 TestAddOps(src, dst, allow3 == 1, (src & 1) == 1);
1650 LOG(ERROR) << "Failed for src=" << src << " dst=" << dst
1651 << " allow3=" << allow3;
1659 TEST_F(GLHelperTest, CheckOptimizations) {
1660 // Test in baseclass since it is friends with GLHelperScaling
1661 CheckOptimizationsTest();
1666 // These tests needs to run against a proper GL environment, so we
1667 // need to set it up before we can run the tests.
1668 int main(int argc, char** argv) {
1669 CommandLine::Init(argc, argv);
1670 base::TestSuite* suite = new content::ContentTestSuite(argc, argv);
1671 #if defined(OS_MACOSX)
1672 base::mac::ScopedNSAutoreleasePool pool;
1674 #if defined(TOOLKIT_GTK)
1675 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
1677 gpu::ApplyGpuDriverBugWorkarounds(CommandLine::ForCurrentProcess());
1679 content::UnitTestTestSuite runner(suite);
1680 base::MessageLoop message_loop;
1681 return runner.Run();