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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Copyright (C) 2009 Apple 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:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 APPLE COMPUTER, INC. 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 USE(ACCELERATED_COMPOSITING)
#include "WebTiledLayer.h"
#include "GraphicsLayer.h"
#include "WKCACFLayerRenderer.h"
namespace WebCore {
using namespace std;
#ifndef NDEBUG
void WebTiledLayer::internalCheckLayerConsistency()
{
WKCACFLayer::internalCheckLayerConsistency();
// Additionally make sure the tiled parent is valid
CFArrayRef sublayers = CACFLayerGetSublayers(layer());
// Make sure there is a tile parent and it is the same as we remember
size_t n = CFArrayGetCount(sublayers);
ASSERT(n > 0);
const void* element = CFArrayGetValueAtIndex(sublayers, 0);
ASSERT(m_tileParent.get() == element);
// Make sure the tile parent doesn't have user data. If it does, it is probably
// a WKCACFLayer in the wrong place.
ASSERT(!layer(m_tileParent.get()));
}
#endif
void WebTiledLayer::tileDisplayCallback(CACFLayerRef layer, CGContextRef context)
{
static_cast<WebTiledLayer*>(CACFLayerGetUserData(layer))->drawTile(layer, context);
}
PassRefPtr<WebTiledLayer> WebTiledLayer::create(const CGSize& tileSize, GraphicsLayer* owner)
{
ASSERT(WKCACFLayerRenderer::acceleratedCompositingAvailable());
return adoptRef(new WebTiledLayer(tileSize, owner));
}
WebTiledLayer::WebTiledLayer(const CGSize& tileSize, GraphicsLayer* owner)
: WebLayer(WKCACFLayer::Layer, owner)
, m_tileSize(tileSize)
, m_constrainedSize(constrainedSize(bounds().size))
{
// Tiled layers are placed in a child layer that is always the first child of the TiledLayer
m_tileParent.adoptCF(CACFLayerCreate(kCACFLayer));
CACFLayerInsertSublayer(layer(), m_tileParent.get(), 0);
updateTiles();
}
WebTiledLayer::~WebTiledLayer()
{
}
void WebTiledLayer::setBounds(const CGRect& rect)
{
if (CGRectEqualToRect(rect, bounds()))
return;
WebLayer::setBounds(rect);
m_constrainedSize = constrainedSize(rect.size);
updateTiles();
}
void WebTiledLayer::setFrame(const CGRect& rect)
{
if (CGRectEqualToRect(rect, frame()))
return;
WebLayer::setFrame(rect);
updateTiles();
}
void WebTiledLayer::internalSetNeedsDisplay(const CGRect* dirtyRect)
{
// FIXME: Only setNeedsDisplay for tiles that are currently visible
int numTileLayers = tileCount();
for (int i = 0; i < numTileLayers; ++i)
CACFLayerSetNeedsDisplay(tileAtIndex(i), dirtyRect);
if (m_owner->showRepaintCounter()) {
CGRect layerBounds = bounds();
CGRect indicatorRect = CGRectMake(layerBounds.origin.x, layerBounds.origin.y, 80, 25);
CACFLayerSetNeedsDisplay(tileAtIndex(0), &indicatorRect);
}
}
size_t WebTiledLayer::internalSublayerCount() const
{
ASSERT(WebLayer::internalSublayerCount() > 0);
// Subtract 1 to account for the tile parent layer
return WebLayer::internalSublayerCount() - 1;
}
void WebTiledLayer::internalRemoveAllSublayers()
{
// Restore the tile parent after removal
WebLayer::internalRemoveAllSublayers();
CACFLayerInsertSublayer(layer(), m_tileParent.get(), 0);
}
void WebTiledLayer::internalSetSublayers(const Vector<RefPtr<WKCACFLayer> >& sublayers)
{
// Preserve the tile parent after set
WebLayer::internalSetSublayers(sublayers);
CACFLayerInsertSublayer(layer(), m_tileParent.get(), 0);
}
void WebTiledLayer::internalInsertSublayer(PassRefPtr<WKCACFLayer> layer, size_t index)
{
// Add 1 to account for the tile parent layer
WebLayer::internalInsertSublayer(layer, index + 1);
}
WKCACFLayer* WebTiledLayer::internalSublayerAtIndex(int i) const
{
// Add 1 to account for the tile parent layer
return WebLayer::internalSublayerAtIndex(i + 1);
}
int WebTiledLayer::internalIndexOfSublayer(const WKCACFLayer* layer)
{
int i = WebLayer::internalIndexOfSublayer(layer);
// Add 1 to account for the tile parent layer (but be safe about it)
return (i > 0) ? i - 1 : -1;
}
CGSize WebTiledLayer::constrainedSize(const CGSize& size) const
{
const int cMaxTileCount = 512;
const float cSqrtMaxTileCount = sqrtf(cMaxTileCount);
CGSize constrainedSize = size;
int tileColumns = ceilf(constrainedSize.width / m_tileSize.width);
int tileRows = ceilf(constrainedSize.height / m_tileSize.height);
int numTiles = tileColumns * tileRows;
// If number of tiles vertically or horizontally is < sqrt(cMaxTileCount)
// just shorten the longer dimension. Otherwise shorten both dimensions
// according to the ratio of width to height
if (numTiles > cMaxTileCount) {
if (tileRows < cSqrtMaxTileCount)
tileColumns = floorf(cMaxTileCount / tileRows);
else if (tileColumns < cSqrtMaxTileCount)
tileRows = floorf(cMaxTileCount / tileColumns);
else {
tileRows = ceilf(sqrtf(cMaxTileCount * constrainedSize.height / constrainedSize.width));
tileColumns = floorf(cMaxTileCount / tileRows);
}
constrainedSize.width = tileColumns * m_tileSize.width;
constrainedSize.height = tileRows * m_tileSize.height;
}
return constrainedSize;
}
void WebTiledLayer::addTile()
{
RetainPtr<CACFLayerRef> newLayer(AdoptCF, CACFLayerCreate(kCACFLayer));
CACFLayerSetAnchorPoint(newLayer.get(), CGPointMake(0, 1));
CACFLayerSetUserData(newLayer.get(), this);
CACFLayerSetDisplayCallback(newLayer.get(), tileDisplayCallback);
CFArrayRef sublayers = CACFLayerGetSublayers(m_tileParent.get());
CACFLayerInsertSublayer(m_tileParent.get(), newLayer.get(), sublayers ? CFArrayGetCount(sublayers) : 0);
if (m_owner->showDebugBorders()) {
CGColorRef borderColor = CGColorCreateGenericRGB(0.5, 0, 0.5, 0.7);
CACFLayerSetBorderColor(newLayer.get(), borderColor);
CGColorRelease(borderColor);
CACFLayerSetBorderWidth(newLayer.get(), 2);
}
}
void WebTiledLayer::removeTile()
{
CACFLayerRemoveFromSuperlayer(tileAtIndex(tileCount() - 1));
}
CACFLayerRef WebTiledLayer::tileAtIndex(int index)
{
CFArrayRef sublayers = CACFLayerGetSublayers(m_tileParent.get());
if (!sublayers || index < 0 || index >= tileCount() )
return 0;
return static_cast<CACFLayerRef>(const_cast<void*>(CFArrayGetValueAtIndex(sublayers, index)));
}
int WebTiledLayer::tileCount() const
{
CFArrayRef sublayers = CACFLayerGetSublayers(m_tileParent.get());
return sublayers ? CFArrayGetCount(sublayers) : 0;
}
void WebTiledLayer::updateTiles()
{
// FIXME: In addition to redoing the number of tiles, we need to only render and have backing
// store for visible layers
int numTilesHorizontal = ceil(m_constrainedSize.width / m_tileSize.width);
int numTilesVertical = ceil(m_constrainedSize.height / m_tileSize.height);
int numTilesTotal = numTilesHorizontal * numTilesVertical;
int numTilesToChange = numTilesTotal - tileCount();
if (numTilesToChange >= 0) {
// Add new tiles
for (int i = 0; i < numTilesToChange; ++i)
addTile();
} else {
// Remove old tiles
numTilesToChange = -numTilesToChange;
for (int i = 0; i < numTilesToChange; ++i)
removeTile();
}
// Set coordinates for all tiles
CFArrayRef tileArray = CACFLayerGetSublayers(m_tileParent.get());
for (int i = 0; i < numTilesHorizontal; ++i) {
for (int j = 0; j < numTilesVertical; ++j) {
CACFLayerRef tile = static_cast<CACFLayerRef>(const_cast<void*>(CFArrayGetValueAtIndex(tileArray, i * numTilesVertical + j)));
CACFLayerSetPosition(tile, CGPointMake(i * m_tileSize.width, j * m_tileSize.height));
int width = min(m_tileSize.width, m_constrainedSize.width - i * m_tileSize.width);
int height = min(m_tileSize.height, m_constrainedSize.height - j * m_tileSize.height);
CACFLayerSetBounds(tile, CGRectMake(i * m_tileSize.width, j * m_tileSize.height, width, height));
// Flip Y to compensate for the flipping that happens during render to match the CG context coordinate space
CATransform3D transform = CATransform3DMakeScale(1, -1, 1);
CATransform3DTranslate(transform, 0, height, 0);
CACFLayerSetTransform(tile, transform);
#ifndef NDEBUG
String name = "Tile (" + String::number(i) + "," + String::number(j) + ")";
CACFLayerSetName(tile, RetainPtr<CFStringRef>(AdoptCF, name.createCFString()).get());
#endif
}
}
}
void WebTiledLayer::drawTile(CACFLayerRef tile, CGContextRef context)
{
CGPoint tilePosition = CACFLayerGetPosition(tile);
CGRect tileBounds = CACFLayerGetBounds(tile);
CGContextSaveGState(context);
// Transform context to be at the origin of the parent layer
CGContextTranslateCTM(context, -tilePosition.x, -tilePosition.y);
// Set the context clipping rectangle to the current tile
CGContextClipToRect(context, CGRectMake(tilePosition.x, tilePosition.y, tileBounds.size.width, tileBounds.size.height));
if (m_owner->contentsOrientation() == WebCore::GraphicsLayer::CompositingCoordinatesTopDown) {
// If the layer is rendering top-down, it will flip the coordinates in y. Tiled layers are
// already flipping, so we need to undo that here.
CGContextTranslateCTM(context, 0, bounds().size.height);
CGContextScaleCTM(context, 1, -1);
}
// Draw the tile
drawInContext(context);
CGContextRestoreGState(context);
}
}
#endif // USE(ACCELERATED_COMPOSITING)
|