summaryrefslogtreecommitdiffstats
path: root/cc/raster/texture_compressor_perftest.cc
blob: 4b2967c0ba646e5129d767c999ec29de137ecd4e (plain)
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
// Copyright 2015 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/logging.h"
#include "cc/debug/lap_timer.h"
#include "cc/raster/texture_compressor.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_test.h"

namespace cc {
namespace {

const int kTimeLimitMillis = 2000;
const int kWarmupRuns = 5;
const int kTimeCheckInterval = 10;

const int kImageWidth = 256;
const int kImageHeight = 256;
const int kImageChannels = 4;
const int kImageSizeInBytes = kImageWidth * kImageHeight * kImageChannels;

std::string FormatName(TextureCompressor::Format format) {
  switch (format) {
    case TextureCompressor::kFormatETC1:
      return "ETC1";
  }

  NOTREACHED();
  return "";
}

std::string QualityName(TextureCompressor::Quality quality) {
  switch (quality) {
    case TextureCompressor::kQualityLow:
      return "Low";
    case TextureCompressor::kQualityMedium:
      return "Medium";
    case TextureCompressor::kQualityHigh:
      return "High";
  }

  NOTREACHED();
  return "";
}

class TextureCompressorPerfTest
    : public testing::TestWithParam<
          ::testing::tuple<TextureCompressor::Quality,
                           TextureCompressor::Format>> {
 public:
  TextureCompressorPerfTest()
      : timer_(kWarmupRuns,
               base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
               kTimeCheckInterval) {}

  void SetUp() override {
    TextureCompressor::Format format = ::testing::get<1>(GetParam());
    compressor_ = TextureCompressor::Create(format);
  }

  void RunTest(const std::string& name) {
    TextureCompressor::Quality quality = ::testing::get<0>(GetParam());
    timer_.Reset();
    do {
      compressor_->Compress(src_, dst_, kImageWidth, kImageHeight, quality);
      timer_.NextLap();
    } while (!timer_.HasTimeLimitExpired());

    TextureCompressor::Format format = ::testing::get<1>(GetParam());
    std::string str = FormatName(format) + " " + QualityName(quality);
    perf_test::PrintResult("Compress256x256", name, str, timer_.MsPerLap(),
                           "us", true);
  }

 protected:
  LapTimer timer_;
  scoped_ptr<TextureCompressor> compressor_;
  uint8_t src_[kImageSizeInBytes];
  uint8_t dst_[kImageSizeInBytes];
};

TEST_P(TextureCompressorPerfTest, Compress256x256BlackAndWhiteGradientImage) {
  for (int i = 0; i < kImageSizeInBytes; ++i)
    src_[i] = i % 256;

  RunTest("BlackAndWhiteGradientImage");
}

TEST_P(TextureCompressorPerfTest, Compress256x256SolidBlackImage) {
  memset(src_, 0, kImageSizeInBytes);

  RunTest("SolidBlackImage");
}

TEST_P(TextureCompressorPerfTest, Compress256x256SolidColorImage) {
  for (int i = 0; i < kImageSizeInBytes; ++i)
    src_[i] = (4 - i % 4) * 50;

  RunTest("SolidColorImage");
}

TEST_P(TextureCompressorPerfTest, Compress256x256RandomColorImage) {
  unsigned int kImageSeed = 1234567890;
  srand(kImageSeed);
  for (int i = 0; i < kImageSizeInBytes; ++i)
    src_[i] = rand() % 256;  // NOLINT

  RunTest("RandomColorImage");
}

INSTANTIATE_TEST_CASE_P(
    TextureCompressorPerfTests,
    TextureCompressorPerfTest,
    ::testing::Combine(::testing::Values(TextureCompressor::kQualityLow,
                                         TextureCompressor::kQualityMedium,
                                         TextureCompressor::kQualityHigh),
                       ::testing::Values(TextureCompressor::kFormatETC1)));

}  // namespace
}  // namespace cc