Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/OpenGL/CustomCompositeTest.java
47216 views
/*1* Copyright (c) 2012, 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 712434726* @summary Verifies that rendering with XOR composite, and arbitraty27* custom composite doesn not cause internal errors.28*29* @run main/othervm -Dsun.java2d.opengl=True CustomCompositeTest30*/3132import java.awt.AWTException;33import java.awt.Color;34import java.awt.Composite;35import java.awt.CompositeContext;36import java.awt.Dimension;37import java.awt.GradientPaint;38import java.awt.Graphics;39import java.awt.Graphics2D;40import java.awt.GraphicsConfiguration;41import java.awt.GraphicsEnvironment;42import java.awt.ImageCapabilities;43import java.awt.RenderingHints;44import java.awt.image.BufferedImage;45import java.awt.image.ColorModel;46import java.awt.image.DataBufferInt;47import java.awt.image.Raster;48import java.awt.image.SinglePixelPackedSampleModel;49import java.awt.image.VolatileImage;50import java.awt.image.WritableRaster;51import java.util.concurrent.CountDownLatch;52import javax.swing.JComponent;53import javax.swing.JFrame;54import javax.swing.SwingUtilities;5556public class CustomCompositeTest {5758private static JFrame frame;59private static CountDownLatch paintLatch;60private static Throwable paintError;6162public static void main(String[] args) {6364paintLatch = new CountDownLatch(1);65paintError = null;6667SwingUtilities.invokeLater(new Runnable() {68public void run() {69initGUI();70}71});7273try {74paintLatch.await();75} catch (InterruptedException e) {76};77System.out.println("Paint is done!");78if (paintError != null) {79frame.dispose();80throw new RuntimeException("Test FAILED.", paintError);81}8283System.out.println("Phase 1: PASSED.");8485// now resise the frame in order to cause re-paint with accelerated86// source images.87paintError = null;88paintLatch = new CountDownLatch(1);8990SwingUtilities.invokeLater(new Runnable() {91@Override92public void run() {93Dimension size = frame.getSize();94size.width += 50;95size.height += 50;9697frame.setSize(size);98}99});100101try {102paintLatch.await();103} catch (InterruptedException e) {104};105if (paintError != null) {106frame.dispose();107throw new RuntimeException("Resize test FAILED.", paintError);108}109frame.dispose();110System.out.println("Phase 2: PASSED.");111112GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();113GraphicsConfiguration cfg = env.getDefaultScreenDevice().getDefaultConfiguration();114// test rendering to accelerated volatile image115testVolatileImage(cfg, true);116System.out.println("Phase 3: PASSED.");117118// test rendering to unaccelerated volatile image119testVolatileImage(cfg, false);120System.out.println("Phase 4: PASSED.");121}122123private static void testVolatileImage(GraphicsConfiguration cfg,124boolean accelerated)125{126VolatileImage dst = null;127try {128dst = cfg.createCompatibleVolatileImage(640, 480,129new ImageCapabilities(accelerated));130} catch (AWTException e) {131System.out.println("Unable to create volatile image, skip the test.");132return;133}134renderToVolatileImage(dst);135}136137private static void renderToVolatileImage(VolatileImage dst) {138Graphics2D g = dst.createGraphics();139do {140System.out.println("Render to volatile image..");141try {142MyComp.renderTest(g, dst.getHeight(), dst.getHeight());143} catch (Throwable e) {144throw new RuntimeException("Test FAILED.", e);145}146} while (dst.contentsLost());147System.out.println("Done.");148}149150private static void initGUI() {151frame = new JFrame("Silly composite");152frame.getContentPane().add(new MyComp());153frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);154frame.pack();155frame.setVisible(true);156}157158private static class MyComp extends JComponent {159160private static BufferedImage theImage;161162public MyComp() {163}164165private static BufferedImage getTestImage() {166if (theImage == null) {167theImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);168Graphics2D g2d = theImage.createGraphics();169g2d.setColor(Color.red);170g2d.fillRect(0, 0, 256, 256);171172g2d.setPaint(new GradientPaint(0, 0, Color.red, 256, 256, Color.blue));173g2d.fillRect(0, 100, 256, 256);174g2d.dispose();175}176return theImage;177}178179public Dimension getPreferredSize() {180return new Dimension(640, 375);181}182183public void paintComponent(Graphics g) {184185186Graphics2D g2d = (Graphics2D) g;187try {188renderTest(g2d, getWidth(), getHeight());189} catch (Throwable e) {190paintError = e;191}192if (paintLatch != null) {193paintLatch.countDown();194}195}196197public static void renderTest(Graphics2D g2d, int w, int h) {198g2d.setColor(Color.yellow);199g2d.fillRect(0, 0, w, h);200201BufferedImage image = getTestImage();202// draw original image203g2d.drawRenderedImage(image, null);204205// draw image with custom composite206g2d.translate(175, 25);207Composite currentComposite = g2d.getComposite();208g2d.setComposite(new TestComposite());209g2d.drawRenderedImage(image, null);210g2d.setComposite(currentComposite);211212// draw image with XOR213g2d.translate(175, 25);214g2d.setXORMode(Color.red);215g2d.drawRenderedImage(image, null);216217218System.out.println("Painting is done...");219}220}221222// A silly custom Composite to demonstrate the problem - just inverts the RGB223private static class TestComposite implements Composite {224225public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints) {226return new TestCompositeContext();227}228}229230private static class TestCompositeContext implements CompositeContext {231232public void dispose() {233}234235public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {236int w = src.getWidth();237int h = src.getHeight();238239DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();240DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer();241int srcRGB[] = srcDB.getBankData()[0];242int dstOutRGB[] = dstOutDB.getBankData()[0];243int srcOffset = srcDB.getOffset();244int dstOutOffset = dstOutDB.getOffset();245int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride();246int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride();247int srcAdjust = srcScanStride - w;248int dstOutAdjust = dstOutScanStride - w;249250int si = srcOffset;251int doi = dstOutOffset;252253for (int i = 0; i < h; i++) {254for (int j = 0; j < w; j++) {255dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff;256si++;257doi++;258}259260si += srcAdjust;261doi += dstOutAdjust;262}263}264}265}266267268