1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
7bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com
8f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina#ifndef Benchmark_DEFINED
9f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina#define Benchmark_DEFINED
10f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina
11bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com#include "SkPoint.h"
12f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina#include "SkRefCnt.h"
13111fd11e11cbc296c97225ea16b7ff174a4bcdb5scroggo@google.com#include "SkString.h"
14f523e25da069e3e1af2cb73d37073a34f3bb9ea2reed@android.com#include "SkTRegistry.h"
15bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com
166adce6783c5d7cbef276d04cc08a2b19789a0156commit-bot@chromium.org#define DEF_BENCH(code)                                                 \
176adce6783c5d7cbef276d04cc08a2b19789a0156commit-bot@chromium.orgnamespace {                                                             \
18f168b86d7fafc5c20c87bebc6fd393cb17e120catfarinastatic Benchmark* SK_MACRO_APPEND_LINE(factory)(void*) { code; }      \
1938aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.orgBenchRegistry SK_MACRO_APPEND_LINE(g_R_)(SK_MACRO_APPEND_LINE(factory)); \
206adce6783c5d7cbef276d04cc08a2b19789a0156commit-bot@chromium.org}
21b8b92ea089a380e17984e4df7b49b379cb6bd929reed@google.com
22b8b92ea089a380e17984e4df7b49b379cb6bd929reed@google.com/*
23b8b92ea089a380e17984e4df7b49b379cb6bd929reed@google.com *  With the above macros, you can register benches as follows (at the bottom
24b8b92ea089a380e17984e4df7b49b379cb6bd929reed@google.com *  of your .cpp)
25b8b92ea089a380e17984e4df7b49b379cb6bd929reed@google.com *
26410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.com *  DEF_BENCH(return new MyBenchmark(...))
27410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.com *  DEF_BENCH(return new MyBenchmark(...))
28410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.com *  DEF_BENCH(return new MyBenchmark(...))
29b8b92ea089a380e17984e4df7b49b379cb6bd929reed@google.com */
30b8b92ea089a380e17984e4df7b49b379cb6bd929reed@google.com
31b8b92ea089a380e17984e4df7b49b379cb6bd929reed@google.com
32bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.comclass SkCanvas;
334bc1983e01d756ae9c91fd380758457f579d26eareed@android.comclass SkPaint;
34bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com
354e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.comclass SkTriState {
364e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.compublic:
374e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com    enum State {
384e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com        kDefault,
394e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com        kTrue,
404e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com        kFalse
414e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com    };
42c289743864e2ab926a95e617a5cd1d29b26d1825mtklein@google.com    static const char* Name[];
434e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com};
444e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com
45f168b86d7fafc5c20c87bebc6fd393cb17e120catfarinaclass Benchmark : public SkRefCnt {
46bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.compublic:
47f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina    SK_DECLARE_INST_COUNT(Benchmark)
4815e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com
49f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina    Benchmark();
504bc1983e01d756ae9c91fd380758457f579d26eareed@android.com
51bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com    const char* getName();
52bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com    SkIPoint getSize();
5330e6d2c2054c15f1cb6c0637bee6756261291751bsalomon@google.com
54644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org    enum Backend {
55644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org        kNonRendering_Backend,
56644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org        kRaster_Backend,
57644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org        kGPU_Backend,
58644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org        kPDF_Backend,
59644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org    };
60644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org
61644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org    // Call to determine whether the benchmark is intended for
62644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org    // the rendering mode.
63644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org    virtual bool isSuitableFor(Backend backend) {
64644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org        return backend != kNonRendering_Backend;
65644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org    }
66644629c1c7913a43ced172b98d56e0f471bc348bcommit-bot@chromium.org
6730e6d2c2054c15f1cb6c0637bee6756261291751bsalomon@google.com    // Call before draw, allows the benchmark to do setup work outside of the
6830e6d2c2054c15f1cb6c0637bee6756261291751bsalomon@google.com    // timer. When a benchmark is repeatedly drawn, this should be called once
6930e6d2c2054c15f1cb6c0637bee6756261291751bsalomon@google.com    // before the initial draw.
7030e6d2c2054c15f1cb6c0637bee6756261291751bsalomon@google.com    void preDraw();
7130e6d2c2054c15f1cb6c0637bee6756261291751bsalomon@google.com
723361471a3504ecd0351ff70f4c42d8d6fee963d4commit-bot@chromium.org    // Bench framework can tune loops to be large enough for stable timing.
733361471a3504ecd0351ff70f4c42d8d6fee963d4commit-bot@chromium.org    void draw(const int loops, SkCanvas*);
7430e6d2c2054c15f1cb6c0637bee6756261291751bsalomon@google.com
754bc1983e01d756ae9c91fd380758457f579d26eareed@android.com    void setForceAlpha(int alpha) {
764bc1983e01d756ae9c91fd380758457f579d26eareed@android.com        fForceAlpha = alpha;
774bc1983e01d756ae9c91fd380758457f579d26eareed@android.com    }
78fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
794bc1983e01d756ae9c91fd380758457f579d26eareed@android.com    void setForceAA(bool aa) {
804bc1983e01d756ae9c91fd380758457f579d26eareed@android.com        fForceAA = aa;
814bc1983e01d756ae9c91fd380758457f579d26eareed@android.com    }
82fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
8329348cb0612e19030d979156860946241e2ff4bdreed@android.com    void setForceFilter(bool filter) {
8429348cb0612e19030d979156860946241e2ff4bdreed@android.com        fForceFilter = filter;
8529348cb0612e19030d979156860946241e2ff4bdreed@android.com    }
86fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
874e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com    void setDither(SkTriState::State state) {
884e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com        fDither = state;
894e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com    }
90e9d0060f4d7b5a07a220182d83aae3a140784c4breed@android.com
91ef77ec210416733e38072175cc948dcbdb831685reed@google.com    /** Assign masks for paint-flags. These will be applied when setupPaint()
92ef77ec210416733e38072175cc948dcbdb831685reed@google.com     *  is called.
93ef77ec210416733e38072175cc948dcbdb831685reed@google.com     *
94ef77ec210416733e38072175cc948dcbdb831685reed@google.com     *  Performs the following on the paint:
95ef77ec210416733e38072175cc948dcbdb831685reed@google.com     *      uint32_t flags = paint.getFlags();
96ef77ec210416733e38072175cc948dcbdb831685reed@google.com     *      flags &= ~clearMask;
97ef77ec210416733e38072175cc948dcbdb831685reed@google.com     *      flags |= orMask;
98ef77ec210416733e38072175cc948dcbdb831685reed@google.com     *      paint.setFlags(flags);
99ef77ec210416733e38072175cc948dcbdb831685reed@google.com     */
100ef77ec210416733e38072175cc948dcbdb831685reed@google.com    void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
101ef77ec210416733e38072175cc948dcbdb831685reed@google.com        fOrMask = orMask;
102ef77ec210416733e38072175cc948dcbdb831685reed@google.com        fClearMask = clearMask;
103ef77ec210416733e38072175cc948dcbdb831685reed@google.com    }
104ef77ec210416733e38072175cc948dcbdb831685reed@google.com
105bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.comprotected:
1060561a3ccd24e46259d825adf5507c7ee9531b9c5reed@google.com    virtual void setupPaint(SkPaint* paint);
1074bc1983e01d756ae9c91fd380758457f579d26eareed@android.com
108bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com    virtual const char* onGetName() = 0;
10930e6d2c2054c15f1cb6c0637bee6756261291751bsalomon@google.com    virtual void onPreDraw() {}
1103361471a3504ecd0351ff70f4c42d8d6fee963d4commit-bot@chromium.org    // Each bench should do its main work in a loop like this:
1113361471a3504ecd0351ff70f4c42d8d6fee963d4commit-bot@chromium.org    //   for (int i = 0; i < loops; i++) { <work here> }
1123361471a3504ecd0351ff70f4c42d8d6fee963d4commit-bot@chromium.org    virtual void onDraw(const int loops, SkCanvas*) = 0;
113f523e25da069e3e1af2cb73d37073a34f3bb9ea2reed@android.com
114f523e25da069e3e1af2cb73d37073a34f3bb9ea2reed@android.com    virtual SkIPoint onGetSize();
115f523e25da069e3e1af2cb73d37073a34f3bb9ea2reed@android.com
1164bc1983e01d756ae9c91fd380758457f579d26eareed@android.comprivate:
1174bc1983e01d756ae9c91fd380758457f579d26eareed@android.com    int     fForceAlpha;
1184bc1983e01d756ae9c91fd380758457f579d26eareed@android.com    bool    fForceAA;
11929348cb0612e19030d979156860946241e2ff4bdreed@android.com    bool    fForceFilter;
1204e635f9eb218b15f656a02e71c60d4f787fe9306reed@android.com    SkTriState::State  fDither;
121ef77ec210416733e38072175cc948dcbdb831685reed@google.com    uint32_t    fOrMask, fClearMask;
12215e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com
12315e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com    typedef SkRefCnt INHERITED;
124bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com};
125bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com
126f168b86d7fafc5c20c87bebc6fd393cb17e120catfarinatypedef SkTRegistry<Benchmark*(*)(void*)> BenchRegistry;
127f523e25da069e3e1af2cb73d37073a34f3bb9ea2reed@android.com
128bd700c391dd45acd8ea1a40321789c9d92a14bb8reed@android.com#endif
129