OK, I've investigated this a bit more.
This is an issue with DWM, but it is caused by our code.
Essentially it is caused by doing D3D and GDI rendering to the
same window which is a bad idea on Vista - it's pretty
Unfortunately in some situations Swing allows rendering directly
to the screen - which is what happens in this case.
The JXCollapsiblePane widget does some animation on collasing/expanding.
As part of that animation it apparently first paints the component to
an off-screen image. This triggers a special code path in
The setInSync() indicates to the paint manager that the back-buffer is
no longer in sync with what's on the screen. After this all copyAreas
get redirected to the screen instead of the back-buffer (why only copyAreas?).
It's not clear why doing painting to another offscreen surface
necessarily indicates an out of sync back-buffer though.
Now, the D3D pipeline does special handling for rendering to
the screen - it redirects all rendering to a dedicated back-buffer,
a sort of "fake window", which is presented to the screen when
needed. That way we can still accelerated on-screen rendering using
Direct3D (direct on-screen rendering is not possible with Direct3D 9).
However, this special handling is disabled for windows which have their
own BufferStrategy - which is the case here, since Swing uses BufferStrategy
as a back-buffer when it is in "bufferPerWindow" mode - aka the "gray rect fix",
or "true double-buffering".
Because of that the rendering goes directly to the screen, so the
copy area is performed using GDICopyArea. It works fine XP, but
produces artifacts on Vista with Aero.
Looking at the latest code for JXCollapsiblePane it looks like it
no longer uses this approach for doing their animation, so it could
be that this particular case will no longer trigger the issue, but the
underlying problem is still there of course.
Since all circumstances under which Swing may switch from
buffer per window mode to the "regular" one, or allow on-screen
rendering, are not clear it doesn't seem likely that we could
fix swing's "buffer out of sync" cases.
One solution would be to use the old painting mode - where
Swing's back-buffer is a VolatileImage which is copied to the
screen - in this case the D3D pipeline's on-screen rendering
support will ensure that one never renders directly to the
Since Aero is already double-buffered (which in particular
means that applications don't get 'expose' events when another
window is moved over their window) the 'true double-buffering'
doesn't really give any advantages.
More investigation is needed.
Note: it is possible that this is the cause of other
issues on Vista like the one described here:
It looks especially plausible since disabling buffer per window
also helped there.
The solution we're working on is disabling Swing's
"true double-buffering" mode on Vista with DWM(Aero) enabled.
From the code review:
With buffer per window disabled Swing will use VolatileImage
as back-buffer and that will allow the D3D pipeline
to guarantee that no on-screen rendering will occur since
our mechanism will be working.
Another reason is that since Aero already does double-buffering
there's no point in wasting resources, the application
doesn't get expose events anyway.
This solution is also less risky than
changing the way Swing paints on all platforms.
Notes about the implementation: while the state of DWM
compositing can change during the lifetime of the
application it is an extremely rare event, and is
in most cases temporary (some app turns it off, then
re-enables it). So if it was turned on when we started
we'll detect it and not use buffer per window.
If they disable it during the runtime - no big deal,
everything will look sucky (flashing) anyway and we'll
be no exception.
If we find that we do need to handle runtime changes -
they're easy to handle, just add a listener mechanism
to Win32GraphicsEnv (espose through some shared interface),
and have BufferStrategyPaintManager listen to those
events and disable bpw if needed.