blob: 55078d96b65a9202db4c7ace010df1c9affb0c0f [file] [log] [blame]
robertphillips@google.com6806bda2012-09-04 13:39:01 +00001/*
2 * Copyright 2012 The Android Open Source Project
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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
Florin Malitaab244f02017-05-03 19:16:58 +00009#include "SkBitmap.h"
robertphillips@google.com6806bda2012-09-04 13:39:01 +000010#include "SkCanvas.h"
11
12
13/**
14 * This bench mark tests the use case where the user writes the a canvas
15 * and then reads small chunks from it repeatedly. This can cause trouble
16 * for the GPU as readbacks are very expensive.
17 */
tfarinaf168b862014-06-19 12:32:29 -070018class ReadPixBench : public Benchmark {
robertphillips@google.com6806bda2012-09-04 13:39:01 +000019public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000020 ReadPixBench() {}
robertphillips@google.com6806bda2012-09-04 13:39:01 +000021
22protected:
mtklein36352bf2015-03-25 18:17:31 -070023 const char* onGetName() override {
robertphillips@google.com6806bda2012-09-04 13:39:01 +000024 return "readpix";
25 }
26
mtkleina1ebeb22015-10-01 09:43:39 -070027 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips@google.com6806bda2012-09-04 13:39:01 +000028 canvas->clear(SK_ColorBLACK);
29
Mike Reed3661bc92017-02-22 13:21:42 -050030 SkISize size = canvas->getBaseLayerSize();
robertphillips@google.com6806bda2012-09-04 13:39:01 +000031
32 int offX = (size.width() - kWindowSize) / kNumStepsX;
33 int offY = (size.height() - kWindowSize) / kNumStepsY;
34
35 SkPaint paint;
36
37 paint.setColor(SK_ColorBLUE);
38
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +000039 canvas->drawCircle(SkIntToScalar(size.width()/2),
40 SkIntToScalar(size.height()/2),
robertphillips@google.com6806bda2012-09-04 13:39:01 +000041 SkIntToScalar(size.width()/2),
42 paint);
43
44 SkBitmap bitmap;
45
Mike Reed12e946b2017-04-17 10:53:29 -040046 bitmap.allocPixels(SkImageInfo::MakeN32Premul(kWindowSize, kWindowSize));
robertphillips@google.com6806bda2012-09-04 13:39:01 +000047
commit-bot@chromium.org33614712013-12-03 18:17:16 +000048 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000049 for (int x = 0; x < kNumStepsX; ++x) {
50 for (int y = 0; y < kNumStepsY; ++y) {
Mike Reed12e946b2017-04-17 10:53:29 -040051 canvas->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(),
52 x * offX, y * offY);
mtklein@google.comc2897432013-09-10 19:23:38 +000053 }
robertphillips@google.com6806bda2012-09-04 13:39:01 +000054 }
55 }
56 }
57
58private:
59 static const int kNumStepsX = 30;
60 static const int kNumStepsY = 30;
61 static const int kWindowSize = 5;
62
tfarinaf168b862014-06-19 12:32:29 -070063 typedef Benchmark INHERITED;
robertphillips@google.com6806bda2012-09-04 13:39:01 +000064};
Mike Reed7306bcd2017-10-25 10:37:30 -040065DEF_BENCH( return new ReadPixBench(); )
robertphillips@google.com6806bda2012-09-04 13:39:01 +000066
67////////////////////////////////////////////////////////////////////////////////
Mike Reed7306bcd2017-10-25 10:37:30 -040068#include "SkBitmap.h"
69#include "SkPixmapPriv.h"
robertphillips@google.com6806bda2012-09-04 13:39:01 +000070
Mike Reed7306bcd2017-10-25 10:37:30 -040071class PixmapOrientBench : public Benchmark {
72public:
73 PixmapOrientBench() {}
74
75protected:
76 void onDelayedSetup() override {
77 const SkImageInfo info = SkImageInfo::MakeN32Premul(2048, 1024);
78 fSrc.allocPixels(info);
79 fSrc.eraseColor(SK_ColorBLACK);
80 fDst.allocPixels(info.makeWH(info.height(), info.width()));
81 }
82
83 const char* onGetName() override {
84 return "orient_pixmap";
85 }
86
87 bool isSuitableFor(Backend backend) override {
88 return backend == kNonRendering_Backend;
89 }
90
91 void onDraw(int loops, SkCanvas*) override {
92 const SkPixmapPriv::OrientFlags flags = SkPixmapPriv::kSwapXY;
93
94 SkPixmap src, dst;
95 fSrc.peekPixels(&src);
96 fDst.peekPixels(&dst);
97 for (int i = 0; i < loops; ++i) {
98 SkPixmapPriv::Orient(dst, src, flags);
99 }
100 }
101
102private:
103 SkBitmap fSrc, fDst;
104
105 typedef Benchmark INHERITED;
106};
107DEF_BENCH( return new PixmapOrientBench(); )