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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
* Copyright (C) 2012 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 COMPUTER, 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"
#include "core/rendering/RenderGeometryMap.h"
#include "core/frame/LocalFrame.h"
#include "core/rendering/RenderLayer.h"
#include "core/rendering/RenderView.h"
#include "platform/geometry/TransformState.h"
#include "wtf/TemporaryChange.h"
namespace blink {
RenderGeometryMap::RenderGeometryMap(MapCoordinatesFlags flags)
: m_insertionPosition(kNotFound)
, m_nonUniformStepsCount(0)
, m_transformedStepsCount(0)
, m_fixedStepsCount(0)
, m_mapCoordinatesFlags(flags)
{
}
RenderGeometryMap::~RenderGeometryMap()
{
}
void RenderGeometryMap::mapToContainer(TransformState& transformState, const RenderLayerModelObject* container) const
{
// If the mapping includes something like columns, we have to go via renderers.
if (hasNonUniformStep()) {
m_mapping.last().m_renderer->mapLocalToContainer(container, transformState, ApplyContainerFlip | m_mapCoordinatesFlags);
transformState.flatten();
return;
}
bool inFixed = false;
#if ENABLE(ASSERT)
bool foundContainer = !container || (m_mapping.size() && m_mapping[0].m_renderer == container);
#endif
for (int i = m_mapping.size() - 1; i >= 0; --i) {
const RenderGeometryMapStep& currentStep = m_mapping[i];
// If container is the root RenderView (step 0) we want to apply its fixed position offset.
if (i > 0 && currentStep.m_renderer == container) {
#if ENABLE(ASSERT)
foundContainer = true;
#endif
break;
}
// If this box has a transform, it acts as a fixed position container
// for fixed descendants, which prevents the propagation of 'fixed'
// unless the layer itself is also fixed position.
if (i && currentStep.m_hasTransform && !currentStep.m_isFixedPosition)
inFixed = false;
else if (currentStep.m_isFixedPosition)
inFixed = true;
ASSERT(!i == isTopmostRenderView(currentStep.m_renderer));
if (!i) {
// A null container indicates mapping through the root RenderView, so including its transform (the page scale).
if (!container && currentStep.m_transform)
transformState.applyTransform(*currentStep.m_transform.get());
} else {
TransformState::TransformAccumulation accumulate = currentStep.m_accumulatingTransform ? TransformState::AccumulateTransform : TransformState::FlattenTransform;
if (currentStep.m_transform)
transformState.applyTransform(*currentStep.m_transform.get(), accumulate);
else
transformState.move(currentStep.m_offset.width(), currentStep.m_offset.height(), accumulate);
}
if (inFixed && !currentStep.m_offsetForFixedPosition.isZero()) {
ASSERT(currentStep.m_renderer->isRenderView());
transformState.move(currentStep.m_offsetForFixedPosition);
}
}
ASSERT(foundContainer);
transformState.flatten();
}
FloatPoint RenderGeometryMap::mapToContainer(const FloatPoint& p, const RenderLayerModelObject* container) const
{
FloatPoint result;
if (!hasFixedPositionStep() && !hasTransformStep() && !hasNonUniformStep() && (!container || (m_mapping.size() && container == m_mapping[0].m_renderer)))
result = p + m_accumulatedOffset;
else {
TransformState transformState(TransformState::ApplyTransformDirection, p);
mapToContainer(transformState, container);
result = transformState.lastPlanarPoint();
}
#if ENABLE(ASSERT)
if (m_mapping.size() > 0) {
const RenderObject* lastRenderer = m_mapping.last().m_renderer;
const RenderLayer* layer = lastRenderer->enclosingLayer();
// Bounds for invisible layers are intentionally not calculated, and are
// therefore not necessarily expected to be correct here. This is ok,
// because they will be recomputed if the layer becomes visible.
if (!layer || !layer->subtreeIsInvisible()) {
FloatPoint rendererMappedResult = lastRenderer->localToContainerPoint(p, container, m_mapCoordinatesFlags);
ASSERT(roundedIntPoint(rendererMappedResult) == roundedIntPoint(result));
}
}
#endif
return result;
}
#ifndef NDEBUG
// Handy function to call from gdb while debugging mismatched point/rect errors.
void RenderGeometryMap::dumpSteps() const
{
fprintf(stderr, "RenderGeometryMap::dumpSteps accumulatedOffset=%d,%d\n", m_accumulatedOffset.width().toInt(), m_accumulatedOffset.height().toInt());
for (int i = m_mapping.size() - 1; i >= 0; --i) {
fprintf(stderr, " [%d] %s: offset=%d,%d", i, m_mapping[i].m_renderer->debugName().ascii().data(), m_mapping[i].m_offset.width().toInt(), m_mapping[i].m_offset.height().toInt());
if (m_mapping[i].m_hasTransform)
fprintf(stderr, " hasTransform");
fprintf(stderr, "\n");
}
}
#endif
FloatQuad RenderGeometryMap::mapToContainer(const FloatRect& rect, const RenderLayerModelObject* container) const
{
FloatQuad result;
if (!hasFixedPositionStep() && !hasTransformStep() && !hasNonUniformStep() && (!container || (m_mapping.size() && container == m_mapping[0].m_renderer))) {
result = rect;
result.move(m_accumulatedOffset);
} else {
TransformState transformState(TransformState::ApplyTransformDirection, rect.center(), rect);
mapToContainer(transformState, container);
result = transformState.lastPlanarQuad();
}
#if ENABLE(ASSERT)
if (m_mapping.size() > 0) {
const RenderObject* lastRenderer = m_mapping.last().m_renderer;
const RenderLayer* layer = lastRenderer->enclosingLayer();
// Bounds for invisible layers are intentionally not calculated, and are
// therefore not necessarily expected to be correct here. This is ok,
// because they will be recomputed if the layer becomes visible.
if (!layer->subtreeIsInvisible() && lastRenderer->style()->visibility() == VISIBLE) {
FloatRect rendererMappedResult = lastRenderer->localToContainerQuad(rect, container, m_mapCoordinatesFlags).boundingBox();
// Inspector creates renderers with negative width <https://bugs.webkit.org/show_bug.cgi?id=87194>.
// Taking FloatQuad bounds avoids spurious assertions because of that.
ASSERT(enclosingIntRect(rendererMappedResult) == enclosingIntRect(result.boundingBox()));
}
}
#endif
return result;
}
void RenderGeometryMap::pushMappingsToAncestor(const RenderObject* renderer, const RenderLayerModelObject* ancestorRenderer)
{
// We need to push mappings in reverse order here, so do insertions rather than appends.
TemporaryChange<size_t> positionChange(m_insertionPosition, m_mapping.size());
do {
renderer = renderer->pushMappingToContainer(ancestorRenderer, *this);
} while (renderer && renderer != ancestorRenderer);
ASSERT(m_mapping.isEmpty() || isTopmostRenderView(m_mapping[0].m_renderer));
}
static bool canMapBetweenRenderers(const RenderObject* renderer, const RenderObject* ancestor)
{
for (const RenderObject* current = renderer; ; current = current->parent()) {
const RenderStyle* style = current->style();
if (style->position() == FixedPosition || style->isFlippedBlocksWritingMode())
return false;
if (current->hasColumns() || current->hasTransformRelatedProperty() || current->isRenderFlowThread() || current->isSVGRoot())
return false;
if (current == ancestor)
break;
}
return true;
}
void RenderGeometryMap::pushMappingsToAncestor(const RenderLayer* layer, const RenderLayer* ancestorLayer)
{
const RenderObject* renderer = layer->renderer();
bool crossDocument = ancestorLayer && layer->renderer()->frame() != ancestorLayer->renderer()->frame();
ASSERT(!crossDocument || m_mapCoordinatesFlags & TraverseDocumentBoundaries);
// We have to visit all the renderers to detect flipped blocks. This might defeat the gains
// from mapping via layers.
bool canConvertInLayerTree = (ancestorLayer && !crossDocument) ? canMapBetweenRenderers(layer->renderer(), ancestorLayer->renderer()) : false;
// fprintf(stderr, "RenderGeometryMap::pushMappingsToAncestor from layer %p to layer %p, canConvertInLayerTree=%d\n", layer, ancestorLayer, canConvertInLayerTree);
if (canConvertInLayerTree) {
LayoutPoint layerOffset;
layer->convertToLayerCoords(ancestorLayer, layerOffset);
// The RenderView must be pushed first.
if (!m_mapping.size()) {
ASSERT(ancestorLayer->renderer()->isRenderView());
pushMappingsToAncestor(ancestorLayer->renderer(), 0);
}
TemporaryChange<size_t> positionChange(m_insertionPosition, m_mapping.size());
bool accumulatingTransform = layer->renderer()->style()->preserves3D() || ancestorLayer->renderer()->style()->preserves3D();
push(renderer, toLayoutSize(layerOffset), accumulatingTransform, /*isNonUniform*/ false, /*isFixedPosition*/ false, /*hasTransform*/ false);
return;
}
const RenderLayerModelObject* ancestorRenderer = ancestorLayer ? ancestorLayer->renderer() : 0;
pushMappingsToAncestor(renderer, ancestorRenderer);
}
void RenderGeometryMap::push(const RenderObject* renderer, const LayoutSize& offsetFromContainer, bool accumulatingTransform, bool isNonUniform, bool isFixedPosition, bool hasTransform, LayoutSize offsetForFixedPosition)
{
// fprintf(stderr, "RenderGeometryMap::push %p %d,%d isNonUniform=%d\n", renderer, offsetFromContainer.width().toInt(), offsetFromContainer.height().toInt(), isNonUniform);
ASSERT(m_insertionPosition != kNotFound);
ASSERT(!renderer->isRenderView() || !m_insertionPosition || m_mapCoordinatesFlags & TraverseDocumentBoundaries);
ASSERT(offsetForFixedPosition.isZero() || renderer->isRenderView());
m_mapping.insert(m_insertionPosition, RenderGeometryMapStep(renderer, accumulatingTransform, isNonUniform, isFixedPosition, hasTransform));
RenderGeometryMapStep& step = m_mapping[m_insertionPosition];
step.m_offset = offsetFromContainer;
step.m_offsetForFixedPosition = offsetForFixedPosition;
stepInserted(step);
}
void RenderGeometryMap::push(const RenderObject* renderer, const TransformationMatrix& t, bool accumulatingTransform, bool isNonUniform, bool isFixedPosition, bool hasTransform, LayoutSize offsetForFixedPosition)
{
ASSERT(m_insertionPosition != kNotFound);
ASSERT(!renderer->isRenderView() || !m_insertionPosition || m_mapCoordinatesFlags & TraverseDocumentBoundaries);
ASSERT(offsetForFixedPosition.isZero() || renderer->isRenderView());
m_mapping.insert(m_insertionPosition, RenderGeometryMapStep(renderer, accumulatingTransform, isNonUniform, isFixedPosition, hasTransform));
RenderGeometryMapStep& step = m_mapping[m_insertionPosition];
step.m_offsetForFixedPosition = offsetForFixedPosition;
if (!t.isIntegerTranslation())
step.m_transform = adoptPtr(new TransformationMatrix(t));
else
step.m_offset = LayoutSize(t.e(), t.f());
stepInserted(step);
}
void RenderGeometryMap::popMappingsToAncestor(const RenderLayerModelObject* ancestorRenderer)
{
ASSERT(m_mapping.size());
while (m_mapping.size() && m_mapping.last().m_renderer != ancestorRenderer) {
stepRemoved(m_mapping.last());
m_mapping.removeLast();
}
}
void RenderGeometryMap::popMappingsToAncestor(const RenderLayer* ancestorLayer)
{
const RenderLayerModelObject* ancestorRenderer = ancestorLayer ? ancestorLayer->renderer() : 0;
popMappingsToAncestor(ancestorRenderer);
}
void RenderGeometryMap::stepInserted(const RenderGeometryMapStep& step)
{
m_accumulatedOffset += step.m_offset;
if (step.m_isNonUniform)
++m_nonUniformStepsCount;
if (step.m_transform)
++m_transformedStepsCount;
if (step.m_isFixedPosition)
++m_fixedStepsCount;
}
void RenderGeometryMap::stepRemoved(const RenderGeometryMapStep& step)
{
m_accumulatedOffset -= step.m_offset;
if (step.m_isNonUniform) {
ASSERT(m_nonUniformStepsCount);
--m_nonUniformStepsCount;
}
if (step.m_transform) {
ASSERT(m_transformedStepsCount);
--m_transformedStepsCount;
}
if (step.m_isFixedPosition) {
ASSERT(m_fixedStepsCount);
--m_fixedStepsCount;
}
}
#if ENABLE(ASSERT)
bool RenderGeometryMap::isTopmostRenderView(const RenderObject* renderer) const
{
if (!renderer->isRenderView())
return false;
// If we're not working with multiple RenderViews, then any view is considered
// "topmost" (to preserve original behavior).
if (!(m_mapCoordinatesFlags & TraverseDocumentBoundaries))
return true;
return renderer->frame()->isMainFrame();
}
#endif
} // namespace blink
|