-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8303904: [macos]Transparent Windows Paint Wrong (Opaque, Pixelated) w/o Volatile Buffering #23430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
👋 Welcome back anass-baya! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
@anass-baya The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
test/jdk/javax/swing/JWindow/NonVolatileTransparentWindows/PixelatedImageTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/JWindow/NonVolatileTransparentWindows/TransparentWindowTest.java
Outdated
Show resolved
Hide resolved
float scaleFactor = getDevice().getScaleFactor(); | ||
int fullResWidth = Math.round( scaleFactor * width); | ||
int fullResHeight = Math.round( scaleFactor * height); | ||
WritableRaster wr = model.createCompatibleWritableRaster(fullResWidth, fullResHeight); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this. :)
I have a couple of questions:
- This math looks great for the simple case of a 200% high-res monitor; is the rounding safe for non-integer resolutions?
For ex if width is 33 and scaleFactor is 1.25, then we'll call round(41.25)
, so fullResWidth = 41
.
Will the native windowing system allocate 41px for our Window? (Because if so: great. We have a perfect fit.) Or could it allocate 42px? (In which case we may have a thin border on the right/bottom that becomes unpaintable...)
- I'm assuming if
fullResWidth
/fullResHeight
is ever zero we'll get a RTE. I don't think it's stated, but the preexisting code suggestswidth
andheight
were never zero. Is it possiblescaleFactor
can ever be less than .5? (I've never observed that in my day-to-day life, but I'm just thinking about edge cases and RTE's...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @mickleness ,
Thank you for your review.
It is well known that we can lose a fraction of a pixel. For example in #23183 we allowed a 1-pixel margin of error to compensate for that as the rounding error could accumulate
returnValue.scale(scaleFactor, scaleFactor); | ||
return returnValue; | ||
} | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be outside the scope of this ticket, but I'm seeing similar code in Win32GraphicsConfig#createAcceleratedImage(..), X11GraphicsConfig#createAcceleratedImage(..), and GLXGraphicsConfig#createAcceleratedImage(..) .
Should those be looked at, or is there another ticket to explore those? (That is: are they also going to be opaque and the wrong resolution?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @mickleness,
The problem is not reproduced on windows !! I can see it only on mac
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tldr: Wow / huh. You're right. Thanks for checking.
That code looks so suspicious that I did some digging:
(A. On Windows the TransparentWindowTest says it fails because Image.getTransparency() is OPAQUE, but the test is wrong: the window looks fine.)
B. It works because on Windows we end up using the TranslucentWindowPainter. We never invoke Win32GraphicsConfig#createAcceleratedImage(..)
at all (unless the TransparentWindowTest invokes it directly).
I tested:
A. repainting on a timer (so we'd explicitly go through the RepaintManager), and
B. an ellipse fill instead of a rectangle fill (to double-check that transparent pixels really were transparent).
C. using Window.setShape(..) instead of changing the window's background color to transparent (to make sure it followed the same path)
(... and I don't have access to any other platforms to make sure Linux is doing the right thing.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @mickleness,
Yes, on Windows we use TranslucentWindowPainter.
I moved the PR to draft since I'm trying to simplify the scaling handler logic. I'll come back to you once I'm done with that.
This test is only for macOS. On Windows, it will fail because we don’t explicitly set the Translucent flag.
returnValue.scale(scaleFactor, scaleFactor); | ||
return returnValue; | ||
} | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this new createAcceleratedImage(..)
logic be moved to CGraphicsConfig
?
This looks like copied and pasted code in two subclasses that could be abstracted into the parent class. (I could be wrong here; I just wanted to check opinions...?)
It looks like these two classes (MTLGraphicsConfig and CGLGraphicsConfig) are the only subclasses for CGraphicsConfig.
@anass-baya If your fix is based on @mickleness's previous PR, you should him as co-author using the |
@anass-baya Syntax:
User names can only be used for users in the census associated with this repository. For other contributors you need to supply the full name and email address. |
/contributor add Jeremy mickleness mickleness@gmail.com |
@anass-baya |
@mickleness I wonder what is your preferred email address and name. You were referenced by another email in your recent contribution. |
|
||
Image img = getLWGC().createAcceleratedImage(getTarget(), scaledWidth, scaledHeight); | ||
if(ScaleX == 1 || ScaleY == 1) | ||
return img; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be &&
instead of ||
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes It should
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me (note I'm not an official OpenJDK reviewer). I'm not a fan of the instanceof Graphics2D
check, but any alternative I propose would be more lines of code so I accept this is good enough.
import java.awt.Rectangle; | ||
import java.awt.Toolkit; | ||
import java.awt.Window; | ||
import java.awt.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know in other PRs reviewers have asked to expand wildcard imports. (I haven't studied that topic closely enough to have an informed opinion.)
Image img = getLWGC().createAcceleratedImage(getTarget(), scaledWidth, scaledHeight); | ||
if(ScaleX == 1 || ScaleY == 1) | ||
return img; | ||
return new BaseMultiResolutionImage(img.getScaledInstance(width, height, Image.SCALE_DEFAULT), img) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Unfortunately, even our own code assumes that at least a BufferedImage is returned here.
- The getScaledInstance() method is slow and largely unsupported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, even our own code assumes that at least a BufferedImage is returned here.
Where?
Assuming that's a problem: this expectation sounds (IMO) like something we should bake into the new unit test for this PR (right?)
The getScaledInstance() method is slow and largely unsupported.
Sure. And I don't love using this sort of trick BaseMultiResolutionImage. But I have to admit: there's an elegance to this design that I've come to like.
I agree getScaledInstance should be mostly avoided, but in this case: it looks harmless to me. This code constructs what is mostly a dummy ToolkitImage & FilteredImageSource. We never actually have to produce the scaled pixels (which is the slow/bad part).
In reality I think the Image we return only needs to implement getWidth, getHeight, and getGraphics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know Anass's availability/preferences, but if I better understand the problems we need to resolve here: I might be able to add to this PR (or fork it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where?
Here
When HiDPI support was integrated, these old methods for non-accelerated buffers were not updated, because it is not possible to return a BufferedImage whose bounds bigger than the component itself. And some code assumes that we always return a BufferedImage. We could consider returning a MultiResolution version of the BufferedImage, but it is better try something else before that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree getScaledInstance should be mostly avoided, but in this case: it looks harmless to me. This code constructs what is mostly a dummy ToolkitImage & FilteredImageSource. We never actually have to produce the scaled pixels (which is the slow/bad part).
But why do we need to use the slow ToolkitImage? We can use non-accelerated but still fast BufferedImages instead, we just need to scale the image from one to another without changing its type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof. That is... both helpful and not helpful. (That's not a criticism towards you; it's towards that TODO note.)
I can confirm that the TransparentWindowTest in this PR works. (That is: there's no obvious ClassCastException or anything bad.) But now I'm worried about other code paths that aren't represented in this PR that will break. Does anyone has any theories on what "some stuff will break" in that link refers to? (That is: what should I be testing to see something break?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This replaces my previous response.)
But why do we need to use the slow ToolkitImage?
To use the BaseMultiResolutionImage we needed our first (base) image to be the correct size. That's all we needed that ToolkitImage for: it reported the correct dimensions.
(We do NOT ever want to actually scale any image. So whether we use a ToolkitImage or a BufferedImage: there should be no scaling.)
Here's an alternative approach that wraps a scaled BufferedImage in a MultiResolutionImage:
@Override
public final Image createImage(final int width, final int height) {
GraphicsConfiguration gc = getGraphicsConfiguration();
AffineTransform at = gc.getDefaultTransform();
double ScaleX = at.getScaleX();
double ScaleY = at.getScaleY();
int scaledWidth = Math.round((float)ScaleX * width);
int scaledHeight = Math.round((float)ScaleY * height);
Image img = getLWGC().createAcceleratedImage(getTarget(), scaledWidth, scaledHeight);
if(ScaleX == 1 && ScaleY == 1)
return img;
// This new subclass functions as a scaled BufferedImage. For ex: this
// image may present its perceived size as 100x100px, but it's actually
// backed by a BufferedImage that's 150x150px, or 200x200px.
return new AbstractMultiResolutionImage() {
@Override
public Graphics getGraphics() {
Graphics graphics = img.getGraphics();
if (graphics instanceof Graphics2D)
((Graphics2D) graphics).scale(ScaleX, ScaleY);
return graphics;
}
@Override
public int getWidth(ImageObserver observer) {
return width;
}
@Override
public int getHeight(ImageObserver observer) {
return height;
}
@Override
public Image getBaseImage() {
return img;
}
@Override
public Image getResolutionVariant(double destImageWidth,
double destImageHeight) {
return img;
}
@Override
public List<Image> getResolutionVariants() {
return Arrays.asList(img);
}
};
}
This is longer, but it may be a little clearer what it's trying to do.
(I confirmed this passes the new test case in this PR.)
Does this speak to your question?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @mickleness, @mrserb,
I submitted the PR as a draft due to a few issues.
It's not incorrect to return a BaseMultiResolutionImage, but one of our CI tests DrawImageBilinear.java assumes that createImage() returns something that can be cast to a BufferedImage. So, if we proceed with that change, I’ll need to adjust the test accordingly.
@mickleness , regarding your proposed fix using MultiResolutionImage: I’ve already tried that, and it works fine in tests. This approach is also used in JViewport.
However, I’m not entirely sure whether we actually need either method here. Do we really need multi-resolution images in this context ? I think we might just be able to scale the image in createAcceleratedImage and return an OffScreenImage, or even just a BufferedImage. (I could be wrong, I'm still new to graphics.) What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth checking how hard it is to change the implementation of RepaintManager and create a buffer there w/o using this api.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is Anass's branch/PR so I'm not sure if I can/should try to commit to it. But I moved these changes to RepaintManager and wrote some fully automated tests:
This is functionally the same as the approach we were discussing. Architecturally I still prefer the idea of Component#createImage() being responsible for creating the multi-resolution image, but I'm more interested in resolving this bug than architecture.
@@ -984,7 +988,25 @@ public boolean requestFocus(Component lightweightChild, boolean temporary, | |||
|
|||
@Override | |||
public final Image createImage(final int width, final int height) { | |||
return getLWGC().createAcceleratedImage(getTarget(), width, height); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this method called from? If it is invoked by the RepaintManager, it might be better to create the appropriate back buffer there - or alternatively, you can implement OffScreenImage similar to VolatileImage. Then it will cover all platforms.
Also note that the RepaintManager uses different round logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anass-baya (and anyone else): would you like me to try adapting the test(s) here so they don't require a person to pass/fail them? (I'm not sure what the prevailing philosophy of this group is on this subject.) |
Hello @mickleness, Feel free to propose any suggestions. However, I still need to modify these tests. The keyboard @headful is missing in the TransparentWindowTest, and the keyboard manual is missing in PixelatedImageTest. |
This reverts commit 295b166.
@anass-baya This pull request has been inactive for more than 8 weeks and will be automatically closed if another 8 weeks passes without any activity. To avoid this, simply issue a |
The PR includes two fix :
1 - The first fix targets proper rendering of the transparent image.
2 - The second fix ensures the image is painted at the correct resolution to avoid pixelation.
Progress
Issue
Reviewers
Contributors
<mickleness@gmail.com>
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/23430/head:pull/23430
$ git checkout pull/23430
Update a local copy of the PR:
$ git checkout pull/23430
$ git pull https://git.openjdk.org/jdk.git pull/23430/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 23430
View PR using the GUI difftool:
$ git pr show -t 23430
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/23430.diff
Using Webrev
Link to Webrev Comment