Skip to content

Instantly share code, notes, and snippets.

@rschroll
Created March 3, 2015 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rschroll/71a9cc6feb322b671293 to your computer and use it in GitHub Desktop.
Save rschroll/71a9cc6feb322b671293 to your computer and use it in GitHub Desktop.
PDF rendering timing test
// valac render.vala --pkg gtk+-3.0 --pkg poppler-glib
const int WIDTH = 1920;
const int HEIGHT = 1080;
public int main(string[] args) {
if (args.length != 2)
error("Must specify name of PDF file as argument");
File file = File.new_for_commandline_arg(args[1]);
Poppler.Document document;
try {
document = new Poppler.Document.from_file(file.get_uri(), null);
} catch (Error e) {
error("Could not load document");
}
stdout.printf("\tRender\tPixbuf\tSave\tLoad\tScale\tSurface\tRecord\tPlayback\n");
for (int i = 0; i < document.get_n_pages(); i++) {
Poppler.Page page = document.get_page(i);
double page_width, page_height;
page.get_size(out page_width, out page_height);
double scale = double.min(WIDTH / page_width, HEIGHT / page_height);
int64 start = get_monotonic_time();
Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, WIDTH, HEIGHT);
Cairo.Context cr = new Cairo.Context(surface);
cr.set_source_rgb(255, 255, 255);
cr.rectangle(0, 0, WIDTH, HEIGHT);
cr.fill();
cr.scale(scale, scale);
page.render(cr);
int64 render_pdf = get_monotonic_time();
Gdk.Pixbuf pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, WIDTH, HEIGHT);
int64 paint_pixbuf = get_monotonic_time();
uint8[] buffer;
try {
pixbuf.save_to_buffer(out buffer, "png", "compression", "1", null);
} catch (Error e) {
error("Error saving to PNG");
}
int64 save_png = get_monotonic_time();
Gdk.PixbufLoader loader = new Gdk.PixbufLoader();
try {
loader.write(buffer);
loader.close();
} catch (Error e) {
error("Error reading from PNG");
}
pixbuf = loader.get_pixbuf();
int64 load_png = get_monotonic_time();
pixbuf.scale_simple(WIDTH/2, HEIGHT/2, Gdk.InterpType.BILINEAR);
int64 scale_pixbuf = get_monotonic_time();
Gdk.cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
cr.rectangle(0, 0, WIDTH, HEIGHT);
cr.fill();
int64 render_pixbuf = get_monotonic_time();
Cairo.Rectangle rect = {0, 0, WIDTH, HEIGHT};
Cairo.RecordingSurface record = new Cairo.RecordingSurface(Cairo.Content.COLOR, rect);
Cairo.Context context = new Cairo.Context(record);
context.set_source_rgb(255, 255, 255);
context.rectangle(0, 0, WIDTH, HEIGHT);
context.fill();
context.scale(scale, scale);
page.render(context);
int64 render_record = get_monotonic_time();
context = new Cairo.Context(surface);
context.set_source_surface(record, 0, 0);
context.paint();
int64 paint_record = get_monotonic_time();
stdout.printf("%3i)\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", i, format_time(render_pdf, start),
format_time(paint_pixbuf, render_pdf), format_time(save_png, paint_pixbuf),
format_time(load_png, save_png), format_time(scale_pixbuf, load_png),
format_time(render_pixbuf, scale_pixbuf), format_time(render_record, render_pixbuf),
format_time(paint_record, render_record));
}
return 0;
}
string format_time(int64 stop, int64 start) {
double delta = (double) (stop - start) / 1000;
return "%7.1f".printf(delta);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment