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" |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 19 | |
| 20 | namespace { |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 21 | struct WStreamWriteTextBenchmark : public Benchmark { |
| 22 | std::unique_ptr<SkWStream> fWStream; |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 23 | WStreamWriteTextBenchmark() : fWStream(new SkNullWStream) {} |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 24 | 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 | |
| 38 | DEF_BENCH(return new WStreamWriteTextBenchmark;) |
| 39 | |
Hal Canary | 3c36ef6 | 2018-01-02 16:25:53 -0500 | [diff] [blame] | 40 | // Test speed of SkFloatToDecimal for typical floats that |
| 41 | // might be found in a PDF document. |
| 42 | struct 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 | |
| 57 | DEF_BENCH(return new PDFScalarBench;) |
| 58 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 59 | #ifdef SK_SUPPORT_PDF |
| 60 | |
| 61 | #include "SkPDFBitmap.h" |
| 62 | #include "SkPDFDocument.h" |
| 63 | #include "SkPDFShader.h" |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 64 | |
| 65 | namespace { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 66 | static void test_pdf_object_serialization(const sk_sp<SkPDFObject> object) { |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 67 | // SkDebugWStream wStream; |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 68 | SkNullWStream wStream; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 69 | SkPDFObjNumMap objNumMap; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 70 | objNumMap.addObjectRecursively(object.get()); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 71 | for (int i = 0; i < objNumMap.objects().count(); ++i) { |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 72 | SkPDFObject* object = objNumMap.objects()[i].get(); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 73 | wStream.writeDecAsText(i + 1); |
| 74 | wStream.writeText(" 0 obj\n"); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 75 | object->emitObject(&wStream, objNumMap); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 76 | wStream.writeText("\nendobj\n"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | class PDFImageBench : public Benchmark { |
| 81 | public: |
| 82 | PDFImageBench() {} |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 83 | ~PDFImageBench() override {} |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 84 | |
| 85 | protected: |
| 86 | const char* onGetName() override { return "PDFImage"; } |
| 87 | bool isSuitableFor(Backend backend) override { |
| 88 | return backend == kNonRendering_Backend; |
| 89 | } |
| 90 | void onDelayedSetup() override { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 91 | sk_sp<SkImage> img(GetResourceAsImage("images/color_wheel.png")); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 92 | 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)) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 97 | fImage = SkImage::MakeRasterCopy(pixmap); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | void onDraw(int loops, SkCanvas*) override { |
| 102 | if (!fImage) { |
| 103 | return; |
| 104 | } |
| 105 | while (loops-- > 0) { |
Mike Reed | 2e3f2e9 | 2017-12-15 20:43:03 +0000 | [diff] [blame] | 106 | auto object = SkPDFCreateBitmapObject(fImage); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 107 | SkASSERT(object); |
| 108 | if (!object) { |
| 109 | return; |
| 110 | } |
| 111 | test_pdf_object_serialization(object); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 116 | sk_sp<SkImage> fImage; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | class PDFJpegImageBench : public Benchmark { |
| 120 | public: |
| 121 | PDFJpegImageBench() {} |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 122 | ~PDFJpegImageBench() override {} |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 123 | |
| 124 | protected: |
| 125 | const char* onGetName() override { return "PDFJpegImage"; } |
| 126 | bool isSuitableFor(Backend backend) override { |
| 127 | return backend == kNonRendering_Backend; |
| 128 | } |
| 129 | void onDelayedSetup() override { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 130 | sk_sp<SkImage> img(GetResourceAsImage("images/mandrill_512_q075.jpg")); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 131 | if (!img) { return; } |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 132 | sk_sp<SkData> encoded = img->refEncodedData(); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 133 | SkASSERT(encoded); |
| 134 | if (!encoded) { return; } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 135 | fImage = img; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 136 | } |
| 137 | void onDraw(int loops, SkCanvas*) override { |
| 138 | if (!fImage) { |
| 139 | SkDEBUGFAIL(""); |
| 140 | return; |
| 141 | } |
| 142 | while (loops-- > 0) { |
Mike Reed | 2e3f2e9 | 2017-12-15 20:43:03 +0000 | [diff] [blame] | 143 | auto object = SkPDFCreateBitmapObject(fImage); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 144 | SkASSERT(object); |
| 145 | if (!object) { |
| 146 | return; |
| 147 | } |
| 148 | test_pdf_object_serialization(object); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 153 | sk_sp<SkImage> fImage; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 156 | /** Test calling DEFLATE on a 78k PDF command stream. Used for measuring |
| 157 | alternate zlib settings, usage, and library versions. */ |
| 158 | class PDFCompressionBench : public Benchmark { |
| 159 | public: |
| 160 | PDFCompressionBench() {} |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 161 | ~PDFCompressionBench() override {} |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 162 | |
| 163 | protected: |
| 164 | const char* onGetName() override { return "PDFCompression"; } |
| 165 | bool isSuitableFor(Backend backend) override { |
| 166 | return backend == kNonRendering_Backend; |
| 167 | } |
| 168 | void onDelayedSetup() override { |
Mike Reed | 71f867c | 2017-07-23 13:14:10 -0400 | [diff] [blame] | 169 | fAsset = GetResourceAsStream("pdf_command_stream.txt"); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 170 | } |
| 171 | void onDraw(int loops, SkCanvas*) override { |
| 172 | SkASSERT(fAsset); |
| 173 | if (!fAsset) { return; } |
| 174 | while (loops-- > 0) { |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 175 | sk_sp<SkPDFObject> object = |
| 176 | sk_make_sp<SkPDFSharedStream>( |
| 177 | std::unique_ptr<SkStreamAsset>(fAsset->duplicate())); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 178 | test_pdf_object_serialization(object); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 183 | std::unique_ptr<SkStreamAsset> fAsset; |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 186 | struct 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 | |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 201 | struct 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 Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 218 | SkNullWStream nullStream; |
Hal Canary | c5980d0 | 2018-01-08 15:02:36 -0500 | [diff] [blame] | 219 | SkPDFDocument doc(&nullStream, SkDocument::PDFMetadata()); |
Hal Canary | 7e872ca | 2017-07-19 15:51:18 -0400 | [diff] [blame] | 220 | sk_sp<SkPDFObject> shader = SkPDFMakeShader(&doc, fShader.get(), SkMatrix::I(), |
| 221 | {0, 0, 400, 400}, SK_ColorBLACK); |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | }; |
| 225 | |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 226 | struct WritePDFTextBenchmark : public Benchmark { |
| 227 | std::unique_ptr<SkWStream> fWStream; |
Mike Reed | 2a65cc0 | 2017-03-22 10:01:53 -0400 | [diff] [blame] | 228 | WritePDFTextBenchmark() : fWStream(new SkNullWStream) {} |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 229 | 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 | |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 245 | } // namespace |
| 246 | DEF_BENCH(return new PDFImageBench;) |
| 247 | DEF_BENCH(return new PDFJpegImageBench;) |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 248 | DEF_BENCH(return new PDFCompressionBench;) |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 249 | DEF_BENCH(return new PDFColorComponentBench;) |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 250 | DEF_BENCH(return new PDFShaderBench;) |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 251 | DEF_BENCH(return new WritePDFTextBenchmark;) |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 252 | |
| 253 | #endif |
| 254 | |