Index: third_party/WebKit/Source/core/paint/PaintPropertyTreeBuilder.cpp |
diff --git a/third_party/WebKit/Source/core/paint/PaintPropertyTreeBuilder.cpp b/third_party/WebKit/Source/core/paint/PaintPropertyTreeBuilder.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..09a0156d277f489baebe32a0c343eaa16dbb2e77 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/paint/PaintPropertyTreeBuilder.cpp |
@@ -0,0 +1,224 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "core/paint/PaintPropertyTreeBuilder.h" |
+ |
+#include "core/frame/FrameView.h" |
+#include "core/layout/LayoutView.h" |
+#include "core/paint/ObjectPaintProperties.h" |
+#include "core/paint/PaintLayer.h" |
+#include "platform/graphics/paint/TransformPaintPropertyNode.h" |
+#include "platform/transforms/TransformationMatrix.h" |
+ |
+namespace blink { |
+ |
+// The context for layout tree walk. |
+// The walk will be done in the primary tree order (= DOM order), thus the context will also be |
+// responsible for bookkeeping tree state in other order, for example, the most recent position |
+// container seen. |
+struct PaintPropertyTreeBuilder::Context { |
+ Context() : currentTransform(nullptr), transformForOutOfFlowPositioned(nullptr), transformForFixedPositioned(nullptr) { } |
+ |
+ // The combination of a transform and paint offset describes a linear space. |
+ // When a layout object recur to its children, the main context is expected to refer |
+ // the object's border box, then the callee will derive its own border box by translating |
+ // the space with its own layout location. |
+ TransformPaintPropertyNode* currentTransform; |
+ LayoutPoint paintOffset; |
+ |
+ // Separate context for out-of-flow positioned and fixed positioned elements are needed |
+ // because they don't use DOM parent as their positioning parent (i.e. containing block). |
+ // These additional contexts normally pass through untouched, and are only copied from |
+ // the main context when the current element serves as the positioning parent of corresponding |
+ // positioned descendants. |
+ TransformPaintPropertyNode* transformForOutOfFlowPositioned; |
+ LayoutPoint paintOffsetForOutOfFlowPositioned; |
+ |
+ TransformPaintPropertyNode* transformForFixedPositioned; |
+ LayoutPoint paintOffsetForFixedPositioned; |
+}; |
+ |
+void PaintPropertyTreeBuilder::buildPropertyTrees(FrameView& rootFrame) |
+{ |
+ walk(rootFrame, Context()); |
+} |
+ |
+void PaintPropertyTreeBuilder::walk(FrameView& frameView, const Context& context) |
+{ |
+ Context localContext(context); |
+ |
+ TransformationMatrix frameTranslate; |
+ frameTranslate.translate(frameView.x(), frameView.y()); |
+ // The frame owner applies paint offset already. |
+ // This assumption may change in the future. |
+ ASSERT(context.paintOffset == LayoutPoint()); |
+ RefPtr<TransformPaintPropertyNode> newTransformNodeForPreTranslation = TransformPaintPropertyNode::create(frameTranslate, FloatPoint3D(), context.currentTransform); |
+ localContext.transformForFixedPositioned = newTransformNodeForPreTranslation.get(); |
+ localContext.paintOffsetForFixedPositioned = LayoutPoint(); |
+ |
+ // This is going away in favor of Settings::rootLayerScrolls. |
+ DoubleSize scrollOffset = frameView.scrollOffsetDouble(); |
+ TransformationMatrix frameScroll; |
+ frameScroll.translate(-scrollOffset.width(), -scrollOffset.height()); |
+ RefPtr<TransformPaintPropertyNode> newTransformNodeForScrollTranslation = TransformPaintPropertyNode::create(frameScroll, FloatPoint3D(), newTransformNodeForPreTranslation); |
+ localContext.currentTransform = localContext.transformForOutOfFlowPositioned = newTransformNodeForScrollTranslation.get(); |
+ localContext.paintOffset = localContext.paintOffsetForOutOfFlowPositioned = LayoutPoint(); |
+ |
+ frameView.setPreTranslation(newTransformNodeForPreTranslation.release()); |
+ frameView.setScrollTranslation(newTransformNodeForScrollTranslation.release()); |
+ |
+ if (LayoutView* layoutView = frameView.layoutView()) |
+ walk(*layoutView, localContext); |
+} |
+ |
+static bool shouldCreatePaintOffsetTranslationNode(LayoutBoxModelObject& object) |
+{ |
+ // TODO(trchen): Eliminate PaintLayer dependency. |
+ PaintLayer* layer = object.layer(); |
+ if (!layer) |
+ return false; |
+ |
+ return layer->paintsWithTransform(GlobalPaintNormalPhase); |
+} |
+ |
+static FloatPoint3D transformOrigin(const LayoutBox& box) |
+{ |
+ const ComputedStyle& style = box.styleRef(); |
+ FloatSize borderBoxSize(box.size()); |
+ return FloatPoint3D( |
+ floatValueForLength(style.transformOriginX(), borderBoxSize.width()), |
+ floatValueForLength(style.transformOriginY(), borderBoxSize.height()), |
+ style.transformOriginZ()); |
+} |
+ |
+static FloatPoint perspectiveOrigin(const LayoutBox& box) |
+{ |
+ const ComputedStyle& style = box.styleRef(); |
+ FloatSize borderBoxSize(box.size()); |
+ return FloatPoint( |
+ floatValueForLength(style.perspectiveOriginX(), borderBoxSize.width()), |
+ floatValueForLength(style.perspectiveOriginY(), borderBoxSize.height())); |
+} |
+ |
+void PaintPropertyTreeBuilder::walk(LayoutBoxModelObject& object, const Context& context) |
chrishtr
2015/10/21 17:15:19
This function is too long.
pdr.
2015/10/21 17:18:02
@trchen, what do you think about breaking each ste
trchen
2015/10/21 22:21:14
Sure! I was thinking about it so we can use the fu
|
+{ |
+ ASSERT(object.isBox() != object.isLayoutInline()); // Either or. |
+ |
+ Context localContext(context); |
+ RefPtr<TransformPaintPropertyNode> newTransformNodeForPaintOffsetTranslation; |
+ RefPtr<TransformPaintPropertyNode> newTransformNodeForTransform; |
+ RefPtr<TransformPaintPropertyNode> newTransformNodeForPerspective; |
+ RefPtr<TransformPaintPropertyNode> newTransformNodeForScrollTranslation; |
+ |
+ // * Figure out the layout space of the current object. |
+ const ComputedStyle& style = object.styleRef(); |
+ // TODO(trchen): There is some insanity going on with tables. Double check results. |
+ switch (style.position()) { |
+ case StaticPosition: |
+ break; |
+ case RelativePosition: |
+ localContext.paintOffset += object.offsetForInFlowPosition(); |
+ break; |
+ case AbsolutePosition: |
+ localContext.currentTransform = context.transformForOutOfFlowPositioned; |
+ localContext.paintOffset = context.paintOffsetForOutOfFlowPositioned; |
+ break; |
+ case StickyPosition: |
+ localContext.paintOffset += object.offsetForInFlowPosition(); |
+ break; |
+ case FixedPosition: |
+ localContext.currentTransform = context.transformForFixedPositioned; |
+ localContext.paintOffset = context.paintOffsetForFixedPositioned; |
+ break; |
+ default: |
+ ASSERT_NOT_REACHED(); |
+ } |
+ if (object.isBox()) |
+ localContext.paintOffset += toLayoutBox(object).locationOffset(); |
+ |
+ // * Create a transform node to consolidate paint offset (if needed). |
+ if (shouldCreatePaintOffsetTranslationNode(object) && localContext.paintOffset != LayoutPoint()) { |
+ TransformationMatrix matrix; |
+ matrix.translate(localContext.paintOffset.x(), localContext.paintOffset.y()); |
+ newTransformNodeForPaintOffsetTranslation = TransformPaintPropertyNode::create( |
+ matrix, FloatPoint3D(), localContext.currentTransform); |
+ localContext.currentTransform = newTransformNodeForPaintOffsetTranslation.get(); |
+ localContext.paintOffset = LayoutPoint(); |
+ } |
+ |
+ // * Create a transform node for CSS transform (if present). |
+ bool hasTransform = object.isBox() && style.hasTransform(); |
+ if (hasTransform) { |
+ TransformationMatrix matrix; |
+ style.applyTransform(matrix, toLayoutBox(object).size(), ComputedStyle::ExcludeTransformOrigin, |
+ ComputedStyle::IncludeMotionPath, ComputedStyle::IncludeIndependentTransformProperties); |
+ newTransformNodeForTransform = TransformPaintPropertyNode::create( |
+ matrix, transformOrigin(toLayoutBox(object)), localContext.currentTransform); |
+ localContext.currentTransform = newTransformNodeForTransform.get(); |
+ ASSERT(localContext.paintOffset == LayoutPoint()); |
+ } |
+ // At this point, the current space is the space we paint the element itself. |
+ |
+ // * Create a transform node for CSS perspective (if present). |
+ if (object.isBox() && style.hasPerspective()) { |
+ TransformationMatrix matrix; |
+ matrix.applyPerspective(style.perspective()); |
+ newTransformNodeForPerspective = TransformPaintPropertyNode::create( |
+ matrix, perspectiveOrigin(toLayoutBox(object)) + toLayoutSize(localContext.paintOffset), localContext.currentTransform); |
+ localContext.currentTransform = newTransformNodeForPerspective.get(); |
+ } |
+ |
+ // * Create a transform node for overflow clip (if present). |
+ if (object.hasOverflowClip()) { |
+ PaintLayer* layer = object.layer(); |
+ ASSERT(layer); |
+ DoubleSize scrollOffset = layer->scrollableArea()->scrollOffset(); |
+ if (!scrollOffset.isZero() || layer->scrollsOverflow()) { |
+ TransformationMatrix matrix; |
+ matrix.translate(-scrollOffset.width(), -scrollOffset.height()); |
+ newTransformNodeForScrollTranslation = TransformPaintPropertyNode::create(matrix, FloatPoint3D(), localContext.currentTransform); |
+ localContext.currentTransform = newTransformNodeForScrollTranslation.get(); |
+ } |
+ } |
+ // At this point, the current space is the space we paint in-flow children. |
+ |
+ // * Update the context for out-of-flow descendants (if position container is established). |
+ if (style.position() != StaticPosition || hasTransform) { |
+ localContext.transformForOutOfFlowPositioned = localContext.currentTransform; |
+ localContext.paintOffsetForOutOfFlowPositioned = localContext.paintOffset; |
+ } |
+ if (hasTransform) { |
+ localContext.transformForFixedPositioned = localContext.currentTransform; |
+ localContext.paintOffsetForFixedPositioned = localContext.paintOffset; |
+ } |
+ |
+ // * Memorize the nodes we created for future reference during paint. |
+ if (newTransformNodeForPaintOffsetTranslation || newTransformNodeForTransform || newTransformNodeForPerspective || newTransformNodeForScrollTranslation) { |
+ ObjectPaintProperties& properties = object.ensureObjectPaintProperties(); |
+ properties.setPaintOffsetTranslation(newTransformNodeForPaintOffsetTranslation.release()); |
+ properties.setTransform(newTransformNodeForTransform.release()); |
+ properties.setPerspective(newTransformNodeForPerspective.release()); |
+ properties.setScrollTranslation(newTransformNodeForScrollTranslation.release()); |
+ } else { |
+ object.clearObjectPaintProperties(); |
+ } |
+ |
+ // * Recur. |
+ |
+ // TODO(trchen): Walk subframes for LayoutFrame. |
+ |
+ // TODO(trchen): Implement SVG walk. |
+ if (object.isSVGRoot()) { |
+ return; |
+ } |
+ |
+ for (LayoutObject* child = object.slowFirstChild(); child; child = child->nextSibling()) { |
+ if (child->isText()) |
+ continue; |
+ walk(toLayoutBoxModelObject(*child), localContext); |
+ } |
+} |
+ |
+} // namespace blink |