Fork me on GitHub

GLG2D OpenGL accelerated Graphics2D

Implementation Status

GLG2D is intended to speed up the most common painting operations. Not all Graphics2D methods are implemented according to the standard. In general, we try to give priority to the most commonly-used functions. If you have a request for a feature or want to report a bug, you can submit an issue on GitHub or send an email to borkhobs (at) gmail.

Below are a list of all the abstract functions in Graphics2D. Most methods are fully implemented, and some still have work left in order to behave like the Sun implementation.

draw(Shape) yes
drawString(String, int, int) yes
drawString(String, float, float) yes
drawString(AttributedCharacterIterator, int, int) does not draw attributes
drawString(AttributedCharacterIterator, float, float) does not draw attributes
drawGlyphVector(GlyphVector, float, float) not yet
fill(Shape) yes
hit(Rectangle, Shape, boolean) yes
getDeviceConfiguration() yes
getComposite() yes
setComposite(Composite) not all composite types are supported
setPaint(Paint) only accepts Color
setRenderingHint(Key, Object) accepts a subset of the rendering hints (e.g. anti-aliasing)
getRenderingHint(Key) accepts a subset of the rendering hints (e.g. anti-aliasing)
setRenderingHints(Map) accepts a subset of the rendering hints (e.g. anti-aliasing)
addRenderingHints(Map) accepts a subset of the rendering hints (e.g. anti-aliasing)
getRenderingHints() yes
translate(int, int) yes
translate(double, double) yes
rotate(double) yes
rotate(double, double, double) yes
scale(double, double) yes
shear(double, double) yes
transform(AffineTransform) yes
setTransform(AffineTransform) yes
getTransform() yes
getPaint() yes
getColor() yes
setColor(Color) yes
setBackground(Color) yes
getBackground() yes
getStroke() yes
setStroke(Stroke) yes
setPaintMode() yes
setXORMode(Color) not yet
getFont() yes
setFont(Font) yes
getFontMetrics(Font) yes
getFontRenderContext() yes
getClipBounds() yes
clip(Shape s) yes
clipRect(int, int, int, int) yes
setClip(int, int, int, int) yes
getClip() yes
setClip(Shape clipShape) yes
copyArea(int, int, int, int, int, int) yes
drawLine(int, int, int, int) yes
fillRect(int, int, int, int) yes
clearRect(int, int, int, int) yes
drawRoundRect(int, int, int, int, int, int) yes
fillRoundRect(int, int, int, int, int, int) yes
drawOval(int, int, int, int) yes
fillOval(int, int, int, int) yes
drawArc(int, int, int, int, int, int) yes
fillArc(int, int, int, int, int, int) yes
drawPolyline(int[], int[], int) yes
drawPolygon(int[], int[], int) yes
fillPolygon(int[], int[], int) yes
drawImage(Image, AffineTransform, ImageObserver) yes
drawImage(BufferedImage, BufferedImageOp, int, int) yes
drawRenderedImage(RenderedImage, AffineTransform) not yet
drawRenderableImage(RenderableImage, AffineTransform) not yet
drawImage(Image, int, int, ImageObserver) yes
drawImage(Image, int, int, Color, ImageObserver) yes
drawImage(Image, int, int, int, int, ImageObserver) yes
drawImage(Image, int, int, int, int, Color, ImageObserver) yes
drawImage(Image, int, int, int, int, int, int, int, int, ImageObserver) yes
drawImage(Image, int, int, int, int, int, int, int, int, Color) yes
create() yes
dispose() yes

Shader implementation

The initial implementation of GLG2D used just the fixed-function OpenGL pipeline. However, there are several methods in Graphics2D that need shaders. The most obvious one is Graphics2D.setPaint(Paint). A fragment shader could paint a gradient or any other kind of paint, whereas the fixed function pipeline can only paint a solid color (easily). This is why the current implementation status of setPaint(Paint) is partial.

Another interesting use of shaders is rendering a shape with a BasicStroke. The Stroke interface specifies only one method, createStrokedShape(Shape). That means theoretically you would have to tessellate every shape outline — a very expensive task! Instead, GLG2D determines if you're using a BasicStroke and uses a specialized renderer that draws the stroke using GL_QUADS. But all the vertex calculation is done on the CPU. This could be offloaded to the GPU with a geometry shader.


GLG2D — fast Java2D painting using OpenGL © Brandon Borkholder 2013