Release textureFrame pool

pull/374/head
Mattia Iavarone 7 years ago
parent b4383d1871
commit 2dbc63722a
  1. 28
      cameraview/src/main/gles/com/otaliastudios/cameraview/Pool.java
  2. 3
      cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java

@ -32,38 +32,44 @@ class Pool<T> {
@Nullable @Nullable
T get() { T get() {
if (!canGet()) {
LOG.v("GET: Returning null. Too much items requested.");
return null;
}
T buffer = mQueue.poll(); T buffer = mQueue.poll();
if (buffer != null) { if (buffer != null) {
activeCount++; activeCount++; // poll decreases, this fixes
LOG.v("GET: Reusing recycled item. Count", count(), "Active", activeCount(), "Cached", cachedCount()); LOG.v("GET: Reusing recycled item.", this);
return buffer; return buffer;
} }
if (!canGet()) {
LOG.v("GET: Returning null. Too much items requested.", this);
return null;
}
activeCount++; activeCount++;
LOG.v("GET: Creating a new item. Count", count(), "Active", activeCount(), "Cached", cachedCount()); LOG.v("GET: Creating a new item.", this);
return factory.create(); return factory.create();
} }
void recycle(@NonNull T item) { void recycle(@NonNull T item) {
LOG.v("RECYCLE: Recycling item. Count", count(), "Active", activeCount(), "Cached", cachedCount()); LOG.v("RECYCLE: Recycling item.", this);
if (--activeCount < 0) { if (--activeCount < 0) {
throw new IllegalStateException("Trying to recycle an item which makes activeCount < 0." + throw new IllegalStateException("Trying to recycle an item which makes activeCount < 0." +
"This means that this or some previous items being recycled were not coming from " + "This means that this or some previous items being recycled were not coming from " +
"this pool, or some item was recycled more than once."); "this pool, or some item was recycled more than once. " + this);
} }
if (!mQueue.offer(item)) { if (!mQueue.offer(item)) {
throw new IllegalStateException("Trying to recycle an item while the queue is full. " + throw new IllegalStateException("Trying to recycle an item while the queue is full. " +
"This means that this or some previous items being recycled were not coming from " + "This means that this or some previous items being recycled were not coming from " +
"this pool, or some item was recycled more than once."); "this pool, or some item was recycled more than once. " + this);
} }
} }
@NonNull
@Override
public String toString() {
return getClass().getSimpleName() + " -- count:" + count() + ", active:" + activeCount() + ", cached:" + cachedCount();
}
final int count() { final int count() {
return activeCount() + cachedCount(); return activeCount() + cachedCount();
} }

@ -42,7 +42,7 @@ class TextureMediaEncoder extends VideoMediaEncoder<TextureMediaEncoder.Config>
private EglCore mEglCore; private EglCore mEglCore;
private EglWindowSurface mWindow; private EglWindowSurface mWindow;
private EglViewport mViewport; private EglViewport mViewport;
private Pool<TextureFrame> mFramePool = new Pool<>(30, new Pool.Factory<TextureFrame>() { private Pool<TextureFrame> mFramePool = new Pool<>(100, new Pool.Factory<TextureFrame>() {
@Override @Override
public TextureFrame create() { public TextureFrame create() {
return new TextureFrame(); return new TextureFrame();
@ -130,6 +130,7 @@ class TextureMediaEncoder extends VideoMediaEncoder<TextureMediaEncoder.Config>
mViewport.drawFrame(mConfig.textureId, transform); mViewport.drawFrame(mConfig.textureId, transform);
mWindow.setPresentationTime(frame.timestamp); mWindow.setPresentationTime(frame.timestamp);
mWindow.swapBuffers(); mWindow.swapBuffers();
mFramePool.recycle(frame);
} }
@Override @Override

Loading…
Cancel
Save