diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txt --- a/libs/ui/CMakeLists.txt +++ b/libs/ui/CMakeLists.txt @@ -161,6 +161,7 @@ opengl/kis_opengl_image_textures.cpp opengl/kis_texture_tile.cpp opengl/kis_opengl_shader_loader.cpp + opengl/kis_opengl_texture_loader.cpp opengl/kis_texture_tile_info_pool.cpp kis_fps_decoration.cpp recorder/kis_node_query_path_editor.cc diff --git a/libs/ui/kis_config.h b/libs/ui/kis_config.h --- a/libs/ui/kis_config.h +++ b/libs/ui/kis_config.h @@ -175,6 +175,9 @@ bool disableVSync(bool defaultValue = false) const; void setDisableVSync(bool disableVSync); + bool asyncTextureUpload(bool defaultValue = false) const; + void setAsyncTextureUpload(bool async); + bool showAdvancedOpenGLSettings(bool defaultValue = false) const; bool forceOpenGLFenceWorkaround(bool defaultValue = false) const; diff --git a/libs/ui/kis_config.cc b/libs/ui/kis_config.cc --- a/libs/ui/kis_config.cc +++ b/libs/ui/kis_config.cc @@ -685,6 +685,25 @@ m_cfg.writeEntry("disableVSync", disableVSync); } +bool KisConfig::asyncTextureUpload(bool defaultValue) const +{ +#ifdef Q_OS_OSX + const bool fallback = true; +#else + const bool fallback = false; +#endif + return (defaultValue ? fallback : m_cfg.readEntry("asyncTextureUpload", fallback)); +} + +void KisConfig::setAsyncTextureUpload(bool async) +{ + if (async == asyncTextureUpload(true)) { + m_cfg.deleteEntry("asyncTextureUpload"); // never write default + } else { + m_cfg.writeEntry("asyncTextureUpload", async); + } +} + bool KisConfig::showAdvancedOpenGLSettings(bool defaultValue) const { return (defaultValue ? false : m_cfg.readEntry("showAdvancedOpenGLSettings", false)); diff --git a/libs/ui/opengl/kis_opengl_canvas2.h b/libs/ui/opengl/kis_opengl_canvas2.h --- a/libs/ui/opengl/kis_opengl_canvas2.h +++ b/libs/ui/opengl/kis_opengl_canvas2.h @@ -102,6 +102,7 @@ public Q_SLOTS: void slotConfigChanged(); + void slotResumeTextureUploading(); protected: // KisCanvasWidgetBase bool callFocusNextPrevChild(bool next) override; diff --git a/libs/ui/opengl/kis_opengl_canvas2.cpp b/libs/ui/opengl/kis_opengl_canvas2.cpp --- a/libs/ui/opengl/kis_opengl_canvas2.cpp +++ b/libs/ui/opengl/kis_opengl_canvas2.cpp @@ -168,6 +168,9 @@ setDisplayFilterImpl(colorConverter->displayFilter(), true); connect(KisConfigNotifier::instance(), SIGNAL(configChanged()), SLOT(slotConfigChanged())); +#ifdef Q_OS_OSX + connect(this, SIGNAL(frameSwapped()), SLOT(slotResumeTextureUploading())); +#endif slotConfigChanged(); cfg.writeEntry("canvasState", "OPENGL_SUCCESS"); } @@ -242,7 +245,7 @@ KisConfig cfg; d->openGLImageTextures->setProofingConfig(canvas()->proofingConfiguration()); - d->openGLImageTextures->initGL(context()->functions()); + d->openGLImageTextures->initGL(context()); d->openGLImageTextures->generateCheckerTexture(createCheckersImage(cfg.checkSize())); initializeShaders(); @@ -359,6 +362,8 @@ KisOpenglCanvasDebugger::instance()->nofityPaintRequested(); + d->openGLImageTextures->getTextureLoader()->pause(); + renderCanvasGL(); if (d->glSyncObject) { @@ -366,6 +371,10 @@ } d->glSyncObject = Sync::getSync(); +#ifndef Q_OS_OSX + d->openGLImageTextures->getTextureLoader()->resume(); +#endif + QPainter gc(this); renderDecorations(&gc); gc.end(); @@ -462,7 +471,8 @@ bool KisOpenGLCanvas2::isBusy() const { - const bool isBusyStatus = Sync::syncStatus(d->glSyncObject) == Sync::Unsignaled; + bool isBusyStatus = Sync::syncStatus(d->glSyncObject) == Sync::Unsignaled; + isBusyStatus = isBusyStatus || d->openGLImageTextures->getTextureLoader()->isBusy(); KisOpenglCanvasDebugger::instance()->nofitySyncStatus(isBusyStatus); return isBusyStatus; @@ -664,6 +674,8 @@ int imageColumns = maxColumn - minColumn + 1; int imageRows = maxRow - minRow + 1; + QOpenGLFunctions *f = context()->functions(); + for (int col = firstColumn; col <= lastColumn; col++) { for (int row = firstRow; row <= lastRow; row++) { @@ -734,7 +746,7 @@ } glActiveTexture(GL_TEXTURE0); - tile->bindToActiveTexture(); + tile->bindToActiveTexture(f); if (currentLodPlane > 0) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); @@ -792,6 +804,14 @@ notifyConfigChanged(); } +void KisOpenGLCanvas2::slotResumeTextureUploading() +{ +#ifdef Q_OS_OSX + // on OS X, resuming texture uploading is only save once the whole frame swap is done. + d->openGLImageTextures->getTextureLoader()->resume(); +#endif +} + QVariant KisOpenGLCanvas2::inputMethodQuery(Qt::InputMethodQuery query) const { return processInputMethodQuery(query); @@ -884,10 +904,12 @@ * There is a bug on OSX: if we issue frame redraw before the tiles finished * uploading, the tiles will become corrupted. Depending on the GPU/driver * version either the tile itself, or its mipmaps will become totally - * transparent. + * transparent. KisOpenGLTextureLoader (if available) works around that. */ - glFinish(); + if (d->openGLImageTextures->getTextureLoader()->isBlocking()) { + glFinish(); + } #endif return QRect(); // FIXME: Implement dirty rect for OpenGL diff --git a/libs/ui/opengl/kis_opengl_image_textures.h b/libs/ui/opengl/kis_opengl_image_textures.h --- a/libs/ui/opengl/kis_opengl_image_textures.h +++ b/libs/ui/opengl/kis_opengl_image_textures.h @@ -28,10 +28,12 @@ #include "canvas/kis_update_info.h" #include "opengl/kis_texture_tile.h" +#include "opengl/kis_opengl_texture_loader.h" #include "KisProofingConfiguration.h" #include class KisOpenGLImageTextures; +class KisOpenGLTextureLoader; typedef KisSharedPtr KisOpenGLImageTexturesSP; @@ -81,7 +83,7 @@ * Complete initialization can only happen once an OpenGL context has been created. * @param f Pointer to OpenGL functions. They must already be ininitialized. */ - void initGL(QOpenGLFunctions *f); + void initGL(QOpenGLContext *context); void setChannelFlags(const QBitArray &channelFlags); void setProofingConfig(KisProofingConfigurationSP); @@ -138,6 +140,10 @@ void slotImageSizeChanged(qint32 w, qint32 h); + inline KisOpenGLTextureLoader *getTextureLoader() const { + return m_textureLoader; + } + protected: KisOpenGLImageTextures(KisImageWSP image, const KoColorProfile *monitorProfile, @@ -202,6 +208,8 @@ KisTextureTileInfoPoolSP m_infoChunksPool; + KisOpenGLTextureLoader *m_textureLoader; + private: typedef QMap ImageTexturesMap; static ImageTexturesMap imageTexturesMap; diff --git a/libs/ui/opengl/kis_opengl_image_textures.cpp b/libs/ui/opengl/kis_opengl_image_textures.cpp --- a/libs/ui/opengl/kis_opengl_image_textures.cpp +++ b/libs/ui/opengl/kis_opengl_image_textures.cpp @@ -56,6 +56,16 @@ KisOpenGLImageTextures::ImageTexturesMap KisOpenGLImageTextures::imageTexturesMap; +KisOpenGLTextureLoader *createTextureLoader() { + KisConfig cfg; + + if (cfg.asyncTextureUpload()) { + return new KisOpenGLThreadedTextureLoader(); + } else { + return new KisOpenGLBlockingTextureLoader(); + } +} + KisOpenGLImageTextures::KisOpenGLImageTextures() : m_image(0) , m_monitorProfile(0) @@ -68,6 +78,7 @@ , m_allChannelsSelected(true) , m_useOcio(false) , m_initialized(false) + , m_textureLoader(createTextureLoader()) { KisConfig cfg; m_renderingIntent = (KoColorConversionTransformation::Intent)cfg.monitorRenderIntent(); @@ -94,12 +105,23 @@ , m_allChannelsSelected(true) , m_useOcio(false) , m_initialized(false) + , m_textureLoader(createTextureLoader()) { Q_ASSERT(renderingIntent < 4); } -void KisOpenGLImageTextures::initGL(QOpenGLFunctions *f) +void KisOpenGLImageTextures::initGL(QOpenGLContext *context) { + if (!m_textureLoader->initGL(context)) { + delete m_textureLoader; + m_textureLoader = new KisOpenGLBlockingTextureLoader(); + const bool ok = m_textureLoader->initGL(context); + Q_ASSERT(ok); + } + dbgUI << "Texture loader is " << (m_textureLoader->isBlocking() ? "blocking" : "threaded"); + + QOpenGLFunctions *f = context->functions(); + if (f) { m_glFuncs = f; } else { @@ -133,6 +155,8 @@ destroyImageTextureTiles(); m_glFuncs->glDeleteTextures(1, &m_checkerTexture); + + delete m_textureLoader; } KisImageSP KisOpenGLImageTextures::image() const @@ -216,8 +240,6 @@ QOpenGLContext *ctx = QOpenGLContext::currentContext(); if (ctx) { - QOpenGLFunctions *f = ctx->functions(); - m_initialized = true; dbgUI << "OpenGL: creating texture tiles of size" << m_texturesInfo.height << "x" << m_texturesInfo.width; @@ -232,7 +254,8 @@ mode, config.useOpenGLTextureBuffer(), config.numMipmapLevels(), - f); + ctx, + this); m_textureTiles.append(tile); } } @@ -240,10 +263,14 @@ else { dbgUI << "Tried to init texture tiles without a current OpenGL Context."; } + + m_textureLoader->start(); } void KisOpenGLImageTextures::destroyImageTextureTiles() { + m_textureLoader->terminate(); + if (m_textureTiles.isEmpty()) return; Q_FOREACH (KisTextureTile *tile, m_textureTiles) { @@ -380,13 +407,7 @@ KisOpenGLUpdateInfoSP glInfo = dynamic_cast(info.data()); if(!glInfo) return; - KisTextureTileUpdateInfoSP tileInfo; - Q_FOREACH (tileInfo, glInfo->tileList) { - KisTextureTile *tile = getTextureTileCR(tileInfo->tileCol(), tileInfo->tileRow()); - KIS_ASSERT_RECOVER_RETURN(tile); - - tile->update(*tileInfo); - } + m_textureLoader->update(this, glInfo); } void KisOpenGLImageTextures::generateCheckerTexture(const QImage &checkImage) @@ -445,10 +466,14 @@ { if(m_textureTiles.isEmpty()) return; + m_textureLoader->pause(); + Q_FOREACH (KisTextureTile *tile, m_textureTiles) { tile->setUseBuffer(useBuffer); tile->setNumMipmapLevels(NumMipmapLevels); } + + m_textureLoader->resume(); } void KisOpenGLImageTextures::slotImageSizeChanged(qint32 /*w*/, qint32 /*h*/) @@ -681,4 +706,3 @@ destinationColorDepthId.id(), profile); } - diff --git a/libs/ui/opengl/kis_opengl_texture_loader.h b/libs/ui/opengl/kis_opengl_texture_loader.h new file mode 100644 --- /dev/null +++ b/libs/ui/opengl/kis_opengl_texture_loader.h @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2017 Bernhard Liebl + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __KIS_OPENGL_TEXTURE_LOADER_H +#define __KIS_OPENGL_TEXTURE_LOADER_H + +#include +#include +#include + +#include "canvas/kis_update_info.h" + +class KisOpenGLImageTextures; +class KisTextureTile; +class QThread; +class KisOpenGLTextureWorker; + +class KisOpenGLTextureLoader : public QObject +{ +Q_OBJECT +public: + virtual ~KisOpenGLTextureLoader() { } + + virtual bool initGL(QOpenGLContext *context) = 0; + + virtual void start() = 0; + virtual void terminate() = 0; + + virtual void pause() = 0; + virtual void resume() = 0; + + virtual void update(KisOpenGLImageTextures *textures, KisOpenGLUpdateInfoSP info) = 0; + + virtual bool isBlocking() const = 0; + virtual bool isPaused() const = 0; + virtual bool isBusy() const = 0; +}; + +class KisOpenGLBlockingTextureLoader : public KisOpenGLTextureLoader { +Q_OBJECT +public: + KisOpenGLBlockingTextureLoader(); + ~KisOpenGLBlockingTextureLoader() override; + + bool initGL(QOpenGLContext *context) override; + + void start() override; + void terminate() override; + + void pause() override; + void resume() override; + + void update(KisOpenGLImageTextures *textures, KisOpenGLUpdateInfoSP info) override; + + bool isBlocking() const override; + bool isPaused() const override; + bool isBusy() const override; + +private: + QOpenGLContext *m_context; +}; + +class KisOpenGLThreadedTextureLoader : public KisOpenGLTextureLoader { +Q_OBJECT +public: + KisOpenGLThreadedTextureLoader(); + ~KisOpenGLThreadedTextureLoader() override; + + bool initGL(QOpenGLContext *context) override; + + void start() override; + void terminate() override; + + void pause() override; + void resume() override; + + void update(KisOpenGLImageTextures *textures, KisOpenGLUpdateInfoSP info) override; + + bool isBlocking() const override; + bool isPaused() const override; + bool isBusy() const override; + +Q_SIGNALS: + void sigInitialize(); + void sigShutdown(); + void sigPause(); + void sigResume(); + +private: + QOpenGLContext *m_context; + QSurface *m_surface; + + QThread *m_thread; + KisOpenGLTextureWorker *m_worker; + + bool m_isInitialized; + bool m_isPaused; +}; + +class KisOpenGLTextureWorker : public QObject +{ +Q_OBJECT +public: + KisOpenGLTextureWorker(QOpenGLContext *context, QSurface *surface); + + void update(KisOpenGLImageTextures *textures, KisOpenGLUpdateInfoSP info); + + bool isBusy() const { + return m_busy.load(); + } + +Q_SIGNALS: + void sigWork(); + +protected Q_SLOTS: + void slotInitialize(); + void slotShutdown(); + void slotWork(); + void slotPause(); + void slotResume(); + +private: + struct Task { + KisTextureTile *tile; + KisTextureTileUpdateInfoSP info; + uint hash; + }; + + QOpenGLContext *m_context; + QSurface *m_surface; + + enum State { + SETUP, + READY, + PAUSED, + SHUTDOWN + }; + + QMutex m_mutex; + QQueue m_tasks; + State m_state; + QAtomicInt m_busy; +}; + +#endif /* __KIS_OPENGL_TEXTURE_LOADER_H */ diff --git a/libs/ui/opengl/kis_opengl_texture_loader.cpp b/libs/ui/opengl/kis_opengl_texture_loader.cpp new file mode 100644 --- /dev/null +++ b/libs/ui/opengl/kis_opengl_texture_loader.cpp @@ -0,0 +1,338 @@ +/* + * Copyright (c) 2017 Bernhard Liebl + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "kis_opengl_texture_loader.h" +#include "kis_opengl_image_textures.h" +#include "kis_texture_tile.h" + +#include + +#define DEBUG_DATA_CONSISTENCY 0 +#define DEBUG_PAUSE_ELAPSED 0 + +#if DEBUG_PAUSE_ELAPSED || DEBUG_DATA_CONSISTENCY +#include +#include +#endif + +KisOpenGLBlockingTextureLoader::KisOpenGLBlockingTextureLoader() : m_context(nullptr) +{ +} + +KisOpenGLBlockingTextureLoader::~KisOpenGLBlockingTextureLoader() +{ +} + +bool KisOpenGLBlockingTextureLoader::initGL(QOpenGLContext *context) +{ + m_context = context; + return true; +} + +void KisOpenGLBlockingTextureLoader::start() +{ +} + +void KisOpenGLBlockingTextureLoader::terminate() +{ +} + +void KisOpenGLBlockingTextureLoader::pause() +{ +} + +void KisOpenGLBlockingTextureLoader::resume() +{ +} + +void KisOpenGLBlockingTextureLoader::update(KisOpenGLImageTextures *textures, KisOpenGLUpdateInfoSP info) +{ + QOpenGLFunctions *f = m_context->functions(); + KIS_ASSERT_RECOVER_RETURN(f); + + Q_FOREACH (const KisTextureTileUpdateInfoSP &tileInfo, info->tileList) { + KisTextureTile *tile = textures->getTextureTileCR(tileInfo->tileCol(), tileInfo->tileRow()); + KIS_ASSERT_RECOVER_RETURN(tile); + + tile->update(f, *tileInfo); + } +} + +bool KisOpenGLBlockingTextureLoader::isBlocking() const +{ + return true; +} + +bool KisOpenGLBlockingTextureLoader::isPaused() const +{ + return true; +} + +bool KisOpenGLBlockingTextureLoader::isBusy() const +{ + return false; +} + + +// note: callers giving us KisTextureTiles must assure that KisOpenGLTextureLoader +// gets destroyed before any KisTextureTiles are destroyed. + +KisOpenGLThreadedTextureLoader::KisOpenGLThreadedTextureLoader() + : m_context(nullptr) + , m_surface(nullptr) + , m_thread(nullptr) + , m_worker(nullptr) + , m_isInitialized(false) + , m_isPaused(false) +{ +} + +KisOpenGLThreadedTextureLoader::~KisOpenGLThreadedTextureLoader() +{ + terminate(); + + if (m_context) { + delete m_context; + m_context = nullptr; + } +} + +bool KisOpenGLThreadedTextureLoader::initGL(QOpenGLContext *context) +{ + Q_ASSERT(!m_isInitialized); + + if (QOpenGLContext::supportsThreadedOpenGL()) { + QOpenGLContext *sharedContext = new QOpenGLContext(nullptr); + sharedContext->setShareContext(context); + if (sharedContext->create()) { + m_context = sharedContext; + m_surface = context->surface(); + start(); + } else { + errUI << "Could not enable async texture upload: could not create shared context."; + delete sharedContext; + return false; + } + } else { + errUI << "Could not enable async texture upload: threaded OpenGL is not supported."; + return false; + } + + m_isInitialized = true; + return true; +} + +void KisOpenGLThreadedTextureLoader::start() +{ + Q_ASSERT(!m_thread); + + m_thread = new QThread(); + m_context->moveToThread(m_thread); + + m_worker = new KisOpenGLTextureWorker(m_context, m_surface); + m_worker->moveToThread(m_thread); + + connect(this, SIGNAL(sigInitialize()), + m_worker, SLOT(slotInitialize()), Qt::BlockingQueuedConnection); + + connect(this, SIGNAL(sigShutdown()), + m_worker, SLOT(slotShutdown()), Qt::BlockingQueuedConnection); + + connect(this, SIGNAL(sigPause()), + m_worker, SLOT(slotPause()), Qt::BlockingQueuedConnection); + + connect(this, SIGNAL(sigResume()), + m_worker, SLOT(slotResume()), Qt::BlockingQueuedConnection); + + m_thread->start(); + + emit sigInitialize(); + + m_isPaused = false; +} + +void KisOpenGLThreadedTextureLoader::terminate() +{ + if (m_thread) { + emit sigShutdown(); + m_thread->quit(); + m_thread->wait(); + delete m_thread; + m_thread = nullptr; + delete m_worker; + m_worker = nullptr; + } + + m_isPaused = true; +} + +void KisOpenGLThreadedTextureLoader::update(KisOpenGLImageTextures *textures, KisOpenGLUpdateInfoSP info) +{ + KIS_SAFE_ASSERT_RECOVER_RETURN(m_worker); + m_worker->update(textures, info); +} + +void KisOpenGLThreadedTextureLoader::pause() +{ +#if DEBUG_PAUSE_ELAPSED + QElapsedTimer timer; + timer.start(); +#endif + + emit sigPause(); + +#if DEBUG_PAUSE_ELAPSED + qInfo() << "KisOpenGLTextureLoader::pause() took " << timer.nsecsElapsed() / 1000; +#endif + + m_isPaused = true; +} + +void KisOpenGLThreadedTextureLoader::resume() +{ + m_isPaused = false; + emit sigResume(); +} + +bool KisOpenGLThreadedTextureLoader::isBlocking() const { + return false; +} + +bool KisOpenGLThreadedTextureLoader::isPaused() const { + return m_isPaused; +} + +bool KisOpenGLThreadedTextureLoader::isBusy() const { + return m_worker ? m_worker->isBusy() : false; +} + +KisOpenGLTextureWorker::KisOpenGLTextureWorker(QOpenGLContext *context, QSurface *surface) + : m_context(context) + , m_surface(surface) + , m_state(SETUP) { + + connect(this, SIGNAL(sigWork()), + this, SLOT(slotWork()), Qt::QueuedConnection); +} + +void KisOpenGLTextureWorker::update(KisOpenGLImageTextures *textures, KisOpenGLUpdateInfoSP info) +{ + // needs to run in main thread because it needs getTextureTileCR. + + QMutexLocker locker(&m_mutex); + + if (m_state == SHUTDOWN) { + return; + } + + bool first = true; + + Q_FOREACH (const KisTextureTileUpdateInfoSP &tileInfo, info->tileList) { + KisTextureTile *tile = textures->getTextureTileCR(tileInfo->tileCol(), tileInfo->tileRow()); + if (!tile) { + continue; + } + + Task task; + + task.tile = tile; + task.info = tileInfo; +#if DEBUG_DATA_CONSISTENCY + task.hash = qHashBits(info->data(), info->patchPixelsLength()); +#endif + + if (first) { + if (m_tasks.empty()) { + emit sigWork(); + } + + m_busy = true; + first = false; + } + + m_tasks.enqueue(task); + } +} + +void KisOpenGLTextureWorker::slotInitialize() +{ + m_context->makeCurrent(m_surface); + Q_ASSERT(m_state == SETUP); + m_state = READY; +} + +void KisOpenGLTextureWorker::slotShutdown() +{ + m_state = SHUTDOWN; + m_busy = false; + m_context->doneCurrent(); + m_context->moveToThread(QApplication::instance()->thread()); +} + +void KisOpenGLTextureWorker::slotPause() +{ + Q_ASSERT(m_state == READY || m_state == PAUSED); + m_state = PAUSED; +} + +void KisOpenGLTextureWorker::slotResume() +{ + if (m_state == READY) { + return; + } + + Q_ASSERT(m_state == PAUSED); + m_state = READY; + + if (!m_tasks.empty()) { + emit sigWork(); + } +} + +void KisOpenGLTextureWorker::slotWork() +{ + while (m_state == READY) { + QQueue tasks; + + { + QMutexLocker locker(&m_mutex); + if (m_tasks.empty()) { + m_busy = false; + break; + } + m_tasks.swap(tasks); + + } + + Q_FOREACH (const Task &task, tasks) { + task.tile->update(m_context->functions(), *task.info); + } + +#ifdef Q_OS_OSX + m_context->functions()->glFinish(); +#endif + +#if DEBUG_DATA_CONSISTENCY + Q_FOREACH (const Task &task, tasks) { + if (task.hash != qHashBits(task.info->data(), task.info->patchPixelsLength())) { + qError() << "ERROR data in KisTextureTileUpdateInfo changed"; + } + } +#endif + } +} diff --git a/libs/ui/opengl/kis_texture_tile.h b/libs/ui/opengl/kis_texture_tile.h --- a/libs/ui/opengl/kis_texture_tile.h +++ b/libs/ui/opengl/kis_texture_tile.h @@ -22,6 +22,7 @@ #include // no forward-declaration, used to get GL* primitive types defined #include +#include #include "kis_opengl.h" @@ -29,6 +30,7 @@ #define USE_PIXEL_BUFFERS #endif +class KisOpenGLImageTextures; class KisTextureTileUpdateInfo; class QOpenGLBuffer; @@ -69,18 +71,15 @@ public: KisTextureTile(const QRect &imageRect, const KisGLTexturesInfo *texturesInfo, const QByteArray &fillData, KisOpenGL::FilterMode mode, - bool useBuffer, int numMipmapLevels, QOpenGLFunctions *f); + bool useBuffer, int numMipmapLevels, QOpenGLContext *context, + KisOpenGLImageTextures *textures); ~KisTextureTile(); - void setUseBuffer(bool useBuffer) { - m_useBuffer = useBuffer; - } + void setUseBuffer(bool useBuffer); - void setNumMipmapLevels(int num) { - m_numMipmapLevels = num; - } + void setNumMipmapLevels(int num); - void update(const KisTextureTileUpdateInfo &updateInfo); + void update(QOpenGLFunctions *f, const KisTextureTileUpdateInfo &updateInfo); inline QRect tileRectInImagePixels() { return m_tileRectInImagePixels; @@ -94,32 +93,43 @@ return m_tileRectInTexturePixels; } - void bindToActiveTexture(); + void bindToActiveTexture(QOpenGLFunctions *f); int currentLodPlane() const; private: - inline void setTextureParameters(); + inline void setTextureParameters(QOpenGLFunctions *f); void setNeedsMipmapRegeneration(); void setCurrentLodPlane(int lod); - GLuint m_textureId; + const GLuint m_textureId; #ifdef USE_PIXEL_BUFFERS void createTextureBuffer(const char*data, int size); + // accessed only from uploading thread during lifetime QOpenGLBuffer *m_glBuffer; #endif - QRect m_tileRectInImagePixels; - QRectF m_tileRectInTexturePixels; - QRect m_textureRectInImagePixels; - KisOpenGL::FilterMode m_filter; - const KisGLTexturesInfo *m_texturesInfo; + const QRect m_tileRectInImagePixels; + const QRect m_textureRectInImagePixels; + const QRectF m_tileRectInTexturePixels; + const KisOpenGL::FilterMode m_filter; + const KisGLTexturesInfo * const m_texturesInfo; + + // accessed only from uploading thread during lifetime bool m_needsMipmapRegeneration; + + // written from uploading thread, read from main thread + // only while uploading thread is paused int m_currentLodPlane; + + // the following are written in the main thread only while + // uploading thread is paused, uploading thread is reading bool m_useBuffer; int m_numMipmapLevels; - QOpenGLFunctions *f; + + QOpenGLContext * const m_creationContext; + KisOpenGLImageTextures * const m_textures; Q_DISABLE_COPY(KisTextureTile) }; diff --git a/libs/ui/opengl/kis_texture_tile.cpp b/libs/ui/opengl/kis_texture_tile.cpp --- a/libs/ui/opengl/kis_texture_tile.cpp +++ b/libs/ui/opengl/kis_texture_tile.cpp @@ -19,6 +19,7 @@ #define GL_GLEXT_PROTOTYPES #include "kis_texture_tile.h" #include "kis_texture_tile_update_info.h" +#include "kis_opengl_image_textures.h" #include #if !defined(QT_OPENGL_ES) @@ -29,7 +30,17 @@ #define GL_BGRA 0x814F #endif -void KisTextureTile::setTextureParameters() +void KisTextureTile::setUseBuffer(bool useBuffer) { + KIS_SAFE_ASSERT_RECOVER_NOOP(m_textures->getTextureLoader()->isPaused()); + m_useBuffer = useBuffer; +} + +void KisTextureTile::setNumMipmapLevels(int num) { + KIS_SAFE_ASSERT_RECOVER_NOOP(m_textures->getTextureLoader()->isPaused()); + m_numMipmapLevels = num; +} + +void KisTextureTile::setTextureParameters(QOpenGLFunctions *f) { f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -61,37 +72,42 @@ return QRectF(x, y, w, h); } +inline GLuint genTexture(QOpenGLFunctions *f) { + GLuint textureId = 0; + f->glGenTextures(1, &textureId); + return textureId; +} KisTextureTile::KisTextureTile(const QRect &imageRect, const KisGLTexturesInfo *texturesInfo, const QByteArray &fillData, KisOpenGL::FilterMode filter, - bool useBuffer, int numMipmapLevels, QOpenGLFunctions *fcn) + bool useBuffer, int numMipmapLevels, QOpenGLContext *context, + KisOpenGLImageTextures *textures) - : m_textureId(0) + : m_textureId(genTexture(context->functions())) #ifdef USE_PIXEL_BUFFERS , m_glBuffer(0) #endif , m_tileRectInImagePixels(imageRect) + , m_textureRectInImagePixels(stretchRect(m_tileRectInImagePixels, texturesInfo->border)) + , m_tileRectInTexturePixels(relativeRect(m_textureRectInImagePixels, + m_tileRectInImagePixels, + texturesInfo)) , m_filter(filter) , m_texturesInfo(texturesInfo) , m_needsMipmapRegeneration(false) , m_currentLodPlane(0) , m_useBuffer(useBuffer) , m_numMipmapLevels(numMipmapLevels) - , f(fcn) + , m_creationContext(context) + , m_textures(textures) { - const GLvoid *fd = fillData.constData(); - - m_textureRectInImagePixels = - stretchRect(m_tileRectInImagePixels, texturesInfo->border); + QOpenGLFunctions *f = m_creationContext->functions(); - m_tileRectInTexturePixels = relativeRect(m_textureRectInImagePixels, - m_tileRectInImagePixels, - m_texturesInfo); + const GLvoid *fd = fillData.constData(); - f->glGenTextures(1, &m_textureId); f->glBindTexture(GL_TEXTURE_2D, m_textureId); - setTextureParameters(); + setTextureParameters(f); #ifdef USE_PIXEL_BUFFERS createTextureBuffer(fillData.constData(), fillData.size()); @@ -117,14 +133,18 @@ KisTextureTile::~KisTextureTile() { + QOpenGLFunctions *f = m_creationContext->functions(); #ifdef USE_PIXEL_BUFFERS delete m_glBuffer; #endif - f->glDeleteTextures(1, &m_textureId); + GLuint textureId = m_textureId; + f->glDeleteTextures(1, &textureId); } -void KisTextureTile::bindToActiveTexture() +void KisTextureTile::bindToActiveTexture(QOpenGLFunctions *f) { + KIS_SAFE_ASSERT_RECOVER_NOOP(m_textures->getTextureLoader()->isPaused()); + f->glBindTexture(GL_TEXTURE_2D, m_textureId); if (m_needsMipmapRegeneration) { @@ -146,6 +166,7 @@ int KisTextureTile::currentLodPlane() const { + KIS_SAFE_ASSERT_RECOVER_NOOP(m_textures->getTextureLoader()->isPaused()); return m_currentLodPlane; } @@ -155,12 +176,11 @@ m_needsMipmapRegeneration = false; } -void KisTextureTile::update(const KisTextureTileUpdateInfo &updateInfo) +void KisTextureTile::update(QOpenGLFunctions *f, const KisTextureTileUpdateInfo &updateInfo) { - f->initializeOpenGLFunctions(); f->glBindTexture(GL_TEXTURE_2D, m_textureId); - setTextureParameters(); + setTextureParameters(f); const int patchLevelOfDetail = updateInfo.patchLevelOfDetail(); const QSize patchSize = updateInfo.realPatchSize();