|
| 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 | +#include "flutter/flow/display_list.h" |
| 7 | +#include "flutter/fml/mapping.h" |
| 8 | +#include "flutter/testing/test_metal_context.h" |
| 9 | +#include "flutter/testing/test_metal_surface.h" |
| 10 | +#include "flutter/testing/testing.h" |
| 11 | + |
| 12 | +#include "third_party/skia/include/core/SkPoint.h" |
| 13 | +#include "third_party/skia/include/core/SkSurface.h" |
| 14 | + |
| 15 | +namespace flutter { |
| 16 | + |
| 17 | +namespace testing { |
| 18 | + |
| 19 | +class CanvasProvider { |
| 20 | + public: |
| 21 | + virtual const std::string BackendName() = 0; |
| 22 | + virtual sk_sp<SkSurface> GetSurface() = 0; |
| 23 | + |
| 24 | + virtual bool Snapshot(std::string filename) { |
| 25 | + auto image = GetSurface()->makeImageSnapshot(); |
| 26 | + if (!image) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + auto raster = image->makeRasterImage(); |
| 30 | + if (!raster) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + auto data = raster->encodeToData(); |
| 34 | + if (!data) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + fml::NonOwnedMapping mapping(static_cast<const uint8_t*>(data->data()), |
| 38 | + data->size()); |
| 39 | + return WriteAtomically(OpenFixturesDirectory(), filename.c_str(), mapping); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +class SoftwareCanvasProvider : public CanvasProvider { |
| 44 | + public: |
| 45 | + SoftwareCanvasProvider(const size_t width, const size_t height) { |
| 46 | + surface_ = SkSurface::MakeRasterN32Premul(width, height); |
| 47 | + surface_->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 48 | + } |
| 49 | + |
| 50 | + sk_sp<SkSurface> GetSurface() override { return surface_; } |
| 51 | + |
| 52 | + const std::string BackendName() override { return "Software"; } |
| 53 | + |
| 54 | + private: |
| 55 | + sk_sp<SkSurface> surface_; |
| 56 | +}; |
| 57 | + |
| 58 | +class MetalCanvasProvider : public CanvasProvider { |
| 59 | + public: |
| 60 | + MetalCanvasProvider(const size_t width, const size_t height) { |
| 61 | + metal_context_ = std::make_unique<TestMetalContext>(); |
| 62 | + metal_surface_ = |
| 63 | + TestMetalSurface::Create(*metal_context_, SkISize::Make(width, height)); |
| 64 | + metal_surface_->GetSurface()->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 65 | + } |
| 66 | + |
| 67 | + sk_sp<SkSurface> GetSurface() override { |
| 68 | + return metal_surface_->GetSurface(); |
| 69 | + } |
| 70 | + |
| 71 | + const std::string BackendName() override { return "Metal"; } |
| 72 | + |
| 73 | + private: |
| 74 | + std::unique_ptr<TestMetalContext> metal_context_; |
| 75 | + std::unique_ptr<TestMetalSurface> metal_surface_; |
| 76 | +}; |
| 77 | + |
| 78 | +template <class T> |
| 79 | +void DrawLine(benchmark::State& state) { |
| 80 | + DisplayListBuilder builder; |
| 81 | + T canvas_provider(state.range(0), state.range(0)); |
| 82 | + |
| 83 | + auto canvas = canvas_provider.GetSurface()->getCanvas(); |
| 84 | + |
| 85 | + // Always draw 10k lines regardless of the line length requested |
| 86 | + size_t length = state.range(0); |
| 87 | + for (size_t i = 0; i < 10000; i++) { |
| 88 | + // The (approximate) line lengths are passed to us via the benchmark state |
| 89 | + // object, so that we can benchmark the performance of drawLine and how it |
| 90 | + // differs based on line length |
| 91 | + builder.drawLine(SkPoint::Make(i % length, 0), |
| 92 | + SkPoint::Make(length - i % length, length)); |
| 93 | + } |
| 94 | + |
| 95 | + auto displaylist = builder.Build(); |
| 96 | + |
| 97 | + // We only want to time the actual rasterization. |
| 98 | + while (state.KeepRunning()) { |
| 99 | + displaylist->RenderTo(canvas); |
| 100 | + canvas->flush(); |
| 101 | + } |
| 102 | + |
| 103 | + auto filename = canvas_provider.BackendName() + "-DrawLine-" + |
| 104 | + std::to_string(state.range(0)) + ".png"; |
| 105 | + canvas_provider.Snapshot(filename); |
| 106 | +} |
| 107 | + |
| 108 | +BENCHMARK_TEMPLATE(DrawLine, SoftwareCanvasProvider) |
| 109 | + ->RangeMultiplier(2) |
| 110 | + ->Range(16, 2048) |
| 111 | + ->Unit(benchmark::kMillisecond); |
| 112 | + |
| 113 | +BENCHMARK_TEMPLATE(DrawLine, MetalCanvasProvider) |
| 114 | + ->RangeMultiplier(2) |
| 115 | + ->Range(16, 2048) |
| 116 | + ->UseRealTime() |
| 117 | + ->Unit(benchmark::kMillisecond); |
| 118 | + |
| 119 | +} // namespace testing |
| 120 | +} // namespace flutter |
0 commit comments