initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / gpu / TilingData.cpp
1 /*
2  * Copyright (c) 2010, 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 #include "config.h"
32
33 #if USE(ACCELERATED_COMPOSITING) || ENABLE(ACCELERATED_2D_CANVAS)
34
35 #include "TilingData.h"
36
37 #include "FloatRect.h"
38 #include "IntRect.h"
39 #include <algorithm>
40
41 using namespace std;
42
43 namespace WebCore {
44
45 static int computeNumTiles(int maxTextureSize, int totalSize, int borderTexels)
46 {
47     if (maxTextureSize - 2 * borderTexels <= 0)
48         return totalSize > 0 && maxTextureSize >= totalSize ? 1 : 0;
49
50     int numTiles = max(1, 1 + (totalSize - 1 - 2 * borderTexels) / (maxTextureSize - 2 * borderTexels));
51     return totalSize > 0 ? numTiles : 0;
52 }
53
54 TilingData::TilingData(int maxTextureSize, int totalSizeX, int totalSizeY, bool hasBorderTexels)
55     : m_maxTextureSize(maxTextureSize)
56     , m_totalSizeX(totalSizeX)
57     , m_totalSizeY(totalSizeY)
58     , m_borderTexels(hasBorderTexels ? 1 : 0)
59 {
60     recomputeNumTiles();
61 }
62
63 void TilingData::setTotalSize(int totalSizeX, int totalSizeY)
64 {
65     m_totalSizeX = totalSizeX;
66     m_totalSizeY = totalSizeY;
67     recomputeNumTiles();
68 }
69
70 void TilingData::setMaxTextureSize(int maxTextureSize)
71 {
72     m_maxTextureSize = maxTextureSize;
73     recomputeNumTiles();
74 }
75
76 int TilingData::tileXIndexFromSrcCoord(int srcPos) const
77 {
78     if (numTilesX() <= 1)
79         return 0;
80
81     ASSERT(m_maxTextureSize - 2 * m_borderTexels);
82     int x = (srcPos - m_borderTexels) / (m_maxTextureSize - 2 * m_borderTexels);
83     return min(max(x, 0), numTilesX() - 1);
84 }
85
86 int TilingData::tileYIndexFromSrcCoord(int srcPos) const
87 {
88     if (numTilesY() <= 1)
89         return 0;
90
91     ASSERT(m_maxTextureSize - 2 * m_borderTexels);
92     int y = (srcPos - m_borderTexels) / (m_maxTextureSize - 2 * m_borderTexels);
93     return min(max(y, 0), numTilesY() - 1);
94 }
95
96 IntRect TilingData::tileBounds(int tile) const
97 {
98     assertTile(tile);
99     int ix = tileXIndex(tile);
100     int iy = tileYIndex(tile);
101     int x = tilePositionX(ix);
102     int y = tilePositionY(iy);
103     int width = tileSizeX(ix);
104     int height = tileSizeY(iy);
105     ASSERT(x >= 0 && y >= 0 && width >= 0 && height >= 0);
106     ASSERT(x <= totalSizeX() && y <= totalSizeY());
107     return IntRect(x, y, width, height);
108 }
109
110 IntRect TilingData::tileBoundsWithBorder(int tile) const
111 {
112     IntRect bounds = tileBounds(tile);
113
114     if (m_borderTexels) {
115         int x1 = bounds.x();
116         int x2 = bounds.maxX();
117         int y1 = bounds.y();
118         int y2 = bounds.maxY();
119
120         if (tileXIndex(tile) > 0)
121             x1--;
122         if (tileXIndex(tile) < (numTilesX() - 1))
123             x2++;
124         if (tileYIndex(tile) > 0)
125             y1--;
126         if (tileYIndex(tile) < (numTilesY() - 1))
127             y2++;
128
129         bounds = IntRect(x1, y1, x2 - x1, y2 - y1);
130     }
131
132     return bounds;
133 }
134
135 FloatRect TilingData::tileBoundsNormalized(int tile) const
136 {
137     assertTile(tile);
138     FloatRect bounds(tileBounds(tile));
139     bounds.scale(1.0f / m_totalSizeX, 1.0f / m_totalSizeY);
140     return bounds;
141 }
142
143 int TilingData::tilePositionX(int xIndex) const
144 {
145     ASSERT(xIndex >= 0 && xIndex < numTilesX());
146
147     int pos = 0;
148     for (int i = 0; i < xIndex; i++)
149         pos += tileSizeX(i);
150
151     return pos;
152 }
153
154 int TilingData::tilePositionY(int yIndex) const
155 {
156     ASSERT(yIndex >= 0 && yIndex < numTilesY());
157
158     int pos = 0;
159     for (int i = 0; i < yIndex; i++)
160         pos += tileSizeY(i);
161
162     return pos;
163 }
164
165 int TilingData::tileSizeX(int xIndex) const
166 {
167     ASSERT(xIndex >= 0 && xIndex < numTilesX());
168
169     if (!xIndex && m_numTilesX == 1)
170         return m_totalSizeX;
171     if (!xIndex && m_numTilesX > 1)
172         return m_maxTextureSize - m_borderTexels;
173     if (xIndex < numTilesX() - 1)
174         return m_maxTextureSize - 2 * m_borderTexels;
175     if (xIndex == numTilesX() - 1)
176         return m_totalSizeX - tilePositionX(xIndex);
177
178     ASSERT_NOT_REACHED();
179     return 0;
180 }
181
182 int TilingData::tileSizeY(int yIndex) const
183 {
184     ASSERT(yIndex >= 0 && yIndex < numTilesY());
185
186     if (!yIndex && m_numTilesY == 1)
187         return m_totalSizeY;
188     if (!yIndex && m_numTilesY > 1)
189         return m_maxTextureSize - m_borderTexels;
190     if (yIndex < numTilesY() - 1)
191         return m_maxTextureSize - 2 * m_borderTexels;
192     if (yIndex == numTilesY() - 1)
193         return m_totalSizeY - tilePositionY(yIndex);
194
195     ASSERT_NOT_REACHED();
196     return 0;
197 }
198
199 IntRect TilingData::overlappedTileIndices(const WebCore::IntRect &srcRect) const
200 {
201     int x = tileXIndexFromSrcCoord(srcRect.x());
202     int y = tileYIndexFromSrcCoord(srcRect.y());
203     int r = tileXIndexFromSrcCoord(srcRect.maxX());
204     int b = tileYIndexFromSrcCoord(srcRect.maxY());
205     return IntRect(x, y, r - x, b - y);
206 }
207
208 IntRect TilingData::overlappedTileIndices(const WebCore::FloatRect &srcRect) const
209 {
210     return overlappedTileIndices(enclosingIntRect(srcRect));
211 }
212
213 void TilingData::intersectDrawQuad(const FloatRect& srcRect, const FloatRect& dstRect, int tile,
214                                    FloatRect* newSrc, FloatRect* newDst) const
215 {
216     // Intersect with tile
217     FloatRect tileBounds = this->tileBounds(tile);
218     FloatRect srcRectIntersected = srcRect;
219     srcRectIntersected.intersect(tileBounds);
220
221     if (srcRectIntersected.isEmpty()) {
222         *newSrc = *newDst = FloatRect(0, 0, 0, 0);
223         return;
224     }
225
226     float srcRectIntersectedNormX = (srcRectIntersected.x() - srcRect.x()) / srcRect.width();
227     float srcRectIntersectedNormY = (srcRectIntersected.y() - srcRect.y()) / srcRect.height();
228     float srcRectIntersectedNormW = srcRectIntersected.width() / srcRect.width();
229     float srcRectIntersectedNormH = srcRectIntersected.height() / srcRect.height();
230
231     *newSrc = srcRectIntersected;
232     newSrc->move(
233         -tileBounds.x() + ((tileXIndex(tile) > 0) ? m_borderTexels : 0),
234         -tileBounds.y() + ((tileYIndex(tile) > 0) ? m_borderTexels : 0));
235
236     *newDst = FloatRect(
237         srcRectIntersectedNormX * dstRect.width() + dstRect.x(),
238         srcRectIntersectedNormY * dstRect.height() + dstRect.y(),
239         srcRectIntersectedNormW * dstRect.width(),
240         srcRectIntersectedNormH * dstRect.height());
241 }
242
243 IntPoint TilingData::textureOffset(int xIndex, int yIndex) const
244 {
245     int left = (!xIndex || m_numTilesX == 1) ? 0 : m_borderTexels;
246     int top = (!yIndex || m_numTilesY == 1) ? 0 : m_borderTexels;
247
248     return IntPoint(left, top);
249 }
250
251 void TilingData::recomputeNumTiles()
252 {
253     m_numTilesX = computeNumTiles(m_maxTextureSize, m_totalSizeX, m_borderTexels);
254     m_numTilesY = computeNumTiles(m_maxTextureSize, m_totalSizeY, m_borderTexels);
255 }
256
257 }
258
259 #endif