1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.awt.image;
  26 
  27 import java.awt.Image;
  28 import java.awt.image.ImageObserver;
  29 import java.awt.image.MultiResolutionImage;
  30 import java.util.Arrays;
  31 import java.util.List;
  32 import sun.misc.SoftCache;
  33 
  34 public class MultiResolutionToolkitImage extends ToolkitImage
  35         implements MultiResolutionImage {
  36 
  37     private final Image resolutionVariant;
  38     private final ImageResolutionHelper imageResolutionHelper;
  39 
  40     public MultiResolutionToolkitImage(Image lowResolutionImage,
  41             Image resolutionVariant) {
  42         this(lowResolutionImage, resolutionVariant,
  43                 ImageResolutionHelperType.getPlatformHelper());
  44     }
  45 
  46     public MultiResolutionToolkitImage(Image lowResolutionImage,
  47             Image resolutionVariant, ImageResolutionHelper imageResolutionHelper) {
  48         super(lowResolutionImage.getSource());
  49         this.resolutionVariant = resolutionVariant;
  50         this.imageResolutionHelper = imageResolutionHelper;
  51     }
  52 
  53     @Override
  54     public Image getResolutionVariant(float logicalDPIX, float logicalDPIY,
  55             float baseWidth, float baseHeight, float destWidth, float destHeight) {
  56         float hiDPIWidth = imageResolutionHelper.getHiDPISize(logicalDPIX,
  57                 baseWidth, destWidth);
  58         float hiDPIHeight = imageResolutionHelper.getHiDPISize(logicalDPIY,
  59                 baseHeight, destHeight);
  60         return getResolutionVariant(hiDPIWidth, hiDPIHeight);
  61     }
  62 
  63     private Image getResolutionVariant(float width, float height) {
  64         return ((width <= getWidth() && height <= getHeight()))
  65                 ? this : resolutionVariant;
  66     }
  67 
  68     public Image getResolutionVariant() {
  69         return resolutionVariant;
  70     }
  71 
  72     @Override
  73     public List<Image> getResolutionVariants() {
  74         return Arrays.<Image>asList(this, resolutionVariant);
  75     }
  76 
  77     private static final int BITS_INFO = ImageObserver.SOMEBITS
  78             | ImageObserver.FRAMEBITS | ImageObserver.ALLBITS;
  79 
  80     private static class ObserverCache {
  81 
  82         static final SoftCache INSTANCE = new SoftCache();
  83     }
  84 
  85     public static ImageObserver getResolutionVariantObserver(
  86             final Image image, final ImageObserver observer,
  87             final int imgWidth, final int imgHeight,
  88             final int rvWidth, final int rvHeight) {
  89 
  90         if (observer == null) {
  91             return null;
  92         }
  93 
  94         synchronized (ObserverCache.INSTANCE) {
  95             ImageObserver o = (ImageObserver) ObserverCache.INSTANCE.get(image);
  96 
  97             if (o == null) {
  98 
  99                 o = (Image resolutionVariant, int flags,
 100                         int x, int y, int width, int height) -> {
 101 
 102                             if ((flags & (ImageObserver.WIDTH | BITS_INFO)) != 0) {
 103                                 width = (width + 1) / 2;
 104                             }
 105 
 106                             if ((flags & (ImageObserver.HEIGHT | BITS_INFO)) != 0) {
 107                                 height = (height + 1) / 2;
 108                             }
 109 
 110                             if ((flags & BITS_INFO) != 0) {
 111                                 x /= 2;
 112                                 y /= 2;
 113                             }
 114 
 115                             return observer.imageUpdate(
 116                                     image, flags, x, y, width, height);
 117                         };
 118 
 119                 ObserverCache.INSTANCE.put(image, o);
 120             }
 121             return o;
 122         }
 123     }
 124 }