blob: bef80b3fcaaab9456b01c0552cd50221d5520f52 [file] [log] [blame]
mtklein281b33f2016-07-12 15:01:26 -07001/*
2 * Copyright 2016 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
8#include "Benchmark.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -04009#include "SkOpts.h"
mtklein281b33f2016-07-12 15:01:26 -070010#include "SkRasterPipeline.h"
mtklein281b33f2016-07-12 15:01:26 -070011
Mike Kleincb793932017-02-27 09:39:38 -050012static const int N = 15;
mtklein281b33f2016-07-12 15:01:26 -070013
Mike Kleinafb48b62016-10-03 15:14:04 -040014static uint64_t dst[N]; // sRGB or F16
15static uint32_t src[N]; // sRGB
16static uint8_t mask[N]; // 8-bit linear
mtklein281b33f2016-07-12 15:01:26 -070017
18// We'll build up a somewhat realistic useful pipeline:
19// - load srgb src
20// - scale src by 8-bit mask
Mike Kleinafb48b62016-10-03 15:14:04 -040021// - load srgb/f16 dst
mtklein281b33f2016-07-12 15:01:26 -070022// - src = srcover(dst, src)
Mike Kleinafb48b62016-10-03 15:14:04 -040023// - store src back as srgb/f16
mtklein281b33f2016-07-12 15:01:26 -070024
Mike Klein8729e5b2017-02-16 06:51:48 -050025template <bool kF16>
mtklein281b33f2016-07-12 15:01:26 -070026class SkRasterPipelineBench : public Benchmark {
27public:
mtklein281b33f2016-07-12 15:01:26 -070028 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
Mike Kleinafb48b62016-10-03 15:14:04 -040029 const char* onGetName() override {
Mike Klein8729e5b2017-02-16 06:51:48 -050030 switch ((int)kF16) {
31 case 0: return "SkRasterPipeline_srgb";
32 case 1: return "SkRasterPipeline_f16";
Mike Kleina2d25ec2017-01-05 15:03:53 -050033 }
34 return "whoops";
Mike Kleinafb48b62016-10-03 15:14:04 -040035 }
mtklein281b33f2016-07-12 15:01:26 -070036
37 void onDraw(int loops, SkCanvas*) override {
Mike Kleinbd3fe472016-10-25 15:43:46 -040038 void* mask_ctx = mask;
39 void* src_ctx = src;
40 void* dst_ctx = dst;
41
Mike Kleine9f74b82016-10-25 13:31:21 -040042 SkRasterPipeline p;
Mike Klein729b5822016-11-28 18:23:23 -050043 p.append(SkRasterPipeline::load_8888, &src_ctx);
Mike Kleind37d5d92016-12-14 13:38:24 +000044 p.append_from_srgb(kUnpremul_SkAlphaType);
Mike Kleinbd3fe472016-10-25 15:43:46 -040045 p.append(SkRasterPipeline::scale_u8, &mask_ctx);
Mike Klein8c8cb5b2017-01-06 10:21:56 -050046 p.append(SkRasterPipeline::move_src_dst);
Mike Kleine03339a2016-11-28 13:24:27 -050047 if (kF16) {
Mike Klein8c8cb5b2017-01-06 10:21:56 -050048 p.append(SkRasterPipeline::load_f16, &dst_ctx);
Mike Kleine03339a2016-11-28 13:24:27 -050049 } else {
Mike Klein8c8cb5b2017-01-06 10:21:56 -050050 p.append(SkRasterPipeline::load_8888, &dst_ctx);
51 p.append_from_srgb(kPremul_SkAlphaType);
Mike Kleine03339a2016-11-28 13:24:27 -050052 }
Mike Klein8c8cb5b2017-01-06 10:21:56 -050053 p.append(SkRasterPipeline::dstover);
Mike Kleine03339a2016-11-28 13:24:27 -050054 if (kF16) {
55 p.append(SkRasterPipeline::store_f16, &dst_ctx);
56 } else {
57 p.append(SkRasterPipeline::to_srgb);
58 p.append(SkRasterPipeline::store_8888, &dst_ctx);
59 }
Mike Kleine9f74b82016-10-25 13:31:21 -040060
Mike Klein8729e5b2017-02-16 06:51:48 -050061 while (loops --> 0) {
62 p.run(0,N);
mtklein281b33f2016-07-12 15:01:26 -070063 }
64 }
65};
Mike Klein8729e5b2017-02-16 06:51:48 -050066DEF_BENCH( return (new SkRasterPipelineBench< true>); )
67DEF_BENCH( return (new SkRasterPipelineBench<false>); )
Mike Kleinf7688562017-01-17 10:24:15 -050068
Mike Klein0a76b412017-05-22 12:01:59 -040069class SkRasterPipelineCompileVsRunBench : public Benchmark {
Mike Kleinf7688562017-01-17 10:24:15 -050070public:
Mike Klein0a76b412017-05-22 12:01:59 -040071 explicit SkRasterPipelineCompileVsRunBench(bool compile) : fCompile(compile) {}
Mike Kleinf7688562017-01-17 10:24:15 -050072 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
73 const char* onGetName() override {
Mike Klein0a76b412017-05-22 12:01:59 -040074 return fCompile ? "SkRasterPipeline_compile"
75 : "SkRasterPipeline_run";
Mike Kleinf7688562017-01-17 10:24:15 -050076 }
77
78 void onDraw(int loops, SkCanvas*) override {
79 void* src_ctx = src;
80 void* dst_ctx = dst;
81
82 SkRasterPipeline p;
83 p.append(SkRasterPipeline::load_8888, &dst_ctx);
84 p.append(SkRasterPipeline::move_src_dst);
85 p.append(SkRasterPipeline::load_8888, &src_ctx);
86 p.append(SkRasterPipeline::srcover);
87 p.append(SkRasterPipeline::store_8888, &dst_ctx);
88
Mike Klein0a76b412017-05-22 12:01:59 -040089 if (fCompile) {
90 char buffer[1024];
91 SkArenaAlloc alloc(buffer);
92 auto fn = p.compile(&alloc);
93 while (loops --> 0) {
94 fn(0,N);
95 }
96 } else {
97 while (loops --> 0) {
98 p.run(0,N);
99 }
Mike Kleinf7688562017-01-17 10:24:15 -0500100 }
101 }
Mike Klein0a76b412017-05-22 12:01:59 -0400102private:
103 bool fCompile;
Mike Kleinf7688562017-01-17 10:24:15 -0500104};
Mike Klein0a76b412017-05-22 12:01:59 -0400105DEF_BENCH( return (new SkRasterPipelineCompileVsRunBench(true )); )
106DEF_BENCH( return (new SkRasterPipelineCompileVsRunBench(false)); )
Mike Klein795c5b12017-04-21 12:05:01 -0400107
Mike Kleinc7be0032017-04-25 15:51:23 -0400108static SkColorSpaceTransferFn gamma(float g) {
109 SkColorSpaceTransferFn fn = {0,0,0,0,0,0,0};
110 fn.fG = g;
111 fn.fA = 1;
112 return fn;
113}
114
Mike Klein795c5b12017-04-21 12:05:01 -0400115class SkRasterPipeline_2dot2 : public Benchmark {
116public:
117 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
118 const char* onGetName() override {
119 return "SkRasterPipeline_2dot2";
120 }
121
122 void onDraw(int loops, SkCanvas*) override {
123 SkColor4f c = { 1.0f, 1.0f, 1.0f, 1.0f };
Mike Kleinc7be0032017-04-25 15:51:23 -0400124
125 SkColorSpaceTransferFn from_2dot2 = gamma( 2.2f),
126 to_2dot2 = gamma(1/2.2f);
Mike Klein795c5b12017-04-21 12:05:01 -0400127 SkRasterPipeline p;
128 p.append(SkRasterPipeline::constant_color, &c);
Mike Kleinc7be0032017-04-25 15:51:23 -0400129 p.append(SkRasterPipeline::parametric_r, &from_2dot2);
130 p.append(SkRasterPipeline::parametric_g, &from_2dot2);
131 p.append(SkRasterPipeline::parametric_b, &from_2dot2);
132 p.append(SkRasterPipeline::parametric_r, & to_2dot2);
133 p.append(SkRasterPipeline::parametric_g, & to_2dot2);
134 p.append(SkRasterPipeline::parametric_b, & to_2dot2);
Mike Klein795c5b12017-04-21 12:05:01 -0400135
136 while (loops --> 0) {
137 p.run(0,N);
138 }
139 }
140};
141DEF_BENCH( return (new SkRasterPipeline_2dot2); )
Mike Kleinf45e3d72017-05-15 17:36:59 -0400142
143class SkRasterPipelineToSRGB : public Benchmark {
144public:
145 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
146 const char* onGetName() override {
147 return "SkRasterPipeline_to_srgb";
148 }
149
150 void onDraw(int loops, SkCanvas*) override {
151 SkRasterPipeline p;
152 p.append(SkRasterPipeline::to_srgb);
153
154 while (loops --> 0) {
155 p.run(0,N);
156 }
157 }
158};
159DEF_BENCH( return (new SkRasterPipelineToSRGB); )
Mike Klein1859f692017-05-22 08:28:45 -0400160
161class SkRasterPipelineReuseBench : public Benchmark {
162public:
163 enum Mode { None, Some, Full };
164
165 explicit SkRasterPipelineReuseBench(Mode mode) : fMode(mode), fName("SkRasterPipelineReuse") {
166 switch(mode) {
167 case None: fName.append("_none"); break;
168 case Some: fName.append("_some"); break;
169 case Full: fName.append("_full"); break;
170 }
171 }
172 const char* onGetName() override { return fName.c_str(); }
173 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
174
175 void onDraw(int loops, SkCanvas*) override {
176 const int kStages = 20;
177 const auto stage = SkRasterPipeline::to_srgb; // Any stage will do. We won't call it.
178
179 switch(fMode) {
180 case None:
181 while (loops --> 0) {
182 SkRasterPipeline p;
183 for (int i = 0; i < kStages; i++) {
184 p.append(stage);
185 }
186 }
187 break;
188
189 case Some:
190 while (loops --> 0) {
191 SkRasterPipeline p(kStages);
192 for (int i = 0; i < kStages; i++) {
193 p.append(stage);
194 }
195 }
196 break;
197
198 case Full:
199 SkRasterPipeline p(kStages);
200 while (loops --> 0) {
201 p.rewind();
202 for (int i = 0; i < kStages; i++) {
203 p.append(stage);
204 }
205 }
206 break;
207 }
208 }
209
210private:
211 Mode fMode;
212 SkString fName;
213};
214DEF_BENCH( return (new SkRasterPipelineReuseBench(SkRasterPipelineReuseBench::None)); )
215DEF_BENCH( return (new SkRasterPipelineReuseBench(SkRasterPipelineReuseBench::Some)); )
216DEF_BENCH( return (new SkRasterPipelineReuseBench(SkRasterPipelineReuseBench::Full)); )