Commit 27cc01c0 authored by Morten Sorvig's avatar Morten Sorvig
Browse files

Apply some fixes:

 - tile size calculation
 - work around QTBUG-75793
parent 8befa85d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    // we're managing the number of threads elsewhere; set the global
    // We're managing the number of threads elsewhere; set the global
    // thread limit to a large value so it won't interfere.
    QThreadPool::globalInstance()->setMaxThreadCount(1024);
    
+29 −21
Original line number Diff line number Diff line
@@ -55,6 +55,10 @@
#include <QThreadPool>
#include <cmath>

#ifdef Q_OS_WASM
#include <emscripten.h>
#endif

// runs function on a thread from the global QThreadPool, returns a
// function that can be called to vait for the function to finish.
std::function<void()> runOnThread(std::function<void()> fn)
@@ -157,6 +161,7 @@ MandelbrotRenderer::RenderPassResult MandelbrotRenderer::renderTile(int tile, in
    QSize imageSize = image->size();
    int halfWidth = imageSize.width() / 2;
    int halfHeight = imageSize.height() / 2;
    this->tileSize = (imageSize.height() + this->tileCount - 1) / this->tileCount;

    const int MaxIterations = (1 << (2 * pass + 6)) + 32;
    const int Limit = 4;
@@ -164,7 +169,13 @@ MandelbrotRenderer::RenderPassResult MandelbrotRenderer::renderTile(int tile, in
    int beginY = tileBegin(tile, imageSize.height()) - halfHeight;
    int endY = tileEnd(tile, imageSize.height()) - halfHeight;

    // qDebug() << "redner tile" << tile << tileBegin(tile, imageSize.height()) << beginY << endY << imageSize.height() << (beginY  + halfHeight) << (endY -1  + halfHeight) << QThread::currentThread();
#if 0
    qDebug() << "render tile" << tile
             << "tileBegin" << tileBegin(tile, imageSize.height()) << "tileEnd" << tileEnd(tile, imageSize.height())
             << "beginY" << beginY << "endY" << endY
             << "lines" << beginY + halfHeight << endY + halfHeight
             << "image height" << imageSize.height();
#endif

    for (int y = beginY; y < endY; ++y) {
        if (shouldAbort())
@@ -300,14 +311,13 @@ uint MandelbrotRenderer::rgbFromWaveLength(double wave)

int MandelbrotRenderer::tileBegin(int tileIndex, int totalSize)
{
    int tileSize = (totalSize / (tileCount -1));
    return tileIndex * tileSize;
    Q_UNUSED(totalSize);
    return tileIndex * this->tileSize;
}

int MandelbrotRenderer::tileEnd(int tileIndex, int totalSize)
{
    int tileSize = (totalSize / (tileCount -1));
    return (tileIndex * tileSize + tileSize) % (totalSize);
    return qMin(tileIndex * this->tileSize + this->tileSize, totalSize);
}

RenderThread::RenderThread(int threadCount, QObject *parent)
@@ -315,21 +325,19 @@ RenderThread::RenderThread(int threadCount, QObject *parent)
{
    restart = false;
    abort = false;
    
    int threadAndTileCount = threadCount;

    renderer = new MandelbrotRenderer(
        threadAndTileCount * 2,
        threadAndTileCount,
        [this]()-> bool {
    int tileCount = threadCount * 2;
    auto shouldAbort = [this]()-> bool {
        QMutexLocker locker(&mutex);
        return abort || restart;
        },
        [this](const QImage &image, double scaleFactor) {
            qDebug() << "emit renderedImage";
    };
    auto imageReady = [this](const QImage &image, double scaleFactor) {
        emit renderedImage(image.copy(), scaleFactor);
        }    
    );

#ifdef Q_OS_WASM
        MAIN_THREAD_ASYNC_EM_ASM("Browser.mainLoop.resume();");
#endif
    };
    renderer = new MandelbrotRenderer(tileCount, threadCount, shouldAbort, imageReady);
}

RenderThread::~RenderThread()
+1 −3
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ public:

private:
    int tileCount;
    int tileSize;
    int threadCount;
    std::function<bool()> shouldAbort;
    std::function<void(const QImage &, double scaleFactor)> imageReady;
@@ -92,11 +93,8 @@ class ThreadPack
{
public:
    ThreadPack(int threadCount);
    template <typename T>
    T pforeach(int begin, int end, std::function<T(int)>);
private:
    void threadFunction();
    
    int threadCount;
};