1/*
2 * Copyright (C) 2011-2014 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef TileController_h
27#define TileController_h
28
29#include "FloatRect.h"
30#include "IntRect.h"
31#include "PlatformCALayer.h"
32#include "PlatformCALayerClient.h"
33#include "TiledBacking.h"
34#include "Timer.h"
35#include <wtf/Deque.h>
36#include <wtf/HashMap.h>
37#include <wtf/Noncopyable.h>
38#include <wtf/PassOwnPtr.h>
39#include <wtf/RetainPtr.h>
40
41namespace WebCore {
42
43class FloatRect;
44class IntPoint;
45class IntRect;
46class TileCoverageMap;
47class TileGrid;
48
49typedef Vector<RetainPtr<PlatformLayer>> PlatformLayerList;
50
51class TileController final : public TiledBacking {
52    WTF_MAKE_NONCOPYABLE(TileController); WTF_MAKE_FAST_ALLOCATED;
53    friend class TileCoverageMap;
54    friend class TileGrid;
55public:
56    static PassOwnPtr<TileController> create(PlatformCALayer*);
57    ~TileController();
58
59    void tileCacheLayerBoundsChanged();
60
61    void setNeedsDisplay();
62    void setNeedsDisplayInRect(const IntRect&);
63
64    void setContentsScale(float);
65    float contentsScale() const;
66
67    bool acceleratesDrawing() const { return m_acceleratesDrawing; }
68    void setAcceleratesDrawing(bool);
69
70    void setTilesOpaque(bool);
71    bool tilesAreOpaque() const { return m_tilesAreOpaque; }
72
73    PlatformCALayer& rootLayer() { return *m_tileCacheLayer; }
74    const PlatformCALayer& rootLayer() const { return *m_tileCacheLayer; }
75
76    void setTileDebugBorderWidth(float);
77    void setTileDebugBorderColor(Color);
78
79    virtual FloatRect visibleRect() const override { return m_visibleRect; }
80
81    unsigned blankPixelCount() const;
82    static unsigned blankPixelCountForTiles(const PlatformLayerList&, const FloatRect&, const IntPoint&);
83
84#if PLATFORM(IOS)
85    unsigned numberOfUnparentedTiles() const;
86    void removeUnparentedTilesNow();
87#endif
88
89    float deviceScaleFactor() const { return m_deviceScaleFactor; }
90
91    Color tileDebugBorderColor() const { return m_tileDebugBorderColor; }
92    float tileDebugBorderWidth() const { return m_tileDebugBorderWidth; }
93    ScrollingModeIndication indicatorMode() const { return m_indicatorMode; }
94
95    virtual IntSize tileSize() const override { return m_tileSize; }
96    virtual IntRect bounds() const override;
97    virtual IntRect boundsWithoutMargin() const override;
98    virtual bool hasMargins() const override;
99    virtual bool hasHorizontalMargins() const override;
100    virtual bool hasVerticalMargins() const override;
101    virtual int topMarginHeight() const override;
102    virtual int bottomMarginHeight() const override;
103    virtual int leftMarginWidth() const override;
104    virtual int rightMarginWidth() const override;
105    virtual TileCoverage tileCoverage() const override { return m_tileCoverage; }
106    virtual bool unparentsOffscreenTiles() const override { return m_unparentsOffscreenTiles; }
107    virtual bool scrollingPerformanceLoggingEnabled() const override { return m_scrollingPerformanceLoggingEnabled; }
108
109    FloatRect computeTileCoverageRect(const FloatRect& previousVisibleRect, const FloatRect& currentVisibleRect) const;
110
111    IntRect boundsAtLastRevalidate() const { return m_boundsAtLastRevalidate; }
112    IntRect boundsAtLastRevalidateWithoutMargin() const;
113    FloatRect visibleRectAtLastRevalidate() const { return m_visibleRectAtLastRevalidate; }
114    void didRevalidateTiles();
115
116    bool shouldAggressivelyRetainTiles() const;
117    bool shouldTemporarilyRetainTileCohorts() const;
118
119    void updateTileCoverageMap();
120
121    RefPtr<PlatformCALayer> createTileLayer(const IntRect&, TileGrid&);
122
123    const TileGrid& tileGrid() const { return *m_tileGrid; }
124
125    Vector<RefPtr<PlatformCALayer>> containerLayers();
126
127protected:
128    void scheduleTileRevalidation(double interval);
129
130    bool isInWindow() const { return m_isInWindow; }
131    float topContentInset() const { return m_topContentInset; }
132
133private:
134    TileController(PlatformCALayer*);
135
136    TileGrid& tileGrid() { return *m_tileGrid; }
137
138    // TiledBacking member functions.
139    virtual void setVisibleRect(const FloatRect&) override;
140    virtual bool tilesWouldChangeForVisibleRect(const FloatRect&) const override;
141    virtual void setTiledScrollingIndicatorPosition(const FloatPoint&) override;
142    virtual void setTopContentInset(float) override;
143    virtual void prepopulateRect(const FloatRect&) override;
144    virtual void setIsInWindow(bool) override;
145    virtual void setTileCoverage(TileCoverage) override;
146    virtual void revalidateTiles() override;
147    virtual void forceRepaint() override;
148    virtual IntRect tileGridExtent() const override;
149    virtual void setScrollingPerformanceLoggingEnabled(bool flag) override { m_scrollingPerformanceLoggingEnabled = flag; }
150    virtual void setUnparentsOffscreenTiles(bool flag) override { m_unparentsOffscreenTiles = flag; }
151    virtual double retainedTileBackingStoreMemory() const override;
152    virtual IntRect tileCoverageRect() const override;
153#if USE(CA)
154    virtual PlatformCALayer* tiledScrollingIndicatorLayer() override;
155#endif
156    virtual void setScrollingModeIndication(ScrollingModeIndication) override;
157    virtual void setTileMargins(int marginTop, int marginBottom, int marginLeft, int marginRight) override;
158    virtual void setZoomedOutContentsScale(float) override;
159    virtual float zoomedOutContentsScale() const override;
160
161
162    void tileRevalidationTimerFired(Timer<TileController>*);
163
164    void setNeedsRevalidateTiles();
165
166    PlatformCALayerClient* owningGraphicsLayer() const { return m_tileCacheLayer->owner(); }
167
168    PlatformCALayer* m_tileCacheLayer;
169
170    std::unique_ptr<TileCoverageMap> m_coverageMap;
171
172    std::unique_ptr<TileGrid> m_tileGrid;
173    std::unique_ptr<TileGrid> m_zoomedOutTileGrid;
174
175    IntSize m_tileSize;
176    FloatRect m_visibleRect;
177    FloatRect m_visibleRectAtLastRevalidate;
178    IntRect m_boundsAtLastRevalidate;
179
180    Timer<TileController> m_tileRevalidationTimer;
181
182    float m_zoomedOutContentsScale;
183    float m_deviceScaleFactor;
184
185    TileCoverage m_tileCoverage;
186
187    // m_marginTop and m_marginBottom are the height in pixels of the top and bottom margin tiles. The width
188    // of those tiles will be equivalent to the width of the other tiles in the grid. m_marginRight and
189    // m_marginLeft are the width in pixels of the right and left margin tiles, respectively. The height of
190    // those tiles will be equivalent to the height of the other tiles in the grid.
191    int m_marginTop;
192    int m_marginBottom;
193    int m_marginLeft;
194    int m_marginRight;
195
196    bool m_isInWindow;
197    bool m_scrollingPerformanceLoggingEnabled;
198    bool m_unparentsOffscreenTiles;
199    bool m_acceleratesDrawing;
200    bool m_tilesAreOpaque;
201    bool m_hasTilesWithTemporaryScaleFactor; // Used to make low-res tiles when zooming.
202
203    Color m_tileDebugBorderColor;
204    float m_tileDebugBorderWidth;
205    ScrollingModeIndication m_indicatorMode;
206    float m_topContentInset;
207};
208
209} // namespace WebCore
210
211#endif // TileController_h
212