1/*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef PinchViewport_h
32#define PinchViewport_h
33
34#include "platform/geometry/FloatPoint.h"
35#include "platform/geometry/FloatRect.h"
36#include "platform/geometry/IntSize.h"
37#include "platform/graphics/GraphicsLayerClient.h"
38#include "platform/scroll/ScrollableArea.h"
39#include "public/platform/WebScrollbar.h"
40#include "public/platform/WebSize.h"
41#include "wtf/OwnPtr.h"
42#include "wtf/PassOwnPtr.h"
43
44namespace blink {
45class WebLayerTreeView;
46class WebScrollbarLayer;
47}
48
49namespace blink {
50
51class FrameHost;
52class GraphicsContext;
53class GraphicsLayer;
54class GraphicsLayerFactory;
55class IntRect;
56class IntSize;
57class LocalFrame;
58
59// Represents the pinch-to-zoom viewport the user is currently seeing the page through. This
60// class corresponds to the InnerViewport on the compositor. It is a ScrollableArea; it's
61// offset is set through the GraphicsLayer <-> CC sync mechanisms. Its contents is the page's
62// main FrameView, which corresponds to the outer viewport. The inner viewport is always contained
63// in the outer viewport and can pan within it.
64class PinchViewport FINAL : public GraphicsLayerClient, public ScrollableArea {
65public:
66    explicit PinchViewport(FrameHost&);
67    virtual ~PinchViewport();
68
69    void attachToLayerTree(GraphicsLayer*, GraphicsLayerFactory*);
70    GraphicsLayer* rootGraphicsLayer()
71    {
72        return m_rootTransformLayer.get();
73    }
74    GraphicsLayer* containerLayer()
75    {
76        return m_innerViewportContainerLayer.get();
77    }
78
79    // Sets the location of the inner viewport relative to the outer viewport. The
80    // coordinates are in partial CSS pixels.
81    void setLocation(const FloatPoint&);
82    void move(const FloatPoint&);
83    FloatPoint location() const { return m_offset; }
84
85    // Sets the size of the inner viewport when unscaled in CSS pixels.
86    // This will be clamped to the size of the outer viewport (the main frame).
87    void setSize(const IntSize&);
88    IntSize size() const { return m_size; }
89
90    // Resets the viewport to initial state.
91    void reset();
92
93    // Let the viewport know that the main frame changed size (either through screen
94    // rotation on Android or window resize elsewhere).
95    void mainFrameDidChangeSize();
96
97    void setScale(float);
98    float scale() const { return m_scale; }
99
100    void registerLayersWithTreeView(blink::WebLayerTreeView*) const;
101    void clearLayersForTreeView(blink::WebLayerTreeView*) const;
102
103    // The portion of the unzoomed frame visible in the inner "pinch" viewport,
104    // in partial CSS pixels. Relative to the main frame.
105    FloatRect visibleRect() const;
106
107    // The viewport rect relative to the document origin, in partial CSS pixels.
108    FloatRect visibleRectInDocument() const;
109
110    // Scroll the main frame and pinch viewport so that the given rect in the
111    // top-level document is centered in the viewport. This method will avoid
112    // scrolling the pinch viewport unless necessary.
113    void scrollIntoView(const FloatRect&);
114private:
115    // ScrollableArea implementation
116    virtual bool isActive() const OVERRIDE { return false; }
117    virtual int scrollSize(ScrollbarOrientation) const OVERRIDE;
118    virtual bool isScrollCornerVisible() const OVERRIDE { return false; }
119    virtual IntRect scrollCornerRect() const OVERRIDE { return IntRect(); }
120    virtual IntPoint scrollPosition() const OVERRIDE { return flooredIntPoint(m_offset); }
121    virtual IntPoint minimumScrollPosition() const OVERRIDE;
122    virtual IntPoint maximumScrollPosition() const OVERRIDE;
123    virtual int visibleHeight() const OVERRIDE { return visibleRect().height(); };
124    virtual int visibleWidth() const OVERRIDE { return visibleRect().width(); };
125    virtual IntSize contentsSize() const OVERRIDE;
126    virtual bool scrollbarsCanBeActive() const OVERRIDE { return false; }
127    virtual IntRect scrollableAreaBoundingBox() const OVERRIDE;
128    virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE { return true; }
129    virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE { return false; }
130    virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE;
131    virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE { }
132    virtual void setScrollOffset(const IntPoint&) OVERRIDE;
133    virtual GraphicsLayer* layerForContainer() const OVERRIDE;
134    virtual GraphicsLayer* layerForScrolling() const OVERRIDE;
135    virtual GraphicsLayer* layerForHorizontalScrollbar() const OVERRIDE;
136    virtual GraphicsLayer* layerForVerticalScrollbar() const OVERRIDE;
137
138    // GraphicsLayerClient implementation.
139    virtual void notifyAnimationStarted(const GraphicsLayer*, double monotonicTime) OVERRIDE;
140    virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& inClip) OVERRIDE;
141    virtual String debugName(const GraphicsLayer*) OVERRIDE;
142
143    void setupScrollbar(blink::WebScrollbar::Orientation);
144    FloatPoint clampOffsetToBoundaries(const FloatPoint&);
145
146    LocalFrame* mainFrame() const;
147
148    FrameHost& m_frameHost;
149    OwnPtr<GraphicsLayer> m_rootTransformLayer;
150    OwnPtr<GraphicsLayer> m_innerViewportContainerLayer;
151    OwnPtr<GraphicsLayer> m_pageScaleLayer;
152    OwnPtr<GraphicsLayer> m_innerViewportScrollLayer;
153    OwnPtr<GraphicsLayer> m_overlayScrollbarHorizontal;
154    OwnPtr<GraphicsLayer> m_overlayScrollbarVertical;
155    OwnPtr<blink::WebScrollbarLayer> m_webOverlayScrollbarHorizontal;
156    OwnPtr<blink::WebScrollbarLayer> m_webOverlayScrollbarVertical;
157
158    // Offset of the pinch viewport from the main frame's origin, in CSS pixels.
159    FloatPoint m_offset;
160    float m_scale;
161    IntSize m_size;
162};
163
164} // namespace blink
165
166#endif // PinchViewport_h
167