16806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
26806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com/*
36806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com * Copyright 2012 The Android Open Source Project
46806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com *
56806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
66806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com * found in the LICENSE file.
76806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com */
86806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
9f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina#include "Benchmark.h"
106806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com#include "SkCanvas.h"
116806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
126806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
136806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com/**
146806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com * This bench mark tests the use case where the user writes the a canvas
156806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com * and then reads small chunks from it repeatedly. This can cause trouble
166806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com * for the GPU as readbacks are very expensive.
176806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com */
18f168b86d7fafc5c20c87bebc6fd393cb17e120catfarinaclass ReadPixBench : public Benchmark {
196806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.compublic:
20410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.com    ReadPixBench() {}
216806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
226806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.comprotected:
2336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    const char* onGetName() override {
246806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        return "readpix";
256806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com    }
266806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
2736352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onDraw(const int loops, SkCanvas* canvas) override {
286806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        canvas->clear(SK_ColorBLACK);
296806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
306806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        SkISize size = canvas->getDeviceSize();
316806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
326806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        int offX = (size.width() - kWindowSize) / kNumStepsX;
336806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        int offY = (size.height() - kWindowSize) / kNumStepsY;
346806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
356806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        SkPaint paint;
366806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
376806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        paint.setColor(SK_ColorBLUE);
386806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
3973bb3bee937ce55b5bcdb9e480e14fb4df809e5dskia.committer@gmail.com        canvas->drawCircle(SkIntToScalar(size.width()/2),
4073bb3bee937ce55b5bcdb9e480e14fb4df809e5dskia.committer@gmail.com                           SkIntToScalar(size.height()/2),
416806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com                           SkIntToScalar(size.width()/2),
426806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com                           paint);
436806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
446806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        SkBitmap bitmap;
456806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
466c22573edb234ad14df947278cfed010669a39a7reed        bitmap.setInfo(SkImageInfo::MakeN32Premul(kWindowSize, kWindowSize));
476806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
483361471a3504ecd0351ff70f4c42d8d6fee963d4commit-bot@chromium.org        for (int i = 0; i < loops; i++) {
49c289743864e2ab926a95e617a5cd1d29b26d1825mtklein@google.com            for (int x = 0; x < kNumStepsX; ++x) {
50c289743864e2ab926a95e617a5cd1d29b26d1825mtklein@google.com                for (int y = 0; y < kNumStepsY; ++y) {
51c289743864e2ab926a95e617a5cd1d29b26d1825mtklein@google.com                    canvas->readPixels(&bitmap, x * offX, y * offY);
52c289743864e2ab926a95e617a5cd1d29b26d1825mtklein@google.com                }
536806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com            }
546806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com        }
556806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com    }
566806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
576806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.comprivate:
586806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com    static const int kNumStepsX = 30;
596806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com    static const int kNumStepsY = 30;
606806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com    static const int kWindowSize = 5;
616806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
62f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina    typedef Benchmark INHERITED;
636806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com};
646806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
656806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com////////////////////////////////////////////////////////////////////////////////
666806bdaca8b22927ba7ef537fd5336311c7258e1robertphillips@google.com
67410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.comDEF_BENCH( return new ReadPixBench(); )
68