1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
* Copyright (c) 2010, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(ACCELERATED_2D_CANVAS)
#include "TilingData.h"
#include "FloatRect.h"
#include "IntRect.h"
#include <algorithm>
using namespace std;
namespace WebCore {
static int computeNumTiles(int maxTextureSize, int totalSize, int borderTexels)
{
if (maxTextureSize - 2 * borderTexels <= 0)
return totalSize > 0 && maxTextureSize >= totalSize ? 1 : 0;
int numTiles = max(1, 1 + (totalSize - 1 - 2 * borderTexels) / (maxTextureSize - 2 * borderTexels));
return totalSize > 0 ? numTiles : 0;
}
TilingData::TilingData(int maxTextureSize, int totalSizeX, int totalSizeY, bool hasBorderTexels)
: m_maxTextureSize(maxTextureSize)
, m_totalSizeX(totalSizeX)
, m_totalSizeY(totalSizeY)
, m_borderTexels(hasBorderTexels ? 1 : 0)
{
recomputeNumTiles();
}
void TilingData::setTotalSize(int totalSizeX, int totalSizeY)
{
m_totalSizeX = totalSizeX;
m_totalSizeY = totalSizeY;
recomputeNumTiles();
}
void TilingData::setMaxTextureSize(int maxTextureSize)
{
m_maxTextureSize = maxTextureSize;
recomputeNumTiles();
}
int TilingData::tileXIndexFromSrcCoord(int srcPos) const
{
int x = (srcPos - m_borderTexels) / (m_maxTextureSize - 2 * m_borderTexels);
return min(max(x, 0), numTilesX() - 1);
}
int TilingData::tileYIndexFromSrcCoord(int srcPos) const
{
int y = (srcPos - m_borderTexels) / (m_maxTextureSize - 2 * m_borderTexels);
return min(max(y, 0), numTilesY() - 1);
}
IntRect TilingData::tileBounds(int tile) const
{
assertTile(tile);
int ix = tileXIndex(tile);
int iy = tileYIndex(tile);
int x = tilePositionX(ix);
int y = tilePositionY(iy);
int width = tileSizeX(ix);
int height = tileSizeY(iy);
ASSERT(x >= 0 && y >= 0 && width >= 0 && height >= 0);
ASSERT(x <= totalSizeX() && y <= totalSizeY());
return IntRect(x, y, width, height);
}
IntRect TilingData::tileBoundsWithBorder(int tile) const
{
IntRect bounds = tileBounds(tile);
if (m_borderTexels) {
int x1 = bounds.x();
int x2 = bounds.maxX();
int y1 = bounds.y();
int y2 = bounds.maxY();
if (tileXIndex(tile) > 0)
x1--;
if (tileXIndex(tile) < (numTilesX() - 1))
x2++;
if (tileYIndex(tile) > 0)
y1--;
if (tileYIndex(tile) < (numTilesY() - 1))
y2++;
bounds = IntRect(x1, y1, x2 - x1, y2 - y1);
}
return bounds;
}
FloatRect TilingData::tileBoundsNormalized(int tile) const
{
assertTile(tile);
FloatRect bounds(tileBounds(tile));
bounds.scale(1.0f / m_totalSizeX, 1.0f / m_totalSizeY);
return bounds;
}
int TilingData::tilePositionX(int xIndex) const
{
ASSERT(xIndex >= 0 && xIndex < numTilesX());
int pos = 0;
for (int i = 0; i < xIndex; i++)
pos += tileSizeX(i);
return pos;
}
int TilingData::tilePositionY(int yIndex) const
{
ASSERT(yIndex >= 0 && yIndex < numTilesY());
int pos = 0;
for (int i = 0; i < yIndex; i++)
pos += tileSizeY(i);
return pos;
}
int TilingData::tileSizeX(int xIndex) const
{
ASSERT(xIndex >= 0 && xIndex < numTilesX());
if (!xIndex && m_numTilesX == 1)
return m_totalSizeX;
if (!xIndex && m_numTilesX > 1)
return m_maxTextureSize - m_borderTexels;
if (xIndex < numTilesX() - 1)
return m_maxTextureSize - 2 * m_borderTexels;
if (xIndex == numTilesX() - 1)
return m_totalSizeX - tilePositionX(xIndex);
ASSERT_NOT_REACHED();
return 0;
}
int TilingData::tileSizeY(int yIndex) const
{
ASSERT(yIndex >= 0 && yIndex < numTilesY());
if (!yIndex && m_numTilesY == 1)
return m_totalSizeY;
if (!yIndex && m_numTilesY > 1)
return m_maxTextureSize - m_borderTexels;
if (yIndex < numTilesY() - 1)
return m_maxTextureSize - 2 * m_borderTexels;
if (yIndex == numTilesY() - 1)
return m_totalSizeY - tilePositionY(yIndex);
ASSERT_NOT_REACHED();
return 0;
}
IntRect TilingData::overlappedTileIndices(const WebCore::IntRect &srcRect) const
{
int x = tileXIndexFromSrcCoord(srcRect.x());
int y = tileYIndexFromSrcCoord(srcRect.y());
int r = tileXIndexFromSrcCoord(srcRect.maxX());
int b = tileYIndexFromSrcCoord(srcRect.maxY());
return IntRect(x, y, r - x, b - y);
}
IntRect TilingData::overlappedTileIndices(const WebCore::FloatRect &srcRect) const
{
return overlappedTileIndices(enclosingIntRect(srcRect));
}
void TilingData::intersectDrawQuad(const FloatRect& srcRect, const FloatRect& dstRect, int tile,
FloatRect* newSrc, FloatRect* newDst) const
{
// Intersect with tile
FloatRect tileBounds = this->tileBounds(tile);
FloatRect srcRectIntersected = srcRect;
srcRectIntersected.intersect(tileBounds);
if (srcRectIntersected.isEmpty()) {
*newSrc = *newDst = FloatRect(0, 0, 0, 0);
return;
}
float srcRectIntersectedNormX = (srcRectIntersected.x() - srcRect.x()) / srcRect.width();
float srcRectIntersectedNormY = (srcRectIntersected.y() - srcRect.y()) / srcRect.height();
float srcRectIntersectedNormW = srcRectIntersected.width() / srcRect.width();
float srcRectIntersectedNormH = srcRectIntersected.height() / srcRect.height();
*newSrc = srcRectIntersected;
newSrc->move(
-tileBounds.x() + ((tileXIndex(tile) > 0) ? m_borderTexels : 0),
-tileBounds.y() + ((tileYIndex(tile) > 0) ? m_borderTexels : 0));
*newDst = FloatRect(
srcRectIntersectedNormX * dstRect.width() + dstRect.x(),
srcRectIntersectedNormY * dstRect.height() + dstRect.y(),
srcRectIntersectedNormW * dstRect.width(),
srcRectIntersectedNormH * dstRect.height());
}
IntPoint TilingData::textureOffset(int xIndex, int yIndex) const
{
int left = (!xIndex || m_numTilesX == 1) ? 0 : m_borderTexels;
int top = (!yIndex || m_numTilesY == 1) ? 0 : m_borderTexels;
return IntPoint(left, top);
}
void TilingData::recomputeNumTiles()
{
m_numTilesX = computeNumTiles(m_maxTextureSize, m_totalSizeX, m_borderTexels);
m_numTilesY = computeNumTiles(m_maxTextureSize, m_totalSizeY, m_borderTexels);
}
}
#endif
|