initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / cc / CCLayerTilingData.h
1 /*
2  * Copyright (C) 2011 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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26
27 #ifndef CCLayerTilingData_h
28 #define CCLayerTilingData_h
29
30 #if USE(ACCELERATED_COMPOSITING)
31
32 #include "IntRect.h"
33 #include "TilingData.h"
34 #include <wtf/HashMap.h>
35 #include <wtf/HashTraits.h>
36 #include <wtf/PassOwnPtr.h>
37 #include <wtf/RefCounted.h>
38
39 namespace WebCore {
40
41 class CCLayerTilingData {
42 public:
43     enum BorderTexelOption { HasBorderTexels, NoBorderTexels };
44
45     static PassOwnPtr<CCLayerTilingData> create(const IntSize& tileSize, BorderTexelOption);
46
47     int numTiles() const { return m_tilingData.numTiles(); }
48     int numTilesX() const { return m_tilingData.numTilesX(); }
49     int numTilesY() const { return m_tilingData.numTilesY(); }
50     IntRect tileBounds(int i, int j) const { return m_tilingData.tileBounds(m_tilingData.tileIndex(i, j)); }
51     IntPoint textureOffset(int xIndex, int yIndex) const { return m_tilingData.textureOffset(xIndex, yIndex); }
52
53     // Set position of this tiled layer in content space.
54     void setLayerPosition(const IntPoint&);
55
56     // Change the tile size. This may invalidate all the existing tiles.
57     void setTileSize(const IntSize&);
58     const IntSize& tileSize() const { return m_tileSize; }
59     bool hasBorderTexels() const { return m_tilingData.borderTexels(); }
60
61     bool isEmpty() const { return !m_tilingData.numTiles() || !tiles().size(); }
62
63     const CCLayerTilingData& operator=(const CCLayerTilingData&);
64
65     class Tile: public RefCounted<Tile> {
66         WTF_MAKE_NONCOPYABLE(Tile);
67     public:
68         Tile() : m_i(-1), m_j(-1) { }
69         virtual ~Tile() { }
70
71         int i() const { return m_i; }
72         int j() const { return m_j; }
73         void moveTo(int i, int j) { m_i = i; m_j = j; }
74     private:
75         int m_i;
76         int m_j;
77     };
78     // Default hash key traits for integers disallow 0 and -1 as a key, so
79     // use a custom hash trait which disallows -1 and -2 instead.
80     typedef std::pair<int, int> TileMapKey;
81     struct TileMapKeyTraits : HashTraits<TileMapKey> {
82         static const bool emptyValueIsZero = false;
83         static const bool needsDestruction = false;
84         static TileMapKey emptyValue() { return std::make_pair(-1, -1); }
85         static void constructDeletedValue(TileMapKey& slot) { slot = std::make_pair(-2, -2); }
86         static bool isDeletedValue(TileMapKey value) { return value.first == -2 && value.second == -2; }
87     };
88     // FIXME: The mapped value in TileMap should really be an OwnPtr, as the
89     // refcount of a Tile should never be more than 1. However, HashMap
90     // doesn't easily support OwnPtr as a value.
91     typedef HashMap<TileMapKey, RefPtr<Tile>, DefaultHash<TileMapKey>::Hash, TileMapKeyTraits> TileMap;
92
93     void addTile(PassRefPtr<Tile>, int, int);
94     PassRefPtr<Tile> takeTile(int, int);
95     Tile* tileAt(int, int) const;
96     const TileMap& tiles() const { return m_tiles; }
97
98     // Grow layer size to contain this rectangle.
99     void growLayerToContain(const IntRect& contentRect);
100     void contentRectToTileIndices(const IntRect& contentRect, int &left, int &top, int &right, int &bottom) const;
101     IntRect contentRectToLayerRect(const IntRect& contentRect) const;
102     IntRect layerRectToContentRect(const IntRect& layerRect) const;
103     IntRect tileContentRect(const Tile*) const;
104     IntRect tileLayerRect(const Tile*) const;
105
106     void reset();
107
108 protected:
109     CCLayerTilingData(const IntSize& tileSize, BorderTexelOption);
110
111     TileMap m_tiles;
112     IntSize m_tileSize;
113     IntPoint m_layerPosition;
114     TilingData m_tilingData;
115 };
116
117 }
118
119 #endif // USE(ACCELERATED_COMPOSITING)
120
121 #endif