| 1 | package com.mojang.blaze3d.resource;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.ArrayDeque;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Deque;
|
| 7 | import java.util.Iterator;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class CrossFrameResourcePool implements GraphicsResourceAllocator, AutoCloseable {
|
| 13 | private final int framesToKeepResource;
|
| 14 | private final Deque<CrossFrameResourcePool.ResourceEntry<?>> pool = new ArrayDeque<>();
|
| 15 |
|
| 16 | public CrossFrameResourcePool(final int framesToKeepResource) {
|
| 17 | this.framesToKeepResource = framesToKeepResource;
|
| 18 | }
|
| 19 |
|
| 20 | public void endFrame() {
|
| 21 | Iterator<? extends CrossFrameResourcePool.ResourceEntry<?>> iterator = this.pool.iterator();
|
| 22 |
|
| 23 | while (iterator.hasNext()) {
|
| 24 | CrossFrameResourcePool.ResourceEntry<?> entry = (CrossFrameResourcePool.ResourceEntry<?>)iterator.next();
|
| 25 | if (entry.framesToLive-- == 0) {
|
| 26 | entry.close();
|
| 27 | iterator.remove();
|
| 28 | }
|
| 29 | }
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public <T> T acquire(final ResourceDescriptor<T> descriptor) {
|
| 34 | T resource = this.acquireWithoutPreparing(descriptor);
|
| 35 | descriptor.prepare(resource);
|
| 36 | return resource;
|
| 37 | }
|
| 38 |
|
| 39 | private <T> T acquireWithoutPreparing(final ResourceDescriptor<T> descriptor) {
|
| 40 | Iterator<? extends CrossFrameResourcePool.ResourceEntry<?>> iterator = this.pool.iterator();
|
| 41 |
|
| 42 | while (iterator.hasNext()) {
|
| 43 | CrossFrameResourcePool.ResourceEntry<?> entry = (CrossFrameResourcePool.ResourceEntry<?>)iterator.next();
|
| 44 | if (descriptor.canUsePhysicalResource(entry.descriptor)) {
|
| 45 | iterator.remove();
|
| 46 | return (T)entry.value;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | return descriptor.allocate();
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public <T> void release(final ResourceDescriptor<T> descriptor, final T resource) {
|
| 55 | this.pool.addFirst(new CrossFrameResourcePool.ResourceEntry<>(descriptor, resource, this.framesToKeepResource));
|
| 56 | }
|
| 57 |
|
| 58 | public void clear() {
|
| 59 | this.pool.forEach(CrossFrameResourcePool.ResourceEntry::close);
|
| 60 | this.pool.clear();
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public void close() {
|
| 65 | this.clear();
|
| 66 | }
|
| 67 |
|
| 68 | @VisibleForTesting
|
| 69 | protected Collection<CrossFrameResourcePool.ResourceEntry<?>> entries() {
|
| 70 | return this.pool;
|
| 71 | }
|
| 72 |
|
| 73 | @Environment(EnvType.CLIENT)
|
| 74 | @VisibleForTesting
|
| 75 | protected static final class ResourceEntry<T> implements AutoCloseable {
|
| 76 | private final ResourceDescriptor<T> descriptor;
|
| 77 | private final T value;
|
| 78 | private int framesToLive;
|
| 79 |
|
| 80 | private ResourceEntry(final ResourceDescriptor<T> descriptor, final T value, final int framesToLive) {
|
| 81 | this.descriptor = descriptor;
|
| 82 | this.value = value;
|
| 83 | this.framesToLive = framesToLive;
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public void close() {
|
| 88 | this.descriptor.free(this.value);
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|