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