blob: f6ec7339cb40af637e5a42f1bd8e88570bcc2205 [file] [log] [blame]
bungeman@google.com55487522012-05-14 14:09:24 +00001/*
2 * Copyright 2012 Google Inc.
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#include "SkBenchmark.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +00008#include "SkRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +00009#include "SkThread.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +000010#include "SkWeakRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +000011#include <memory>
12
13enum {
mtklein@google.comc2897432013-09-10 19:23:38 +000014 M = 2
bungeman@google.com55487522012-05-14 14:09:24 +000015};
16
17class RefCntBench_Stack : public SkBenchmark {
18public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000019 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
20 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000021 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000022
bungeman@google.com55487522012-05-14 14:09:24 +000023protected:
24 virtual const char* onGetName() {
25 return "ref_cnt_stack";
26 }
27
sugoi@google.com77472f02013-03-05 18:50:01 +000028 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +000029 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000030 SkRefCnt ref;
31 for (int j = 0; j < M; ++j) {
32 ref.ref();
33 ref.unref();
34 }
35 }
36 }
37
38private:
39 typedef SkBenchmark INHERITED;
40};
41
42class PlacedRefCnt : public SkRefCnt {
43public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000044 SK_DECLARE_INST_COUNT(PlacedRefCnt)
45
bungeman@google.com55487522012-05-14 14:09:24 +000046 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000047 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000048
49private:
50 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000051};
52
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000053SK_DEFINE_INST_COUNT(PlacedRefCnt)
54
bungeman@google.com55487522012-05-14 14:09:24 +000055class RefCntBench_Heap : public SkBenchmark {
56public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000057 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
58 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000059 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000060
bungeman@google.com55487522012-05-14 14:09:24 +000061protected:
62 virtual const char* onGetName() {
63 return "ref_cnt_heap";
64 }
65
sugoi@google.com77472f02013-03-05 18:50:01 +000066 virtual void onDraw(SkCanvas*) {
bungeman@google.com55487522012-05-14 14:09:24 +000067 char memory[sizeof(PlacedRefCnt)];
mtklein@google.comc2897432013-09-10 19:23:38 +000068 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000069 PlacedRefCnt* ref = new (memory) PlacedRefCnt();
70 for (int j = 0; j < M; ++j) {
71 ref->ref();
72 ref->unref();
73 }
74 ref->unref();
75 }
76 }
77
78private:
79 typedef SkBenchmark INHERITED;
80};
81
bungeman@google.coma02bc152012-05-16 18:21:56 +000082class RefCntBench_New : public SkBenchmark {
83public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000084 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
85 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +000086 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000087
bungeman@google.coma02bc152012-05-16 18:21:56 +000088protected:
89 virtual const char* onGetName() {
90 return "ref_cnt_new";
91 }
92
sugoi@google.com77472f02013-03-05 18:50:01 +000093 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +000094 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +000095 SkRefCnt* ref = new SkRefCnt();
96 for (int j = 0; j < M; ++j) {
97 ref->ref();
98 ref->unref();
99 }
100 ref->unref();
101 }
102 }
103
104private:
105 typedef SkBenchmark INHERITED;
106};
107
bungeman@google.com55487522012-05-14 14:09:24 +0000108///////////////////////////////////////////////////////////////////////////////
109
bungeman@google.coma02bc152012-05-16 18:21:56 +0000110class WeakRefCntBench_Stack : public SkBenchmark {
111public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000112 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
113 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000114 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000115
bungeman@google.coma02bc152012-05-16 18:21:56 +0000116protected:
117 virtual const char* onGetName() {
118 return "ref_cnt_stack_weak";
119 }
bungeman@google.com55487522012-05-14 14:09:24 +0000120
sugoi@google.com77472f02013-03-05 18:50:01 +0000121 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000122 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000123 SkWeakRefCnt ref;
124 for (int j = 0; j < M; ++j) {
125 ref.ref();
126 ref.unref();
127 }
128 }
129 }
bungeman@google.com55487522012-05-14 14:09:24 +0000130
bungeman@google.coma02bc152012-05-16 18:21:56 +0000131private:
132 typedef SkBenchmark INHERITED;
133};
134
135class PlacedWeakRefCnt : public SkWeakRefCnt {
136public:
137 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000138 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000139};
140
141class WeakRefCntBench_Heap : public SkBenchmark {
142public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000143 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
144 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000145 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000146
bungeman@google.coma02bc152012-05-16 18:21:56 +0000147protected:
148 virtual const char* onGetName() {
149 return "ref_cnt_heap_weak";
150 }
151
sugoi@google.com77472f02013-03-05 18:50:01 +0000152 virtual void onDraw(SkCanvas*) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000153 char memory[sizeof(PlacedWeakRefCnt)];
mtklein@google.comc2897432013-09-10 19:23:38 +0000154 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000155 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
156 for (int j = 0; j < M; ++j) {
157 ref->ref();
158 ref->unref();
159 }
160 ref->unref();
161 }
162 }
163
164private:
165 typedef SkBenchmark INHERITED;
166};
167
168class WeakRefCntBench_New : public SkBenchmark {
169public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000170 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
171 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000172 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000173
bungeman@google.coma02bc152012-05-16 18:21:56 +0000174protected:
175 virtual const char* onGetName() {
176 return "ref_cnt_new_weak";
177 }
178
sugoi@google.com77472f02013-03-05 18:50:01 +0000179 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000180 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000181 SkWeakRefCnt* ref = new SkWeakRefCnt();
182 for (int j = 0; j < M; ++j) {
183 ref->ref();
184 ref->unref();
185 }
186 ref->unref();
187 }
188 }
189
190private:
191 typedef SkBenchmark INHERITED;
192};
193
194///////////////////////////////////////////////////////////////////////////////
195
mtklein@google.com410e6e82013-09-13 19:52:27 +0000196DEF_BENCH( return new RefCntBench_Stack(); )
197DEF_BENCH( return new RefCntBench_Heap(); )
198DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000199
mtklein@google.com410e6e82013-09-13 19:52:27 +0000200DEF_BENCH( return new WeakRefCntBench_Stack(); )
201DEF_BENCH( return new WeakRefCntBench_Heap(); )
202DEF_BENCH( return new WeakRefCntBench_New(); )