blob: 44b96a73dd2ae6b15437d7f685287648bc8f5e36 [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"
77#include "SkPDFDocument.h"
78#include "SkPDFShader.h"
Hal Canary43fb7a02016-12-30 13:09:03 -050079
80namespace {
halcanarya50151d2016-03-25 11:57:49 -070081static void test_pdf_object_serialization(const sk_sp<SkPDFObject> object) {
halcanary1abea462016-02-24 09:25:58 -080082 // SkDebugWStream wStream;
Mike Reed2a65cc02017-03-22 10:01:53 -040083 SkNullWStream wStream;
halcanary1abea462016-02-24 09:25:58 -080084 SkPDFObjNumMap objNumMap;
halcanary530032a2016-08-18 14:22:52 -070085 objNumMap.addObjectRecursively(object.get());
halcanary1abea462016-02-24 09:25:58 -080086 for (int i = 0; i < objNumMap.objects().count(); ++i) {
halcanarybae235e2016-03-21 10:05:23 -070087 SkPDFObject* object = objNumMap.objects()[i].get();
halcanary1abea462016-02-24 09:25:58 -080088 wStream.writeDecAsText(i + 1);
89 wStream.writeText(" 0 obj\n");
halcanary530032a2016-08-18 14:22:52 -070090 object->emitObject(&wStream, objNumMap);
halcanary1abea462016-02-24 09:25:58 -080091 wStream.writeText("\nendobj\n");
92 }
93}
94
95class PDFImageBench : public Benchmark {
96public:
97 PDFImageBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -040098 ~PDFImageBench() override {}
halcanary1abea462016-02-24 09:25:58 -080099
100protected:
101 const char* onGetName() override { return "PDFImage"; }
102 bool isSuitableFor(Backend backend) override {
103 return backend == kNonRendering_Backend;
104 }
105 void onDelayedSetup() override {
Hal Canaryc465d132017-12-08 10:21:31 -0500106 sk_sp<SkImage> img(GetResourceAsImage("images/color_wheel.png"));
halcanary1abea462016-02-24 09:25:58 -0800107 if (img) {
108 // force decoding, throw away reference to encoded data.
109 SkAutoPixmapStorage pixmap;
110 pixmap.alloc(SkImageInfo::MakeN32Premul(img->dimensions()));
111 if (img->readPixels(pixmap, 0, 0)) {
reed9ce9d672016-03-17 10:51:11 -0700112 fImage = SkImage::MakeRasterCopy(pixmap);
halcanary1abea462016-02-24 09:25:58 -0800113 }
114 }
115 }
116 void onDraw(int loops, SkCanvas*) override {
117 if (!fImage) {
118 return;
119 }
120 while (loops-- > 0) {
Mike Reed2e3f2e92017-12-15 20:43:03 +0000121 auto object = SkPDFCreateBitmapObject(fImage);
halcanary1e440512016-02-24 15:17:19 -0800122 SkASSERT(object);
123 if (!object) {
124 return;
125 }
126 test_pdf_object_serialization(object);
halcanary1abea462016-02-24 09:25:58 -0800127 }
128 }
129
130private:
reed9ce9d672016-03-17 10:51:11 -0700131 sk_sp<SkImage> fImage;
halcanary1abea462016-02-24 09:25:58 -0800132};
133
134class PDFJpegImageBench : public Benchmark {
135public:
136 PDFJpegImageBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -0400137 ~PDFJpegImageBench() override {}
halcanary1abea462016-02-24 09:25:58 -0800138
139protected:
140 const char* onGetName() override { return "PDFJpegImage"; }
141 bool isSuitableFor(Backend backend) override {
142 return backend == kNonRendering_Backend;
143 }
144 void onDelayedSetup() override {
Hal Canaryc465d132017-12-08 10:21:31 -0500145 sk_sp<SkImage> img(GetResourceAsImage("images/mandrill_512_q075.jpg"));
halcanary1abea462016-02-24 09:25:58 -0800146 if (!img) { return; }
Mike Reed6409f842017-07-11 16:03:13 -0400147 sk_sp<SkData> encoded = img->refEncodedData();
halcanary1abea462016-02-24 09:25:58 -0800148 SkASSERT(encoded);
149 if (!encoded) { return; }
reed9ce9d672016-03-17 10:51:11 -0700150 fImage = img;
halcanary1abea462016-02-24 09:25:58 -0800151 }
152 void onDraw(int loops, SkCanvas*) override {
153 if (!fImage) {
154 SkDEBUGFAIL("");
155 return;
156 }
157 while (loops-- > 0) {
Mike Reed2e3f2e92017-12-15 20:43:03 +0000158 auto object = SkPDFCreateBitmapObject(fImage);
halcanary1e440512016-02-24 15:17:19 -0800159 SkASSERT(object);
160 if (!object) {
161 return;
162 }
163 test_pdf_object_serialization(object);
halcanary1abea462016-02-24 09:25:58 -0800164 }
165 }
166
167private:
reed9ce9d672016-03-17 10:51:11 -0700168 sk_sp<SkImage> fImage;
halcanary1abea462016-02-24 09:25:58 -0800169};
170
halcanary1e440512016-02-24 15:17:19 -0800171/** Test calling DEFLATE on a 78k PDF command stream. Used for measuring
172 alternate zlib settings, usage, and library versions. */
173class PDFCompressionBench : public Benchmark {
174public:
175 PDFCompressionBench() {}
Brian Salomond3b65972017-03-22 12:05:03 -0400176 ~PDFCompressionBench() override {}
halcanary1e440512016-02-24 15:17:19 -0800177
178protected:
179 const char* onGetName() override { return "PDFCompression"; }
180 bool isSuitableFor(Backend backend) override {
181 return backend == kNonRendering_Backend;
182 }
183 void onDelayedSetup() override {
Mike Reed71f867c2017-07-23 13:14:10 -0400184 fAsset = GetResourceAsStream("pdf_command_stream.txt");
halcanary1e440512016-02-24 15:17:19 -0800185 }
186 void onDraw(int loops, SkCanvas*) override {
187 SkASSERT(fAsset);
188 if (!fAsset) { return; }
189 while (loops-- > 0) {
halcanaryfa251062016-07-29 10:13:18 -0700190 sk_sp<SkPDFObject> object =
191 sk_make_sp<SkPDFSharedStream>(
192 std::unique_ptr<SkStreamAsset>(fAsset->duplicate()));
halcanary1e440512016-02-24 15:17:19 -0800193 test_pdf_object_serialization(object);
194 }
195 }
196
197private:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400198 std::unique_ptr<SkStreamAsset> fAsset;
halcanary1e440512016-02-24 15:17:19 -0800199};
200
halcanaryeb92cb32016-07-15 13:41:27 -0700201struct PDFColorComponentBench : public Benchmark {
202 bool isSuitableFor(Backend b) override {
203 return b == kNonRendering_Backend;
204 }
205 const char* onGetName() override { return "PDFColorComponent"; }
206 void onDraw(int loops, SkCanvas*) override {
207 char dst[5];
208 while (loops-- > 0) {
209 for (int i = 0; i < 256; ++i) {
210 (void)SkPDFUtils::ColorToDecimal(SkToU8(i), dst);
211 }
212 }
213 }
214};
215
halcanaryd11c7262016-03-25 05:52:57 -0700216struct PDFShaderBench : public Benchmark {
217 sk_sp<SkShader> fShader;
218 const char* onGetName() final { return "PDFShader"; }
219 bool isSuitableFor(Backend b) final { return b == kNonRendering_Backend; }
220 void onDelayedSetup() final {
221 const SkPoint pts[2] = {{0.0f, 0.0f}, {100.0f, 100.0f}};
222 const SkColor colors[] = {
223 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
224 SK_ColorWHITE, SK_ColorBLACK,
225 };
226 fShader = SkGradientShader::MakeLinear(
227 pts, colors, nullptr, SK_ARRAY_COUNT(colors),
228 SkShader::kClamp_TileMode);
229 }
230 void onDraw(int loops, SkCanvas*) final {
231 SkASSERT(fShader);
232 while (loops-- > 0) {
Mike Reed2a65cc02017-03-22 10:01:53 -0400233 SkNullWStream nullStream;
Hal Canaryc5980d02018-01-08 15:02:36 -0500234 SkPDFDocument doc(&nullStream, SkDocument::PDFMetadata());
Hal Canary7e872ca2017-07-19 15:51:18 -0400235 sk_sp<SkPDFObject> shader = SkPDFMakeShader(&doc, fShader.get(), SkMatrix::I(),
236 {0, 0, 400, 400}, SK_ColorBLACK);
halcanaryd11c7262016-03-25 05:52:57 -0700237 }
238 }
239};
240
halcanaryee41b752016-06-23 14:08:11 -0700241struct WritePDFTextBenchmark : public Benchmark {
242 std::unique_ptr<SkWStream> fWStream;
Mike Reed2a65cc02017-03-22 10:01:53 -0400243 WritePDFTextBenchmark() : fWStream(new SkNullWStream) {}
halcanaryee41b752016-06-23 14:08:11 -0700244 const char* onGetName() override { return "WritePDFText"; }
245 bool isSuitableFor(Backend backend) override {
246 return backend == kNonRendering_Backend;
247 }
248 void onDraw(int loops, SkCanvas*) override {
249 static const char kHello[] = "HELLO SKIA!\n";
250 static const char kBinary[] = "\001\002\003\004\005\006";
251 while (loops-- > 0) {
252 for (int i = 1000; i-- > 0;) {
253 SkPDFUtils::WriteString(fWStream.get(), kHello, strlen(kHello));
254 SkPDFUtils::WriteString(fWStream.get(), kBinary, strlen(kBinary));
255 }
256 }
257 }
258};
259
halcanary1abea462016-02-24 09:25:58 -0800260} // namespace
261DEF_BENCH(return new PDFImageBench;)
262DEF_BENCH(return new PDFJpegImageBench;)
halcanary1e440512016-02-24 15:17:19 -0800263DEF_BENCH(return new PDFCompressionBench;)
halcanaryeb92cb32016-07-15 13:41:27 -0700264DEF_BENCH(return new PDFColorComponentBench;)
halcanaryd11c7262016-03-25 05:52:57 -0700265DEF_BENCH(return new PDFShaderBench;)
halcanaryee41b752016-06-23 14:08:11 -0700266DEF_BENCH(return new WritePDFTextBenchmark;)
Hal Canary43fb7a02016-12-30 13:09:03 -0500267
268#endif
269