/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.google.common.annotations.VisibleForTesting */ package com.mojang.blaze3d.resource; import com.google.common.annotations.VisibleForTesting; import com.mojang.blaze3d.resource.GraphicsResourceAllocator; import com.mojang.blaze3d.resource.ResourceDescriptor; import java.util.ArrayDeque; import java.util.Collection; import java.util.Deque; import java.util.Iterator; public class CrossFrameResourcePool implements GraphicsResourceAllocator, AutoCloseable { private final int framesToKeepResource; private final Deque> pool = new ArrayDeque(); public CrossFrameResourcePool(int framesToKeepResource) { this.framesToKeepResource = framesToKeepResource; } public void endFrame() { Iterator> iterator = this.pool.iterator(); while (iterator.hasNext()) { ResourceEntry entry = iterator.next(); if (entry.framesToLive-- != 0) continue; entry.close(); iterator.remove(); } } @Override public T acquire(ResourceDescriptor descriptor) { T resource = this.acquireWithoutPreparing(descriptor); descriptor.prepare(resource); return resource; } private T acquireWithoutPreparing(ResourceDescriptor descriptor) { Iterator> iterator = this.pool.iterator(); while (iterator.hasNext()) { ResourceEntry entry = iterator.next(); if (!descriptor.canUsePhysicalResource(entry.descriptor)) continue; iterator.remove(); return entry.value; } return descriptor.allocate(); } @Override public void release(ResourceDescriptor descriptor, T resource) { this.pool.addFirst(new ResourceEntry(descriptor, resource, this.framesToKeepResource)); } public void clear() { this.pool.forEach(ResourceEntry::close); this.pool.clear(); } @Override public void close() { this.clear(); } @VisibleForTesting protected Collection> entries() { return this.pool; } @VisibleForTesting protected static final class ResourceEntry implements AutoCloseable { private final ResourceDescriptor descriptor; private final T value; private int framesToLive; private ResourceEntry(ResourceDescriptor descriptor, T value, int framesToLive) { this.descriptor = descriptor; this.value = value; this.framesToLive = framesToLive; } @Override public void close() { this.descriptor.free(this.value); } } }