Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/DirectX/NonOpaqueDestLCDAATest/NonOpaqueDestLCDAATest.java
38855 views
/*1* Copyright (c) 2008, 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 674906026* @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* @run main/manual/othervm -Dsun.java2d.opengl=True NonOpaqueDestLCDAATest32*/3334import java.awt.AlphaComposite;35import java.awt.Color;36import java.awt.Dimension;37import java.awt.EventQueue;38import java.awt.Font;39import java.awt.Graphics;40import java.awt.Graphics2D;41import java.awt.GraphicsConfiguration;42import java.awt.Image;43import java.awt.RenderingHints;44import java.awt.event.ActionEvent;45import java.awt.event.ActionListener;46import java.awt.event.ComponentAdapter;47import java.awt.event.ComponentEvent;48import java.awt.event.WindowAdapter;49import java.awt.event.WindowEvent;50import java.awt.image.BufferedImage;51import java.awt.image.VolatileImage;52import java.io.File;53import java.util.concurrent.CountDownLatch;54import javax.imageio.ImageIO;55import javax.swing.JButton;56import javax.swing.JFrame;57import javax.swing.JPanel;58import javax.swing.JTextArea;59import static java.awt.Transparency.*;6061public class NonOpaqueDestLCDAATest extends JFrame implements ActionListener {62private static volatile boolean passed = true;63private static CountDownLatch complete = new CountDownLatch(1);6465public NonOpaqueDestLCDAATest() {66JTextArea desc = new JTextArea();67desc.setText(68"\n Instructions: the three text strings below should appear" +69" readable, without smudges or misshapen bold glyphs.\n" +70" You may need a magnifier to notice some bad colorfringing in "+71" in SW Translucent case, especially in vertical stems.\n\n"+72" Basically text rendered to TRANSLUCENT destination should look"+73" similar to one rendered to OPAQUE - it may differ in whether or" +74" not it's LCD, but it should look 'correct'\n\n"+75"If the text looks fine the test PASSED otherwise it FAILED.\n");76desc.setEditable(false);77desc.setBackground(Color.black);78desc.setForeground(Color.green);79add("North", desc);80JPanel renderPanel = new JPanel() {81@Override82public void paintComponent(Graphics g) {83render(g, getWidth(), getHeight());84}85};86renderPanel.setPreferredSize(new Dimension(1024, 650));87renderPanel.addComponentListener(new ComponentAdapter() {88@Override89public void componentResized(ComponentEvent e) {90images = null;91}92});93add("Center", renderPanel);9495JButton passedBtn = new JButton("Passed");96JButton failedBtn = new JButton("Failed");97passedBtn.addActionListener(this);98failedBtn.addActionListener(this);99JPanel p = new JPanel();100p.add(passedBtn);101p.add(failedBtn);102add("South", p);103addWindowListener(new WindowAdapter() {104@Override105public void windowClosing(WindowEvent e) {106complete.countDown();107}108});109setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);110}111112public void render(Graphics g, int w, int h) {113initImages(w, h);114115g.setColor(new Color(0xAD, 0xD8, 0xE6));116g.fillRect(0, 0, w, h);117118Graphics2D g2d = (Graphics2D) g.create();119for (Image im : images) {120g2d.drawImage(im, 0, 0, null);121g2d.translate(0, im.getHeight(null));122}123}124125String tr[] = { "OPAQUE", "BITMASK", "TRANSLUCENT" };126@Override127public void actionPerformed(ActionEvent e) {128if (e.getActionCommand().equals("Passed")) {129passed = true;130System.out.println("Test Passed");131} else if (e.getActionCommand().equals("Failed")) {132System.out.println("Test Failed");133for (int i = 0; i < images.length; i++) {134String f = "NonOpaqueDestLCDAATest_"+tr[i];135try {136if (images[i] instanceof VolatileImage) {137f += "_vi.png";138ImageIO.write(((VolatileImage)images[i]).139getSnapshot(), "png", new File(f));140} else {141f += "_bi.png";142ImageIO.write((BufferedImage)images[i],143"png", new File(f));144}145System.out.printf("Dumped %s image to %s\n", tr[i], f);146} catch (Throwable t) {}147}148passed = false;149}150dispose();151complete.countDown();152}153154static void clear(Graphics2D g, int type, int w, int h) {155Graphics2D gg = (Graphics2D) g.create();156if (type > OPAQUE) {157gg.setColor(new Color(0, 0, 0, 0));158gg.setComposite(AlphaComposite.Src);159} else {160gg.setColor(new Color(0xAD, 0xD8, 0xE6));161}162gg.fillRect(0, 0, w, h);163}164165private void render(Image im, int type, String s) {166Graphics2D g2d = (Graphics2D) im.getGraphics();167clear(g2d, type, im.getWidth(null), im.getHeight(null));168g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,169RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);170Font f = new Font("Dialog", Font.BOLD, 40);// g2d.getFont().deriveFont(32.0f);171g2d.setColor(Color.white);172g2d.setFont(g2d.getFont().deriveFont(36.0f));173g2d.drawString(s, 10, im.getHeight(null) / 2);174}175176Image images[];177private void initImages(int w, int h) {178if (images == null) {179images = new Image[6];180GraphicsConfiguration gc = getGraphicsConfiguration();181for (int i = OPAQUE; i <= TRANSLUCENT; i++) {182VolatileImage vi =183gc.createCompatibleVolatileImage(w,h/images.length,i);184images[i-1] = vi;185vi.validate(gc);186String s = "LCD AA Text rendered to " + tr[i - 1] + " HW destination";187render(vi, i, s);188189s = "LCD AA Text rendered to " + tr[i - 1] + " SW destination";190images[i-1+3] = gc.createCompatibleImage(w, h/images.length, i);191render(images[i-1+3], i, s);192}193}194}195196public static void main(String[] args) throws InterruptedException {197EventQueue.invokeLater(new Runnable() {198@Override199public void run() {200NonOpaqueDestLCDAATest t = new NonOpaqueDestLCDAATest();201t.pack();202t.setVisible(true);203}204});205206complete.await();207if (!passed) {208throw new RuntimeException("Test Failed!");209}210}211}212213214