diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2015-04-08 12:24:14 +0200 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2015-04-08 11:29:56 +0100 |
commit | 294b5b63c568cfb2f4a56fb51e27e218c6ce89b8 (patch) | |
tree | 204cdaf2f2fd3b2c6b44939f9485f7036038acae /docs/multi-threaded.c | |
parent | 7c3520a0849a51ec6e1c754c5a295dc981eea339 (diff) | |
download | mupdf-294b5b63c568cfb2f4a56fb51e27e218c6ce89b8.tar.xz |
Add docs/example.c and docs/multi-threaded.c to 'make examples' target.
Also update the examples for the recent API changes and make them build
under MSVC.
Diffstat (limited to 'docs/multi-threaded.c')
-rw-r--r-- | docs/multi-threaded.c | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/docs/multi-threaded.c b/docs/multi-threaded.c index c5df1f4e..f0432b07 100644 --- a/docs/multi-threaded.c +++ b/docs/multi-threaded.c @@ -82,6 +82,7 @@ renderer(void *data) fz_display_list *list = ((struct data *) data)->list; fz_rect bbox = ((struct data *) data)->bbox; fz_pixmap *pix = ((struct data *) data)->pix; + fz_device *dev; fprintf(stderr, "thread at page %d loading!\n", pagenumber); @@ -95,13 +96,13 @@ renderer(void *data) // will render the request area of the page to the pixmap. fprintf(stderr, "thread at page %d rendering!\n", pagenumber); - fz_device *dev = fz_new_draw_device(ctx, pix); - fz_run_display_list(list, dev, &fz_identity, &bbox, NULL); - fz_free_device(dev); + dev = fz_new_draw_device(ctx, pix); + fz_run_display_list(ctx, list, dev, &fz_identity, &bbox, NULL); + fz_drop_device(ctx, dev); // This threads context is freed. - fz_free_context(ctx); + fz_drop_context(ctx); fprintf(stderr, "thread at page %d done!\n", pagenumber); @@ -135,6 +136,9 @@ int main(int argc, char **argv) pthread_t *thread = NULL; fz_locks_context locks; pthread_mutex_t mutex[FZ_LOCK_MAX]; + fz_context *ctx; + fz_document *doc; + int threads; int i; // Initialize FZ_LOCK_MAX number of non-recursive mutexes. @@ -159,7 +163,7 @@ int main(int argc, char **argv) // locking structure. This context will be used to parse all // the pages from the document. - fz_context *ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED); + ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED); // Register default file types. @@ -168,58 +172,64 @@ int main(int argc, char **argv) // Open the PDF, XPS or CBZ document. Note, this binds doc to ctx. // You must only ever use doc with ctx - never a clone of it! - fz_document *doc = fz_open_document(ctx, filename); + doc = fz_open_document(ctx, filename); // Retrieve the number of pages, which translates to the // number of threads used for rendering pages. - int threads = fz_count_pages(doc); + threads = fz_count_pages(ctx, doc); fprintf(stderr, "spawning %d threads, one per page...\n", threads); thread = malloc(threads * sizeof (pthread_t)); for (i = 0; i < threads; i++) { + fz_page *page; + fz_rect bbox; + fz_irect rbox; + fz_display_list *list; + fz_device *dev; + fz_pixmap *pix; + struct data *data; + // Load the relevant page for each thread. Note, that this // cannot be done on the worker threads, as each use of doc // uses ctx, and only one thread can be using ctx at a time. - fz_page *page = fz_load_page(doc, i); + page = fz_load_page(ctx, doc, i); // Compute the bounding box for each page. - fz_rect bbox; - fz_irect rbox; - fz_bound_page(doc, page, &bbox); + fz_bound_page(ctx, page, &bbox); // Create a display list that will hold the drawing // commands for the page. Once we have the display list // this can safely be used on any other thread as it is // not bound to a given context. - fz_display_list *list = fz_new_display_list(ctx); + list = fz_new_display_list(ctx); // Run the loaded page through a display list device // to populate the page's display list. - fz_device *dev = fz_new_list_device(ctx, list); - fz_run_page(doc, page, dev, &fz_identity, NULL); - fz_free_device(dev); + dev = fz_new_list_device(ctx, list); + fz_run_page(ctx, page, dev, &fz_identity, NULL); + fz_drop_device(ctx, dev); // The page is no longer needed, all drawing commands // are now in the display list. - fz_free_page(doc, page); + fz_drop_page(ctx, page); // Create a white pixmap using the correct dimensions. - fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_round_rect(&rbox, &bbox)); + pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_round_rect(&rbox, &bbox)); fz_clear_pixmap_with_value(ctx, pix, 0xff); // Populate the data structure to be sent to the // rendering thread for this page. - struct data *data = malloc(sizeof (struct data)); + data = malloc(sizeof (struct data)); data->pagenumber = i + 1; data->ctx = ctx; @@ -272,8 +282,8 @@ int main(int argc, char **argv) // Finally the document is closed and the main thread's // context is freed. - fz_drop_document(doc); - fz_free_context(ctx); + fz_drop_document(ctx, doc); + fz_drop_context(ctx); return 0; } |