Skip to content

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

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

anass-baya
Copy link
Contributor

@anass-baya anass-baya commented Feb 4, 2025

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

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8303904: [macos]Transparent Windows Paint Wrong (Opaque, Pixelated) w/o Volatile Buffering (Bug - P3)

Reviewers

Contributors

  • Jeremy mickleness <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

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 4, 2025

👋 Welcome back anass-baya! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Feb 4, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot changed the title JDK-8303904 [macos]Transparent Windows Paint Wrong (Opaque, Pixelated) w/o Volatile Buffering 8303904: [macos]Transparent Windows Paint Wrong (Opaque, Pixelated) w/o Volatile Buffering Feb 4, 2025
@openjdk
Copy link

openjdk bot commented Feb 4, 2025

@anass-baya The following label will be automatically applied to this pull request:

  • client

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.

@openjdk openjdk bot added client client-libs-dev@openjdk.org rfr Pull request is ready for review labels Feb 4, 2025
@mlbridge
Copy link

mlbridge bot commented Feb 4, 2025

Webrevs

float scaleFactor = getDevice().getScaleFactor();
int fullResWidth = Math.round( scaleFactor * width);
int fullResHeight = Math.round( scaleFactor * height);
WritableRaster wr = model.createCompatibleWritableRaster(fullResWidth, fullResHeight);
Copy link
Contributor

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:

  1. 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...)

  1. 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 suggests width and height were never zero. Is it possible scaleFactor 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...)

Copy link
Contributor Author

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;
}
};
}
Copy link
Contributor

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?)

Copy link
Contributor Author

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

Copy link
Contributor

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.)

Copy link
Contributor Author

@anass-baya anass-baya Feb 5, 2025

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;
}
};
}
Copy link
Contributor

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.

@aivanov-jdk
Copy link
Member

@anass-baya If your fix is based on @mickleness's previous PR, you should him as co-author using the /contributor command.

@openjdk
Copy link

openjdk bot commented Feb 4, 2025

@anass-baya @mickleness was not found in the census.

Syntax: /contributor (add|remove) [@user | openjdk-user | Full Name <email@address>]. For example:

  • /contributor add @openjdk-bot
  • /contributor add duke
  • /contributor add J. Duke <duke@openjdk.org>

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.

@anass-baya
Copy link
Contributor Author

/contributor add Jeremy mickleness mickleness@gmail.com

@openjdk
Copy link

openjdk bot commented Feb 4, 2025

@anass-baya
Contributor Jeremy mickleness <mickleness@gmail.com> successfully added.

@aivanov-jdk
Copy link
Member

@mickleness I wonder what is your preferred email address and name. You were referenced by another email in your recent contribution.

@anass-baya anass-baya marked this pull request as draft February 5, 2025 14:26
@openjdk openjdk bot removed the rfr Pull request is ready for review label Feb 5, 2025
@anass-baya anass-baya marked this pull request as ready for review February 24, 2025 08:38
@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 24, 2025

Image img = getLWGC().createAcceleratedImage(getTarget(), scaledWidth, scaledHeight);
if(ScaleX == 1 || ScaleY == 1)
return img;
Copy link
Contributor

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 ||?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes It should

Copy link
Contributor

@mickleness mickleness left a 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.*;
Copy link
Contributor

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.)

@anass-baya anass-baya changed the title 8303904: [macos]Transparent Windows Paint Wrong (Opaque, Pixelated) w/o Volatile Buffering Draft: 8303904: [macos]Transparent Windows Paint Wrong (Opaque, Pixelated) w/o Volatile Buffering Apr 9, 2025
@openjdk openjdk bot removed the rfr Pull request is ready for review label Apr 9, 2025
@anass-baya anass-baya marked this pull request as draft April 9, 2025 18:05
@anass-baya anass-baya changed the title Draft: 8303904: [macos]Transparent Windows Paint Wrong (Opaque, Pixelated) w/o Volatile Buffering 8303904: [macos]Transparent Windows Paint Wrong (Opaque, Pixelated) w/o Volatile Buffering Apr 9, 2025
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) {
Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Contributor

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).

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here

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?)

Copy link
Contributor

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?

Copy link
Contributor Author

@anass-baya anass-baya May 6, 2025

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?

Copy link
Member

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.

Copy link
Contributor

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:

https://github.com/openjdk/jdk/pull/13196/files#diff-c4425bd7c3151fbc23c7f40fcc81e02d2bba1c14793edb438fdfdc43b454451f

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);
Copy link
Member

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.

Copy link
Contributor

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?

Very few places:

image

Interestingly JViewPort may already have a helper class for this ( BackingStoreMultiResolutionImage ). I'll see if I can extract/reuse that in the next few days.

@mickleness
Copy link
Contributor

@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.)

@anass-baya
Copy link
Contributor Author

Hello @mickleness,
These were my first created jdk tests. I discussed them with Alexey, and he informed me that it's okay to write a manual test if I couldn't come up with an automated one. I tried automating it, but it was inconsistent because I couldn't come up with a finely-tuned sharpness threshold to detected the pixelated edge of the text, as the image is not too pixelated.

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.

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 9, 2025

@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 /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client client-libs-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

5 participants