robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 10 | #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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | class ReadPixBench : public Benchmark { |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 19 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 20 | ReadPixBench() {} |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 21 | |
| 22 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | const char* onGetName() override { |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 24 | return "readpix"; |
| 25 | } |
| 26 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 27 | void onDraw(int loops, SkCanvas* canvas) override { |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 28 | canvas->clear(SK_ColorBLACK); |
| 29 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 30 | SkISize size = canvas->getBaseLayerSize(); |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 31 | |
| 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.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 39 | canvas->drawCircle(SkIntToScalar(size.width()/2), |
| 40 | SkIntToScalar(size.height()/2), |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 41 | SkIntToScalar(size.width()/2), |
| 42 | paint); |
| 43 | |
| 44 | SkBitmap bitmap; |
| 45 | |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 46 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(kWindowSize, kWindowSize)); |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 48 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 49 | for (int x = 0; x < kNumStepsX; ++x) { |
| 50 | for (int y = 0; y < kNumStepsY; ++y) { |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 51 | canvas->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), |
| 52 | x * offX, y * offY); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 53 | } |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | static const int kNumStepsX = 30; |
| 60 | static const int kNumStepsY = 30; |
| 61 | static const int kWindowSize = 5; |
| 62 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 63 | typedef Benchmark INHERITED; |
robertphillips@google.com | 6806bda | 2012-09-04 13:39:01 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | //////////////////////////////////////////////////////////////////////////////// |
| 67 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 68 | DEF_BENCH( return new ReadPixBench(); ) |