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