blob: f612357c6cdeb8c1b3b6b0dcbe470dae0db09264 [file] [log] [blame]
halcanary1abea462016-02-24 09:25:58 -08001/*
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"
Hal Canary43fb7a02016-12-30 13:09:03 -05009
halcanary1abea462016-02-24 09:25:58 -080010#include "Resources.h"
robertphillipsc5035e72016-03-17 06:58:39 -070011#include "SkAutoPixmapStorage.h"
halcanary1abea462016-02-24 09:25:58 -080012#include "SkData.h"
Hal Canary3c36ef62018-01-02 16:25:53 -050013#include "SkFloatToDecimal.h"
halcanaryd11c7262016-03-25 05:52:57 -070014#include "SkGradientShader.h"
halcanary1e440512016-02-24 15:17:19 -080015#include "SkImage.h"
halcanary1e440512016-02-24 15:17:19 -080016#include "SkPixmap.h"
halcanarycf430132016-03-09 10:49:23 -080017#include "SkRandom.h"
halcanarycbc060a2016-04-11 19:41:48 -070018#include "SkStream.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040019#include "SkTo.h"
halcanary1abea462016-02-24 09:25:58 -080020
21namespace {
Hal Canary43fb7a02016-12-30 13:09:03 -050022struct WStreamWriteTextBenchmark : public Benchmark {
23 std::unique_ptr<SkWStream> fWStream;
Mike Reed2a65cc02017-03-22 10:01:53 -040024 WStreamWriteTextBenchmark() : fWStream(new SkNullWStream) {}
Hal Canary43fb7a02016-12-30 13:09:03 -050025 const char* onGetName() override { return "WStreamWriteText"; }
26 bool isSuitableFor(Backend backend) override {
27 return backend == kNonRendering_Backend;
28 }
29 void onDraw(int loops, SkCanvas*) override {
30 while (loops-- > 0) {
31 for (int i = 1000; i-- > 0;) {
32 fWStream->writeText("HELLO SKIA!\n");
33 }
34 }
35 }
36};
37} // namespace
38
39DEF_BENCH(return new WStreamWriteTextBenchmark;)
40
Hal Canary3c36ef62018-01-02 16:25:53 -050041// Test speed of SkFloatToDecimal for typical floats that
42// might be found in a PDF document.
43struct PDFScalarBench : public Benchmark {
Hal Canaryb4a8a622018-02-21 15:49:41 -050044 PDFScalarBench(const char* n, float (*f)(SkRandom*)) : fName(n), fNextFloat(f) {}
45 const char* fName;
46 float (*fNextFloat)(SkRandom*);
Hal Canary3c36ef62018-01-02 16:25:53 -050047 bool isSuitableFor(Backend b) override {
48 return b == kNonRendering_Backend;
49 }
Hal Canaryb4a8a622018-02-21 15:49:41 -050050 const char* onGetName() override { return fName; }
Hal Canary3c36ef62018-01-02 16:25:53 -050051 void onDraw(int loops, SkCanvas*) override {
52 SkRandom random;
53 char dst[kMaximumSkFloatToDecimalLength];
54 while (loops-- > 0) {
Hal Canaryb4a8a622018-02-21 15:49:41 -050055 auto f = fNextFloat(&random);
Hal Canary3c36ef62018-01-02 16:25:53 -050056 (void)SkFloatToDecimal(f, dst);
57 }
58 }
59};
60
Hal Canaryb4a8a622018-02-21 15:49:41 -050061float next_common(SkRandom* random) {
62 return random->nextRangeF(-500.0f, 1500.0f);
63}
64float next_any(SkRandom* random) {
65 union { uint32_t u; float f; };
66 u = random->nextU();
67 static_assert(sizeof(float) == sizeof(uint32_t), "");
68 return f;
69}
70
71DEF_BENCH(return new PDFScalarBench("PDFScalar_common", next_common);)
72DEF_BENCH(return new PDFScalarBench("PDFScalar_random", next_any);)
Hal Canary3c36ef62018-01-02 16:25:53 -050073
Hal Canary43fb7a02016-12-30 13:09:03 -050074#ifdef SK_SUPPORT_PDF
75
76#include "SkPDFBitmap.h"
Hal Canary23564b92018-09-07 14:33:14 -040077#include "SkPDFDocumentPriv.h"
Hal Canary43fb7a02016-12-30 13:09:03 -050078#include "SkPDFShader.h"
Hal Canary23564b92018-09-07 14:33:14 -040079#include "SkPDFUtils.h"
Hal Canary43fb7a02016-12-30 13:09:03 -050080
81namespace {
halcanarya50151d2016-03-25 11:57:49 -070082static void test_pdf_object_serialization(const sk_sp<SkPDFObject> object) {
halcanary1abea462016-02-24 09:25:58 -080083 // SkDebugWStream wStream;
Mike Reed2a65cc02017-03-22 10:01:53 -040084 SkNullWStream wStream;
halcanary1abea462016-02-24 09:25:58 -080085 SkPDFObjNumMap objNumMap;
halcanary530032a2016-08-18 14:22:52 -070086 objNumMap.addObjectRecursively(object.get());
Hal Canary9e41c212018-09-03 12:00:23 -040087 for (size_t i = 0; i < objNumMap.objects().size(); ++i) {
halcanarybae235e2016-03-21 10:05:23 -070088 SkPDFObject* object = objNumMap.objects()[i].get();
Cary Clark60ebf142018-09-06 12:22:33 +000089 wStream.writeDecAsText(i + 1);
halcanary1abea462016-02-24 09:25:58 -080090 wStream.writeText(" 0 obj\n");
Hal Canaryf6462c42018-11-13 16:19:59 -050091 object->emitObject(&wStream);
halcanary1abea462016-02-24 09:25:58 -080092 wStream.writeText("\nendobj\n");
93 }
94}
95
96class PDFImageBench : public Benchmark {
97public:
98 PDFImageBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -040099 ~PDFImageBench() override {}
halcanary1abea462016-02-24 09:25:58 -0800100
101protected:
102 const char* onGetName() override { return "PDFImage"; }
103 bool isSuitableFor(Backend backend) override {
104 return backend == kNonRendering_Backend;
105 }
106 void onDelayedSetup() override {
Hal Canaryc465d132017-12-08 10:21:31 -0500107 sk_sp<SkImage> img(GetResourceAsImage("images/color_wheel.png"));
halcanary1abea462016-02-24 09:25:58 -0800108 if (img) {
109 // force decoding, throw away reference to encoded data.
110 SkAutoPixmapStorage pixmap;
111 pixmap.alloc(SkImageInfo::MakeN32Premul(img->dimensions()));
112 if (img->readPixels(pixmap, 0, 0)) {
reed9ce9d672016-03-17 10:51:11 -0700113 fImage = SkImage::MakeRasterCopy(pixmap);
halcanary1abea462016-02-24 09:25:58 -0800114 }
115 }
116 }
117 void onDraw(int loops, SkCanvas*) override {
118 if (!fImage) {
119 return;
120 }
121 while (loops-- > 0) {
Hal Canarya1211832018-11-13 16:45:14 -0500122 SkNullWStream nullStream;
123 SkPDFDocument doc(&nullStream, SkPDF::Metadata());
124 doc.beginPage(256, 256);
125 (void)SkPDFSerializeImage(fImage.get(), &doc);
halcanary1abea462016-02-24 09:25:58 -0800126 }
127 }
128
129private:
reed9ce9d672016-03-17 10:51:11 -0700130 sk_sp<SkImage> fImage;
halcanary1abea462016-02-24 09:25:58 -0800131};
132
133class PDFJpegImageBench : public Benchmark {
134public:
135 PDFJpegImageBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -0400136 ~PDFJpegImageBench() override {}
halcanary1abea462016-02-24 09:25:58 -0800137
138protected:
139 const char* onGetName() override { return "PDFJpegImage"; }
140 bool isSuitableFor(Backend backend) override {
141 return backend == kNonRendering_Backend;
142 }
143 void onDelayedSetup() override {
Hal Canaryc465d132017-12-08 10:21:31 -0500144 sk_sp<SkImage> img(GetResourceAsImage("images/mandrill_512_q075.jpg"));
halcanary1abea462016-02-24 09:25:58 -0800145 if (!img) { return; }
Mike Reed6409f842017-07-11 16:03:13 -0400146 sk_sp<SkData> encoded = img->refEncodedData();
halcanary1abea462016-02-24 09:25:58 -0800147 SkASSERT(encoded);
148 if (!encoded) { return; }
reed9ce9d672016-03-17 10:51:11 -0700149 fImage = img;
halcanary1abea462016-02-24 09:25:58 -0800150 }
151 void onDraw(int loops, SkCanvas*) override {
152 if (!fImage) {
153 SkDEBUGFAIL("");
154 return;
155 }
156 while (loops-- > 0) {
Hal Canarya1211832018-11-13 16:45:14 -0500157 SkNullWStream nullStream;
158 SkPDFDocument doc(&nullStream, SkPDF::Metadata());
159 doc.beginPage(256, 256);
160 (void)SkPDFSerializeImage(fImage.get(), &doc);
halcanary1abea462016-02-24 09:25:58 -0800161 }
162 }
163
164private:
reed9ce9d672016-03-17 10:51:11 -0700165 sk_sp<SkImage> fImage;
halcanary1abea462016-02-24 09:25:58 -0800166};
167
halcanary1e440512016-02-24 15:17:19 -0800168/** Test calling DEFLATE on a 78k PDF command stream. Used for measuring
169 alternate zlib settings, usage, and library versions. */
170class PDFCompressionBench : public Benchmark {
171public:
172 PDFCompressionBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -0400173 ~PDFCompressionBench() override {}
halcanary1e440512016-02-24 15:17:19 -0800174
175protected:
176 const char* onGetName() override { return "PDFCompression"; }
177 bool isSuitableFor(Backend backend) override {
178 return backend == kNonRendering_Backend;
179 }
180 void onDelayedSetup() override {
Mike Reed71f867c2017-07-23 13:14:10 -0400181 fAsset = GetResourceAsStream("pdf_command_stream.txt");
halcanary1e440512016-02-24 15:17:19 -0800182 }
183 void onDraw(int loops, SkCanvas*) override {
184 SkASSERT(fAsset);
185 if (!fAsset) { return; }
186 while (loops-- > 0) {
halcanaryfa251062016-07-29 10:13:18 -0700187 sk_sp<SkPDFObject> object =
188 sk_make_sp<SkPDFSharedStream>(
189 std::unique_ptr<SkStreamAsset>(fAsset->duplicate()));
halcanary1e440512016-02-24 15:17:19 -0800190 test_pdf_object_serialization(object);
191 }
192 }
193
194private:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400195 std::unique_ptr<SkStreamAsset> fAsset;
halcanary1e440512016-02-24 15:17:19 -0800196};
197
halcanaryeb92cb32016-07-15 13:41:27 -0700198struct PDFColorComponentBench : public Benchmark {
199 bool isSuitableFor(Backend b) override {
200 return b == kNonRendering_Backend;
201 }
202 const char* onGetName() override { return "PDFColorComponent"; }
203 void onDraw(int loops, SkCanvas*) override {
204 char dst[5];
205 while (loops-- > 0) {
206 for (int i = 0; i < 256; ++i) {
207 (void)SkPDFUtils::ColorToDecimal(SkToU8(i), dst);
208 }
209 }
210 }
211};
212
halcanaryd11c7262016-03-25 05:52:57 -0700213struct PDFShaderBench : public Benchmark {
214 sk_sp<SkShader> fShader;
215 const char* onGetName() final { return "PDFShader"; }
216 bool isSuitableFor(Backend b) final { return b == kNonRendering_Backend; }
217 void onDelayedSetup() final {
218 const SkPoint pts[2] = {{0.0f, 0.0f}, {100.0f, 100.0f}};
219 const SkColor colors[] = {
220 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
221 SK_ColorWHITE, SK_ColorBLACK,
222 };
223 fShader = SkGradientShader::MakeLinear(
224 pts, colors, nullptr, SK_ARRAY_COUNT(colors),
225 SkShader::kClamp_TileMode);
226 }
227 void onDraw(int loops, SkCanvas*) final {
228 SkASSERT(fShader);
229 while (loops-- > 0) {
Mike Reed2a65cc02017-03-22 10:01:53 -0400230 SkNullWStream nullStream;
Hal Canary23564b92018-09-07 14:33:14 -0400231 SkPDFDocument doc(&nullStream, SkPDF::Metadata());
Hal Canary7e872ca2017-07-19 15:51:18 -0400232 sk_sp<SkPDFObject> shader = SkPDFMakeShader(&doc, fShader.get(), SkMatrix::I(),
233 {0, 0, 400, 400}, SK_ColorBLACK);
halcanaryd11c7262016-03-25 05:52:57 -0700234 }
235 }
236};
237
halcanaryee41b752016-06-23 14:08:11 -0700238struct WritePDFTextBenchmark : public Benchmark {
239 std::unique_ptr<SkWStream> fWStream;
Mike Reed2a65cc02017-03-22 10:01:53 -0400240 WritePDFTextBenchmark() : fWStream(new SkNullWStream) {}
halcanaryee41b752016-06-23 14:08:11 -0700241 const char* onGetName() override { return "WritePDFText"; }
242 bool isSuitableFor(Backend backend) override {
243 return backend == kNonRendering_Backend;
244 }
245 void onDraw(int loops, SkCanvas*) override {
246 static const char kHello[] = "HELLO SKIA!\n";
247 static const char kBinary[] = "\001\002\003\004\005\006";
248 while (loops-- > 0) {
249 for (int i = 1000; i-- > 0;) {
Hal Canarye650b852018-09-12 09:12:36 -0400250 SkPDFWriteString(fWStream.get(), kHello, strlen(kHello));
251 SkPDFWriteString(fWStream.get(), kBinary, strlen(kBinary));
halcanaryee41b752016-06-23 14:08:11 -0700252 }
253 }
254 }
255};
256
halcanary1abea462016-02-24 09:25:58 -0800257} // namespace
258DEF_BENCH(return new PDFImageBench;)
259DEF_BENCH(return new PDFJpegImageBench;)
halcanary1e440512016-02-24 15:17:19 -0800260DEF_BENCH(return new PDFCompressionBench;)
halcanaryeb92cb32016-07-15 13:41:27 -0700261DEF_BENCH(return new PDFColorComponentBench;)
halcanaryd11c7262016-03-25 05:52:57 -0700262DEF_BENCH(return new PDFShaderBench;)
halcanaryee41b752016-06-23 14:08:11 -0700263DEF_BENCH(return new WritePDFTextBenchmark;)
Hal Canary43fb7a02016-12-30 13:09:03 -0500264
265#endif
266