Flutter Impeller
canvas_benchmarks.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "flutter/benchmarking/benchmarking.h"
6 
7 #include "impeller/aiks/canvas.h"
8 
9 namespace impeller {
10 
11 namespace {
12 
13 using CanvasCallback = size_t (*)(Canvas&);
14 
15 size_t DrawRect(Canvas& canvas) {
16  for (auto i = 0; i < 500; i++) {
17  canvas.DrawRect(Rect::MakeLTRB(0, 0, 100, 100),
18  {.color = Color::DarkKhaki()});
19  }
20  return 500;
21 }
22 
23 size_t DrawCircle(Canvas& canvas) {
24  for (auto i = 0; i < 500; i++) {
25  canvas.DrawCircle({100, 100}, 5, {.color = Color::DarkKhaki()});
26  }
27  return 500;
28 }
29 
30 size_t DrawLine(Canvas& canvas) {
31  for (auto i = 0; i < 500; i++) {
32  canvas.DrawLine({0, 0}, {100, 100}, {.color = Color::DarkKhaki()});
33  }
34  return 500;
35 }
36 } // namespace
37 
38 // A set of benchmarks that measures the CPU cost of encoding canvas operations.
39 // These benchmarks do not measure the cost of conversion through the HAL, no
40 // do they measure the GPU side cost of executing the required shader programs.
41 template <class... Args>
42 static void BM_CanvasRecord(benchmark::State& state, Args&&... args) {
43  auto args_tuple = std::make_tuple(std::move(args)...);
44  auto test_proc = std::get<CanvasCallback>(args_tuple);
45 
46  size_t op_count = 0u;
47  size_t canvas_count = 0u;
48  while (state.KeepRunning()) {
49  // A new canvas is allocated for each iteration to avoid the benchmark
50  // becoming a measurement of only the entity vector re-allocation time.
51  Canvas canvas;
52  op_count += test_proc(canvas);
53  canvas_count++;
54  }
55  state.counters["TotalOpCount"] = op_count;
56  state.counters["TotalCanvasCount"] = canvas_count;
57 }
58 
59 BENCHMARK_CAPTURE(BM_CanvasRecord, draw_rect, &DrawRect);
60 BENCHMARK_CAPTURE(BM_CanvasRecord, draw_circle, &DrawCircle);
61 BENCHMARK_CAPTURE(BM_CanvasRecord, draw_line, &DrawLine);
62 
63 } // namespace impeller
impeller::Canvas
Definition: canvas.h:58
impeller::BENCHMARK_CAPTURE
BENCHMARK_CAPTURE(BM_CanvasRecord, draw_rect, &DrawRect)
impeller::Color::DarkKhaki
static constexpr Color DarkKhaki()
Definition: color.h:374
impeller::BM_CanvasRecord
static void BM_CanvasRecord(benchmark::State &state, Args &&... args)
Definition: canvas_benchmarks.cc:42
canvas.h
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
impeller
Definition: aiks_blur_unittests.cc:20