Skip to content

Commit

Permalink
GLStateTracker code review:
Browse files Browse the repository at this point in the history
 - using LinkedList as stack replacement for non index based push/pop
 - increased initial map size since the default values already exceeds the default mapsize
 - size()==0 -> isEmpty() (can be significantly faster if the deque impl would change in future)
 - map copy with initial size
 - minor other changes
  • Loading branch information
mbien committed Mar 5, 2011
1 parent b0c3311 commit 0b57c66
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/jogl/classes/jogamp/opengl/GLStateTracker.java
Expand Up @@ -39,56 +39,58 @@

package jogamp.opengl;

import java.util.List;
import java.util.ArrayList;
import javax.media.opengl.*;
import com.jogamp.common.util.IntIntHashMap;
import java.nio.IntBuffer;
import java.util.LinkedList;

/**
* Tracks as closely as possible OpenGL states.
* GLStateTracker objects are allocated on a per-OpenGL-context basis.
* <p>
* Currently supported states: PixelStorei
*/

public class GLStateTracker {
private static final boolean DEBUG = Debug.debug("GLStateTracker");

// private static final boolean DEBUG = Debug.debug("GLStateTracker");

private volatile boolean enabled = true;

private IntIntHashMap pixelStateMap;
private final LinkedList<SavedState> stack; // would be used as Deque interface in jdk6

private static class SavedState {

static class SavedState {
/**
* Empty pixel-store state
*/
SavedState() {
this.pixelStateMap = null;
}
private IntIntHashMap pixelStateMap;

/**
* set (client) pixel-store state
*/
void putPixelStateMap(IntIntHashMap pixelStateMap) {
this.pixelStateMap = new IntIntHashMap();
private void putPixelStateMap(IntIntHashMap pixelStateMap) {
//TODO add copy constructor to primitive hashmaps
this.pixelStateMap = new IntIntHashMap(Math.max(16, pixelStateMap.size()));
this.pixelStateMap.setKeyNotFoundValue(-1);
this.pixelStateMap.putAll(pixelStateMap);
}

/**
* get (client) pixel-store state
*/
IntIntHashMap getPixelStateMap() { return pixelStateMap; }
private IntIntHashMap getPixelStateMap() { return pixelStateMap; }

private IntIntHashMap pixelStateMap;
// private Map otherStateMap;
}
private List/*<SavedState>*/ stack = new ArrayList();


public GLStateTracker() {
pixelStateMap = new IntIntHashMap();

pixelStateMap = new IntIntHashMap(32);
pixelStateMap.setKeyNotFoundValue(-1);
resetStates();

stack = new LinkedList<SavedState>();
}

public void clearStates(boolean enable) {
Expand Down Expand Up @@ -119,7 +121,7 @@ public boolean getInt(int pname, int[] params, int params_offset) {

/** @return true if found in our map, otherwise false,
* which forces the caller to query GL. */
public boolean getInt(int pname, java.nio.IntBuffer params, int dummy) {
public boolean getInt(int pname, IntBuffer params, int dummy) {
if(enabled) {
int value = pixelStateMap.get(pname);
if(0 <= value) {
Expand All @@ -143,16 +145,16 @@ public void pushAttrib(int flags) {
// save client pixel-store state
state.putPixelStateMap(pixelStateMap);
}
stack.add(0, state); // push
stack.addFirst(state); // push
}
}

public void popAttrib() {
if(enabled) {
if(stack.size()==0) {
if(stack.isEmpty()) {
throw new GLException("stack contains no elements");
}
SavedState state = (SavedState) stack.remove(0); // pop
SavedState state = stack.pollFirst(); // pop
if(null==state) {
throw new GLException("null stack element (remaining stack size "+stack.size()+")");
}
Expand All @@ -168,17 +170,17 @@ private void resetStates() {
pixelStateMap.clear();

pixelStateMap.put(GL.GL_PACK_ALIGNMENT, 4);
pixelStateMap.put(GL2GL3.GL_PACK_SWAP_BYTES, 0 /* GL_FALSE */);
pixelStateMap.put(GL2GL3.GL_PACK_LSB_FIRST, 0 /* GL_FALSE */);
pixelStateMap.put(GL2GL3.GL_PACK_SWAP_BYTES, GL.GL_FALSE);
pixelStateMap.put(GL2GL3.GL_PACK_LSB_FIRST, GL.GL_FALSE);
pixelStateMap.put(GL2GL3.GL_PACK_ROW_LENGTH, 0);
pixelStateMap.put(GL2GL3.GL_PACK_SKIP_ROWS, 0);
pixelStateMap.put(GL2GL3.GL_PACK_SKIP_PIXELS, 0);
pixelStateMap.put(GL2GL3.GL_PACK_IMAGE_HEIGHT, 0);
pixelStateMap.put(GL2GL3.GL_PACK_SKIP_IMAGES, 0);

pixelStateMap.put(GL.GL_UNPACK_ALIGNMENT, 4);
pixelStateMap.put(GL2GL3.GL_UNPACK_SWAP_BYTES, 0 /* GL_FALSE */);
pixelStateMap.put(GL2GL3.GL_UNPACK_LSB_FIRST, 0 /* GL_FALSE */);
pixelStateMap.put(GL2GL3.GL_UNPACK_SWAP_BYTES, GL.GL_FALSE);
pixelStateMap.put(GL2GL3.GL_UNPACK_LSB_FIRST, GL.GL_FALSE);
pixelStateMap.put(GL2GL3.GL_UNPACK_ROW_LENGTH, 0);
pixelStateMap.put(GL2GL3.GL_UNPACK_SKIP_ROWS, 0);
pixelStateMap.put(GL2GL3.GL_UNPACK_SKIP_PIXELS, 0);
Expand Down

0 comments on commit 0b57c66

Please sign in to comment.