Work around WebKeyboardEvent.domCode not being set on OS X.
[chromium-blink-merge.git] / media / base / yuv_convert_perftest.cc
blob7d3d64c956cf298ab45e0e3dc4996f2fd69d7fc5
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.
5 #include "base/base_paths.h"
6 #include "base/cpu.h"
7 #include "base/files/file_util.h"
8 #include "base/logging.h"
9 #include "base/path_service.h"
10 #include "base/time/time.h"
11 #include "media/base/simd/convert_yuv_to_rgb.h"
12 #include "media/base/yuv_convert.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/perf/perf_test.h"
16 namespace media {
17 #if !defined(ARCH_CPU_ARM_FAMILY) && !defined(ARCH_CPU_MIPS_FAMILY)
18 // Size of raw image.
19 static const int kSourceWidth = 640;
20 static const int kSourceHeight = 360;
21 static const int kSourceYSize = kSourceWidth * kSourceHeight;
22 static const int kSourceUOffset = kSourceYSize;
23 static const int kSourceVOffset = kSourceYSize * 5 / 4;
24 static const int kBpp = 4;
26 // Width of the row to convert. Odd so that we exercise the ending
27 // one-pixel-leftover case.
28 static const int kWidth = 639;
30 // Surface sizes for various test files.
31 static const int kYUV12Size = kSourceYSize * 12 / 8;
32 static const int kRGBSize = kSourceYSize * kBpp;
34 static const int kPerfTestIterations = 2000;
36 class YUVConvertPerfTest : public testing::Test {
37 public:
38 YUVConvertPerfTest()
39 : yuv_bytes_(new uint8[kYUV12Size]),
40 rgb_bytes_converted_(new uint8[kRGBSize]) {
41 base::FilePath path;
42 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &path));
43 path = path.Append(FILE_PATH_LITERAL("media"))
44 .Append(FILE_PATH_LITERAL("test"))
45 .Append(FILE_PATH_LITERAL("data"))
46 .Append(FILE_PATH_LITERAL("bali_640x360_P420.yuv"));
48 // Verify file size is correct.
49 int64 actual_size = 0;
50 base::GetFileSize(path, &actual_size);
51 CHECK_EQ(actual_size, kYUV12Size);
53 // Verify bytes read are correct.
54 int bytes_read = base::ReadFile(
55 path, reinterpret_cast<char*>(yuv_bytes_.get()), kYUV12Size);
57 CHECK_EQ(bytes_read, kYUV12Size);
60 scoped_ptr<uint8[]> yuv_bytes_;
61 scoped_ptr<uint8[]> rgb_bytes_converted_;
63 private:
64 DISALLOW_COPY_AND_ASSIGN(YUVConvertPerfTest);
67 TEST_F(YUVConvertPerfTest, ConvertYUVToRGB32Row_SSE) {
68 ASSERT_TRUE(base::CPU().has_sse());
70 base::TimeTicks start = base::TimeTicks::Now();
71 for (int i = 0; i < kPerfTestIterations; ++i) {
72 for (int row = 0; row < kSourceHeight; ++row) {
73 int chroma_row = row / 2;
74 ConvertYUVToRGB32Row_SSE(
75 yuv_bytes_.get() + row * kSourceWidth,
76 yuv_bytes_.get() + kSourceUOffset + (chroma_row * kSourceWidth / 2),
77 yuv_bytes_.get() + kSourceVOffset + (chroma_row * kSourceWidth / 2),
78 rgb_bytes_converted_.get(),
79 kWidth,
80 GetLookupTable(YV12));
83 double total_time_seconds = (base::TimeTicks::Now() - start).InSecondsF();
84 perf_test::PrintResult(
85 "yuv_convert_perftest", "", "ConvertYUVToRGB32Row_SSE",
86 kPerfTestIterations / total_time_seconds, "runs/s", true);
87 media::EmptyRegisterState();
90 // 64-bit release + component builds on Windows are too smart and optimizes
91 // away the function being tested.
92 #if defined(OS_WIN) && (defined(ARCH_CPU_X86) || !defined(COMPONENT_BUILD))
93 TEST_F(YUVConvertPerfTest, ScaleYUVToRGB32Row_SSE) {
94 ASSERT_TRUE(base::CPU().has_sse());
96 const int kSourceDx = 80000; // This value means a scale down.
98 base::TimeTicks start = base::TimeTicks::Now();
99 for (int i = 0; i < kPerfTestIterations; ++i) {
100 for (int row = 0; row < kSourceHeight; ++row) {
101 int chroma_row = row / 2;
102 ScaleYUVToRGB32Row_SSE(
103 yuv_bytes_.get() + row * kSourceWidth,
104 yuv_bytes_.get() + kSourceUOffset + (chroma_row * kSourceWidth / 2),
105 yuv_bytes_.get() + kSourceVOffset + (chroma_row * kSourceWidth / 2),
106 rgb_bytes_converted_.get(),
107 kWidth,
108 kSourceDx,
109 GetLookupTable(YV12));
112 double total_time_seconds = (base::TimeTicks::Now() - start).InSecondsF();
113 perf_test::PrintResult(
114 "yuv_convert_perftest", "", "ScaleYUVToRGB32Row_SSE",
115 kPerfTestIterations / total_time_seconds, "runs/s", true);
116 media::EmptyRegisterState();
119 TEST_F(YUVConvertPerfTest, LinearScaleYUVToRGB32Row_SSE) {
120 ASSERT_TRUE(base::CPU().has_sse());
122 const int kSourceDx = 80000; // This value means a scale down.
124 base::TimeTicks start = base::TimeTicks::Now();
125 for (int i = 0; i < kPerfTestIterations; ++i) {
126 for (int row = 0; row < kSourceHeight; ++row) {
127 int chroma_row = row / 2;
128 LinearScaleYUVToRGB32Row_SSE(
129 yuv_bytes_.get() + row * kSourceWidth,
130 yuv_bytes_.get() + kSourceUOffset + (chroma_row * kSourceWidth / 2),
131 yuv_bytes_.get() + kSourceVOffset + (chroma_row * kSourceWidth / 2),
132 rgb_bytes_converted_.get(),
133 kWidth,
134 kSourceDx,
135 GetLookupTable(YV12));
138 double total_time_seconds = (base::TimeTicks::Now() - start).InSecondsF();
139 perf_test::PrintResult(
140 "yuv_convert_perftest", "", "LinearScaleYUVToRGB32Row_SSE",
141 kPerfTestIterations / total_time_seconds, "runs/s", true);
142 media::EmptyRegisterState();
144 #endif // defined(OS_WIN) && (ARCH_CPU_X86 || COMPONENT_BUILD)
146 #endif // !defined(ARCH_CPU_ARM_FAMILY) && !defined(ARCH_CPU_MIPS_FAMILY)
148 } // namespace media