diff options
author | Sven Göthel <[email protected]> | 2024-01-22 06:02:30 +0100 |
---|---|---|
committer | Sven Göthel <[email protected]> | 2024-01-22 06:02:30 +0100 |
commit | bf096870c73898963558bef5c9d75760f9f76290 (patch) | |
tree | aca281df69d3974cae2c8991499e7d1654ee0c6c | |
parent | a883f3e2e1563736df32573141fd5119f0678c92 (diff) |
Bug 1490 - GraphUI Performance: Group/Scene: Use temp sorted arrays
Group/Scene's uses temp arrays for Z oder sorting,
which should be maintained locally to avoid too many temp object creations.
-rw-r--r-- | src/graphui/classes/com/jogamp/graph/ui/Group.java | 21 | ||||
-rw-r--r-- | src/graphui/classes/com/jogamp/graph/ui/Scene.java | 20 |
2 files changed, 25 insertions, 16 deletions
diff --git a/src/graphui/classes/com/jogamp/graph/ui/Group.java b/src/graphui/classes/com/jogamp/graph/ui/Group.java index e3431224c..af07d4053 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/Group.java +++ b/src/graphui/classes/com/jogamp/graph/ui/Group.java @@ -27,6 +27,7 @@ */ package com.jogamp.graph.ui; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; @@ -38,6 +39,7 @@ import com.jogamp.graph.curve.opengl.RegionRenderer; import com.jogamp.graph.ui.layout.Padding; import com.jogamp.graph.ui.shapes.Rectangle; import com.jogamp.math.FloatUtil; +import com.jogamp.math.Matrix4f; import com.jogamp.math.Vec2f; import com.jogamp.math.Vec3f; import com.jogamp.math.Vec4f; @@ -79,6 +81,7 @@ public class Group extends Shape implements Container { } private final List<Shape> shapes = new CopyOnWriteArrayList<Shape>(); + private Shape[] drawShapeArray = new Shape[0]; // reduce memory re-alloc @ display /** Enforced fixed size. In case z-axis is NaN, its 3D z-axis will be adjusted. */ private final Vec3f fixedSize = new Vec3f(); private Layout layouter; @@ -300,6 +303,8 @@ public class Group extends Shape implements Container { // s.clearImpl0(gl, renderer); s.clear(gl, renderer); } + shapes.clear(); + drawShapeArray = new Shape[0]; } @Override @@ -308,6 +313,8 @@ public class Group extends Shape implements Container { // s.destroyImpl0(gl, renderer); s.destroy(gl, renderer); } + shapes.clear(); + drawShapeArray = new Shape[0]; if( null != border ) { border.destroy(gl, renderer); border = null; @@ -322,12 +329,14 @@ public class Group extends Shape implements Container { @Override public final boolean isFrustumCullingEnabled() { return doFrustumCulling; } - @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected void drawImpl0(final GL2ES2 gl, final RegionRenderer renderer, final Vec4f rgba) { final PMVMatrix4f pmv = renderer.getMatrix(); - final Object[] shapesS = shapes.toArray(); - Arrays.sort(shapesS, (Comparator)Shape.ZAscendingComparator); + final int shapeCount = shapes.size(); + Arrays.fill(drawShapeArray, null); // flush old refs + final Shape[] shapeArray = shapes.toArray(drawShapeArray); // local-backup + drawShapeArray = shapeArray; // keep backup + Arrays.sort(shapeArray, 0, shapeCount, Shape.ZAscendingComparator); final boolean useClipFrustum = null != clipFrustum; if( useClipFrustum || clipOnBounds ) { @@ -336,9 +345,8 @@ public class Group extends Shape implements Container { final Frustum frustumMv = useClipFrustum ? clipFrustum : tempC00.set( box ).transform( pmv.getMv() ).updateFrustumPlanes(tempF00); renderer.setClipFrustum( frustumMv ); - final int shapeCount = shapesS.length; for(int i=0; i<shapeCount; i++) { - final Shape shape = (Shape) shapesS[i]; + final Shape shape = shapeArray[i]; if( shape.isVisible() ) { pmv.pushMv(); shape.applyMatToMv(pmv); @@ -356,9 +364,8 @@ public class Group extends Shape implements Container { } renderer.setClipFrustum(origClipFrustum); } else { - final int shapeCount = shapesS.length; for(int i=0; i<shapeCount; i++) { - final Shape shape = (Shape) shapesS[i]; + final Shape shape = shapeArray[i]; if( shape.isVisible() ) { pmv.pushMv(); shape.applyMatToMv(pmv); diff --git a/src/graphui/classes/com/jogamp/graph/ui/Scene.java b/src/graphui/classes/com/jogamp/graph/ui/Scene.java index 8a9952c04..b3ae5c9a0 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/Scene.java +++ b/src/graphui/classes/com/jogamp/graph/ui/Scene.java @@ -55,6 +55,7 @@ import com.jogamp.graph.curve.opengl.RegionRenderer; import com.jogamp.graph.curve.opengl.RenderState; import com.jogamp.graph.ui.Shape.Visitor2; import com.jogamp.math.FloatUtil; +import com.jogamp.math.Matrix4f; import com.jogamp.math.Ray; import com.jogamp.math.Recti; import com.jogamp.math.Vec2f; @@ -129,6 +130,7 @@ public final class Scene implements Container, GLEventListener { private static final boolean DEBUG = false; private final List<Shape> shapes = new CopyOnWriteArrayList<Shape>(); + private Shape[] displayShapeArray = new Shape[0]; // reduce memory re-alloc @ display private final AtomicReference<Tooltip> toolTipActive = new AtomicReference<Tooltip>(); private final AtomicReference<Shape> toolTipHUD = new AtomicReference<Shape>(); @@ -449,13 +451,13 @@ public final class Scene implements Container, GLEventListener { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void display(final GLAutoDrawable drawable) { - final Object[] shapesS = shapes.toArray(); - Arrays.sort(shapesS, (Comparator)Shape.ZAscendingComparator); + final int shapeCount = shapes.size(); + Arrays.fill(displayShapeArray, null); // flush old refs + final Shape[] shapeArray = shapes.toArray(displayShapeArray); // local-backup + displayShapeArray = shapeArray; // keep backup + Arrays.sort(shapeArray, 0, shapeCount, Shape.ZAscendingComparator); - display(drawable, shapesS); - } - private void display(final GLAutoDrawable drawable, final Object[] shapes) { final GL2ES2 gl = drawable.getGL().getGL2ES2(); if( null != clearColor ) { @@ -466,9 +468,8 @@ public final class Scene implements Container, GLEventListener { renderer.enable(gl, true); - final int shapeCount = shapes.length; for(int i=0; i<shapeCount; i++) { - final Shape shape = (Shape)shapes[i]; + final Shape shape = shapeArray[i]; if( shape.isVisible() ) { pmv.pushMv(); shape.applyMatToMv(pmv); @@ -571,6 +572,7 @@ public final class Scene implements Container, GLEventListener { } } shapes.clear(); + displayShapeArray = new Shape[0]; disposeActions.clear(); if( drawable == cDrawable ) { cDrawable = null; @@ -599,7 +601,7 @@ public final class Scene implements Container, GLEventListener { * Method performs on current thread and returns after probing every {@link Shape}. * </p> * @param pmv a new {@link PMVMatrix4f} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix4f, Recti) be setup}, - * {@link Shape#setTransformMv(PMVMatrix4f) shape-transformed} and can be reused by the caller and runnable. + * {@link Shape#applyMatToMv(PMVMatrix4f) shape-transformed} and can be reused by the caller and runnable. * @param glWinX window X coordinate, bottom-left origin * @param glWinY window Y coordinate, bottom-left origin * @param objPos storage for found object position in model-space of found {@link Shape} @@ -736,7 +738,7 @@ public final class Scene implements Container, GLEventListener { * @param glWinX in GL window coordinates, origin bottom-left * @param glWinY in GL window coordinates, origin bottom-left * @param pmv a new {@link PMVMatrix4f} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix4f, Recti) be setup}, - * {@link Shape#setTransformMv(PMVMatrix4f) shape-transformed} and can be reused by the caller and runnable. + * {@link Shape#applyMatToMv(PMVMatrix4f) shape-transformed} and can be reused by the caller and runnable. * @param objPos resulting object position * @param runnable action */ |