1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "FloatPoint.h"
29#include "FloatQuad.h"
30#include "LayoutSize.h"
31#include "RenderObject.h"
32#include "TransformationMatrix.h"
33#include <memory>
34
35namespace WebCore {
36
37class RenderFragmentedFlow;
38class RenderLayer;
39class RenderLayerModelObject;
40class RenderView;
41class TransformState;
42
43// Stores data about how to map from one renderer to its container.
44struct RenderGeometryMapStep {
45 RenderGeometryMapStep(const RenderGeometryMapStep& o)
46 : m_renderer(o.m_renderer)
47 , m_offset(o.m_offset)
48 , m_accumulatingTransform(o.m_accumulatingTransform)
49 , m_isNonUniform(o.m_isNonUniform)
50 , m_isFixedPosition(o.m_isFixedPosition)
51 , m_hasTransform(o.m_hasTransform)
52 {
53 ASSERT(!o.m_transform);
54 }
55 RenderGeometryMapStep(const RenderObject* renderer, bool accumulatingTransform, bool isNonUniform, bool isFixedPosition, bool hasTransform)
56 : m_renderer(renderer)
57 , m_accumulatingTransform(accumulatingTransform)
58 , m_isNonUniform(isNonUniform)
59 , m_isFixedPosition(isFixedPosition)
60 , m_hasTransform(hasTransform)
61 {
62 }
63 const RenderObject* m_renderer;
64 LayoutSize m_offset;
65 std::unique_ptr<TransformationMatrix> m_transform; // Includes offset if non-null.
66 bool m_accumulatingTransform;
67 bool m_isNonUniform; // Mapping depends on the input point, e.g. because of CSS columns.
68 bool m_isFixedPosition;
69 bool m_hasTransform;
70};
71
72// Can be used while walking the Renderer tree to cache data about offsets and transforms.
73class RenderGeometryMap {
74 WTF_MAKE_NONCOPYABLE(RenderGeometryMap);
75public:
76 explicit RenderGeometryMap(MapCoordinatesFlags = UseTransforms);
77 ~RenderGeometryMap();
78
79 MapCoordinatesFlags mapCoordinatesFlags() const { return m_mapCoordinatesFlags; }
80
81 FloatPoint absolutePoint(const FloatPoint& p) const
82 {
83 return mapToContainer(p, nullptr);
84 }
85
86 FloatRect absoluteRect(const FloatRect& rect) const
87 {
88 return mapToContainer(rect, nullptr).boundingBox();
89 }
90
91 // Map to a container. Will assert that the container has been pushed onto this map.
92 // A null container maps through the RenderView (including its scale transform, if any).
93 // If the container is the RenderView, the scroll offset is applied, but not the scale.
94 FloatPoint mapToContainer(const FloatPoint&, const RenderLayerModelObject*) const;
95 FloatQuad mapToContainer(const FloatRect&, const RenderLayerModelObject*) const;
96
97 // Called by code walking the renderer or layer trees.
98 void pushMappingsToAncestor(const RenderLayer*, const RenderLayer* ancestorLayer, bool respectTransforms = true);
99 void popMappingsToAncestor(const RenderLayer*);
100 void pushMappingsToAncestor(const RenderObject*, const RenderLayerModelObject* ancestorRenderer);
101 void popMappingsToAncestor(const RenderLayerModelObject*);
102
103 // The following methods should only be called by renderers inside a call to pushMappingsToAncestor().
104
105 // Push geometry info between this renderer and some ancestor. The ancestor must be its container() or some
106 // stacking context between the renderer and its container.
107 void push(const RenderObject*, const LayoutSize&, bool accumulatingTransform = false, bool isNonUniform = false, bool isFixedPosition = false, bool hasTransform = false);
108 void push(const RenderObject*, const TransformationMatrix&, bool accumulatingTransform = false, bool isNonUniform = false, bool isFixedPosition = false, bool hasTransform = false);
109
110 // RenderView gets special treatment, because it applies the scroll offset only for elements inside in fixed position.
111 void pushView(const RenderView*, const LayoutSize& scrollOffset, const TransformationMatrix* = nullptr);
112 void pushRenderFragmentedFlow(const RenderFragmentedFlow*);
113
114private:
115 void mapToContainer(TransformState&, const RenderLayerModelObject* container = nullptr) const;
116
117 void stepInserted(const RenderGeometryMapStep&);
118 void stepRemoved(const RenderGeometryMapStep&);
119
120 bool hasNonUniformStep() const { return m_nonUniformStepsCount; }
121 bool hasTransformStep() const { return m_transformedStepsCount; }
122 bool hasFixedPositionStep() const { return m_fixedStepsCount; }
123
124 typedef Vector<RenderGeometryMapStep, 32> RenderGeometryMapSteps;
125
126 size_t m_insertionPosition;
127 int m_nonUniformStepsCount;
128 int m_transformedStepsCount;
129 int m_fixedStepsCount;
130 RenderGeometryMapSteps m_mapping;
131 LayoutSize m_accumulatedOffset;
132 MapCoordinatesFlags m_mapCoordinatesFlags;
133};
134
135} // namespace WebCore
136
137namespace WTF {
138// This is required for a struct with std::unique_ptr<>. We know RenderGeometryMapStep is simple enough that
139// initializing to 0 and moving with memcpy (and then not destructing the original) will work.
140template<> struct VectorTraits<WebCore::RenderGeometryMapStep> : SimpleClassVectorTraits { };
141}
142