2 * Copyright (c) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #if USE(ACCELERATED_COMPOSITING) || ENABLE(ACCELERATED_2D_CANVAS)
35 #include "TilingData.h"
37 #include "FloatRect.h"
45 static int computeNumTiles(int maxTextureSize, int totalSize, int borderTexels)
47 if (maxTextureSize - 2 * borderTexels <= 0)
48 return totalSize > 0 && maxTextureSize >= totalSize ? 1 : 0;
50 int numTiles = max(1, 1 + (totalSize - 1 - 2 * borderTexels) / (maxTextureSize - 2 * borderTexels));
51 return totalSize > 0 ? numTiles : 0;
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)
63 void TilingData::setTotalSize(int totalSizeX, int totalSizeY)
65 m_totalSizeX = totalSizeX;
66 m_totalSizeY = totalSizeY;
70 void TilingData::setMaxTextureSize(int maxTextureSize)
72 m_maxTextureSize = maxTextureSize;
76 int TilingData::tileXIndexFromSrcCoord(int srcPos) const
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);
86 int TilingData::tileYIndexFromSrcCoord(int srcPos) const
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);
96 IntRect TilingData::tileBounds(int tile) const
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);
110 IntRect TilingData::tileBoundsWithBorder(int tile) const
112 IntRect bounds = tileBounds(tile);
114 if (m_borderTexels) {
116 int x2 = bounds.maxX();
118 int y2 = bounds.maxY();
120 if (tileXIndex(tile) > 0)
122 if (tileXIndex(tile) < (numTilesX() - 1))
124 if (tileYIndex(tile) > 0)
126 if (tileYIndex(tile) < (numTilesY() - 1))
129 bounds = IntRect(x1, y1, x2 - x1, y2 - y1);
135 FloatRect TilingData::tileBoundsNormalized(int tile) const
138 FloatRect bounds(tileBounds(tile));
139 bounds.scale(1.0f / m_totalSizeX, 1.0f / m_totalSizeY);
143 int TilingData::tilePositionX(int xIndex) const
145 ASSERT(xIndex >= 0 && xIndex < numTilesX());
148 for (int i = 0; i < xIndex; i++)
154 int TilingData::tilePositionY(int yIndex) const
156 ASSERT(yIndex >= 0 && yIndex < numTilesY());
159 for (int i = 0; i < yIndex; i++)
165 int TilingData::tileSizeX(int xIndex) const
167 ASSERT(xIndex >= 0 && xIndex < numTilesX());
169 if (!xIndex && m_numTilesX == 1)
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);
178 ASSERT_NOT_REACHED();
182 int TilingData::tileSizeY(int yIndex) const
184 ASSERT(yIndex >= 0 && yIndex < numTilesY());
186 if (!yIndex && m_numTilesY == 1)
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);
195 ASSERT_NOT_REACHED();
199 IntRect TilingData::overlappedTileIndices(const WebCore::IntRect &srcRect) const
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);
208 IntRect TilingData::overlappedTileIndices(const WebCore::FloatRect &srcRect) const
210 return overlappedTileIndices(enclosingIntRect(srcRect));
213 void TilingData::intersectDrawQuad(const FloatRect& srcRect, const FloatRect& dstRect, int tile,
214 FloatRect* newSrc, FloatRect* newDst) const
216 // Intersect with tile
217 FloatRect tileBounds = this->tileBounds(tile);
218 FloatRect srcRectIntersected = srcRect;
219 srcRectIntersected.intersect(tileBounds);
221 if (srcRectIntersected.isEmpty()) {
222 *newSrc = *newDst = FloatRect(0, 0, 0, 0);
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();
231 *newSrc = srcRectIntersected;
233 -tileBounds.x() + ((tileXIndex(tile) > 0) ? m_borderTexels : 0),
234 -tileBounds.y() + ((tileYIndex(tile) > 0) ? m_borderTexels : 0));
237 srcRectIntersectedNormX * dstRect.width() + dstRect.x(),
238 srcRectIntersectedNormY * dstRect.height() + dstRect.y(),
239 srcRectIntersectedNormW * dstRect.width(),
240 srcRectIntersectedNormH * dstRect.height());
243 IntPoint TilingData::textureOffset(int xIndex, int yIndex) const
245 int left = (!xIndex || m_numTilesX == 1) ? 0 : m_borderTexels;
246 int top = (!yIndex || m_numTilesY == 1) ? 0 : m_borderTexels;
248 return IntPoint(left, top);
251 void TilingData::recomputeNumTiles()
253 m_numTilesX = computeNumTiles(m_maxTextureSize, m_totalSizeX, m_borderTexels);
254 m_numTilesY = computeNumTiles(m_maxTextureSize, m_totalSizeY, m_borderTexels);