Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderView.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "RenderView.h"
23
24 #include "ColumnInfo.h"
25 #include "Document.h"
26 #include "Element.h"
27 #include "FloatQuad.h"
28 #include "FlowThreadController.h"
29 #include "Frame.h"
30 #include "FrameView.h"
31 #include "GraphicsContext.h"
32 #include "HTMLFrameOwnerElement.h"
33 #include "HitTestResult.h"
34 #include "Page.h"
35 #include "RenderGeometryMap.h"
36 #include "RenderLayer.h"
37 #include "RenderNamedFlowThread.h"
38 #include "RenderSelectionInfo.h"
39 #include "RenderWidget.h"
40 #include "RenderWidgetProtector.h"
41 #include "TransformState.h"
42
43 #if USE(ACCELERATED_COMPOSITING)
44 #include "RenderLayerCompositor.h"
45 #endif
46
47 #if ENABLE(CSS_SHADERS) && ENABLE(WEBGL)
48 #include "CustomFilterGlobalContext.h"
49 #endif
50
51 namespace WebCore {
52
53 RenderView::RenderView(Node* node, FrameView* view)
54     : RenderBlock(node)
55     , m_frameView(view)
56     , m_selectionStart(0)
57     , m_selectionEnd(0)
58     , m_selectionStartPos(-1)
59     , m_selectionEndPos(-1)
60     , m_maximalOutlineSize(0)
61     , m_pageLogicalHeight(0)
62     , m_pageLogicalHeightChanged(false)
63     , m_layoutState(0)
64     , m_layoutStateDisableCount(0)
65     , m_renderQuoteCount(0)
66     , m_renderCounterCount(0)
67 {
68     // Clear our anonymous bit, set because RenderObject assumes
69     // any renderer with document as the node is anonymous.
70     setIsAnonymous(false);
71
72     // init RenderObject attributes
73     setInline(false);
74     
75     m_minPreferredLogicalWidth = 0;
76     m_maxPreferredLogicalWidth = 0;
77
78     setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
79     
80     setPositioned(true); // to 0,0 :)
81 }
82
83 RenderView::~RenderView()
84 {
85 }
86
87 bool RenderView::hitTest(const HitTestRequest& request, HitTestResult& result)
88 {
89     return layer()->hitTest(request, result);
90 }
91
92 void RenderView::computeLogicalHeight()
93 {
94     if (!shouldUsePrintingLayout() && m_frameView)
95         setLogicalHeight(viewLogicalHeight());
96 }
97
98 void RenderView::computeLogicalWidth()
99 {
100     if (!shouldUsePrintingLayout() && m_frameView)
101         setLogicalWidth(viewLogicalWidth());
102 }
103
104 void RenderView::computePreferredLogicalWidths()
105 {
106     ASSERT(preferredLogicalWidthsDirty());
107
108     RenderBlock::computePreferredLogicalWidths();
109 }
110
111 LayoutUnit RenderView::availableLogicalHeight() const
112 {
113     // If we have columns, then the available logical height is reduced to the column height.
114     if (hasColumns())
115         return columnInfo()->columnHeight();
116     return RenderBlock::availableLogicalHeight();
117 }
118
119 bool RenderView::isChildAllowed(RenderObject* child, RenderStyle*) const
120 {
121     return child->isBox();
122 }
123
124 void RenderView::layout()
125 {
126 #if ENABLE(TIZEN_LAYOUTSTATE_NULL_CHECKING_WORKAROUND)
127     if (m_layoutState)
128         return;
129 #endif
130
131     if (!document()->paginated())
132         setPageLogicalHeight(0);
133
134     if (shouldUsePrintingLayout())
135         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = logicalWidth();
136
137     // Use calcWidth/Height to get the new width/height, since this will take the full page zoom factor into account.
138     bool relayoutChildren = !shouldUsePrintingLayout() && (!m_frameView || width() != viewWidth() || height() != viewHeight());
139     if (relayoutChildren) {
140         setChildNeedsLayout(true, MarkOnlyThis);
141         for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
142             if ((child->isBox() && toRenderBox(child)->hasRelativeLogicalHeight())
143                     || child->style()->logicalHeight().isPercent()
144                     || child->style()->logicalMinHeight().isPercent()
145                     || child->style()->logicalMaxHeight().isPercent())
146                 child->setChildNeedsLayout(true, MarkOnlyThis);
147         }
148     }
149
150     ASSERT(!m_layoutState);
151     LayoutState state;
152     // FIXME: May be better to push a clip and avoid issuing offscreen repaints.
153     state.m_clipped = false;
154     state.m_pageLogicalHeight = m_pageLogicalHeight;
155     state.m_pageLogicalHeightChanged = m_pageLogicalHeightChanged;
156     state.m_isPaginated = state.m_pageLogicalHeight;
157     m_pageLogicalHeightChanged = false;
158     m_layoutState = &state;
159
160     if (needsLayout()) {
161         RenderBlock::layout();
162         if (hasRenderNamedFlowThreads())
163             flowThreadController()->layoutRenderNamedFlowThreads();
164     }
165
166     ASSERT(layoutDelta() == LayoutSize());
167     ASSERT(m_layoutStateDisableCount == 0);
168     ASSERT(m_layoutState == &state);
169     m_layoutState = 0;
170     setNeedsLayout(false);
171 }
172
173 void RenderView::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState, ApplyContainerFlipOrNot, bool* wasFixed) const
174 {
175     // If a container was specified, and was not 0 or the RenderView,
176     // then we should have found it by now.
177     ASSERT_ARG(repaintContainer, !repaintContainer || repaintContainer == this);
178     ASSERT_UNUSED(wasFixed, !wasFixed || *wasFixed == fixed);
179
180     if (!repaintContainer && useTransforms && shouldUseTransformFromContainer(0)) {
181         TransformationMatrix t;
182         getTransformFromContainer(0, LayoutSize(), t);
183         transformState.applyTransform(t);
184     }
185     
186     if (fixed && m_frameView)
187         transformState.move(m_frameView->scrollOffsetForFixedPosition());
188 }
189
190 const RenderObject* RenderView::pushMappingToContainer(const RenderBoxModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
191 {
192     // If a container was specified, and was not 0 or the RenderView,
193     // then we should have found it by now.
194     ASSERT_ARG(ancestorToStopAt, !ancestorToStopAt || ancestorToStopAt == this);
195
196     LayoutSize scrollOffset;
197
198     if (m_frameView)
199         scrollOffset = m_frameView->scrollOffsetForFixedPosition();
200
201     if (!ancestorToStopAt && shouldUseTransformFromContainer(0)) {
202         TransformationMatrix t;
203         getTransformFromContainer(0, LayoutSize(), t);
204         geometryMap.pushView(this, scrollOffset, &t);
205     } else
206         geometryMap.pushView(this, scrollOffset);
207
208     return 0;
209 }
210
211 void RenderView::mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState& transformState) const
212 {
213     if (fixed && m_frameView)
214         transformState.move(m_frameView->scrollOffsetForFixedPosition());
215
216     if (useTransforms && shouldUseTransformFromContainer(0)) {
217         TransformationMatrix t;
218         getTransformFromContainer(0, LayoutSize(), t);
219         transformState.applyTransform(t);
220     }
221 }
222
223 bool RenderView::requiresColumns(int desiredColumnCount) const
224 {
225     if (m_frameView) {
226         if (Frame* frame = m_frameView->frame()) {
227             if (Page* page = frame->page())
228                 return frame == page->mainFrame() && page->pagination().mode != Page::Pagination::Unpaginated;
229         }
230     }
231     return RenderBlock::requiresColumns(desiredColumnCount);
232 }
233
234 void RenderView::calcColumnWidth()
235 {
236     int columnWidth = contentLogicalWidth();
237     if (m_frameView && style()->hasInlineColumnAxis()) {
238         if (Frame* frame = m_frameView->frame()) {
239             if (Page* page = frame->page()) {
240                 if (int pageLength = page->pagination().pageLength)
241                     columnWidth = pageLength;
242             }
243         }
244     }
245     setDesiredColumnCountAndWidth(1, columnWidth);
246 }
247
248 ColumnInfo::PaginationUnit RenderView::paginationUnit() const
249 {
250     if (m_frameView) {
251         if (Frame* frame = m_frameView->frame()) {
252             if (Page* page = frame->page())
253                 return (frame == page->mainFrame() && page->pagination().behavesLikeColumns) ? ColumnInfo::Column : ColumnInfo::Page;
254         }
255     }
256     return ColumnInfo::Page;
257 }
258
259 void RenderView::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
260 {
261     // If we ever require layout but receive a paint anyway, something has gone horribly wrong.
262     ASSERT(!needsLayout());
263     // RenderViews should never be called to paint with an offset not on device pixels.
264     ASSERT(LayoutPoint(IntPoint(paintOffset.x(), paintOffset.y())) == paintOffset);
265     paintObject(paintInfo, paintOffset);
266 }
267
268 static inline bool isComposited(RenderObject* object)
269 {
270     return object->hasLayer() && toRenderBoxModelObject(object)->layer()->isComposited();
271 }
272
273 static inline bool rendererObscuresBackground(RenderObject* object)
274 {
275     return object && object->style()->visibility() == VISIBLE
276         && object->style()->opacity() == 1
277         && !object->style()->hasTransform()
278         && !isComposited(object);
279 }
280     
281 void RenderView::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint&)
282 {
283     // Check to see if we are enclosed by a layer that requires complex painting rules.  If so, we cannot blit
284     // when scrolling, and we need to use slow repaints.  Examples of layers that require this are transparent layers,
285     // layers with reflections, or transformed layers.
286     // FIXME: This needs to be dynamic.  We should be able to go back to blitting if we ever stop being inside
287     // a transform, transparency layer, etc.
288     Element* elt;
289     for (elt = document()->ownerElement(); view() && elt && elt->renderer(); elt = elt->document()->ownerElement()) {
290         RenderLayer* layer = elt->renderer()->enclosingLayer();
291         if (layer->cannotBlitToWindow()) {
292             frameView()->setCannotBlitToWindow();
293             break;
294         }
295
296 #if USE(ACCELERATED_COMPOSITING)
297         if (RenderLayer* compositingLayer = layer->enclosingCompositingLayerForRepaint()) {
298             if (!compositingLayer->backing()->paintsIntoWindow()) {
299                 frameView()->setCannotBlitToWindow();
300                 break;
301             }
302         }
303 #endif
304     }
305
306     if (document()->ownerElement() || !view())
307         return;
308
309     bool rootFillsViewport = false;
310     Node* documentElement = document()->documentElement();
311     if (RenderObject* rootRenderer = documentElement ? documentElement->renderer() : 0) {
312         // The document element's renderer is currently forced to be a block, but may not always be.
313         RenderBox* rootBox = rootRenderer->isBox() ? toRenderBox(rootRenderer) : 0;
314         rootFillsViewport = rootBox && !rootBox->x() && !rootBox->y() && rootBox->width() >= width() && rootBox->height() >= height();
315     }
316     
317     Page* page = document()->page();
318     float pageScaleFactor = page ? page->pageScaleFactor() : 1;
319
320     // If painting will entirely fill the view, no need to fill the background.
321     if (rootFillsViewport && rendererObscuresBackground(firstChild()) && pageScaleFactor >= 1)
322         return;
323
324     // This code typically only executes if the root element's visibility has been set to hidden,
325     // if there is a transform on the <html>, or if there is a page scale factor less than 1.
326     // Only fill with the base background color (typically white) if we're the root document, 
327     // since iframes/frames with no background in the child document should show the parent's background.
328     if (frameView()->isTransparent()) // FIXME: This needs to be dynamic.  We should be able to go back to blitting if we ever stop being transparent.
329         frameView()->setCannotBlitToWindow(); // The parent must show behind the child.
330     else {
331         Color baseColor = frameView()->baseBackgroundColor();
332         if (baseColor.alpha() > 0) {
333             CompositeOperator previousOperator = paintInfo.context->compositeOperation();
334             paintInfo.context->setCompositeOperation(CompositeCopy);
335             paintInfo.context->fillRect(paintInfo.rect, baseColor, style()->colorSpace());
336             paintInfo.context->setCompositeOperation(previousOperator);
337         } else
338             paintInfo.context->clearRect(paintInfo.rect);
339     }
340 }
341
342 bool RenderView::shouldRepaint(const LayoutRect& r) const
343 {
344     if (printing() || r.width() == 0 || r.height() == 0)
345         return false;
346
347     if (!m_frameView)
348         return false;
349
350     if (m_frameView->repaintsDisabled())
351         return false;
352
353     return true;
354 }
355
356 void RenderView::repaintViewRectangle(const LayoutRect& ur, bool immediate)
357 {
358     if (!shouldRepaint(ur))
359         return;
360
361     // We always just invalidate the root view, since we could be an iframe that is clipped out
362     // or even invisible.
363     Element* elt = document()->ownerElement();
364     if (!elt)
365         m_frameView->repaintContentRectangle(pixelSnappedIntRect(ur), immediate);
366     else if (RenderBox* obj = elt->renderBox()) {
367         LayoutRect vr = viewRect();
368         LayoutRect r = intersection(ur, vr);
369         
370         // Subtract out the contentsX and contentsY offsets to get our coords within the viewing
371         // rectangle.
372         r.moveBy(-vr.location());
373
374         // FIXME: Hardcoded offsets here are not good.
375         r.moveBy(obj->contentBoxRect().location());
376         obj->repaintRectangle(r, immediate);
377     }
378 }
379
380 void RenderView::repaintRectangleInViewAndCompositedLayers(const LayoutRect& ur, bool immediate)
381 {
382     if (!shouldRepaint(ur))
383         return;
384
385     repaintViewRectangle(ur, immediate);
386     
387 #if USE(ACCELERATED_COMPOSITING)
388     if (compositor()->inCompositingMode())
389         compositor()->repaintCompositedLayersAbsoluteRect(pixelSnappedIntRect(ur));
390 #endif
391 }
392
393 void RenderView::computeRectForRepaint(RenderBoxModelObject* repaintContainer, LayoutRect& rect, bool fixed) const
394 {
395     // If a container was specified, and was not 0 or the RenderView,
396     // then we should have found it by now.
397     ASSERT_ARG(repaintContainer, !repaintContainer || repaintContainer == this);
398
399     if (printing())
400         return;
401
402     if (style()->isFlippedBlocksWritingMode()) {
403         // We have to flip by hand since the view's logical height has not been determined.  We
404         // can use the viewport width and height.
405         if (style()->isHorizontalWritingMode())
406             rect.setY(viewHeight() - rect.maxY());
407         else
408             rect.setX(viewWidth() - rect.maxX());
409     }
410
411     if (fixed && m_frameView)
412         rect.move(m_frameView->scrollOffsetForFixedPosition());
413         
414     // Apply our transform if we have one (because of full page zooming).
415     if (!repaintContainer && m_layer && m_layer->transform())
416         rect = m_layer->transform()->mapRect(rect);
417 }
418
419 void RenderView::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
420 {
421     rects.append(pixelSnappedIntRect(accumulatedOffset, m_layer->size()));
422 }
423
424 void RenderView::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
425 {
426     if (wasFixed)
427         *wasFixed = false;
428     quads.append(FloatRect(FloatPoint(), m_layer->size()));
429 }
430
431 static RenderObject* rendererAfterPosition(RenderObject* object, unsigned offset)
432 {
433     if (!object)
434         return 0;
435
436     RenderObject* child = object->childAt(offset);
437     return child ? child : object->nextInPreOrderAfterChildren();
438 }
439
440 IntRect RenderView::selectionBounds(bool clipToVisibleContent) const
441 {
442     document()->updateStyleIfNeeded();
443
444     typedef HashMap<RenderObject*, OwnPtr<RenderSelectionInfo> > SelectionMap;
445     SelectionMap selectedObjects;
446
447     RenderObject* os = m_selectionStart;
448     RenderObject* stop = rendererAfterPosition(m_selectionEnd, m_selectionEndPos);
449     while (os && os != stop) {
450         if ((os->canBeSelectionLeaf() || os == m_selectionStart || os == m_selectionEnd) && os->selectionState() != SelectionNone) {
451             // Blocks are responsible for painting line gaps and margin gaps. They must be examined as well.
452             selectedObjects.set(os, adoptPtr(new RenderSelectionInfo(os, clipToVisibleContent)));
453             RenderBlock* cb = os->containingBlock();
454             while (cb && !cb->isRenderView()) {
455                 OwnPtr<RenderSelectionInfo>& blockInfo = selectedObjects.add(cb, nullptr).iterator->second;
456                 if (blockInfo)
457                     break;
458                 blockInfo = adoptPtr(new RenderSelectionInfo(cb, clipToVisibleContent));
459                 cb = cb->containingBlock();
460             }
461         }
462
463         os = os->nextInPreOrder();
464     }
465
466     // Now create a single bounding box rect that encloses the whole selection.
467     LayoutRect selRect;
468     SelectionMap::iterator end = selectedObjects.end();
469     for (SelectionMap::iterator i = selectedObjects.begin(); i != end; ++i) {
470         RenderSelectionInfo* info = i->second.get();
471         // RenderSelectionInfo::rect() is in the coordinates of the repaintContainer, so map to page coordinates.
472         LayoutRect currRect = info->rect();
473         if (RenderBoxModelObject* repaintContainer = info->repaintContainer()) {
474             FloatQuad absQuad = repaintContainer->localToAbsoluteQuad(FloatRect(currRect));
475             currRect = absQuad.enclosingBoundingBox(); 
476         }
477         selRect.unite(currRect);
478     }
479     return pixelSnappedIntRect(selRect);
480 }
481
482 #if USE(ACCELERATED_COMPOSITING)
483 // Compositing layer dimensions take outline size into account, so we have to recompute layer
484 // bounds when it changes.
485 // FIXME: This is ugly; it would be nice to have a better way to do this.
486 void RenderView::setMaximalOutlineSize(int o)
487 {
488     if (o != m_maximalOutlineSize) {
489         m_maximalOutlineSize = o;
490
491         // maximalOutlineSize affects compositing layer dimensions.
492         compositor()->setCompositingLayersNeedRebuild();    // FIXME: this really just needs to be a geometry update.
493     }
494 }
495 #endif
496
497 void RenderView::setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos, SelectionRepaintMode blockRepaintMode)
498 {
499     // Make sure both our start and end objects are defined.
500     // Check www.msnbc.com and try clicking around to find the case where this happened.
501     if ((start && !end) || (end && !start))
502         return;
503
504     // Just return if the selection hasn't changed.
505     if (m_selectionStart == start && m_selectionStartPos == startPos &&
506         m_selectionEnd == end && m_selectionEndPos == endPos)
507         return;
508
509     // Record the old selected objects.  These will be used later
510     // when we compare against the new selected objects.
511     int oldStartPos = m_selectionStartPos;
512     int oldEndPos = m_selectionEndPos;
513
514     // Objects each have a single selection rect to examine.
515     typedef HashMap<RenderObject*, OwnPtr<RenderSelectionInfo> > SelectedObjectMap;
516     SelectedObjectMap oldSelectedObjects;
517     SelectedObjectMap newSelectedObjects;
518
519     // Blocks contain selected objects and fill gaps between them, either on the left, right, or in between lines and blocks.
520     // In order to get the repaint rect right, we have to examine left, middle, and right rects individually, since otherwise
521     // the union of those rects might remain the same even when changes have occurred.
522     typedef HashMap<RenderBlock*, OwnPtr<RenderBlockSelectionInfo> > SelectedBlockMap;
523     SelectedBlockMap oldSelectedBlocks;
524     SelectedBlockMap newSelectedBlocks;
525
526     RenderObject* os = m_selectionStart;
527     RenderObject* stop = rendererAfterPosition(m_selectionEnd, m_selectionEndPos);
528     while (os && os != stop) {
529         if ((os->canBeSelectionLeaf() || os == m_selectionStart || os == m_selectionEnd) && os->selectionState() != SelectionNone) {
530             // Blocks are responsible for painting line gaps and margin gaps.  They must be examined as well.
531             oldSelectedObjects.set(os, adoptPtr(new RenderSelectionInfo(os, true)));
532             if (blockRepaintMode == RepaintNewXOROld) {
533                 RenderBlock* cb = os->containingBlock();
534                 while (cb && !cb->isRenderView()) {
535                     OwnPtr<RenderBlockSelectionInfo>& blockInfo = oldSelectedBlocks.add(cb, nullptr).iterator->second;
536                     if (blockInfo)
537                         break;
538                     blockInfo = adoptPtr(new RenderBlockSelectionInfo(cb));
539                     cb = cb->containingBlock();
540                 }
541             }
542         }
543
544         os = os->nextInPreOrder();
545     }
546
547     // Now clear the selection.
548     SelectedObjectMap::iterator oldObjectsEnd = oldSelectedObjects.end();
549     for (SelectedObjectMap::iterator i = oldSelectedObjects.begin(); i != oldObjectsEnd; ++i)
550         i->first->setSelectionStateIfNeeded(SelectionNone);
551
552     // set selection start and end
553     m_selectionStart = start;
554     m_selectionStartPos = startPos;
555     m_selectionEnd = end;
556     m_selectionEndPos = endPos;
557
558     // Update the selection status of all objects between m_selectionStart and m_selectionEnd
559     if (start && start == end)
560         start->setSelectionStateIfNeeded(SelectionBoth);
561     else {
562         if (start)
563             start->setSelectionStateIfNeeded(SelectionStart);
564         if (end)
565             end->setSelectionStateIfNeeded(SelectionEnd);
566     }
567
568     RenderObject* o = start;
569     stop = rendererAfterPosition(end, endPos);
570
571     while (o && o != stop) {
572         if (o != start && o != end && o->canBeSelectionLeaf())
573             o->setSelectionStateIfNeeded(SelectionInside);
574         o = o->nextInPreOrder();
575     }
576
577     if (blockRepaintMode != RepaintNothing)
578         m_layer->clearBlockSelectionGapsBounds();
579
580     // Now that the selection state has been updated for the new objects, walk them again and
581     // put them in the new objects list.
582     o = start;
583     while (o && o != stop) {
584         if ((o->canBeSelectionLeaf() || o == start || o == end) && o->selectionState() != SelectionNone) {
585             newSelectedObjects.set(o, adoptPtr(new RenderSelectionInfo(o, true)));
586             RenderBlock* cb = o->containingBlock();
587             while (cb && !cb->isRenderView()) {
588                 OwnPtr<RenderBlockSelectionInfo>& blockInfo = newSelectedBlocks.add(cb, nullptr).iterator->second;
589                 if (blockInfo)
590                     break;
591                 blockInfo = adoptPtr(new RenderBlockSelectionInfo(cb));
592                 cb = cb->containingBlock();
593             }
594         }
595
596         o = o->nextInPreOrder();
597     }
598
599     if (!m_frameView || blockRepaintMode == RepaintNothing)
600         return;
601
602     m_frameView->beginDeferredRepaints();
603
604     // Have any of the old selected objects changed compared to the new selection?
605     for (SelectedObjectMap::iterator i = oldSelectedObjects.begin(); i != oldObjectsEnd; ++i) {
606         RenderObject* obj = i->first;
607         RenderSelectionInfo* newInfo = newSelectedObjects.get(obj);
608         RenderSelectionInfo* oldInfo = i->second.get();
609         if (!newInfo || oldInfo->rect() != newInfo->rect() || oldInfo->state() != newInfo->state() ||
610             (m_selectionStart == obj && oldStartPos != m_selectionStartPos) ||
611             (m_selectionEnd == obj && oldEndPos != m_selectionEndPos)) {
612             oldInfo->repaint();
613             if (newInfo) {
614                 newInfo->repaint();
615                 newSelectedObjects.remove(obj);
616             }
617         }
618     }
619
620     // Any new objects that remain were not found in the old objects dict, and so they need to be updated.
621     SelectedObjectMap::iterator newObjectsEnd = newSelectedObjects.end();
622     for (SelectedObjectMap::iterator i = newSelectedObjects.begin(); i != newObjectsEnd; ++i)
623         i->second->repaint();
624
625     // Have any of the old blocks changed?
626     SelectedBlockMap::iterator oldBlocksEnd = oldSelectedBlocks.end();
627     for (SelectedBlockMap::iterator i = oldSelectedBlocks.begin(); i != oldBlocksEnd; ++i) {
628         RenderBlock* block = i->first;
629         RenderBlockSelectionInfo* newInfo = newSelectedBlocks.get(block);
630         RenderBlockSelectionInfo* oldInfo = i->second.get();
631         if (!newInfo || oldInfo->rects() != newInfo->rects() || oldInfo->state() != newInfo->state()) {
632             oldInfo->repaint();
633             if (newInfo) {
634                 newInfo->repaint();
635                 newSelectedBlocks.remove(block);
636             }
637         }
638     }
639
640     // Any new blocks that remain were not found in the old blocks dict, and so they need to be updated.
641     SelectedBlockMap::iterator newBlocksEnd = newSelectedBlocks.end();
642     for (SelectedBlockMap::iterator i = newSelectedBlocks.begin(); i != newBlocksEnd; ++i)
643         i->second->repaint();
644
645     m_frameView->endDeferredRepaints();
646 }
647
648 void RenderView::getSelection(RenderObject*& startRenderer, int& startOffset, RenderObject*& endRenderer, int& endOffset) const
649 {
650     startRenderer = m_selectionStart;
651     startOffset = m_selectionStartPos;
652     endRenderer = m_selectionEnd;
653     endOffset = m_selectionEndPos;
654 }
655
656 void RenderView::clearSelection()
657 {
658     m_layer->repaintBlockSelectionGaps();
659     setSelection(0, -1, 0, -1, RepaintNewMinusOld);
660 }
661
662 void RenderView::selectionStartEnd(int& startPos, int& endPos) const
663 {
664     startPos = m_selectionStartPos;
665     endPos = m_selectionEndPos;
666 }
667
668 bool RenderView::printing() const
669 {
670     return document()->printing();
671 }
672
673 bool RenderView::shouldUsePrintingLayout() const
674 {
675     if (!printing() || !m_frameView)
676         return false;
677     Frame* frame = m_frameView->frame();
678     return frame && frame->shouldUsePrintingLayout();
679 }
680
681 size_t RenderView::getRetainedWidgets(Vector<RenderWidget*>& renderWidgets)
682 {
683     size_t size = m_widgets.size();
684
685     renderWidgets.reserveCapacity(size);
686
687     RenderWidgetSet::const_iterator end = m_widgets.end();
688     for (RenderWidgetSet::const_iterator it = m_widgets.begin(); it != end; ++it) {
689         renderWidgets.uncheckedAppend(*it);
690         (*it)->ref();
691     }
692     
693     return size;
694 }
695
696 void RenderView::releaseWidgets(Vector<RenderWidget*>& renderWidgets)
697 {
698     size_t size = renderWidgets.size();
699
700     for (size_t i = 0; i < size; ++i)
701         renderWidgets[i]->deref(renderArena());
702 }
703
704 void RenderView::updateWidgetPositions()
705 {
706     // updateWidgetPosition() can possibly cause layout to be re-entered (via plug-ins running
707     // scripts in response to NPP_SetWindow, for example), so we need to keep the Widgets
708     // alive during enumeration.    
709
710     Vector<RenderWidget*> renderWidgets;
711     size_t size = getRetainedWidgets(renderWidgets);
712     
713     for (size_t i = 0; i < size; ++i)
714         renderWidgets[i]->updateWidgetPosition();
715
716     for (size_t i = 0; i < size; ++i)
717         renderWidgets[i]->widgetPositionsUpdated();
718
719     releaseWidgets(renderWidgets);
720 }
721
722 void RenderView::addWidget(RenderWidget* o)
723 {
724     m_widgets.add(o);
725 }
726
727 void RenderView::removeWidget(RenderWidget* o)
728 {
729     m_widgets.remove(o);
730 }
731
732 void RenderView::notifyWidgets(WidgetNotification notification)
733 {
734     Vector<RenderWidget*> renderWidgets;
735     size_t size = getRetainedWidgets(renderWidgets);
736
737     for (size_t i = 0; i < size; ++i)
738         renderWidgets[i]->notifyWidget(notification);
739
740     releaseWidgets(renderWidgets);
741 }
742
743 LayoutRect RenderView::viewRect() const
744 {
745     if (shouldUsePrintingLayout())
746         return LayoutRect(LayoutPoint(), size());
747     if (m_frameView)
748         return m_frameView->visibleContentRect();
749     return LayoutRect();
750 }
751
752
753 IntRect RenderView::unscaledDocumentRect() const
754 {
755     LayoutRect overflowRect(layoutOverflowRect());
756     flipForWritingMode(overflowRect);
757     return pixelSnappedIntRect(overflowRect);
758 }
759
760 LayoutRect RenderView::backgroundRect(RenderBox* backgroundRenderer) const
761 {
762     if (!hasColumns())
763         return unscaledDocumentRect();
764
765     ColumnInfo* columnInfo = this->columnInfo();
766     LayoutRect backgroundRect(0, 0, columnInfo->desiredColumnWidth(), columnInfo->columnHeight() * columnInfo->columnCount());
767     if (!isHorizontalWritingMode())
768         backgroundRect = backgroundRect.transposedRect();
769     backgroundRenderer->flipForWritingMode(backgroundRect);
770
771     return backgroundRect;
772 }
773
774 IntRect RenderView::documentRect() const
775 {
776     FloatRect overflowRect(unscaledDocumentRect());
777     if (hasTransform())
778         overflowRect = layer()->currentTransform().mapRect(overflowRect);
779     return IntRect(overflowRect);
780 }
781
782 int RenderView::viewHeight() const
783 {
784     int height = 0;
785     if (!shouldUsePrintingLayout() && m_frameView) {
786         height = m_frameView->layoutHeight();
787         height = m_frameView->useFixedLayout() ? ceilf(style()->effectiveZoom() * float(height)) : height;
788     }
789     return height;
790 }
791
792 int RenderView::viewWidth() const
793 {
794     int width = 0;
795     if (!shouldUsePrintingLayout() && m_frameView) {
796         width = m_frameView->layoutWidth();
797         width = m_frameView->useFixedLayout() ? ceilf(style()->effectiveZoom() * float(width)) : width;
798     }
799     return width;
800 }
801
802 int RenderView::viewLogicalHeight() const
803 {
804     int height = style()->isHorizontalWritingMode() ? viewHeight() : viewWidth();
805
806     if (hasColumns() && !style()->hasInlineColumnAxis()) {
807         if (Frame* frame = m_frameView->frame()) {
808             if (Page* page = frame->page()) {
809                 if (frame == page->mainFrame()) {
810                     if (int pageLength = page->pagination().pageLength)
811                         height = pageLength;
812                 }
813             }
814         }
815     }
816
817     return height;
818 }
819
820 float RenderView::zoomFactor() const
821 {
822     Frame* frame = m_frameView->frame();
823     return frame ? frame->pageZoomFactor() : 1;
824 }
825
826 void RenderView::pushLayoutState(RenderObject* root)
827 {
828     ASSERT(m_layoutStateDisableCount == 0);
829     ASSERT(m_layoutState == 0);
830
831     m_layoutState = new (renderArena()) LayoutState(root);
832 }
833
834 void RenderView::pushLayoutState(RenderFlowThread* flowThread, bool regionsChanged)
835 {
836     m_layoutState = new (renderArena()) LayoutState(m_layoutState, flowThread, regionsChanged);
837 }
838
839 bool RenderView::shouldDisableLayoutStateForSubtree(RenderObject* renderer) const
840 {
841     RenderObject* o = renderer;
842     while (o) {
843         if (o->hasColumns() || o->hasTransform() || o->hasReflection())
844             return true;
845         o = o->container();
846     }
847     return false;
848 }
849
850 void RenderView::updateHitTestResult(HitTestResult& result, const LayoutPoint& point)
851 {
852     if (result.innerNode())
853         return;
854
855     Node* node = document()->documentElement();
856     if (node) {
857         result.setInnerNode(node);
858         if (!result.innerNonSharedNode())
859             result.setInnerNonSharedNode(node);
860
861         LayoutPoint adjustedPoint = point;
862         offsetForContents(adjustedPoint);
863
864         result.setLocalPoint(adjustedPoint);
865     }
866 }
867
868 // FIXME: This function is obsolete and only used by embedded WebViews inside AppKit NSViews.
869 // Do not add callers of this function!
870 // The idea here is to take into account what object is moving the pagination point, and
871 // thus choose the best place to chop it.
872 void RenderView::setBestTruncatedAt(int y, RenderBoxModelObject* forRenderer, bool forcedBreak)
873 {
874     // Nobody else can set a page break once we have a forced break.
875     if (m_legacyPrinting.m_forcedPageBreak)
876         return;
877
878     // Forced breaks always win over unforced breaks.
879     if (forcedBreak) {
880         m_legacyPrinting.m_forcedPageBreak = true;
881         m_legacyPrinting.m_bestTruncatedAt = y;
882         return;
883     }
884
885     // Prefer the widest object that tries to move the pagination point
886     IntRect boundingBox = forRenderer->borderBoundingBox();
887     if (boundingBox.width() > m_legacyPrinting.m_truncatorWidth) {
888         m_legacyPrinting.m_truncatorWidth = boundingBox.width();
889         m_legacyPrinting.m_bestTruncatedAt = y;
890     }
891 }
892
893 #if USE(ACCELERATED_COMPOSITING)
894 bool RenderView::usesCompositing() const
895 {
896     return m_compositor && m_compositor->inCompositingMode();
897 }
898
899 RenderLayerCompositor* RenderView::compositor()
900 {
901     if (!m_compositor)
902         m_compositor = adoptPtr(new RenderLayerCompositor(this));
903
904     return m_compositor.get();
905 }
906 #endif
907
908 void RenderView::didMoveOnscreen()
909 {
910 #if USE(ACCELERATED_COMPOSITING)
911     if (m_compositor)
912         m_compositor->didMoveOnscreen();
913 #endif
914 }
915
916 void RenderView::willMoveOffscreen()
917 {
918 #if USE(ACCELERATED_COMPOSITING)
919     if (m_compositor)
920         m_compositor->willMoveOffscreen();
921 #endif
922 }
923
924 #if ENABLE(CSS_SHADERS) && ENABLE(WEBGL)
925 CustomFilterGlobalContext* RenderView::customFilterGlobalContext()
926 {
927     if (!m_customFilterGlobalContext)
928         m_customFilterGlobalContext = adoptPtr(new CustomFilterGlobalContext());
929     return m_customFilterGlobalContext.get();
930 }
931 #endif
932
933 void RenderView::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
934 {
935     RenderBlock::styleDidChange(diff, oldStyle);
936     
937     for (RenderObject* renderer = firstChild(); renderer; renderer = renderer->nextSibling()) {
938         if (renderer->isRenderNamedFlowThread()) {
939             RenderNamedFlowThread* flowRenderer = toRenderNamedFlowThread(renderer);
940             flowRenderer->setStyle(RenderFlowThread::createFlowThreadStyle(style()));
941         }
942     }
943 }
944
945 bool RenderView::hasRenderNamedFlowThreads() const
946 {
947     return m_flowThreadController && m_flowThreadController->hasRenderNamedFlowThreads();
948 }
949
950 FlowThreadController* RenderView::flowThreadController()
951 {
952     if (!m_flowThreadController)
953         m_flowThreadController = FlowThreadController::create(this);
954
955     return m_flowThreadController.get();
956 }
957
958 RenderBlock::IntervalArena* RenderView::intervalArena()
959 {
960     if (!m_intervalArena)
961         m_intervalArena = IntervalArena::create();
962     return m_intervalArena.get();
963 }
964
965 void RenderView::setFixedPositionedObjectsNeedLayout()
966 {
967     ASSERT(m_frameView);
968
969     PositionedObjectsListHashSet* positionedObjects = this->positionedObjects();
970     if (!positionedObjects)
971         return;
972
973     PositionedObjectsListHashSet::const_iterator end = positionedObjects->end();
974     for (PositionedObjectsListHashSet::const_iterator it = positionedObjects->begin(); it != end; ++it) {
975         RenderBox* currBox = *it;
976         currBox->setNeedsLayout(true);
977     }
978 }
979
980 } // namespace WebCore