OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 if (flip) { | |
667 // Flip the pixels back. | |
668 FlipSKBitmap(&output_pixels); | |
669 } | |
670 if (xsize == scaled_xsize && ysize == scaled_ysize) { | |
671 Compare(&input_pixels, | |
672 &output_pixels, | |
673 2, | |
674 NULL, | |
675 stages, | |
676 message + " comparing against input"); | |
677 } | |
678 SkBitmap truth_pixels; | |
679 truth_pixels.setConfig( | |
680 SkBitmap::kARGB_8888_Config, scaled_xsize, scaled_ysize); | |
681 truth_pixels.allocPixels(); | |
682 SkAutoLockPixels truth_lock(truth_pixels); | |
683 | |
684 ScaleSlowRecursive(&input_pixels, &truth_pixels, kQualities[quality]); | |
685 Compare(&truth_pixels, | |
686 &output_pixels, | |
687 2, | |
688 &input_pixels, | |
689 stages, | |
690 message + " comparing against scaled"); | |
691 | |
692 context_->deleteTexture(src_texture); | |
693 context_->deleteTexture(dst_texture); | |
694 context_->deleteFramebuffer(framebuffer); | |
695 } | |
696 | |
697 // Create a scaling pipeline and check that it is made up of | |
698 // valid scaling operations. | |
699 void TestScalerPipeline(size_t quality, | |
700 int xsize, | |
701 int ysize, | |
702 int dst_xsize, | |
703 int dst_ysize) { | |
704 std::vector<GLHelperScaling::ScalerStage> stages; | |
705 helper_scaling_->ComputeScalerStages(kQualities[quality], | |
706 gfx::Size(xsize, ysize), | |
707 gfx::Rect(0, 0, xsize, ysize), | |
708 gfx::Size(dst_xsize, dst_ysize), | |
709 false, | |
710 false, | |
711 &stages); | |
712 ValidateScalerStages(kQualities[quality], | |
713 stages, | |
714 base::StringPrintf( | |
715 "input size: %dx%d " | |
716 "output size: %dx%d " | |
717 "quality: %s", | |
718 xsize, | |
719 ysize, | |
720 dst_xsize, | |
721 dst_ysize, | |
722 kQualityNames[quality])); | |
723 } | |
724 | |
725 // Create a scaling pipeline and make sure that the steps | |
726 // are exactly the steps we expect. | |
727 void CheckPipeline(content::GLHelper::ScalerQuality quality, | |
728 int xsize, | |
729 int ysize, | |
730 int dst_xsize, | |
731 int dst_ysize, | |
732 const std::string& description) { | |
733 std::vector<GLHelperScaling::ScalerStage> stages; | |
734 helper_scaling_->ComputeScalerStages(quality, | |
735 gfx::Size(xsize, ysize), | |
736 gfx::Rect(0, 0, xsize, ysize), | |
737 gfx::Size(dst_xsize, dst_ysize), | |
738 false, | |
739 false, | |
740 &stages); | |
741 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, ""); | |
742 EXPECT_EQ(PrintStages(stages), description); | |
743 } | |
744 | |
745 // Note: Left/Right means Top/Bottom when used for Y dimension. | |
746 enum Margin { | |
747 MarginLeft, | |
748 MarginMiddle, | |
749 MarginRight, | |
750 MarginInvalid, | |
751 }; | |
752 | |
753 static Margin NextMargin(Margin m) { | |
754 switch (m) { | |
755 case MarginLeft: | |
756 return MarginMiddle; | |
757 case MarginMiddle: | |
758 return MarginRight; | |
759 case MarginRight: | |
760 return MarginInvalid; | |
761 default: | |
762 return MarginInvalid; | |
763 } | |
764 } | |
765 | |
766 int compute_margin(int insize, int outsize, Margin m) { | |
767 int available = outsize - insize; | |
768 switch (m) { | |
769 default: | |
770 EXPECT_TRUE(false) << "This should not happen."; | |
771 return 0; | |
772 case MarginLeft: | |
773 return 0; | |
774 case MarginMiddle: | |
775 return (available / 2) & ~1; | |
776 case MarginRight: | |
777 return available; | |
778 } | |
779 } | |
780 | |
781 // Convert 0.0 - 1.0 to 0 - 255 | |
782 int float_to_byte(float v) { | |
783 int ret = static_cast<int>(floorf(v * 255.0f + 0.5f)); | |
784 if (ret < 0) { | |
785 return 0; | |
786 } | |
787 if (ret > 255) { | |
788 return 255; | |
789 } | |
790 return ret; | |
791 } | |
792 | |
793 static void callcallback(const base::Callback<void()>& callback, | |
794 bool result) { | |
795 callback.Run(); | |
796 } | |
797 | |
798 void PrintPlane(unsigned char* plane, int xsize, int stride, int ysize) { | |
799 for (int y = 0; y < ysize; y++) { | |
800 std::string formatted; | |
801 for (int x = 0; x < xsize; x++) { | |
802 formatted.append(base::StringPrintf("%3d, ", plane[y * stride + x])); | |
803 } | |
804 LOG(ERROR) << formatted << " (" << (plane + y * stride) << ")"; | |
805 } | |
806 } | |
807 | |
808 // Compare two planes make sure that each component of each pixel | |
809 // is no more than |maxdiff| apart. | |
810 void ComparePlane(unsigned char* truth, | |
811 unsigned char* other, | |
812 int maxdiff, | |
813 int xsize, | |
814 int stride, | |
815 int ysize, | |
816 SkBitmap* source, | |
817 std::string message) { | |
818 int truth_stride = stride; | |
819 for (int x = 0; x < xsize; x++) { | |
820 for (int y = 0; y < ysize; y++) { | |
821 int a = other[y * stride + x]; | |
822 int b = truth[y * stride + x]; | |
823 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " " | |
824 << message; | |
825 if (std::abs(a - b) > maxdiff) { | |
826 LOG(ERROR) << "-------expected--------"; | |
827 PrintPlane(truth, xsize, truth_stride, ysize); | |
828 LOG(ERROR) << "-------actual--------"; | |
829 PrintPlane(other, xsize, stride, ysize); | |
830 if (source) { | |
831 LOG(ERROR) << "-------before yuv conversion: red--------"; | |
832 PrintChannel(source, 0); | |
833 LOG(ERROR) << "-------before yuv conversion: green------"; | |
834 PrintChannel(source, 1); | |
835 LOG(ERROR) << "-------before yuv conversion: blue-------"; | |
836 PrintChannel(source, 2); | |
837 } | |
838 return; | |
839 } | |
840 } | |
841 } | |
842 } | |
843 | |
844 // YUV readback test. Create a test pattern, convert to YUV | |
845 // with reference implementation and compare to what gl_helper | |
846 // returns. | |
847 void TestYUVReadback(int xsize, | |
848 int ysize, | |
849 int output_xsize, | |
850 int output_ysize, | |
851 int xmargin, | |
852 int ymargin, | |
853 int test_pattern, | |
854 bool flip, | |
855 bool use_mrt, | |
856 content::GLHelper::ScalerQuality quality) { | |
857 WebGLId src_texture = context_->createTexture(); | |
858 SkBitmap input_pixels; | |
859 input_pixels.setConfig(SkBitmap::kARGB_8888_Config, xsize, ysize); | |
860 input_pixels.allocPixels(); | |
861 SkAutoLockPixels lock(input_pixels); | |
862 | |
863 for (int x = 0; x < xsize; ++x) { | |
864 for (int y = 0; y < ysize; ++y) { | |
865 switch (test_pattern) { | |
866 case 0: // Smooth test pattern | |
867 SetChannel(&input_pixels, x, y, 0, x * 10); | |
868 SetChannel(&input_pixels, x, y, 1, y * 10); | |
869 SetChannel(&input_pixels, x, y, 2, (x + y) * 10); | |
870 SetChannel(&input_pixels, x, y, 3, 255); | |
871 break; | |
872 case 1: // Small blocks | |
873 SetChannel(&input_pixels, x, y, 0, x & 1 ? 255 : 0); | |
874 SetChannel(&input_pixels, x, y, 1, y & 1 ? 255 : 0); | |
875 SetChannel(&input_pixels, x, y, 2, (x + y) & 1 ? 255 : 0); | |
876 SetChannel(&input_pixels, x, y, 3, 255); | |
877 break; | |
878 case 2: // Medium blocks | |
879 SetChannel(&input_pixels, x, y, 0, 10 + x / 2 * 50); | |
880 SetChannel(&input_pixels, x, y, 1, 10 + y / 3 * 50); | |
881 SetChannel(&input_pixels, x, y, 2, (x + y) / 5 * 50 + 5); | |
882 SetChannel(&input_pixels, x, y, 3, 255); | |
883 break; | |
884 } | |
885 } | |
886 } | |
887 | |
888 context_->bindTexture(GL_TEXTURE_2D, src_texture); | |
889 context_->texImage2D(GL_TEXTURE_2D, | |
890 0, | |
891 GL_RGBA, | |
892 xsize, | |
893 ysize, | |
894 0, | |
895 GL_RGBA, | |
896 GL_UNSIGNED_BYTE, | |
897 input_pixels.getPixels()); | |
898 | |
899 gpu::Mailbox mailbox; | |
900 context_->genMailboxCHROMIUM(mailbox.name); | |
901 EXPECT_FALSE(mailbox.IsZero()); | |
902 context_->produceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | |
903 uint32 sync_point = context_->insertSyncPoint(); | |
904 | |
905 std::string message = base::StringPrintf( | |
906 "input size: %dx%d " | |
907 "output size: %dx%d " | |
908 "margin: %dx%d " | |
909 "pattern: %d %s %s", | |
910 xsize, | |
911 ysize, | |
912 output_xsize, | |
913 output_ysize, | |
914 xmargin, | |
915 ymargin, | |
916 test_pattern, | |
917 flip ? "flip" : "noflip", | |
918 flip ? "mrt" : "nomrt"); | |
919 scoped_ptr<ReadbackYUVInterface> yuv_reader( | |
920 helper_->CreateReadbackPipelineYUV( | |
921 quality, | |
922 gfx::Size(xsize, ysize), | |
923 gfx::Rect(0, 0, xsize, ysize), | |
924 gfx::Size(output_xsize, output_ysize), | |
925 gfx::Rect(xmargin, ymargin, xsize, ysize), | |
926 flip, | |
927 use_mrt)); | |
928 | |
929 scoped_refptr<media::VideoFrame> output_frame = | |
930 media::VideoFrame::CreateFrame( | |
931 media::VideoFrame::YV12, | |
932 gfx::Size(output_xsize, output_ysize), | |
933 gfx::Rect(0, 0, output_xsize, output_ysize), | |
934 gfx::Size(output_xsize, output_ysize), | |
935 base::TimeDelta::FromSeconds(0)); | |
936 scoped_refptr<media::VideoFrame> truth_frame = | |
937 media::VideoFrame::CreateFrame( | |
938 media::VideoFrame::YV12, | |
939 gfx::Size(output_xsize, output_ysize), | |
940 gfx::Rect(0, 0, output_xsize, output_ysize), | |
941 gfx::Size(output_xsize, output_ysize), | |
942 base::TimeDelta::FromSeconds(0)); | |
943 | |
944 base::RunLoop run_loop; | |
945 yuv_reader->ReadbackYUV(mailbox, | |
946 sync_point, | |
947 output_frame.get(), | |
948 base::Bind(&callcallback, run_loop.QuitClosure())); | |
949 run_loop.Run(); | |
950 | |
951 if (flip) { | |
952 FlipSKBitmap(&input_pixels); | |
953 } | |
954 | |
955 unsigned char* Y = truth_frame->data(media::VideoFrame::kYPlane); | |
956 unsigned char* U = truth_frame->data(media::VideoFrame::kUPlane); | |
957 unsigned char* V = truth_frame->data(media::VideoFrame::kVPlane); | |
958 int32 y_stride = truth_frame->stride(media::VideoFrame::kYPlane); | |
959 int32 u_stride = truth_frame->stride(media::VideoFrame::kUPlane); | |
960 int32 v_stride = truth_frame->stride(media::VideoFrame::kVPlane); | |
961 memset(Y, 0x00, y_stride * output_ysize); | |
962 memset(U, 0x80, u_stride * output_ysize / 2); | |
963 memset(V, 0x80, v_stride * output_ysize / 2); | |
964 | |
965 for (int y = 0; y < ysize; y++) { | |
966 for (int x = 0; x < xsize; x++) { | |
967 Y[(y + ymargin) * y_stride + x + xmargin] = float_to_byte( | |
968 ChannelAsFloat(&input_pixels, x, y, 0) * 0.257 + | |
969 ChannelAsFloat(&input_pixels, x, y, 1) * 0.504 + | |
970 ChannelAsFloat(&input_pixels, x, y, 2) * 0.098 + 0.0625); | |
971 } | |
972 } | |
973 | |
974 for (int y = 0; y < ysize / 2; y++) { | |
975 for (int x = 0; x < xsize / 2; x++) { | |
976 U[(y + ymargin / 2) * u_stride + x + xmargin / 2] = float_to_byte( | |
977 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 0) * -0.148 + | |
978 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 1) * -0.291 + | |
979 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 2) * 0.439 + 0.5); | |
980 V[(y + ymargin / 2) * v_stride + x + xmargin / 2] = float_to_byte( | |
981 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 0) * 0.439 + | |
982 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 1) * -0.368 + | |
983 Bilinear(&input_pixels, x * 2 + 1.0, y * 2 + 1.0, 2) * -0.071 + | |
984 0.5); | |
985 } | |
986 } | |
987 | |
988 ComparePlane(Y, | |
989 output_frame->data(media::VideoFrame::kYPlane), | |
990 2, | |
991 output_xsize, | |
992 y_stride, | |
993 output_ysize, | |
994 &input_pixels, | |
995 message + " Y plane"); | |
996 ComparePlane(U, | |
997 output_frame->data(media::VideoFrame::kUPlane), | |
998 2, | |
999 output_xsize / 2, | |
1000 u_stride, | |
1001 output_ysize / 2, | |
1002 &input_pixels, | |
1003 message + " U plane"); | |
1004 ComparePlane(V, | |
1005 output_frame->data(media::VideoFrame::kVPlane), | |
1006 2, | |
1007 output_xsize / 2, | |
1008 v_stride, | |
1009 output_ysize / 2, | |
1010 &input_pixels, | |
1011 message + " V plane"); | |
1012 | |
1013 context_->deleteTexture(src_texture); | |
1014 } | |
1015 | |
1016 void TestAddOps(int src, int dst, bool scale_x, bool allow3) { | |
1017 std::deque<GLHelperScaling::ScaleOp> ops; | |
1018 GLHelperScaling::ScaleOp::AddOps(src, dst, scale_x, allow3, &ops); | |
1019 // Scale factor 3 is a special case. | |
1020 // It is currently only allowed by itself. | |
1021 if (allow3 && dst * 3 >= src && dst * 2 < src) { | |
1022 EXPECT_EQ(ops[0].scale_factor, 3); | |
1023 EXPECT_EQ(ops.size(), 1U); | |
1024 EXPECT_EQ(ops[0].scale_x, scale_x); | |
1025 EXPECT_EQ(ops[0].scale_size, dst); | |
1026 return; | |
1027 } | |
1028 | |
1029 for (size_t i = 0; i < ops.size(); i++) { | |
1030 EXPECT_EQ(ops[i].scale_x, scale_x); | |
1031 if (i == 0) { | |
1032 // Only the first op is allowed to be a scale up. | |
1033 // (Scaling up *after* scaling down would make it fuzzy.) | |
1034 EXPECT_TRUE(ops[0].scale_factor == 0 || ops[0].scale_factor == 2); | |
1035 } else { | |
1036 // All other operations must be 50% downscales. | |
1037 EXPECT_EQ(ops[i].scale_factor, 2); | |
1038 } | |
1039 } | |
1040 // Check that the scale factors make sense and add up. | |
1041 int tmp = dst; | |
1042 for (int i = static_cast<int>(ops.size() - 1); i >= 0; i--) { | |
1043 EXPECT_EQ(tmp, ops[i].scale_size); | |
1044 if (ops[i].scale_factor == 0) { | |
1045 EXPECT_EQ(i, 0); | |
1046 EXPECT_GT(tmp, src); | |
1047 tmp = src; | |
1048 } else { | |
1049 tmp *= ops[i].scale_factor; | |
1050 } | |
1051 } | |
1052 EXPECT_EQ(tmp, src); | |
1053 } | |
1054 | |
1055 void CheckPipeline2(int xsize, | |
1056 int ysize, | |
1057 int dst_xsize, | |
1058 int dst_ysize, | |
1059 const std::string& description) { | |
1060 std::vector<GLHelperScaling::ScalerStage> stages; | |
1061 helper_scaling_->ConvertScalerOpsToScalerStages( | |
1062 content::GLHelper::SCALER_QUALITY_GOOD, | |
1063 gfx::Size(xsize, ysize), | |
1064 gfx::Rect(0, 0, xsize, ysize), | |
1065 gfx::Size(dst_xsize, dst_ysize), | |
1066 false, | |
1067 false, | |
1068 &x_ops_, | |
1069 &y_ops_, | |
1070 &stages); | |
1071 EXPECT_EQ(x_ops_.size(), 0U); | |
1072 EXPECT_EQ(y_ops_.size(), 0U); | |
1073 ValidateScalerStages(content::GLHelper::SCALER_QUALITY_GOOD, stages, ""); | |
1074 EXPECT_EQ(PrintStages(stages), description); | |
1075 } | |
1076 | |
1077 void CheckOptimizationsTest() { | |
1078 // Basic upscale. X and Y should be combined into one pass. | |
1079 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000)); | |
1080 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000)); | |
1081 CheckPipeline2(1024, 768, 2000, 2000, "1024x768 -> 2000x2000 bilinear\n"); | |
1082 | |
1083 // X scaled 1/2, Y upscaled, should still be one pass. | |
1084 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512)); | |
1085 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000)); | |
1086 CheckPipeline2(1024, 768, 512, 2000, "1024x768 -> 512x2000 bilinear\n"); | |
1087 | |
1088 // X upscaled, Y scaled 1/2, one bilinear pass | |
1089 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000)); | |
1090 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384)); | |
1091 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 2000x384 bilinear\n"); | |
1092 | |
1093 // X scaled 1/2, Y scaled 1/2, one bilinear pass | |
1094 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512)); | |
1095 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384)); | |
1096 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 512x384 bilinear\n"); | |
1097 | |
1098 // X scaled 1/2, Y scaled to 60%, one bilinear2 pass. | |
1099 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50)); | |
1100 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1101 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1102 CheckPipeline2(100, 100, 50, 60, "100x100 -> 50x60 bilinear2 Y\n"); | |
1103 | |
1104 // X scaled to 60%, Y scaled 1/2, one bilinear2 pass. | |
1105 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120)); | |
1106 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60)); | |
1107 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 50)); | |
1108 CheckPipeline2(100, 100, 50, 60, "100x100 -> 60x50 bilinear2 X\n"); | |
1109 | |
1110 // X scaled to 60%, Y scaled 60%, one bilinear2x2 pass. | |
1111 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120)); | |
1112 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60)); | |
1113 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1114 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1115 CheckPipeline2(100, 100, 60, 60, "100x100 -> 60x60 bilinear2x2\n"); | |
1116 | |
1117 // X scaled to 40%, Y scaled 40%, two bilinear3 passes. | |
1118 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40)); | |
1119 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40)); | |
1120 CheckPipeline2(100, | |
1121 100, | |
1122 40, | |
1123 40, | |
1124 "100x100 -> 100x40 bilinear3 Y\n" | |
1125 "100x40 -> 40x40 bilinear3 X\n"); | |
1126 | |
1127 // X scaled to 60%, Y scaled 40% | |
1128 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120)); | |
1129 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60)); | |
1130 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40)); | |
1131 CheckPipeline2(100, | |
1132 100, | |
1133 60, | |
1134 40, | |
1135 "100x100 -> 100x40 bilinear3 Y\n" | |
1136 "100x40 -> 60x40 bilinear2 X\n"); | |
1137 | |
1138 // X scaled to 40%, Y scaled 60% | |
1139 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40)); | |
1140 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1141 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1142 CheckPipeline2(100, | |
1143 100, | |
1144 40, | |
1145 60, | |
1146 "100x100 -> 100x60 bilinear2 Y\n" | |
1147 "100x60 -> 40x60 bilinear3 X\n"); | |
1148 | |
1149 // X scaled to 30%, Y scaled 30% | |
1150 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120)); | |
1151 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60)); | |
1152 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 30)); | |
1153 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1154 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1155 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30)); | |
1156 CheckPipeline2(100, | |
1157 100, | |
1158 30, | |
1159 30, | |
1160 "100x100 -> 100x30 bilinear4 Y\n" | |
1161 "100x30 -> 30x30 bilinear4 X\n"); | |
1162 | |
1163 // X scaled to 50%, Y scaled 30% | |
1164 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50)); | |
1165 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1166 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1167 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30)); | |
1168 CheckPipeline2(100, 100, 50, 30, "100x100 -> 50x30 bilinear4 Y\n"); | |
1169 | |
1170 // X scaled to 150%, Y scaled 30% | |
1171 // Note that we avoid combinding X and Y passes | |
1172 // as that would probably be LESS efficient here. | |
1173 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 150)); | |
1174 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120)); | |
1175 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60)); | |
1176 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30)); | |
1177 CheckPipeline2(100, | |
1178 100, | |
1179 150, | |
1180 30, | |
1181 "100x100 -> 100x30 bilinear4 Y\n" | |
1182 "100x30 -> 150x30 bilinear\n"); | |
1183 | |
1184 // X scaled to 1%, Y scaled 1% | |
1185 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 128)); | |
1186 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 64)); | |
1187 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 32)); | |
1188 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 16)); | |
1189 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 8)); | |
1190 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 4)); | |
1191 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 2)); | |
1192 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 1)); | |
1193 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 128)); | |
1194 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 64)); | |
1195 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 32)); | |
1196 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 16)); | |
1197 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 8)); | |
1198 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 4)); | |
1199 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 2)); | |
1200 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 1)); | |
1201 CheckPipeline2(100, | |
1202 100, | |
1203 30, | |
1204 30, | |
1205 "100x100 -> 100x32 bilinear4 Y\n" | |
1206 "100x32 -> 100x4 bilinear4 Y\n" | |
1207 "100x4 -> 64x1 bilinear2x2\n" | |
1208 "64x1 -> 8x1 bilinear4 X\n" | |
1209 "8x1 -> 1x1 bilinear4 X\n"); | |
1210 } | |
1211 | |
1212 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context_; | |
1213 gpu::ContextSupport* context_support_; | |
1214 scoped_ptr<content::GLHelper> helper_; | |
1215 scoped_ptr<content::GLHelperScaling> helper_scaling_; | |
1216 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; | |
1217 }; | |
1218 | |
1219 TEST_F(GLHelperTest, YUVReadbackOptTest) { | |
1220 // This test uses the cb_command tracing events to detect how many | |
1221 // scaling passes are actually performed by the YUV readback pipeline. | |
1222 StartTracing(TRACE_DISABLED_BY_DEFAULT("cb_command")); | |
1223 | |
1224 TestYUVReadback(800, | |
1225 400, | |
1226 800, | |
1227 400, | |
1228 0, | |
1229 0, | |
1230 1, | |
1231 false, | |
1232 true, | |
1233 content::GLHelper::SCALER_QUALITY_FAST); | |
1234 | |
1235 std::map<std::string, int> event_counts; | |
1236 EndTracing(&event_counts); | |
1237 int draw_buffer_calls = event_counts["kDrawBuffersEXTImmediate"]; | |
1238 int draw_arrays_calls = event_counts["kDrawArrays"]; | |
1239 VLOG(1) << "Draw buffer calls: " << draw_buffer_calls; | |
1240 VLOG(1) << "DrawArrays calls: " << draw_arrays_calls; | |
1241 | |
1242 if (draw_buffer_calls) { | |
1243 // When using MRT, the YUV readback code should only | |
1244 // execute two draw arrays, and scaling should be integrated | |
1245 // into those two calls since we are using the FAST scalign | |
1246 // quality. | |
1247 EXPECT_EQ(2, draw_arrays_calls); | |
1248 } else { | |
1249 // When not using MRT, there are three passes for the YUV, | |
1250 // and one for the scaling. | |
1251 EXPECT_EQ(4, draw_arrays_calls); | |
1252 } | |
1253 } | |
1254 | |
1255 TEST_F(GLHelperTest, YUVReadbackTest) { | |
1256 int sizes[] = {2, 4, 14}; | |
1257 for (int flip = 0; flip <= 1; flip++) { | |
1258 for (int use_mrt = 0; use_mrt <= 1; use_mrt++) { | |
1259 for (unsigned int x = 0; x < arraysize(sizes); x++) { | |
1260 for (unsigned int y = 0; y < arraysize(sizes); y++) { | |
1261 for (unsigned int ox = x; ox < arraysize(sizes); ox++) { | |
1262 for (unsigned int oy = y; oy < arraysize(sizes); oy++) { | |
1263 // If output is a subsection of the destination frame, (letterbox) | |
1264 // then try different variations of where the subsection goes. | |
1265 for (Margin xm = x < ox ? MarginLeft : MarginRight; | |
1266 xm <= MarginRight; | |
1267 xm = NextMargin(xm)) { | |
1268 for (Margin ym = y < oy ? MarginLeft : MarginRight; | |
1269 ym <= MarginRight; | |
1270 ym = NextMargin(ym)) { | |
1271 for (int pattern = 0; pattern < 3; pattern++) { | |
1272 TestYUVReadback(sizes[x], | |
1273 sizes[y], | |
1274 sizes[ox], | |
1275 sizes[oy], | |
1276 compute_margin(sizes[x], sizes[ox], xm), | |
1277 compute_margin(sizes[y], sizes[oy], ym), | |
1278 pattern, | |
1279 flip == 1, | |
1280 use_mrt == 1, | |
1281 content::GLHelper::SCALER_QUALITY_GOOD); | |
1282 if (HasFailure()) { | |
1283 return; | |
1284 } | |
1285 } | |
1286 } | |
1287 } | |
1288 } | |
1289 } | |
1290 } | |
1291 } | |
1292 } | |
1293 } | |
1294 } | |
1295 | |
1296 // Per pixel tests, all sizes are small so that we can print | |
1297 // out the generated bitmaps. | |
1298 TEST_F(GLHelperTest, ScaleTest) { | |
1299 int sizes[] = {3, 6, 16}; | |
1300 for (int flip = 0; flip <= 1; flip++) { | |
1301 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
1302 for (int x = 0; x < 3; x++) { | |
1303 for (int y = 0; y < 3; y++) { | |
1304 for (int dst_x = 0; dst_x < 3; dst_x++) { | |
1305 for (int dst_y = 0; dst_y < 3; dst_y++) { | |
1306 for (int pattern = 0; pattern < 3; pattern++) { | |
1307 TestScale(sizes[x], | |
1308 sizes[y], | |
1309 sizes[dst_x], | |
1310 sizes[dst_y], | |
1311 pattern, | |
1312 q, | |
1313 flip == 1); | |
1314 if (HasFailure()) { | |
1315 return; | |
1316 } | |
1317 } | |
1318 } | |
1319 } | |
1320 } | |
1321 } | |
1322 } | |
1323 } | |
1324 } | |
1325 | |
1326 // Validate that all scaling generates valid pipelines. | |
1327 TEST_F(GLHelperTest, ValidateScalerPipelines) { | |
1328 int sizes[] = {7, 99, 128, 256, 512, 719, 720, 721, 1920, 2011, 3217, 4096}; | |
1329 for (size_t q = 0; q < arraysize(kQualities); q++) { | |
1330 for (size_t x = 0; x < arraysize(sizes); x++) { | |
1331 for (size_t y = 0; y < arraysize(sizes); y++) { | |
1332 for (size_t dst_x = 0; dst_x < arraysize(sizes); dst_x++) { | |
1333 for (size_t dst_y = 0; dst_y < arraysize(sizes); dst_y++) { | |
1334 TestScalerPipeline( | |
1335 q, sizes[x], sizes[y], sizes[dst_x], sizes[dst_y]); | |
1336 if (HasFailure()) { | |
1337 return; | |
1338 } | |
1339 } | |
1340 } | |
1341 } | |
1342 } | |
1343 } | |
1344 } | |
1345 | |
1346 // Make sure we don't create overly complicated pipelines | |
1347 // for a few common use cases. | |
1348 TEST_F(GLHelperTest, CheckSpecificPipelines) { | |
1349 // Upscale should be single pass. | |
1350 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD, | |
1351 1024, | |
1352 700, | |
1353 1280, | |
1354 720, | |
1355 "1024x700 -> 1280x720 bilinear\n"); | |
1356 // Slight downscale should use BILINEAR2X2. | |
1357 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD, | |
1358 1280, | |
1359 720, | |
1360 1024, | |
1361 700, | |
1362 "1280x720 -> 1024x700 bilinear2x2\n"); | |
1363 // Most common tab capture pipeline on the Pixel. | |
1364 // Should be using two BILINEAR3 passes. | |
1365 CheckPipeline(content::GLHelper::SCALER_QUALITY_GOOD, | |
1366 2560, | |
1367 1476, | |
1368 1249, | |
1369 720, | |
1370 "2560x1476 -> 2560x720 bilinear3 Y\n" | |
1371 "2560x720 -> 1249x720 bilinear3 X\n"); | |
1372 } | |
1373 | |
1374 TEST_F(GLHelperTest, ScalerOpTest) { | |
1375 for (int allow3 = 0; allow3 <= 1; allow3++) { | |
1376 for (int dst = 1; dst < 2049; dst += 1 + (dst >> 3)) { | |
1377 for (int src = 1; src < 2049; src++) { | |
1378 TestAddOps(src, dst, allow3 == 1, (src & 1) == 1); | |
1379 if (HasFailure()) { | |
1380 LOG(ERROR) << "Failed for src=" << src << " dst=" << dst | |
1381 << " allow3=" << allow3; | |
1382 return; | |
1383 } | |
1384 } | |
1385 } | |
1386 } | |
1387 } | |
1388 | |
1389 TEST_F(GLHelperTest, CheckOptimizations) { | |
1390 // Test in baseclass since it is friends with GLHelperScaling | |
1391 CheckOptimizationsTest(); | |
1392 } | |
1393 | |
1394 } // namespace | |
1395 | |
1396 // These tests needs to run against a proper GL environment, so we | |
1397 // need to set it up before we can run the tests. | |
1398 int main(int argc, char** argv) { | |
1399 CommandLine::Init(argc, argv); | |
1400 base::TestSuite* suite = new content::ContentTestSuite(argc, argv); | |
1401 #if defined(OS_MACOSX) | |
1402 base::mac::ScopedNSAutoreleasePool pool; | |
1403 #endif | |
1404 #if defined(TOOLKIT_GTK) | |
1405 gfx::GtkInitFromCommandLine(*CommandLine::ForCurrentProcess()); | |
1406 #endif | |
1407 gfx::GLSurface::InitializeOneOff(); | |
1408 gpu::ApplyGpuDriverBugWorkarounds(CommandLine::ForCurrentProcess()); | |
1409 | |
1410 content::UnitTestTestSuite runner(suite); | |
1411 base::MessageLoop message_loop; | |
1412 return runner.Run(); | |
1413 } | |
OLD | NEW |