simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Apple 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 |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "RenderGeometryMap.h" |
| 28 | |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 29 | #include "RenderLayer.h" |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 30 | #include "RenderView.h" |
| 31 | #include "TransformState.h" |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 32 | #include <wtf/TemporaryChange.h> |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 33 | |
| 34 | namespace WebCore { |
| 35 | |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 36 | RenderGeometryMap::RenderGeometryMap() |
| 37 | : m_insertionPosition(notFound) |
| 38 | , m_nonUniformStepsCount(0) |
| 39 | , m_transformedStepsCount(0) |
| 40 | , m_fixedStepsCount(0) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | RenderGeometryMap::~RenderGeometryMap() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | FloatPoint RenderGeometryMap::absolutePoint(const FloatPoint& p) const |
| 49 | { |
| 50 | FloatPoint result; |
| 51 | |
| 52 | if (!hasFixedPositionStep() && !hasTransformStep() && !hasNonUniformStep()) |
| 53 | result = p + m_accumulatedOffset; |
| 54 | else { |
| 55 | TransformState transformState(TransformState::ApplyTransformDirection, p); |
| 56 | mapToAbsolute(transformState); |
| 57 | result = transformState.lastPlanarPoint(); |
| 58 | } |
| 59 | |
| 60 | #if !ASSERT_DISABLED |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 61 | FloatPoint rendererMappedResult = m_mapping.last().m_renderer->localToAbsolute(p, false, true); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 62 | ASSERT(rendererMappedResult == result); |
| 63 | #endif |
| 64 | |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | FloatRect RenderGeometryMap::absoluteRect(const FloatRect& rect) const |
| 69 | { |
| 70 | FloatRect result; |
| 71 | |
| 72 | if (!hasFixedPositionStep() && !hasTransformStep() && !hasNonUniformStep()) { |
| 73 | result = rect; |
| 74 | result.move(m_accumulatedOffset); |
| 75 | } else { |
simon.fraser@apple.com | 0e7f7d9 | 2012-05-27 00:43:12 +0000 | [diff] [blame] | 76 | TransformState transformState(TransformState::ApplyTransformDirection, rect.center(), rect); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 77 | mapToAbsolute(transformState); |
| 78 | result = transformState.lastPlanarQuad().boundingBox(); |
| 79 | } |
| 80 | |
| 81 | #if !ASSERT_DISABLED |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 82 | FloatRect rendererMappedResult = m_mapping.last().m_renderer->localToAbsoluteQuad(rect).boundingBox(); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 83 | // Inspector creates renderers with negative width <https://bugs.webkit.org/show_bug.cgi?id=87194>. |
| 84 | // Taking FloatQuad bounds avoids spurious assertions because of that. |
| 85 | ASSERT(enclosingIntRect(rendererMappedResult) == enclosingIntRect(FloatQuad(result).boundingBox())); |
| 86 | #endif |
| 87 | |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | void RenderGeometryMap::mapToAbsolute(TransformState& transformState) const |
| 92 | { |
| 93 | // If the mapping includes something like columns, we have to go via renderers. |
| 94 | if (hasNonUniformStep()) { |
leviw@chromium.org | 0d46429 | 2012-08-03 22:23:42 +0000 | [diff] [blame] | 95 | m_mapping.last().m_renderer->mapLocalToContainer(0, transformState, UseTransforms | ApplyContainerFlip); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | |
| 99 | bool inFixed = false; |
| 100 | |
| 101 | for (int i = m_mapping.size() - 1; i >= 0; --i) { |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 102 | const RenderGeometryMapStep& currentStep = m_mapping[i]; |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 103 | |
enne@google.com | ca10461 | 2012-05-30 19:51:38 +0000 | [diff] [blame] | 104 | // If this box has a transform, it acts as a fixed position container |
| 105 | // for fixed descendants, which prevents the propagation of 'fixed' |
| 106 | // unless the layer itself is also fixed position. |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 107 | if (currentStep.m_hasTransform && !currentStep.m_isFixedPosition) |
enne@google.com | ca10461 | 2012-05-30 19:51:38 +0000 | [diff] [blame] | 108 | inFixed = false; |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 109 | else if (currentStep.m_isFixedPosition) |
enne@google.com | ca10461 | 2012-05-30 19:51:38 +0000 | [diff] [blame] | 110 | inFixed = true; |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 111 | |
| 112 | if (!i) { |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 113 | if (currentStep.m_transform) |
| 114 | transformState.applyTransform(*currentStep.m_transform.get()); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 115 | |
| 116 | // The root gets special treatment for fixed position |
| 117 | if (inFixed) |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 118 | transformState.move(currentStep.m_offset.width(), currentStep.m_offset.height()); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 119 | } else { |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 120 | TransformState::TransformAccumulation accumulate = currentStep.m_accumulatingTransform ? TransformState::AccumulateTransform : TransformState::FlattenTransform; |
| 121 | if (currentStep.m_transform) |
| 122 | transformState.applyTransform(*currentStep.m_transform.get(), accumulate); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 123 | else |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 124 | transformState.move(currentStep.m_offset.width(), currentStep.m_offset.height(), accumulate); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | transformState.flatten(); |
| 129 | } |
| 130 | |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 131 | void RenderGeometryMap::pushMappingsToAncestor(const RenderObject* renderer, const RenderBoxModelObject* ancestorRenderer) |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 132 | { |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 133 | // We need to push mappings in reverse order here, so do insertions rather than appends. |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 134 | TemporaryChange<size_t> positionChange(m_insertionPosition, m_mapping.size()); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 135 | do { |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 136 | renderer = renderer->pushMappingToContainer(ancestorRenderer, *this); |
| 137 | } while (renderer && renderer != ancestorRenderer); |
commit-queue@webkit.org | dfad0c6 | 2012-07-14 01:08:07 +0000 | [diff] [blame] | 138 | |
| 139 | ASSERT(m_mapping.isEmpty() || m_mapping[0].m_renderer->isRenderView()); |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 140 | } |
| 141 | |
simon.fraser@apple.com | 56e3704 | 2012-07-11 22:26:55 +0000 | [diff] [blame] | 142 | static bool canMapViaLayer(const RenderLayer* layer) |
| 143 | { |
| 144 | RenderStyle* style = layer->renderer()->style(); |
| 145 | if (style->position() == FixedPosition || style->isFlippedBlocksWritingMode()) |
| 146 | return false; |
| 147 | |
| 148 | if (layer->renderer()->hasColumns() || layer->renderer()->hasTransform()) |
| 149 | return false; |
| 150 | |
| 151 | #if ENABLE(SVG) |
| 152 | if (layer->renderer()->isSVGRoot()) |
| 153 | return false; |
| 154 | #endif |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 159 | void RenderGeometryMap::pushMappingsToAncestor(const RenderLayer* layer, const RenderLayer* ancestorLayer) |
| 160 | { |
| 161 | const RenderObject* renderer = layer->renderer(); |
| 162 | |
| 163 | // The simple case can be handled fast in the layer tree. |
simon.fraser@apple.com | 56e3704 | 2012-07-11 22:26:55 +0000 | [diff] [blame] | 164 | bool canConvertInLayerTree = ancestorLayer ? canMapViaLayer(ancestorLayer) : false; |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 165 | for (const RenderLayer* current = layer; current != ancestorLayer && canConvertInLayerTree; current = current->parent()) |
simon.fraser@apple.com | 56e3704 | 2012-07-11 22:26:55 +0000 | [diff] [blame] | 166 | canConvertInLayerTree = canMapViaLayer(current); |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 167 | |
| 168 | if (canConvertInLayerTree) { |
| 169 | TemporaryChange<size_t> positionChange(m_insertionPosition, m_mapping.size()); |
| 170 | LayoutPoint layerOffset; |
| 171 | layer->convertToLayerCoords(ancestorLayer, layerOffset); |
| 172 | push(renderer, toLayoutSize(layerOffset), /*accumulatingTransform*/ true, /*isNonUniform*/ false, /*isFixedPosition*/ false, /*hasTransform*/ false); |
| 173 | return; |
| 174 | } |
| 175 | const RenderBoxModelObject* ancestorRenderer = ancestorLayer ? ancestorLayer->renderer() : 0; |
| 176 | pushMappingsToAncestor(renderer, ancestorRenderer); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void RenderGeometryMap::push(const RenderObject* renderer, const LayoutSize& offsetFromContainer, bool accumulatingTransform, bool isNonUniform, bool isFixedPosition, bool hasTransform) |
| 180 | { |
| 181 | ASSERT(m_insertionPosition != notFound); |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 182 | |
| 183 | m_mapping.insert(m_insertionPosition, RenderGeometryMapStep(renderer, accumulatingTransform, isNonUniform, isFixedPosition, hasTransform)); |
| 184 | |
| 185 | RenderGeometryMapStep& step = m_mapping[m_insertionPosition]; |
| 186 | step.m_offset = offsetFromContainer; |
| 187 | |
| 188 | stepInserted(step); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void RenderGeometryMap::push(const RenderObject* renderer, const TransformationMatrix& t, bool accumulatingTransform, bool isNonUniform, bool isFixedPosition, bool hasTransform) |
| 192 | { |
| 193 | ASSERT(m_insertionPosition != notFound); |
| 194 | |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 195 | m_mapping.insert(m_insertionPosition, RenderGeometryMapStep(renderer, accumulatingTransform, isNonUniform, isFixedPosition, hasTransform)); |
simon.fraser@apple.com | 3ddd013 | 2012-06-27 01:31:22 +0000 | [diff] [blame] | 196 | |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 197 | RenderGeometryMapStep& step = m_mapping[m_insertionPosition]; |
| 198 | if (!t.isIntegerTranslation()) |
| 199 | step.m_transform = adoptPtr(new TransformationMatrix(t)); |
| 200 | else |
| 201 | step.m_offset = LayoutSize(t.e(), t.f()); |
| 202 | |
| 203 | stepInserted(step); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void RenderGeometryMap::pushView(const RenderView* view, const LayoutSize& scrollOffset, const TransformationMatrix* t) |
| 207 | { |
| 208 | ASSERT(m_insertionPosition != notFound); |
commit-queue@webkit.org | dfad0c6 | 2012-07-14 01:08:07 +0000 | [diff] [blame] | 209 | ASSERT(!m_insertionPosition); // The view should always be the first step. |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 210 | |
| 211 | m_mapping.insert(m_insertionPosition, RenderGeometryMapStep(view, false, false, false, t)); |
| 212 | |
| 213 | RenderGeometryMapStep& step = m_mapping[m_insertionPosition]; |
| 214 | step.m_offset = scrollOffset; |
| 215 | if (t) |
| 216 | step.m_transform = adoptPtr(new TransformationMatrix(*t)); |
| 217 | |
| 218 | stepInserted(step); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 219 | } |
| 220 | |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 221 | void RenderGeometryMap::popMappingsToAncestor(const RenderBoxModelObject* ancestorRenderer) |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 222 | { |
| 223 | ASSERT(m_mapping.size()); |
| 224 | |
antti@apple.com | 9b9dc6a | 2012-06-28 18:58:21 +0000 | [diff] [blame] | 225 | while (m_mapping.size() && m_mapping.last().m_renderer != ancestorRenderer) { |
| 226 | stepRemoved(m_mapping.last()); |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 227 | m_mapping.removeLast(); |
| 228 | } |
| 229 | } |
| 230 | |
antti@apple.com | 308c605 | 2012-06-25 00:14:23 +0000 | [diff] [blame] | 231 | void RenderGeometryMap::popMappingsToAncestor(const RenderLayer* ancestorLayer) |
| 232 | { |
| 233 | const RenderBoxModelObject* ancestorRenderer = ancestorLayer ? ancestorLayer->renderer() : 0; |
| 234 | popMappingsToAncestor(ancestorRenderer); |
| 235 | } |
| 236 | |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 237 | void RenderGeometryMap::stepInserted(const RenderGeometryMapStep& step) |
| 238 | { |
commit-queue@webkit.org | dfad0c6 | 2012-07-14 01:08:07 +0000 | [diff] [blame] | 239 | // RenderView's offset, is only applied when we have fixed-positions. |
| 240 | if (!step.m_renderer->isRenderView()) |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 241 | m_accumulatedOffset += step.m_offset; |
| 242 | |
| 243 | if (step.m_isNonUniform) |
| 244 | ++m_nonUniformStepsCount; |
| 245 | |
| 246 | if (step.m_transform) |
| 247 | ++m_transformedStepsCount; |
| 248 | |
| 249 | if (step.m_isFixedPosition) |
| 250 | ++m_fixedStepsCount; |
| 251 | } |
| 252 | |
| 253 | void RenderGeometryMap::stepRemoved(const RenderGeometryMapStep& step) |
| 254 | { |
commit-queue@webkit.org | dfad0c6 | 2012-07-14 01:08:07 +0000 | [diff] [blame] | 255 | // RenderView's offset, is only applied when we have fixed-positions. |
| 256 | if (!step.m_renderer->isRenderView()) |
simon.fraser@apple.com | ca41ec6 | 2012-05-25 21:42:32 +0000 | [diff] [blame] | 257 | m_accumulatedOffset -= step.m_offset; |
| 258 | |
| 259 | if (step.m_isNonUniform) { |
| 260 | ASSERT(m_nonUniformStepsCount); |
| 261 | --m_nonUniformStepsCount; |
| 262 | } |
| 263 | |
| 264 | if (step.m_transform) { |
| 265 | ASSERT(m_transformedStepsCount); |
| 266 | --m_transformedStepsCount; |
| 267 | } |
| 268 | |
| 269 | if (step.m_isFixedPosition) { |
| 270 | ASSERT(m_fixedStepsCount); |
| 271 | --m_fixedStepsCount; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | } // namespace WebCore |