| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google 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 are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "core/frame/PinchViewport.h" | |
| 33 | |
| 34 #include "core/frame/FrameHost.h" | |
| 35 #include "core/frame/FrameView.h" | |
| 36 #include "core/frame/LocalFrame.h" | |
| 37 #include "core/frame/Settings.h" | |
| 38 #include "core/page/Chrome.h" | |
| 39 #include "core/page/ChromeClient.h" | |
| 40 #include "core/page/Page.h" | |
| 41 #include "core/page/scrolling/ScrollingCoordinator.h" | |
| 42 #include "core/rendering/RenderView.h" | |
| 43 #include "core/rendering/compositing/RenderLayerCompositor.h" | |
| 44 #include "platform/TraceEvent.h" | |
| 45 #include "platform/geometry/FloatSize.h" | |
| 46 #include "platform/graphics/GraphicsLayer.h" | |
| 47 #include "platform/graphics/GraphicsLayerFactory.h" | |
| 48 #include "platform/scroll/Scrollbar.h" | |
| 49 #include "public/platform/Platform.h" | |
| 50 #include "public/platform/WebCompositorSupport.h" | |
| 51 #include "public/platform/WebLayer.h" | |
| 52 #include "public/platform/WebLayerTreeView.h" | |
| 53 #include "public/platform/WebScrollbar.h" | |
| 54 #include "public/platform/WebScrollbarLayer.h" | |
| 55 | |
| 56 using blink::WebLayer; | |
| 57 using blink::WebLayerTreeView; | |
| 58 using blink::WebScrollbar; | |
| 59 using blink::WebScrollbarLayer; | |
| 60 using blink::FrameHost; | |
| 61 using blink::GraphicsLayer; | |
| 62 using blink::GraphicsLayerFactory; | |
| 63 | |
| 64 namespace blink { | |
| 65 | |
| 66 PinchViewport::PinchViewport(FrameHost& owner) | |
| 67 : m_frameHost(owner) | |
| 68 , m_scale(1) | |
| 69 { | |
| 70 reset(); | |
| 71 } | |
| 72 | |
| 73 PinchViewport::~PinchViewport() { } | |
| 74 | |
| 75 void PinchViewport::setSize(const IntSize& size) | |
| 76 { | |
| 77 if (m_size == size) | |
| 78 return; | |
| 79 | |
| 80 TRACE_EVENT2("blink", "PinchViewport::setSize", "width", size.width(), "heig
ht", size.height()); | |
| 81 m_size = size; | |
| 82 | |
| 83 // Make sure we clamp the offset to within the new bounds. | |
| 84 setLocation(m_offset); | |
| 85 | |
| 86 if (m_innerViewportContainerLayer) { | |
| 87 m_innerViewportContainerLayer->setSize(m_size); | |
| 88 | |
| 89 // Need to re-compute sizes for the overlay scrollbars. | |
| 90 setupScrollbar(WebScrollbar::Horizontal); | |
| 91 setupScrollbar(WebScrollbar::Vertical); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void PinchViewport::reset() | |
| 96 { | |
| 97 setLocation(FloatPoint()); | |
| 98 setScale(1); | |
| 99 } | |
| 100 | |
| 101 void PinchViewport::mainFrameDidChangeSize() | |
| 102 { | |
| 103 TRACE_EVENT0("blink", "PinchViewport::mainFrameDidChangeSize"); | |
| 104 | |
| 105 // In unit tests we may not have initialized the layer tree. | |
| 106 if (m_innerViewportScrollLayer) | |
| 107 m_innerViewportScrollLayer->setSize(contentsSize()); | |
| 108 | |
| 109 // Make sure the viewport's offset is clamped within the newly sized main fr
ame. | |
| 110 setLocation(m_offset); | |
| 111 } | |
| 112 | |
| 113 FloatRect PinchViewport::visibleRect() const | |
| 114 { | |
| 115 FloatSize scaledSize(m_size); | |
| 116 scaledSize.scale(1 / m_scale); | |
| 117 return FloatRect(m_offset, scaledSize); | |
| 118 } | |
| 119 | |
| 120 FloatRect PinchViewport::visibleRectInDocument() const | |
| 121 { | |
| 122 if (!mainFrame() || !mainFrame()->view()) | |
| 123 return FloatRect(); | |
| 124 | |
| 125 FloatRect viewRect = mainFrame()->view()->visibleContentRect(); | |
| 126 FloatRect pinchRect = visibleRect(); | |
| 127 pinchRect.moveBy(viewRect.location()); | |
| 128 return pinchRect; | |
| 129 } | |
| 130 | |
| 131 void PinchViewport::scrollIntoView(const FloatRect& rect) | |
| 132 { | |
| 133 // FIXME(sky): Delete this whole file. | |
| 134 } | |
| 135 | |
| 136 void PinchViewport::setLocation(const FloatPoint& newLocation) | |
| 137 { | |
| 138 FloatPoint clampedOffset(clampOffsetToBoundaries(newLocation)); | |
| 139 | |
| 140 if (clampedOffset == m_offset) | |
| 141 return; | |
| 142 | |
| 143 m_offset = clampedOffset; | |
| 144 | |
| 145 ScrollingCoordinator* coordinator = m_frameHost.page().scrollingCoordinator(
); | |
| 146 ASSERT(coordinator); | |
| 147 coordinator->scrollableAreaScrollLayerDidChange(this); | |
| 148 } | |
| 149 | |
| 150 void PinchViewport::move(const FloatPoint& delta) | |
| 151 { | |
| 152 setLocation(m_offset + delta); | |
| 153 } | |
| 154 | |
| 155 void PinchViewport::setScale(float scale) | |
| 156 { | |
| 157 if (scale == m_scale) | |
| 158 return; | |
| 159 | |
| 160 m_scale = scale; | |
| 161 | |
| 162 // Old-style pinch sets scale here but we shouldn't call into the | |
| 163 // clamping code below. | |
| 164 if (!m_innerViewportScrollLayer) | |
| 165 return; | |
| 166 | |
| 167 // Ensure we clamp so we remain within the bounds. | |
| 168 setLocation(visibleRect().location()); | |
| 169 | |
| 170 // TODO: We should probably be calling scaleDidChange type functions here. | |
| 171 // see Page::setPageScaleFactor. | |
| 172 } | |
| 173 | |
| 174 // Modifies the top of the graphics layer tree to add layers needed to support | |
| 175 // the inner/outer viewport fixed-position model for pinch zoom. When finished, | |
| 176 // the tree will look like this (with * denoting added layers): | |
| 177 // | |
| 178 // *rootTransformLayer | |
| 179 // +- *innerViewportContainerLayer (fixed pos container) | |
| 180 // +- *pageScaleLayer | |
| 181 // | +- *innerViewportScrollLayer | |
| 182 // | +-- overflowControlsHostLayer (root layer) | |
| 183 // | +-- outerViewportContainerLayer (fixed pos container) [frame
container layer in RenderLayerCompositor] | |
| 184 // | | +-- outerViewportScrollLayer [frame scroll layer in Rend
erLayerCompositor] | |
| 185 // | | +-- content layers ... | |
| 186 // | +-- horizontal ScrollbarLayer (non-overlay) | |
| 187 // | +-- verticalScrollbarLayer (non-overlay) | |
| 188 // | +-- scroll corner (non-overlay) | |
| 189 // +- *horizontalScrollbarLayer (overlay) | |
| 190 // +- *verticalScrollbarLayer (overlay) | |
| 191 // | |
| 192 void PinchViewport::attachToLayerTree(GraphicsLayer* currentLayerTreeRoot, Graph
icsLayerFactory* graphicsLayerFactory) | |
| 193 { | |
| 194 TRACE_EVENT1("blink", "PinchViewport::attachToLayerTree", "currentLayerTreeR
oot", (bool)currentLayerTreeRoot); | |
| 195 if (!currentLayerTreeRoot) { | |
| 196 m_innerViewportScrollLayer->removeAllChildren(); | |
| 197 return; | |
| 198 } | |
| 199 | |
| 200 if (currentLayerTreeRoot->parent() && currentLayerTreeRoot->parent() == m_in
nerViewportScrollLayer) | |
| 201 return; | |
| 202 | |
| 203 if (!m_innerViewportScrollLayer) { | |
| 204 ASSERT(!m_overlayScrollbarHorizontal | |
| 205 && !m_overlayScrollbarVertical | |
| 206 && !m_pageScaleLayer | |
| 207 && !m_innerViewportContainerLayer); | |
| 208 | |
| 209 // FIXME: The root transform layer should only be created on demand. | |
| 210 m_rootTransformLayer = GraphicsLayer::create(graphicsLayerFactory, this)
; | |
| 211 m_innerViewportContainerLayer = GraphicsLayer::create(graphicsLayerFacto
ry, this); | |
| 212 m_pageScaleLayer = GraphicsLayer::create(graphicsLayerFactory, this); | |
| 213 m_innerViewportScrollLayer = GraphicsLayer::create(graphicsLayerFactory,
this); | |
| 214 m_overlayScrollbarHorizontal = GraphicsLayer::create(graphicsLayerFactor
y, this); | |
| 215 m_overlayScrollbarVertical = GraphicsLayer::create(graphicsLayerFactory,
this); | |
| 216 | |
| 217 blink::ScrollingCoordinator* coordinator = m_frameHost.page().scrollingC
oordinator(); | |
| 218 ASSERT(coordinator); | |
| 219 coordinator->setLayerIsContainerForFixedPositionLayers(m_innerViewportSc
rollLayer.get(), true); | |
| 220 | |
| 221 // Set masks to bounds so the compositor doesn't clobber a manually | |
| 222 // set inner viewport container layer size. | |
| 223 m_innerViewportContainerLayer->setMasksToBounds(m_frameHost.settings().m
ainFrameClipsContent()); | |
| 224 m_innerViewportContainerLayer->setSize(m_size); | |
| 225 | |
| 226 m_innerViewportScrollLayer->platformLayer()->setScrollClipLayer( | |
| 227 m_innerViewportContainerLayer->platformLayer()); | |
| 228 m_innerViewportScrollLayer->platformLayer()->setUserScrollable(true, tru
e); | |
| 229 | |
| 230 m_rootTransformLayer->addChild(m_innerViewportContainerLayer.get()); | |
| 231 m_innerViewportContainerLayer->addChild(m_pageScaleLayer.get()); | |
| 232 m_pageScaleLayer->addChild(m_innerViewportScrollLayer.get()); | |
| 233 m_innerViewportContainerLayer->addChild(m_overlayScrollbarHorizontal.get
()); | |
| 234 m_innerViewportContainerLayer->addChild(m_overlayScrollbarVertical.get()
); | |
| 235 | |
| 236 // Ensure this class is set as the scroll layer's ScrollableArea. | |
| 237 coordinator->scrollableAreaScrollLayerDidChange(this); | |
| 238 | |
| 239 // Setup the inner viewport overlay scrollbars. | |
| 240 setupScrollbar(WebScrollbar::Horizontal); | |
| 241 setupScrollbar(WebScrollbar::Vertical); | |
| 242 } | |
| 243 | |
| 244 m_innerViewportScrollLayer->removeAllChildren(); | |
| 245 m_innerViewportScrollLayer->addChild(currentLayerTreeRoot); | |
| 246 | |
| 247 // We only need to disable the existing (outer viewport) scrollbars | |
| 248 // if the existing ones are already overlay. | |
| 249 // FIXME: If we knew in advance before the overflowControlsHostLayer goes | |
| 250 // away, we would re-enable the drawing of these scrollbars. | |
| 251 // FIXME: This doesn't seem to work (at least on Android). Commenting out fo
r now until | |
| 252 // I figure out how to access RenderLayerCompositor from here. | |
| 253 // if (GraphicsLayer* scrollbar = m_frameHost->compositor()->layerForHorizon
talScrollbar()) | |
| 254 // scrollbar->setDrawsContent(!page->mainFrame()->view()->hasOverlayScrol
lbars()); | |
| 255 // if (GraphicsLayer* scrollbar = m_frameHost->compositor()->layerForVertica
lScrollbar()) | |
| 256 // scrollbar->setDrawsContent(!page->mainFrame()->view()->hasOverlayScrol
lbars()); | |
| 257 } | |
| 258 | |
| 259 void PinchViewport::setupScrollbar(WebScrollbar::Orientation orientation) | |
| 260 { | |
| 261 bool isHorizontal = orientation == WebScrollbar::Horizontal; | |
| 262 GraphicsLayer* scrollbarGraphicsLayer = isHorizontal ? | |
| 263 m_overlayScrollbarHorizontal.get() : m_overlayScrollbarVertical.get(); | |
| 264 OwnPtr<WebScrollbarLayer>& webScrollbarLayer = isHorizontal ? | |
| 265 m_webOverlayScrollbarHorizontal : m_webOverlayScrollbarVertical; | |
| 266 | |
| 267 const int overlayScrollbarThickness = m_frameHost.settings().pinchOverlayScr
ollbarThickness(); | |
| 268 | |
| 269 if (!webScrollbarLayer) { | |
| 270 ScrollingCoordinator* coordinator = m_frameHost.page().scrollingCoordina
tor(); | |
| 271 ASSERT(coordinator); | |
| 272 ScrollbarOrientation webcoreOrientation = isHorizontal ? HorizontalScrol
lbar : VerticalScrollbar; | |
| 273 webScrollbarLayer = coordinator->createSolidColorScrollbarLayer(webcoreO
rientation, overlayScrollbarThickness, 0, false); | |
| 274 | |
| 275 webScrollbarLayer->setClipLayer(m_innerViewportContainerLayer->platformL
ayer()); | |
| 276 scrollbarGraphicsLayer->setContentsToPlatformLayer(webScrollbarLayer->la
yer()); | |
| 277 scrollbarGraphicsLayer->setDrawsContent(false); | |
| 278 } | |
| 279 | |
| 280 int xPosition = isHorizontal ? 0 : m_innerViewportContainerLayer->size().wid
th() - overlayScrollbarThickness; | |
| 281 int yPosition = isHorizontal ? m_innerViewportContainerLayer->size().height(
) - overlayScrollbarThickness : 0; | |
| 282 int width = isHorizontal ? m_innerViewportContainerLayer->size().width() - o
verlayScrollbarThickness : overlayScrollbarThickness; | |
| 283 int height = isHorizontal ? overlayScrollbarThickness : m_innerViewportConta
inerLayer->size().height() - overlayScrollbarThickness; | |
| 284 | |
| 285 // Use the GraphicsLayer to position the scrollbars. | |
| 286 scrollbarGraphicsLayer->setPosition(IntPoint(xPosition, yPosition)); | |
| 287 scrollbarGraphicsLayer->setSize(IntSize(width, height)); | |
| 288 scrollbarGraphicsLayer->setContentsRect(IntRect(0, 0, width, height)); | |
| 289 } | |
| 290 | |
| 291 void PinchViewport::registerLayersWithTreeView(WebLayerTreeView* layerTreeView)
const | |
| 292 { | |
| 293 TRACE_EVENT0("blink", "PinchViewport::registerLayersWithTreeView"); | |
| 294 ASSERT(layerTreeView); | |
| 295 ASSERT(m_frameHost.page().mainFrame()); | |
| 296 ASSERT(m_frameHost.page().mainFrame()->contentRenderer()); | |
| 297 | |
| 298 RenderLayerCompositor* compositor = m_frameHost.page().mainFrame()->contentR
enderer()->compositor(); | |
| 299 // Get the outer viewport scroll layer. | |
| 300 WebLayer* scrollLayer = compositor->scrollLayer()->platformLayer(); | |
| 301 | |
| 302 m_webOverlayScrollbarHorizontal->setScrollLayer(scrollLayer); | |
| 303 m_webOverlayScrollbarVertical->setScrollLayer(scrollLayer); | |
| 304 | |
| 305 ASSERT(compositor); | |
| 306 layerTreeView->registerViewportLayers( | |
| 307 m_pageScaleLayer->platformLayer(), | |
| 308 m_innerViewportScrollLayer->platformLayer(), | |
| 309 scrollLayer); | |
| 310 } | |
| 311 | |
| 312 void PinchViewport::clearLayersForTreeView(WebLayerTreeView* layerTreeView) cons
t | |
| 313 { | |
| 314 ASSERT(layerTreeView); | |
| 315 | |
| 316 layerTreeView->clearViewportLayers(); | |
| 317 } | |
| 318 | |
| 319 int PinchViewport::scrollSize(ScrollbarOrientation orientation) const | |
| 320 { | |
| 321 IntSize scrollDimensions = maximumScrollPosition() - minimumScrollPosition()
; | |
| 322 return (orientation == HorizontalScrollbar) ? scrollDimensions.width() : scr
ollDimensions.height(); | |
| 323 } | |
| 324 | |
| 325 IntPoint PinchViewport::minimumScrollPosition() const | |
| 326 { | |
| 327 return IntPoint(); | |
| 328 } | |
| 329 | |
| 330 IntPoint PinchViewport::maximumScrollPosition() const | |
| 331 { | |
| 332 return flooredIntPoint(FloatSize(contentsSize()) - visibleRect().size()); | |
| 333 } | |
| 334 | |
| 335 IntRect PinchViewport::scrollableAreaBoundingBox() const | |
| 336 { | |
| 337 // This method should return the bounding box in the parent view's coordinat
e | |
| 338 // space; however, PinchViewport technically isn't a child of any Frames. | |
| 339 // Nonetheless, the PinchViewport always occupies the entire main frame so j
ust | |
| 340 // return that. | |
| 341 LocalFrame* frame = mainFrame(); | |
| 342 | |
| 343 if (!frame || !frame->view()) | |
| 344 return IntRect(); | |
| 345 | |
| 346 return frame->view()->frameRect(); | |
| 347 } | |
| 348 | |
| 349 IntSize PinchViewport::contentsSize() const | |
| 350 { | |
| 351 LocalFrame* frame = mainFrame(); | |
| 352 | |
| 353 if (!frame || !frame->view()) | |
| 354 return IntSize(); | |
| 355 | |
| 356 ASSERT(frame->view()->visibleContentScaleFactor() == 1); | |
| 357 return frame->view()->visibleContentRect(IncludeScrollbars).size(); | |
| 358 } | |
| 359 | |
| 360 void PinchViewport::invalidateScrollbarRect(Scrollbar*, const IntRect&) | |
| 361 { | |
| 362 // Do nothing. Pinch scrollbars live on the compositor thread and will | |
| 363 // be updated when the viewport is synced to the CC. | |
| 364 } | |
| 365 | |
| 366 void PinchViewport::setScrollOffset(const IntPoint& offset) | |
| 367 { | |
| 368 setLocation(offset); | |
| 369 } | |
| 370 | |
| 371 GraphicsLayer* PinchViewport::layerForContainer() const | |
| 372 { | |
| 373 return m_innerViewportContainerLayer.get(); | |
| 374 } | |
| 375 | |
| 376 GraphicsLayer* PinchViewport::layerForScrolling() const | |
| 377 { | |
| 378 return m_innerViewportScrollLayer.get(); | |
| 379 } | |
| 380 | |
| 381 GraphicsLayer* PinchViewport::layerForHorizontalScrollbar() const | |
| 382 { | |
| 383 return m_overlayScrollbarHorizontal.get(); | |
| 384 } | |
| 385 | |
| 386 GraphicsLayer* PinchViewport::layerForVerticalScrollbar() const | |
| 387 { | |
| 388 return m_overlayScrollbarVertical.get(); | |
| 389 } | |
| 390 | |
| 391 void PinchViewport::notifyAnimationStarted(const GraphicsLayer*, double monotoni
cTime) | |
| 392 { | |
| 393 } | |
| 394 | |
| 395 void PinchViewport::paintContents(const GraphicsLayer*, GraphicsContext&, Graphi
csLayerPaintingPhase, const IntRect& inClip) | |
| 396 { | |
| 397 } | |
| 398 | |
| 399 LocalFrame* PinchViewport::mainFrame() const | |
| 400 { | |
| 401 return m_frameHost.page().mainFrame(); | |
| 402 } | |
| 403 | |
| 404 FloatPoint PinchViewport::clampOffsetToBoundaries(const FloatPoint& offset) | |
| 405 { | |
| 406 FloatPoint clampedOffset(offset); | |
| 407 clampedOffset = clampedOffset.shrunkTo(FloatPoint(maximumScrollPosition())); | |
| 408 clampedOffset = clampedOffset.expandedTo(FloatPoint(minimumScrollPosition())
); | |
| 409 return clampedOffset; | |
| 410 } | |
| 411 | |
| 412 String PinchViewport::debugName(const GraphicsLayer* graphicsLayer) | |
| 413 { | |
| 414 String name; | |
| 415 if (graphicsLayer == m_innerViewportContainerLayer.get()) { | |
| 416 name = "Inner Viewport Container Layer"; | |
| 417 } else if (graphicsLayer == m_pageScaleLayer.get()) { | |
| 418 name = "Page Scale Layer"; | |
| 419 } else if (graphicsLayer == m_innerViewportScrollLayer.get()) { | |
| 420 name = "Inner Viewport Scroll Layer"; | |
| 421 } else if (graphicsLayer == m_overlayScrollbarHorizontal.get()) { | |
| 422 name = "Overlay Scrollbar Horizontal Layer"; | |
| 423 } else if (graphicsLayer == m_overlayScrollbarVertical.get()) { | |
| 424 name = "Overlay Scrollbar Vertical Layer"; | |
| 425 } else { | |
| 426 ASSERT_NOT_REACHED(); | |
| 427 } | |
| 428 | |
| 429 return name; | |
| 430 } | |
| 431 | |
| 432 } // namespace blink | |
| OLD | NEW |