1 /*
   2  * Copyright (c) 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug      8176795
  27  * @summary  Test verifies that we get proper color when we draw translucent
  28  *           color over an opaque color using X Render extension in Linux.
  29  * @requires (os.family == "linux")
  30  * @run      main XRenderTranslucentColorDrawTest -Dsun.java2d.xrender=true
  31  */
  32 
  33 import java.awt.Color;
  34 import java.awt.Graphics2D;
  35 import java.awt.GraphicsConfiguration;
  36 import java.awt.GraphicsDevice;
  37 import java.awt.GraphicsEnvironment;
  38 import java.awt.image.BufferedImage;
  39 import java.awt.image.VolatileImage;
  40 
  41 public class XRenderTranslucentColorDrawTest {
  42 
  43     public static void main(String[] args) throws Exception {
  44         GraphicsEnvironment env = GraphicsEnvironment.
  45                 getLocalGraphicsEnvironment();
  46         GraphicsConfiguration translucentGC = null;
  47         SCREENS: for (GraphicsDevice screen : env.getScreenDevices()) {
  48             for (GraphicsConfiguration gc : screen.getConfigurations()) {
  49                 if (gc.isTranslucencyCapable()) {
  50                     translucentGC = gc;
  51                     break SCREENS;
  52                 }
  53             }
  54         }
  55         if (translucentGC == null) {
  56             throw new RuntimeException("No suitable gc found.");
  57         }
  58         int width = 10;
  59         int height = 10;
  60         VolatileImage image = translucentGC.
  61                 createCompatibleVolatileImage(width, height);
  62         Graphics2D g = image.createGraphics();
  63         // draw opaque black color
  64         g.setColor(new Color(0xff000000, true));
  65         g.fillRect(0, 0, width, height);
  66         // draw translucent white color over opaque black color
  67         g.setColor(new Color(0x80ffffff, true));
  68         g.fillRect(0, 0, width, height);
  69         g.dispose();
  70         // Get snapshot of VolatileImage to pick color and verify the same
  71         BufferedImage snapshot = image.getSnapshot();
  72         int argb = snapshot.getRGB(width / 2, height / 2);
  73         // we expect the resultant rgb hex value to be ff808080
  74         if (!(Integer.toHexString(argb).equals("ff808080"))) {
  75             throw new RuntimeException("Using X Render extension for drawing"
  76                     + " translucent color is not giving expected results.");
  77         }
  78     }
  79 }
  80