OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "ui/gfx/codec/png_codec.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
| 10 |
| 11 #include "base/rand_util.h" |
| 12 #include "cc/debug/lap_timer.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/perf/perf_test.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "third_party/skia/include/core/SkColor.h" |
| 17 #include "third_party/skia/include/core/SkImageInfo.h" |
| 18 |
| 19 namespace gfx { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const int kTimeLimitMillis = 5000; |
| 24 const int kWarmupRuns = 3; |
| 25 const int kTimeCheckInterval = 1; |
| 26 |
| 27 SkBitmap CreateSimpleBitmap() { |
| 28 SkBitmap bitmap; |
| 29 EXPECT_TRUE(bitmap.tryAllocPixels(SkImageInfo::MakeN32Premul(2000, 2000))); |
| 30 bitmap.eraseARGB(0x87, 0x65, 0x43, 0x21); |
| 31 return bitmap; |
| 32 } |
| 33 |
| 34 SkBitmap CreateRandomBitmap() { |
| 35 SkBitmap bitmap; |
| 36 EXPECT_TRUE(bitmap.tryAllocPixels(SkImageInfo::MakeN32Premul(2000, 2000))); |
| 37 SkAutoLockPixels lock(bitmap); |
| 38 base::RandBytes(bitmap.getPixels(), bitmap.getSize()); |
| 39 for (int i = 0; i < bitmap.width(); ++i) { |
| 40 for (int j = 0; j < bitmap.width(); ++j) { |
| 41 uint32_t* pixel = bitmap.getAddr32(i, j); |
| 42 *pixel = SkPreMultiplyColor(*pixel); |
| 43 } |
| 44 } |
| 45 return bitmap; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 class PngCodecPerfTest : public ::testing::Test { |
| 51 public: |
| 52 PngCodecPerfTest() |
| 53 : timer_(kWarmupRuns, |
| 54 base::TimeDelta::FromMilliseconds(kTimeLimitMillis), |
| 55 kTimeCheckInterval) {} |
| 56 |
| 57 protected: |
| 58 void PrintResult(const char* trace) { |
| 59 perf_test::PrintResult("png_encode", "", trace, timer_.MsPerLap(), "ms/run", |
| 60 true); |
| 61 } |
| 62 |
| 63 cc::LapTimer timer_; |
| 64 SkBitmap bitmap_; |
| 65 }; |
| 66 |
| 67 TEST_F(PngCodecPerfTest, SimpleFastEncode) { |
| 68 bitmap_ = CreateSimpleBitmap(); |
| 69 timer_.Reset(); |
| 70 do { |
| 71 std::vector<uint8_t> output; |
| 72 ASSERT_TRUE(PNGCodec::FastEncodeBGRASkBitmap(bitmap_, false, &output)); |
| 73 timer_.NextLap(); |
| 74 } while (!timer_.HasTimeLimitExpired()); |
| 75 PrintResult("fast encode"); |
| 76 } |
| 77 |
| 78 TEST_F(PngCodecPerfTest, SimpleNoCompress) { |
| 79 bitmap_ = CreateSimpleBitmap(); |
| 80 timer_.Reset(); |
| 81 do { |
| 82 std::vector<uint8_t> output; |
| 83 ASSERT_TRUE( |
| 84 PNGCodec::NoCompressEncodeBGRASkBitmap(bitmap_, false, &output)); |
| 85 timer_.NextLap(); |
| 86 } while (!timer_.HasTimeLimitExpired()); |
| 87 PrintResult("no compress encode"); |
| 88 } |
| 89 |
| 90 TEST_F(PngCodecPerfTest, RandomFastEncode) { |
| 91 bitmap_ = CreateRandomBitmap(); |
| 92 timer_.Reset(); |
| 93 do { |
| 94 std::vector<uint8_t> output; |
| 95 ASSERT_TRUE(PNGCodec::FastEncodeBGRASkBitmap(bitmap_, false, &output)); |
| 96 timer_.NextLap(); |
| 97 } while (!timer_.HasTimeLimitExpired()); |
| 98 PrintResult("fast encode"); |
| 99 } |
| 100 |
| 101 TEST_F(PngCodecPerfTest, RandomNoCompress) { |
| 102 bitmap_ = CreateRandomBitmap(); |
| 103 timer_.Reset(); |
| 104 do { |
| 105 std::vector<uint8_t> output; |
| 106 ASSERT_TRUE( |
| 107 PNGCodec::NoCompressEncodeBGRASkBitmap(bitmap_, false, &output)); |
| 108 timer_.NextLap(); |
| 109 } while (!timer_.HasTimeLimitExpired()); |
| 110 PrintResult("no compress encode"); |
| 111 } |
| 112 |
| 113 } // namespace gfx |
OLD | NEW |