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 COMPUTER, 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 COMPUTER, 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#ifndef RenderGeometryMap_h
27#define RenderGeometryMap_h
28
29#include "core/platform/graphics/FloatPoint.h"
30#include "core/platform/graphics/FloatQuad.h"
31#include "core/platform/graphics/IntSize.h"
32#include "core/platform/graphics/LayoutSize.h"
33#include "core/platform/graphics/transforms/TransformationMatrix.h"
34#include "core/rendering/RenderObject.h"
35#include "wtf/OwnPtr.h"
36
37namespace WebCore {
38
39class RenderLayer;
40class RenderLayerModelObject;
41class RenderView;
42class TransformState;
43
44// Stores data about how to map from one renderer to its container.
45struct RenderGeometryMapStep {
46    RenderGeometryMapStep(const RenderGeometryMapStep& o)
47        : m_renderer(o.m_renderer)
48        , m_offset(o.m_offset)
49        , m_accumulatingTransform(o.m_accumulatingTransform)
50        , m_isNonUniform(o.m_isNonUniform)
51        , m_isFixedPosition(o.m_isFixedPosition)
52        , m_hasTransform(o.m_hasTransform)
53    {
54        ASSERT(!o.m_transform);
55    }
56    RenderGeometryMapStep(const RenderObject* renderer, bool accumulatingTransform, bool isNonUniform, bool isFixedPosition, bool hasTransform)
57        : m_renderer(renderer)
58        , m_accumulatingTransform(accumulatingTransform)
59        , m_isNonUniform(isNonUniform)
60        , m_isFixedPosition(isFixedPosition)
61        , m_hasTransform(hasTransform)
62    {
63    }
64    const RenderObject* m_renderer;
65    LayoutSize m_offset;
66    OwnPtr<TransformationMatrix> m_transform; // Includes offset if non-null.
67    bool m_accumulatingTransform;
68    bool m_isNonUniform; // Mapping depends on the input point, e.g. because of CSS columns.
69    bool m_isFixedPosition;
70    bool m_hasTransform;
71};
72
73// Can be used while walking the Renderer tree to cache data about offsets and transforms.
74class RenderGeometryMap {
75    WTF_MAKE_NONCOPYABLE(RenderGeometryMap);
76public:
77    RenderGeometryMap(MapCoordinatesFlags = UseTransforms);
78    ~RenderGeometryMap();
79
80    MapCoordinatesFlags mapCoordinatesFlags() const { return m_mapCoordinatesFlags; }
81
82    FloatPoint absolutePoint(const FloatPoint& p) const
83    {
84        return mapToContainer(p, 0);
85    }
86
87    FloatRect absoluteRect(const FloatRect& rect) const
88    {
89        return mapToContainer(rect, 0).boundingBox();
90    }
91
92    // Map to a container. Will assert that the container has been pushed onto this map.
93    // A null container maps through the RenderView (including its scale transform, if any).
94    // If the container is the RenderView, the scroll offset is applied, but not the scale.
95    FloatPoint mapToContainer(const FloatPoint&, const RenderLayerModelObject*) const;
96    FloatQuad mapToContainer(const FloatRect&, const RenderLayerModelObject*) const;
97
98    // Called by code walking the renderer or layer trees.
99    void pushMappingsToAncestor(const RenderLayer*, const RenderLayer* ancestorLayer);
100    void popMappingsToAncestor(const RenderLayer*);
101    void pushMappingsToAncestor(const RenderObject*, const RenderLayerModelObject* ancestorRenderer);
102    void popMappingsToAncestor(const RenderLayerModelObject*);
103
104    // The following methods should only be called by renderers inside a call to pushMappingsToAncestor().
105
106    // Push geometry info between this renderer and some ancestor. The ancestor must be its container() or some
107    // stacking context between the renderer and its container.
108    void push(const RenderObject*, const LayoutSize&, bool accumulatingTransform = false, bool isNonUniform = false, bool isFixedPosition = false, bool hasTransform = false);
109    void push(const RenderObject*, const TransformationMatrix&, bool accumulatingTransform = false, bool isNonUniform = false, bool isFixedPosition = false, bool hasTransform = false);
110
111    // RenderView gets special treatment, because it applies the scroll offset only for elements inside in fixed position.
112    void pushView(const RenderView*, const LayoutSize& scrollOffset, const TransformationMatrix* = 0);
113
114private:
115    void mapToContainer(TransformState&, const RenderLayerModelObject* container = 0) 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 OwnPtr. 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
143#endif // RenderGeometryMap_h
144