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