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