1/*
2 * Copyright (C) 2011 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 LegacyTileGrid_h
27#define LegacyTileGrid_h
28
29#if PLATFORM(IOS)
30
31#include "IntPoint.h"
32#include "IntPointHash.h"
33#include "IntRect.h"
34#include "IntSize.h"
35#include "LegacyTileCache.h"
36#include <wtf/HashMap.h>
37#include <wtf/Noncopyable.h>
38#include <wtf/OwnPtr.h>
39#include <wtf/PassOwnPtr.h>
40#include <wtf/PassRefPtr.h>
41#include <wtf/RetainPtr.h>
42
43#define LOG_TILING 0
44
45@class CALayer;
46
47namespace WebCore {
48
49class LegacyTileGridTile;
50
51class LegacyTileGrid {
52    WTF_MAKE_NONCOPYABLE(LegacyTileGrid);
53public:
54    typedef IntPoint TileIndex;
55
56    static PassOwnPtr<LegacyTileGrid> create(LegacyTileCache* cache, const IntSize& tileSize) { return adoptPtr(new LegacyTileGrid(cache, tileSize)); }
57
58    ~LegacyTileGrid();
59
60    LegacyTileCache* tileCache() const { return m_tileCache; }
61
62    CALayer *tileHostLayer() const;
63    IntRect bounds() const;
64    unsigned tileCount() const;
65
66    float scale() const { return m_scale; }
67    void setScale(float scale) { m_scale = scale; }
68
69    IntRect visibleRect() const;
70
71    void createTiles(LegacyTileCache::SynchronousTileCreationMode);
72
73    void dropAllTiles();
74    void dropInvalidTiles();
75    void dropTilesOutsideRect(const IntRect&);
76    void dropTilesIntersectingRect(const IntRect&);
77    // Drops tiles that intersect dropRect but do not intersect keepRect.
78    void dropTilesBetweenRects(const IntRect& dropRect, const IntRect& keepRect);
79    bool dropDistantTiles(unsigned tilesNeeded, double shortestDistance);
80
81    void addTilesCoveringRect(const IntRect&);
82
83    bool tilesCover(const IntRect&) const;
84    void centerTileGridOrigin(const IntRect& visibleRect);
85    void invalidateTiles(const IntRect& dirtyRect);
86
87    void updateTileOpacity();
88    void updateTileBorderVisibility();
89    void updateHostLayerSize();
90    bool checkDoSingleTileLayout();
91
92    bool hasTiles() const { return !m_tiles.isEmpty(); }
93
94    IntRect calculateCoverRect(const IntRect& visibleRect, bool& centerGrid);
95
96    // Logging
97    void dumpTiles();
98
99private:
100    double tileDistance2(const IntRect& visibleRect, const IntRect& tileRect) const;
101    unsigned tileByteSize() const;
102
103    void addTileForIndex(const TileIndex&);
104
105    PassRefPtr<LegacyTileGridTile> tileForIndex(const TileIndex&) const;
106    IntRect tileRectForIndex(const TileIndex&) const;
107    PassRefPtr<LegacyTileGridTile> tileForPoint(const IntPoint&) const;
108    TileIndex tileIndexForPoint(const IntPoint&) const;
109
110    IntRect adjustCoverRectForPageBounds(const IntRect&) const;
111    bool shouldUseMinimalTileCoverage() const;
112
113private:
114    LegacyTileGrid(LegacyTileCache*, const IntSize&);
115
116    LegacyTileCache* m_tileCache;
117    RetainPtr<CALayer> m_tileHostLayer;
118
119    IntPoint m_origin;
120    IntSize m_tileSize;
121
122    float m_scale;
123
124    typedef HashMap<TileIndex, RefPtr<LegacyTileGridTile>> TileMap;
125    TileMap m_tiles;
126
127    IntRect m_validBounds;
128};
129
130static inline IntPoint topLeft(const IntRect& rect)
131{
132    return rect.location();
133}
134
135static inline IntPoint bottomRight(const IntRect& rect)
136{
137    return IntPoint(rect.maxX() - 1, rect.maxY() - 1);
138}
139
140} // namespace WebCore
141
142#endif // PLATFORM(IOS)
143#endif // TileGrid_h
144