initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / cc / CCLayerTilingData.cpp
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 #include "config.h"
28
29 #if USE(ACCELERATED_COMPOSITING)
30
31 #include "cc/CCLayerTilingData.h"
32
33 using namespace std;
34
35 namespace WebCore {
36
37 PassOwnPtr<CCLayerTilingData> CCLayerTilingData::create(const IntSize& tileSize, BorderTexelOption border)
38 {
39     return adoptPtr(new CCLayerTilingData(tileSize, border));
40 }
41
42 CCLayerTilingData::CCLayerTilingData(const IntSize& tileSize, BorderTexelOption border)
43     : m_tilingData(max(tileSize.width(), tileSize.height()), 0, 0, border == HasBorderTexels)
44 {
45     setTileSize(tileSize);
46 }
47
48 void CCLayerTilingData::setTileSize(const IntSize& size)
49 {
50     if (m_tileSize == size)
51         return;
52
53     reset();
54
55     m_tileSize = size;
56     m_tilingData.setMaxTextureSize(max(size.width(), size.height()));
57 }
58
59 const CCLayerTilingData& CCLayerTilingData::operator=(const CCLayerTilingData& tiler)
60 {
61     m_tileSize = tiler.m_tileSize;
62     m_layerPosition = tiler.m_layerPosition;
63     m_tilingData = tiler.m_tilingData;
64
65     return *this;
66 }
67
68 void CCLayerTilingData::addTile(PassRefPtr<Tile> tile, int i, int j)
69 {
70     ASSERT(!tileAt(i, j));
71     tile->moveTo(i, j);
72     m_tiles.add(make_pair(i, j), tile);
73 }
74
75 PassRefPtr<CCLayerTilingData::Tile> CCLayerTilingData::takeTile(int i, int j)
76 {
77     return m_tiles.take(make_pair(i, j));
78 }
79
80 CCLayerTilingData::Tile* CCLayerTilingData::tileAt(int i, int j) const
81 {
82     Tile* tile = m_tiles.get(make_pair(i, j)).get();
83     ASSERT(!tile || tile->refCount() == 1);
84     return tile;
85 }
86
87 void CCLayerTilingData::reset()
88 {
89     m_tiles.clear();
90     m_tilingData.setTotalSize(0, 0);
91 }
92
93 void CCLayerTilingData::contentRectToTileIndices(const IntRect& contentRect, int& left, int& top, int& right, int& bottom) const
94 {
95     const IntRect layerRect = contentRectToLayerRect(contentRect);
96
97     left = m_tilingData.tileXIndexFromSrcCoord(layerRect.x());
98     top = m_tilingData.tileYIndexFromSrcCoord(layerRect.y());
99     right = m_tilingData.tileXIndexFromSrcCoord(layerRect.maxX() - 1);
100     bottom = m_tilingData.tileYIndexFromSrcCoord(layerRect.maxY() - 1);
101 }
102
103 IntRect CCLayerTilingData::contentRectToLayerRect(const IntRect& contentRect) const
104 {
105     IntPoint pos(contentRect.x() - m_layerPosition.x(), contentRect.y() - m_layerPosition.y());
106     IntRect layerRect(pos, contentRect.size());
107
108     // Clip to the position.
109     if (pos.x() < 0 || pos.y() < 0)
110         layerRect = IntRect(IntPoint(0, 0), IntSize(contentRect.width() + pos.x(), contentRect.height() + pos.y()));
111     return layerRect;
112 }
113
114 IntRect CCLayerTilingData::layerRectToContentRect(const IntRect& layerRect) const
115 {
116     IntRect contentRect = layerRect;
117     contentRect.move(m_layerPosition.x(), m_layerPosition.y());
118     return contentRect;
119 }
120
121 IntRect CCLayerTilingData::tileContentRect(const Tile* tile) const
122 {
123     IntRect contentRect = tileLayerRect(tile);
124     contentRect.move(m_layerPosition.x(), m_layerPosition.y());
125     return contentRect;
126 }
127
128 IntRect CCLayerTilingData::tileLayerRect(const Tile* tile) const
129 {
130     const int index = m_tilingData.tileIndex(tile->i(), tile->j());
131     IntRect layerRect = m_tilingData.tileBoundsWithBorder(index);
132     layerRect.setSize(m_tileSize);
133     return layerRect;
134 }
135
136 void CCLayerTilingData::setLayerPosition(const IntPoint& layerPosition)
137 {
138     m_layerPosition = layerPosition;
139 }
140
141 void CCLayerTilingData::growLayerToContain(const IntRect& contentRect)
142 {
143     // Grow the tile array to contain this content rect.
144     IntRect layerRect = contentRectToLayerRect(contentRect);
145     IntSize rectSize = IntSize(layerRect.maxX(), layerRect.maxY());
146
147     IntSize oldLayerSize(m_tilingData.totalSizeX(), m_tilingData.totalSizeY());
148     IntSize newSize = rectSize.expandedTo(oldLayerSize);
149     m_tilingData.setTotalSize(newSize.width(), newSize.height());
150 }
151
152 } // namespace WebCore
153
154 #endif // USE(ACCELERATED_COMPOSITING)