Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / gl_helper_unittest.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <cmath>
7 #include <string>
8 #include <vector>
9
10 #include <GLES2/gl2.h>
11 #include <GLES2/gl2ext.h>
12 #include <GLES2/gl2extchromium.h>
13
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"
36
37 #if defined(OS_MACOSX)
38 #include "base/mac/scoped_nsautorelease_pool.h"
39 #endif
40
41 #if defined(TOOLKIT_GTK)
42 #include "ui/gfx/gtk_util.h"
43 #endif
44
45 namespace content {
46
47 using blink::WebGLId;
48 using blink::WebGraphicsContext3D;
49 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
50
51 content::GLHelper::ScalerQuality kQualities[] = {
52     content::GLHelper::SCALER_QUALITY_BEST,
53     content::GLHelper::SCALER_QUALITY_GOOD,
54     content::GLHelper::SCALER_QUALITY_FAST, };
55
56 const char* kQualityNames[] = {"best", "good", "fast", };
57
58 class GLHelperTest : public testing::Test {
59  protected:
60   virtual void SetUp() {
61     WebGraphicsContext3D::Attributes attributes;
62     context_ =
63         WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
64             attributes);
65     context_->makeContextCurrent();
66     context_support_ = context_->GetContextSupport();
67     helper_.reset(
68         new content::GLHelper(context_->GetGLInterface(), context_support_));
69     helper_scaling_.reset(new content::GLHelperScaling(
70         context_->GetGLInterface(), helper_.get()));
71   }
72
73   virtual void TearDown() {
74     helper_scaling_.reset(NULL);
75     helper_.reset(NULL);
76     context_.reset(NULL);
77   }
78
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);
84   }
85
86   static void TraceDataCB(
87       const base::Callback<void()>& callback,
88       std::string* output,
89       const scoped_refptr<base::RefCountedString>& json_events_str,
90       bool has_more_events) {
91     if (output->size() > 1) {
92       output->append(",");
93     }
94     output->append(json_events_str->data());
95     if (!has_more_events) {
96       callback.Run();
97     }
98   }
99
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)));
110     run_loop.Run();
111     json_data.append("]");
112
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));
121         std::string name;
122         CHECK(dict->GetString("name", &name));
123         (*event_counts)[name]++;
124         VLOG(1) << "trace name: " << name;
125       }
126     }
127   }
128
129   // Bicubic filter kernel function.
130   static float Bicubic(float x) {
131     const float a = -0.5;
132     x = std::abs(x);
133     float x2 = x * x;
134     float x3 = x2 * x;
135     if (x <= 1) {
136       return (a + 2) * x3 - (a + 3) * x2 + 1;
137     } else if (x < 2) {
138       return a * x3 - 5 * a * x2 + 8 * a * x - 4 * a;
139     } else {
140       return 0.0f;
141     }
142   }
143
144   // Look up a single R/G/B/A value.
145   // Clamp x/y.
146   int Channel(SkBitmap* pixels, int x, int y, int c) {
147     uint32* data =
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;
151   }
152
153   // Set a single R/G/B/A value.
154   void SetChannel(SkBitmap* pixels, int x, int y, int c, int v) {
155     DCHECK_GE(x, 0);
156     DCHECK_GE(y, 0);
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));
162   }
163
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)));
171       }
172       LOG(ERROR) << formatted;
173     }
174   }
175
176   // Print out the individual steps of a scaler pipeline.
177   std::string PrintStages(
178       const std::vector<GLHelperScaling::ScalerStage>& scaler_stages) {
179     std::string ret;
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");
190           break;
191         case GLHelperScaling::SHADER_BILINEAR2:
192           ret.append("bilinear2");
193           xy_matters = true;
194           break;
195         case GLHelperScaling::SHADER_BILINEAR3:
196           ret.append("bilinear3");
197           xy_matters = true;
198           break;
199         case GLHelperScaling::SHADER_BILINEAR4:
200           ret.append("bilinear4");
201           xy_matters = true;
202           break;
203         case GLHelperScaling::SHADER_BILINEAR2X2:
204           ret.append("bilinear2x2");
205           break;
206         case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
207           ret.append("bicubic upscale");
208           xy_matters = true;
209           break;
210         case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
211           ret.append("bicubic 1/2");
212           xy_matters = true;
213           break;
214         case GLHelperScaling::SHADER_PLANAR:
215           ret.append("planar");
216           break;
217         case GLHelperScaling::SHADER_YUV_MRT_PASS1:
218           ret.append("rgb2yuv pass 1");
219           break;
220         case GLHelperScaling::SHADER_YUV_MRT_PASS2:
221           ret.append("rgb2yuv pass 2");
222           break;
223       }
224
225       if (xy_matters) {
226         if (scaler_stages[i].scale_x) {
227           ret.append(" X");
228         } else {
229           ret.append(" Y");
230         }
231       }
232       ret.append("\n");
233     }
234     return ret;
235   }
236
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) {
240       return true;
241     }
242     // Is it an exact down-scale (50%, 25%, etc.?)
243     if (scale == 2.0 * samples) {
244       return true;
245     }
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) {
250         return true;
251       }
252       // Multi-sample upscale-downscale combination?
253       if (scale > samples / 2.0 && scale < samples) {
254         return true;
255       }
256     }
257     return false;
258   }
259
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());
279     }
280
281     // Used to verify that up-scales are not attempted after some
282     // other scale.
283     bool scaled_x = false;
284     bool scaled_y = false;
285
286     for (size_t i = 0; i < scaler_stages.size(); i++) {
287       // Note: 2.0 means scaling down by 50%
288       double x_scale =
289           static_cast<double>(scaler_stages[i].src_subrect.width()) /
290           static_cast<double>(scaler_stages[i].dst_size.width());
291       double y_scale =
292           static_cast<double>(scaler_stages[i].src_subrect.height()) /
293           static_cast<double>(scaler_stages[i].dst_size.height());
294
295       int x_samples = 0;
296       int y_samples = 0;
297
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.";
304           break;
305
306         case GLHelperScaling::SHADER_BILINEAR:
307           if (quality != content::GLHelper::SCALER_QUALITY_FAST) {
308             x_samples = 1;
309             y_samples = 1;
310           }
311           break;
312         case GLHelperScaling::SHADER_BILINEAR2:
313           x_samples = 2;
314           y_samples = 1;
315           break;
316         case GLHelperScaling::SHADER_BILINEAR3:
317           x_samples = 3;
318           y_samples = 1;
319           break;
320         case GLHelperScaling::SHADER_BILINEAR4:
321           x_samples = 4;
322           y_samples = 1;
323           break;
324         case GLHelperScaling::SHADER_BILINEAR2X2:
325           x_samples = 2;
326           y_samples = 2;
327           break;
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);
332           } else {
333             EXPECT_EQ(x_scale, 1.0);
334             EXPECT_LT(y_scale, 1.0);
335           }
336           break;
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);
341           } else {
342             EXPECT_EQ(x_scale, 1.0);
343             EXPECT_EQ(y_scale, 2.0);
344           }
345           break;
346       }
347
348       if (!scaler_stages[i].scale_x) {
349         std::swap(x_samples, y_samples);
350       }
351
352       if (x_samples) {
353         EXPECT_TRUE(CheckScale(x_scale, x_samples, scaled_x))
354             << "x_scale = " << x_scale;
355       }
356       if (y_samples) {
357         EXPECT_TRUE(CheckScale(y_scale, y_samples, scaled_y))
358             << "y_scale = " << y_scale;
359       }
360
361       if (x_scale != 1.0) {
362         scaled_x = true;
363       }
364       if (y_scale != 1.0) {
365         scaled_y = true;
366       }
367     }
368
369     if (HasFailure() && !previous_error) {
370       LOG(ERROR) << "Invalid scaler stages: " << message;
371       LOG(ERROR) << "Scaler stages:";
372       LOG(ERROR) << PrintStages(scaler_stages);
373     }
374   }
375
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,
380                SkBitmap* other,
381                int maxdiff,
382                SkBitmap* source,
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
393                                      << " " << message;
394           if (std::abs(a - b) > maxdiff) {
395             LOG(ERROR) << "-------expected--------";
396             PrintChannel(truth, c);
397             LOG(ERROR) << "-------actual--------";
398             PrintChannel(other, c);
399             if (source) {
400               LOG(ERROR) << "-------before scaling--------";
401               PrintChannel(source, c);
402             }
403             LOG(ERROR) << "-----Scaler stages------";
404             LOG(ERROR) << PrintStages(scaler_stages);
405             return;
406           }
407         }
408       }
409     }
410   }
411
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;
415   }
416
417   // Works like a GL_LINEAR lookup on an SkBitmap.
418   float Bilinear(SkBitmap* pixels, float x, float y, int c) {
419     x -= 0.5;
420     y -= 0.5;
421     int base_x = static_cast<int>(floorf(x));
422     int base_y = static_cast<int>(floorf(y));
423     x -= base_x;
424     y -= base_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);
429   }
430
431   // Very slow bicubic / bilinear scaler for reference.
432   void ScaleSlow(SkBitmap* input,
433                  SkBitmap* output,
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;
444
445           float value = 0.0f;
446           float sum = 0.0f;
447           switch (quality) {
448             case content::GLHelper::SCALER_QUALITY_BEST:
449               for (int src_y = -10; src_y < input->height() + 10; ++src_y) {
450                 float coeff_y =
451                     Bicubic((src_y + 0.5f - dst_y_in_src) * clamped_yscale);
452                 if (coeff_y == 0.0f) {
453                   continue;
454                 }
455                 for (int src_x = -10; src_x < input->width() + 10; ++src_x) {
456                   float coeff =
457                       coeff_y *
458                       Bicubic((src_x + 0.5f - dst_x_in_src) * clamped_xscale);
459                   if (coeff == 0.0f) {
460                     continue;
461                   }
462                   sum += coeff;
463                   float c = ChannelAsFloat(input, src_x, src_y, channel);
464                   value += c * coeff;
465                 }
466               }
467               break;
468
469             case content::GLHelper::SCALER_QUALITY_GOOD: {
470               int xshift = 0, yshift = 0;
471               while ((output->width() << xshift) < input->width()) {
472                 xshift++;
473               }
474               while ((output->height() << yshift) < input->height()) {
475                 yshift++;
476               }
477               int xmag = 1 << xshift;
478               int ymag = 1 << yshift;
479               if (xmag == 4 && output->width() * 3 >= input->width()) {
480                 xmag = 3;
481               }
482               if (ymag == 4 && output->height() * 3 >= input->height()) {
483                 ymag = 3;
484               }
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,
490                                     channel);
491                   sum += 1.0;
492                 }
493               }
494               break;
495             }
496
497             case content::GLHelper::SCALER_QUALITY_FAST:
498               value = Bilinear(input, dst_x_in_src, dst_y_in_src, channel);
499               sum = 1.0;
500           }
501           value /= sum;
502           SetChannel(output,
503                      dst_x,
504                      dst_y,
505                      channel,
506                      static_cast<int>(value * 255.0f + 0.5f));
507         }
508       }
509     }
510   }
511
512   void FlipSKBitmap(SkBitmap* bitmap) {
513     int top_line = 0;
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));
519       }
520       top_line++;
521       bottom_line--;
522     }
523   }
524
525   // gl_helper scales recursively, so we'll need to do that
526   // in the reference implementation too.
527   void ScaleSlowRecursive(SkBitmap* input,
528                           SkBitmap* output,
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);
533       return;
534     }
535
536     float xscale = static_cast<float>(output->width()) / input->width();
537
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);
546       return;
547     }
548
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();
553
554     if (output->height() != input->height()) {
555       ytmp = output->height();
556       while (ytmp < input->height() && ytmp * 2 != input->height()) {
557         ytmp += ytmp;
558       }
559     } else {
560       xtmp = output->width();
561       while (xtmp < input->width() && xtmp * 2 != input->width()) {
562         xtmp += xtmp;
563       }
564     }
565
566     SkBitmap tmp;
567     tmp.setConfig(SkBitmap::kARGB_8888_Config, xtmp, ytmp);
568     tmp.allocPixels();
569     SkAutoLockPixels lock(tmp);
570
571     ScaleSlowRecursive(input, &tmp, quality);
572     ScaleSlowRecursive(&tmp, output, quality);
573   }
574
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,
578                  int ysize,
579                  int scaled_xsize,
580                  int scaled_ysize,
581                  int test_pattern,
582                  size_t quality,
583                  bool flip) {
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);
590
591     for (int x = 0; x < xsize; ++x) {
592       for (int y = 0; y < ysize; ++y) {
593         switch (test_pattern) {
594           case 0:  // Smooth test pattern
595             SetChannel(&input_pixels, x, y, 0, x * 10);
596             SetChannel(&input_pixels, x, y, 1, y * 10);
597             SetChannel(&input_pixels, x, y, 2, (x + y) * 10);
598             SetChannel(&input_pixels, x, y, 3, 255);
599             break;
600           case 1:  // Small blocks
601             SetChannel(&input_pixels, x, y, 0, x & 1 ? 255 : 0);
602             SetChannel(&input_pixels, x, y, 1, y & 1 ? 255 : 0);
603             SetChannel(&input_pixels, x, y, 2, (x + y) & 1 ? 255 : 0);
604             SetChannel(&input_pixels, x, y, 3, 255);
605             break;
606           case 2:  // Medium blocks
607             SetChannel(&input_pixels, x, y, 0, 10 + x / 2 * 50);
608             SetChannel(&input_pixels, x, y, 1, 10 + y / 3 * 50);
609             SetChannel(&input_pixels, x, y, 2, (x + y) / 5 * 50 + 5);
610             SetChannel(&input_pixels, x, y, 3, 255);
611             break;
612         }
613       }
614     }
615
616     context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
617     context_->bindTexture(GL_TEXTURE_2D, src_texture);
618     context_->texImage2D(GL_TEXTURE_2D,
619                          0,
620                          GL_RGBA,
621                          xsize,
622                          ysize,
623                          0,
624                          GL_RGBA,
625                          GL_UNSIGNED_BYTE,
626                          input_pixels.getPixels());
627
628     std::string message = base::StringPrintf(
629         "input size: %dx%d "
630         "output size: %dx%d "
631         "pattern: %d quality: %s",
632         xsize,
633         ysize,
634         scaled_xsize,
635         scaled_ysize,
636         test_pattern,
637         kQualityNames[quality]);
638
639     std::vector<GLHelperScaling::ScalerStage> stages;
640     helper_scaling_->ComputeScalerStages(kQualities[quality],
641                                          gfx::Size(xsize, ysize),
642                                          gfx::Rect(0, 0, xsize, ysize),
643                                          gfx::Size(scaled_xsize, scaled_ysize),
644                                          flip,
645                                          false,
646                                          &stages);
647     ValidateScalerStages(kQualities[quality], stages, message);
648
649     WebGLId dst_texture =
650         helper_->CopyAndScaleTexture(src_texture,
651                                      gfx::Size(xsize, ysize),
652                                      gfx::Size(scaled_xsize, scaled_ysize),
653                                      flip,
654                                      kQualities[quality]);
655
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);
661
662     helper_->ReadbackTextureSync(
663         dst_texture,
664         gfx::Rect(0, 0, scaled_xsize, scaled_ysize),
665         static_cast<unsigned char*>(output_pixels.getPixels()),
666         SkBitmap::kARGB_8888_Config);
667     if (flip) {
668       // Flip the pixels back.
669       FlipSKBitmap(&output_pixels);
670     }
671     if (xsize == scaled_xsize && ysize == scaled_ysize) {
672       Compare(&input_pixels,
673               &output_pixels,
674               2,
675               NULL,
676               stages,
677               message + " comparing against input");
678     }
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);
684
685     ScaleSlowRecursive(&input_pixels, &truth_pixels, kQualities[quality]);
686     Compare(&truth_pixels,
687             &output_pixels,
688             2,
689             &input_pixels,
690             stages,
691             message + " comparing against scaled");
692
693     context_->deleteTexture(src_texture);
694     context_->deleteTexture(dst_texture);
695     context_->deleteFramebuffer(framebuffer);
696   }
697
698   // Create a scaling pipeline and check that it is made up of
699   // valid scaling operations.
700   void TestScalerPipeline(size_t quality,
701                           int xsize,
702                           int ysize,
703                           int dst_xsize,
704                           int dst_ysize) {
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),
710                                          false,
711                                          false,
712                                          &stages);
713     ValidateScalerStages(kQualities[quality],
714                          stages,
715                          base::StringPrintf(
716                              "input size: %dx%d "
717                              "output size: %dx%d "
718                              "quality: %s",
719                              xsize,
720                              ysize,
721                              dst_xsize,
722                              dst_ysize,
723                              kQualityNames[quality]));
724   }
725
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,
729                      int xsize,
730                      int ysize,
731                      int dst_xsize,
732                      int dst_ysize,
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),
739                                          false,
740                                          false,
741                                          &stages);
742     ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, "");
743     EXPECT_EQ(PrintStages(stages), description);
744   }
745
746   // Note: Left/Right means Top/Bottom when used for Y dimension.
747   enum Margin {
748     MarginLeft,
749     MarginMiddle,
750     MarginRight,
751     MarginInvalid,
752   };
753
754   static Margin NextMargin(Margin m) {
755     switch (m) {
756       case MarginLeft:
757         return MarginMiddle;
758       case MarginMiddle:
759         return MarginRight;
760       case MarginRight:
761         return MarginInvalid;
762       default:
763         return MarginInvalid;
764     }
765   }
766
767   int compute_margin(int insize, int outsize, Margin m) {
768     int available = outsize - insize;
769     switch (m) {
770       default:
771         EXPECT_TRUE(false) << "This should not happen.";
772         return 0;
773       case MarginLeft:
774         return 0;
775       case MarginMiddle:
776         return (available / 2) & ~1;
777       case MarginRight:
778         return available;
779     }
780   }
781
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));
785     if (ret < 0) {
786       return 0;
787     }
788     if (ret > 255) {
789       return 255;
790     }
791     return ret;
792   }
793
794   static void callcallback(const base::Callback<void()>& callback,
795                            bool result) {
796     callback.Run();
797   }
798
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]));
804       }
805       LOG(ERROR) << formatted << "   (" << (plane + y * stride) << ")";
806     }
807   }
808
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,
813                     int maxdiff,
814                     int xsize,
815                     int stride,
816                     int ysize,
817                     SkBitmap* source,
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 << " "
825                                    << message;
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);
831           if (source) {
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);
838           }
839           return;
840         }
841       }
842     }
843   }
844
845   void DrawGridToBitmap(int w, int h,
846                         SkColor background_color,
847                         SkColor grid_color,
848                         int grid_pitch,
849                         int grid_width,
850                         SkBitmap& bmp) {
851     ASSERT_GT(grid_pitch, 0);
852     ASSERT_GT(grid_width, 0);
853     ASSERT_NE(background_color, grid_color);
854
855     for (int y = 0; y < h; ++y) {
856       bool y_on_grid = ((y % grid_pitch) < grid_width);
857
858       for (int x = 0; x < w; ++x) {
859         bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width));
860
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);
865         }
866       }
867     }
868   }
869
870   void DrawCheckerToBitmap(int w, int h,
871                            SkColor color1, SkColor color2,
872                            int rect_w, int rect_h,
873                            SkBitmap& bmp) {
874     ASSERT_GT(rect_w, 0);
875     ASSERT_GT(rect_h, 0);
876     ASSERT_NE(color1, color2);
877
878     for (int y = 0; y < h; ++y) {
879       bool y_bit = (((y / rect_h) & 0x1) == 0);
880
881       for (int x = 0; x < w; ++x) {
882         bool x_bit = (((x / rect_w) & 0x1) == 0);
883
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);
889         }
890       }
891     }
892   }
893
894   bool ColorComponentsClose(SkColor component1,
895                             SkColor component2,
896                             SkBitmap::Config config) {
897     int c1 = static_cast<int>(component1);
898     int c2 = static_cast<int>(component2);
899     bool result = false;
900     switch (config) {
901       case SkBitmap::kARGB_8888_Config:
902         result = (std::abs(c1 - c2) == 0);
903         break;
904       case SkBitmap::kRGB_565_Config:
905         result = (std::abs(c1 - c2) <= 7);
906         break;
907       default:
908         break;
909     }
910     return result;
911   }
912
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;
924     }
925     return red && blue && green && alpha;
926   }
927
928   bool IsEqual(const SkBitmap& bmp1, const SkBitmap& bmp2) {
929     if (bmp1.isNull() && bmp2.isNull())
930       return true;
931     if (bmp1.width() != bmp2.width() ||
932         bmp1.height() != bmp2.height()) {
933         LOG(ERROR) << "Bitmap geometry check failure";
934         return false;
935     }
936     if (bmp1.getConfig() != bmp2.getConfig())
937       return false;
938
939     SkAutoLockPixels lock1(bmp1);
940     SkAutoLockPixels lock2(bmp2);
941     if (!bmp1.getPixels() || !bmp2.getPixels()) {
942       LOG(ERROR) << "Empty Bitmap!";
943       return false;
944     }
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),
948                          bmp2.getColor(x,y),
949                          bmp1.getConfig())) {
950           LOG(ERROR) << "Bitmap color comparision failure";
951           return false;
952         }
953       }
954     }
955     return true;
956   }
957
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) ?
964                     GL_RGB : GL_RGBA;
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,
968                          0,
969                          format,
970                          src_size.width(),
971                          src_size.height(),
972                          0,
973                          format,
974                          type,
975                          input_pixels.getPixels());
976   }
977
978   void ReadBackTexture(GLuint src_texture,
979                        const gfx::Size& src_size,
980                        unsigned char* pixels,
981                        SkBitmap::Config bitmap_config,
982                        bool async) {
983     if (async) {
984       base::RunLoop run_loop;
985       helper_->ReadbackTextureAsync(src_texture,
986                                     src_size,
987                                     pixels,
988                                     bitmap_config,
989                                     base::Bind(&callcallback,
990                                                run_loop.QuitClosure()));
991       run_loop.Run();
992     } else {
993       helper_->ReadbackTextureSync(src_texture,
994                                    gfx::Rect(src_size),
995                                    pixels,
996                                    bitmap_config);
997     }
998   }
999
1000   // Test basic format readback.
1001   bool TestTextureFormatReadback(const gfx::Size& src_size,
1002                          SkBitmap::Config bitmap_config,
1003                          bool async) {
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";
1010       return true;
1011     }
1012     WebGLId src_texture = context_->createTexture();
1013     SkBitmap input_pixels;
1014     input_pixels.setConfig(bitmap_config, src_size.width(),
1015                            src_size.height());
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,
1022                                    bitmap_config,
1023                                    src_size,
1024                                    input_pixels);
1025     SkBitmap output_pixels;
1026     output_pixels.setConfig(bitmap_config, src_size.width(),
1027                            src_size.height());
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);
1036     if (!result) {
1037       LOG(ERROR) << "Bitmap comparision failure Pattern-1";
1038       return false;
1039     }
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(),
1044                    color2, color1,
1045                    src_grid_pitch, src_grid_width,
1046                    input_pixels);
1047     BindAndAttachTextureWithPixels(src_texture,
1048                                    bitmap_config,
1049                                    src_size,
1050                                    input_pixels);
1051     ReadBackTexture(src_texture, src_size, pixels, bitmap_config, async);
1052     result = IsEqual(input_pixels, output_pixels);
1053     if (!result) {
1054       LOG(ERROR) << "Bitmap comparision failure Pattern-2";
1055       return false;
1056     }
1057     // Test Pattern-3, Fill with CheckerBoard Pattern.
1058     DrawCheckerToBitmap(src_size.width(),
1059                     src_size.height(),
1060                     color1,
1061                     color2, rect_w, rect_h, input_pixels);
1062     BindAndAttachTextureWithPixels(src_texture,
1063                                    bitmap_config,
1064                                    src_size,
1065                                    input_pixels);
1066     ReadBackTexture(src_texture, src_size, pixels, bitmap_config, async);
1067     result = IsEqual(input_pixels, output_pixels);
1068     if (!result) {
1069       LOG(ERROR) << "Bitmap comparision failure Pattern-3";
1070       return false;
1071     }
1072     context_->deleteTexture(src_texture);
1073     if (HasFailure()) {
1074       return false;
1075     }
1076     return true;
1077   }
1078
1079   // YUV readback test. Create a test pattern, convert to YUV
1080   // with reference implementation and compare to what gl_helper
1081   // returns.
1082   void TestYUVReadback(int xsize,
1083                        int ysize,
1084                        int output_xsize,
1085                        int output_ysize,
1086                        int xmargin,
1087                        int ymargin,
1088                        int test_pattern,
1089                        bool flip,
1090                        bool use_mrt,
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);
1097
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);
1106             break;
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);
1112             break;
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);
1118             break;
1119         }
1120       }
1121     }
1122
1123     context_->bindTexture(GL_TEXTURE_2D, src_texture);
1124     context_->texImage2D(GL_TEXTURE_2D,
1125                          0,
1126                          GL_RGBA,
1127                          xsize,
1128                          ysize,
1129                          0,
1130                          GL_RGBA,
1131                          GL_UNSIGNED_BYTE,
1132                          input_pixels.getPixels());
1133
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();
1139
1140     std::string message = base::StringPrintf(
1141         "input size: %dx%d "
1142         "output size: %dx%d "
1143         "margin: %dx%d "
1144         "pattern: %d %s %s",
1145         xsize,
1146         ysize,
1147         output_xsize,
1148         output_ysize,
1149         xmargin,
1150         ymargin,
1151         test_pattern,
1152         flip ? "flip" : "noflip",
1153         flip ? "mrt" : "nomrt");
1154     scoped_ptr<ReadbackYUVInterface> yuv_reader(
1155         helper_->CreateReadbackPipelineYUV(
1156             quality,
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),
1161             flip,
1162             use_mrt));
1163
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));
1178
1179     base::RunLoop run_loop;
1180     yuv_reader->ReadbackYUV(mailbox,
1181                             sync_point,
1182                             output_frame.get(),
1183                             base::Bind(&callcallback, run_loop.QuitClosure()));
1184     run_loop.Run();
1185
1186     if (flip) {
1187       FlipSKBitmap(&input_pixels);
1188     }
1189
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);
1199
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);
1206       }
1207     }
1208
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 +
1219             0.5);
1220       }
1221     }
1222
1223     ComparePlane(Y,
1224                  output_frame->data(media::VideoFrame::kYPlane),
1225                  2,
1226                  output_xsize,
1227                  y_stride,
1228                  output_ysize,
1229                  &input_pixels,
1230                  message + " Y plane");
1231     ComparePlane(U,
1232                  output_frame->data(media::VideoFrame::kUPlane),
1233                  2,
1234                  output_xsize / 2,
1235                  u_stride,
1236                  output_ysize / 2,
1237                  &input_pixels,
1238                  message + " U plane");
1239     ComparePlane(V,
1240                  output_frame->data(media::VideoFrame::kVPlane),
1241                  2,
1242                  output_xsize / 2,
1243                  v_stride,
1244                  output_ysize / 2,
1245                  &input_pixels,
1246                  message + " V plane");
1247
1248     context_->deleteTexture(src_texture);
1249   }
1250
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);
1261       return;
1262     }
1263
1264     for (size_t i = 0; i < ops.size(); i++) {
1265       EXPECT_EQ(ops[i].scale_x, scale_x);
1266       if (i == 0) {
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);
1270       } else {
1271         // All other operations must be 50% downscales.
1272         EXPECT_EQ(ops[i].scale_factor, 2);
1273       }
1274     }
1275     // Check that the scale factors make sense and add up.
1276     int tmp = dst;
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) {
1280         EXPECT_EQ(i, 0);
1281         EXPECT_GT(tmp, src);
1282         tmp = src;
1283       } else {
1284         tmp *= ops[i].scale_factor;
1285       }
1286     }
1287     EXPECT_EQ(tmp, src);
1288   }
1289
1290   void CheckPipeline2(int xsize,
1291                       int ysize,
1292                       int dst_xsize,
1293                       int dst_ysize,
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),
1301         false,
1302         false,
1303         &x_ops_,
1304         &y_ops_,
1305         &stages);
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);
1310   }
1311
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");
1317
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");
1322
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");
1327
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");
1332
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");
1338
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");
1344
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");
1351
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));
1355     CheckPipeline2(100,
1356                    100,
1357                    40,
1358                    40,
1359                    "100x100 -> 100x40 bilinear3 Y\n"
1360                    "100x40 -> 40x40 bilinear3 X\n");
1361
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));
1366     CheckPipeline2(100,
1367                    100,
1368                    60,
1369                    40,
1370                    "100x100 -> 100x40 bilinear3 Y\n"
1371                    "100x40 -> 60x40 bilinear2 X\n");
1372
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));
1377     CheckPipeline2(100,
1378                    100,
1379                    40,
1380                    60,
1381                    "100x100 -> 100x60 bilinear2 Y\n"
1382                    "100x60 -> 40x60 bilinear3 X\n");
1383
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));
1391     CheckPipeline2(100,
1392                    100,
1393                    30,
1394                    30,
1395                    "100x100 -> 100x30 bilinear4 Y\n"
1396                    "100x30 -> 30x30 bilinear4 X\n");
1397
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");
1404
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));
1412     CheckPipeline2(100,
1413                    100,
1414                    150,
1415                    30,
1416                    "100x100 -> 100x30 bilinear4 Y\n"
1417                    "100x30 -> 150x30 bilinear\n");
1418
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));
1436     CheckPipeline2(100,
1437                    100,
1438                    30,
1439                    30,
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");
1445   }
1446
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_;
1452 };
1453
1454 TEST_F(GLHelperTest, ARGBSyncReadbackTest) {
1455   const int kTestSize = 64;
1456   bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1457                                           SkBitmap::kARGB_8888_Config,
1458                                           false);
1459   EXPECT_EQ(result, true);
1460 }
1461
1462 TEST_F(GLHelperTest, RGB565SyncReadbackTest) {
1463   const int kTestSize = 64;
1464   bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1465                                           SkBitmap::kRGB_565_Config,
1466                                           false);
1467   EXPECT_EQ(result, true);
1468 }
1469
1470 TEST_F(GLHelperTest, ARGBASyncReadbackTest) {
1471   const int kTestSize = 64;
1472   bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1473                                           SkBitmap::kARGB_8888_Config,
1474                                           true);
1475   EXPECT_EQ(result, true);
1476 }
1477
1478 TEST_F(GLHelperTest, RGB565ASyncReadbackTest) {
1479   const int kTestSize = 64;
1480   bool result = TestTextureFormatReadback(gfx::Size(kTestSize,kTestSize),
1481                                           SkBitmap::kRGB_565_Config,
1482                                           true);
1483   EXPECT_EQ(result, true);
1484 }
1485
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"));
1490
1491   TestYUVReadback(800,
1492                   400,
1493                   800,
1494                   400,
1495                   0,
1496                   0,
1497                   1,
1498                   false,
1499                   true,
1500                   content::GLHelper::SCALER_QUALITY_FAST);
1501
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;
1508
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
1513     // quality.
1514     EXPECT_EQ(2, draw_arrays_calls);
1515   } else {
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);
1519   }
1520 }
1521
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;
1533                    xm <= MarginRight;
1534                    xm = NextMargin(xm)) {
1535                 for (Margin ym = y < oy ? MarginLeft : MarginRight;
1536                      ym <= MarginRight;
1537                      ym = NextMargin(ym)) {
1538                   for (int pattern = 0; pattern < 3; pattern++) {
1539                     TestYUVReadback(sizes[x],
1540                                     sizes[y],
1541                                     sizes[ox],
1542                                     sizes[oy],
1543                                     compute_margin(sizes[x], sizes[ox], xm),
1544                                     compute_margin(sizes[y], sizes[oy], ym),
1545                                     pattern,
1546                                     flip == 1,
1547                                     use_mrt == 1,
1548                                     content::GLHelper::SCALER_QUALITY_GOOD);
1549                     if (HasFailure()) {
1550                       return;
1551                     }
1552                   }
1553                 }
1554               }
1555             }
1556           }
1557         }
1558       }
1559     }
1560   }
1561 }
1562
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++) {
1574                 TestScale(sizes[x],
1575                           sizes[y],
1576                           sizes[dst_x],
1577                           sizes[dst_y],
1578                           pattern,
1579                           q,
1580                           flip == 1);
1581                 if (HasFailure()) {
1582                   return;
1583                 }
1584               }
1585             }
1586           }
1587         }
1588       }
1589     }
1590   }
1591 }
1592
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++) {
1601             TestScalerPipeline(
1602                 q, sizes[x], sizes[y], sizes[dst_x], sizes[dst_y]);
1603             if (HasFailure()) {
1604               return;
1605             }
1606           }
1607         }
1608       }
1609     }
1610   }
1611 }
1612
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,
1618                 1024,
1619                 700,
1620                 1280,
1621                 720,
1622                 "1024x700 -> 1280x720 bilinear\n");
1623   // Slight downscale should use BILINEAR2X2.
1624   CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD,
1625                 1280,
1626                 720,
1627                 1024,
1628                 700,
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,
1633                 2560,
1634                 1476,
1635                 1249,
1636                 720,
1637                 "2560x1476 -> 2560x720 bilinear3 Y\n"
1638                 "2560x720 -> 1249x720 bilinear3 X\n");
1639 }
1640
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);
1646         if (HasFailure()) {
1647           LOG(ERROR) << "Failed for src=" << src << " dst=" << dst
1648                      << " allow3=" << allow3;
1649           return;
1650         }
1651       }
1652     }
1653   }
1654 }
1655
1656 TEST_F(GLHelperTest, CheckOptimizations) {
1657   // Test in baseclass since it is friends with GLHelperScaling
1658   CheckOptimizationsTest();
1659 }
1660
1661 }  // namespace
1662
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;
1670 #endif
1671 #if defined(TOOLKIT_GTK)
1672   gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess());
1673 #endif
1674   gpu::ApplyGpuDriverBugWorkarounds(CommandLine::ForCurrentProcess());
1675
1676   content::UnitTestTestSuite runner(suite);
1677   base::MessageLoop message_loop;
1678   return runner.Run();
1679 }