OpenToonz - An open-source full-featured 2D animation creation software
C
fix ImageRasterizer::build
Shinya Kitaoka70f81d091c03
diff --git a/toonz/sources/toonz/main.cpp b/toonz/sources/toonz/main.cppindex 3852d901a..216228d43 100644--- a/toonz/sources/toonz/main.cpp+++ b/toonz/sources/toonz/main.cpp@@ -330,9 +330,6 @@ void initToonzEnv()if (cacheDir.isEmpty())cacheDir = TEnv::getStuffDir() + "cache";TImageCache::instance()->setRootDir(cacheDir);-- DV_IMPORT_API void initializeImageRasterizer();- initializeImageRasterizer();}//-----------------------------------------------------------------------------diff --git a/toonz/sources/toonzlib/imagebuilders.cpp b/toonz/sources/toonzlib/imagebuilders.cppindex 0d7ccb349..508cd6d9f 100644--- a/toonz/sources/toonzlib/imagebuilders.cpp+++ b/toonz/sources/toonzlib/imagebuilders.cpp@@ -26,8 +26,11 @@#include "toonz/fill.h"// Qt includes-#include <QGLContext>-#include <QGLPixelBuffer>+#include <QImage>+#include <QThread>+#include <QOpenGLContext>+#include <QOffscreenSurface>+#include <QOpenGLFramebufferObject>#include "imagebuilders.h"@@ -37,20 +40,6 @@extern TOfflineGL *currentOfflineGL;-//-------------------------------------------------------------------------------QGLPixelBuffer *imageRasterizerPixelBuffer = 0;--void DV_EXPORT_API initializeImageRasterizer()-{- assert(QGLPixelBuffer::hasOpenGLPbuffers());- if (!QGLPixelBuffer::hasOpenGLPbuffers()) {- imageRasterizerPixelBuffer = 0;- } else {- imageRasterizerPixelBuffer = new QGLPixelBuffer(QSize(1024, 1024));- }-}-//***************************************************************************************// ImageLoader implementation//***************************************************************************************@@ -281,54 +270,81 @@ TImageP ImageRasterizer::build(int imFlags, void *extData)TGlContext oldContext = tglGetCurrentContext();- if (imageRasterizerPixelBuffer == 0) {+ if (!QOpenGLFramebufferObject::hasOpenGLFramebufferObjects()) {TRaster32P ras(d);TRasterImageP ri(ras);ri->setOffset(off + ras->getCenter());return ri;}- QGLPixelBuffer *gl = imageRasterizerPixelBuffer; // (QSize(d.lx, d.ly));-- gl->makeCurrent();- assert(glGetError() == 0);-- glViewport(0, 0, 1024, 1024); // d.lx,d.ly);- glMatrixMode(GL_PROJECTION);- glLoadIdentity();- gluOrtho2D(0, d.lx, 0, d.ly);- glMatrixMode(GL_MODELVIEW);- glLoadIdentity();- glTranslatef(0.375, 0.375, 0.0);- glClearColor(0, 0, 0, 0);- glClear(GL_COLOR_BUFFER_BIT);-- assert(glGetError() == 0);- tglDraw(rd, vi.getPointer());- assert(glGetError() == 0);-- assert(glGetError() == 0);- glFlush();- assert(glGetError() == 0);-- QImage img = gl->toImage().scaled(QSize(d.lx, d.ly), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);- TRaster32P ras(d);-- int wrap = ras->getLx() * sizeof(TPixel32);- uchar *srcPix = img.bits();- uchar *dstPix = ras->getRawData() + wrap * (d.ly - 1);- for (int y = 0; y < d.ly; y++) {- memcpy(dstPix, srcPix, wrap);- dstPix -= wrap;- srcPix += wrap;- }- gl->doneCurrent();+ // this is too slow.+ {+ QSurfaceFormat format;+ format.setProfile(QSurfaceFormat::CompatibilityProfile);++ std::unique_ptr<QOffscreenSurface> surface(new QOffscreenSurface());+ surface->setFormat(format);+ surface->create();++ std::unique_ptr<QOpenGLContext> context(new QOpenGLContext());+ context->moveToThread(QThread::currentThread());+ context->makeCurrent(surface.get());- tglMakeCurrent(oldContext);+ TRaster32P ras(d);++ glPushAttrib(GL_ALL_ATTRIB_BITS);+ glMatrixMode(GL_MODELVIEW), glPushMatrix();+ glMatrixMode(GL_PROJECTION), glPushMatrix();+ {+ std::unique_ptr<QOpenGLFramebufferObject> fb(new QOpenGLFramebufferObject(d.lx, d.ly));++ fb->bind();+ assert(glGetError() == 0);++ glViewport(0, 0, d.lx, d.ly);+ glClearColor(0, 0, 0, 0);+ glClear(GL_COLOR_BUFFER_BIT);++ glMatrixMode(GL_PROJECTION);+ glLoadIdentity();+ gluOrtho2D(0, d.lx, 0, d.ly);++ glMatrixMode(GL_MODELVIEW);+ glLoadIdentity();+ glTranslatef(0.375, 0.375, 0.0);++ assert(glGetError() == 0);+ tglDraw(rd, vi.getPointer());+ assert(glGetError() == 0);++ assert(glGetError() == 0);+ glFlush();+ assert(glGetError() == 0);++ QImage img = fb->toImage().scaled(QSize(d.lx, d.ly), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);++ int wrap = ras->getLx() * sizeof(TPixel32);+ uchar *srcPix = img.bits();+ uchar *dstPix = ras->getRawData() + wrap * (d.ly - 1);+ for (int y = 0; y < d.ly; y++) {+ memcpy(dstPix, srcPix, wrap);+ dstPix -= wrap;+ srcPix += wrap;+ }+ fb->release();+ }+ glMatrixMode(GL_MODELVIEW), glPopMatrix();+ glMatrixMode(GL_PROJECTION), glPopMatrix();++ glPopAttrib();++ context->doneCurrent();- TRasterImageP ri = TRasterImageP(ras);- ri->setOffset(off + ras->getCenter());- return ri;+ TRasterImageP ri = TRasterImageP(ras);+ ri->setOffset(off + ras->getCenter());++ return ri;+ }}}