Skip to content

Commit

Permalink
Small time optimization for the rendering in Java2D
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed May 16, 2021
1 parent b9a24ca commit aef68ce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 61 deletions.
103 changes: 50 additions & 53 deletions msi.gama.core/src/msi/gama/outputs/display/AWTDisplayGraphics.java
Expand Up @@ -51,6 +51,7 @@

import msi.gama.common.geometry.AxisAngle;
import msi.gama.common.geometry.GeometryUtils;
import msi.gama.common.interfaces.ILayer;
import msi.gama.metamodel.shape.GamaShape;
import msi.gama.metamodel.shape.IShape;
import msi.gama.outputs.layers.OverlayLayer;
Expand Down Expand Up @@ -80,13 +81,35 @@
* @version $Revision: 1.13 $ $Date: 2010-03-19 07:12:24 $
*/

public class AWTDisplayGraphics extends AbstractDisplayGraphics implements PointTransformation {
public class AWTDisplayGraphics extends AbstractDisplayGraphics {

private Graphics2D currentRenderer, overlayRenderer, normalRenderer;
private Rectangle2D temporaryEnvelope = null;
private final ShapeWriter sw = new ShapeWriter(this);

private static final Font defaultFont = new Font("Helvetica", Font.BOLD, 12);

private class Java2DPointTransformation implements PointTransformation {

double xOffset, yOffset, xRatio, yRatio;

@Override
public void transform(final Coordinate src, final Point2D dest) {
dest.setLocation(xOffset + xRatio * src.x, yOffset + yRatio * src.y);

}

public void adapt() {
xOffset = getXOffsetInPixels();
yOffset = getYOffsetInPixels();
xRatio = getxRatioBetweenPixelsAndModelUnits();
yRatio = getyRatioBetweenPixelsAndModelUnits();
}

}

private final Java2DPointTransformation pf = new Java2DPointTransformation();
private final ShapeWriter sw = new ShapeWriter(pf);

static {

QUALITY_RENDERING.put(KEY_RENDERING, VALUE_RENDER_QUALITY);
Expand Down Expand Up @@ -117,15 +140,9 @@ public AWTDisplayGraphics(final Graphics2D g2) {
@Override
public void dispose() {
super.dispose();
if (currentRenderer != null) {
currentRenderer.dispose();
}
if (normalRenderer != null) {
normalRenderer.dispose();
}
if (overlayRenderer != null) {
overlayRenderer.dispose();
}
if (currentRenderer != null) { currentRenderer.dispose(); }
if (normalRenderer != null) { normalRenderer.dispose(); }
if (overlayRenderer != null) { overlayRenderer.dispose(); }
}

@Override
Expand All @@ -135,41 +152,35 @@ public boolean beginDrawingLayers() {
}

@Override
public void setOpacity(final double alpha) {
super.setOpacity(alpha);
currentRenderer.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) alpha));
public void beginDrawingLayer(final ILayer layer) {
super.beginDrawingLayer(layer);
pf.adapt();
}

/**
* Implements PointTransformation.transform
*
* @see org.locationtech.jts.awt.PointTransformation#transform(org.locationtech.jts.geom.Coordinate,
* java.awt.geom.Point2D)
*/
@Override
public void transform(final Coordinate c, final Point2D p) {
p.setLocation(xFromModelUnitsToPixels(c.x), yFromModelUnitsToPixels(c.y));
public void setOpacity(final double alpha) {
super.setOpacity(alpha);
currentRenderer.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) alpha));
}

@Override
public Rectangle2D drawField(final double[] fieldValues, final FieldDrawingAttributes attributes) {
final List<?> textures = attributes.getTextures();
if (textures == null) { return null; }
if (textures == null) return null;
final Object image = textures.get(0);
if (image instanceof GamaFile) { return drawFile((GamaFile<?, ?>) image, attributes); }
if (image instanceof BufferedImage) { return drawImage((BufferedImage) image, attributes); }
if (image instanceof GamaFile) return drawFile((GamaFile<?, ?>) image, attributes);
if (image instanceof BufferedImage) return drawImage((BufferedImage) image, attributes);
return null;
}

@Override
public Rectangle2D drawFile(final GamaFile<?, ?> file, final DrawingAttributes attributes) {
final IScope scope = surface.getScope();
if (file instanceof GamaImageFile) {
if (file instanceof GamaImageFile)
return drawImage(((GamaImageFile) file).getImage(scope, attributes.useCache()), attributes);
}
if (!(file instanceof GamaGeometryFile)) { return null; }
if (!(file instanceof GamaGeometryFile)) return null;
IShape shape = Cast.asGeometry(scope, file);
if (shape == null) { return null; }
if (shape == null) return null;
final AxisAngle rotation = attributes.getRotation();
shape = new GamaShape(shape, null, rotation, attributes.getLocation(), attributes.getSize(), true);
final GamaColor c = attributes.getColor();
Expand Down Expand Up @@ -210,14 +221,10 @@ public Rectangle2D drawImage(final BufferedImage img, final DrawingAttributes at
currentRenderer.drawImage(img, imageTransform, null);
// currentRenderer.drawImage(img, (int) FastMath.round(curX), (int) FastMath.round(curY), (int) curWidth,
// (int) curHeight, null);
if (attributes.getBorder() != null) {
drawGridLine(img, attributes.getBorder());
}
if (attributes.getBorder() != null) { drawGridLine(img, attributes.getBorder()); }
// currentRenderer.setTransform(saved);
rect.setRect(curX, curY, curWidth, curHeight);
if (highlight) {
highlightRectangleInPixels(rect);
}
if (highlight) { highlightRectangleInPixels(rect); }
return rect.getBounds2D();
}

Expand Down Expand Up @@ -287,7 +294,7 @@ private void setFont(final Font f) {

@Override
public Rectangle2D drawShape(final Geometry geometry, final DrawingAttributes attributes) {
if (geometry == null) { return null; }
if (geometry == null) return null;
if (geometry instanceof GeometryCollection) {
final Rectangle2D result = new Rectangle2D.Double();
GeometryUtils.applyToInnerGeometries(geometry, (g) -> result.add(drawShape(g, attributes)));
Expand All @@ -296,26 +303,18 @@ public Rectangle2D drawShape(final Geometry geometry, final DrawingAttributes at
final boolean isLine = geometry instanceof Lineal || geometry instanceof Puntal;

GamaColor border = isLine ? attributes.getColor() : attributes.getBorder();
if (border == null && attributes.isEmpty()) {
border = attributes.getColor();
}
if (border == null && attributes.isEmpty()) { border = attributes.getColor(); }
if (highlight) {
attributes.setFill(GamaColor.getInt(data.getHighlightColor().getRGB()));
if (border != null) {
border = attributes.getColor();
}
if (border != null) { border = attributes.getColor(); }
}
final Shape s = sw.toShape(geometry);
try {
final Rectangle2D r = s.getBounds2D();
currentRenderer.setColor(attributes.getColor());
if (!isLine && !attributes.isEmpty()) {
currentRenderer.fill(s);
}
if (!isLine && !attributes.isEmpty()) { currentRenderer.fill(s); }
if (isLine || border != null || attributes.isEmpty()) {
if (border != null) {
currentRenderer.setColor(border);
}
if (border != null) { currentRenderer.setColor(border); }
currentRenderer.draw(s);
}
return r;
Expand All @@ -337,7 +336,7 @@ public void drawGridLine(final BufferedImage image, final Color lineColor) {
// The image contains the dimensions of the grid.
final double stepx = (double) getLayerWidth() / (double) image.getWidth();
final double stepy = (double) getLayerHeight() / (double) image.getHeight();
if (stepx < 2 || stepy < 2) { return; }
if (stepx < 2 || stepy < 2) return;
final Line2D line = new Line2D.Double();
currentRenderer.setColor(lineColor);
for (double step = 0.0, end = getLayerWidth(); step < end + 1; step += stepx) {
Expand All @@ -361,7 +360,7 @@ public void drawGridLine(final BufferedImage image, final Color lineColor) {
}

private void highlightRectangleInPixels(final Rectangle2D r) {
if (r == null) { return; }
if (r == null) return;
final Stroke oldStroke = currentRenderer.getStroke();
currentRenderer.setStroke(new BasicStroke(5));
final Color old = currentRenderer.getColor();
Expand Down Expand Up @@ -412,9 +411,7 @@ public void endOverlay() {
public void setGraphics2D(final Graphics2D g) {
normalRenderer = g;
currentRenderer = g;
if (g != null) {
setFont(defaultFont);
}
if (g != null) { setFont(defaultFont); }
}

public void setUntranslatedGraphics2D(final Graphics2D g) {
Expand Down
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* msi.gama.outputs.display.AbstractDisplayGraphics.java, in plugin msi.gama.core,
* is part of the source code of the GAMA modeling and simulation platform (v. 1.8.1)
* msi.gama.outputs.display.AbstractDisplayGraphics.java, in plugin msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gama.outputs.display;

Expand Down Expand Up @@ -82,19 +82,18 @@ protected final double hFromModelUnitsToPixels(final double mu) {

@Override
public double getxRatioBetweenPixelsAndModelUnits() {
if (currentLayer == null) { return getDisplayWidth() / data.getEnvWidth(); }
if (currentLayer == null) return getDisplayWidth() / data.getEnvWidth();
return currentLayer.getData().getSizeInPixels().x / data.getEnvWidth();
}

@Override
public double getyRatioBetweenPixelsAndModelUnits() {
if (currentLayer == null) {
if (currentLayer == null)
return getDisplayHeight() / data.getEnvHeight();
} else if (currentLayer instanceof OverlayLayer) {
else if (currentLayer instanceof OverlayLayer)
return getxRatioBetweenPixelsAndModelUnits();
} else {
else
return currentLayer.getData().getSizeInPixels().y / data.getEnvHeight();
}
}

@Override
Expand All @@ -121,6 +120,7 @@ public void endOverlay() {}
@Override
public void beginDrawingLayer(final ILayer layer) {
currentLayer = layer;

}

@Override
Expand Down

0 comments on commit aef68ce

Please sign in to comment.