Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_renderer.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_scene.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheet.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsimplelevel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "trenderer.h"
Toshihiro Shimizu 890ddd
#include "toonz/scenefx.h"
Toshihiro Shimizu 890ddd
#include "toonz/sceneproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcamera.h"
Toshihiro Shimizu 890ddd
#include "toutputproperties.h"
Toshihiro Shimizu 890ddd
#include <qeventloop></qeventloop>
Toshihiro Shimizu 890ddd
#include <qwaitcondition></qwaitcondition>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "timagecache.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TScriptBinding {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=======================================================
Toshihiro Shimizu 890ddd
shun-iwasawa 27b0cf
static QScriptValue getScene(QScriptContext *context,
shun-iwasawa 27b0cf
                             const QScriptValue &sceneArg, Scene *&scene) {
Shinya Kitaoka 120a6e
  scene = qscriptvalue_cast<scene *="">(sceneArg);</scene>
Shinya Kitaoka 120a6e
  if (!scene)
Shinya Kitaoka 120a6e
    return context->throwError(
Shinya Kitaoka 120a6e
        QObject::tr("First argument must be a scene : %1")
Shinya Kitaoka 120a6e
            .arg(sceneArg.toString()));
Shinya Kitaoka 120a6e
  if (scene->getToonzScene() == 0)
Shinya Kitaoka 120a6e
    return context->throwError(QObject::tr("Can't render empty scene"));
Shinya Kitaoka 120a6e
  return QScriptValue();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Campbell Barton b3bd84
static void valueToIntList(const QScriptValue &arr, QList<int> &list) {</int>
Shinya Kitaoka 120a6e
  list.clear();
Shinya Kitaoka 120a6e
  if (arr.isArray()) {
Shinya Kitaoka 120a6e
    int length = arr.property("length").toInteger();
Shinya Kitaoka 120a6e
    for (int i = 0; i < length; i++) list.append(arr.property(i).toInteger());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=======================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class Renderer::Imp final : public TRenderPort {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TScriptBinding::Image *m_outputImage;
Shinya Kitaoka 120a6e
  TScriptBinding::Level *m_outputLevel;
Shinya Kitaoka 120a6e
  TPointD m_cameraDpi;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool m_completed;
Shinya Kitaoka 120a6e
  TRenderer m_renderer;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QList<int> m_columnList;</int>
Shinya Kitaoka 120a6e
  QList<int> m_frameList;</int>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Imp() : m_completed(false) {
Shinya Kitaoka 120a6e
    m_renderer.setThreadsCount(1);
Shinya Kitaoka 120a6e
    m_renderer.addPort(this);
Shinya Kitaoka 120a6e
    m_outputImage = 0;
Shinya Kitaoka 120a6e
    m_outputLevel = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~Imp() {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void setRenderArea(ToonzScene *scene) {
Shinya Kitaoka 120a6e
    TDimension cameraRes = scene->getCurrentCamera()->getRes();
Shinya Kitaoka 120a6e
    double rx = cameraRes.lx * 0.5, ry = cameraRes.ly * 0.5;
Shinya Kitaoka 120a6e
    TRectD renderArea(-rx, -ry, rx, ry);
Shinya Kitaoka 120a6e
    TRenderPort::setRenderArea(renderArea);
Shinya Kitaoka 120a6e
    m_cameraDpi = scene->getCurrentCamera()->getDpi();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void enableColumns(ToonzScene *scene, QList<bool> &oldStatus) {</bool>
Shinya Kitaoka 120a6e
    if (m_columnList.empty()) return;
Shinya Kitaoka 120a6e
    QList<bool> newStatus;</bool>
Shinya Kitaoka 120a6e
    TXsheet *xsh = scene->getXsheet();
Shinya Kitaoka 120a6e
    for (int i = 0; i < xsh->getColumnCount(); i++) {
Shinya Kitaoka 120a6e
      oldStatus.append(xsh->getColumn(i)->isPreviewVisible());
Shinya Kitaoka 120a6e
      newStatus.append(false);
Shinya Kitaoka 120a6e
    }
otakuto dfe1cf
    for (int i : m_columnList) {
Shinya Kitaoka 120a6e
      if (0 <= i && i < xsh->getColumnCount()) newStatus[i] = true;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    for (int i = 0; i < newStatus.length(); i++) {
Shinya Kitaoka 120a6e
      xsh->getColumn(i)->setPreviewVisible(newStatus[i]);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void restoreColumns(ToonzScene *scene, const QList<bool> &oldStatus) {</bool>
Shinya Kitaoka 120a6e
    TXsheet *xsh = scene->getXsheet();
Shinya Kitaoka 120a6e
    for (int i = 0; i < oldStatus.length(); i++) {
Shinya Kitaoka 120a6e
      xsh->getColumn(i)->setPreviewVisible(oldStatus[i]);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::vector<trenderer::renderdata> *makeRenderData(</trenderer::renderdata>
Shinya Kitaoka 120a6e
      ToonzScene *scene, const std::vector<int> &rows) {</int>
Shinya Kitaoka 120a6e
    TRenderSettings settings =
Shinya Kitaoka 120a6e
        scene->getProperties()->getOutputProperties()->getRenderSettings();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    QList<bool> oldColumnStates;</bool>
Shinya Kitaoka 120a6e
    enableColumns(scene, oldColumnStates);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    std::vector<trenderer::renderdata> *rds =</trenderer::renderdata>
Shinya Kitaoka 120a6e
        new std::vector<trenderer::renderdata>;</trenderer::renderdata>
Shinya Kitaoka 120a6e
    for (int i = 0; i < (int)rows.size(); i++) {
Shinya Kitaoka 120a6e
      double frame = rows[i];
Shinya Kitaoka 120a6e
      TFxP sceneFx = buildSceneFx(scene, frame, 1, false);
Shinya Kitaoka 120a6e
      TFxPair fxPair;
Shinya Kitaoka 120a6e
      fxPair.m_frameA = sceneFx;
Shinya Kitaoka 120a6e
      rds->push_back(TRenderer::RenderData(frame, settings, fxPair));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    restoreColumns(scene, oldColumnStates);
Shinya Kitaoka 120a6e
    return rds;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void render(std::vector<trenderer::renderdata> *rds) {</trenderer::renderdata>
Shinya Kitaoka 120a6e
    QMutex mutex;
Shinya Kitaoka 120a6e
    mutex.lock();
Shinya Kitaoka 120a6e
    m_completed = false;
Shinya Kitaoka 120a6e
    m_renderer.startRendering(rds);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (!m_completed) {
Shinya Kitaoka 120a6e
      QEventLoop loop;
Shinya Kitaoka 120a6e
      loop.processEvents();
Shinya Kitaoka 120a6e
      QWaitCondition waitCondition;
Shinya Kitaoka 120a6e
      waitCondition.wait(&mutex, 100);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    mutex.unlock();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void renderFrame(ToonzScene *scene, int row, Image *outputImage) {
Shinya Kitaoka 120a6e
    setRenderArea(scene);
Shinya Kitaoka 120a6e
    std::vector<int> rows;</int>
Shinya Kitaoka 120a6e
    rows.push_back(row);
Shinya Kitaoka 120a6e
    m_outputImage = outputImage;
Shinya Kitaoka 120a6e
    m_outputLevel = 0;
Shinya Kitaoka 120a6e
    render(makeRenderData(scene, rows));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void renderScene(ToonzScene *scene, Level *outputLevel) {
Shinya Kitaoka 120a6e
    setRenderArea(scene);
Shinya Kitaoka 120a6e
    std::vector<int> rows;</int>
Shinya Kitaoka 120a6e
    if (m_frameList.empty()) {
Shinya Kitaoka 120a6e
      for (int i = 0; i < scene->getFrameCount(); i++) rows.push_back(i);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      for (int i = 0; i < m_frameList.length(); i++)
Shinya Kitaoka 120a6e
        rows.push_back(m_frameList[i]);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    m_outputImage = 0;
Shinya Kitaoka 120a6e
    m_outputLevel = outputLevel;
Shinya Kitaoka 120a6e
    render(makeRenderData(scene, rows));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  void onRenderRasterStarted(const RenderData &renderData) override {
Shinya Kitaoka 38fd86
    int a = 1;
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  void onRenderRasterCompleted(const RenderData &renderData) override {
Shinya Kitaoka 120a6e
    TRasterP outputRaster = renderData.m_rasA;
Shinya Kitaoka 120a6e
    TRasterImageP img(outputRaster->clone());
Shinya Kitaoka 120a6e
    img->setDpi(m_cameraDpi.x, m_cameraDpi.y);
Shinya Kitaoka 120a6e
    if (m_outputImage)
Shinya Kitaoka 120a6e
      m_outputImage->setImg(img);
Shinya Kitaoka 120a6e
    else if (m_outputLevel) {
Shinya Kitaoka 120a6e
      std::vector<std::string> ids;</std::string>
Shinya Kitaoka 120a6e
      for (int i = 0; i < (int)renderData.m_frames.size(); i++) {
Shinya Kitaoka 120a6e
        TFrameId fid((int)(renderData.m_frames[i]) + 1);
Shinya Kitaoka 120a6e
        m_outputLevel->setFrame(fid, img);
Shinya Kitaoka 120a6e
        std::string id = m_outputLevel->getSimpleLevel()->getImageId(fid);
Shinya Kitaoka 120a6e
        ids.push_back(id);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      img = TImageP();
Shinya Kitaoka 120a6e
      for (int i = 0; i < (int)ids.size(); i++)
Shinya Kitaoka 120a6e
        TImageCache::instance()->compress(ids[i]);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  void onRenderFailure(const RenderData &renderData, TException &e) override {}
Shinya Kitaoka 120a6e
  void onRenderFinished() { m_completed = true; }
Shinya Kitaoka 120a6e
};  // class RenderEngine
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=======================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Renderer::Renderer() : m_imp(new Imp()) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Renderer::~Renderer() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Renderer::ctor(QScriptContext *context, QScriptEngine *engine) {
Shinya Kitaoka 120a6e
  QScriptValue r = create(engine, new Renderer());
Shinya Kitaoka 120a6e
  r.setProperty("frames", engine->newArray());
Shinya Kitaoka 120a6e
  r.setProperty("columns", engine->newArray());
Shinya Kitaoka 120a6e
  return r;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Renderer::toString() { return "Renderer"; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QScriptValue Renderer::renderScene(const QScriptValue &sceneArg) {
Shinya Kitaoka 120a6e
  QScriptValue obj = context()->thisObject();
Shinya Kitaoka 120a6e
  valueToIntList(obj.property("frames"), m_imp->m_frameList);
Shinya Kitaoka 120a6e
  valueToIntList(obj.property("columns"), m_imp->m_columnList);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Scene *scene     = 0;
Shinya Kitaoka 120a6e
  QScriptValue err = getScene(context(), sceneArg, scene);
Shinya Kitaoka 120a6e
  if (err.isError()) return err;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Level *outputLevel = new Level();
Shinya Kitaoka 120a6e
  // engine()->collectGarbage();
Shinya Kitaoka 120a6e
  m_imp->renderScene(scene->getToonzScene(), outputLevel);
Shinya Kitaoka 120a6e
  return create(engine(), outputLevel);
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
for(int row=0;row<scene->getToonzScene()->getFrameCount();row++)</scene->
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
engine()->collectGarbage();
Shinya Kitaoka 120a6e
TImageP img = renderEngine.renderFrame(row);
Shinya Kitaoka 120a6e
if(img)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  QScriptValue frame = create(new Image(img));
Shinya Kitaoka 120a6e
  QScriptValueList args; args << QString::number(row+1) << frame;
Shinya Kitaoka 120a6e
  newLevel.property("setFrame").call(newLevel, args);
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
else
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  return context()->throwError(tr("Render failed"));
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
return newLevel;
Shinya Kitaoka 120a6e
*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Q_INVOKABLE QScriptValue Renderer::renderFrame(const QScriptValue &sceneArg,
Shinya Kitaoka 120a6e
                                               int frame) {
Shinya Kitaoka 120a6e
  QScriptValue obj = context()->thisObject();
Shinya Kitaoka 120a6e
  valueToIntList(obj.property("columns"), m_imp->m_columnList);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Scene *scene     = 0;
Shinya Kitaoka 120a6e
  QScriptValue err = getScene(context(), sceneArg, scene);
Shinya Kitaoka 120a6e
  if (err.isError()) return err;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Image *outputImage = new Image();
Shinya Kitaoka 120a6e
  engine()->collectGarbage();
Shinya Kitaoka 120a6e
  m_imp->renderFrame(scene->getToonzScene(), frame, outputImage);
Shinya Kitaoka 120a6e
  return create(engine(), outputImage);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
Scene *scene = 0;
Shinya Kitaoka 120a6e
QScriptValue err = getScene(context(), sceneArg, scene);
Shinya Kitaoka 120a6e
if(err.isError()) return err;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
engine()->collectGarbage();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
RenderEngine renderEngine(scene->getToonzScene());
Shinya Kitaoka 120a6e
TImageP img = renderEngine.renderFrame(frame);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
for(int i=0;i
Shinya Kitaoka 120a6e
xsh->getColumn(i)->setPreviewVisible(oldStatus[i]);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
if(img)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
return create(engine(), new Image(img));
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
else
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
return context()->throwError(tr("Render failed"));
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
  QScriptValue Renderer::renderColumns(const QScriptValue &sceneArg, const
Shinya Kitaoka 120a6e
  QScriptValue &columnListArg)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    Scene *scene = 0;
Toshihiro Shimizu 890ddd
    QScriptValue err = getScene(context(), sceneArg, scene);
Toshihiro Shimizu 890ddd
    if(err.isError()) return err;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    QList<bool> oldStatus;</bool>
Toshihiro Shimizu 890ddd
    QList<bool> newStatus;</bool>
Toshihiro Shimizu 890ddd
    TXsheet *xsh = scene->getToonzScene()->getXsheet();
Toshihiro Shimizu 890ddd
    for(int i=0;i<xsh->getColumnCount();i++)</xsh->
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      oldStatus.append(xsh->getColumn(i)->isPreviewVisible());
Toshihiro Shimizu 890ddd
      newStatus.append(false);
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    if(!columnListArg.isArray())
Shinya Kitaoka 120a6e
      return context()->throwError(tr("Second argument must be an array of
Shinya Kitaoka 120a6e
  column indices : ").arg(columnListArg.toString()));
Toshihiro Shimizu 890ddd
    int m = columnListArg.property("length").toInt32();
Toshihiro Shimizu 890ddd
    for(quint32 i=0;i<(int)m;i++)
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      QScriptValue c = columnListArg.property(i);
Toshihiro Shimizu 890ddd
      if(!c.isNumber())
Toshihiro Shimizu 890ddd
      {
Shinya Kitaoka 120a6e
        return context()->throwError(tr("Second argument must be an array of
Shinya Kitaoka 120a6e
  integer numbers : %1 (#%2)")
Toshihiro Shimizu 890ddd
          .arg(columnListArg.toString())
Toshihiro Shimizu 890ddd
          .arg(i));
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
      int index = c.toInteger();
Toshihiro Shimizu 890ddd
      if(0<=index && index
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
    for(int i=0;i
Toshihiro Shimizu 890ddd
      xsh->getColumn(i)->setPreviewVisible(newStatus[i]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    err = QScriptValue();
Toshihiro Shimizu 890ddd
    QScriptValue newLevel = create(new Level());
Toshihiro Shimizu 890ddd
    RenderEngine renderEngine(scene->getToonzScene());
Toshihiro Shimizu 890ddd
    for(int row=0;row<scene->getToonzScene()->getFrameCount();row++)</scene->
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      engine()->collectGarbage();
Toshihiro Shimizu 890ddd
      TImageP img = renderEngine.renderFrame(row);
Toshihiro Shimizu 890ddd
      if(img)
Toshihiro Shimizu 890ddd
      {
Toshihiro Shimizu 890ddd
        QScriptValue frame = create(new Image(img));
Toshihiro Shimizu 890ddd
        QScriptValueList args; args << QString::number(row+1) << frame;
Toshihiro Shimizu 890ddd
        newLevel.property("setFrame").call(newLevel, args);
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
      else
Toshihiro Shimizu 890ddd
      {
Toshihiro Shimizu 890ddd
        err = context()->throwError(tr("Render failed"));
Toshihiro Shimizu 890ddd
        break;
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    for(int i=0;i
Toshihiro Shimizu 890ddd
      xsh->getColumn(i)->setPreviewVisible(oldStatus[i]);
Toshihiro Shimizu 890ddd
    if(err.isError()) return err;
Toshihiro Shimizu 890ddd
    else return newLevel;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Renderer::dumpCache() {
Shinya Kitaoka 120a6e
  TImageCache::instance()->outputMap(0, "C:\\Users\\gmt\\PLI\\cache.log");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace TScriptBinding