blob: 2efb19cc9be865ce7d7e5d9575ab2096bdb39eb7 [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"
robertphillips@google.com6806bda2012-09-04 13:39:01 +00009#include "SkCanvas.h"
10
11
12/**
13 * This bench mark tests the use case where the user writes the a canvas
14 * and then reads small chunks from it repeatedly. This can cause trouble
15 * for the GPU as readbacks are very expensive.
16 */
tfarinaf168b862014-06-19 12:32:29 -070017class ReadPixBench : public Benchmark {
robertphillips@google.com6806bda2012-09-04 13:39:01 +000018public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000019 ReadPixBench() {}
robertphillips@google.com6806bda2012-09-04 13:39:01 +000020
21protected:
mtklein36352bf2015-03-25 18:17:31 -070022 const char* onGetName() override {
robertphillips@google.com6806bda2012-09-04 13:39:01 +000023 return "readpix";
24 }
25
mtkleina1ebeb22015-10-01 09:43:39 -070026 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips@google.com6806bda2012-09-04 13:39:01 +000027 canvas->clear(SK_ColorBLACK);
28
Mike Reed3661bc92017-02-22 13:21:42 -050029 SkISize size = canvas->getBaseLayerSize();
robertphillips@google.com6806bda2012-09-04 13:39:01 +000030
31 int offX = (size.width() - kWindowSize) / kNumStepsX;
32 int offY = (size.height() - kWindowSize) / kNumStepsY;
33
34 SkPaint paint;
35
36 paint.setColor(SK_ColorBLUE);
37
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +000038 canvas->drawCircle(SkIntToScalar(size.width()/2),
39 SkIntToScalar(size.height()/2),
robertphillips@google.com6806bda2012-09-04 13:39:01 +000040 SkIntToScalar(size.width()/2),
41 paint);
42
43 SkBitmap bitmap;
44
Mike Reed12e946b2017-04-17 10:53:29 -040045 bitmap.allocPixels(SkImageInfo::MakeN32Premul(kWindowSize, kWindowSize));
robertphillips@google.com6806bda2012-09-04 13:39:01 +000046
commit-bot@chromium.org33614712013-12-03 18:17:16 +000047 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000048 for (int x = 0; x < kNumStepsX; ++x) {
49 for (int y = 0; y < kNumStepsY; ++y) {
Mike Reed12e946b2017-04-17 10:53:29 -040050 canvas->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(),
51 x * offX, y * offY);
mtklein@google.comc2897432013-09-10 19:23:38 +000052 }
robertphillips@google.com6806bda2012-09-04 13:39:01 +000053 }
54 }
55 }
56
57private:
58 static const int kNumStepsX = 30;
59 static const int kNumStepsY = 30;
60 static const int kWindowSize = 5;
61
tfarinaf168b862014-06-19 12:32:29 -070062 typedef Benchmark INHERITED;
robertphillips@google.com6806bda2012-09-04 13:39:01 +000063};
64
65////////////////////////////////////////////////////////////////////////////////
66
mtklein@google.com410e6e82013-09-13 19:52:27 +000067DEF_BENCH( return new ReadPixBench(); )