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

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

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

Powered by Google App Engine
This is Rietveld 408576698