Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Thread-safe zoom preview
Dat Le2e7505044d16
diff --git a/libs/ui/tool/KisAsyncColorSamplerHelper.cpp b/libs/ui/tool/KisAsyncColorSamplerHelper.cppindex 7a250fd107..520d1fd608 100644--- a/libs/ui/tool/KisAsyncColorSamplerHelper.cpp+++ b/libs/ui/tool/KisAsyncColorSamplerHelper.cpp@@ -727,28 +727,28 @@ QImage KisAsyncColorSamplerHelper::cacheCanvasImage(QRect &canvasPixelRect) {if (m_d->cacheCanvasPreviewRect.isEmpty() || !m_d->cacheCanvasPreviewRect.contains(canvasPixelRect)) {// Cache an area larger than the needed preview area to avoid rapid small dynamic allocations- // Attempt to reduce cache size when sample size is big to reduce delay- qreal cacheScale = 1000;- // if (canvasPixelRect.width() < 250) cacheScale = 4;- // else if (canvasPixelRect.width() < 500) cacheScale = 2;+ // And also avoid frequent preview delay from fetching canvas image asynchronously repeatedly+ qreal cacheScale = 4;QRect cacheCanvasRect = canvasPixelRect;cacheCanvasRect.setSize(canvasPixelRect.size() * cacheScale);cacheCanvasRect.moveCenter(canvasPixelRect.center());- // if cache larger than canvas, then just copy whole canvas+ // if cache requirement is larger than canvas, then just copy whole canvasif (cacheCanvasRect.width() >= canvasImage->width() || cacheCanvasRect.height() >= canvasImage->height()) {cacheCanvasRect = canvasImage->bounds();}- // If already fetching, then do nothing- if (m_d->canvasPreviewFetchingStarted) return QImage();-- m_d->canvasPreviewFetchingStarted = true;- m_d->strokesFacade()->addJob(m_d->strokeId,- new KisColorSamplerStrokeStrategy::GenerateCanvasZoomPreviewData(canvasImage, cacheCanvasRect, canvasImage->profile()));+ // If not already have a job fetching canvas image, then do it+ // TODO: There's probably a better way to do this+ if (!m_d->canvasPreviewFetchingStarted) {+ m_d->canvasPreviewFetchingStarted = true;+ m_d->strokesFacade()->addJob(m_d->strokeId,+ new KisColorSamplerStrokeStrategy::GenerateCanvasZoomPreviewData(canvasImage, cacheCanvasRect, canvasImage->profile()));+ }- return QImage();+ // Instead of returning an empty QImage and cause painful flickering+ // Just return old cache, it's not really noticeable normally. In extreme cases, we have a cool lazy loading effect :D}canvasPixelRect.translate(-m_d->cacheCanvasPreviewRect.topLeft());diff --git a/libs/ui/tool/KisAsyncColorSamplerHelper.h b/libs/ui/tool/KisAsyncColorSamplerHelper.hindex 4b0513f8b3..13a955f24c 100644--- a/libs/ui/tool/KisAsyncColorSamplerHelper.h+++ b/libs/ui/tool/KisAsyncColorSamplerHelper.h@@ -83,8 +83,9 @@ private:void paintCircle(QPainter &gc, const QRectF &viewRectF, const QColor ¤tColor, const QColor &baseColor);void paintCircleCrosshair(QPainter &gc, const QRectF &viewRectF, const QColor ¤tColor);+ // Return true if cache miss, will repaint later when canvas image is fetched+ // Return false if cache hit, and paintedbool paintCircleCanvasPreview(QPainter &gc, const QRectF &viewRectF, const QRectF &zoomDocRectF, const QPainterPath &clip);- // Return true if at least one reference image preview was drawnvoid paintCircleReferenceImagePreview(QPainter &gc, const QRectF &viewRectF, const QRectF &zoomDocRectF, const QPainterPath &clip);// Returns a QImage that should either contain the canvasPixelRectF, or the whole canvas if canvasPixelRectF > canvas size.