aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Image/AutoDeleteCache.cs
blob: 33ed78818accaa5804db18a5613ea0a8da60d161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Collections;
using System.Collections.Generic;

namespace Ryujinx.Graphics.Gpu.Image
{
    class AutoDeleteCache : IEnumerable<Texture>
    {
        private const int MaxCapacity = 2048;

        private LinkedList<Texture> _textures;

        public AutoDeleteCache()
        {
            _textures = new LinkedList<Texture>();
        }

        public void Add(Texture texture)
        {
            texture.IncrementReferenceCount();

            texture.CacheNode = _textures.AddLast(texture);

            if (_textures.Count > MaxCapacity)
            {
                Texture oldestTexture = _textures.First.Value;

                _textures.RemoveFirst();

                oldestTexture.DecrementReferenceCount();

                oldestTexture.CacheNode = null;
            }
        }

        public void Lift(Texture texture)
        {
            if (texture.CacheNode != null)
            {
                if (texture.CacheNode != _textures.Last)
                {
                    _textures.Remove(texture.CacheNode);

                    texture.CacheNode = _textures.AddLast(texture);
                }
            }
            else
            {
                Add(texture);
            }
        }

        public IEnumerator<Texture> GetEnumerator()
        {
            return _textures.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _textures.GetEnumerator();
        }
    }
}