APIdia Launch Partner
Top Fields Constructors Methods
sun.java2d.pipe

public Class BufferedPaints

extends Object
Class Inheritance
Imports
java.awt.Color, .GradientPaint, .LinearGradientPaint, .MultipleGradientPaint, .MultipleGradientPaint.ColorSpaceType, .MultipleGradientPaint.CycleMethod, .Paint, .RadialGradientPaint, .TexturePaint, java.awt.geom.AffineTransform, .Point2D, .Rectangle2D, java.awt.image.AffineTransformOp, .BufferedImage, sun.awt.image.PixelConverter, sun.java2d.SunGraphics2D, .SurfaceData, sun.java2d.loops.CompositeType, .SurfaceType, java.lang.annotation.Native

Field Summary

Modifier and TypeField and Description
public static final int
MULTI_MAX_FRACTIONS

The maximum number of gradient "stops" supported by our native fragment shader implementations.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static int
colorToIntArgbPrePixel(Color c, boolean linear)

Helper function to convert a (non-premultiplied) Color in sRGB space to an IntArgbPre pixel value, optionally in linear RGB space.

public static int
convertSRGBtoLinearRGB(int color)

Helper function to convert a color component in sRGB space to linear RGB space.

private static int[]
convertToIntArgbPrePixels(Color[] colors, boolean linear)

Converts the given array of Color objects into an int array containing IntArgbPre pixel values.

pack-priv static void
private static void
setColor(RenderQueue rq, int pixel)

Color support

private static void
setGradientPaint(RenderQueue rq, AffineTransform at, Color c1, Color c2, Point2D pt1, Point2D pt2, boolean isCyclic, boolean useMask)

Note

This code is factored out into a separate static method so that it can be shared by both the Gradient and LinearGradient implementations.

private static void
setGradientPaint(RenderQueue rq, SunGraphics2D sg2d, GradientPaint paint, boolean useMask)

private static void
setLinearGradientPaint(RenderQueue rq, SunGraphics2D sg2d, LinearGradientPaint paint, boolean useMask)

This method uses techniques that are nearly identical to those employed in setGradientPaint() above.

pack-priv static void
setPaint(RenderQueue rq, SunGraphics2D sg2d, Paint paint, int ctxflags)

private static void
setRadialGradientPaint(RenderQueue rq, SunGraphics2D sg2d, RadialGradientPaint paint, boolean useMask)

This method calculates six m** values and a focusX value that are used by the native fragment shader.

private static void
setTexturePaint(RenderQueue rq, SunGraphics2D sg2d, TexturePaint paint, boolean useMask)

We use OpenGL's texture coordinate generator to automatically map the TexturePaint image to the geometry being rendered.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

MULTI_MAX_FRACTIONSback to summary
public static final int MULTI_MAX_FRACTIONS

The maximum number of gradient "stops" supported by our native fragment shader implementations. This value has been empirically determined and capped to allow our native shaders to run on all shader-level graphics hardware, even on the older, more limited GPUs. Even the oldest Nvidia hardware could handle 16, or even 32 fractions without any problem. But the first-generation boards from ATI would fall back into software mode (which is unusably slow) for values larger than 12; it appears that those boards do not have enough native registers to support the number of array accesses required by our gradient shaders. So for now we will cap this value at 12, but we can re-evaluate this in the future as hardware becomes more capable.

Annotations
@Native

Constructor Detail

BufferedPaintsback to summary
public BufferedPaints()

Method Detail

colorToIntArgbPrePixelback to summary
private static int colorToIntArgbPrePixel(Color c, boolean linear)

Helper function to convert a (non-premultiplied) Color in sRGB space to an IntArgbPre pixel value, optionally in linear RGB space. Based on the PixelConverter.ArgbPre.rgbToPixel() method.

convertSRGBtoLinearRGBback to summary
public static int convertSRGBtoLinearRGB(int color)

Helper function to convert a color component in sRGB space to linear RGB space. Copied directly from the MultipleGradientPaintContext class.

convertToIntArgbPrePixelsback to summary
private static int[] convertToIntArgbPrePixels(Color[] colors, boolean linear)

Converts the given array of Color objects into an int array containing IntArgbPre pixel values. If the linear parameter is true, the Color values will be converted into a linear RGB color space before being returned.

resetPaintback to summary
pack-priv static void resetPaint(RenderQueue rq)
setColorback to summary
private static void setColor(RenderQueue rq, int pixel)

Color support

setGradientPaintback to summary
private static void setGradientPaint(RenderQueue rq, AffineTransform at, Color c1, Color c2, Point2D pt1, Point2D pt2, boolean isCyclic, boolean useMask)

Note

This code is factored out into a separate static method so that it can be shared by both the Gradient and LinearGradient implementations. LinearGradient uses this code (for the two-color sRGB case only) because it can be much faster than the equivalent implementation that uses fragment shaders. We use OpenGL's texture coordinate generator to automatically apply a smooth gradient (either cyclic or acyclic) to the geometry being rendered. This technique is almost identical to the one described in the comments for BufferedPaints.setTexturePaint(), except the calculations take place in one dimension instead of two. Instead of an anchor rectangle in the TexturePaint case, we use the vector between the two GradientPaint end points in our calculations. The generator uses a single plane equation that takes the (x,y) location (in device space) of the fragment being rendered to calculate a (u) texture coordinate for that fragment: u = Ax + By + Cz + Dw The gradient renderer uses a two-pixel 1D texture where the first pixel contains the first GradientPaint color, and the second pixel contains the second GradientPaint color. (Note that we use the GL_CLAMP_TO_EDGE wrapping mode for acyclic gradients so that we clamp the colors properly at the extremes.) The following diagram attempts to show the layout of the texture containing the two GradientPaint colors (C1 and C2): +-----------------+ | C1 | C2 | | | | +-----------------+ u=0 .25 .5 .75 1 We calculate our plane equation constants (A,B,D) such that u=0.25 corresponds to the first GradientPaint end point in user space and u=0.75 corresponds to the second end point. This is somewhat non-obvious, but since the gradient colors are generated by interpolating between C1 and C2, we want the pure color at the end points, and we will get the pure color only when u correlates to the center of a texel. The following chart shows the expected color for some sample values of u (where C' is the color halfway between C1 and C2): u value acyclic (GL_CLAMP) cyclic (GL_REPEAT) ------- ------------------ ------------------ -0.25 C1 C2 0.0 C1 C' 0.25 C1 C1 0.5 C' C' 0.75 C2 C2 1.0 C2 C' 1.25 C2 C1 Original inspiration for this technique came from UMD's Agile2D project (GradientManager.java).

setGradientPaintback to summary
private static void setGradientPaint(RenderQueue rq, SunGraphics2D sg2d, GradientPaint paint, boolean useMask)
setLinearGradientPaintback to summary
private static void setLinearGradientPaint(RenderQueue rq, SunGraphics2D sg2d, LinearGradientPaint paint, boolean useMask)

This method uses techniques that are nearly identical to those employed in setGradientPaint() above. The primary difference is that at the native level we use a fragment shader to manually apply the plane equation constants to the current fragment position to calculate the gradient position in the range [0,1] (the native code for GradientPaint does the same, except that it uses OpenGL's automatic texture coordinate generation facilities). One other minor difference worth mentioning is that setGradientPaint() calculates the plane equation constants such that the gradient end points are positioned at 0.25 and 0.75 (for reasons discussed in the comments for that method). In contrast, for LinearGradientPaint we setup the equation constants such that the gradient end points fall at 0.0 and 1.0. The reason for this difference is that in the fragment shader we have more control over how the gradient values are interpreted (depending on the paint's CycleMethod).

setPaintback to summary
pack-priv static void setPaint(RenderQueue rq, SunGraphics2D sg2d, Paint paint, int ctxflags)
setRadialGradientPaintback to summary
private static void setRadialGradientPaint(RenderQueue rq, SunGraphics2D sg2d, RadialGradientPaint paint, boolean useMask)

This method calculates six m** values and a focusX value that are used by the native fragment shader. These techniques are based on a whitepaper by Daniel Rice on radial gradient performance (attached to the bug report for 6521533). One can refer to that document for the complete set of formulas and calculations, but the basic goal is to compose a transform that will convert an (x,y) position in device space into a "u" value that represents the relative distance to the gradient focus point. The resulting value can be used to look up the appropriate color by linearly interpolating between the two nearest colors in the gradient.

setTexturePaintback to summary
private static void setTexturePaint(RenderQueue rq, SunGraphics2D sg2d, TexturePaint paint, boolean useMask)

We use OpenGL's texture coordinate generator to automatically map the TexturePaint image to the geometry being rendered. The generator uses two separate plane equations that take the (x,y) location (in device space) of the fragment being rendered to calculate (u,v) texture coordinates for that fragment: u = Ax + By + Cz + Dw v = Ex + Fy + Gz + Hw Since we use a 2D orthographic projection, we can assume that z=0 and w=1 for any fragment. So we need to calculate appropriate values for the plane equation constants (A,B,D) and (E,F,H) such that {u,v}=0 for the top-left of the TexturePaint's anchor rectangle and {u,v}=1 for the bottom-right of the anchor rectangle. We can easily make the texture image repeat for {u,v} values outside the range [0,1] by specifying the GL_REPEAT texture wrap mode. Calculating the plane equation constants is surprisingly simple. We can think of it as an inverse matrix operation that takes device space coordinates and transforms them into user space coordinates that correspond to a location relative to the anchor rectangle. First, we translate and scale the current user space transform by applying the anchor rectangle bounds. We then take the inverse of this affine transform. The rows of the resulting inverse matrix correlate nicely to the plane equation constants we were seeking.