fix(rendering): fix GLCleaner cannot work
@@ -13,6 +13,8 @@ public final class GLCleaner {
private static final ReferenceQueue<Object> queue = new ReferenceQueue<>();
+ private static final PhantomDisposable list = new PhantomDisposable();
+
public static Disposable register(Object obj, Runnable runnable) {
return new PhantomDisposable(obj, queue, runnable);
}
@@ -55,15 +57,42 @@ public final class GLCleaner {
private final Runnable runnable;
+ private PhantomDisposable prev = this, next = this;
+
+ PhantomDisposable() {
+ super(null, null);
+ runnable = null;
+ }
+
public PhantomDisposable(Object referent, ReferenceQueue<? super Object> q, Runnable runnable) {
super(referent, q);
this.runnable = runnable;
+ insert();
}
@Override
public void dispose() {
+ if (remove()) {
clear();
runnable.run();
}
}
+
+ private void insert() {
+ prev = list;
+ next = list.next;
+ next.prev = this;
+ list.next = this;
+ }
+
+ private boolean remove() {
+ if (next == this) return false;
+
+ next.prev = prev;
+ prev.next = next;
+ prev = this;
+ next = this;
+ return true;
+ }
+ }
}
