1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.ui.misc;
  34 
  35 import java.awt.Font;
  36 import java.awt.Graphics2D;
  37 import java.awt.RenderingHints;
  38 import java.awt.geom.AffineTransform;
  39 import java.awt.image.BufferedImage;
  40 import java.awt.image.DataBufferByte;
  41 
  42 import org.eclipse.swt.events.PaintEvent;
  43 import org.eclipse.swt.graphics.Image;
  44 import org.eclipse.swt.graphics.ImageData;
  45 import org.eclipse.swt.graphics.PaletteData;
  46 import org.eclipse.swt.widgets.Display;
  47 import org.openjdk.jmc.ui.UIPlugin;
  48 import org.openjdk.jmc.ui.common.util.Environment;
  49 import org.openjdk.jmc.ui.preferences.PreferenceConstants;
  50 
  51 public class AwtCanvas {
  52         private ImageData imageDataSWT;
  53         private BufferedImage imageAWT;
  54 
  55         private static final double X_SCALE = (Display.getCurrent().getDPI().x) / Environment.getNormalDPI();
  56         private static final double Y_SCALE = (Display.getCurrent().getDPI().y) / Environment.getNormalDPI();
  57 
  58         public boolean hasImage(int width, int height) {
  59                 return (imageDataSWT != null) && (imageDataSWT.width == width) && (imageDataSWT.height == height);
  60         }
  61 
  62         public Graphics2D getGraphics(int width, int height) {
  63                 if ((imageDataSWT == null) || (imageDataSWT.width != width) || (imageDataSWT.height != height)) {
  64                         imageAWT = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  65                         PaletteData vpPalette = new PaletteData(0xff, 0xff00, 0xff0000);
  66                         int scanlinePad = width * 3;
  67                         byte[] byteData = ((DataBufferByte) imageAWT.getRaster().getDataBuffer()).getData();
  68                         imageDataSWT = new ImageData(width, height, 24, vpPalette, scanlinePad, byteData);
  69                         Graphics2D graphicsAWT = imageAWT.createGraphics();
  70                         setAntiAliasing(graphicsAWT);
  71                         graphicsAWT.setFont(new Font("OptionPane.font", Font.PLAIN, 12)); //$NON-NLS-1$
  72                         fixDPI(graphicsAWT);
  73                         return graphicsAWT;
  74                 } else {
  75                         Graphics2D graphicsAWT = imageAWT.createGraphics();
  76                         setAntiAliasing(graphicsAWT);
  77                         graphicsAWT.setFont(new Font("OptionPane.font", Font.PLAIN, 12)); //$NON-NLS-1$
  78                         graphicsAWT.clearRect(0, 0, width, height);
  79                         fixDPI(graphicsAWT);
  80                         return graphicsAWT;
  81                 }
  82         }
  83         
  84         private void setAntiAliasing(Graphics2D ctx) {
  85                 Boolean antiAliasing = UIPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.P_ANTI_ALIASING);
  86                 if (antiAliasing) {
  87                         ctx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  88                 } else {
  89                         ctx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
  90                 }
  91         }
  92 
  93         private void fixDPI(Graphics2D ctx) {
  94                 AffineTransform defaultTransform = ctx.getDeviceConfiguration().getDefaultTransform();
  95                 defaultTransform.scale(X_SCALE, Y_SCALE);
  96                 ctx.setTransform(defaultTransform);
  97         }
  98 
  99         public void paint(PaintEvent e, int x, int y) {
 100                 try {
 101                         Image img = new Image(e.display, imageDataSWT);
 102                         e.gc.drawImage(img, x, y);
 103                         img.dispose();
 104                 } catch (ArrayIndexOutOfBoundsException ex) {
 105                         // Workaround for image construction bug
 106                 }
 107         }
 108 }