1/*
2 *  Copyright 2012 The LibYuv Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS. All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <stdlib.h>
12#include <time.h>
13
14#include "libyuv/cpu_id.h"
15#include "libyuv/rotate_argb.h"
16#include "libyuv/row.h"
17#include "../unit_test/unit_test.h"
18
19namespace libyuv {
20
21void TestRotateBpp(int src_width, int src_height,
22                   int dst_width, int dst_height,
23                   libyuv::RotationMode mode,
24                   int benchmark_iterations,
25                   const int kBpp) {
26  if (src_width < 1) {
27    src_width = 1;
28  }
29  if (src_height < 1) {
30    src_height = 1;
31  }
32  if (dst_width < 1) {
33    dst_width = 1;
34  }
35  if (dst_height < 1) {
36    dst_height = 1;
37  }
38  int src_stride_argb = src_width * kBpp;
39  int src_argb_plane_size = src_stride_argb * src_height;
40  align_buffer_64(src_argb, src_argb_plane_size);
41  for (int i = 0; i < src_argb_plane_size; ++i) {
42    src_argb[i] = random() & 0xff;
43  }
44
45  int dst_stride_argb = dst_width * kBpp;
46  int dst_argb_plane_size = dst_stride_argb * dst_height;
47  align_buffer_64(dst_argb_c, dst_argb_plane_size);
48  align_buffer_64(dst_argb_opt, dst_argb_plane_size);
49  memset(dst_argb_c, 2, dst_argb_plane_size);
50  memset(dst_argb_opt, 3, dst_argb_plane_size);
51
52  if (kBpp == 1) {
53    MaskCpuFlags(0);  // Disable all CPU optimization.
54    RotatePlane(src_argb, src_stride_argb,
55                dst_argb_c, dst_stride_argb,
56                src_width, src_height, mode);
57
58    MaskCpuFlags(-1);  // Enable all CPU optimization.
59    for (int i = 0; i < benchmark_iterations; ++i) {
60      RotatePlane(src_argb, src_stride_argb,
61                  dst_argb_opt, dst_stride_argb,
62                  src_width, src_height, mode);
63    }
64  } else if (kBpp == 4) {
65    MaskCpuFlags(0);  // Disable all CPU optimization.
66    ARGBRotate(src_argb, src_stride_argb,
67               dst_argb_c, dst_stride_argb,
68               src_width, src_height, mode);
69
70    MaskCpuFlags(-1);  // Enable all CPU optimization.
71    for (int i = 0; i < benchmark_iterations; ++i) {
72      ARGBRotate(src_argb, src_stride_argb,
73                 dst_argb_opt, dst_stride_argb,
74                 src_width, src_height, mode);
75    }
76  }
77
78  // Rotation should be exact.
79  for (int i = 0; i < dst_argb_plane_size; ++i) {
80    EXPECT_EQ(dst_argb_c[i], dst_argb_opt[i]);
81  }
82
83  free_aligned_buffer_64(dst_argb_c);
84  free_aligned_buffer_64(dst_argb_opt);
85  free_aligned_buffer_64(src_argb);
86}
87
88static void ARGBTestRotate(int src_width, int src_height,
89                           int dst_width, int dst_height,
90                           libyuv::RotationMode mode,
91                           int benchmark_iterations) {
92  TestRotateBpp(src_width, src_height,
93                dst_width, dst_height,
94                mode, benchmark_iterations, 4);
95}
96
97TEST_F(libyuvTest, ARGBRotate0) {
98  ARGBTestRotate(benchmark_width_, benchmark_height_,
99                 benchmark_width_, benchmark_height_,
100                 kRotate0, benchmark_iterations_);
101}
102
103TEST_F(libyuvTest, ARGBRotate90) {
104  ARGBTestRotate(benchmark_width_, benchmark_height_,
105                 benchmark_height_, benchmark_width_,
106                 kRotate90, benchmark_iterations_);
107}
108
109TEST_F(libyuvTest, ARGBRotate180) {
110  ARGBTestRotate(benchmark_width_, benchmark_height_,
111                 benchmark_width_, benchmark_height_,
112                 kRotate180, benchmark_iterations_);
113}
114
115TEST_F(libyuvTest, ARGBRotate270) {
116  ARGBTestRotate(benchmark_width_, benchmark_height_,
117                 benchmark_height_, benchmark_width_,
118                 kRotate270, benchmark_iterations_);
119}
120
121TEST_F(libyuvTest, ARGBRotate0_Odd) {
122  ARGBTestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
123                 benchmark_width_ - 3, benchmark_height_ - 1,
124                 kRotate0, benchmark_iterations_);
125}
126
127TEST_F(libyuvTest, ARGBRotate90_Odd) {
128  ARGBTestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
129                 benchmark_height_ - 1, benchmark_width_ - 3,
130                 kRotate90, benchmark_iterations_);
131}
132
133TEST_F(libyuvTest, ARGBRotate180_Odd) {
134  ARGBTestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
135                 benchmark_width_ - 3, benchmark_height_ - 1,
136                 kRotate180, benchmark_iterations_);
137}
138
139TEST_F(libyuvTest, ARGBRotate270_Odd) {
140  ARGBTestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
141                 benchmark_height_ - 1, benchmark_width_ - 3,
142                 kRotate270, benchmark_iterations_);
143}
144
145static void TestRotatePlane(int src_width, int src_height,
146                            int dst_width, int dst_height,
147                            libyuv::RotationMode mode,
148                            int benchmark_iterations) {
149  TestRotateBpp(src_width, src_height,
150                dst_width, dst_height,
151                mode, benchmark_iterations, 1);
152}
153
154TEST_F(libyuvTest, RotatePlane0) {
155  TestRotatePlane(benchmark_width_, benchmark_height_,
156                  benchmark_width_, benchmark_height_,
157                  kRotate0, benchmark_iterations_);
158}
159
160TEST_F(libyuvTest, RotatePlane90) {
161  TestRotatePlane(benchmark_width_, benchmark_height_,
162                  benchmark_height_, benchmark_width_,
163                  kRotate90, benchmark_iterations_);
164}
165
166TEST_F(libyuvTest, RotatePlane180) {
167  TestRotatePlane(benchmark_width_, benchmark_height_,
168                  benchmark_width_, benchmark_height_,
169                  kRotate180, benchmark_iterations_);
170}
171
172TEST_F(libyuvTest, RotatePlane270) {
173  TestRotatePlane(benchmark_width_, benchmark_height_,
174                  benchmark_height_, benchmark_width_,
175                  kRotate270, benchmark_iterations_);
176}
177
178TEST_F(libyuvTest, RotatePlane0_Odd) {
179  TestRotatePlane(benchmark_width_ - 3, benchmark_height_ - 1,
180                  benchmark_width_ - 3, benchmark_height_ - 1,
181                  kRotate0, benchmark_iterations_);
182}
183
184TEST_F(libyuvTest, RotatePlane90_Odd) {
185  TestRotatePlane(benchmark_width_ - 3, benchmark_height_ - 1,
186                  benchmark_height_ - 1, benchmark_width_ - 3,
187                  kRotate90, benchmark_iterations_);
188}
189
190TEST_F(libyuvTest, RotatePlane180_Odd) {
191  TestRotatePlane(benchmark_width_ - 3, benchmark_height_ - 1,
192                  benchmark_width_ - 3, benchmark_height_ - 1,
193                  kRotate180, benchmark_iterations_);
194}
195
196TEST_F(libyuvTest, RotatePlane270_Odd) {
197  TestRotatePlane(benchmark_width_ - 3, benchmark_height_ - 1,
198                  benchmark_height_ - 1, benchmark_width_ - 3,
199                  kRotate270, benchmark_iterations_);
200}
201
202}  // namespace libyuv
203