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" |
| 9 | #include "Resources.h" |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 10 | #include "SkAutoPixmapStorage.h" |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 11 | #include "SkData.h" |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 12 | #include "SkGradientShader.h" |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 13 | #include "SkImage.h" |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 14 | #include "SkPDFBitmap.h" |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 15 | #include "SkPDFDocument.h" |
| 16 | #include "SkPDFShader.h" |
halcanary | cf43013 | 2016-03-09 10:49:23 -0800 | [diff] [blame] | 17 | #include "SkPDFUtils.h" |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 18 | #include "SkPixmap.h" |
halcanary | cf43013 | 2016-03-09 10:49:23 -0800 | [diff] [blame] | 19 | #include "SkRandom.h" |
halcanary | cbc060a | 2016-04-11 19:41:48 -0700 | [diff] [blame] | 20 | #include "SkStream.h" |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | struct NullWStream : public SkWStream { |
| 24 | NullWStream() : fN(0) {} |
| 25 | bool write(const void*, size_t n) override { fN += n; return true; } |
| 26 | size_t bytesWritten() const override { return fN; } |
| 27 | size_t fN; |
| 28 | }; |
| 29 | |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 30 | static void test_pdf_object_serialization(const sk_sp<SkPDFObject> object) { |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 31 | // SkDebugWStream wStream; |
| 32 | NullWStream wStream; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 33 | SkPDFObjNumMap objNumMap; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 34 | objNumMap.addObjectRecursively(object.get()); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 35 | for (int i = 0; i < objNumMap.objects().count(); ++i) { |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 36 | SkPDFObject* object = objNumMap.objects()[i].get(); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 37 | wStream.writeDecAsText(i + 1); |
| 38 | wStream.writeText(" 0 obj\n"); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 39 | object->emitObject(&wStream, objNumMap); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 40 | wStream.writeText("\nendobj\n"); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | class PDFImageBench : public Benchmark { |
| 45 | public: |
| 46 | PDFImageBench() {} |
| 47 | virtual ~PDFImageBench() {} |
| 48 | |
| 49 | protected: |
| 50 | const char* onGetName() override { return "PDFImage"; } |
| 51 | bool isSuitableFor(Backend backend) override { |
| 52 | return backend == kNonRendering_Backend; |
| 53 | } |
| 54 | void onDelayedSetup() override { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 55 | sk_sp<SkImage> img(GetResourceAsImage("color_wheel.png")); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 56 | if (img) { |
| 57 | // force decoding, throw away reference to encoded data. |
| 58 | SkAutoPixmapStorage pixmap; |
| 59 | pixmap.alloc(SkImageInfo::MakeN32Premul(img->dimensions())); |
| 60 | if (img->readPixels(pixmap, 0, 0)) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 61 | fImage = SkImage::MakeRasterCopy(pixmap); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | void onDraw(int loops, SkCanvas*) override { |
| 66 | if (!fImage) { |
| 67 | return; |
| 68 | } |
| 69 | while (loops-- > 0) { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 70 | auto object = SkPDFCreateBitmapObject(fImage, nullptr); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 71 | SkASSERT(object); |
| 72 | if (!object) { |
| 73 | return; |
| 74 | } |
| 75 | test_pdf_object_serialization(object); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 80 | sk_sp<SkImage> fImage; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | class PDFJpegImageBench : public Benchmark { |
| 84 | public: |
| 85 | PDFJpegImageBench() {} |
| 86 | virtual ~PDFJpegImageBench() {} |
| 87 | |
| 88 | protected: |
| 89 | const char* onGetName() override { return "PDFJpegImage"; } |
| 90 | bool isSuitableFor(Backend backend) override { |
| 91 | return backend == kNonRendering_Backend; |
| 92 | } |
| 93 | void onDelayedSetup() override { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 94 | sk_sp<SkImage> img(GetResourceAsImage("mandrill_512_q075.jpg")); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 95 | if (!img) { return; } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 96 | sk_sp<SkData> encoded(img->refEncoded()); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 97 | SkASSERT(encoded); |
| 98 | if (!encoded) { return; } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 99 | fImage = img; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 100 | } |
| 101 | void onDraw(int loops, SkCanvas*) override { |
| 102 | if (!fImage) { |
| 103 | SkDEBUGFAIL(""); |
| 104 | return; |
| 105 | } |
| 106 | while (loops-- > 0) { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 107 | auto object = SkPDFCreateBitmapObject(fImage, nullptr); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 108 | SkASSERT(object); |
| 109 | if (!object) { |
| 110 | return; |
| 111 | } |
| 112 | test_pdf_object_serialization(object); |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 117 | sk_sp<SkImage> fImage; |
halcanary | 1abea46 | 2016-02-24 09:25:58 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 120 | /** Test calling DEFLATE on a 78k PDF command stream. Used for measuring |
| 121 | alternate zlib settings, usage, and library versions. */ |
| 122 | class PDFCompressionBench : public Benchmark { |
| 123 | public: |
| 124 | PDFCompressionBench() {} |
| 125 | virtual ~PDFCompressionBench() {} |
| 126 | |
| 127 | protected: |
| 128 | const char* onGetName() override { return "PDFCompression"; } |
| 129 | bool isSuitableFor(Backend backend) override { |
| 130 | return backend == kNonRendering_Backend; |
| 131 | } |
| 132 | void onDelayedSetup() override { |
| 133 | fAsset.reset(GetResourceAsStream("pdf_command_stream.txt")); |
| 134 | } |
| 135 | void onDraw(int loops, SkCanvas*) override { |
| 136 | SkASSERT(fAsset); |
| 137 | if (!fAsset) { return; } |
| 138 | while (loops-- > 0) { |
halcanary | fa25106 | 2016-07-29 10:13:18 -0700 | [diff] [blame] | 139 | sk_sp<SkPDFObject> object = |
| 140 | sk_make_sp<SkPDFSharedStream>( |
| 141 | std::unique_ptr<SkStreamAsset>(fAsset->duplicate())); |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 142 | test_pdf_object_serialization(object); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 147 | std::unique_ptr<SkStreamAsset> fAsset; |
halcanary | 1e44051 | 2016-02-24 15:17:19 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
halcanary | cf43013 | 2016-03-09 10:49:23 -0800 | [diff] [blame] | 150 | // Test speed of SkPDFUtils::FloatToDecimal for typical floats that |
| 151 | // might be found in a PDF document. |
| 152 | struct PDFScalarBench : public Benchmark { |
| 153 | bool isSuitableFor(Backend b) override { |
| 154 | return b == kNonRendering_Backend; |
| 155 | } |
| 156 | const char* onGetName() override { return "PDFScalar"; } |
| 157 | void onDraw(int loops, SkCanvas*) override { |
| 158 | SkRandom random; |
| 159 | char dst[SkPDFUtils::kMaximumFloatDecimalLength]; |
| 160 | while (loops-- > 0) { |
| 161 | auto f = random.nextRangeF(-500.0f, 1500.0f); |
| 162 | (void)SkPDFUtils::FloatToDecimal(f, dst); |
| 163 | } |
| 164 | } |
| 165 | }; |
| 166 | |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 167 | struct PDFColorComponentBench : public Benchmark { |
| 168 | bool isSuitableFor(Backend b) override { |
| 169 | return b == kNonRendering_Backend; |
| 170 | } |
| 171 | const char* onGetName() override { return "PDFColorComponent"; } |
| 172 | void onDraw(int loops, SkCanvas*) override { |
| 173 | char dst[5]; |
| 174 | while (loops-- > 0) { |
| 175 | for (int i = 0; i < 256; ++i) { |
| 176 | (void)SkPDFUtils::ColorToDecimal(SkToU8(i), dst); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | }; |
| 181 | |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 182 | struct PDFShaderBench : public Benchmark { |
| 183 | sk_sp<SkShader> fShader; |
| 184 | const char* onGetName() final { return "PDFShader"; } |
| 185 | bool isSuitableFor(Backend b) final { return b == kNonRendering_Backend; } |
| 186 | void onDelayedSetup() final { |
| 187 | const SkPoint pts[2] = {{0.0f, 0.0f}, {100.0f, 100.0f}}; |
| 188 | const SkColor colors[] = { |
| 189 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, |
| 190 | SK_ColorWHITE, SK_ColorBLACK, |
| 191 | }; |
| 192 | fShader = SkGradientShader::MakeLinear( |
| 193 | pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 194 | SkShader::kClamp_TileMode); |
| 195 | } |
| 196 | void onDraw(int loops, SkCanvas*) final { |
| 197 | SkASSERT(fShader); |
| 198 | while (loops-- > 0) { |
| 199 | NullWStream nullStream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 200 | SkPDFDocument doc(&nullStream, nullptr, 72, |
| 201 | SkDocument::PDFMetadata(), nullptr, false); |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 202 | sk_sp<SkPDFObject> shader( |
| 203 | SkPDFShader::GetPDFShader( |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 204 | &doc, 72, fShader.get(), SkMatrix::I(), |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 205 | SkIRect::MakeWH(400,400), 72)); |
| 206 | } |
| 207 | } |
| 208 | }; |
| 209 | |
halcanary | cbc060a | 2016-04-11 19:41:48 -0700 | [diff] [blame] | 210 | struct WStreamWriteTextBenchmark : public Benchmark { |
| 211 | std::unique_ptr<SkWStream> fWStream; |
| 212 | WStreamWriteTextBenchmark() : fWStream(new NullWStream) {} |
| 213 | const char* onGetName() override { return "WStreamWriteText"; } |
| 214 | bool isSuitableFor(Backend backend) override { |
| 215 | return backend == kNonRendering_Backend; |
| 216 | } |
| 217 | void onDraw(int loops, SkCanvas*) override { |
| 218 | while (loops-- > 0) { |
| 219 | for (int i = 1000; i-- > 0;) { |
| 220 | fWStream->writeText("HELLO SKIA!\n"); |
| 221 | } |
| 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; |
| 228 | WritePDFTextBenchmark() : fWStream(new NullWStream) {} |
| 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 | cf43013 | 2016-03-09 10:49:23 -0800 | [diff] [blame] | 249 | DEF_BENCH(return new PDFScalarBench;) |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 250 | DEF_BENCH(return new PDFColorComponentBench;) |
halcanary | d11c726 | 2016-03-25 05:52:57 -0700 | [diff] [blame] | 251 | DEF_BENCH(return new PDFShaderBench;) |
halcanary | cbc060a | 2016-04-11 19:41:48 -0700 | [diff] [blame] | 252 | DEF_BENCH(return new WStreamWriteTextBenchmark;) |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 253 | DEF_BENCH(return new WritePDFTextBenchmark;) |