blob: 9965941d8e34b7159c5144ddd6401047a08dc670 [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"
halcanaryd11c7262016-03-25 05:52:57 -070013#include "SkGradientShader.h"
halcanary1e440512016-02-24 15:17:19 -080014#include "SkImage.h"
halcanary1e440512016-02-24 15:17:19 -080015#include "SkPixmap.h"
halcanarycf430132016-03-09 10:49:23 -080016#include "SkRandom.h"
halcanarycbc060a2016-04-11 19:41:48 -070017#include "SkStream.h"
halcanary1abea462016-02-24 09:25:58 -080018
19namespace {
Hal Canary43fb7a02016-12-30 13:09:03 -050020struct WStreamWriteTextBenchmark : public Benchmark {
21 std::unique_ptr<SkWStream> fWStream;
Mike Reed2a65cc02017-03-22 10:01:53 -040022 WStreamWriteTextBenchmark() : fWStream(new SkNullWStream) {}
Hal Canary43fb7a02016-12-30 13:09:03 -050023 const char* onGetName() override { return "WStreamWriteText"; }
24 bool isSuitableFor(Backend backend) override {
25 return backend == kNonRendering_Backend;
26 }
27 void onDraw(int loops, SkCanvas*) override {
28 while (loops-- > 0) {
29 for (int i = 1000; i-- > 0;) {
30 fWStream->writeText("HELLO SKIA!\n");
31 }
32 }
33 }
34};
35} // namespace
36
37DEF_BENCH(return new WStreamWriteTextBenchmark;)
38
39#ifdef SK_SUPPORT_PDF
40
41#include "SkPDFBitmap.h"
42#include "SkPDFDocument.h"
43#include "SkPDFShader.h"
44#include "SkPDFUtils.h"
45
46namespace {
halcanarya50151d2016-03-25 11:57:49 -070047static void test_pdf_object_serialization(const sk_sp<SkPDFObject> object) {
halcanary1abea462016-02-24 09:25:58 -080048 // SkDebugWStream wStream;
Mike Reed2a65cc02017-03-22 10:01:53 -040049 SkNullWStream wStream;
halcanary1abea462016-02-24 09:25:58 -080050 SkPDFObjNumMap objNumMap;
halcanary530032a2016-08-18 14:22:52 -070051 objNumMap.addObjectRecursively(object.get());
halcanary1abea462016-02-24 09:25:58 -080052 for (int i = 0; i < objNumMap.objects().count(); ++i) {
halcanarybae235e2016-03-21 10:05:23 -070053 SkPDFObject* object = objNumMap.objects()[i].get();
halcanary1abea462016-02-24 09:25:58 -080054 wStream.writeDecAsText(i + 1);
55 wStream.writeText(" 0 obj\n");
halcanary530032a2016-08-18 14:22:52 -070056 object->emitObject(&wStream, objNumMap);
halcanary1abea462016-02-24 09:25:58 -080057 wStream.writeText("\nendobj\n");
58 }
59}
60
61class PDFImageBench : public Benchmark {
62public:
63 PDFImageBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -040064 ~PDFImageBench() override {}
halcanary1abea462016-02-24 09:25:58 -080065
66protected:
67 const char* onGetName() override { return "PDFImage"; }
68 bool isSuitableFor(Backend backend) override {
69 return backend == kNonRendering_Backend;
70 }
71 void onDelayedSetup() override {
reed9ce9d672016-03-17 10:51:11 -070072 sk_sp<SkImage> img(GetResourceAsImage("color_wheel.png"));
halcanary1abea462016-02-24 09:25:58 -080073 if (img) {
74 // force decoding, throw away reference to encoded data.
75 SkAutoPixmapStorage pixmap;
76 pixmap.alloc(SkImageInfo::MakeN32Premul(img->dimensions()));
77 if (img->readPixels(pixmap, 0, 0)) {
reed9ce9d672016-03-17 10:51:11 -070078 fImage = SkImage::MakeRasterCopy(pixmap);
halcanary1abea462016-02-24 09:25:58 -080079 }
80 }
81 }
82 void onDraw(int loops, SkCanvas*) override {
83 if (!fImage) {
84 return;
85 }
86 while (loops-- > 0) {
halcanarya50151d2016-03-25 11:57:49 -070087 auto object = SkPDFCreateBitmapObject(fImage, nullptr);
halcanary1e440512016-02-24 15:17:19 -080088 SkASSERT(object);
89 if (!object) {
90 return;
91 }
92 test_pdf_object_serialization(object);
halcanary1abea462016-02-24 09:25:58 -080093 }
94 }
95
96private:
reed9ce9d672016-03-17 10:51:11 -070097 sk_sp<SkImage> fImage;
halcanary1abea462016-02-24 09:25:58 -080098};
99
100class PDFJpegImageBench : public Benchmark {
101public:
102 PDFJpegImageBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -0400103 ~PDFJpegImageBench() override {}
halcanary1abea462016-02-24 09:25:58 -0800104
105protected:
106 const char* onGetName() override { return "PDFJpegImage"; }
107 bool isSuitableFor(Backend backend) override {
108 return backend == kNonRendering_Backend;
109 }
110 void onDelayedSetup() override {
reed9ce9d672016-03-17 10:51:11 -0700111 sk_sp<SkImage> img(GetResourceAsImage("mandrill_512_q075.jpg"));
halcanary1abea462016-02-24 09:25:58 -0800112 if (!img) { return; }
Mike Reed6409f842017-07-11 16:03:13 -0400113 sk_sp<SkData> encoded = img->refEncodedData();
halcanary1abea462016-02-24 09:25:58 -0800114 SkASSERT(encoded);
115 if (!encoded) { return; }
reed9ce9d672016-03-17 10:51:11 -0700116 fImage = img;
halcanary1abea462016-02-24 09:25:58 -0800117 }
118 void onDraw(int loops, SkCanvas*) override {
119 if (!fImage) {
120 SkDEBUGFAIL("");
121 return;
122 }
123 while (loops-- > 0) {
halcanarya50151d2016-03-25 11:57:49 -0700124 auto object = SkPDFCreateBitmapObject(fImage, nullptr);
halcanary1e440512016-02-24 15:17:19 -0800125 SkASSERT(object);
126 if (!object) {
127 return;
128 }
129 test_pdf_object_serialization(object);
halcanary1abea462016-02-24 09:25:58 -0800130 }
131 }
132
133private:
reed9ce9d672016-03-17 10:51:11 -0700134 sk_sp<SkImage> fImage;
halcanary1abea462016-02-24 09:25:58 -0800135};
136
halcanary1e440512016-02-24 15:17:19 -0800137/** Test calling DEFLATE on a 78k PDF command stream. Used for measuring
138 alternate zlib settings, usage, and library versions. */
139class PDFCompressionBench : public Benchmark {
140public:
141 PDFCompressionBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -0400142 ~PDFCompressionBench() override {}
halcanary1e440512016-02-24 15:17:19 -0800143
144protected:
145 const char* onGetName() override { return "PDFCompression"; }
146 bool isSuitableFor(Backend backend) override {
147 return backend == kNonRendering_Backend;
148 }
149 void onDelayedSetup() override {
Mike Reed71f867c2017-07-23 13:14:10 -0400150 fAsset = GetResourceAsStream("pdf_command_stream.txt");
halcanary1e440512016-02-24 15:17:19 -0800151 }
152 void onDraw(int loops, SkCanvas*) override {
153 SkASSERT(fAsset);
154 if (!fAsset) { return; }
155 while (loops-- > 0) {
halcanaryfa251062016-07-29 10:13:18 -0700156 sk_sp<SkPDFObject> object =
157 sk_make_sp<SkPDFSharedStream>(
158 std::unique_ptr<SkStreamAsset>(fAsset->duplicate()));
halcanary1e440512016-02-24 15:17:19 -0800159 test_pdf_object_serialization(object);
160 }
161 }
162
163private:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400164 std::unique_ptr<SkStreamAsset> fAsset;
halcanary1e440512016-02-24 15:17:19 -0800165};
166
halcanarycf430132016-03-09 10:49:23 -0800167// Test speed of SkPDFUtils::FloatToDecimal for typical floats that
168// might be found in a PDF document.
169struct PDFScalarBench : public Benchmark {
170 bool isSuitableFor(Backend b) override {
171 return b == kNonRendering_Backend;
172 }
173 const char* onGetName() override { return "PDFScalar"; }
174 void onDraw(int loops, SkCanvas*) override {
175 SkRandom random;
176 char dst[SkPDFUtils::kMaximumFloatDecimalLength];
177 while (loops-- > 0) {
178 auto f = random.nextRangeF(-500.0f, 1500.0f);
179 (void)SkPDFUtils::FloatToDecimal(f, dst);
180 }
181 }
182};
183
halcanaryeb92cb32016-07-15 13:41:27 -0700184struct PDFColorComponentBench : public Benchmark {
185 bool isSuitableFor(Backend b) override {
186 return b == kNonRendering_Backend;
187 }
188 const char* onGetName() override { return "PDFColorComponent"; }
189 void onDraw(int loops, SkCanvas*) override {
190 char dst[5];
191 while (loops-- > 0) {
192 for (int i = 0; i < 256; ++i) {
193 (void)SkPDFUtils::ColorToDecimal(SkToU8(i), dst);
194 }
195 }
196 }
197};
198
halcanaryd11c7262016-03-25 05:52:57 -0700199struct PDFShaderBench : public Benchmark {
200 sk_sp<SkShader> fShader;
201 const char* onGetName() final { return "PDFShader"; }
202 bool isSuitableFor(Backend b) final { return b == kNonRendering_Backend; }
203 void onDelayedSetup() final {
204 const SkPoint pts[2] = {{0.0f, 0.0f}, {100.0f, 100.0f}};
205 const SkColor colors[] = {
206 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
207 SK_ColorWHITE, SK_ColorBLACK,
208 };
209 fShader = SkGradientShader::MakeLinear(
210 pts, colors, nullptr, SK_ARRAY_COUNT(colors),
211 SkShader::kClamp_TileMode);
212 }
213 void onDraw(int loops, SkCanvas*) final {
214 SkASSERT(fShader);
215 while (loops-- > 0) {
Mike Reed2a65cc02017-03-22 10:01:53 -0400216 SkNullWStream nullStream;
halcanary4b656662016-04-27 07:45:18 -0700217 SkPDFDocument doc(&nullStream, nullptr, 72,
218 SkDocument::PDFMetadata(), nullptr, false);
Hal Canary7e872ca2017-07-19 15:51:18 -0400219 sk_sp<SkPDFObject> shader = SkPDFMakeShader(&doc, fShader.get(), SkMatrix::I(),
220 {0, 0, 400, 400}, SK_ColorBLACK);
halcanaryd11c7262016-03-25 05:52:57 -0700221 }
222 }
223};
224
halcanaryee41b752016-06-23 14:08:11 -0700225struct WritePDFTextBenchmark : public Benchmark {
226 std::unique_ptr<SkWStream> fWStream;
Mike Reed2a65cc02017-03-22 10:01:53 -0400227 WritePDFTextBenchmark() : fWStream(new SkNullWStream) {}
halcanaryee41b752016-06-23 14:08:11 -0700228 const char* onGetName() override { return "WritePDFText"; }
229 bool isSuitableFor(Backend backend) override {
230 return backend == kNonRendering_Backend;
231 }
232 void onDraw(int loops, SkCanvas*) override {
233 static const char kHello[] = "HELLO SKIA!\n";
234 static const char kBinary[] = "\001\002\003\004\005\006";
235 while (loops-- > 0) {
236 for (int i = 1000; i-- > 0;) {
237 SkPDFUtils::WriteString(fWStream.get(), kHello, strlen(kHello));
238 SkPDFUtils::WriteString(fWStream.get(), kBinary, strlen(kBinary));
239 }
240 }
241 }
242};
243
halcanary1abea462016-02-24 09:25:58 -0800244} // namespace
245DEF_BENCH(return new PDFImageBench;)
246DEF_BENCH(return new PDFJpegImageBench;)
halcanary1e440512016-02-24 15:17:19 -0800247DEF_BENCH(return new PDFCompressionBench;)
halcanarycf430132016-03-09 10:49:23 -0800248DEF_BENCH(return new PDFScalarBench;)
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