File: JPEGImageEncoderTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (233 lines) | stat: -rw-r--r-- 7,938 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/timer/elapsed_timer.h"
#include "platform/image-encoders/RGBAtoRGB.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/build_config.h"

namespace blink {

class RGBAtoRGBTest : public ::testing::Test {
 public:
  RGBAtoRGBTest() {}
};

static const size_t channelsRGBA = 4;
static const size_t channelsRGB = 3;

inline size_t calculateRGBAPixels(size_t inputBufferSize) {
  size_t pixels = inputBufferSize / channelsRGBA;
  return pixels;
}

inline size_t calculateRGBOutputSize(size_t inputBufferSize) {
  size_t pixels = calculateRGBAPixels(inputBufferSize);
  pixels *= channelsRGB;
  return pixels;
}

TEST_F(RGBAtoRGBTest, testOpaqueCaseEven8pixels) {
  unsigned char canvas[] = {255, 0,   0, 255, 255, 0,   0,   255, 255, 0,  0,
                            255, 255, 0, 0,   255, 0,   255, 0,   255, 0,  255,
                            0,   255, 0, 255, 0,   255, 0,   255, 0,   255};

  unsigned char expected[] = {255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0,
                              255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0};
#if OS(WIN)
  // Windows release bot can't be reasoned with (compiler error C2131).
  static const constexpr size_t pixels = sizeof(canvas) / channelsRGBA;
  static const constexpr size_t rgbSize = pixels * channelsRGB;
#else
  const size_t pixels = calculateRGBAPixels(sizeof(canvas));
  const size_t rgbSize = calculateRGBOutputSize(sizeof(canvas));
#endif

  unsigned char output[rgbSize];
  memset(output, 0, rgbSize);

  blink::RGBAtoRGB(canvas, static_cast<unsigned>(pixels), output);

  EXPECT_EQ(memcmp(expected, output, rgbSize), 0);
}

#ifdef __ARM_NEON__
TEST_F(RGBAtoRGBTest, testCaseEven16pixels) {
  unsigned char canvas[] = {
      255, 0,   0,   255, 255, 0,   0,   255, 255, 0,   0,   255, 255,
      0,   0,   255, 0,   255, 0,   255, 0,   255, 0,   255, 0,   255,
      0,   255, 0,   255, 0,   255, 0,   0,   255, 128, 0,   0,   255,
      128, 0,   0,   255, 128, 0,   0,   255, 128, 128, 128, 128, 128,
      128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128};

  const size_t pixels = calculateRGBAPixels(sizeof(canvas));
  const size_t rgbSize = calculateRGBOutputSize(sizeof(canvas));
  unsigned char output[rgbSize];
  unsigned char expected[rgbSize];
  memset(output, 0, rgbSize);
  memset(expected, 0, rgbSize);

  blink::RGBAtoRGBScalar(canvas, static_cast<unsigned>(pixels), expected);
  blink::RGBAtoRGBNeon(canvas, static_cast<unsigned>(pixels), output);

  EXPECT_EQ(memcmp(expected, output, rgbSize), 0);
}

TEST_F(RGBAtoRGBTest, testCaseOdd17pixels) {
  unsigned char canvas[] = {
      255, 0,   0,   255, 255, 0,   0,   255, 255, 0,   0,   255, 255, 0,
      0,   255, 0,   255, 0,   255, 0,   255, 0,   255, 0,   255, 0,   255,
      0,   255, 0,   255, 0,   0,   255, 128, 0,   0,   255, 128, 0,   0,
      255, 128, 0,   0,   255, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      128, 128, 128, 128, 128, 128, 128, 128, 10,  10,  10,  100};

  const size_t pixels = calculateRGBAPixels(sizeof(canvas));
  const size_t rgbSize = calculateRGBOutputSize(sizeof(canvas));
  unsigned char output[rgbSize];
  unsigned char expected[rgbSize];
  memset(output, 0, rgbSize);
  memset(expected, 0, rgbSize);

  blink::RGBAtoRGBScalar(canvas, static_cast<unsigned>(pixels), expected);
  blink::RGBAtoRGBNeon(canvas, static_cast<unsigned>(pixels), output);

  EXPECT_EQ(memcmp(expected, output, rgbSize), 0);
}

TEST_F(RGBAtoRGBTest, testCaseEven32pixels) {
  unsigned char canvas[] = {
      255, 0,   0,   255, 255, 0,   0,   255, 255, 0,   0,   255, 255, 0,   0,
      255, 0,   255, 0,   255, 0,   255, 0,   255, 0,   255, 0,   255, 0,   255,
      0,   255, 0,   0,   255, 128, 0,   0,   255, 128, 0,   0,   255, 128, 0,
      0,   255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
      128, 128, 128, 128, 128, 128, 128, 255, 128, 128, 128, 255, 128, 128, 128,
      255, 128, 128, 128, 255, 255, 0,   0,   255, 255, 0,   0,   255, 255, 0,
      0,   255, 255, 0,   0,   255, 0,   255, 0,   255, 0,   255, 0,   255, 0,
      255, 0,   255, 0,   255, 0,   255, 0,   0,   255, 128, 0,   0,   255, 128,
      0,   0,   255, 128, 0,   0,   255, 128};

  const size_t pixels = calculateRGBAPixels(sizeof(canvas));
  const size_t rgbSize = calculateRGBOutputSize(sizeof(canvas));
  unsigned char output[rgbSize];
  unsigned char expected[rgbSize];
  memset(output, 0, rgbSize);
  memset(expected, 0, rgbSize);

  blink::RGBAtoRGBScalar(canvas, static_cast<unsigned>(pixels), expected);
  blink::RGBAtoRGBNeon(canvas, static_cast<unsigned>(pixels), output);

  EXPECT_EQ(memcmp(expected, output, rgbSize), 0);
}

static base::TimeDelta testNpixels(bool fastPath = true,
                                   const size_t width = 1024,
                                   const size_t height = 1024,
                                   bool setAlpha = true) {
  const size_t pixels = width * height;
  const size_t canvasLen = channelsRGBA * width * height;
  const size_t outputLen = channelsRGB * width * height;
  unsigned char* canvas = new unsigned char[canvasLen];
  unsigned char* output = new unsigned char[outputLen];

  auto cleanup = [&]() {
    if (canvas)
      delete[] canvas;
    if (output)
      delete[] output;
  };

  if (!canvas || !output) {
    cleanup();
    return base::TimeDelta();
  }

  if (setAlpha) {
    memset(canvas, 128, canvasLen);
  } else {
    memset(canvas, 200, canvasLen);
  }

  base::ElapsedTimer runTime;
  if (fastPath) {
    blink::RGBAtoRGBNeon(canvas, static_cast<unsigned>(pixels), output);
  } else {
    blink::RGBAtoRGBScalar(canvas, static_cast<unsigned>(pixels), output);
  }

  auto result = runTime.Elapsed();
  cleanup();
  return result;
}

TEST_F(RGBAtoRGBTest, testPerf1k) {
  auto neonElapsed = testNpixels();
  auto scalarElapsed = testNpixels(false);

  EXPECT_TRUE(neonElapsed < scalarElapsed)
      << "Neon: " << neonElapsed << "\tScalar: " << scalarElapsed << std::endl;
}

TEST_F(RGBAtoRGBTest, testPerf4k) {
  auto neonElapsed = testNpixels(true, 4000, 4000);
  auto scalarElapsed = testNpixels(false, 4000, 4000);

  EXPECT_TRUE(neonElapsed < scalarElapsed)
      << "Neon: " << neonElapsed << "\tScalar: " << scalarElapsed << std::endl;
}

// This width will force the tail case, cause width = (16 * 64) + 15.
static bool testRandNpixels(const size_t width = 1039,
                            const size_t height = 1024,
                            bool setAlpha = true) {
  const size_t pixels = width * height;
  const size_t canvasLen = channelsRGBA * pixels;
  const size_t outputLen = channelsRGB * pixels;
  unsigned char* canvas = new unsigned char[canvasLen];
  unsigned char* expected = new unsigned char[outputLen];
  unsigned char* output = new unsigned char[outputLen];

  auto cleanup = [&]() {
    if (canvas)
      delete[] canvas;
    if (expected)
      delete[] expected;
    if (output)
      delete[] output;
  };

  if (!canvas || !output || !expected) {
    cleanup();
    return false;
  }

  if (setAlpha) {
    memset(canvas, 128, canvasLen);
  } else {
    memset(canvas, 200, canvasLen);
  }

  srand(time(0));
  unsigned char* ptr = canvas;
  for (size_t i = 0; i < pixels; ++i) {
    *ptr++ = static_cast<unsigned char>(rand() % 255);
    *ptr++ = static_cast<unsigned char>(rand() % 255);
    *ptr++ = static_cast<unsigned char>(rand() % 255);
    *ptr++ = static_cast<unsigned char>(rand() % 255);
  }

  blink::RGBAtoRGBScalar(canvas, static_cast<unsigned>(pixels), expected);
  blink::RGBAtoRGBNeon(canvas, static_cast<unsigned>(pixels), output);

  bool result = memcmp(expected, output, outputLen) == 0;

  cleanup();
  return result;
}

TEST_F(RGBAtoRGBTest, randomPixels) {
  EXPECT_TRUE(testRandNpixels());
}

#endif
}  // namespace blink