halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 1 | /* |
| 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 Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 9 | |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 10 | #include "Resources.h" |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 11 | #include "SkAutoPixmapStorage.h" |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 12 | #include "SkData.h" |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 13 | #include "SkFloatToDecimal.h" |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 14 | #include "SkGradientShader.h" |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 15 | #include "SkImage.h" |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 16 | #include "SkPixmap.h" |
halcanary | cf43013 | 2016-03-09 10:49:23 -0800 | [diff] [blame] | 17 | #include "SkRandom.h" |
halcanary | cbc060a | 2016-04-11 19:41:48 -0700 | [diff] [blame] | 18 | #include "SkStream.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 19 | #include "SkTo.h" |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 20 | |
| 21 | namespace { |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 22 | struct WStreamWriteTextBenchmark : public Benchmark { |
| 23 | std::unique_ptr<SkWStream> fWStream; |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 24 | WStreamWriteTextBenchmark() : fWStream(new SkNullWStream) {} |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 25 | 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 | |
| 39 | DEF_BENCH(return new WStreamWriteTextBenchmark;) |
| 40 | |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 41 | // Test speed of SkFloatToDecimal for typical floats that |
| 42 | // might be found in a PDF document. |
| 43 | struct PDFScalarBench : public Benchmark { |
Hal Canary | b4a8a62 | 2018-02-21 15:49:41 -0500 | [diff] [blame] | 44 | PDFScalarBench(const char* n, float (*f)(SkRandom*)) : fName(n), fNextFloat(f) {} |
| 45 | const char* fName; |
| 46 | float (*fNextFloat)(SkRandom*); |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 47 | bool isSuitableFor(Backend b) override { |
| 48 | return b == kNonRendering_Backend; |
| 49 | } |
Hal Canary | b4a8a62 | 2018-02-21 15:49:41 -0500 | [diff] [blame] | 50 | const char* onGetName() override { return fName; } |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 51 | void onDraw(int loops, SkCanvas*) override { |
| 52 | SkRandom random; |
| 53 | char dst[kMaximumSkFloatToDecimalLength]; |
| 54 | while (loops-- > 0) { |
Hal Canary | b4a8a62 | 2018-02-21 15:49:41 -0500 | [diff] [blame] | 55 | auto f = fNextFloat(&random); |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 56 | (void)SkFloatToDecimal(f, dst); |
| 57 | } |
| 58 | } |
| 59 | }; |
| 60 | |
Hal Canary | b4a8a62 | 2018-02-21 15:49:41 -0500 | [diff] [blame] | 61 | float next_common(SkRandom* random) { |
| 62 | return random->nextRangeF(-500.0f, 1500.0f); |
| 63 | } |
| 64 | float 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 | |
| 71 | DEF_BENCH(return new PDFScalarBench("PDFScalar_common", next_common);) |
| 72 | DEF_BENCH(return new PDFScalarBench("PDFScalar_random", next_any);) |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 73 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 74 | #ifdef SK_SUPPORT_PDF |
| 75 | |
| 76 | #include "SkPDFBitmap.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 77 | #include "SkPDFDocumentPriv.h" |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 78 | #include "SkPDFShader.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 79 | #include "SkPDFUtils.h" |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 80 | |
| 81 | namespace { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 82 | static void test_pdf_object_serialization(const sk_sp<SkPDFObject> object) { |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 83 | // SkDebugWStream wStream; |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 84 | SkNullWStream wStream; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 85 | SkPDFObjNumMap objNumMap; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 86 | objNumMap.addObjectRecursively(object.get()); |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 87 | for (size_t i = 0; i < objNumMap.objects().size(); ++i) { |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 88 | SkPDFObject* object = objNumMap.objects()[i].get(); |
Cary Clark | 60ebf14 | 2018-09-06 12:22:33 +0000 | [diff] [blame] | 89 | wStream.writeDecAsText(i + 1); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 90 | wStream.writeText(" 0 obj\n"); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 91 | object->emitObject(&wStream, objNumMap); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 92 | wStream.writeText("\nendobj\n"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | class PDFImageBench : public Benchmark { |
| 97 | public: |
| 98 | PDFImageBench() {} |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 99 | ~PDFImageBench() override {} |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 100 | |
| 101 | protected: |
| 102 | const char* onGetName() override { return "PDFImage"; } |
| 103 | bool isSuitableFor(Backend backend) override { |
| 104 | return backend == kNonRendering_Backend; |
| 105 | } |
| 106 | void onDelayedSetup() override { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 107 | sk_sp<SkImage> img(GetResourceAsImage("images/color_wheel.png")); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 108 | 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)) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 113 | fImage = SkImage::MakeRasterCopy(pixmap); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | void onDraw(int loops, SkCanvas*) override { |
| 118 | if (!fImage) { |
| 119 | return; |
| 120 | } |
| 121 | while (loops-- > 0) { |
Mike Reed | 2e3f2e9 | 2017-12-15 20:43:03 +0000 | [diff] [blame] | 122 | auto object = SkPDFCreateBitmapObject(fImage); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 123 | SkASSERT(object); |
| 124 | if (!object) { |
| 125 | return; |
| 126 | } |
| 127 | test_pdf_object_serialization(object); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 132 | sk_sp<SkImage> fImage; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class PDFJpegImageBench : public Benchmark { |
| 136 | public: |
| 137 | PDFJpegImageBench() {} |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 138 | ~PDFJpegImageBench() override {} |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 139 | |
| 140 | protected: |
| 141 | const char* onGetName() override { return "PDFJpegImage"; } |
| 142 | bool isSuitableFor(Backend backend) override { |
| 143 | return backend == kNonRendering_Backend; |
| 144 | } |
| 145 | void onDelayedSetup() override { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 146 | sk_sp<SkImage> img(GetResourceAsImage("images/mandrill_512_q075.jpg")); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 147 | if (!img) { return; } |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 148 | sk_sp<SkData> encoded = img->refEncodedData(); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 149 | SkASSERT(encoded); |
| 150 | if (!encoded) { return; } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 151 | fImage = img; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 152 | } |
| 153 | void onDraw(int loops, SkCanvas*) override { |
| 154 | if (!fImage) { |
| 155 | SkDEBUGFAIL(""); |
| 156 | return; |
| 157 | } |
| 158 | while (loops-- > 0) { |
Mike Reed | 2e3f2e9 | 2017-12-15 20:43:03 +0000 | [diff] [blame] | 159 | auto object = SkPDFCreateBitmapObject(fImage); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 160 | SkASSERT(object); |
| 161 | if (!object) { |
| 162 | return; |
| 163 | } |
| 164 | test_pdf_object_serialization(object); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 169 | sk_sp<SkImage> fImage; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 172 | /** Test calling DEFLATE on a 78k PDF command stream. Used for measuring |
| 173 | alternate zlib settings, usage, and library versions. */ |
| 174 | class PDFCompressionBench : public Benchmark { |
| 175 | public: |
| 176 | PDFCompressionBench() {} |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 177 | ~PDFCompressionBench() override {} |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 178 | |
| 179 | protected: |
| 180 | const char* onGetName() override { return "PDFCompression"; } |
| 181 | bool isSuitableFor(Backend backend) override { |
| 182 | return backend == kNonRendering_Backend; |
| 183 | } |
| 184 | void onDelayedSetup() override { |
Mike Reed | 71f867c | 2017-07-23 13:14:10 -0400 | [diff] [blame] | 185 | fAsset = GetResourceAsStream("pdf_command_stream.txt"); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 186 | } |
| 187 | void onDraw(int loops, SkCanvas*) override { |
| 188 | SkASSERT(fAsset); |
| 189 | if (!fAsset) { return; } |
| 190 | while (loops-- > 0) { |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 191 | sk_sp<SkPDFObject> object = |
| 192 | sk_make_sp<SkPDFSharedStream>( |
| 193 | std::unique_ptr<SkStreamAsset>(fAsset->duplicate())); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 194 | test_pdf_object_serialization(object); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 199 | std::unique_ptr<SkStreamAsset> fAsset; |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 202 | struct PDFColorComponentBench : public Benchmark { |
| 203 | bool isSuitableFor(Backend b) override { |
| 204 | return b == kNonRendering_Backend; |
| 205 | } |
| 206 | const char* onGetName() override { return "PDFColorComponent"; } |
| 207 | void onDraw(int loops, SkCanvas*) override { |
| 208 | char dst[5]; |
| 209 | while (loops-- > 0) { |
| 210 | for (int i = 0; i < 256; ++i) { |
| 211 | (void)SkPDFUtils::ColorToDecimal(SkToU8(i), dst); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | }; |
| 216 | |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 217 | struct PDFShaderBench : public Benchmark { |
| 218 | sk_sp<SkShader> fShader; |
| 219 | const char* onGetName() final { return "PDFShader"; } |
| 220 | bool isSuitableFor(Backend b) final { return b == kNonRendering_Backend; } |
| 221 | void onDelayedSetup() final { |
| 222 | const SkPoint pts[2] = {{0.0f, 0.0f}, {100.0f, 100.0f}}; |
| 223 | const SkColor colors[] = { |
| 224 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, |
| 225 | SK_ColorWHITE, SK_ColorBLACK, |
| 226 | }; |
| 227 | fShader = SkGradientShader::MakeLinear( |
| 228 | pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 229 | SkShader::kClamp_TileMode); |
| 230 | } |
| 231 | void onDraw(int loops, SkCanvas*) final { |
| 232 | SkASSERT(fShader); |
| 233 | while (loops-- > 0) { |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 234 | SkNullWStream nullStream; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 235 | SkPDFDocument doc(&nullStream, SkPDF::Metadata()); |
Hal Canary | 7e872ca | 2017-07-19 15:51:18 -0400 | [diff] [blame] | 236 | sk_sp<SkPDFObject> shader = SkPDFMakeShader(&doc, fShader.get(), SkMatrix::I(), |
| 237 | {0, 0, 400, 400}, SK_ColorBLACK); |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | }; |
| 241 | |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 242 | struct WritePDFTextBenchmark : public Benchmark { |
| 243 | std::unique_ptr<SkWStream> fWStream; |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 244 | WritePDFTextBenchmark() : fWStream(new SkNullWStream) {} |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 245 | const char* onGetName() override { return "WritePDFText"; } |
| 246 | bool isSuitableFor(Backend backend) override { |
| 247 | return backend == kNonRendering_Backend; |
| 248 | } |
| 249 | void onDraw(int loops, SkCanvas*) override { |
| 250 | static const char kHello[] = "HELLO SKIA!\n"; |
| 251 | static const char kBinary[] = "\001\002\003\004\005\006"; |
| 252 | while (loops-- > 0) { |
| 253 | for (int i = 1000; i-- > 0;) { |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 254 | SkPDFWriteString(fWStream.get(), kHello, strlen(kHello)); |
| 255 | SkPDFWriteString(fWStream.get(), kBinary, strlen(kBinary)); |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | }; |
| 260 | |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 261 | } // namespace |
| 262 | DEF_BENCH(return new PDFImageBench;) |
| 263 | DEF_BENCH(return new PDFJpegImageBench;) |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 264 | DEF_BENCH(return new PDFCompressionBench;) |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 265 | DEF_BENCH(return new PDFColorComponentBench;) |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 266 | DEF_BENCH(return new PDFShaderBench;) |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 267 | DEF_BENCH(return new WritePDFTextBenchmark;) |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 268 | |
| 269 | #endif |
| 270 | |