void SwatchCacheManager::setFx(const TFxP &fx)
{
	QMutexLocker locker(&m_mutex);

	//Update the fxs id data
	if (fx == TFxP()) {
		//Clear if no fx is set
		m_setFxId = 0;
		m_childrenFxIds.clear();
	} else {
		m_setFxId = fx->getIdentifier();
		m_childrenFxIds.clear();
		assert(m_setFxId != 0);

		TRasterFx *rfx = dynamic_cast<TRasterFx *>(fx.getPointer());
		assert(rfx);

		for (int i = 0; i < fx->getInputPortCount(); ++i) {
			//Fxs not allowing cache on the input port are skipped
			if (!rfx->allowUserCacheOnPort(i))
				continue;

			TFxPort *iport = fx->getInputPort(i);
			if (iport && iport->isConnected()) {
				TFx *child = iport->getFx();

				//In the zerary case, extract the actual fx
				TZeraryColumnFx *zcfx = dynamic_cast<TZeraryColumnFx *>(child);
				if (zcfx)
					child = zcfx->getZeraryFx();

				assert(child && child->getIdentifier() != 0);
				m_childrenFxIds.insert(child->getIdentifier());
			}
		}
	}

	//NOTE: Check if this should be avoided in some case...

	//Release the locks and clear the resources
	if (m_currEditedFxResult)
		m_currEditedFxResult->releaseLock();
	m_currEditedFxResult = TCacheResourceP();

	std::set<TCacheResourceP>::iterator it;
	for (it = m_swatchCacheContainer.begin(); it != m_swatchCacheContainer.end(); ++it)
		(*it)->releaseLock();
	m_swatchCacheContainer.clear();

#ifdef USE_SQLITE_HDPOOL
	TCacheResourcePool::instance()->releaseReferences("S");
#else
	for (it = m_genericCacheContainer.begin(); it != m_genericCacheContainer.end(); ++it)
		(*it)->releaseLock();
	m_genericCacheContainer.clear();
#endif
}
void SwatchCacheManager::getResource(
	TCacheResourceP &resource, const string &alias,
	const TFxP &fx, double frame, const TRenderSettings &rs,
	ResourceDeclaration *resData)
{
	//Only FX RESULTS are interesting - plus, avoid if we're not currently
	//editing an fx.
	if (!(fx && m_setFxId > 0))
		return;

	QMutexLocker locker(&m_mutex);

	//Cache the result in case the fx's id is among the stored ones.
	unsigned long fxId = fx->getIdentifier();

	if (fxId == m_setFxId && rs.m_isSwatch) {
		if (!resource)
			resource = TCacheResourceP(alias, true);

		resource->addLock();
		if (m_currEditedFxResult)
			m_currEditedFxResult->releaseLock();

		m_currEditedFxResult = resource;
		return;
	}

	if (m_childrenFxIds.find(fxId) != m_childrenFxIds.end()) {
		if (!resource)
			resource = TCacheResourceP(alias, true);

		if (rs.m_isSwatch) {
			std::set<TCacheResourceP>::iterator it =
				m_swatchCacheContainer.find(resource);

			if (it == m_swatchCacheContainer.end()) {
				resource->addLock();
				m_swatchCacheContainer.insert(resource);
			}
		} else {
#ifdef USE_SQLITE_HDPOOL
			resource->enableBackup();
			TCacheResourcePool::instance()->addReference(resource, "S");
#else
			std::set<TCacheResourceP>::iterator it =
				m_genericCacheContainer.find(resource);

			if (it == m_genericCacheContainer.end()) {
				resource->addLock();
				m_genericCacheContainer.insert(resource);
			}
#endif
		}
	}
}