summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/platform/graphics/paint/PaintArtifactToSkCanvas.cpp
blob: 18ca92529e0ad5644c01d4e395532534ef065099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/graphics/paint/PaintArtifactToSkCanvas.h"

#include "platform/graphics/paint/ClipPaintPropertyNode.h"
#include "platform/graphics/paint/DrawingDisplayItem.h"
#include "platform/graphics/paint/EffectPaintPropertyNode.h"
#include "platform/graphics/paint/PaintArtifact.h"
#include "platform/graphics/paint/PaintChunk.h"
#include "platform/graphics/paint/TransformPaintPropertyNode.h"
#include "platform/graphics/skia/SkiaUtils.h"
#include "platform/transforms/AffineTransform.h"
#include "platform/transforms/TransformationMatrix.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"

namespace blink {

namespace {

void paintDisplayItemToSkCanvas(const DisplayItem& displayItem, SkCanvas* canvas)
{
    DisplayItem::Type type = displayItem.type();

    if (DisplayItem::isDrawingType(type)) {
        canvas->drawPicture(static_cast<const DrawingDisplayItem&>(displayItem).picture());
        return;
    }
}

} // namespace

// Compute the list of nodes from 'a' to commonAncestor(a, b) and 'b' to
// commonAncestor(a, b), exclusive.
//
// For the following tree:
//   _root_
//  |      |
//  a      d
//  |      |
//  b      e
//  |
//  c
// The common ancestor of 'c' and 'e' is 'root'. The paths would be:
// path from c to commonAncestor(c,e) = [b,a]
// path from e to commonAncestor(c,e) = [d]
static void computePathsToCommonAncestor(const EffectPaintPropertyNode* a, const EffectPaintPropertyNode* b, Vector<const EffectPaintPropertyNode*>& aToCommonAncestor, Vector<const EffectPaintPropertyNode*>& bToCommonAncestor)
{
    // Calculate the path from a -> root.
    const EffectPaintPropertyNode* current = a;
    while (current) {
        aToCommonAncestor.append(current);
        current = current->parent();
    }

    // Calculate the path from b -> root.
    current = b;
    while (current) {
        bToCommonAncestor.append(current);
        current = current->parent();
    }

    // Pop nodes from root -> a and root -> b while the last node is equal so we
    // are left with just the common ancestor path, not including the common
    // ancestor itself.
    while (aToCommonAncestor.size() && bToCommonAncestor.size() && aToCommonAncestor.last() == bToCommonAncestor.last()) {
        aToCommonAncestor.removeLast();
        bToCommonAncestor.removeLast();
    }
}

static void applyEffectNodesToCanvas(const EffectPaintPropertyNode* previousEffect, const EffectPaintPropertyNode* currentEffect, SkCanvas* canvas)
{
    if (previousEffect == currentEffect)
        return;

    Vector<const EffectPaintPropertyNode*> effectsToUnapply;
    Vector<const EffectPaintPropertyNode*> effectsToApply;
    computePathsToCommonAncestor(previousEffect, currentEffect, effectsToUnapply, effectsToApply);

    size_t popEffectCount = effectsToUnapply.size();
    for (size_t popEffect = 0; popEffect < popEffectCount; popEffect++)
        canvas->restore();

    for (Vector<const EffectPaintPropertyNode*>::reverse_iterator it = effectsToApply.rbegin(); it != effectsToApply.rend(); ++it) {
        const EffectPaintPropertyNode* node = *it;
        ASSERT(node);
        SkPaint layerPaint;
        layerPaint.setAlpha(static_cast<unsigned char>(node->opacity() * 255));
        canvas->saveLayer(nullptr, &layerPaint);
    }
}

static TransformationMatrix totalTransform(const TransformPaintPropertyNode* currentSpace)
{
    TransformationMatrix matrix;
    for (; currentSpace; currentSpace = currentSpace->parent()) {
        TransformationMatrix localMatrix = currentSpace->matrix();
        localMatrix.applyTransformOrigin(currentSpace->origin());
        matrix = localMatrix * matrix;
    }
    return matrix;
}

void paintArtifactToSkCanvas(const PaintArtifact& artifact, SkCanvas* canvas)
{
    SkAutoCanvasRestore restore(canvas, true);
    const DisplayItemList& displayItems = artifact.displayItemList();
    const EffectPaintPropertyNode* previousEffect = nullptr;
    for (const PaintChunk& chunk : artifact.paintChunks()) {
        // Setup the canvas clip state first because it clobbers matrix state.
        for (const ClipPaintPropertyNode* currentClipNode = chunk.properties.clip.get();
            currentClipNode; currentClipNode = currentClipNode->parent()) {
            canvas->setMatrix(TransformationMatrix::toSkMatrix44(totalTransform(currentClipNode->localTransformSpace())));
            canvas->clipRRect(currentClipNode->clipRect());
        }

        // Set the canvas state to match the paint properties.
        TransformationMatrix combinedMatrix = totalTransform(chunk.properties.transform.get());
        canvas->setMatrix(TransformationMatrix::toSkMatrix44(combinedMatrix));

        // Push and pop layers on the SkCanvas as necessary to implement the
        // current effect.
        // TODO(pdr): This will need to be revisited for non-opacity effects
        // such as filters which require interleaving with transforms.
        const EffectPaintPropertyNode* chunkEffect = chunk.properties.effect.get();
        applyEffectNodesToCanvas(previousEffect, chunkEffect, canvas);
        previousEffect = chunkEffect;

        // Draw the display items in the paint chunk.
        for (const auto& displayItem : displayItems.itemsInPaintChunk(chunk))
            paintDisplayItemToSkCanvas(displayItem, canvas);
    }
}

PassRefPtr<SkPicture> paintArtifactToSkPicture(const PaintArtifact& artifact, const SkRect& bounds)
{
    SkPictureRecorder recorder;
    SkCanvas* canvas = recorder.beginRecording(bounds);
    paintArtifactToSkCanvas(artifact, canvas);
    return adoptRef(recorder.endRecordingAsPicture());
}

} // namespace blink