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 "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"
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
{
56 virtual void SetUp() {
57 WebGraphicsContext3D::Attributes attributes
;
58 bool lose_context_when_out_of_memory
= false;
60 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
61 attributes
, lose_context_when_out_of_memory
);
62 context_
->makeContextCurrent();
63 context_support_
= context_
->GetContextSupport();
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
);
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
,
86 const scoped_refptr
<base::RefCountedString
>& json_events_str
,
87 bool has_more_events
) {
88 if (output
->size() > 1) {
91 output
->append(json_events_str
->data());
92 if (!has_more_events
) {
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
)));
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
));
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;
133 return (a
+ 2) * x3
- (a
+ 3) * x2
+ 1;
135 return a
* x3
- 5 * a
* x2
+ 8 * a
* x
- 4 * a
;
141 // Look up a single R/G/B/A value.
143 int Channel(SkBitmap
* pixels
, int x
, int y
, int c
) {
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
) {
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
) {
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");
188 case GLHelperScaling::SHADER_BILINEAR2
:
189 ret
.append("bilinear2");
192 case GLHelperScaling::SHADER_BILINEAR3
:
193 ret
.append("bilinear3");
196 case GLHelperScaling::SHADER_BILINEAR4
:
197 ret
.append("bilinear4");
200 case GLHelperScaling::SHADER_BILINEAR2X2
:
201 ret
.append("bilinear2x2");
203 case GLHelperScaling::SHADER_BICUBIC_UPSCALE
:
204 ret
.append("bicubic upscale");
207 case GLHelperScaling::SHADER_BICUBIC_HALF_1D
:
208 ret
.append("bicubic 1/2");
211 case GLHelperScaling::SHADER_PLANAR
:
212 ret
.append("planar");
214 case GLHelperScaling::SHADER_YUV_MRT_PASS1
:
215 ret
.append("rgb2yuv pass 1");
217 case GLHelperScaling::SHADER_YUV_MRT_PASS2
:
218 ret
.append("rgb2yuv pass 2");
223 if (scaler_stages
[i
].scale_x
) {
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) {
239 // Is it an exact down-scale (50%, 25%, etc.?)
240 if (scale
== 2.0 * samples
) {
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) {
249 // Multi-sample upscale-downscale combination?
250 if (scale
> samples
/ 2.0 && scale
< samples
) {
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(),
282 EXPECT_EQ(scaler_stages
[scaler_stages
.size() - 1].dst_size
.height(),
285 // Used to verify that up-scales are not attempted after some
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%
293 static_cast<double>(scaler_stages
[i
].src_subrect
.width()) /
294 static_cast<double>(scaler_stages
[i
].dst_size
.width());
296 static_cast<double>(scaler_stages
[i
].src_subrect
.height()) /
297 static_cast<double>(scaler_stages
[i
].dst_size
.height());
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.";
310 case GLHelperScaling::SHADER_BILINEAR
:
311 if (quality
!= content::GLHelper::SCALER_QUALITY_FAST
) {
316 case GLHelperScaling::SHADER_BILINEAR2
:
320 case GLHelperScaling::SHADER_BILINEAR3
:
324 case GLHelperScaling::SHADER_BILINEAR4
:
328 case GLHelperScaling::SHADER_BILINEAR2X2
:
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);
337 EXPECT_EQ(x_scale
, 1.0);
338 EXPECT_LT(y_scale
, 1.0);
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);
346 EXPECT_EQ(x_scale
, 1.0);
347 EXPECT_EQ(y_scale
, 2.0);
352 if (!scaler_stages
[i
].scale_x
) {
353 std::swap(x_samples
, y_samples
);
357 EXPECT_TRUE(CheckScale(x_scale
, x_samples
, scaled_x
))
358 << "x_scale = " << x_scale
;
361 EXPECT_TRUE(CheckScale(y_scale
, y_samples
, scaled_y
))
362 << "y_scale = " << y_scale
;
365 if (x_scale
!= 1.0) {
368 if (y_scale
!= 1.0) {
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
,
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
398 if (std::abs(a
- b
) > maxdiff
) {
399 LOG(ERROR
) << "-------expected--------";
400 PrintChannel(truth
, c
);
401 LOG(ERROR
) << "-------actual--------";
402 PrintChannel(other
, c
);
404 LOG(ERROR
) << "-------before scaling--------";
405 PrintChannel(source
, c
);
407 LOG(ERROR
) << "-----Scaler stages------";
408 LOG(ERROR
) << PrintStages(scaler_stages
);
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
) {
425 int base_x
= static_cast<int>(floorf(x
));
426 int base_y
= static_cast<int>(floorf(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
,
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
;
452 case content::GLHelper::SCALER_QUALITY_BEST
:
453 for (int src_y
= -10; src_y
< input
->height() + 10; ++src_y
) {
455 Bicubic((src_y
+ 0.5f
- dst_y_in_src
) * clamped_yscale
);
456 if (coeff_y
== 0.0f
) {
459 for (int src_x
= -10; src_x
< input
->width() + 10; ++src_x
) {
462 Bicubic((src_x
+ 0.5f
- dst_x_in_src
) * clamped_xscale
);
467 float c
= ChannelAsFloat(input
, src_x
, src_y
, channel
);
473 case content::GLHelper::SCALER_QUALITY_GOOD
: {
474 int xshift
= 0, yshift
= 0;
475 while ((output
->width() << xshift
) < input
->width()) {
478 while ((output
->height() << yshift
) < input
->height()) {
481 int xmag
= 1 << xshift
;
482 int ymag
= 1 << yshift
;
483 if (xmag
== 4 && output
->width() * 3 >= input
->width()) {
486 if (ymag
== 4 && output
->height() * 3 >= input
->height()) {
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
,
501 case content::GLHelper::SCALER_QUALITY_FAST
:
502 value
= Bilinear(input
, dst_x_in_src
, dst_y_in_src
, channel
);
510 static_cast<int>(value
* 255.0f
+ 0.5f
));
516 void FlipSKBitmap(SkBitmap
* bitmap
) {
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
));
529 // gl_helper scales recursively, so we'll need to do that
530 // in the reference implementation too.
531 void ScaleSlowRecursive(SkBitmap
* input
,
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
);
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
);
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()) {
564 xtmp
= output
->width();
565 while (xtmp
< input
->width() && xtmp
* 2 != input
->width()) {
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
,
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);
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
],
649 gfx::Size(scaled_xsize
, scaled_ysize
),
652 WebGLId dst_texture
=
653 helper_
->CopyAndScaleTexture(src_texture
,
654 gfx::Size(xsize
, ysize
),
655 gfx::Size(scaled_xsize
, scaled_ysize
),
657 kQualities
[quality
]);
659 SkBitmap output_pixels
;
660 output_pixels
.allocN32Pixels(scaled_xsize
, scaled_ysize
);
662 helper_
->ReadbackTextureSync(
664 gfx::Rect(0, 0, scaled_xsize
, scaled_ysize
),
665 static_cast<unsigned char*>(output_pixels
.getPixels()),
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
.allocN32Pixels(scaled_xsize
, scaled_ysize
);
682 ScaleSlowRecursive(&input_pixels
, &truth_pixels
, kQualities
[quality
]);
683 Compare(&truth_pixels
,
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
,
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
),
710 ValidateScalerStages(kQualities
[quality
],
712 gfx::Size(dst_xsize
, dst_ysize
),
715 "output size: %dx%d "
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
,
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
),
740 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD
,
742 gfx::Size(dst_xsize
, dst_ysize
),
744 EXPECT_EQ(PrintStages(stages
), description
);
747 // Note: Left/Right means Top/Bottom when used for Y dimension.
755 static Margin
NextMargin(Margin m
) {
762 return MarginInvalid
;
764 return MarginInvalid
;
768 int compute_margin(int insize
, int outsize
, Margin m
) {
769 int available
= outsize
- insize
;
772 EXPECT_TRUE(false) << "This should not happen.";
777 return (available
/ 2) & ~1;
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
));
795 static void callcallback(const base::Callback
<void()>& callback
,
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
,
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
<< " "
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
);
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);
846 void DrawGridToBitmap(int w
, int h
,
847 SkColor background_color
,
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
,
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
,
897 SkColorType color_type
) {
898 int c1
= static_cast<int>(component1
);
899 int c2
= static_cast<int>(component2
);
901 switch (color_type
) {
902 case kN32_SkColorType
:
903 result
= (std::abs(c1
- c2
) == 0);
905 case kRGB_565_SkColorType
:
906 result
= (std::abs(c1
- c2
) <= 7);
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())
932 if (bmp1
.width() != bmp2
.width() ||
933 bmp1
.height() != bmp2
.height()) {
934 LOG(ERROR
) << "Bitmap geometry check failure";
937 if (bmp1
.colorType() != bmp2
.colorType())
940 SkAutoLockPixels
lock1(bmp1
);
941 SkAutoLockPixels
lock2(bmp2
);
942 if (!bmp1
.getPixels() || !bmp2
.getPixels()) {
943 LOG(ERROR
) << "Empty Bitmap!";
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
),
951 LOG(ERROR
) << "Bitmap color comparision failure";
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
) ?
966 GLenum type
= (color_type
== kRGB_565_SkColorType
) ?
967 GL_UNSIGNED_SHORT_5_6_5
: GL_UNSIGNED_BYTE
;
968 context_
->texImage2D(GL_TEXTURE_2D
,
976 input_pixels
.getPixels());
979 void ReadBackTexture(GLuint src_texture
,
980 const gfx::Size
& src_size
,
981 unsigned char* pixels
,
982 SkColorType color_type
,
985 base::RunLoop run_loop
;
986 helper_
->ReadbackTextureAsync(src_texture
,
990 base::Bind(&callcallback
,
991 run_loop
.QuitClosure()));
994 helper_
->ReadbackTextureSync(src_texture
,
1001 // Test basic format readback.
1002 bool TestTextureFormatReadback(const gfx::Size
& src_size
,
1003 SkColorType color_type
,
1006 SkImageInfo::Make(src_size
.width(),
1009 kPremul_SkAlphaType
);
1010 if (!helper_
->IsReadbackConfigSupported(color_type
)) {
1011 LOG(INFO
) << "Skipping test format not supported" << color_type
;
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
,
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
);
1033 LOG(ERROR
) << "Bitmap comparision failure Pattern-1";
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(),
1041 src_grid_pitch
, src_grid_width
,
1043 BindAndAttachTextureWithPixels(src_texture
,
1047 ReadBackTexture(src_texture
, src_size
, pixels
, color_type
, async
);
1048 result
= IsEqual(input_pixels
, output_pixels
);
1050 LOG(ERROR
) << "Bitmap comparision failure Pattern-2";
1053 // Test Pattern-3, Fill with CheckerBoard Pattern.
1054 DrawCheckerToBitmap(src_size
.width(),
1057 color2
, rect_w
, rect_h
, input_pixels
);
1058 BindAndAttachTextureWithPixels(src_texture
,
1062 ReadBackTexture(src_texture
, src_size
, pixels
, color_type
, async
);
1063 result
= IsEqual(input_pixels
, output_pixels
);
1065 LOG(ERROR
) << "Bitmap comparision failure Pattern-3";
1068 context_
->deleteTexture(src_texture
);
1075 // YUV readback test. Create a test pattern, convert to YUV
1076 // with reference implementation and compare to what gl_helper
1078 void TestYUVReadback(int xsize
,
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);
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);
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);
1117 context_
->bindTexture(GL_TEXTURE_2D
, src_texture
);
1118 context_
->texImage2D(GL_TEXTURE_2D
,
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 "
1138 "pattern: %d %s %s",
1146 flip
? "flip" : "noflip",
1147 flip
? "mrt" : "nomrt");
1148 scoped_ptr
<ReadbackYUVInterface
> yuv_reader(
1149 helper_
->CreateReadbackPipelineYUV(
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
),
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
,
1177 base::Bind(&callcallback
, run_loop
.QuitClosure()));
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 +
1218 output_frame
->data(media::VideoFrame::kYPlane
),
1224 message
+ " Y plane");
1226 output_frame
->data(media::VideoFrame::kUPlane
),
1232 message
+ " U plane");
1234 output_frame
->data(media::VideoFrame::kVPlane
),
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
);
1258 for (size_t i
= 0; i
< ops
.size(); i
++) {
1259 EXPECT_EQ(ops
[i
].scale_x
, scale_x
);
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);
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.
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) {
1275 EXPECT_GT(tmp
, src
);
1278 tmp
*= ops
[i
].scale_factor
;
1281 EXPECT_EQ(tmp
, src
);
1284 void CheckPipeline2(int xsize
,
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
),
1300 EXPECT_EQ(x_ops_
.size(), 0U);
1301 EXPECT_EQ(y_ops_
.size(), 0U);
1302 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD
,
1304 gfx::Size(dst_xsize
, dst_ysize
),
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));
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));
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));
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));
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));
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));
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
{
1453 gfx::DisableNullDrawGLBindings enable_pixel_output_
;
1456 TEST_F(GLHelperTest
, ARGBSyncReadbackTest
) {
1457 const int kTestSize
= 64;
1458 bool result
= TestTextureFormatReadback(gfx::Size(kTestSize
,kTestSize
),
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
,
1469 EXPECT_EQ(result
, true);
1472 TEST_F(GLHelperTest
, ARGBASyncReadbackTest
) {
1473 const int kTestSize
= 64;
1474 bool result
= TestTextureFormatReadback(gfx::Size(kTestSize
,kTestSize
),
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
,
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,
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
1516 EXPECT_EQ(2, draw_arrays_calls
);
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
;
1536 xm
= NextMargin(xm
)) {
1537 for (Margin ym
= y
< oy
? MarginLeft
: MarginRight
;
1539 ym
= NextMargin(ym
)) {
1540 for (int pattern
= 0; pattern
< 3; pattern
++) {
1541 TestYUVReadback(sizes
[x
],
1545 compute_margin(sizes
[x
], sizes
[ox
], xm
),
1546 compute_margin(sizes
[y
], sizes
[oy
], ym
),
1550 content::GLHelper::SCALER_QUALITY_GOOD
);
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
++) {
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
++) {
1604 q
, sizes
[x
], sizes
[y
], sizes
[dst_x
], sizes
[dst_y
]);
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
,
1624 "1024x700 -> 1280x720 bilinear\n");
1625 // Slight downscale should use BILINEAR2X2.
1626 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD
,
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
,
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);
1649 LOG(ERROR
) << "Failed for src=" << src
<< " dst=" << dst
1650 << " allow3=" << allow3
;
1658 TEST_F(GLHelperTest
, CheckOptimizations
) {
1659 // Test in baseclass since it is friends with GLHelperScaling
1660 CheckOptimizationsTest();
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
;
1674 content::UnitTestTestSuite
runner(suite
);
1675 base::MessageLoop message_loop
;
1676 return runner
.Run();