MultiThreaded.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (C) 2022 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. /**
  23. * Multi-threaded rendering of all pages in a document to PNG images.
  24. *
  25. * First look at Example.java and make sure you understand it.
  26. *
  27. * MuPDF can also be used in a more complex way; where the MuPDF
  28. * library is called concurrently from multiple threads within a
  29. * single application. The MuPDF JNI library internally uses a set of
  30. * mutexes (critical section objects or pthreads depending on
  31. * platform) to handle locking around operations that would conflict
  32. * if performed concurrently in multiple threads.
  33. *
  34. * The following simple rules should be followed to ensure that
  35. * multi-threaded operations run smoothly:
  36. *
  37. * * The Document can only be accessed by one thread at a time, but
  38. * once DisplayLists are created from that Document, multiple
  39. * threads can operate on those.
  40. *
  41. * * Each Device may only be accessed by one thread at a time.
  42. *
  43. * This example will create one main thread for reading pages from the
  44. * document, and one thread per page for rendering. After rendering
  45. * the main thread will wait for each rendering thread to complete
  46. * before writing that thread's rendered image to a PNG image. There
  47. * is nothing in MuPDF requiring a rendering thread to only render a
  48. * single page, this is just a design decision taken for this example.
  49. *
  50. * To build this example in a source tree:
  51. * make -C platform/java examples
  52. *
  53. * To render all page from a document and output PNGs, run:
  54. * java -classpath build/java/debug -Djava.library.path=build/java/debug \
  55. * example.MultiThreaded document.pdf
  56. *
  57. * Caution! As all pages are rendered simultaneously, please choose a
  58. * file with just a few pages to avoid stressing your machine too
  59. * much. Also you may run in to a limitation on the number of threads
  60. * depending on your environment.
  61. */
  62. package example;
  63. /* Import all MuPDF java classes. */
  64. import com.artifex.mupdf.fitz.*;
  65. class MultiThreaded
  66. {
  67. public static void main(String args[])
  68. {
  69. /* Parse arguments. */
  70. if (args.length < 1)
  71. {
  72. System.err.println("usage: MultiThreaded input-file");
  73. System.err.println("\tinput-file: path of PDF, XPS, CBZ or EPUB document to open");
  74. return;
  75. }
  76. String filename = args[0];
  77. /* Open the document on the main thread.
  78. * You may not reference doc in any other thread.
  79. */
  80. Document doc;
  81. try {
  82. doc = Document.openDocument(filename);
  83. } catch (RuntimeException ex) {
  84. System.err.println("cannot open document: " + ex.getMessage());
  85. return;
  86. }
  87. /* Count the pages on the main thread. */
  88. int pageCount;
  89. try {
  90. pageCount = doc.countPages();
  91. } catch (RuntimeException ex) {
  92. System.err.println("cannot count document pages: " + ex.getMessage());
  93. return;
  94. }
  95. /* Create a thread, an output pixmap and an exception placeholder per page. */
  96. final Thread[] threads = new Thread[pageCount];
  97. final Pixmap[] pixmaps = new Pixmap[pageCount];
  98. final Exception[] exceptions = new Exception[pageCount];
  99. for (int i = 0; i < pageCount; ++i)
  100. {
  101. final int pageNumber = i;
  102. try {
  103. /* Load page and convert it to a display list on
  104. * the main thread. Note that this cannot be done
  105. * in worker threads since doc should only be used
  106. * by the main thread.
  107. */
  108. Page page = doc.loadPage(pageNumber);
  109. /* Determine the bounding box for the page */
  110. final Rect bounds = page.getBounds();
  111. /* Convert the page into a display list. The display
  112. * list can be used by any other thread as it is not
  113. * bound to doc.
  114. */
  115. final DisplayList displayList = page.toDisplayList();
  116. /* Pass display list, page size and destination pixmap to each
  117. * thread for rendering. Also pass page number for debug printing.
  118. * Everything inside run() takes place in each worker thread.
  119. */
  120. threads[pageNumber] = new Thread() {
  121. public void run() {
  122. try {
  123. System.out.println(pageNumber + ": creating pixmap");
  124. /* Create a white destination pixmap with correct dimensions. */
  125. pixmaps[pageNumber] = new Pixmap(ColorSpace.DeviceRGB, bounds);
  126. pixmaps[pageNumber].clear(0xff);
  127. System.out.println(pageNumber + ": rendering display list to pixmap");
  128. /* Run the display list through a DrawDevice which
  129. * will render the requested area of the page to the
  130. * given pixmap.
  131. */
  132. DrawDevice dev = new DrawDevice(pixmaps[pageNumber]);
  133. displayList.run(dev, Matrix.Identity(), bounds, null);
  134. dev.close();
  135. } catch (RuntimeException ex) {
  136. pixmaps[pageNumber] = null;
  137. exceptions[pageNumber] = ex;
  138. }
  139. }
  140. };
  141. threads[pageNumber].start();
  142. } catch (RuntimeException ex) {
  143. System.err.println(pageNumber + ": cannot load page, skipping render: " + ex.getMessage());
  144. exceptions[pageNumber] = ex;
  145. }
  146. }
  147. /* Wait for threads to finish in reverse order. */
  148. System.out.println("joining " + pageCount + " threads");
  149. for (int i = 0; i < pageCount; ++i)
  150. {
  151. if (threads[i] == null) {
  152. System.err.println(i + ": skipping save, page loading failed: " + exceptions[i].toString());
  153. continue;
  154. }
  155. try {
  156. threads[i].join();
  157. } catch (InterruptedException ex) {
  158. System.err.println(i + ": interrupted while waiting for rendering result, skipping all remaining pages: " + ex.getMessage());
  159. break;
  160. }
  161. if (pixmaps[i] == null) {
  162. System.err.println(i + ": skipping save, page rendering failed: " + exceptions[i].toString());
  163. continue;
  164. }
  165. /* Save destination pixmap from each thread to a PNG. */
  166. String pngfilename = String.format("out-%04d.png", i);
  167. System.out.println(i + ": saving rendered pixmap as " + pngfilename);
  168. pixmaps[i].saveAsPNG(pngfilename);
  169. }
  170. System.out.println("finally!");
  171. }
  172. }