2016-02-24 17:25:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Benchmark.h"
|
|
|
|
#include "Resources.h"
|
2016-03-17 13:58:39 +00:00
|
|
|
#include "SkAutoPixmapStorage.h"
|
2016-02-24 17:25:58 +00:00
|
|
|
#include "SkData.h"
|
2016-02-24 23:17:19 +00:00
|
|
|
#include "SkImage.h"
|
2016-02-24 17:25:58 +00:00
|
|
|
#include "SkPDFBitmap.h"
|
2016-03-09 18:49:23 +00:00
|
|
|
#include "SkPDFUtils.h"
|
2016-02-24 23:17:19 +00:00
|
|
|
#include "SkPixmap.h"
|
2016-03-09 18:49:23 +00:00
|
|
|
#include "SkRandom.h"
|
2016-02-24 17:25:58 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct NullWStream : public SkWStream {
|
|
|
|
NullWStream() : fN(0) {}
|
|
|
|
bool write(const void*, size_t n) override { fN += n; return true; }
|
|
|
|
size_t bytesWritten() const override { return fN; }
|
|
|
|
size_t fN;
|
|
|
|
};
|
|
|
|
|
2016-02-24 23:17:19 +00:00
|
|
|
static void test_pdf_object_serialization(SkPDFObject* object) {
|
2016-02-24 17:25:58 +00:00
|
|
|
// SkDebugWStream wStream;
|
|
|
|
NullWStream wStream;
|
|
|
|
SkPDFSubstituteMap substitutes;
|
|
|
|
SkPDFObjNumMap objNumMap;
|
|
|
|
objNumMap.addObjectRecursively(object, substitutes);
|
|
|
|
for (int i = 0; i < objNumMap.objects().count(); ++i) {
|
2016-03-21 17:05:23 +00:00
|
|
|
SkPDFObject* object = objNumMap.objects()[i].get();
|
2016-02-24 17:25:58 +00:00
|
|
|
wStream.writeDecAsText(i + 1);
|
|
|
|
wStream.writeText(" 0 obj\n");
|
|
|
|
object->emitObject(&wStream, objNumMap, substitutes);
|
|
|
|
wStream.writeText("\nendobj\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PDFImageBench : public Benchmark {
|
|
|
|
public:
|
|
|
|
PDFImageBench() {}
|
|
|
|
virtual ~PDFImageBench() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override { return "PDFImage"; }
|
|
|
|
bool isSuitableFor(Backend backend) override {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
void onDelayedSetup() override {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> img(GetResourceAsImage("color_wheel.png"));
|
2016-02-24 17:25:58 +00:00
|
|
|
if (img) {
|
|
|
|
// force decoding, throw away reference to encoded data.
|
|
|
|
SkAutoPixmapStorage pixmap;
|
|
|
|
pixmap.alloc(SkImageInfo::MakeN32Premul(img->dimensions()));
|
|
|
|
if (img->readPixels(pixmap, 0, 0)) {
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = SkImage::MakeRasterCopy(pixmap);
|
2016-02-24 17:25:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
|
|
|
if (!fImage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (loops-- > 0) {
|
2016-02-24 23:17:19 +00:00
|
|
|
SkAutoTUnref<SkPDFObject> object(
|
2016-03-17 17:51:11 +00:00
|
|
|
SkPDFCreateBitmapObject(fImage.get(), nullptr));
|
2016-02-24 23:17:19 +00:00
|
|
|
SkASSERT(object);
|
|
|
|
if (!object) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
test_pdf_object_serialization(object);
|
2016-02-24 17:25:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
2016-02-24 17:25:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class PDFJpegImageBench : public Benchmark {
|
|
|
|
public:
|
|
|
|
PDFJpegImageBench() {}
|
|
|
|
virtual ~PDFJpegImageBench() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override { return "PDFJpegImage"; }
|
|
|
|
bool isSuitableFor(Backend backend) override {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
void onDelayedSetup() override {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> img(GetResourceAsImage("mandrill_512_q075.jpg"));
|
2016-02-24 17:25:58 +00:00
|
|
|
if (!img) { return; }
|
|
|
|
SkAutoTUnref<SkData> encoded(img->refEncoded());
|
|
|
|
SkASSERT(encoded);
|
|
|
|
if (!encoded) { return; }
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = img;
|
2016-02-24 17:25:58 +00:00
|
|
|
}
|
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
|
|
|
if (!fImage) {
|
|
|
|
SkDEBUGFAIL("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (loops-- > 0) {
|
2016-02-24 23:17:19 +00:00
|
|
|
SkAutoTUnref<SkPDFObject> object(
|
2016-03-17 17:51:11 +00:00
|
|
|
SkPDFCreateBitmapObject(fImage.get(), nullptr));
|
2016-02-24 23:17:19 +00:00
|
|
|
SkASSERT(object);
|
|
|
|
if (!object) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
test_pdf_object_serialization(object);
|
2016-02-24 17:25:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
2016-02-24 17:25:58 +00:00
|
|
|
};
|
|
|
|
|
2016-02-24 23:17:19 +00:00
|
|
|
/** Test calling DEFLATE on a 78k PDF command stream. Used for measuring
|
|
|
|
alternate zlib settings, usage, and library versions. */
|
|
|
|
class PDFCompressionBench : public Benchmark {
|
|
|
|
public:
|
|
|
|
PDFCompressionBench() {}
|
|
|
|
virtual ~PDFCompressionBench() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override { return "PDFCompression"; }
|
|
|
|
bool isSuitableFor(Backend backend) override {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
void onDelayedSetup() override {
|
|
|
|
fAsset.reset(GetResourceAsStream("pdf_command_stream.txt"));
|
|
|
|
}
|
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
|
|
|
SkASSERT(fAsset);
|
|
|
|
if (!fAsset) { return; }
|
|
|
|
while (loops-- > 0) {
|
|
|
|
SkAutoTUnref<SkPDFObject> object(
|
|
|
|
new SkPDFSharedStream(fAsset->duplicate()));
|
|
|
|
test_pdf_object_serialization(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkAutoTDelete<SkStreamAsset> fAsset;
|
|
|
|
};
|
|
|
|
|
2016-03-09 18:49:23 +00:00
|
|
|
// Test speed of SkPDFUtils::FloatToDecimal for typical floats that
|
|
|
|
// might be found in a PDF document.
|
|
|
|
struct PDFScalarBench : public Benchmark {
|
|
|
|
bool isSuitableFor(Backend b) override {
|
|
|
|
return b == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
const char* onGetName() override { return "PDFScalar"; }
|
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
|
|
|
SkRandom random;
|
|
|
|
char dst[SkPDFUtils::kMaximumFloatDecimalLength];
|
|
|
|
while (loops-- > 0) {
|
|
|
|
auto f = random.nextRangeF(-500.0f, 1500.0f);
|
|
|
|
(void)SkPDFUtils::FloatToDecimal(f, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-24 17:25:58 +00:00
|
|
|
} // namespace
|
|
|
|
DEF_BENCH(return new PDFImageBench;)
|
|
|
|
DEF_BENCH(return new PDFJpegImageBench;)
|
2016-02-24 23:17:19 +00:00
|
|
|
DEF_BENCH(return new PDFCompressionBench;)
|
2016-03-09 18:49:23 +00:00
|
|
|
DEF_BENCH(return new PDFScalarBench;)
|