On Wed, 4 Mar 2026 08:07:05 GMT, Lukasz Kostyra <[email protected]> wrote:

>> This change fixes a potential threading issue in RTImage.draw() method in 
>> WebView. The issue has been theorized back in 
>> [JDK-8333374](https://bugs.openjdk.org/browse/JDK-8333374), I have not 
>> managed to figure out a reliable reproducer for it, but the circumstances 
>> for it to happen are there.
>> 
>> Issue can happen in `RTimage.draw()` method. In 99% of cases 
>> `RTImage.draw()` will be called only by QuantumRenderer thread (that is 
>> during regular drawing to a Stage/Scene), however there is also a 
>> possibility we will call `RTImage.draw()` from a different thread, notably 
>> with a `PrinterGraphics` object (or in other words, when printing). Then 
>> there is a chance we will run some of the initial checks in parallel to 
>> QuantumRenderer thread. I found two places that can be affected - the 
>> initial checks at the beginning of the method and the `getTexture()` call 
>> inside the code block responsible for printing.
>> 
>> The first place was fixed by running the checks on the render thread. That 
>> way we can ensure the render thread will not overwrite the `tex` reference 
>> from the render thread while the printing thread reads it. To make this 
>> happen I added `PrismInvoker.callOnRenderThread()` which functions like 
>> `PrismInvoker.runOnRenderThread()` but with a `Callable<>` instead of a 
>> `Runnable` object.
>> 
>> Second place can happen if, for some reason, in between previous checks and 
>> the `getTexture()` call QuantumRenderer will modify or free RTImage's tex 
>> reference (ex. while pruning the Vram pool) - then the `getTexture()` call 
>> will return `null` (introduced as NPE prevention from 
>> [JDK-8333374](https://bugs.openjdk.org/browse/JDK-8333374)) which can cause 
>> an NPE on QuantumRenderer thread. If that happens, we skip the 
>> `readPixels()` call which will print an empty RTImage.
>> 
>> I verified these changes on Windows and Linux with our test suite and with 
>> manual testing via `HelloWebView` and using an old `SimplePrint.java` 
>> reproducer from [JDK-8118415](https://bugs.openjdk.org/browse/JDK-8118415); 
>> both regular drawing and printing work as they did. I found simply loading 
>> `google.com` via both apps works to trigger this draw call multiple times 
>> (`SimplePrint.java` needed to be adjusted slightly to print out the web page 
>> instead of predefined HTML code). Test suite completes successfully (there 
>> were 3 web.test failures on my end but they also happened on master and 
>> refer to other, unrelated parts of javafx.web) and printing works the same 
>> way it used to.
>
> Lukasz Kostyra has updated the pull request incrementally with one additional 
> commit since the last revision:
> 
>   PrismInvoker: Remove redundant type when creating FutureTasks

I am getting assertion messages while printing:


PrinterJob: JavaFX PrinterJob Printer Print to PDF
 Collation = UNCOLLATED
 Copies = 1
 Sides = ONE_SIDED
 JobName = JavaFX Print Job
 Output file = 
 Page ranges = null
 Print color = COLOR
 Print quality = NORMAL
 Print resolution = Feed res=300dpi. Cross Feed res=300dpi.
 Paper source = Paper source : Automatic
 Page layout = Paper=Paper: A4 size=210.0x297.0 MM Orient=PORTRAIT 
leftMargin=54.0 rightMargin=54.0 topMargin=54.0 bottomMargin=54.0
Job Status = NOT_STARTED

*** unexpected PG access
java.lang.Exception: Stack trace
        at java.base/java.lang.Thread.dumpStack(Thread.java:2018)
        at javafx.graphics/javafx.scene.Node.getPeer(Node.java:2700)
        at javafx.graphics/javafx.scene.Node$1.getPeer(Node.java:539)
        at 
javafx.graphics/com.sun.javafx.scene.NodeHelper.getPeer(NodeHelper.java:213)
        at 
javafx.graphics/com.sun.prism.j2d.print.J2DPrinterJob$J2DPageable.printNode(J2DPrinterJob.java:1145)
        at 
javafx.graphics/com.sun.prism.j2d.print.J2DPrinterJob$J2DPageable.print(J2DPrinterJob.java:1138)
        at java.desktop/sun.lwawt.macosx.CPrinterJob$4.run(CPrinterJob.java:878)
        at 
java.desktop/sun.lwawt.macosx.CPrinterJob.printAndGetPageFormatArea(CPrinterJob.java:897)
        at java.desktop/sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)
        at java.desktop/sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:377)
        at 
javafx.graphics/com.sun.prism.j2d.print.J2DPrinterJob$PrintJobRunnable.run(J2DPrinterJob.java:863)
        at java.base/java.lang.Thread.run(Thread.java:1474)


the print job succeeds though.

-------------

PR Comment: https://git.openjdk.org/jfx/pull/2092#issuecomment-4000110890

Reply via email to