[JDK-8185862](https://bugs.openjdk.org/browse/JDK-8185862) introduced several behavioral changes:
1. `CountMonitors()` started counting only those `HMONITOR` from `EnumDisplayMonitors` for which `CreateDC` was successful. 2. A new state `0 valid monitors` appeared, which did not exist before. 3. In `Devices::GetDevice`, the lower bound check was insufficient (`index < 0` instead of `index <= 0`, this was not introduced by [JDK-8185862](https://bugs.openjdk.org/browse/JDK-8185862) though). Thus, we could attempt to read the first element of a zero-length array, which caused a crash in `sun.awt.Win32GraphicsDevice.initNativeScale`. 3. Invalidation only occurres for devices whose indices were removed from the end of the array. In RDP scenarios, a device may be replaced at the same index without changing the total number of screens, which bypassed invalidation. --- Changes in this fix: 1. Combined `CountMonitors()` and `CollectMonitors()`. Monitors are now collected into a dynamic array in a single pass. 2. The working list of devices is no longer replaced by an empty one. 3. `displayChanged()` is called only after a successful native update. 5. Added RDP transient states handling (retries by timer and by session connect). 6. `GetDevice()` now returns `NULL` if `numDevices <= 0`. 7. Improved device comparison logic: comparisons are now performed using display data (`dwFlags`, `szDevice`). Changed devices are invalidated, unchanged ones receive transferred Java references to the new native device. --- Testing: The fix seems to be stable over various scenarios of changing display configurations, locally and over RDP. CI testing is also green. --------- - [x] I confirm that I make this contribution in accordance with the [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai). ------------- Commit messages: - 8341382: EXCEPTION_ACCESS_VIOLATION in awt.dll after upgrade to OpenJDK 17.0.12 Changes: https://git.openjdk.org/jdk/pull/31238/files Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=31238&range=00 Issue: https://bugs.openjdk.org/browse/JDK-8341382 Stats: 262 lines in 6 files changed: 186 ins; 17 del; 59 mod Patch: https://git.openjdk.org/jdk/pull/31238.diff Fetch: git fetch https://git.openjdk.org/jdk.git pull/31238/head:pull/31238 PR: https://git.openjdk.org/jdk/pull/31238
