On Wed, 7 Jun 2023 10:36:19 GMT, Alexander Zvegintsev <[email protected]> 
wrote:

>> Modern Linux systems often come with 
>> [Wayland](https://wayland.freedesktop.org/) by default.
>> This comes with some difficulties, and one of them is the inability to get 
>> screenshots from the system.
>> This is because we now use the [X Window System 
>> API](https://en.wikipedia.org/wiki/X_Window_System) to capture screenshots 
>> and it cannot access data outside the [XWayland 
>> server](https://wayland.freedesktop.org/xserver.html) 
>> 
>> But this functionality is a very important part of automated testing.
>> 
>> 
>> At the moment there are two obvious solutions to this problem, and both use 
>> [xdg-desktop-portal](https://github.com/flatpak/xdg-desktop-portal):
>> 
>> 1. [org.freedesktop.portal.Screenshot DBUS 
>> API](https://flatpak.github.io/xdg-desktop-portal/#gdbus-org.freedesktop.portal.Screenshot)
>> It has several drawbacks though:
>> + It saves a screenshot to disk, which must be read and deleted(may add some 
>> delays depending on the type of a disk drive).
>> + There is no way to disable the visual "screen flash" after screenshot
>> + It asks a user confirmation to save a screenshot. This confirmation can be 
>> saved on Gnome 43+. 
>> Since we would like Ubuntu 22.04 LTS which comes with Gnome 42 this option 
>> is not acceptable for us because it would require user confirmation for each 
>> screenshot.
>> But we still can consider this option as a fallback.
>> 
>> 
>> 
>> 2. 
>> [org.freedesktop.portal.ScreenCast](https://flatpak.github.io/xdg-desktop-portal/#gdbus-org.freedesktop.portal.ScreenCast)
>> It typically used by applications that need to capture the contents of the 
>> user's screen or a specific window for the purpose of sharing, recording, or 
>> streaming.
>> This might be a bit of overkill, but it avoids several of the problems 
>> mentioned in the Screenshot API.
>> 
>> + implementation is more complicated comparing to Screenshot API
>> + no intermediate file, screenshot data can be obtained from memory
>> + Permission to make screenshots can be stored with 
>> [`restore_token`](https://flatpak.github.io/xdg-desktop-portal/#gdbus-method-org-freedesktop-portal-ScreenCast.SelectSources)
>> 
>> 
>> So this PR adds the ability to take screenshots using the ScreenCast API. 
>> This functionality is currently disabled by default.
>> 
>> This change also introduces some new behavior for the robot:
>> A system window now appears asking for confirmation from the user to capture 
>> the screen.
>> + The user can refuse the screen capture completely. In this case a security 
>> exception will be thrown.
>> + The user can allow...
>
> Alexander Zvegintsev has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   address review comments

Just a fyi for anyone that stumbles upon this PR trying to figure out how to 
screenshot in Wayland, but finds out that, even when using JDK 21+, the 
screenshots are still black: If you are using KDE Plasma with fractional 
scaling (I use 150%) the capture will always fail with


callbackScreenCastStart:745 available screen count 1
rebuildScreenData:116 
==== screenId#98
rebuildScreenData:161 -----------------------
rebuildScreenData:162 screenId#98
||      bounds         x     0 y     0 w  1707 h   960
||      capture area   x     0 y     0 w     0 h     0 shouldCapture 0

rebuildScreenData:163 #---------------------#

callbackScreenCastStart:751 rebuildScreenData result |0|
callbackScreenCastStart:764 restore_token |5b0f7d56-d05f-483e-a85a-99727b3a36f6|
storeRestoreToken:805 saving token, old: |16521d36-3b86-4b25-b990-319ce54e3283| 
> new: |5b0f7d56-d05f-483e-a85a-99727b3a36f6|
portalScreenCastStart:843 ScreenCastResult |0|
initAndStartSession:1116 portalScreenCastStart result |0|
checkCanCaptureAllRequiredScreens:991 Could not find required screen 0 0 2560 
1440 in allowed bounds
getPipewireFd:1132 The location of the screens has changed, the capture area is 
outside the allowed area.
Java_sun_awt_screencast_ScreencastHelper_getRGBPixelsImpl:1036 Screencast 
attempt failed with -12, re-trying...


The reason is because it keeps trying to find the bounds with the "logical" 
resolution size (the size without any scaling) and it keeps failing because the 
Screencast API gives the scaled resolution size.

Using the default non-scaled resolution fixes the issue, but that's quite 
annoying so I think this should be reported on the bug tracker later.

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

PR Comment: https://git.openjdk.org/jdk/pull/13803#issuecomment-3836935621

Reply via email to