Florin Malita | 922e225 | 2019-08-26 11:01:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkString.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "src/core/SkAutoPixmapStorage.h" |
| 14 | |
| 15 | class CTConvertBench : public Benchmark { |
| 16 | public: |
| 17 | enum class Mode { kDrawBitmap, kReadPixels }; |
| 18 | |
| 19 | CTConvertBench(SkColorType from, SkColorType to, Mode m) |
| 20 | : fName(SkStringPrintf("ctconvert_%d_%d_%s", from, to, |
| 21 | m == Mode::kReadPixels ? "readpixels" : "drawbitmap")) |
| 22 | , fFrom(from) |
| 23 | , fTo(to) |
| 24 | , fMode(m) {} |
| 25 | |
| 26 | bool isSuitableFor(Backend backend) override { |
| 27 | return backend == kNonRendering_Backend; |
| 28 | } |
| 29 | |
| 30 | protected: |
| 31 | const char* onGetName() override { |
| 32 | return fName.c_str(); |
| 33 | } |
| 34 | |
| 35 | void onPreDraw(SkCanvas*) override { |
| 36 | if (fSrc.addr()) return; |
| 37 | |
| 38 | fSrc.alloc(SkImageInfo::Make(kBMSize, kBMSize, fFrom, kPremul_SkAlphaType)); |
| 39 | fSrc.erase(0x80808080); |
| 40 | |
| 41 | fDst.alloc(SkImageInfo::Make(kBMSize, kBMSize, fTo , kPremul_SkAlphaType)); |
| 42 | |
| 43 | if (fMode == Mode::kDrawBitmap) { |
| 44 | fSrcBM.installPixels(fSrc); |
| 45 | fDstSurf = SkSurface::MakeRasterDirect(fDst.info(), fDst.writable_addr(), |
| 46 | fDst.info().minRowBytes()); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void onDraw(int loops, SkCanvas*) override { |
| 51 | switch (fMode) { |
| 52 | case Mode::kDrawBitmap: { |
| 53 | SkPaint p; |
| 54 | p.setBlendMode(SkBlendMode::kSrc); |
| 55 | for (int i = 0; i < loops; ++i) { |
| 56 | fDstSurf->getCanvas()->drawBitmap(fSrcBM, 0, 0, &p); |
| 57 | } |
| 58 | } break; |
| 59 | case Mode::kReadPixels: { |
| 60 | for (int i = 0; i < loops; ++i) { |
| 61 | fSrc.readPixels(fDst); |
| 62 | } |
| 63 | } break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | static constexpr size_t kBMSize = 512; |
| 69 | |
| 70 | const SkString fName; |
| 71 | const SkColorType fFrom, fTo; |
| 72 | const Mode fMode; |
| 73 | |
| 74 | SkAutoPixmapStorage fSrc, fDst; |
| 75 | SkBitmap fSrcBM; |
| 76 | sk_sp<SkSurface> fDstSurf; |
| 77 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 78 | using INHERITED = Benchmark; |
Florin Malita | 922e225 | 2019-08-26 11:01:14 -0400 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | DEF_BENCH( return new CTConvertBench(kRGBA_8888_SkColorType, kBGRA_8888_SkColorType, |
| 82 | CTConvertBench::Mode::kDrawBitmap); ) |
| 83 | |
| 84 | DEF_BENCH( return new CTConvertBench(kRGBA_8888_SkColorType, kBGRA_8888_SkColorType, |
| 85 | CTConvertBench::Mode::kReadPixels); ) |
| 86 | |
| 87 | DEF_BENCH( return new CTConvertBench(kRGBA_8888_SkColorType, kRGBA_8888_SkColorType, |
| 88 | CTConvertBench::Mode::kDrawBitmap); ) |
| 89 | |
| 90 | DEF_BENCH( return new CTConvertBench(kRGBA_8888_SkColorType, kRGBA_8888_SkColorType, |
| 91 | CTConvertBench::Mode::kReadPixels); ) |