Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/fxhistogramrender.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/histogram.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "timagecache.h"
Toshihiro Shimizu 890ddd
#include "trasterfx.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "toutputproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheethandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/tfxhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/tscenehandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevelhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/sceneproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/scenefx.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcamera.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qmetatype></qmetatype>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*******************************************************************************************************
Toshihiro Shimizu 890ddd
// FxHistogramRenderPort
Toshihiro Shimizu 890ddd
//*******************************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FxHistogramRenderPort::FxHistogramRenderPort()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FxHistogramRenderPort::~FxHistogramRenderPort()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FxHistogramRenderPort::onRenderRasterCompleted(const RenderData &renderData)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//It is necessary to clone the raster. The raster is the same for each render and
Toshihiro Shimizu 890ddd
	//each new render modify the images in the filpBooks showing old previews.
Toshihiro Shimizu 890ddd
	emit renderCompleted(renderData.m_rasA->clone(), renderData.m_renderId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*******************************************************************************************************
Toshihiro Shimizu 890ddd
// PreviewFxManager
Toshihiro Shimizu 890ddd
//*******************************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FxHistogramRender::FxHistogramRender()
Toshihiro Shimizu 890ddd
	: QObject(), m_renderer(TSystem::getProcessorCount()), m_lastFrameInfo(), m_scene(), m_histograms(), m_isCameraViewMode(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_renderPort = new FxHistogramRenderPort();
Toshihiro Shimizu 890ddd
	m_renderer.enablePrecomputing(false);
Toshihiro Shimizu 890ddd
	m_renderer.addPort(m_renderPort);
Toshihiro Shimizu 890ddd
	m_abortedRendering.clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	qRegisterMetaType<trasterp>("TRasterP");</trasterp>
Toshihiro Shimizu 890ddd
	qRegisterMetaType<uint>("UINT");</uint>
Toshihiro Shimizu 890ddd
	connect(m_renderPort, SIGNAL(renderCompleted(const TRasterP &, UINT)), this, SLOT(onRenderCompleted(const TRasterP &, UINT)));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FxHistogramRender::~FxHistogramRender()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_renderPort)
Toshihiro Shimizu 890ddd
		delete m_renderPort;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FxHistogramRender::setScene(ToonzScene *scene)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_scene = scene;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FxHistogramRender::setHistograms(Histograms *histograms)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_histograms = histograms;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FxHistogramRender::computeHistogram(TFxP fx, int frame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_histograms || !m_scene)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (!fx.getPointer()) {
Toshihiro Shimizu 890ddd
		m_histograms->setRaster(0);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TSceneProperties *sceneProperties = m_scene->getProperties();
Toshihiro Shimizu 890ddd
	if (!sceneProperties)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TOutputProperties *outputProperties = sceneProperties->getPreviewProperties();
Toshihiro Shimizu 890ddd
	if (!outputProperties)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	const TRenderSettings rs = outputProperties->getRenderSettings();
Toshihiro Shimizu 890ddd
	TFxP buildedFx;
Toshihiro Shimizu 890ddd
	if (m_isCameraViewMode)
Toshihiro Shimizu 890ddd
		buildedFx = buildPartialSceneFx(m_scene, (double)frame, fx, rs.m_shrinkX, true);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		buildedFx = buildSceneFx(m_scene, frame, fx, true);
Toshihiro Shimizu 890ddd
	if (!buildedFx.getPointer())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TRasterFxP rasterFx(buildedFx);
Toshihiro Shimizu 890ddd
	if (!rasterFx)
Toshihiro Shimizu 890ddd
		return;
Shinya Kitaoka 3bfa54
	std::string alias = rasterFx->getAlias(frame, rs);
Toshihiro Shimizu 890ddd
	if (!TImageCache::instance()->isCached(alias + ".noext" + toString(frame))) {
Toshihiro Shimizu 890ddd
		TDimension size = m_scene->getCurrentCamera()->getRes();
Toshihiro Shimizu 890ddd
		TRectD area(TPointD(-0.5 * size.lx, -0.5 * size.ly), TDimensionD(size.lx, size.ly));
Toshihiro Shimizu 890ddd
		m_renderPort->setRenderArea(area);
Toshihiro Shimizu 890ddd
		TFxPair fxPair;
Toshihiro Shimizu 890ddd
		fxPair.m_frameA = buildedFx;
Toshihiro Shimizu 890ddd
		m_lastFrameInfo.m_renderId = m_renderer.startRendering((double)frame, rs, fxPair);
Toshihiro Shimizu 890ddd
		if (m_lastFrameInfo.m_renderId == (UINT)-1)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_lastFrameInfo.m_frame = frame;
Toshihiro Shimizu 890ddd
		m_lastFrameInfo.m_fx = fx;
Toshihiro Shimizu 890ddd
		m_lastFrameInfo.m_fxAlias = alias;
Toshihiro Shimizu 890ddd
	} else {
Shinya Kitaoka 3bfa54
		std::string id = toString(fx->getIdentifier()) + ".noext" + toString(frame);
Toshihiro Shimizu 890ddd
		TRasterImageP img = TImageCache::instance()->get(id, false);
Toshihiro Shimizu 890ddd
		m_histograms->setRaster(img->getRaster());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FxHistogramRender::invalidateFrame(int frame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_histograms || !m_scene)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
	updateRenderer(frame);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FxHistogramRender::updateRenderer(int frame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_histograms || !m_scene)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (!m_lastFrameInfo.m_fx.getPointer()) {
Toshihiro Shimizu 890ddd
		m_histograms->setRaster(0);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//abort old render
Toshihiro Shimizu 890ddd
	UINT renderId = m_lastFrameInfo.m_renderId;
Toshihiro Shimizu 890ddd
	m_renderer.abortRendering(renderId);
Toshihiro Shimizu 890ddd
	m_abortedRendering.append(renderId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_scene->getFrameCount(); i++) {
Shinya Kitaoka 3bfa54
		std::string id = toString(m_lastFrameInfo.m_fx->getIdentifier()) + ".noext" + toString(i);
Toshihiro Shimizu 890ddd
		TImageCache::instance()->remove(id);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	m_lastFrameInfo.m_frame = frame;
Toshihiro Shimizu 890ddd
	remakeRender();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FxHistogramRender::remakeRender()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_histograms || !m_scene)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (!m_lastFrameInfo.m_fx.getPointer()) {
Toshihiro Shimizu 890ddd
		m_histograms->setRaster(0);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TDimension size = m_scene->getCurrentCamera()->getRes();
Toshihiro Shimizu 890ddd
	TRectD area(TPointD(-0.5 * size.lx, -0.5 * size.ly), TDimensionD(size.lx, size.ly));
Toshihiro Shimizu 890ddd
	m_renderPort->setRenderArea(area);
Toshihiro Shimizu 890ddd
	const TRenderSettings rs = m_scene->getProperties()->getPreviewProperties()->getRenderSettings();
Toshihiro Shimizu 890ddd
	TFxP buildedFx = buildPartialSceneFx(m_scene, (double)m_lastFrameInfo.m_frame, m_lastFrameInfo.m_fx, rs.m_shrinkX, true);
Toshihiro Shimizu 890ddd
	TRasterFxP rasterFx(buildedFx);
Toshihiro Shimizu 890ddd
	if (!rasterFx)
Toshihiro Shimizu 890ddd
		return;
Shinya Kitaoka 3bfa54
	std::string alias = rasterFx->getAlias(m_lastFrameInfo.m_frame, rs);
Toshihiro Shimizu 890ddd
	TFxPair fxPair;
Toshihiro Shimizu 890ddd
	fxPair.m_frameA = buildedFx;
Toshihiro Shimizu 890ddd
	m_lastFrameInfo.m_renderId = m_renderer.startRendering((double)m_lastFrameInfo.m_frame, rs, fxPair);
Toshihiro Shimizu 890ddd
	if (m_lastFrameInfo.m_renderId == (UINT)-1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_lastFrameInfo.m_fxAlias = alias;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FxHistogramRender::onRenderCompleted(const TRasterP &raster, UINT renderId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_abortedRendering.contains(renderId)) {
Toshihiro Shimizu 890ddd
		m_abortedRendering.removeAll(renderId);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
	TRasterImageP img(raster);
Shinya Kitaoka 3bfa54
	std::string id = toString(m_lastFrameInfo.m_fx->getIdentifier()) + ".noext" + toString(m_lastFrameInfo.m_frame);
Toshihiro Shimizu 890ddd
	TImageCache::instance()->add(id, img, true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_histograms->setRaster(raster);
Toshihiro Shimizu 890ddd
}