File: SVGRenderingContext.cpp

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (237 lines) | stat: -rw-r--r-- 9,939 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) 2007, 2008 Rob Buis <buis@kde.org>
 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
 * Copyright (C) 2009 Google, Inc.  All rights reserved.
 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
 *
 * 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 "config.h"
#include "core/rendering/svg/SVGRenderingContext.h"

#include "core/frame/FrameHost.h"
#include "core/rendering/PaintInfo.h"
#include "core/rendering/RenderLayer.h"
#include "core/rendering/svg/RenderSVGResourceFilter.h"
#include "core/rendering/svg/RenderSVGResourceMasker.h"
#include "core/rendering/svg/SVGRenderSupport.h"
#include "core/rendering/svg/SVGResources.h"
#include "core/rendering/svg/SVGResourcesCache.h"
#include "platform/FloatConversion.h"

namespace blink {

SVGRenderingContext::~SVGRenderingContext()
{
    ASSERT(m_object && m_paintInfo);

    if (m_renderingFlags & PostApplyResources) {
        ASSERT(m_masker || m_clipper || m_filter);
        ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object));

        if (m_filter) {
            ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->filter() == m_filter);
            m_filter->finishEffect(m_object, m_paintInfo->context);
            m_paintInfo->rect = m_savedPaintRect;
        }

        if (m_clipper) {
            ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->clipper() == m_clipper);
            m_clipper->postApplyStatefulResource(m_object, m_paintInfo->context, m_clipperState);
        }

        if (m_masker) {
            ASSERT(SVGResourcesCache::cachedResourcesForRenderObject(m_object)->masker() == m_masker);
            m_masker->finishEffect(m_object, m_paintInfo->context);
        }
    }
}

void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo)
{
    ASSERT(object);

#if ENABLE(ASSERT)
    // This function must not be called twice!
    ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled));
    m_renderingFlags |= PrepareToRenderSVGContentWasCalled;
#endif

    m_object = object;
    m_paintInfo = &paintInfo;

    RenderStyle* style = m_object->style();
    ASSERT(style);

    const SVGRenderStyle& svgStyle = style->svgStyle();

    // Setup transparency layers before setting up SVG resources!
    bool isRenderingMask = paintInfo.isRenderingClipPathAsMaskImage();

    float opacity = style->opacity();
    bool hasBlendMode = style->hasBlendMode() && object->isBlendingAllowed();

    // RenderLayer takes care of root opacity and blend mode.
    if (!isRenderingMask && !object->isSVGRoot() && (opacity < 1 || hasBlendMode)) {
        m_clipRecorder = adoptPtr(new FloatClipRecorder(*m_paintInfo->context, m_object->displayItemClient(), m_paintInfo->phase, m_object->paintInvalidationRectInLocalCoordinates()));
        WebBlendMode blendMode = hasBlendMode ? style->blendMode() : WebBlendModeNormal;
        CompositeOperator compositeOp = hasBlendMode ? CompositeSourceOver : m_paintInfo->context->compositeOperation();
        m_transparencyRecorder = adoptPtr(new TransparencyRecorder(m_paintInfo->context, object->displayItemClient(), compositeOp, blendMode, opacity, compositeOp));
    }

    SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);

    // Prefer a 'clipper' (non-prefixed 'clip-path') to a 'clip shape'
    // ('-webkit-clip-path'), until these two properties end up being merged
    // properly.
    if (RenderSVGResourceClipper* clipper = resources ? resources->clipper() : nullptr) {
        if (!clipper->applyStatefulResource(m_object, m_paintInfo->context, m_clipperState))
            return;
        m_clipper = clipper;
        m_renderingFlags |= PostApplyResources;
    } else {
        ClipPathOperation* clipPathOperation = style->clipPath();
        if (clipPathOperation && clipPathOperation->type() == ClipPathOperation::SHAPE) {
            ShapeClipPathOperation* clipPath = toShapeClipPathOperation(clipPathOperation);
            m_clipPathRecorder = adoptPtr(new ClipPathRecorder(*m_paintInfo->context, object->displayItemClient(), clipPath->path(object->objectBoundingBox()), clipPath->windRule()));
        }
    }

    if (isRenderingMask) {
        m_renderingFlags |= RenderingPrepared;
        return;
    }

    if (resources) {
        if (RenderSVGResourceMasker* masker = resources->masker()) {
            if (!masker->prepareEffect(m_object, m_paintInfo->context))
                return;
            m_masker = masker;
            m_renderingFlags |= PostApplyResources;
        }

        m_filter = resources->filter();
        if (m_filter) {
            m_savedPaintRect = m_paintInfo->rect;
            // Return with false here may mean that we don't need to draw the content
            // (because it was either drawn before or empty) but we still need to apply the filter.
            m_renderingFlags |= PostApplyResources;
            if (!m_filter->prepareEffect(m_object, m_paintInfo->context))
                return;

            // Since we're caching the resulting bitmap and do not invalidate it on paint invalidation rect
            // changes, we need to paint the whole filter region. Otherwise, elements not visible
            // at the time of the initial paint (due to scrolling, window size, etc.) will never
            // be drawn.
            m_paintInfo->rect = LayoutRect::infiniteIntRect();
        }
    } else {
        // Broken filter disables rendering.
        if (svgStyle.hasFilter())
            return;
    }

    if (!isIsolationInstalled() && SVGRenderSupport::isIsolationRequired(object))
        m_transparencyRecorder = adoptPtr(new TransparencyRecorder(m_paintInfo->context, object->displayItemClient(), m_paintInfo->context->compositeOperation(), WebBlendModeNormal, 1, m_paintInfo->context->compositeOperation()));

    m_renderingFlags |= RenderingPrepared;
}

bool SVGRenderingContext::isIsolationInstalled() const
{
    if (m_transparencyRecorder)
        return true;
    if (m_masker || m_filter)
        return true;
    if (m_clipper && m_clipperState == RenderSVGResourceClipper::ClipperAppliedMask)
        return true;
    return false;
}

static AffineTransform& currentContentTransformation()
{
    DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
    return s_currentContentTransformation;
}

SubtreeContentTransformScope::SubtreeContentTransformScope(const AffineTransform& subtreeContentTransformation)
{
    AffineTransform& contentTransformation = currentContentTransformation();
    m_savedContentTransformation = contentTransformation;
    contentTransformation = subtreeContentTransformation * contentTransformation;
}

SubtreeContentTransformScope::~SubtreeContentTransformScope()
{
    currentContentTransformation() = m_savedContentTransformation;
}

float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer)
{
    // FIXME: trying to compute a device space transform at record time is wrong. All clients
    // should be updated to avoid relying on this information, and the method should be removed.

    ASSERT(renderer);
    // We're about to possibly clear renderer, so save the deviceScaleFactor now.
    float deviceScaleFactor = renderer->document().frameHost()->deviceScaleFactor();

    // Walk up the render tree, accumulating SVG transforms.
    AffineTransform ctm = currentContentTransformation();
    while (renderer) {
        ctm = renderer->localToParentTransform() * ctm;
        if (renderer->isSVGRoot())
            break;
        renderer = renderer->parent();
    }

    // Continue walking up the layer tree, accumulating CSS transforms.
    // FIXME: this queries layer compositing state - which is not
    // supported during layout. Hence, the result may not include all CSS transforms.
    RenderLayer* layer = renderer ? renderer->enclosingLayer() : 0;
    while (layer && layer->isAllowedToQueryCompositingState()) {
        // We can stop at compositing layers, to match the backing resolution.
        // FIXME: should we be computing the transform to the nearest composited layer,
        // or the nearest composited layer that does not paint into its ancestor?
        // I think this is the nearest composited ancestor since we will inherit its
        // transforms in the composited layer tree.
        if (layer->compositingState() != NotComposited)
            break;

        if (TransformationMatrix* layerTransform = layer->transform())
            ctm = layerTransform->toAffineTransform() * ctm;

        layer = layer->parent();
    }

    ctm.scale(deviceScaleFactor);

    return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
}

void SVGRenderingContext::renderSubtree(GraphicsContext* context, RenderObject* item)
{
    ASSERT(context);
    ASSERT(item);
    ASSERT(!item->needsLayout());

    PaintInfo info(context, LayoutRect::infiniteIntRect(), PaintPhaseForeground, PaintBehaviorNormal);
    item->paint(info, IntPoint());
}

} // namespace blink