[OpenJDK 2D-Dev] <AWT Dev> [8] Review request for 8011059 [macosx] Make JDK demos look perfect on retina displays

Alexander Scherbatiy alexandr.scherbatiy at oracle.com
Wed Nov 6 05:19:29 PST 2013


On 11/6/2013 5:39 AM, Jim Graham wrote:
> Why is getScaledInstance() being consulted here?  It seems a misuse of 
> that method.
     We need to introduce a new API that allows developers to return 
scaled images for HiDPI displays.
     Scaled images are just new images N times bigger than the original 
one and created by designers.

     The good way is to allow a user to override a method like 
getScaledImage() which has either scaledFactor or width and height 
parameters and returns necessary scaled images.
     It could look like:
    -----------------
         final Image image1 = ImageIO.read("image1.png");
         final Image image2 = ImageIO.read("image2.png");
         final Image image3 = ImageIO.read("image3.png");

         final int WIDTH = image1.getWidth(null);
         final int HEIGHT = image1.getHeight(null);

         Image image = new BufferedImage(WIDTH, HEIGHT, 
BufferedImage.TYPE_INT_RGB) {
             @Override
             public Image getScaledImage(int width, int height) {
                 if (width < WIDTH && height < HEIGHT) {
                     return this;
                 } else if (width < 2 * WIDTH && height < 2 * HEIGHT) {
                     return image2;
                 } else if (width < 3 * WIDTH && height < 3 * HEIGHT) {
                     return image3;
                 }
             }
         };

         image.getGraphics().drawImage(image1, 0, 0, null);
    -----------------

     But the  getScaledImage(width, height) method looks similar to the 
getScaledInstance(width, height, hints). It also can confuse a user 
which method should be overridden to return
     the scaled version of an image.

    The current fix is based on the CCC request 8011059 where it has 
been approved to use the getScaledInstance(width, height, hints) method 
for this purposes.

> The method was designed to return a rescaled version of the same 
> pixels that you would get if you examined the raw pixels.  You are 
> overriding it to return a different image.  That does not fit in with 
> the original intent of that method as I created it.
>
> It is also causing implementation headaches (read reflection) in the 
> SG2D code to try to use it that way.
      The Image.SCALE_DEFAULT hint is used to retrieve user scaled 
images in the CCC request 8011059.
      That was a wrong idea. There are intersections with the original 
meaning with the SCALE_DEFAULT hint.
      Also there are performance regression and exceptions like OOM or 
IOBE if the getScaledInstance(width, height, hints) is not overridden 
and it is invoked for each image.

      The better way could be to use a new Image hint designed only to 
retrieve scaled images provided by a user.
      I have created the CCC request 8027655 for this.
      Once the request will be approved it will be possible to get rid 
of the reflection usage.

    Thanks,
    Alexandr.

>
> The @2x mechanism should be based on different API.  I guess it would 
> have to be internal-only for 8.0 and could be exposed to allow 
> developers to call it and possibly to be a provider for it in JDK9...
>
>             ...jim
>
> On 10/31/13 9:19 AM, Alexander Scherbatiy wrote:
>>
>>   Could you review the updated fix:
>> http://cr.openjdk.java.net/~alexsch/8011059/webrev.04/
>>
>>     The reflection is used to skip the Image.getScaledInstance() method
>> call if it is not overridden by a user.
>>
>> On 10/29/2013 11:08 PM, Sergey Bylokhov wrote:
>>> Hi, Alexander.
>>> The fix looks fine to me in general. But there is at least one issue.
>>> I build you fix and test it:
>>>  - Consuming of cpu increased by 500 times Java2Demo on images tab.
>>>  - FPS is dropped from 220(jdk8)/35(jdk7u40) to 15 in guimark2. Note
>>> that jdk6 has the same FPS(15) on my system.
>>
>>     The main problem is that the Image.SCALE_DEFAULT hint is used to
>> retrieve a scaled image from Image.getScaledInstance() method.
>>     It always uses the ReplicateScaleFilter for images which
>> getScaledInstance() method has not been overridden.
>>     The ReplicateScaleFilter creates a lot of arrays and consumes the
>> CPU during the image parsing.
>>
>>     The better fix would be to introduce the new Image.SCALE_CUSTOM hint
>> which could be used to get a scaled image and does not use filters by
>> default.
>>     But it should be a separated bug with a new CCC request.
>>
>>    Thanks,
>>    Alexandr.
>>
>>>
>>> On 29.10.2013 20:45, Alexander Scherbatiy wrote:
>>>>
>>>>   Could you review the updated fix:
>>>> http://cr.openjdk.java.net/~alexsch/8011059/webrev.03
>>>>
>>>> On 10/28/2013 2:33 PM, Artem Ananiev wrote:
>>>>> Hi, Alexander,
>>>>>
>>>>> a few comments:
>>>>>
>>>>> 1. SunGraphics2D.java:3076 - should isHiDPIImage() be used here?
>>>>      The isHiDPIImage() method is used to check that the
>>>> drawHiDPIImage should be called like:
>>>>         if (isHiDPIImage(img)) {
>>>>             return drawHiDPIImage(...);
>>>>         }
>>>>
>>>>> 2. I'm not sure that the proposed getScaledImageName()
>>>>> implementation in ScalableToolkitImage works perfectly for URLs like
>>>>> this:
>>>>>
>>>>> http://www.exampmle.com/dir/image
>>>>>
>>>>> In this case it will try to find 2x image here:
>>>>>
>>>>> http://www.example@2x.com/dir/image
>>>>>
>>>>> which doesn't look correct.
>>>>        Fixed. Only path part of a URL is converted to path2x.
>>>>
>>>>> 3. RenderingHints spec references Retina or non-Retina displays,
>>>>> which should be removed.
>>>>        Fixed.
>>>>
>>>>     - devScale is used instead of transform parsing in the
>>>> drawHiDPIImage() method just to not have performance regression more
>>>> than 2 times on HiDPI displays
>>>>     - LWCToolkit.ScalableToolkitImage is made public for the fix
>>>> 8024926 [macosx] AquaIcon HiDPI support.
>>>>
>>>>     Thanks,
>>>>     Alexandr.
>>>>
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Artem
>>>>>
>>>>> On 10/25/2013 5:18 PM, Alexander Scherbatiy wrote:
>>>>>>
>>>>>>    Could you review the updated fix:
>>>>>> http://cr.openjdk.java.net/~alexsch/8011059/webrev.02/
>>>>>>
>>>>>>    - Scaled image width and height are transformed according to the
>>>>>> AffineTransform type.
>>>>>>    - ToolkitImage subclass is used to hold @2x image instance.
>>>>>>
>>>>>>   Thanks,
>>>>>>   Alexandr.
>>>>>>
>>>>>> On 10/23/2013 7:24 PM, Alexander Scherbatiy wrote:
>>>>>>>
>>>>>>>   Could you review the updated fix:
>>>>>>> http://cr.openjdk.java.net/~alexsch/8011059/webrev.01/
>>>>>>>
>>>>>>>   The JCK failures has been resolved:
>>>>>>>     - Some tests tries to draw an image with Integer.MAX_VALUE 
>>>>>>> width
>>>>>>> or height. Passing large values to image.getScaledImage(width,
>>>>>>> height,
>>>>>>> hints).
>>>>>>>        leads that an Image filter is not able to create necessary
>>>>>>> arrays.  The fix uses the original image if width or height are 
>>>>>>> equal
>>>>>>> to Integer.MAX_VALUE.
>>>>>>>     - Using Image.SCALE_DEFAULT hint for the getScaledImage(width,
>>>>>>> height, hints) method to get the high resolution image interferes
>>>>>>> with
>>>>>>>        JCK tests that expect that the scaled image by certain
>>>>>>> algorithm is returned. This is fixed by invoking the
>>>>>>> super.getScaledImage(width, height, hints)
>>>>>>>        method in ToolkitImage in case if a high resolution image is
>>>>>>> not set.
>>>>>>>
>>>>>>>     Thanks,
>>>>>>>     Alexandr.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On 10/22/2013 1:31 PM, Alexander Scherbatiy wrote:
>>>>>>>>
>>>>>>>> Hello,
>>>>>>>>
>>>>>>>> Could you review the fix:
>>>>>>>>
>>>>>>>>   bug: https://bugs.openjdk.java.net/browse/JDK-8011059
>>>>>>>>   webrev: http://cr.openjdk.java.net/~alexsch/8011059/webrev.00
>>>>>>>>
>>>>>>>>   The IMAGE_SCALING rendering hint is added to the RenderingHints
>>>>>>>> class.
>>>>>>>>   Enabling the image scaling rendering hint forces the 
>>>>>>>> SunGraphics2D
>>>>>>>> to use getScaledInstance(width, height, hints) method
>>>>>>>>   from Image class with SCALE_DEFAULT hint.
>>>>>>>>
>>>>>>>>   By default the image scaling rendering hint is enabled on HiDPI
>>>>>>>> display and disabled for standard displays.
>>>>>>>>
>>>>>>>>   User can override the getScaledInstance(width, height, hints)
>>>>>>>> method and return necessary high resolution image
>>>>>>>>   according to the given image width and height.
>>>>>>>>
>>>>>>>>   For example:
>>>>>>>>   ---------------------
>>>>>>>>         final Image highResolutionImage =
>>>>>>>>                 new BufferedImage(2 * WIDTH, 2 * HEIGHT,
>>>>>>>> BufferedImage.TYPE_INT_RGB);
>>>>>>>>         Image image = new BufferedImage(WIDTH, HEIGHT,
>>>>>>>> BufferedImage.TYPE_INT_RGB) {
>>>>>>>>
>>>>>>>>             @Override
>>>>>>>>             public Image getScaledInstance(int width, int height,
>>>>>>>> int
>>>>>>>> hints) {
>>>>>>>>                 if ((hints & Image.SCALE_DEFAULT) != 0) {
>>>>>>>>                     return (width <= WIDTH && height <= HEIGHT)
>>>>>>>>                             ? this : highResolutionImage;
>>>>>>>>                 }
>>>>>>>>                 return super.getScaledInstance(width, height,
>>>>>>>> hints);
>>>>>>>>             }
>>>>>>>>         };
>>>>>>>>   ---------------------
>>>>>>>>
>>>>>>>>   The LWCToolkit and ToolkitImage classes are patched to
>>>>>>>> automatically get provided image at 2x.ext images on MacOSX.
>>>>>>>>
>>>>>>>>   There are no significant changes in the Java2D demo to make it
>>>>>>>> look
>>>>>>>> perfect on Retina displays.
>>>>>>>>   It needs only to put necessary images with the @2x postfix and
>>>>>>>> they
>>>>>>>> will be automatically drawn.
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>> Alexandr.
>>>>>>>>
>>>>>>>
>>>>>>
>>>>
>>>
>>>
>>



More information about the macosx-port-dev mailing list