blob: b7a111bcb05225cb2c5dba9f7453fd465c73319d [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
commit-bot@chromium.org33614712013-12-03 18:17:16 +000028 virtual void onDraw(const int loops, SkCanvas*) {
29 for (int i = 0; i < loops; ++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
53class RefCntBench_Heap : public SkBenchmark {
54public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000055 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
56 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000057 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000058
bungeman@google.com55487522012-05-14 14:09:24 +000059protected:
60 virtual const char* onGetName() {
61 return "ref_cnt_heap";
62 }
63
commit-bot@chromium.org33614712013-12-03 18:17:16 +000064 virtual void onDraw(const int loops, SkCanvas*) {
bungeman@google.com55487522012-05-14 14:09:24 +000065 char memory[sizeof(PlacedRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +000066 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000067 PlacedRefCnt* ref = new (memory) PlacedRefCnt();
68 for (int j = 0; j < M; ++j) {
69 ref->ref();
70 ref->unref();
71 }
72 ref->unref();
73 }
74 }
75
76private:
77 typedef SkBenchmark INHERITED;
78};
79
bungeman@google.coma02bc152012-05-16 18:21:56 +000080class RefCntBench_New : public SkBenchmark {
81public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000082 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
83 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +000084 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000085
bungeman@google.coma02bc152012-05-16 18:21:56 +000086protected:
87 virtual const char* onGetName() {
88 return "ref_cnt_new";
89 }
90
commit-bot@chromium.org33614712013-12-03 18:17:16 +000091 virtual void onDraw(const int loops, SkCanvas*) {
92 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +000093 SkRefCnt* ref = new SkRefCnt();
94 for (int j = 0; j < M; ++j) {
95 ref->ref();
96 ref->unref();
97 }
98 ref->unref();
99 }
100 }
101
102private:
103 typedef SkBenchmark INHERITED;
104};
105
bungeman@google.com55487522012-05-14 14:09:24 +0000106///////////////////////////////////////////////////////////////////////////////
107
bungeman@google.coma02bc152012-05-16 18:21:56 +0000108class WeakRefCntBench_Stack : public SkBenchmark {
109public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000110 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
111 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000112 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000113
bungeman@google.coma02bc152012-05-16 18:21:56 +0000114protected:
115 virtual const char* onGetName() {
116 return "ref_cnt_stack_weak";
117 }
bungeman@google.com55487522012-05-14 14:09:24 +0000118
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000119 virtual void onDraw(const int loops, SkCanvas*) {
120 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000121 SkWeakRefCnt ref;
122 for (int j = 0; j < M; ++j) {
123 ref.ref();
124 ref.unref();
125 }
126 }
127 }
bungeman@google.com55487522012-05-14 14:09:24 +0000128
bungeman@google.coma02bc152012-05-16 18:21:56 +0000129private:
130 typedef SkBenchmark INHERITED;
131};
132
133class PlacedWeakRefCnt : public SkWeakRefCnt {
134public:
135 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000136 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000137};
138
139class WeakRefCntBench_Heap : public SkBenchmark {
140public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000141 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
142 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000143 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000144
bungeman@google.coma02bc152012-05-16 18:21:56 +0000145protected:
146 virtual const char* onGetName() {
147 return "ref_cnt_heap_weak";
148 }
149
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000150 virtual void onDraw(const int loops, SkCanvas*) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000151 char memory[sizeof(PlacedWeakRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000152 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000153 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
154 for (int j = 0; j < M; ++j) {
155 ref->ref();
156 ref->unref();
157 }
158 ref->unref();
159 }
160 }
161
162private:
163 typedef SkBenchmark INHERITED;
164};
165
166class WeakRefCntBench_New : public SkBenchmark {
167public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000168 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
169 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000170 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000171
bungeman@google.coma02bc152012-05-16 18:21:56 +0000172protected:
173 virtual const char* onGetName() {
174 return "ref_cnt_new_weak";
175 }
176
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000177 virtual void onDraw(const int loops, SkCanvas*) {
178 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000179 SkWeakRefCnt* ref = new SkWeakRefCnt();
180 for (int j = 0; j < M; ++j) {
181 ref->ref();
182 ref->unref();
183 }
184 ref->unref();
185 }
186 }
187
188private:
189 typedef SkBenchmark INHERITED;
190};
191
192///////////////////////////////////////////////////////////////////////////////
193
mtklein@google.com410e6e82013-09-13 19:52:27 +0000194DEF_BENCH( return new RefCntBench_Stack(); )
195DEF_BENCH( return new RefCntBench_Heap(); )
196DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000197
mtklein@google.com410e6e82013-09-13 19:52:27 +0000198DEF_BENCH( return new WeakRefCntBench_Stack(); )
199DEF_BENCH( return new WeakRefCntBench_Heap(); )
200DEF_BENCH( return new WeakRefCntBench_New(); )