MultiThreadedWithPool.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 using a thread pool.
  24. *
  25. * First look at MultiThreaded.java and make sure you understand it.
  26. * The caution at the top of the file mentions that creating one thread
  27. * per page in a document with many pages may create performance issues.
  28. *
  29. * The code below both renders pages to pixmaps in a Callable task scheduled on
  30. * a thread pool provided by ExecutorService. The number of threads in the
  31. * thread pool is limited to four here as an example.
  32. *
  33. * To build this example in a source tree:
  34. * make -C platform/java examples
  35. *
  36. * To render all page from a document and output PNGs, run:
  37. * java -classpath build/java/debug -Djava.library.path=build/java/debug \
  38. * example.MultiThreadedWithPool document.pdf
  39. */
  40. package example;
  41. /* Import classes for scheduling on a thread pool. */
  42. import java.util.*;
  43. import java.util.concurrent.*;
  44. /* Import all MuPDF java classes. */
  45. import com.artifex.mupdf.fitz.*;
  46. class MultiThreadedWithPool
  47. {
  48. public static void main(String args[])
  49. {
  50. /* Parse arguments. */
  51. if (args.length < 1)
  52. {
  53. System.err.println("usage: MultiThreadedWithPool input-file");
  54. System.err.println("\tinput-file: path of PDF, XPS, CBZ or EPUB document to open");
  55. return;
  56. }
  57. /* Open the document and count its pages on the main thread. */
  58. String filename = args[0];
  59. Document doc;
  60. int pageCount;
  61. try {
  62. doc = Document.openDocument(filename);
  63. } catch (RuntimeException ex) {
  64. System.err.println("cannot open document: " + ex.getMessage());
  65. return;
  66. }
  67. try {
  68. pageCount = doc.countPages();
  69. } catch (RuntimeException ex) {
  70. System.err.println("cannot count document pages: " + ex.getMessage());
  71. return;
  72. }
  73. /* Create an ExecutorService with a thread pool of 4 threads. */
  74. ExecutorService executor = Executors.newFixedThreadPool(4);
  75. /* A list holding futures for the rendered images from each page. */
  76. List renderingFutures = new LinkedList();
  77. for (int i = 0; i < pageCount; ++i) {
  78. final int pageNumber = i;
  79. try {
  80. Page page = doc.loadPage(pageNumber);
  81. final Rect bounds = page.getBounds();
  82. final DisplayList displayList = page.toDisplayList();
  83. /* Append this callable to the list of page rendering futures above.
  84. * It may not be scheduled to run by the executor until later when
  85. * asking for the resulting pixmap.
  86. */
  87. renderingFutures.add(executor.submit(new Callable<Pixmap>() {
  88. public Pixmap call() {
  89. System.out.println(pageNumber + ": creating pixmap");
  90. /* Create a white destination pixmap with correct dimensions. */
  91. Pixmap pixmap = new Pixmap(ColorSpace.DeviceRGB, bounds);
  92. pixmap.clear(0xff);
  93. System.out.println(pageNumber + ": rendering display list to pixmap");
  94. /* Run the display list through a DrawDevice which
  95. * will render the requested area of the page to the
  96. * given pixmap.
  97. */
  98. DrawDevice dev = new DrawDevice(pixmap);
  99. displayList.run(dev, Matrix.Identity(), bounds, null);
  100. dev.close();
  101. /* Return the rendered pixmap to the future. */
  102. return pixmap;
  103. }
  104. }));
  105. } catch (RuntimeException ex) {
  106. System.err.println(pageNumber + ": cannot load page, skipping render: " + ex.getMessage());
  107. renderingFutures.add(ex);
  108. }
  109. }
  110. /* Get the resulting pixmap from each page rendering future. */
  111. System.out.println("awaiting " + pageCount + " futures");
  112. for (int i = 0; i < pageCount; ++i) {
  113. if (renderingFutures.get(i) instanceof Exception) {
  114. Exception ex = (Exception) renderingFutures.get(i);
  115. System.err.println(i + ": skipping save, page loading failed: " + ex.toString());
  116. continue;
  117. }
  118. Future<Pixmap> future = (Future<Pixmap>) renderingFutures.get(i);
  119. if (future == null) {
  120. System.err.println(i + ": skipping save, page loading failed");
  121. continue;
  122. }
  123. Pixmap pixmap;
  124. try {
  125. pixmap = future.get();
  126. } catch (InterruptedException ex) {
  127. System.err.println(i + ": interrupted while waiting for rendering result, skipping all remaining pages: " + ex.getMessage());
  128. break;
  129. } catch (ExecutionException ex) {
  130. System.err.println(i + ": skipping save, page rendering failed: " + ex.getMessage());
  131. continue;
  132. }
  133. /* Save destination pixmap to a PNG. */
  134. String pngfilename = String.format("out-%04d.png", i);
  135. System.out.println(i + ": saving rendered pixmap as " + pngfilename);
  136. pixmap.saveAsPNG(pngfilename);
  137. }
  138. /* Stop all thread pool threads. */
  139. executor.shutdown();
  140. System.out.println("finally!");
  141. }
  142. }