Home | History | Annotate | Download | only in frame
      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 
     44 namespace blink {
     45 class WebLayerTreeView;
     46 class WebScrollbarLayer;
     47 }
     48 
     49 namespace WebCore {
     50 
     51 class FrameHost;
     52 class GraphicsContext;
     53 class GraphicsLayer;
     54 class GraphicsLayerFactory;
     55 class IntRect;
     56 class IntSize;
     57 class 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.
     64 class PinchViewport FINAL : public GraphicsLayerClient, public ScrollableArea {
     65 public:
     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 
     84     // Sets the size of the inner viewport when unscaled in CSS pixels.
     85     // This will be clamped to the size of the outer viewport (the main frame).
     86     void setSize(const IntSize&);
     87     IntSize size() const { return m_size; }
     88 
     89     // Resets the viewport to initial state.
     90     void reset();
     91 
     92     // Let the viewport know that the main frame changed size (either through screen
     93     // rotation on Android or window resize elsewhere).
     94     void mainFrameDidChangeSize();
     95 
     96     void setScale(float);
     97     float scale() const { return m_scale; }
     98 
     99     void registerLayersWithTreeView(blink::WebLayerTreeView*) const;
    100     void clearLayersForTreeView(blink::WebLayerTreeView*) const;
    101 
    102     // The portion of the unzoomed frame visible in the inner "pinch" viewport,
    103     // in partial CSS pixels. Relative to the main frame.
    104     FloatRect visibleRect() const;
    105 
    106     // The viewport rect relative to the document origin, in partial CSS pixels.
    107     FloatRect visibleRectInDocument() const;
    108 
    109     // Scroll the main frame and pinch viewport so that the given rect in the
    110     // top-level document is centered in the viewport. This method will avoid
    111     // scrolling the pinch viewport unless necessary.
    112     void scrollIntoView(const FloatRect&);
    113 private:
    114     // ScrollableArea implementation
    115     virtual bool isActive() const OVERRIDE { return false; }
    116     virtual int scrollSize(ScrollbarOrientation) const OVERRIDE;
    117     virtual bool isScrollCornerVisible() const OVERRIDE { return false; }
    118     virtual IntRect scrollCornerRect() const OVERRIDE { return IntRect(); }
    119     virtual IntPoint scrollPosition() const OVERRIDE { return flooredIntPoint(m_offset); }
    120     virtual IntPoint minimumScrollPosition() const OVERRIDE;
    121     virtual IntPoint maximumScrollPosition() const OVERRIDE;
    122     virtual int visibleHeight() const OVERRIDE { return visibleRect().height(); };
    123     virtual int visibleWidth() const OVERRIDE { return visibleRect().width(); };
    124     virtual IntSize contentsSize() const OVERRIDE;
    125     virtual bool scrollbarsCanBeActive() const OVERRIDE { return false; }
    126     virtual IntRect scrollableAreaBoundingBox() const OVERRIDE;
    127     virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE { return true; }
    128     virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE { return false; }
    129     virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE;
    130     virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE { }
    131     virtual void setScrollOffset(const IntPoint&) OVERRIDE;
    132     virtual GraphicsLayer* layerForContainer() const OVERRIDE;
    133     virtual GraphicsLayer* layerForScrolling() const OVERRIDE;
    134     virtual GraphicsLayer* layerForHorizontalScrollbar() const OVERRIDE;
    135     virtual GraphicsLayer* layerForVerticalScrollbar() const OVERRIDE;
    136 
    137     // GraphicsLayerClient implementation.
    138     virtual void notifyAnimationStarted(const GraphicsLayer*, double monotonicTime) OVERRIDE;
    139     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& inClip) OVERRIDE;
    140     virtual String debugName(const GraphicsLayer*) OVERRIDE;
    141 
    142     void setupScrollbar(blink::WebScrollbar::Orientation);
    143     FloatPoint clampOffsetToBoundaries(const FloatPoint&);
    144 
    145     LocalFrame* mainFrame() const;
    146 
    147     FrameHost& m_frameHost;
    148     OwnPtr<GraphicsLayer> m_rootTransformLayer;
    149     OwnPtr<GraphicsLayer> m_innerViewportContainerLayer;
    150     OwnPtr<GraphicsLayer> m_pageScaleLayer;
    151     OwnPtr<GraphicsLayer> m_innerViewportScrollLayer;
    152     OwnPtr<GraphicsLayer> m_overlayScrollbarHorizontal;
    153     OwnPtr<GraphicsLayer> m_overlayScrollbarVertical;
    154     OwnPtr<blink::WebScrollbarLayer> m_webOverlayScrollbarHorizontal;
    155     OwnPtr<blink::WebScrollbarLayer> m_webOverlayScrollbarVertical;
    156 
    157     // Offset of the pinch viewport from the main frame's origin, in CSS pixels.
    158     FloatPoint m_offset;
    159     float m_scale;
    160     IntSize m_size;
    161 };
    162 
    163 } // namespace WebCore
    164 
    165 #endif // PinchViewport_h
    166