Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / PaintInvalidationState.cpp
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/rendering/PaintInvalidationState.h"
7
8 #include "core/rendering/RenderInline.h"
9 #include "core/rendering/RenderLayer.h"
10 #include "core/rendering/RenderView.h"
11 #include "core/rendering/svg/RenderSVGModelObject.h"
12 #include "platform/Partitions.h"
13
14 namespace blink {
15
16 PaintInvalidationState::PaintInvalidationState(RenderObject& renderer)
17     : m_clipped(false)
18     , m_cachedOffsetsEnabled(true)
19     , m_forceCheckForPaintInvalidation(false)
20     , m_paintInvalidationContainer(*renderer.containerForPaintInvalidation())
21     , m_renderer(renderer)
22 {
23     bool establishesPaintInvalidationContainer = &m_renderer == &m_paintInvalidationContainer;
24     if (!establishesPaintInvalidationContainer) {
25         if (!renderer.supportsPaintInvalidationStateCachedOffsets()) {
26             m_cachedOffsetsEnabled = false;
27             return;
28         }
29         bool invalidationContainerSkipped;
30         RenderObject* container = renderer.container(&m_paintInvalidationContainer, &invalidationContainerSkipped);
31         if (container && !invalidationContainerSkipped) {
32             FloatPoint point = container->localToContainerPoint(FloatPoint(), &m_paintInvalidationContainer);
33             if (container->isTableRow())
34                 point = FloatPoint(point.x() - toRenderBox(container)->x().toFloat(), point.y() - toRenderBox(container)->y().toFloat());
35             m_paintOffset = LayoutSize(point.x(), point.y());
36
37             applyClipIfNeeded(*container);
38         }
39     } else {
40         applyClipIfNeeded(m_renderer);
41     }
42 }
43
44 PaintInvalidationState::PaintInvalidationState(const PaintInvalidationState& next, RenderLayerModelObject& renderer, const RenderLayerModelObject& paintInvalidationContainer)
45     : m_clipped(false)
46     , m_cachedOffsetsEnabled(true)
47     , m_forceCheckForPaintInvalidation(next.m_forceCheckForPaintInvalidation)
48     , m_paintInvalidationContainer(paintInvalidationContainer)
49     , m_renderer(renderer)
50 {
51     // FIXME: SVG could probably benefit from a stack-based optimization like html does. crbug.com/391054
52     bool establishesPaintInvalidationContainer = &m_renderer == &m_paintInvalidationContainer;
53     bool fixed = m_renderer.isOutOfFlowPositioned() && m_renderer.style()->position() == FixedPosition;
54
55     if (establishesPaintInvalidationContainer) {
56         // When we hit a new paint invalidation container, we don't need to
57         // continue forcing a check for paint invalidation because movement
58         // from our parents will just move the whole invalidation container.
59         m_forceCheckForPaintInvalidation = false;
60     } else {
61         if (!renderer.supportsPaintInvalidationStateCachedOffsets() || !next.m_cachedOffsetsEnabled) {
62             m_cachedOffsetsEnabled = false;
63         } else {
64             LayoutSize offset = m_renderer.isBox() && !m_renderer.isTableRow() ? toRenderBox(renderer).locationOffset() : LayoutSize();
65             if (fixed) {
66                 // FIXME: This doesn't work correctly with transforms.
67                 FloatPoint fixedOffset = m_renderer.view()->localToAbsolute(FloatPoint(), IsFixed);
68                 m_paintOffset = LayoutSize(fixedOffset.x(), fixedOffset.y()) + offset;
69             } else {
70                 m_paintOffset = next.m_paintOffset + offset;
71             }
72
73             if (m_renderer.isOutOfFlowPositioned() && !fixed) {
74                 if (RenderObject* container = m_renderer.container()) {
75                     if (container->style()->hasInFlowPosition() && container->isRenderInline())
76                         m_paintOffset += toRenderInline(container)->offsetForInFlowPositionedInline(toRenderBox(renderer));
77                 }
78             }
79
80             if (m_renderer.style()->hasInFlowPosition() && renderer.hasLayer())
81                 m_paintOffset += renderer.layer()->offsetForInFlowPosition();
82         }
83
84         m_clipped = !fixed && next.m_clipped;
85         if (m_clipped)
86             m_clipRect = next.m_clipRect;
87     }
88
89     applyClipIfNeeded(renderer);
90
91     // FIXME: <http://bugs.webkit.org/show_bug.cgi?id=13443> Apply control clip if present.
92 }
93
94 void PaintInvalidationState::applyClipIfNeeded(const RenderObject& renderer)
95 {
96     if (!renderer.hasOverflowClip())
97         return;
98
99     const RenderBox& box = toRenderBox(renderer);
100
101     // Do not clip scroll layer contents because the compositor expects the whole layer
102     // to be always invalidated in-time.
103     if (box.usesCompositedScrolling()) {
104         ASSERT(!m_clipped); // The box should establish paint invalidation container, so no m_clipped inherited.
105     } else {
106         LayoutRect clipRect(toPoint(m_paintOffset), box.layer()->size());
107         if (m_clipped) {
108             m_clipRect.intersect(clipRect);
109         } else {
110             m_clipRect = clipRect;
111             m_clipped = true;
112         }
113     }
114
115     m_paintOffset -= box.scrolledContentOffset();
116 }
117
118 } // namespace blink