/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#pragma once

#include "Base/ICloseable.hpp"
#include "MetalBackedTexture.h"

#import <CoreVideo/CVPixelBuffer.h>
#import <CoreVideo/CVMetalTextureCache.h>

namespace OpenVulkano::Vulkan
{
	class Renderer;

	class MetalTextureCache : public ICloseable
	{
		CVMetalTextureCacheRef m_textureCache = nullptr;
		Vulkan::ResourceManager* m_resourceManager = nullptr;
		std::map<void*, MetalBackedTexture> m_mtlToVkTextureMap;
		Renderer* m_renderer;

	public:
		~MetalTextureCache() { if (m_resourceManager) Close(); }

		void Init(IRenderer* renderer);

		void Close() override;

		Scene::Texture* Get(CVPixelBufferRef pixelBuffer, MTLPixelFormat pixelFormat);

		void ReturnTexture(Scene::Texture* texture);

		operator bool() const { return m_resourceManager; }
	};
}
