blob: bebf2b9f17a74c82b5715865594b327913672351 [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"
halcanary1abea462016-02-24 09:25:58 -080019
20namespace {
Hal Canary43fb7a02016-12-30 13:09:03 -050021struct WStreamWriteTextBenchmark : public Benchmark {
22 std::unique_ptr<SkWStream> fWStream;
Mike Reed2a65cc02017-03-22 10:01:53 -040023 WStreamWriteTextBenchmark() : fWStream(new SkNullWStream) {}
Hal Canary43fb7a02016-12-30 13:09:03 -050024 const char* onGetName() override { return "WStreamWriteText"; }
25 bool isSuitableFor(Backend backend) override {
26 return backend == kNonRendering_Backend;
27 }
28 void onDraw(int loops, SkCanvas*) override {
29 while (loops-- > 0) {
30 for (int i = 1000; i-- > 0;) {
31 fWStream->writeText("HELLO SKIA!\n");
32 }
33 }
34 }
35};
36} // namespace
37
38DEF_BENCH(return new WStreamWriteTextBenchmark;)
39
Hal Canary3c36ef62018-01-02 16:25:53 -050040// Test speed of SkFloatToDecimal for typical floats that
41// might be found in a PDF document.
42struct PDFScalarBench : public Benchmark {
43 bool isSuitableFor(Backend b) override {
44 return b == kNonRendering_Backend;
45 }
46 const char* onGetName() override { return "PDFScalar"; }
47 void onDraw(int loops, SkCanvas*) override {
48 SkRandom random;
49 char dst[kMaximumSkFloatToDecimalLength];
50 while (loops-- > 0) {
51 auto f = random.nextRangeF(-500.0f, 1500.0f);
52 (void)SkFloatToDecimal(f, dst);
53 }
54 }
55};
56
57DEF_BENCH(return new PDFScalarBench;)
58
Hal Canary43fb7a02016-12-30 13:09:03 -050059#ifdef SK_SUPPORT_PDF
60
61#include "SkPDFBitmap.h"
62#include "SkPDFDocument.h"
63#include "SkPDFShader.h"
Hal Canary43fb7a02016-12-30 13:09:03 -050064
65namespace {
halcanarya50151d2016-03-25 11:57:49 -070066static void test_pdf_object_serialization(const sk_sp<SkPDFObject> object) {
halcanary1abea462016-02-24 09:25:58 -080067 // SkDebugWStream wStream;
Mike Reed2a65cc02017-03-22 10:01:53 -040068 SkNullWStream wStream;
halcanary1abea462016-02-24 09:25:58 -080069 SkPDFObjNumMap objNumMap;
halcanary530032a2016-08-18 14:22:52 -070070 objNumMap.addObjectRecursively(object.get());
halcanary1abea462016-02-24 09:25:58 -080071 for (int i = 0; i < objNumMap.objects().count(); ++i) {
halcanarybae235e2016-03-21 10:05:23 -070072 SkPDFObject* object = objNumMap.objects()[i].get();
halcanary1abea462016-02-24 09:25:58 -080073 wStream.writeDecAsText(i + 1);
74 wStream.writeText(" 0 obj\n");
halcanary530032a2016-08-18 14:22:52 -070075 object->emitObject(&wStream, objNumMap);
halcanary1abea462016-02-24 09:25:58 -080076 wStream.writeText("\nendobj\n");
77 }
78}
79
80class PDFImageBench : public Benchmark {
81public:
82 PDFImageBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -040083 ~PDFImageBench() override {}
halcanary1abea462016-02-24 09:25:58 -080084
85protected:
86 const char* onGetName() override { return "PDFImage"; }
87 bool isSuitableFor(Backend backend) override {
88 return backend == kNonRendering_Backend;
89 }
90 void onDelayedSetup() override {
Hal Canaryc465d132017-12-08 10:21:31 -050091 sk_sp<SkImage> img(GetResourceAsImage("images/color_wheel.png"));
halcanary1abea462016-02-24 09:25:58 -080092 if (img) {
93 // force decoding, throw away reference to encoded data.
94 SkAutoPixmapStorage pixmap;
95 pixmap.alloc(SkImageInfo::MakeN32Premul(img->dimensions()));
96 if (img->readPixels(pixmap, 0, 0)) {
reed9ce9d672016-03-17 10:51:11 -070097 fImage = SkImage::MakeRasterCopy(pixmap);
halcanary1abea462016-02-24 09:25:58 -080098 }
99 }
100 }
101 void onDraw(int loops, SkCanvas*) override {
102 if (!fImage) {
103 return;
104 }
105 while (loops-- > 0) {
Mike Reed2e3f2e92017-12-15 20:43:03 +0000106 auto object = SkPDFCreateBitmapObject(fImage);
halcanary1e440512016-02-24 15:17:19 -0800107 SkASSERT(object);
108 if (!object) {
109 return;
110 }
111 test_pdf_object_serialization(object);
halcanary1abea462016-02-24 09:25:58 -0800112 }
113 }
114
115private:
reed9ce9d672016-03-17 10:51:11 -0700116 sk_sp<SkImage> fImage;
halcanary1abea462016-02-24 09:25:58 -0800117};
118
119class PDFJpegImageBench : public Benchmark {
120public:
121 PDFJpegImageBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -0400122 ~PDFJpegImageBench() override {}
halcanary1abea462016-02-24 09:25:58 -0800123
124protected:
125 const char* onGetName() override { return "PDFJpegImage"; }
126 bool isSuitableFor(Backend backend) override {
127 return backend == kNonRendering_Backend;
128 }
129 void onDelayedSetup() override {
Hal Canaryc465d132017-12-08 10:21:31 -0500130 sk_sp<SkImage> img(GetResourceAsImage("images/mandrill_512_q075.jpg"));
halcanary1abea462016-02-24 09:25:58 -0800131 if (!img) { return; }
Mike Reed6409f842017-07-11 16:03:13 -0400132 sk_sp<SkData> encoded = img->refEncodedData();
halcanary1abea462016-02-24 09:25:58 -0800133 SkASSERT(encoded);
134 if (!encoded) { return; }
reed9ce9d672016-03-17 10:51:11 -0700135 fImage = img;
halcanary1abea462016-02-24 09:25:58 -0800136 }
137 void onDraw(int loops, SkCanvas*) override {
138 if (!fImage) {
139 SkDEBUGFAIL("");
140 return;
141 }
142 while (loops-- > 0) {
Mike Reed2e3f2e92017-12-15 20:43:03 +0000143 auto object = SkPDFCreateBitmapObject(fImage);
halcanary1e440512016-02-24 15:17:19 -0800144 SkASSERT(object);
145 if (!object) {
146 return;
147 }
148 test_pdf_object_serialization(object);
halcanary1abea462016-02-24 09:25:58 -0800149 }
150 }
151
152private:
reed9ce9d672016-03-17 10:51:11 -0700153 sk_sp<SkImage> fImage;
halcanary1abea462016-02-24 09:25:58 -0800154};
155
halcanary1e440512016-02-24 15:17:19 -0800156/** Test calling DEFLATE on a 78k PDF command stream. Used for measuring
157 alternate zlib settings, usage, and library versions. */
158class PDFCompressionBench : public Benchmark {
159public:
160 PDFCompressionBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -0400161 ~PDFCompressionBench() override {}
halcanary1e440512016-02-24 15:17:19 -0800162
163protected:
164 const char* onGetName() override { return "PDFCompression"; }
165 bool isSuitableFor(Backend backend) override {
166 return backend == kNonRendering_Backend;
167 }
168 void onDelayedSetup() override {
Mike Reed71f867c2017-07-23 13:14:10 -0400169 fAsset = GetResourceAsStream("pdf_command_stream.txt");
halcanary1e440512016-02-24 15:17:19 -0800170 }
171 void onDraw(int loops, SkCanvas*) override {
172 SkASSERT(fAsset);
173 if (!fAsset) { return; }
174 while (loops-- > 0) {
halcanaryfa251062016-07-29 10:13:18 -0700175 sk_sp<SkPDFObject> object =
176 sk_make_sp<SkPDFSharedStream>(
177 std::unique_ptr<SkStreamAsset>(fAsset->duplicate()));
halcanary1e440512016-02-24 15:17:19 -0800178 test_pdf_object_serialization(object);
179 }
180 }
181
182private:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400183 std::unique_ptr<SkStreamAsset> fAsset;
halcanary1e440512016-02-24 15:17:19 -0800184};
185
halcanaryeb92cb32016-07-15 13:41:27 -0700186struct PDFColorComponentBench : public Benchmark {
187 bool isSuitableFor(Backend b) override {
188 return b == kNonRendering_Backend;
189 }
190 const char* onGetName() override { return "PDFColorComponent"; }
191 void onDraw(int loops, SkCanvas*) override {
192 char dst[5];
193 while (loops-- > 0) {
194 for (int i = 0; i < 256; ++i) {
195 (void)SkPDFUtils::ColorToDecimal(SkToU8(i), dst);
196 }
197 }
198 }
199};
200
halcanaryd11c7262016-03-25 05:52:57 -0700201struct PDFShaderBench : public Benchmark {
202 sk_sp<SkShader> fShader;
203 const char* onGetName() final { return "PDFShader"; }
204 bool isSuitableFor(Backend b) final { return b == kNonRendering_Backend; }
205 void onDelayedSetup() final {
206 const SkPoint pts[2] = {{0.0f, 0.0f}, {100.0f, 100.0f}};
207 const SkColor colors[] = {
208 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
209 SK_ColorWHITE, SK_ColorBLACK,
210 };
211 fShader = SkGradientShader::MakeLinear(
212 pts, colors, nullptr, SK_ARRAY_COUNT(colors),
213 SkShader::kClamp_TileMode);
214 }
215 void onDraw(int loops, SkCanvas*) final {
216 SkASSERT(fShader);
217 while (loops-- > 0) {
Mike Reed2a65cc02017-03-22 10:01:53 -0400218 SkNullWStream nullStream;
Hal Canaryc5980d02018-01-08 15:02:36 -0500219 SkPDFDocument doc(&nullStream, SkDocument::PDFMetadata());
Hal Canary7e872ca2017-07-19 15:51:18 -0400220 sk_sp<SkPDFObject> shader = SkPDFMakeShader(&doc, fShader.get(), SkMatrix::I(),
221 {0, 0, 400, 400}, SK_ColorBLACK);
halcanaryd11c7262016-03-25 05:52:57 -0700222 }
223 }
224};
225
halcanaryee41b752016-06-23 14:08:11 -0700226struct WritePDFTextBenchmark : public Benchmark {
227 std::unique_ptr<SkWStream> fWStream;
Mike Reed2a65cc02017-03-22 10:01:53 -0400228 WritePDFTextBenchmark() : fWStream(new SkNullWStream) {}
halcanaryee41b752016-06-23 14:08:11 -0700229 const char* onGetName() override { return "WritePDFText"; }
230 bool isSuitableFor(Backend backend) override {
231 return backend == kNonRendering_Backend;
232 }
233 void onDraw(int loops, SkCanvas*) override {
234 static const char kHello[] = "HELLO SKIA!\n";
235 static const char kBinary[] = "\001\002\003\004\005\006";
236 while (loops-- > 0) {
237 for (int i = 1000; i-- > 0;) {
238 SkPDFUtils::WriteString(fWStream.get(), kHello, strlen(kHello));
239 SkPDFUtils::WriteString(fWStream.get(), kBinary, strlen(kBinary));
240 }
241 }
242 }
243};
244
halcanary1abea462016-02-24 09:25:58 -0800245} // namespace
246DEF_BENCH(return new PDFImageBench;)
247DEF_BENCH(return new PDFJpegImageBench;)
halcanary1e440512016-02-24 15:17:19 -0800248DEF_BENCH(return new PDFCompressionBench;)
halcanaryeb92cb32016-07-15 13:41:27 -0700249DEF_BENCH(return new PDFColorComponentBench;)
halcanaryd11c7262016-03-25 05:52:57 -0700250DEF_BENCH(return new PDFShaderBench;)
halcanaryee41b752016-06-23 14:08:11 -0700251DEF_BENCH(return new WritePDFTextBenchmark;)
Hal Canary43fb7a02016-12-30 13:09:03 -0500252
253#endif
254