Path: blob/master/test/jdk/sun/java2d/DirectX/NonOpaqueDestLCDAATest/NonOpaqueDestLCDAATest.java
41153 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6728834 6749060 819861326* @summary Tests that LCD AA text rendering works properly with destinations27* being VolatileImage of all transparency types28* @author Dmitri.Trembovetski: area=Graphics29* @run main/manual/othervm -Dsun.java2d.d3d=false NonOpaqueDestLCDAATest30* @run main/manual/othervm NonOpaqueDestLCDAATest31*/3233import java.awt.AlphaComposite;34import java.awt.Color;35import java.awt.Dimension;36import java.awt.EventQueue;37import java.awt.Font;38import java.awt.Graphics;39import java.awt.Graphics2D;40import java.awt.GraphicsConfiguration;41import java.awt.Image;42import java.awt.RenderingHints;43import java.awt.event.ActionEvent;44import java.awt.event.ActionListener;45import java.awt.event.ComponentAdapter;46import java.awt.event.ComponentEvent;47import java.awt.event.WindowAdapter;48import java.awt.event.WindowEvent;49import java.awt.image.BufferedImage;50import java.awt.image.VolatileImage;51import java.io.File;52import java.util.concurrent.CountDownLatch;53import javax.imageio.ImageIO;54import javax.swing.JButton;55import javax.swing.JFrame;56import javax.swing.JPanel;57import javax.swing.JTextArea;58import static java.awt.Transparency.*;5960public class NonOpaqueDestLCDAATest extends JFrame implements ActionListener {61private static volatile boolean passed = true;62private static CountDownLatch complete = new CountDownLatch(1);6364public NonOpaqueDestLCDAATest() {65JTextArea desc = new JTextArea();66desc.setText(67"\n Instructions: the three text strings below should appear" +68" readable, without smudges or misshapen bold glyphs.\n" +69" You may need a magnifier to notice some bad colorfringing in "+70" in SW Translucent case, especially in vertical stems.\n\n"+71" Basically text rendered to TRANSLUCENT destination should look"+72" similar to one rendered to OPAQUE - it may differ in whether or" +73" not it's LCD, but it should look 'correct'\n\n"+74"If the text looks fine the test PASSED otherwise it FAILED.\n");75desc.setEditable(false);76desc.setBackground(Color.black);77desc.setForeground(Color.green);78add("North", desc);79JPanel renderPanel = new JPanel() {80@Override81public void paintComponent(Graphics g) {82render(g, getWidth(), getHeight());83}84};85renderPanel.setPreferredSize(new Dimension(1024, 650));86renderPanel.addComponentListener(new ComponentAdapter() {87@Override88public void componentResized(ComponentEvent e) {89images = null;90}91});92add("Center", renderPanel);9394JButton passedBtn = new JButton("Passed");95JButton failedBtn = new JButton("Failed");96passedBtn.addActionListener(this);97failedBtn.addActionListener(this);98JPanel p = new JPanel();99p.add(passedBtn);100p.add(failedBtn);101add("South", p);102addWindowListener(new WindowAdapter() {103@Override104public void windowClosing(WindowEvent e) {105complete.countDown();106}107});108setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);109}110111public void render(Graphics g, int w, int h) {112initImages(w, h);113114g.setColor(new Color(0xAD, 0xD8, 0xE6));115g.fillRect(0, 0, w, h);116117Graphics2D g2d = (Graphics2D) g.create();118for (Image im : images) {119g2d.drawImage(im, 0, 0, null);120g2d.translate(0, im.getHeight(null));121}122}123124String tr[] = { "OPAQUE", "BITMASK", "TRANSLUCENT" };125@Override126public void actionPerformed(ActionEvent e) {127if (e.getActionCommand().equals("Passed")) {128passed = true;129System.out.println("Test Passed");130} else if (e.getActionCommand().equals("Failed")) {131System.out.println("Test Failed");132for (int i = 0; i < images.length; i++) {133String f = "NonOpaqueDestLCDAATest_"+tr[i];134try {135if (images[i] instanceof VolatileImage) {136f += "_vi.png";137ImageIO.write(((VolatileImage)images[i]).138getSnapshot(), "png", new File(f));139} else {140f += "_bi.png";141ImageIO.write((BufferedImage)images[i],142"png", new File(f));143}144System.out.printf("Dumped %s image to %s\n", tr[i], f);145} catch (Throwable t) {}146}147passed = false;148}149dispose();150complete.countDown();151}152153static void clear(Graphics2D g, int type, int w, int h) {154Graphics2D gg = (Graphics2D) g.create();155if (type > OPAQUE) {156gg.setColor(new Color(0, 0, 0, 0));157gg.setComposite(AlphaComposite.Src);158} else {159gg.setColor(new Color(0xAD, 0xD8, 0xE6));160}161gg.fillRect(0, 0, w, h);162}163164private void render(Image im, int type, String s) {165Graphics2D g2d = (Graphics2D) im.getGraphics();166clear(g2d, type, im.getWidth(null), im.getHeight(null));167g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,168RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);169Font f = new Font("Dialog", Font.BOLD, 40);// g2d.getFont().deriveFont(32.0f);170g2d.setColor(Color.white);171g2d.setFont(g2d.getFont().deriveFont(36.0f));172g2d.drawString(s, 10, im.getHeight(null) / 2);173}174175Image images[];176private void initImages(int w, int h) {177if (images == null) {178images = new Image[6];179GraphicsConfiguration gc = getGraphicsConfiguration();180for (int i = OPAQUE; i <= TRANSLUCENT; i++) {181VolatileImage vi =182gc.createCompatibleVolatileImage(w,h/images.length,i);183images[i-1] = vi;184vi.validate(gc);185String s = "LCD AA Text rendered to " + tr[i - 1] + " HW destination";186render(vi, i, s);187188s = "LCD AA Text rendered to " + tr[i - 1] + " SW destination";189images[i-1+3] = gc.createCompatibleImage(w, h/images.length, i);190render(images[i-1+3], i, s);191}192}193}194195public static void main(String[] args) throws InterruptedException {196EventQueue.invokeLater(new Runnable() {197@Override198public void run() {199NonOpaqueDestLCDAATest t = new NonOpaqueDestLCDAATest();200t.pack();201t.setVisible(true);202}203});204205complete.await();206if (!passed) {207throw new RuntimeException("Test Failed!");208}209}210}211212213