Path: blob/master/src/java.desktop/share/classes/sun/java2d/opengl/OGLPaints.java
41159 views
/*1* Copyright (c) 2007, 2011, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.opengl;2627import java.awt.GradientPaint;28import java.awt.LinearGradientPaint;29import java.awt.MultipleGradientPaint;30import java.awt.MultipleGradientPaint.ColorSpaceType;31import java.awt.MultipleGradientPaint.CycleMethod;32import java.awt.RadialGradientPaint;33import java.awt.TexturePaint;34import java.awt.image.BufferedImage;35import java.util.HashMap;36import java.util.Map;37import sun.java2d.SunGraphics2D;38import sun.java2d.SurfaceData;39import sun.java2d.loops.CompositeType;40import static sun.java2d.pipe.BufferedPaints.*;41import static sun.java2d.opengl.OGLContext.OGLContextCaps.*;4243abstract class OGLPaints {4445/**46* Holds all registered implementations, using the corresponding47* SunGraphics2D.PAINT_* constant as the hash key.48*/49private static Map<Integer, OGLPaints> impls =50new HashMap<Integer, OGLPaints>(4, 1.0f);5152static {53impls.put(SunGraphics2D.PAINT_GRADIENT, new Gradient());54impls.put(SunGraphics2D.PAINT_LIN_GRADIENT, new LinearGradient());55impls.put(SunGraphics2D.PAINT_RAD_GRADIENT, new RadialGradient());56impls.put(SunGraphics2D.PAINT_TEXTURE, new Texture());57}5859/**60* Attempts to locate an implementation corresponding to the paint state61* of the provided SunGraphics2D object. If no implementation can be62* found, or if the paint cannot be accelerated under the conditions63* of the SunGraphics2D, this method returns false; otherwise, returns64* true.65*/66static boolean isValid(SunGraphics2D sg2d) {67OGLPaints impl = impls.get(sg2d.paintState);68return (impl != null && impl.isPaintValid(sg2d));69}7071/**72* Returns true if this implementation is able to accelerate the73* Paint object associated with, and under the conditions of, the74* provided SunGraphics2D instance; otherwise returns false.75*/76abstract boolean isPaintValid(SunGraphics2D sg2d);7778/************************* GradientPaint support ****************************/7980private static class Gradient extends OGLPaints {81private Gradient() {}8283/**84* There are no restrictions for accelerating GradientPaint, so85* this method always returns true.86*/87@Override88boolean isPaintValid(SunGraphics2D sg2d) {89return true;90}91}9293/************************** TexturePaint support ****************************/9495private static class Texture extends OGLPaints {96private Texture() {}9798/**99* Returns true if the given TexturePaint instance can be used by the100* accelerated OGLPaints.Texture implementation. A TexturePaint is101* considered valid if the following conditions are met:102* - the texture image dimensions are power-of-two (or the103* GL_ARB_texture_non_power_of_two extension is present)104* - the texture image can be (or is already) cached in an OpenGL105* texture object106*/107@Override108boolean isPaintValid(SunGraphics2D sg2d) {109TexturePaint paint = (TexturePaint)sg2d.paint;110OGLSurfaceData dstData = (OGLSurfaceData)sg2d.surfaceData;111BufferedImage bi = paint.getImage();112113// see if texture-non-pow2 extension is available114if (!dstData.isTexNonPow2Available()) {115int imgw = bi.getWidth();116int imgh = bi.getHeight();117118// verify that the texture image dimensions are pow2119if ((imgw & (imgw - 1)) != 0 || (imgh & (imgh - 1)) != 0) {120return false;121}122}123124SurfaceData srcData =125dstData.getSourceSurfaceData(bi,126SunGraphics2D.TRANSFORM_ISIDENT,127CompositeType.SrcOver, null);128if (!(srcData instanceof OGLSurfaceData)) {129// REMIND: this is a hack that attempts to cache the system130// memory image from the TexturePaint instance into an131// OpenGL texture...132srcData =133dstData.getSourceSurfaceData(bi,134SunGraphics2D.TRANSFORM_ISIDENT,135CompositeType.SrcOver, null);136if (!(srcData instanceof OGLSurfaceData)) {137return false;138}139}140141// verify that the source surface is actually a texture142OGLSurfaceData oglData = (OGLSurfaceData)srcData;143if (oglData.getType() != OGLSurfaceData.TEXTURE) {144return false;145}146147return true;148}149}150151/****************** Shared MultipleGradientPaint support ********************/152153private abstract static class MultiGradient extends OGLPaints {154protected MultiGradient() {}155156/**157* Returns true if the given MultipleGradientPaint instance can be158* used by the accelerated OGLPaints.MultiGradient implementation.159* A MultipleGradientPaint is considered valid if the following160* conditions are met:161* - the number of gradient "stops" is <= MAX_FRACTIONS162* - the destination has support for fragment shaders163*/164@Override165boolean isPaintValid(SunGraphics2D sg2d) {166MultipleGradientPaint paint = (MultipleGradientPaint)sg2d.paint;167// REMIND: ugh, this creates garbage; would be nicer if168// we had a MultipleGradientPaint.getNumStops() method...169if (paint.getFractions().length > MULTI_MAX_FRACTIONS) {170return false;171}172173OGLSurfaceData dstData = (OGLSurfaceData)sg2d.surfaceData;174OGLGraphicsConfig gc = dstData.getOGLGraphicsConfig();175if (!gc.isCapPresent(CAPS_EXT_GRAD_SHADER)) {176return false;177}178179return true;180}181}182183/********************** LinearGradientPaint support *************************/184185private static class LinearGradient extends MultiGradient {186private LinearGradient() {}187188@Override189boolean isPaintValid(SunGraphics2D sg2d) {190LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;191192if (paint.getFractions().length == 2 &&193paint.getCycleMethod() != CycleMethod.REPEAT &&194paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)195{196// we can delegate to the optimized two-color gradient197// codepath, which does not require fragment shader support198return true;199}200201return super.isPaintValid(sg2d);202}203}204205/********************** RadialGradientPaint support *************************/206207private static class RadialGradient extends MultiGradient {208private RadialGradient() {}209}210}211212213