File: image_decode_bench.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 4,310 bytes parent folder | download | duplicates (6)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

// Provides a minimal wrapping of the Blink image decoders. Used to perform
// a non-threaded, memory-to-memory image decode using micro second accuracy
// clocks to measure image decode time.
//
// TODO(noel): Consider integrating this tool in Chrome telemetry for realz,
// using the image corpora used to assess Blink image decode performance. See
// http://crbug.com/398235#c103 and http://crbug.com/258324#c5

#include <chrono>
#include <fstream>

#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_executor.h"
#include "mojo/core/embedder/embedder.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/image-decoders/image_decoder.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"

namespace blink {

namespace {

scoped_refptr<SharedBuffer> ReadFile(const char* name) {
  std::string file;
  if (!base::ReadFileToString(base::FilePath::FromUTF8Unsafe(name), &file)) {
    perror(name);
    exit(2);
  }
  return SharedBuffer::Create(file);
}

struct ImageMeta {
  const char* name;
  int width;
  int height;
  int frames;
  // Cumulative time in seconds to decode all frames.
  double time;
};

void DecodeFailure(ImageMeta* image) {
  fprintf(stderr, "Failed to decode image %s\n", image->name);
  exit(3);
}

void DecodeImageData(SharedBuffer* data, ImageMeta* image) {
  const bool all_data_received = true;

  std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create(
      data, all_data_received, ImageDecoder::kAlphaPremultiplied,
      ImageDecoder::kDefaultBitDepth, ColorBehavior::kIgnore,
      cc::AuxImage::kDefault, Platform::GetMaxDecodedImageBytes());

  auto start = std::chrono::steady_clock::now();

  decoder->SetData(data, all_data_received);
  size_t frame_count = decoder->FrameCount();
  for (size_t index = 0; index < frame_count; ++index) {
    if (!decoder->DecodeFrameBufferAtIndex(index))
      DecodeFailure(image);
  }

  auto end = std::chrono::steady_clock::now();

  if (!frame_count || decoder->Failed())
    DecodeFailure(image);

  image->time += std::chrono::duration<double>(end - start).count();
  image->width = decoder->Size().width();
  image->height = decoder->Size().height();
  image->frames = frame_count;
}

}  // namespace

void ImageDecodeBenchMain(int argc, char* argv[]) {
  int option, iterations = 1;

  auto usage_exit = [&] {
    fprintf(stderr, "Usage: %s [-i iterations] file [file...]\n", argv[0]);
    exit(1);
  };

  for (option = 1; option < argc; ++option) {
    if (argv[option][0] != '-')
      break;  // End of optional arguments.
    if (std::string(argv[option]) != "-i")
      usage_exit();
    iterations = (++option < argc) ? atoi(argv[option]) : 0;
    if (iterations < 1)
      usage_exit();
  }

  if (option >= argc)
    usage_exit();

  // Setup Blink platform.

  std::unique_ptr<Platform> platform = std::make_unique<Platform>();
  Platform::CreateMainThreadAndInitialize(platform.get());

  // Bench each image file.

  while (option < argc) {
    const char* name = argv[option++];

    // Read entire file content into |data| (a contiguous block of memory) then
    // decode it to verify the image and record its ImageMeta data.

    ImageMeta image = {name, 0, 0, 0, 0};
    scoped_refptr<SharedBuffer> data = ReadFile(name);
    DecodeImageData(data.get(), &image);

    // Image decode bench for iterations.

    double total_time = 0.0;
    for (int i = 0; i < iterations; ++i) {
      image.time = 0.0;
      DecodeImageData(data.get(), &image);
      total_time += image.time;
    }

    // Results to stdout.

    double average_time = total_time / iterations;
    printf("%f %f %s\n", total_time, average_time, name);
  }
}

}  // namespace blink

int main(int argc, char* argv[]) {
  base::SingleThreadTaskExecutor main_task_executor;
  mojo::core::Init();
  base::CommandLine::Init(argc, argv);
  blink::ImageDecodeBenchMain(argc, argv);
  return 0;
}