using System;
using System.Collections.Generic;
using UnityEngine;

public class RenderTextureCache
{
	public static bool CheckSupportsRenderTextureFormat(RenderTextureFormat tex_format)
	{
		if (!RenderTextureCache.supports_format_dic_.ContainsKey(tex_format))
		{
			RenderTextureCache.supports_format_dic_.Add(tex_format, SystemInfo.SupportsRenderTextureFormat(tex_format));
		}
		return RenderTextureCache.supports_format_dic_[tex_format];
	}

	public RenderTexture GetTexture(int width, int height, RenderTextureFormat tex_format, KeyValuePair<int, Action<RenderTexture>> recreate_func)
	{
		if (!this.render_tex_dic_.ContainsKey(width))
		{
			this.render_tex_dic_.Add(width, new Dictionary<int, List<RenderTextureCache.Data>>());
		}
		Dictionary<int, List<RenderTextureCache.Data>> dictionary = this.render_tex_dic_[width];
		if (!dictionary.ContainsKey(height))
		{
			dictionary.Add(height, new List<RenderTextureCache.Data>());
		}
		List<RenderTextureCache.Data> list = dictionary[height];
		RenderTextureCache.Data data = null;
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].status == RenderTextureCache.Data.Status.Free && list[i].tex.format == tex_format)
			{
				data = list[i];
				break;
			}
		}
		if (data == null)
		{
			data = new RenderTextureCache.Data();
			list.Add(data);
			data.size = new Vector2((float)width, (float)height);
			data.tex = this.CreateTex(width, height, tex_format);
			this.render_tex_list_.Add(data);
		}
		data.recreate_func = recreate_func;
		RenderTexture active = RenderTexture.active;
		data.tex.Create();
		RenderTexture.active = active;
		data.status = RenderTextureCache.Data.Status.Use;
		return data.tex;
	}

	public void RemoveTexture(RenderTexture texture)
	{
		if (texture == null)
		{
			return;
		}
		foreach (KeyValuePair<int, Dictionary<int, List<RenderTextureCache.Data>>> keyValuePair in this.render_tex_dic_)
		{
			Dictionary<int, List<RenderTextureCache.Data>> value = keyValuePair.Value;
			foreach (KeyValuePair<int, List<RenderTextureCache.Data>> keyValuePair2 in value)
			{
				List<RenderTextureCache.Data> value2 = keyValuePair2.Value;
				for (int i = 0; i < value2.Count; i++)
				{
					if (value2[i].tex == texture)
					{
						value2[i].tex.Release();
						value2[i].recreate_func = new KeyValuePair<int, Action<RenderTexture>>(0, null);
						value2[i].status = RenderTextureCache.Data.Status.Free;
						return;
					}
				}
			}
		}
	}

	public void Update()
	{
		List<RenderTextureCache.Data> list = null;
		for (int i = 0; i < this.render_tex_list_.Count; i++)
		{
			if (this.render_tex_list_[i].status == RenderTextureCache.Data.Status.Use && !this.render_tex_list_[i].tex.IsCreated())
			{
				if (list == null)
				{
					list = new List<RenderTextureCache.Data>();
				}
				list.Add(this.render_tex_list_[i]);
			}
		}
		if (list != null)
		{
			SortedDictionary<int, List<RenderTextureCache.Data>> sortedDictionary = new SortedDictionary<int, List<RenderTextureCache.Data>>();
			for (int j = 0; j < list.Count; j++)
			{
				int key = list[j].recreate_func.Key;
				if (!sortedDictionary.ContainsKey(key))
				{
					sortedDictionary.Add(key, new List<RenderTextureCache.Data>());
				}
				sortedDictionary[key].Add(list[j]);
			}
			foreach (KeyValuePair<int, List<RenderTextureCache.Data>> keyValuePair in sortedDictionary)
			{
				List<RenderTextureCache.Data> value = keyValuePair.Value;
				for (int k = 0; k < value.Count; k++)
				{
					RenderTexture active = RenderTexture.active;
					value[k].tex.Create();
					RenderTexture.active = active;
					value[k].recreate_func.Value(value[k].tex);
				}
			}
		}
	}

	private RenderTexture CreateTex(int width, int height, RenderTextureFormat tex_format)
	{
		RenderTexture active = RenderTexture.active;
		RenderTexture renderTexture = new RenderTexture(width, height, 0, tex_format);
		renderTexture.Create();
		RenderTexture.active = active;
		return renderTexture;
	}

	private static Dictionary<RenderTextureFormat, bool> supports_format_dic_ = new Dictionary<RenderTextureFormat, bool>();

	private Dictionary<int, Dictionary<int, List<RenderTextureCache.Data>>> render_tex_dic_ = new Dictionary<int, Dictionary<int, List<RenderTextureCache.Data>>>();

	private List<RenderTextureCache.Data> render_tex_list_ = new List<RenderTextureCache.Data>();

	private class Data
	{
		public RenderTexture tex;

		public Vector2 size;

		public RenderTextureCache.Data.Status status;

		public KeyValuePair<int, Action<RenderTexture>> recreate_func;

		public enum Status
		{
			Free,
			Use
		}
	}
}
