OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "Resources.h" | 9 #include "Resources.h" |
| 10 #include "SkData.h" |
10 #include "SkImage.h" | 11 #include "SkImage.h" |
| 12 #include "SkPDFBitmap.h" |
11 #include "SkPixmap.h" | 13 #include "SkPixmap.h" |
12 #include "SkData.h" | 14 #include "SkRandom.h" |
13 | |
14 #if SK_SUPPORT_PDF | |
15 | |
16 #include "SkPDFBitmap.h" | |
17 | 15 |
18 namespace { | 16 namespace { |
19 struct NullWStream : public SkWStream { | 17 struct NullWStream : public SkWStream { |
20 NullWStream() : fN(0) {} | 18 NullWStream() : fN(0) {} |
21 bool write(const void*, size_t n) override { fN += n; return true; } | 19 bool write(const void*, size_t n) override { fN += n; return true; } |
22 size_t bytesWritten() const override { return fN; } | 20 size_t bytesWritten() const override { return fN; } |
23 size_t fN; | 21 size_t fN; |
24 }; | 22 }; |
25 | 23 |
26 static void test_pdf_image_serialization(SkImage* img) { | 24 static void test_pdf_object_serialization(SkPDFObject* object) { |
27 SkAutoTUnref<SkPDFObject> object( | |
28 SkPDFCreateBitmapObject(img, nullptr)); | |
29 if (!object) { | |
30 SkDEBUGFAIL(""); | |
31 return; | |
32 } | |
33 // SkDebugWStream wStream; | 25 // SkDebugWStream wStream; |
34 NullWStream wStream; | 26 NullWStream wStream; |
35 SkPDFSubstituteMap substitutes; | 27 SkPDFSubstituteMap substitutes; |
36 SkPDFObjNumMap objNumMap; | 28 SkPDFObjNumMap objNumMap; |
37 objNumMap.addObjectRecursively(object, substitutes); | 29 objNumMap.addObjectRecursively(object, substitutes); |
38 for (int i = 0; i < objNumMap.objects().count(); ++i) { | 30 for (int i = 0; i < objNumMap.objects().count(); ++i) { |
39 SkPDFObject* object = objNumMap.objects()[i]; | 31 SkPDFObject* object = objNumMap.objects()[i]; |
40 wStream.writeDecAsText(i + 1); | 32 wStream.writeDecAsText(i + 1); |
41 wStream.writeText(" 0 obj\n"); | 33 wStream.writeText(" 0 obj\n"); |
42 object->emitObject(&wStream, objNumMap, substitutes); | 34 object->emitObject(&wStream, objNumMap, substitutes); |
43 wStream.writeText("\nendobj\n"); | 35 wStream.writeText("\nendobj\n"); |
44 } | 36 } |
45 } | 37 } |
46 | 38 |
| 39 static void test_pdf_image_serialization(SkImage* img) { |
| 40 SkAutoTUnref<SkPDFObject> object( |
| 41 SkPDFCreateBitmapObject(img, nullptr)); |
| 42 if (!object) { |
| 43 SkDEBUGFAIL(""); |
| 44 return; |
| 45 } |
| 46 test_pdf_object_serialization(object); |
| 47 } |
| 48 |
47 class PDFImageBench : public Benchmark { | 49 class PDFImageBench : public Benchmark { |
48 public: | 50 public: |
49 PDFImageBench() {} | 51 PDFImageBench() {} |
50 virtual ~PDFImageBench() {} | 52 virtual ~PDFImageBench() {} |
51 | 53 |
52 protected: | 54 protected: |
53 const char* onGetName() override { return "PDFImage"; } | 55 const char* onGetName() override { return "PDFImage"; } |
54 bool isSuitableFor(Backend backend) override { | 56 bool isSuitableFor(Backend backend) override { |
55 return backend == kNonRendering_Backend; | 57 return backend == kNonRendering_Backend; |
56 } | 58 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 } | 108 } |
107 while (loops-- > 0) { | 109 while (loops-- > 0) { |
108 test_pdf_image_serialization(fImage); | 110 test_pdf_image_serialization(fImage); |
109 } | 111 } |
110 } | 112 } |
111 | 113 |
112 private: | 114 private: |
113 SkAutoTUnref<SkImage> fImage; | 115 SkAutoTUnref<SkImage> fImage; |
114 }; | 116 }; |
115 | 117 |
| 118 static SkAutoTDelete<SkStreamAsset> generate_random_text() { |
| 119 SkRandom random(0x5EED); |
| 120 size_t N = 1 << 16; |
| 121 SkAutoTDelete<SkStreamAsset> buffer(new SkMemoryStream(N)); |
| 122 uint8_t* ptr = (uint8_t*)(buffer->getMemoryBase()); |
| 123 const uint8_t* const end = &ptr[N]; |
| 124 while (ptr != end) { |
| 125 *ptr++ = SkToU8(random.nextRangeU(' ', '~')); |
| 126 } |
| 127 return buffer; |
| 128 } |
| 129 |
| 130 /** Test calling DEFLATE on 64k of random text. Used for measuring |
| 131 alternate zlib settings, usage, and library versions. */ |
| 132 class PDFCompressionBench : public Benchmark { |
| 133 public: |
| 134 PDFCompressionBench() {} |
| 135 virtual ~PDFCompressionBench() {} |
| 136 |
| 137 protected: |
| 138 const char* onGetName() override { return "PDFCompression"; } |
| 139 bool isSuitableFor(Backend backend) override { |
| 140 return backend == kNonRendering_Backend; |
| 141 } |
| 142 void onDelayedSetup() override { |
| 143 fRandomText = generate_random_text(); |
| 144 } |
| 145 void onDraw(int loops, SkCanvas*) override { |
| 146 SkASSERT(fRandomText); |
| 147 while (loops-- > 0) { |
| 148 SkAutoTUnref<SkPDFObject> object( |
| 149 new SkPDFSharedStream(fRandomText->duplicate())); |
| 150 test_pdf_object_serialization(object); |
| 151 } |
| 152 } |
| 153 |
| 154 private: |
| 155 SkAutoTDelete<SkStreamAsset> fRandomText; |
| 156 }; |
| 157 |
116 } // namespace | 158 } // namespace |
117 DEF_BENCH(return new PDFImageBench;) | 159 DEF_BENCH(return new PDFImageBench;) |
118 DEF_BENCH(return new PDFJpegImageBench;) | 160 DEF_BENCH(return new PDFJpegImageBench;) |
119 | 161 DEF_BENCH(return new PDFCompressionBench;) |
120 #endif // SK_SUPPORT_PDF | |
OLD | NEW |