kis_async_merger.cpp 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright (c) Dmitry Kazakov <dimula73@gmail.com>, 2009
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kis_async_merger.h"


#include <QDebug>
#include <QBitArray>

25
#include <KoChannelInfo.h>
26
#include <KoCompositeOpRegistry.h>
27
#include <KoProgressUpdater.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <KoUpdater.h>

#include "kis_paint_device.h"
#include "kis_node_visitor.h"
#include "kis_painter.h"
#include "kis_layer.h"
#include "kis_group_layer.h"
#include "kis_adjustment_layer.h"
#include "generator/kis_generator_layer.h"
#include "kis_external_layer_iface.h"
#include "kis_paint_layer.h"
#include "filter/kis_filter.h"
#include "filter/kis_filter_configuration.h"
#include "filter/kis_filter_registry.h"
#include "kis_selection.h"
#include "kis_clone_layer.h"
#include "kis_processing_information.h"
#include "kis_node_progress_proxy.h"


#include "kis_merge_walker.h"
Dmitry Kazakov's avatar
Dmitry Kazakov committed
49
#include "kis_refresh_subtree_walker.h"
50

51 52
#include "kis_abstract_projection_plane.h"

53 54 55 56 57 58 59 60 61 62 63 64 65 66

//#define DEBUG_MERGER

#ifdef DEBUG_MERGER
#define DEBUG_NODE_ACTION(message, type, node, rect)            \
    qDebug() << message << type << ":" << node->name() << rect
#else
#define DEBUG_NODE_ACTION(message, type, node, rect)
#endif


class KisUpdateOriginalVisitor : public KisNodeVisitor
{
public:
Dmitry Kazakov's avatar
Dmitry Kazakov committed
67 68 69 70
    KisUpdateOriginalVisitor(QRect updateRect, KisPaintDeviceSP projection, QRect cropRect)
        : m_updateRect(updateRect),
          m_cropRect(cropRect),
          m_projection(projection)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        {
        }

    ~KisUpdateOriginalVisitor() {
    }

public:
    using KisNodeVisitor::visit;

    bool visit(KisAdjustmentLayer* layer) {
        if (!layer->visible()) return true;

        if (!m_projection) {
            warnImage << "ObligeChild mechanism has been activated for "
                "an adjustment layer! Do nothing...";
            layer->original()->clear();
            return true;
        }

        KisPaintDeviceSP originalDevice = layer->original();
        originalDevice->clear(m_updateRect);

        QRect applyRect = m_updateRect & m_projection->extent();

        // If the intersection of the updaterect and the projection extent is
        //      null, we are finish here.
        if(applyRect.isNull()) return true;

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        KisSafeFilterConfigurationSP filterConfig = layer->filter();
        if (!filterConfig) {
            /**
             * When an adjustment layer is just created, it may have no
             * filter inside. Then the layer has work as a pass-through
             * node. Just copy the merged data to the layer's original.
             */
            KisPainter gc(originalDevice);
            gc.setCompositeOp(COMPOSITE_COPY);
            gc.bitBlt(applyRect.topLeft(), m_projection, applyRect);
            return true;
        }

        KisFilterSP filter = KisFilterRegistry::instance()->value(filterConfig->name());
        if (!filter) return false;

115 116 117 118 119 120 121
        Q_ASSERT(layer->nodeProgressProxy());

        KoProgressUpdater updater(layer->nodeProgressProxy());
        updater.start(100, filter->name());
        QPointer<KoUpdater> updaterPtr = updater.startSubtask();

        // We do not create a transaction here, as srcDevice != dstDevice
122
        filter->process(m_projection, originalDevice, 0, applyRect, filterConfig.data(), updaterPtr);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

        updaterPtr->setProgress(100);

        return true;
    }

    bool visit(KisExternalLayer*) {
        return true;
    }

    bool visit(KisGeneratorLayer*) {
        return true;
    }

    bool visit(KisPaintLayer*) {
        return true;
    }

    bool visit(KisGroupLayer*) {
        return true;
    }

Dmitry Kazakov's avatar
Dmitry Kazakov committed
145 146 147 148 149 150 151 152 153 154 155
    bool visit(KisCloneLayer *layer) {
        QRect emptyRect;
        KisRefreshSubtreeWalker walker(emptyRect);
        KisAsyncMerger merger;

        KisLayerSP srcLayer = layer->copyFrom();
        QRect srcRect = m_updateRect.translated(-layer->x(), -layer->y());

        QRegion prepareRegion(srcRect);
        prepareRegion -= m_cropRect;

156 157 158 159 160 161 162 163 164 165 166
        QStack<QRect> applyRects;

        /**
         * If a clone has complicated masks, we should prepare additional
         * source area to ensure the rect is prepared.
         */
        QRect needRectOnSource = layer->needRectOnSourceForMasks(srcRect);
        if (!needRectOnSource.isEmpty()) {
            prepareRegion += needRectOnSource;
        }

Dmitry Kazakov's avatar
Dmitry Kazakov committed
167 168 169 170 171
        foreach(const QRect &rect, prepareRegion.rects()) {
            walker.collectRects(srcLayer, rect);
            merger.startMerge(walker, false);
        }

172 173 174 175 176 177 178 179 180
        return true;
    }

    bool visit(KisNode*) {
        return true;
    }
    bool visit(KisFilterMask*) {
        return true;
    }
181 182 183
    bool visit(KisTransformMask*) {
        return true;
    }
184 185 186 187 188 189 190 191 192
    bool visit(KisTransparencyMask*) {
        return true;
    }
    bool visit(KisSelectionMask*) {
        return true;
    }

private:
    QRect m_updateRect;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
193
    QRect m_cropRect;
194 195 196 197 198 199 200 201 202 203 204
    KisPaintDeviceSP m_projection;
};


/*********************************************************************/
/*                     KisAsyncMerger                                */
/*********************************************************************/

/**
 * FIXME: Check node<->layer transitions
 */
Dmitry Kazakov's avatar
Dmitry Kazakov committed
205
void KisAsyncMerger::startMerge(KisBaseRectsWalker &walker, bool notifyClones) {
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    KisMergeWalker::NodeStack &nodeStack = walker.nodeStack();

    const bool useTempProjections = walker.needRectVaries();

    while(!nodeStack.isEmpty()) {
        KisMergeWalker::JobItem item = nodeStack.pop();
        if(isRootNode(item.m_node)) continue;

        KisLayerSP currentNode = dynamic_cast<KisLayer*>(item.m_node.data());
        // All the masks should be filtered by the walkers
        Q_ASSERT(currentNode);

        QRect applyRect = item.m_applyRect;

        if(item.m_position & KisMergeWalker::N_EXTRA) {
            // The type of layers that will not go to projection.

            DEBUG_NODE_ACTION("Updating", "N_EXTRA", currentNode, applyRect);
            KisUpdateOriginalVisitor originalVisitor(applyRect,
Dmitry Kazakov's avatar
Dmitry Kazakov committed
225 226
                                                     m_currentProjection,
                                                     walker.cropRect());
227
            currentNode->accept(originalVisitor);
228
            currentNode->projectionPlane()->recalculate(applyRect, currentNode);
229 230 231 232 233 234 235 236 237

            continue;
        }


        if(!m_currentProjection)
            setupProjection(currentNode, applyRect, useTempProjections);

        KisUpdateOriginalVisitor originalVisitor(applyRect,
Dmitry Kazakov's avatar
Dmitry Kazakov committed
238 239
                                                 m_currentProjection,
                                                 walker.cropRect());
240 241 242 243

        if(item.m_position & KisMergeWalker::N_FILTHY) {
            DEBUG_NODE_ACTION("Updating", "N_FILTHY", currentNode, applyRect);
            currentNode->accept(originalVisitor);
244
            currentNode->projectionPlane()->recalculate(applyRect, walker.startNode());
245 246 247
        }
        else if(item.m_position & KisMergeWalker::N_ABOVE_FILTHY) {
            DEBUG_NODE_ACTION("Updating", "N_ABOVE_FILTHY", currentNode, applyRect);
Dmitry Kazakov's avatar
Dmitry Kazakov committed
248 249
            if(dependOnLowerNodes(currentNode)) {
                currentNode->accept(originalVisitor);
250
                currentNode->projectionPlane()->recalculate(applyRect, currentNode);
Dmitry Kazakov's avatar
Dmitry Kazakov committed
251
            }
252 253 254
        }
        else if(item.m_position & KisMergeWalker::N_FILTHY_PROJECTION) {
            DEBUG_NODE_ACTION("Updating", "N_FILTHY_PROJECTION", currentNode, applyRect);
255
            currentNode->projectionPlane()->recalculate(applyRect, walker.startNode());
256 257 258 259 260 261 262 263 264 265 266 267 268
        }
        else /*if(item.m_position & KisMergeWalker::N_BELOW_FILTHY)*/ {
            DEBUG_NODE_ACTION("Updating", "N_BELOW_FILTHY", currentNode, applyRect);
            /* nothing to do */
        }

        compositeWithProjection(currentNode, applyRect);

        if(item.m_position & KisMergeWalker::N_TOPMOST) {
            writeProjection(currentNode, useTempProjections, applyRect);
            resetProjection();
        }
    }
Dmitry Kazakov's avatar
Dmitry Kazakov committed
269 270 271 272

    if(notifyClones) {
        doNotifyClones(walker);
    }
273 274 275 276 277 278 279 280 281

    if(m_currentProjection) {
        warnImage << "BUG: The walker hasn't reached the root layer!";
        warnImage << "     Start node:" << walker.startNode() << "Requested rect:" << walker.requestedRect();
        warnImage << "     There must be an inconsistency in the walkers happened!";
        warnImage << "     Please report a bug describing how you got this message.";
        // reset projection to avoid artefacts in next merges and allow people to work further
        resetProjection();
    }
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
}

bool KisAsyncMerger::isRootNode(KisNodeSP node) {
    return !node->parent();
}

bool KisAsyncMerger::dependOnLowerNodes(KisNodeSP node) {
    return qobject_cast<KisAdjustmentLayer*>(node.data());
}

void KisAsyncMerger::resetProjection() {
    m_currentProjection = 0;
    m_finalProjection = 0;
}

void KisAsyncMerger::setupProjection(KisNodeSP currentNode, const QRect& rect, bool useTempProjection) {
    KisPaintDeviceSP parentOriginal = currentNode->parent()->original();

    if (parentOriginal != currentNode->projection()) {
        if (useTempProjection) {
            if(!m_cachedPaintDevice)
                m_cachedPaintDevice = new KisPaintDevice(parentOriginal->colorSpace());
            m_currentProjection = m_cachedPaintDevice;
            m_currentProjection->prepareClone(parentOriginal);
            m_finalProjection = parentOriginal;
        }
        else {
            parentOriginal->clear(rect);
            m_finalProjection = m_currentProjection = parentOriginal;
        }
    }
    else {
        /**
         * It happened so that our parent uses our own projection as
         * its original. It means obligeChild mechanism works.
         * We won't initialise m_currentProjection. This will cause
         * writeProjection() and compositeWithProjection() do nothing
         * when called.
         */
        /* NOP */
    }
}

void KisAsyncMerger::writeProjection(KisNodeSP topmostNode, bool useTempProjection, QRect rect) {
    Q_UNUSED(useTempProjection);
    Q_UNUSED(topmostNode);
    if (!m_currentProjection) return;

    if(m_currentProjection != m_finalProjection) {
        KisPainter gc(m_finalProjection);
        gc.setCompositeOp(m_finalProjection->colorSpace()->compositeOp(COMPOSITE_COPY));
        gc.bitBlt(rect.topLeft(), m_currentProjection, rect);
    }
    DEBUG_NODE_ACTION("Writing projection", "", topmostNode->parent(), rect);
}

bool KisAsyncMerger::compositeWithProjection(KisLayerSP layer, const QRect &rect) {

    if (!m_currentProjection) return true;
    if (!layer->visible()) return true;

    KisPainter gc(m_currentProjection);
344
    layer->projectionPlane()->apply(&gc, rect);
345 346 347 348

    DEBUG_NODE_ACTION("Compositing projection", "", layer, needRect);
    return true;
}
Dmitry Kazakov's avatar
Dmitry Kazakov committed
349 350 351 352 353 354 355 356 357 358 359

void KisAsyncMerger::doNotifyClones(KisBaseRectsWalker &walker) {
    KisBaseRectsWalker::CloneNotificationsVector &vector =
        walker.cloneNotifications();

    KisBaseRectsWalker::CloneNotificationsVector::iterator it;

    for(it = vector.begin(); it != vector.end(); ++it) {
        (*it).notify();
    }
}