BufferObject: find minimal buffer larger than requested size
This commit is contained in:
parent
7587c1a041
commit
913afab264
@ -22,10 +22,11 @@ import java.nio.Buffer;
|
|||||||
import javax.annotation.CheckReturnValue;
|
import javax.annotation.CheckReturnValue;
|
||||||
|
|
||||||
import org.oscim.backend.GL20;
|
import org.oscim.backend.GL20;
|
||||||
|
import org.oscim.utils.pool.Inlist;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public final class BufferObject {
|
public final class BufferObject extends Inlist<BufferObject> {
|
||||||
static final Logger log = LoggerFactory.getLogger(BufferObject.class);
|
static final Logger log = LoggerFactory.getLogger(BufferObject.class);
|
||||||
private static final int MB = 1024 * 1024;
|
private static final int MB = 1024 * 1024;
|
||||||
private static final int LIMIT_BUFFERS = 16 * MB;
|
private static final int LIMIT_BUFFERS = 16 * MB;
|
||||||
@ -41,8 +42,6 @@ public final class BufferObject {
|
|||||||
/** GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER */
|
/** GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER */
|
||||||
private int target;
|
private int target;
|
||||||
|
|
||||||
private BufferObject next;
|
|
||||||
|
|
||||||
private BufferObject(int target, int id) {
|
private BufferObject(int target, int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.target = target;
|
this.target = target;
|
||||||
@ -51,10 +50,10 @@ public final class BufferObject {
|
|||||||
public void loadBufferData(Buffer buf, int newSize) {
|
public void loadBufferData(Buffer buf, int newSize) {
|
||||||
boolean clear = false;
|
boolean clear = false;
|
||||||
|
|
||||||
if (buf.position() != 0)
|
if (buf.position() != 0) {
|
||||||
|
log.debug("flip your buffer!");
|
||||||
buf.flip();
|
buf.flip();
|
||||||
|
}
|
||||||
//throw new IllegalArgumentException("rewind buffer! " + buf.position());
|
|
||||||
|
|
||||||
GL.glBindBuffer(target, id);
|
GL.glBindBuffer(target, id);
|
||||||
|
|
||||||
@ -87,9 +86,7 @@ public final class BufferObject {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
log.debug("use: " + mBufferMemoryUsage / MB + "MB");
|
log.debug("use: " + mBufferMemoryUsage / MB + "MB");
|
||||||
|
mBufferMemoryUsage -= BufferObject.limitUsage(MB);
|
||||||
mBufferMemoryUsage -= BufferObject.limitUsage(1024 * 1024);
|
|
||||||
|
|
||||||
log.debug("now: " + mBufferMemoryUsage / MB + "MB");
|
log.debug("now: " + mBufferMemoryUsage / MB + "MB");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +99,7 @@ public final class BufferObject {
|
|||||||
|
|
||||||
if (pool[t] == null) {
|
if (pool[t] == null) {
|
||||||
if (counter[t] != 0)
|
if (counter[t] != 0)
|
||||||
throw new IllegalStateException("lost BufferObjects: " + counter[t]);
|
throw new IllegalStateException("lost objects: " + counter[t]);
|
||||||
|
|
||||||
createBuffers(target, 10);
|
createBuffers(target, 10);
|
||||||
counter[t] += 10;
|
counter[t] += 10;
|
||||||
@ -110,22 +107,26 @@ public final class BufferObject {
|
|||||||
counter[t]--;
|
counter[t]--;
|
||||||
|
|
||||||
if (size != 0) {
|
if (size != 0) {
|
||||||
// find an item that has bound more than 'size' bytes.
|
/* find the item with minimal size greater 'size' bytes. */
|
||||||
// this has the advantage that either memory can be reused or
|
BufferObject bo = pool[t];
|
||||||
// a large unused block will be replaced by a smaller one.
|
/* actually points to BufferObject before min */
|
||||||
|
BufferObject min = null;
|
||||||
BufferObject prev = null;
|
BufferObject prev = null;
|
||||||
for (BufferObject bo = pool[t]; bo != null; bo = bo.next) {
|
|
||||||
if (bo.size > size) {
|
|
||||||
if (prev == null)
|
|
||||||
pool[t] = bo.next;
|
|
||||||
else
|
|
||||||
prev.next = bo.next;
|
|
||||||
|
|
||||||
bo.next = null;
|
for (; bo != null; bo = bo.next) {
|
||||||
return bo;
|
if (bo.size > size) {
|
||||||
|
if (min == null || min.next.size > bo.size)
|
||||||
|
min = prev;
|
||||||
}
|
}
|
||||||
prev = bo;
|
prev = bo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (min != null && min != pool[t]) {
|
||||||
|
bo = min.next;
|
||||||
|
min.next = bo.next;
|
||||||
|
bo.next = null;
|
||||||
|
return bo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferObject bo = pool[t];
|
BufferObject bo = pool[t];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user