Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: components/display_compositor/gl_helper_unittest.cc

Issue 2873243002: Move components/display_compositor to components/viz/display_compositor (Closed)
Patch Set: Rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 <stddef.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <cmath>
10 #include <string>
11 #include <vector>
12
13 #include <GLES2/gl2.h>
14 #include <GLES2/gl2ext.h>
15 #include <GLES2/gl2extchromium.h>
16
17 #include "base/at_exit.h"
18 #include "base/bind.h"
19 #include "base/command_line.h"
20 #include "base/files/file_util.h"
21 #include "base/json/json_reader.h"
22 #include "base/macros.h"
23 #include "base/memory/ref_counted_memory.h"
24 #include "base/message_loop/message_loop.h"
25 #include "base/run_loop.h"
26 #include "base/strings/stringprintf.h"
27 #include "base/synchronization/waitable_event.h"
28 #include "base/test/launcher/unit_test_launcher.h"
29 #include "base/test/test_suite.h"
30 #include "base/threading/thread_task_runner_handle.h"
31 #include "base/time/time.h"
32 #include "base/trace_event/trace_event.h"
33 #include "components/display_compositor/gl_helper.h"
34 #include "components/display_compositor/gl_helper_readback_support.h"
35 #include "components/display_compositor/gl_helper_scaling.h"
36 #include "gpu/command_buffer/client/gles2_implementation.h"
37 #include "gpu/command_buffer/client/shared_memory_limits.h"
38 #include "gpu/ipc/gl_in_process_context.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "third_party/skia/include/core/SkBitmap.h"
41 #include "third_party/skia/include/core/SkTypes.h"
42 #include "ui/gl/gl_implementation.h"
43
44 namespace display_compositor {
45
46 display_compositor::GLHelper::ScalerQuality kQualities[] = {
47 display_compositor::GLHelper::SCALER_QUALITY_BEST,
48 display_compositor::GLHelper::SCALER_QUALITY_GOOD,
49 display_compositor::GLHelper::SCALER_QUALITY_FAST,
50 };
51
52 const char* kQualityNames[] = {
53 "best", "good", "fast",
54 };
55
56 class GLHelperTest : public testing::Test {
57 protected:
58 void SetUp() override {
59 gpu::gles2::ContextCreationAttribHelper attributes;
60 attributes.alpha_size = 8;
61 attributes.depth_size = 24;
62 attributes.red_size = 8;
63 attributes.green_size = 8;
64 attributes.blue_size = 8;
65 attributes.stencil_size = 8;
66 attributes.samples = 4;
67 attributes.sample_buffers = 1;
68 attributes.bind_generates_resource = false;
69
70 context_.reset(
71 gpu::GLInProcessContext::Create(nullptr, /* service */
72 nullptr, /* surface */
73 true, /* offscreen */
74 gpu::kNullSurfaceHandle, /* window */
75 nullptr, /* share_context */
76 attributes, gpu::SharedMemoryLimits(),
77 nullptr, /* gpu_memory_buffer_manager */
78 nullptr, /* image_factory */
79 base::ThreadTaskRunnerHandle::Get()));
80 gl_ = context_->GetImplementation();
81 gpu::ContextSupport* support = context_->GetImplementation();
82
83 helper_.reset(new display_compositor::GLHelper(gl_, support));
84 helper_scaling_.reset(
85 new display_compositor::GLHelperScaling(gl_, helper_.get()));
86 }
87
88 void TearDown() override {
89 helper_scaling_.reset(nullptr);
90 helper_.reset(nullptr);
91 context_.reset(nullptr);
92 }
93
94 // Bicubic filter kernel function.
95 static float Bicubic(float x) {
96 const float a = -0.5;
97 x = std::abs(x);
98 float x2 = x * x;
99 float x3 = x2 * x;
100 if (x <= 1) {
101 return (a + 2) * x3 - (a + 3) * x2 + 1;
102 } else if (x < 2) {
103 return a * x3 - 5 * a * x2 + 8 * a * x - 4 * a;
104 } else {
105 return 0.0f;
106 }
107 }
108
109 // Look up a single channel value. Works for 4-channel and single channel
110 // bitmaps. Clamp x/y.
111 int Channel(SkBitmap* pixels, int x, int y, int c) {
112 if (pixels->bytesPerPixel() == 4) {
113 uint32_t* data =
114 pixels->getAddr32(std::max(0, std::min(x, pixels->width() - 1)),
115 std::max(0, std::min(y, pixels->height() - 1)));
116 return (*data) >> (c * 8) & 0xff;
117 } else {
118 DCHECK_EQ(pixels->bytesPerPixel(), 1);
119 DCHECK_EQ(c, 0);
120 return *pixels->getAddr8(std::max(0, std::min(x, pixels->width() - 1)),
121 std::max(0, std::min(y, pixels->height() - 1)));
122 }
123 }
124
125 // Set a single channel value. Works for 4-channel and single channel
126 // bitmaps. Clamp x/y.
127 void SetChannel(SkBitmap* pixels, int x, int y, int c, int v) {
128 DCHECK_GE(x, 0);
129 DCHECK_GE(y, 0);
130 DCHECK_LT(x, pixels->width());
131 DCHECK_LT(y, pixels->height());
132 if (pixels->bytesPerPixel() == 4) {
133 uint32_t* data = pixels->getAddr32(x, y);
134 v = std::max(0, std::min(v, 255));
135 *data = (*data & ~(0xffu << (c * 8))) | (v << (c * 8));
136 } else {
137 DCHECK_EQ(pixels->bytesPerPixel(), 1);
138 DCHECK_EQ(c, 0);
139 uint8_t* data = pixels->getAddr8(x, y);
140 v = std::max(0, std::min(v, 255));
141 *data = v;
142 }
143 }
144
145 // Print all the R, G, B or A values from an SkBitmap in a
146 // human-readable format.
147 void PrintChannel(SkBitmap* pixels, int c) {
148 for (int y = 0; y < pixels->height(); y++) {
149 std::string formatted;
150 for (int x = 0; x < pixels->width(); x++) {
151 formatted.append(base::StringPrintf("%3d, ", Channel(pixels, x, y, c)));
152 }
153 LOG(ERROR) << formatted;
154 }
155 }
156
157 // Print out the individual steps of a scaler pipeline.
158 std::string PrintStages(
159 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages) {
160 std::string ret;
161 for (size_t i = 0; i < scaler_stages.size(); i++) {
162 ret.append(base::StringPrintf(
163 "%dx%d -> %dx%d ", scaler_stages[i].src_size.width(),
164 scaler_stages[i].src_size.height(), scaler_stages[i].dst_size.width(),
165 scaler_stages[i].dst_size.height()));
166 bool xy_matters = false;
167 switch (scaler_stages[i].shader) {
168 case GLHelperScaling::SHADER_BILINEAR:
169 ret.append("bilinear");
170 break;
171 case GLHelperScaling::SHADER_BILINEAR2:
172 ret.append("bilinear2");
173 xy_matters = true;
174 break;
175 case GLHelperScaling::SHADER_BILINEAR3:
176 ret.append("bilinear3");
177 xy_matters = true;
178 break;
179 case GLHelperScaling::SHADER_BILINEAR4:
180 ret.append("bilinear4");
181 xy_matters = true;
182 break;
183 case GLHelperScaling::SHADER_BILINEAR2X2:
184 ret.append("bilinear2x2");
185 break;
186 case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
187 ret.append("bicubic upscale");
188 xy_matters = true;
189 break;
190 case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
191 ret.append("bicubic 1/2");
192 xy_matters = true;
193 break;
194 case GLHelperScaling::SHADER_PLANAR:
195 ret.append("planar");
196 break;
197 case GLHelperScaling::SHADER_YUV_MRT_PASS1:
198 ret.append("rgb2yuv pass 1");
199 break;
200 case GLHelperScaling::SHADER_YUV_MRT_PASS2:
201 ret.append("rgb2yuv pass 2");
202 break;
203 }
204
205 if (xy_matters) {
206 if (scaler_stages[i].scale_x) {
207 ret.append(" X");
208 } else {
209 ret.append(" Y");
210 }
211 }
212 ret.append("\n");
213 }
214 return ret;
215 }
216
217 bool CheckScale(double scale, int samples, bool already_scaled) {
218 // 1:1 is valid if there is one sample.
219 if (samples == 1 && scale == 1.0) {
220 return true;
221 }
222 // Is it an exact down-scale (50%, 25%, etc.?)
223 if (scale == 2.0 * samples) {
224 return true;
225 }
226 // Upscales, only valid if we haven't already scaled in this dimension.
227 if (!already_scaled) {
228 // Is it a valid bilinear upscale?
229 if (samples == 1 && scale <= 1.0) {
230 return true;
231 }
232 // Multi-sample upscale-downscale combination?
233 if (scale > samples / 2.0 && scale < samples) {
234 return true;
235 }
236 }
237 return false;
238 }
239
240 // Make sure that the stages of the scaler pipeline are sane.
241 void ValidateScalerStages(
242 display_compositor::GLHelper::ScalerQuality quality,
243 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages,
244 const gfx::Size& dst_size,
245 const std::string& message) {
246 bool previous_error = HasFailure();
247 // First, check that the input size for each stage is equal to
248 // the output size of the previous stage.
249 for (size_t i = 1; i < scaler_stages.size(); i++) {
250 EXPECT_EQ(scaler_stages[i - 1].dst_size.width(),
251 scaler_stages[i].src_size.width());
252 EXPECT_EQ(scaler_stages[i - 1].dst_size.height(),
253 scaler_stages[i].src_size.height());
254 EXPECT_EQ(scaler_stages[i].src_subrect.x(), 0);
255 EXPECT_EQ(scaler_stages[i].src_subrect.y(), 0);
256 EXPECT_EQ(scaler_stages[i].src_subrect.width(),
257 scaler_stages[i].src_size.width());
258 EXPECT_EQ(scaler_stages[i].src_subrect.height(),
259 scaler_stages[i].src_size.height());
260 }
261
262 // Check the output size matches the destination of the last stage
263 EXPECT_EQ(scaler_stages.back().dst_size.width(), dst_size.width());
264 EXPECT_EQ(scaler_stages.back().dst_size.height(), dst_size.height());
265
266 // Used to verify that up-scales are not attempted after some
267 // other scale.
268 bool scaled_x = false;
269 bool scaled_y = false;
270
271 for (size_t i = 0; i < scaler_stages.size(); i++) {
272 // Note: 2.0 means scaling down by 50%
273 double x_scale =
274 static_cast<double>(scaler_stages[i].src_subrect.width()) /
275 static_cast<double>(scaler_stages[i].dst_size.width());
276 double y_scale =
277 static_cast<double>(scaler_stages[i].src_subrect.height()) /
278 static_cast<double>(scaler_stages[i].dst_size.height());
279
280 int x_samples = 0;
281 int y_samples = 0;
282
283 // Codify valid scale operations.
284 switch (scaler_stages[i].shader) {
285 case GLHelperScaling::SHADER_PLANAR:
286 case GLHelperScaling::SHADER_YUV_MRT_PASS1:
287 case GLHelperScaling::SHADER_YUV_MRT_PASS2:
288 EXPECT_TRUE(false) << "Invalid shader.";
289 break;
290
291 case GLHelperScaling::SHADER_BILINEAR:
292 if (quality != display_compositor::GLHelper::SCALER_QUALITY_FAST) {
293 x_samples = 1;
294 y_samples = 1;
295 }
296 break;
297 case GLHelperScaling::SHADER_BILINEAR2:
298 x_samples = 2;
299 y_samples = 1;
300 break;
301 case GLHelperScaling::SHADER_BILINEAR3:
302 x_samples = 3;
303 y_samples = 1;
304 break;
305 case GLHelperScaling::SHADER_BILINEAR4:
306 x_samples = 4;
307 y_samples = 1;
308 break;
309 case GLHelperScaling::SHADER_BILINEAR2X2:
310 x_samples = 2;
311 y_samples = 2;
312 break;
313 case GLHelperScaling::SHADER_BICUBIC_UPSCALE:
314 if (scaler_stages[i].scale_x) {
315 EXPECT_LT(x_scale, 1.0);
316 EXPECT_EQ(y_scale, 1.0);
317 } else {
318 EXPECT_EQ(x_scale, 1.0);
319 EXPECT_LT(y_scale, 1.0);
320 }
321 break;
322 case GLHelperScaling::SHADER_BICUBIC_HALF_1D:
323 if (scaler_stages[i].scale_x) {
324 EXPECT_EQ(x_scale, 2.0);
325 EXPECT_EQ(y_scale, 1.0);
326 } else {
327 EXPECT_EQ(x_scale, 1.0);
328 EXPECT_EQ(y_scale, 2.0);
329 }
330 break;
331 }
332
333 if (!scaler_stages[i].scale_x) {
334 std::swap(x_samples, y_samples);
335 }
336
337 if (x_samples) {
338 EXPECT_TRUE(CheckScale(x_scale, x_samples, scaled_x)) << "x_scale = "
339 << x_scale;
340 }
341 if (y_samples) {
342 EXPECT_TRUE(CheckScale(y_scale, y_samples, scaled_y)) << "y_scale = "
343 << y_scale;
344 }
345
346 if (x_scale != 1.0) {
347 scaled_x = true;
348 }
349 if (y_scale != 1.0) {
350 scaled_y = true;
351 }
352 }
353
354 if (HasFailure() && !previous_error) {
355 LOG(ERROR) << "Invalid scaler stages: " << message;
356 LOG(ERROR) << "Scaler stages:";
357 LOG(ERROR) << PrintStages(scaler_stages);
358 }
359 }
360
361 // Compares two bitmaps taking color types into account. Checks whether each
362 // component of each pixel is no more than |maxdiff| apart. If bitmaps are not
363 // similar enough, prints out |truth|, |other|, |source|, |scaler_stages|
364 // and |message|.
365 void Compare(SkBitmap* truth,
366 SkBitmap* other,
367 int maxdiff,
368 SkBitmap* source,
369 const std::vector<GLHelperScaling::ScalerStage>& scaler_stages,
370 std::string message) {
371 EXPECT_EQ(truth->width(), other->width());
372 EXPECT_EQ(truth->height(), other->height());
373 bool swizzle = (truth->colorType() == kRGBA_8888_SkColorType &&
374 other->colorType() == kBGRA_8888_SkColorType) ||
375 (truth->colorType() == kBGRA_8888_SkColorType &&
376 other->colorType() == kRGBA_8888_SkColorType);
377 EXPECT_TRUE(swizzle || truth->colorType() == other->colorType());
378 int bpp = truth->bytesPerPixel();
379 for (int x = 0; x < truth->width(); x++) {
380 for (int y = 0; y < truth->height(); y++) {
381 for (int c = 0; c < bpp; c++) {
382 int a = Channel(truth, x, y, c);
383 // swizzle when comparing if needed
384 int b = swizzle && (c == 0 || c == 2)
385 ? Channel(other, x, y, (c + 2) & 2)
386 : Channel(other, x, y, c);
387 EXPECT_NEAR(a, b, maxdiff) << " x=" << x << " y=" << y << " c=" << c
388 << " " << message;
389 if (std::abs(a - b) > maxdiff) {
390 LOG(ERROR) << "-------expected--------";
391 for (int i = 0; i < bpp; i++) {
392 LOG(ERROR) << "Channel " << i << ":";
393 PrintChannel(truth, i);
394 }
395 LOG(ERROR) << "-------actual--------";
396 for (int i = 0; i < bpp; i++) {
397 LOG(ERROR) << "Channel " << i << ":";
398 PrintChannel(other, i);
399 }
400 if (source) {
401 LOG(ERROR) << "-------original--------";
402 for (int i = 0; i < source->bytesPerPixel(); i++) {
403 LOG(ERROR) << "Channel " << i << ":";
404 PrintChannel(source, i);
405 }
406 }
407 LOG(ERROR) << "-----Scaler stages------";
408 LOG(ERROR) << PrintStages(scaler_stages);
409 return;
410 }
411 }
412 }
413 }
414 }
415
416 // Get a single R, G, B or A value as a float.
417 float ChannelAsFloat(SkBitmap* pixels, int x, int y, int c) {
418 return Channel(pixels, x, y, c) / 255.0;
419 }
420
421 // Works like a GL_LINEAR lookup on an SkBitmap.
422 float Bilinear(SkBitmap* pixels, float x, float y, int c) {
423 x -= 0.5;
424 y -= 0.5;
425 int base_x = static_cast<int>(floorf(x));
426 int base_y = static_cast<int>(floorf(y));
427 x -= base_x;
428 y -= base_y;
429 return (ChannelAsFloat(pixels, base_x, base_y, c) * (1 - x) * (1 - y) +
430 ChannelAsFloat(pixels, base_x + 1, base_y, c) * x * (1 - y) +
431 ChannelAsFloat(pixels, base_x, base_y + 1, c) * (1 - x) * y +
432 ChannelAsFloat(pixels, base_x + 1, base_y + 1, c) * x * y);
433 }
434
435 // Encodes an RGBA bitmap to grayscale.
436 // Reference implementation for
437 // GLHelper::CopyToTextureImpl::EncodeTextureAsGrayscale.
438 void EncodeToGrayscaleSlow(SkBitmap* input, SkBitmap* output) {
439 const float kRGBtoGrayscaleColorWeights[3] = {0.213f, 0.715f, 0.072f};
440 CHECK_EQ(kAlpha_8_SkColorType, output->colorType());
441 CHECK_EQ(input->width(), output->width());
442 CHECK_EQ(input->height(), output->height());
443 CHECK_EQ(input->colorType(), kRGBA_8888_SkColorType);
444
445 for (int dst_y = 0; dst_y < output->height(); dst_y++) {
446 for (int dst_x = 0; dst_x < output->width(); dst_x++) {
447 float c0 = ChannelAsFloat(input, dst_x, dst_y, 0);
448 float c1 = ChannelAsFloat(input, dst_x, dst_y, 1);
449 float c2 = ChannelAsFloat(input, dst_x, dst_y, 2);
450 float value = c0 * kRGBtoGrayscaleColorWeights[0] +
451 c1 * kRGBtoGrayscaleColorWeights[1] +
452 c2 * kRGBtoGrayscaleColorWeights[2];
453 SetChannel(output, dst_x, dst_y, 0,
454 static_cast<int>(value * 255.0f + 0.5f));
455 }
456 }
457 }
458
459 // Very slow bicubic / bilinear scaler for reference.
460 void ScaleSlow(SkBitmap* input,
461 SkBitmap* output,
462 display_compositor::GLHelper::ScalerQuality quality) {
463 float xscale = static_cast<float>(input->width()) / output->width();
464 float yscale = static_cast<float>(input->height()) / output->height();
465 float clamped_xscale = xscale < 1.0 ? 1.0 : 1.0 / xscale;
466 float clamped_yscale = yscale < 1.0 ? 1.0 : 1.0 / yscale;
467 for (int dst_y = 0; dst_y < output->height(); dst_y++) {
468 for (int dst_x = 0; dst_x < output->width(); dst_x++) {
469 for (int channel = 0; channel < 4; channel++) {
470 float dst_x_in_src = (dst_x + 0.5f) * xscale;
471 float dst_y_in_src = (dst_y + 0.5f) * yscale;
472
473 float value = 0.0f;
474 float sum = 0.0f;
475 switch (quality) {
476 case display_compositor::GLHelper::SCALER_QUALITY_BEST:
477 for (int src_y = -10; src_y < input->height() + 10; ++src_y) {
478 float coeff_y =
479 Bicubic((src_y + 0.5f - dst_y_in_src) * clamped_yscale);
480 if (coeff_y == 0.0f) {
481 continue;
482 }
483 for (int src_x = -10; src_x < input->width() + 10; ++src_x) {
484 float coeff =
485 coeff_y *
486 Bicubic((src_x + 0.5f - dst_x_in_src) * clamped_xscale);
487 if (coeff == 0.0f) {
488 continue;
489 }
490 sum += coeff;
491 float c = ChannelAsFloat(input, src_x, src_y, channel);
492 value += c * coeff;
493 }
494 }
495 break;
496
497 case display_compositor::GLHelper::SCALER_QUALITY_GOOD: {
498 int xshift = 0, yshift = 0;
499 while ((output->width() << xshift) < input->width()) {
500 xshift++;
501 }
502 while ((output->height() << yshift) < input->height()) {
503 yshift++;
504 }
505 int xmag = 1 << xshift;
506 int ymag = 1 << yshift;
507 if (xmag == 4 && output->width() * 3 >= input->width()) {
508 xmag = 3;
509 }
510 if (ymag == 4 && output->height() * 3 >= input->height()) {
511 ymag = 3;
512 }
513 for (int x = 0; x < xmag; x++) {
514 for (int y = 0; y < ymag; y++) {
515 value += Bilinear(
516 input, (dst_x * xmag + x + 0.5) * xscale / xmag,
517 (dst_y * ymag + y + 0.5) * yscale / ymag, channel);
518 sum += 1.0;
519 }
520 }
521 break;
522 }
523
524 case display_compositor::GLHelper::SCALER_QUALITY_FAST:
525 value = Bilinear(input, dst_x_in_src, dst_y_in_src, channel);
526 sum = 1.0;
527 }
528 value /= sum;
529 SetChannel(output, dst_x, dst_y, channel,
530 static_cast<int>(value * 255.0f + 0.5f));
531 }
532 }
533 }
534 }
535
536 void FlipSKBitmap(SkBitmap* bitmap) {
537 int bpp = bitmap->bytesPerPixel();
538 DCHECK(bpp == 4 || bpp == 1);
539 int top_line = 0;
540 int bottom_line = bitmap->height() - 1;
541 while (top_line < bottom_line) {
542 for (int x = 0; x < bitmap->width(); x++) {
543 bpp == 4 ? std::swap(*bitmap->getAddr32(x, top_line),
544 *bitmap->getAddr32(x, bottom_line))
545 : std::swap(*bitmap->getAddr8(x, top_line),
546 *bitmap->getAddr8(x, bottom_line));
547 }
548 top_line++;
549 bottom_line--;
550 }
551 }
552
553 // Swaps red and blue channels in each pixel in a 32-bit bitmap.
554 void SwizzleSKBitmap(SkBitmap* bitmap) {
555 int bpp = bitmap->bytesPerPixel();
556 DCHECK(bpp == 4);
557 for (int y = 0; y < bitmap->height(); y++) {
558 for (int x = 0; x < bitmap->width(); x++) {
559 // Swap channels 0 and 2 (red and blue)
560 int c0 = Channel(bitmap, x, y, 0);
561 int c2 = Channel(bitmap, x, y, 2);
562 SetChannel(bitmap, x, y, 2, c0);
563 SetChannel(bitmap, x, y, 0, c2);
564 }
565 }
566 }
567
568 // gl_helper scales recursively, so we'll need to do that
569 // in the reference implementation too.
570 void ScaleSlowRecursive(SkBitmap* input,
571 SkBitmap* output,
572 display_compositor::GLHelper::ScalerQuality quality) {
573 if (quality == display_compositor::GLHelper::SCALER_QUALITY_FAST ||
574 quality == display_compositor::GLHelper::SCALER_QUALITY_GOOD) {
575 ScaleSlow(input, output, quality);
576 return;
577 }
578
579 float xscale = static_cast<float>(output->width()) / input->width();
580
581 // This corresponds to all the operations we can do directly.
582 float yscale = static_cast<float>(output->height()) / input->height();
583 if ((xscale == 1.0f && yscale == 1.0f) ||
584 (xscale == 0.5f && yscale == 1.0f) ||
585 (xscale == 1.0f && yscale == 0.5f) ||
586 (xscale >= 1.0f && yscale == 1.0f) ||
587 (xscale == 1.0f && yscale >= 1.0f)) {
588 ScaleSlow(input, output, quality);
589 return;
590 }
591
592 // Now we break the problem down into smaller pieces, using the
593 // operations available.
594 int xtmp = input->width();
595 int ytmp = input->height();
596
597 if (output->height() != input->height()) {
598 ytmp = output->height();
599 while (ytmp < input->height() && ytmp * 2 != input->height()) {
600 ytmp += ytmp;
601 }
602 } else {
603 xtmp = output->width();
604 while (xtmp < input->width() && xtmp * 2 != input->width()) {
605 xtmp += xtmp;
606 }
607 }
608
609 SkBitmap tmp;
610 tmp.allocN32Pixels(xtmp, ytmp);
611
612 ScaleSlowRecursive(input, &tmp, quality);
613 ScaleSlowRecursive(&tmp, output, quality);
614 }
615
616 // Creates an RGBA SkBitmap
617 std::unique_ptr<SkBitmap> CreateTestBitmap(int width,
618 int height,
619 int test_pattern) {
620 std::unique_ptr<SkBitmap> bitmap(new SkBitmap);
621 bitmap->allocPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType,
622 kPremul_SkAlphaType));
623
624 for (int x = 0; x < width; ++x) {
625 for (int y = 0; y < height; ++y) {
626 switch (test_pattern) {
627 case 0: // Smooth test pattern
628 SetChannel(bitmap.get(), x, y, 0, x * 10);
629 SetChannel(bitmap.get(), x, y, 0, y == 0 ? x * 50 : x * 10);
630 SetChannel(bitmap.get(), x, y, 1, y * 10);
631 SetChannel(bitmap.get(), x, y, 2, (x + y) * 10);
632 SetChannel(bitmap.get(), x, y, 3, 255);
633 break;
634 case 1: // Small blocks
635 SetChannel(bitmap.get(), x, y, 0, x & 1 ? 255 : 0);
636 SetChannel(bitmap.get(), x, y, 1, y & 1 ? 255 : 0);
637 SetChannel(bitmap.get(), x, y, 2, (x + y) & 1 ? 255 : 0);
638 SetChannel(bitmap.get(), x, y, 3, 255);
639 break;
640 case 2: // Medium blocks
641 SetChannel(bitmap.get(), x, y, 0, 10 + x / 2 * 50);
642 SetChannel(bitmap.get(), x, y, 1, 10 + y / 3 * 50);
643 SetChannel(bitmap.get(), x, y, 2, (x + y) / 5 * 50 + 5);
644 SetChannel(bitmap.get(), x, y, 3, 255);
645 break;
646 }
647 }
648 }
649 return bitmap;
650 }
651
652 // Binds texture and framebuffer and loads the bitmap pixels into the texture.
653 void BindTextureAndFrameBuffer(GLuint texture,
654 GLuint framebuffer,
655 SkBitmap* bitmap,
656 int width,
657 int height) {
658 gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer);
659 gl_->BindTexture(GL_TEXTURE_2D, texture);
660 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
661 GL_UNSIGNED_BYTE, bitmap->getPixels());
662 }
663
664 // Create a test image, transform it using
665 // GLHelper::CropScaleReadbackAndCleanTexture and a reference implementation
666 // and compare the results.
667 void TestCropScaleReadbackAndCleanTexture(int xsize,
668 int ysize,
669 int scaled_xsize,
670 int scaled_ysize,
671 int test_pattern,
672 SkColorType out_color_type,
673 bool swizzle,
674 size_t quality_index) {
675 DCHECK(out_color_type == kAlpha_8_SkColorType ||
676 out_color_type == kRGBA_8888_SkColorType ||
677 out_color_type == kBGRA_8888_SkColorType);
678 GLuint src_texture;
679 gl_->GenTextures(1, &src_texture);
680 GLuint framebuffer;
681 gl_->GenFramebuffers(1, &framebuffer);
682 std::unique_ptr<SkBitmap> input_pixels =
683 CreateTestBitmap(xsize, ysize, test_pattern);
684 BindTextureAndFrameBuffer(src_texture, framebuffer, input_pixels.get(),
685 xsize, ysize);
686
687 std::string message = base::StringPrintf(
688 "input size: %dx%d "
689 "output size: %dx%d "
690 "pattern: %d , quality: %s, "
691 "out_color_type: %d",
692 xsize, ysize, scaled_xsize, scaled_ysize, test_pattern,
693 kQualityNames[quality_index], out_color_type);
694
695 // Transform the bitmap using GLHelper::CropScaleReadbackAndCleanTexture.
696 SkBitmap output_pixels;
697 output_pixels.allocPixels(SkImageInfo::Make(
698 scaled_xsize, scaled_ysize, out_color_type, kPremul_SkAlphaType));
699 base::RunLoop run_loop;
700 gfx::Size encoded_texture_size;
701 helper_->CropScaleReadbackAndCleanTexture(
702 src_texture, gfx::Size(xsize, ysize), gfx::Rect(xsize, ysize),
703 gfx::Size(scaled_xsize, scaled_ysize),
704 static_cast<unsigned char*>(output_pixels.getPixels()), out_color_type,
705 base::Bind(&callcallback, run_loop.QuitClosure()),
706 kQualities[quality_index]);
707 run_loop.Run();
708 // CropScaleReadbackAndCleanTexture flips the pixels. Flip them back.
709 FlipSKBitmap(&output_pixels);
710
711 // If the bitmap shouldn't have changed - compare against input.
712 if (xsize == scaled_xsize && ysize == scaled_ysize &&
713 out_color_type != kAlpha_8_SkColorType) {
714 const std::vector<GLHelperScaling::ScalerStage> dummy_stages;
715 Compare(input_pixels.get(), &output_pixels, 0, nullptr, dummy_stages,
716 message + " comparing against input");
717 return;
718 }
719
720 // Now transform the bitmap using the reference implementation.
721 SkBitmap scaled_pixels;
722 scaled_pixels.allocPixels(SkImageInfo::Make(scaled_xsize, scaled_ysize,
723 kRGBA_8888_SkColorType,
724 kPremul_SkAlphaType));
725 SkBitmap truth_pixels;
726 // Step 1: Scale
727 ScaleSlowRecursive(input_pixels.get(), &scaled_pixels,
728 kQualities[quality_index]);
729 // Step 2: Encode to grayscale if needed.
730 if (out_color_type == kAlpha_8_SkColorType) {
731 truth_pixels.allocPixels(SkImageInfo::Make(
732 scaled_xsize, scaled_ysize, out_color_type, kPremul_SkAlphaType));
733 EncodeToGrayscaleSlow(&scaled_pixels, &truth_pixels);
734 } else {
735 truth_pixels = scaled_pixels;
736 }
737
738 // Now compare the results.
739 const std::vector<GLHelperScaling::ScalerStage> dummy_stages;
740 Compare(&truth_pixels, &output_pixels, 2, input_pixels.get(), dummy_stages,
741 message + " comparing against transformed/scaled");
742
743 gl_->DeleteTextures(1, &src_texture);
744 gl_->DeleteFramebuffers(1, &framebuffer);
745 }
746
747 // Scaling test: Create a test image, scale it using GLHelperScaling
748 // and a reference implementation and compare the results.
749 void TestScale(int xsize,
750 int ysize,
751 int scaled_xsize,
752 int scaled_ysize,
753 int test_pattern,
754 size_t quality_index,
755 bool flip) {
756 GLuint src_texture;
757 gl_->GenTextures(1, &src_texture);
758 GLuint framebuffer;
759 gl_->GenFramebuffers(1, &framebuffer);
760 std::unique_ptr<SkBitmap> input_pixels =
761 CreateTestBitmap(xsize, ysize, test_pattern);
762 BindTextureAndFrameBuffer(src_texture, framebuffer, input_pixels.get(),
763 xsize, ysize);
764
765 std::string message = base::StringPrintf(
766 "input size: %dx%d "
767 "output size: %dx%d "
768 "pattern: %d quality: %s",
769 xsize, ysize, scaled_xsize, scaled_ysize, test_pattern,
770 kQualityNames[quality_index]);
771
772 std::vector<GLHelperScaling::ScalerStage> stages;
773 helper_scaling_->ComputeScalerStages(
774 kQualities[quality_index], gfx::Size(xsize, ysize),
775 gfx::Rect(0, 0, xsize, ysize), gfx::Size(scaled_xsize, scaled_ysize),
776 flip, false, &stages);
777 ValidateScalerStages(kQualities[quality_index], stages,
778 gfx::Size(scaled_xsize, scaled_ysize), message);
779
780 GLuint dst_texture = helper_->CopyAndScaleTexture(
781 src_texture, gfx::Size(xsize, ysize),
782 gfx::Size(scaled_xsize, scaled_ysize), flip, kQualities[quality_index]);
783
784 SkBitmap output_pixels;
785 output_pixels.allocPixels(SkImageInfo::Make(scaled_xsize, scaled_ysize,
786 kRGBA_8888_SkColorType,
787 kPremul_SkAlphaType));
788
789 helper_->ReadbackTextureSync(
790 dst_texture, gfx::Rect(0, 0, scaled_xsize, scaled_ysize),
791 static_cast<unsigned char*>(output_pixels.getPixels()),
792 kRGBA_8888_SkColorType);
793 if (flip) {
794 // Flip the pixels back.
795 FlipSKBitmap(&output_pixels);
796 }
797
798 // If the bitmap shouldn't have changed - compare against input.
799 if (xsize == scaled_xsize && ysize == scaled_ysize) {
800 Compare(input_pixels.get(), &output_pixels, 0, nullptr, stages,
801 message + " comparing against input");
802 return;
803 }
804
805 // Now scale the bitmap using the reference implementation.
806 SkBitmap truth_pixels;
807 truth_pixels.allocPixels(SkImageInfo::Make(scaled_xsize, scaled_ysize,
808 kRGBA_8888_SkColorType,
809 kPremul_SkAlphaType));
810 ScaleSlowRecursive(input_pixels.get(), &truth_pixels,
811 kQualities[quality_index]);
812 Compare(&truth_pixels, &output_pixels, 2, input_pixels.get(), stages,
813 message + " comparing against scaled");
814
815 gl_->DeleteTextures(1, &src_texture);
816 gl_->DeleteTextures(1, &dst_texture);
817 gl_->DeleteFramebuffers(1, &framebuffer);
818 }
819
820 // Create a scaling pipeline and check that it is made up of
821 // valid scaling operations.
822 void TestScalerPipeline(size_t quality,
823 int xsize,
824 int ysize,
825 int dst_xsize,
826 int dst_ysize) {
827 std::vector<GLHelperScaling::ScalerStage> stages;
828 helper_scaling_->ComputeScalerStages(
829 kQualities[quality], gfx::Size(xsize, ysize),
830 gfx::Rect(0, 0, xsize, ysize), gfx::Size(dst_xsize, dst_ysize), false,
831 false, &stages);
832 ValidateScalerStages(kQualities[quality], stages,
833 gfx::Size(dst_xsize, dst_ysize),
834 base::StringPrintf("input size: %dx%d "
835 "output size: %dx%d "
836 "quality: %s",
837 xsize, ysize, dst_xsize, dst_ysize,
838 kQualityNames[quality]));
839 }
840
841 // Create a scaling pipeline and make sure that the steps
842 // are exactly the steps we expect.
843 void CheckPipeline(display_compositor::GLHelper::ScalerQuality quality,
844 int xsize,
845 int ysize,
846 int dst_xsize,
847 int dst_ysize,
848 const std::string& description) {
849 std::vector<GLHelperScaling::ScalerStage> stages;
850 helper_scaling_->ComputeScalerStages(
851 quality, gfx::Size(xsize, ysize), gfx::Rect(0, 0, xsize, ysize),
852 gfx::Size(dst_xsize, dst_ysize), false, false, &stages);
853 ValidateScalerStages(display_compositor::GLHelper::SCALER_QUALITY_GOOD,
854 stages, gfx::Size(dst_xsize, dst_ysize), "");
855 EXPECT_EQ(PrintStages(stages), description);
856 }
857
858 static void callcallback(const base::Callback<void()>& callback,
859 bool result) {
860 callback.Run();
861 }
862
863 void DrawGridToBitmap(int w,
864 int h,
865 SkColor background_color,
866 SkColor grid_color,
867 int grid_pitch,
868 int grid_width,
869 SkBitmap& bmp) {
870 ASSERT_GT(grid_pitch, 0);
871 ASSERT_GT(grid_width, 0);
872 ASSERT_NE(background_color, grid_color);
873
874 for (int y = 0; y < h; ++y) {
875 bool y_on_grid = ((y % grid_pitch) < grid_width);
876
877 for (int x = 0; x < w; ++x) {
878 bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width));
879
880 if (bmp.colorType() == kRGBA_8888_SkColorType ||
881 bmp.colorType() == kBGRA_8888_SkColorType) {
882 *bmp.getAddr32(x, y) = (on_grid ? grid_color : background_color);
883 } else if (bmp.colorType() == kRGB_565_SkColorType) {
884 *bmp.getAddr16(x, y) = (on_grid ? grid_color : background_color);
885 }
886 }
887 }
888 }
889
890 void DrawCheckerToBitmap(int w,
891 int h,
892 SkColor color1,
893 SkColor color2,
894 int rect_w,
895 int rect_h,
896 SkBitmap& bmp) {
897 ASSERT_GT(rect_w, 0);
898 ASSERT_GT(rect_h, 0);
899 ASSERT_NE(color1, color2);
900
901 for (int y = 0; y < h; ++y) {
902 bool y_bit = (((y / rect_h) & 0x1) == 0);
903
904 for (int x = 0; x < w; ++x) {
905 bool x_bit = (((x / rect_w) & 0x1) == 0);
906
907 bool use_color2 = (x_bit != y_bit); // xor
908 if (bmp.colorType() == kRGBA_8888_SkColorType ||
909 bmp.colorType() == kBGRA_8888_SkColorType) {
910 *bmp.getAddr32(x, y) = (use_color2 ? color2 : color1);
911 } else if (bmp.colorType() == kRGB_565_SkColorType) {
912 *bmp.getAddr16(x, y) = (use_color2 ? color2 : color1);
913 }
914 }
915 }
916 }
917
918 bool ColorComponentsClose(SkColor component1,
919 SkColor component2,
920 SkColorType color_type) {
921 int c1 = static_cast<int>(component1);
922 int c2 = static_cast<int>(component2);
923 bool result = false;
924 switch (color_type) {
925 case kRGBA_8888_SkColorType:
926 case kBGRA_8888_SkColorType:
927 result = (std::abs(c1 - c2) == 0);
928 break;
929 case kRGB_565_SkColorType:
930 result = (std::abs(c1 - c2) <= 7);
931 break;
932 default:
933 break;
934 }
935 return result;
936 }
937
938 bool ColorsClose(SkColor color1, SkColor color2, SkColorType color_type) {
939 bool red = ColorComponentsClose(SkColorGetR(color1), SkColorGetR(color2),
940 color_type);
941 bool green = ColorComponentsClose(SkColorGetG(color1), SkColorGetG(color2),
942 color_type);
943 bool blue = ColorComponentsClose(SkColorGetB(color1), SkColorGetB(color2),
944 color_type);
945 bool alpha = ColorComponentsClose(SkColorGetA(color1), SkColorGetA(color2),
946 color_type);
947 if (color_type == kRGB_565_SkColorType) {
948 return red && blue && green;
949 }
950 return red && blue && green && alpha;
951 }
952
953 bool IsEqual(const SkBitmap& bmp1, const SkBitmap& bmp2) {
954 if (bmp1.isNull() && bmp2.isNull())
955 return true;
956 if (bmp1.width() != bmp2.width() || bmp1.height() != bmp2.height()) {
957 LOG(ERROR) << "Bitmap geometry check failure";
958 return false;
959 }
960 if (bmp1.colorType() != bmp2.colorType())
961 return false;
962
963 if (!bmp1.getPixels() || !bmp2.getPixels()) {
964 LOG(ERROR) << "Empty Bitmap!";
965 return false;
966 }
967 for (int y = 0; y < bmp1.height(); ++y) {
968 for (int x = 0; x < bmp1.width(); ++x) {
969 if (!ColorsClose(bmp1.getColor(x, y), bmp2.getColor(x, y),
970 bmp1.colorType())) {
971 LOG(ERROR) << "Bitmap color comparision failure";
972 return false;
973 }
974 }
975 }
976 return true;
977 }
978
979 void BindAndAttachTextureWithPixels(GLuint src_texture,
980 SkColorType color_type,
981 const gfx::Size& src_size,
982 const SkBitmap& input_pixels) {
983 gl_->BindTexture(GL_TEXTURE_2D, src_texture);
984 GLenum format = 0;
985 switch (color_type) {
986 case kBGRA_8888_SkColorType:
987 format = GL_BGRA_EXT;
988 break;
989 case kRGBA_8888_SkColorType:
990 format = GL_RGBA;
991 break;
992 case kRGB_565_SkColorType:
993 format = GL_RGB;
994 break;
995 default:
996 NOTREACHED();
997 }
998 GLenum type = (color_type == kRGB_565_SkColorType) ? GL_UNSIGNED_SHORT_5_6_5
999 : GL_UNSIGNED_BYTE;
1000 gl_->TexImage2D(GL_TEXTURE_2D, 0, format, src_size.width(),
1001 src_size.height(), 0, format, type,
1002 input_pixels.getPixels());
1003 }
1004
1005 void ReadBackTexture(GLuint src_texture,
1006 const gfx::Size& src_size,
1007 unsigned char* pixels,
1008 SkColorType color_type,
1009 bool async) {
1010 if (async) {
1011 base::RunLoop run_loop;
1012 helper_->ReadbackTextureAsync(
1013 src_texture, src_size, pixels, color_type,
1014 base::Bind(&callcallback, run_loop.QuitClosure()));
1015 run_loop.Run();
1016 } else {
1017 helper_->ReadbackTextureSync(src_texture, gfx::Rect(src_size), pixels,
1018 color_type);
1019 }
1020 }
1021 // Test basic format readback.
1022 bool TestTextureFormatReadback(const gfx::Size& src_size,
1023 SkColorType color_type,
1024 bool async) {
1025 SkImageInfo info = SkImageInfo::Make(src_size.width(), src_size.height(),
1026 color_type, kPremul_SkAlphaType);
1027 if (!helper_->IsReadbackConfigSupported(color_type)) {
1028 LOG(INFO) << "Skipping test format not supported" << color_type;
1029 return true;
1030 }
1031 GLuint src_texture;
1032 gl_->GenTextures(1, &src_texture);
1033 SkBitmap input_pixels;
1034 input_pixels.allocPixels(info);
1035 // Test Pattern-1, Fill with Plain color pattern.
1036 // Erase the input bitmap with red color.
1037 input_pixels.eraseColor(SK_ColorRED);
1038 BindAndAttachTextureWithPixels(src_texture, color_type, src_size,
1039 input_pixels);
1040 SkBitmap output_pixels;
1041 output_pixels.allocPixels(info);
1042 // Initialize the output bitmap with Green color.
1043 // When the readback is over output bitmap should have the red color.
1044 output_pixels.eraseColor(SK_ColorGREEN);
1045 uint8_t* pixels = static_cast<uint8_t*>(output_pixels.getPixels());
1046 ReadBackTexture(src_texture, src_size, pixels, color_type, async);
1047 bool result = IsEqual(input_pixels, output_pixels);
1048 if (!result) {
1049 LOG(ERROR) << "Bitmap comparision failure Pattern-1";
1050 return false;
1051 }
1052 const int rect_w = 10, rect_h = 4, src_grid_pitch = 10, src_grid_width = 4;
1053 const SkColor color1 = SK_ColorRED, color2 = SK_ColorBLUE;
1054 // Test Pattern-2, Fill with Grid Pattern.
1055 DrawGridToBitmap(src_size.width(), src_size.height(), color2, color1,
1056 src_grid_pitch, src_grid_width, input_pixels);
1057 BindAndAttachTextureWithPixels(src_texture, color_type, src_size,
1058 input_pixels);
1059 ReadBackTexture(src_texture, src_size, pixels, color_type, async);
1060 result = IsEqual(input_pixels, output_pixels);
1061 if (!result) {
1062 LOG(ERROR) << "Bitmap comparision failure Pattern-2";
1063 return false;
1064 }
1065 // Test Pattern-3, Fill with CheckerBoard Pattern.
1066 DrawCheckerToBitmap(src_size.width(), src_size.height(), color1, color2,
1067 rect_w, rect_h, input_pixels);
1068 BindAndAttachTextureWithPixels(src_texture, color_type, src_size,
1069 input_pixels);
1070 ReadBackTexture(src_texture, src_size, pixels, color_type, async);
1071 result = IsEqual(input_pixels, output_pixels);
1072 if (!result) {
1073 LOG(ERROR) << "Bitmap comparision failure Pattern-3";
1074 return false;
1075 }
1076 gl_->DeleteTextures(1, &src_texture);
1077 if (HasFailure()) {
1078 return false;
1079 }
1080 return true;
1081 }
1082
1083 void TestAddOps(int src, int dst, bool scale_x, bool allow3) {
1084 std::deque<GLHelperScaling::ScaleOp> ops;
1085 GLHelperScaling::ScaleOp::AddOps(src, dst, scale_x, allow3, &ops);
1086 // Scale factor 3 is a special case.
1087 // It is currently only allowed by itself.
1088 if (allow3 && dst * 3 >= src && dst * 2 < src) {
1089 EXPECT_EQ(ops[0].scale_factor, 3);
1090 EXPECT_EQ(ops.size(), 1U);
1091 EXPECT_EQ(ops[0].scale_x, scale_x);
1092 EXPECT_EQ(ops[0].scale_size, dst);
1093 return;
1094 }
1095
1096 for (size_t i = 0; i < ops.size(); i++) {
1097 EXPECT_EQ(ops[i].scale_x, scale_x);
1098 if (i == 0) {
1099 // Only the first op is allowed to be a scale up.
1100 // (Scaling up *after* scaling down would make it fuzzy.)
1101 EXPECT_TRUE(ops[0].scale_factor == 0 || ops[0].scale_factor == 2);
1102 } else {
1103 // All other operations must be 50% downscales.
1104 EXPECT_EQ(ops[i].scale_factor, 2);
1105 }
1106 }
1107 // Check that the scale factors make sense and add up.
1108 int tmp = dst;
1109 for (int i = static_cast<int>(ops.size() - 1); i >= 0; i--) {
1110 EXPECT_EQ(tmp, ops[i].scale_size);
1111 if (ops[i].scale_factor == 0) {
1112 EXPECT_EQ(i, 0);
1113 EXPECT_GT(tmp, src);
1114 tmp = src;
1115 } else {
1116 tmp *= ops[i].scale_factor;
1117 }
1118 }
1119 EXPECT_EQ(tmp, src);
1120 }
1121
1122 void CheckPipeline2(int xsize,
1123 int ysize,
1124 int dst_xsize,
1125 int dst_ysize,
1126 const std::string& description) {
1127 std::vector<GLHelperScaling::ScalerStage> stages;
1128 helper_scaling_->ConvertScalerOpsToScalerStages(
1129 display_compositor::GLHelper::SCALER_QUALITY_GOOD,
1130 gfx::Size(xsize, ysize), gfx::Rect(0, 0, xsize, ysize),
1131 gfx::Size(dst_xsize, dst_ysize), false, false, &x_ops_, &y_ops_,
1132 &stages);
1133 EXPECT_EQ(x_ops_.size(), 0U);
1134 EXPECT_EQ(y_ops_.size(), 0U);
1135 ValidateScalerStages(display_compositor::GLHelper::SCALER_QUALITY_GOOD,
1136 stages, gfx::Size(dst_xsize, dst_ysize), "");
1137 EXPECT_EQ(PrintStages(stages), description);
1138 }
1139
1140 void CheckOptimizationsTest() {
1141 // Basic upscale. X and Y should be combined into one pass.
1142 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000));
1143 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000));
1144 CheckPipeline2(1024, 768, 2000, 2000, "1024x768 -> 2000x2000 bilinear\n");
1145
1146 // X scaled 1/2, Y upscaled, should still be one pass.
1147 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512));
1148 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 2000));
1149 CheckPipeline2(1024, 768, 512, 2000, "1024x768 -> 512x2000 bilinear\n");
1150
1151 // X upscaled, Y scaled 1/2, one bilinear pass
1152 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 2000));
1153 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384));
1154 CheckPipeline2(1024, 768, 2000, 384, "1024x768 -> 2000x384 bilinear\n");
1155
1156 // X scaled 1/2, Y scaled 1/2, one bilinear pass
1157 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 512));
1158 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 384));
1159 CheckPipeline2(1024, 768, 512, 384, "1024x768 -> 512x384 bilinear\n");
1160
1161 // X scaled 1/2, Y scaled to 60%, one bilinear2 pass.
1162 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50));
1163 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1164 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1165 CheckPipeline2(100, 100, 50, 60, "100x100 -> 50x60 bilinear2 Y\n");
1166
1167 // X scaled to 60%, Y scaled 1/2, one bilinear2 pass.
1168 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1169 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1170 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 50));
1171 CheckPipeline2(100, 100, 60, 50, "100x100 -> 60x50 bilinear2 X\n");
1172
1173 // X scaled to 60%, Y scaled 60%, one bilinear2x2 pass.
1174 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1175 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1176 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1177 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1178 CheckPipeline2(100, 100, 60, 60, "100x100 -> 60x60 bilinear2x2\n");
1179
1180 // X scaled to 40%, Y scaled 40%, two bilinear3 passes.
1181 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40));
1182 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40));
1183 CheckPipeline2(100, 100, 40, 40,
1184 "100x100 -> 100x40 bilinear3 Y\n"
1185 "100x40 -> 40x40 bilinear3 X\n");
1186
1187 // X scaled to 60%, Y scaled 40%
1188 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1189 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1190 y_ops_.push_back(GLHelperScaling::ScaleOp(3, false, 40));
1191 CheckPipeline2(100, 100, 60, 40,
1192 "100x100 -> 100x40 bilinear3 Y\n"
1193 "100x40 -> 60x40 bilinear2 X\n");
1194
1195 // X scaled to 40%, Y scaled 60%
1196 x_ops_.push_back(GLHelperScaling::ScaleOp(3, true, 40));
1197 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1198 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1199 CheckPipeline2(100, 100, 40, 60,
1200 "100x100 -> 100x60 bilinear2 Y\n"
1201 "100x60 -> 40x60 bilinear3 X\n");
1202
1203 // X scaled to 30%, Y scaled 30%
1204 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 120));
1205 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 60));
1206 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 30));
1207 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1208 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1209 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1210 CheckPipeline2(100, 100, 30, 30,
1211 "100x100 -> 100x30 bilinear4 Y\n"
1212 "100x30 -> 30x30 bilinear4 X\n");
1213
1214 // X scaled to 50%, Y scaled 30%
1215 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 50));
1216 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1217 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1218 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1219 CheckPipeline2(100, 100, 50, 30, "100x100 -> 50x30 bilinear4 Y\n");
1220
1221 // X scaled to 150%, Y scaled 30%
1222 // Note that we avoid combinding X and Y passes
1223 // as that would probably be LESS efficient here.
1224 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 150));
1225 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 120));
1226 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 60));
1227 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 30));
1228 CheckPipeline2(100, 100, 150, 30,
1229 "100x100 -> 100x30 bilinear4 Y\n"
1230 "100x30 -> 150x30 bilinear\n");
1231
1232 // X scaled to 1%, Y scaled 1%
1233 x_ops_.push_back(GLHelperScaling::ScaleOp(0, true, 128));
1234 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 64));
1235 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 32));
1236 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 16));
1237 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 8));
1238 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 4));
1239 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 2));
1240 x_ops_.push_back(GLHelperScaling::ScaleOp(2, true, 1));
1241 y_ops_.push_back(GLHelperScaling::ScaleOp(0, false, 128));
1242 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 64));
1243 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 32));
1244 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 16));
1245 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 8));
1246 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 4));
1247 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 2));
1248 y_ops_.push_back(GLHelperScaling::ScaleOp(2, false, 1));
1249 CheckPipeline2(100, 100, 1, 1,
1250 "100x100 -> 100x32 bilinear4 Y\n"
1251 "100x32 -> 100x4 bilinear4 Y\n"
1252 "100x4 -> 64x1 bilinear2x2\n"
1253 "64x1 -> 8x1 bilinear4 X\n"
1254 "8x1 -> 1x1 bilinear4 X\n");
1255 }
1256
1257 std::unique_ptr<gpu::GLInProcessContext> context_;
1258 gpu::gles2::GLES2Interface* gl_;
1259 std::unique_ptr<display_compositor::GLHelper> helper_;
1260 std::unique_ptr<display_compositor::GLHelperScaling> helper_scaling_;
1261 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_;
1262 base::MessageLoop message_loop_;
1263 };
1264
1265 class GLHelperPixelTest : public GLHelperTest {
1266 private:
1267 gl::DisableNullDrawGLBindings enable_pixel_output_;
1268 };
1269
1270 TEST_F(GLHelperTest, RGBASyncReadbackTest) {
1271 const int kTestSize = 64;
1272 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize),
1273 kRGBA_8888_SkColorType, false);
1274 EXPECT_EQ(result, true);
1275 }
1276
1277 TEST_F(GLHelperTest, BGRASyncReadbackTest) {
1278 const int kTestSize = 64;
1279 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize),
1280 kBGRA_8888_SkColorType, false);
1281 EXPECT_EQ(result, true);
1282 }
1283
1284 TEST_F(GLHelperTest, RGB565SyncReadbackTest) {
1285 const int kTestSize = 64;
1286 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize),
1287 kRGB_565_SkColorType, false);
1288 EXPECT_EQ(result, true);
1289 }
1290
1291 TEST_F(GLHelperTest, RGBAASyncReadbackTest) {
1292 const int kTestSize = 64;
1293 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize),
1294 kRGBA_8888_SkColorType, true);
1295 EXPECT_EQ(result, true);
1296 }
1297
1298 TEST_F(GLHelperTest, BGRAASyncReadbackTest) {
1299 const int kTestSize = 64;
1300 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize),
1301 kBGRA_8888_SkColorType, true);
1302 EXPECT_EQ(result, true);
1303 }
1304
1305 TEST_F(GLHelperTest, RGB565ASyncReadbackTest) {
1306 const int kTestSize = 64;
1307 bool result = TestTextureFormatReadback(gfx::Size(kTestSize, kTestSize),
1308 kRGB_565_SkColorType, true);
1309 EXPECT_EQ(result, true);
1310 }
1311
1312 int kRGBReadBackSizes[] = {3, 6, 16};
1313
1314 class GLHelperPixelReadbackTest
1315 : public GLHelperPixelTest,
1316 public ::testing::WithParamInterface<std::tr1::tuple<unsigned int,
1317 unsigned int,
1318 unsigned int,
1319 unsigned int,
1320 unsigned int>> {};
1321
1322 // Per pixel tests, all sizes are small so that we can print
1323 // out the generated bitmaps.
1324 TEST_P(GLHelperPixelReadbackTest, ScaleTest) {
1325 unsigned int q_index = std::tr1::get<0>(GetParam());
1326 unsigned int x = std::tr1::get<1>(GetParam());
1327 unsigned int y = std::tr1::get<2>(GetParam());
1328 unsigned int dst_x = std::tr1::get<3>(GetParam());
1329 unsigned int dst_y = std::tr1::get<4>(GetParam());
1330
1331 for (int flip = 0; flip <= 1; flip++) {
1332 for (int pattern = 0; pattern < 3; pattern++) {
1333 TestScale(kRGBReadBackSizes[x], kRGBReadBackSizes[y],
1334 kRGBReadBackSizes[dst_x], kRGBReadBackSizes[dst_y], pattern,
1335 q_index, flip == 1);
1336 if (HasFailure()) {
1337 return;
1338 }
1339 }
1340 }
1341 }
1342
1343 // Per pixel tests, all sizes are small so that we can print
1344 // out the generated bitmaps.
1345 TEST_P(GLHelperPixelReadbackTest, CropScaleReadbackAndCleanTextureTest) {
1346 unsigned int q_index = std::tr1::get<0>(GetParam());
1347 unsigned int x = std::tr1::get<1>(GetParam());
1348 unsigned int y = std::tr1::get<2>(GetParam());
1349 unsigned int dst_x = std::tr1::get<3>(GetParam());
1350 unsigned int dst_y = std::tr1::get<4>(GetParam());
1351
1352 const SkColorType kColorTypes[] = {
1353 kAlpha_8_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType};
1354 for (size_t color_type = 0; color_type < arraysize(kColorTypes);
1355 color_type++) {
1356 for (int pattern = 0; pattern < 3; pattern++) {
1357 TestCropScaleReadbackAndCleanTexture(
1358 kRGBReadBackSizes[x], kRGBReadBackSizes[y], kRGBReadBackSizes[dst_x],
1359 kRGBReadBackSizes[dst_y], pattern, kColorTypes[color_type], false,
1360 q_index);
1361 if (HasFailure())
1362 return;
1363 }
1364 }
1365 }
1366
1367 INSTANTIATE_TEST_CASE_P(
1368 ,
1369 GLHelperPixelReadbackTest,
1370 ::testing::Combine(
1371 ::testing::Range<unsigned int>(0, arraysize(kQualities)),
1372 ::testing::Range<unsigned int>(0, arraysize(kRGBReadBackSizes)),
1373 ::testing::Range<unsigned int>(0, arraysize(kRGBReadBackSizes)),
1374 ::testing::Range<unsigned int>(0, arraysize(kRGBReadBackSizes)),
1375 ::testing::Range<unsigned int>(0, arraysize(kRGBReadBackSizes))));
1376
1377 // Validate that all scaling generates valid pipelines.
1378 TEST_F(GLHelperTest, ValidateScalerPipelines) {
1379 int sizes[] = {7, 99, 128, 256, 512, 719, 720, 721, 1920, 2011, 3217, 4096};
1380 for (size_t q = 0; q < arraysize(kQualities); q++) {
1381 for (size_t x = 0; x < arraysize(sizes); x++) {
1382 for (size_t y = 0; y < arraysize(sizes); y++) {
1383 for (size_t dst_x = 0; dst_x < arraysize(sizes); dst_x++) {
1384 for (size_t dst_y = 0; dst_y < arraysize(sizes); dst_y++) {
1385 TestScalerPipeline(q, sizes[x], sizes[y], sizes[dst_x],
1386 sizes[dst_y]);
1387 if (HasFailure()) {
1388 return;
1389 }
1390 }
1391 }
1392 }
1393 }
1394 }
1395 }
1396
1397 // Make sure we don't create overly complicated pipelines
1398 // for a few common use cases.
1399 TEST_F(GLHelperTest, CheckSpecificPipelines) {
1400 // Upscale should be single pass.
1401 CheckPipeline(display_compositor::GLHelper::SCALER_QUALITY_GOOD, 1024, 700,
1402 1280, 720, "1024x700 -> 1280x720 bilinear\n");
1403 // Slight downscale should use BILINEAR2X2.
1404 CheckPipeline(display_compositor::GLHelper::SCALER_QUALITY_GOOD, 1280, 720,
1405 1024, 700, "1280x720 -> 1024x700 bilinear2x2\n");
1406 // Most common tab capture pipeline on the Pixel.
1407 // Should be using two BILINEAR3 passes.
1408 CheckPipeline(display_compositor::GLHelper::SCALER_QUALITY_GOOD, 2560, 1476,
1409 1249, 720,
1410 "2560x1476 -> 2560x720 bilinear3 Y\n"
1411 "2560x720 -> 1249x720 bilinear3 X\n");
1412 }
1413
1414 TEST_F(GLHelperTest, ScalerOpTest) {
1415 for (int allow3 = 0; allow3 <= 1; allow3++) {
1416 for (int dst = 1; dst < 2049; dst += 1 + (dst >> 3)) {
1417 for (int src = 1; src < 2049; src++) {
1418 TestAddOps(src, dst, allow3 == 1, (src & 1) == 1);
1419 if (HasFailure()) {
1420 LOG(ERROR) << "Failed for src=" << src << " dst=" << dst
1421 << " allow3=" << allow3;
1422 return;
1423 }
1424 }
1425 }
1426 }
1427 }
1428
1429 TEST_F(GLHelperTest, CheckOptimizations) {
1430 // Test in baseclass since it is friends with GLHelperScaling
1431 CheckOptimizationsTest();
1432 }
1433
1434 } // namespace display_compositor
OLDNEW
« no previous file with comments | « components/display_compositor/gl_helper_scaling.cc ('k') | components/display_compositor/host_shared_bitmap_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698