PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/src/qt/qtwebkit/Source/WebCore/rendering/RenderLayerBacking.cpp

https://gitlab.com/x33n/phantomjs
C++ | 1208 lines | 913 code | 194 blank | 101 comment | 250 complexity | 45e6df16fc1432918a208b7b3ce058f4 MD5 | raw file
  1. /*
  2. * Copyright (C) 2009, 2010, 2011 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 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. #include "config.h"
  26. #if USE(ACCELERATED_COMPOSITING)
  27. #include "RenderLayerBacking.h"
  28. #include "AnimationController.h"
  29. #include "CanvasRenderingContext.h"
  30. #include "CSSPropertyNames.h"
  31. #include "CachedImage.h"
  32. #include "Chrome.h"
  33. #include "FontCache.h"
  34. #include "FrameView.h"
  35. #include "GraphicsContext.h"
  36. #include "GraphicsLayer.h"
  37. #include "HTMLCanvasElement.h"
  38. #include "HTMLIFrameElement.h"
  39. #include "HTMLMediaElement.h"
  40. #include "HTMLNames.h"
  41. #include "HTMLPlugInElement.h"
  42. #include "InspectorInstrumentation.h"
  43. #include "KeyframeList.h"
  44. #include "PluginViewBase.h"
  45. #include "ProgressTracker.h"
  46. #include "RenderApplet.h"
  47. #include "RenderIFrame.h"
  48. #include "RenderImage.h"
  49. #include "RenderLayerCompositor.h"
  50. #include "RenderEmbeddedObject.h"
  51. #include "RenderVideo.h"
  52. #include "RenderView.h"
  53. #include "ScrollingCoordinator.h"
  54. #include "Settings.h"
  55. #include "StyleResolver.h"
  56. #include "TiledBacking.h"
  57. #include <wtf/text/StringBuilder.h>
  58. #if ENABLE(CSS_FILTERS)
  59. #include "FilterEffectRenderer.h"
  60. #if ENABLE(CSS_SHADERS)
  61. #include "CustomFilterOperation.h"
  62. #endif
  63. #endif
  64. #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
  65. #include "GraphicsContext3D.h"
  66. #endif
  67. using namespace std;
  68. namespace WebCore {
  69. using namespace HTMLNames;
  70. static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle*);
  71. static IntRect clipBox(RenderBox* renderer);
  72. static inline bool isAcceleratedCanvas(RenderObject* renderer)
  73. {
  74. #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
  75. if (renderer->isCanvas()) {
  76. HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer->node());
  77. if (CanvasRenderingContext* context = canvas->renderingContext())
  78. return context->isAccelerated();
  79. }
  80. #else
  81. UNUSED_PARAM(renderer);
  82. #endif
  83. return false;
  84. }
  85. // Get the scrolling coordinator in a way that works inside RenderLayerBacking's destructor.
  86. static ScrollingCoordinator* scrollingCoordinatorFromLayer(RenderLayer* layer)
  87. {
  88. Page* page = layer->renderer()->frame()->page();
  89. if (!page)
  90. return 0;
  91. return page->scrollingCoordinator();
  92. }
  93. bool RenderLayerBacking::m_creatingPrimaryGraphicsLayer = false;
  94. RenderLayerBacking::RenderLayerBacking(RenderLayer* layer)
  95. : m_owningLayer(layer)
  96. , m_scrollLayerID(0)
  97. , m_artificiallyInflatedBounds(false)
  98. , m_boundsConstrainedByClipping(false)
  99. , m_isMainFrameRenderViewLayer(false)
  100. , m_usingTiledCacheLayer(false)
  101. , m_requiresOwnBackingStore(true)
  102. #if ENABLE(CSS_FILTERS)
  103. , m_canCompositeFilters(false)
  104. #endif
  105. , m_backgroundLayerPaintsFixedRootBackground(false)
  106. , m_didSwitchToFullTileCoverageDuringLoading(false)
  107. {
  108. if (layer->isRootLayer()) {
  109. Frame* frame = toRenderView(renderer())->frameView()->frame();
  110. Page* page = frame ? frame->page() : 0;
  111. if (page && frame && page->mainFrame() == frame) {
  112. m_isMainFrameRenderViewLayer = true;
  113. #if PLATFORM(MAC)
  114. // FIXME: It's a little weird that we base this decision on whether there's a scrolling coordinator or not.
  115. if (page->scrollingCoordinator())
  116. m_usingTiledCacheLayer = true;
  117. #endif
  118. }
  119. }
  120. createPrimaryGraphicsLayer();
  121. if (m_usingTiledCacheLayer) {
  122. TiledBacking* tiledBacking = this->tiledBacking();
  123. if (Page* page = renderer()->frame()->page()) {
  124. Frame* frame = renderer()->frame();
  125. tiledBacking->setIsInWindow(page->isInWindow());
  126. if (m_isMainFrameRenderViewLayer)
  127. tiledBacking->setUnparentsOffscreenTiles(true);
  128. tiledBacking->setScrollingPerformanceLoggingEnabled(frame->settings() && frame->settings()->scrollingPerformanceLoggingEnabled());
  129. adjustTiledBackingCoverage();
  130. }
  131. }
  132. }
  133. RenderLayerBacking::~RenderLayerBacking()
  134. {
  135. updateClippingLayers(false, false);
  136. updateOverflowControlsLayers(false, false, false);
  137. updateForegroundLayer(false);
  138. updateBackgroundLayer(false);
  139. updateMaskLayer(false);
  140. updateScrollingLayers(false);
  141. detachFromScrollingCoordinator();
  142. destroyGraphicsLayers();
  143. }
  144. void RenderLayerBacking::willDestroyLayer(const GraphicsLayer* layer)
  145. {
  146. if (layer && layer->usingTiledBacking()) {
  147. if (RenderLayerCompositor* compositor = this->compositor())
  148. compositor->layerTiledBackingUsageChanged(layer, false);
  149. }
  150. }
  151. PassOwnPtr<GraphicsLayer> RenderLayerBacking::createGraphicsLayer(const String& name)
  152. {
  153. GraphicsLayerFactory* graphicsLayerFactory = 0;
  154. if (Page* page = renderer()->frame()->page())
  155. graphicsLayerFactory = page->chrome().client()->graphicsLayerFactory();
  156. OwnPtr<GraphicsLayer> graphicsLayer = GraphicsLayer::create(graphicsLayerFactory, this);
  157. #ifndef NDEBUG
  158. graphicsLayer->setName(name);
  159. #else
  160. UNUSED_PARAM(name);
  161. #endif
  162. graphicsLayer->setMaintainsPixelAlignment(compositor()->keepLayersPixelAligned());
  163. #if PLATFORM(MAC) && USE(CA)
  164. graphicsLayer->setAcceleratesDrawing(compositor()->acceleratedDrawingEnabled());
  165. #endif
  166. return graphicsLayer.release();
  167. }
  168. bool RenderLayerBacking::shouldUseTiledBacking(const GraphicsLayer*) const
  169. {
  170. return m_usingTiledCacheLayer && m_creatingPrimaryGraphicsLayer;
  171. }
  172. void RenderLayerBacking::tiledBackingUsageChanged(const GraphicsLayer* layer, bool usingTiledBacking)
  173. {
  174. compositor()->layerTiledBackingUsageChanged(layer, usingTiledBacking);
  175. }
  176. TiledBacking* RenderLayerBacking::tiledBacking() const
  177. {
  178. return m_graphicsLayer->tiledBacking();
  179. }
  180. static TiledBacking::TileCoverage computeTileCoverage(RenderLayerBacking* backing)
  181. {
  182. // FIXME: When we use TiledBacking for overflow, this should look at RenderView scrollability.
  183. Frame* frame = backing->owningLayer()->renderer()->frame();
  184. if (!frame)
  185. return TiledBacking::CoverageForVisibleArea;
  186. TiledBacking::TileCoverage tileCoverage = TiledBacking::CoverageForVisibleArea;
  187. FrameView* frameView = frame->view();
  188. bool useMinimalTilesDuringLiveResize = frameView->inLiveResize();
  189. bool useMinimalTilesDuringLoading = false;
  190. // Avoid churn.
  191. if (!backing->didSwitchToFullTileCoverageDuringLoading()) {
  192. useMinimalTilesDuringLoading = !frameView->isVisuallyNonEmpty() || (frame->page()->progress()->isMainLoadProgressing() && !frameView->wasScrolledByUser());
  193. if (!useMinimalTilesDuringLoading)
  194. backing->setDidSwitchToFullTileCoverageDuringLoading();
  195. }
  196. if (!(useMinimalTilesDuringLoading || useMinimalTilesDuringLiveResize)) {
  197. bool clipsToExposedRect = backing->tiledBacking()->clipsToExposedRect();
  198. if (frameView->horizontalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
  199. tileCoverage |= TiledBacking::CoverageForHorizontalScrolling;
  200. if (frameView->verticalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
  201. tileCoverage |= TiledBacking::CoverageForVerticalScrolling;
  202. }
  203. if (ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(backing->owningLayer())) {
  204. // Ask our TiledBacking for large tiles unless the only reason we're main-thread-scrolling
  205. // is a page overlay (find-in-page, the Web Inspector highlight mechanism, etc.).
  206. if (scrollingCoordinator->mainThreadScrollingReasons() & ~ScrollingCoordinator::ForcedOnMainThread)
  207. tileCoverage |= TiledBacking::CoverageForSlowScrolling;
  208. }
  209. return tileCoverage;
  210. }
  211. void RenderLayerBacking::adjustTiledBackingCoverage()
  212. {
  213. if (!m_usingTiledCacheLayer)
  214. return;
  215. TiledBacking::TileCoverage tileCoverage = computeTileCoverage(this);
  216. tiledBacking()->setTileCoverage(tileCoverage);
  217. }
  218. void RenderLayerBacking::updateDebugIndicators(bool showBorder, bool showRepaintCounter)
  219. {
  220. m_graphicsLayer->setShowDebugBorder(showBorder);
  221. m_graphicsLayer->setShowRepaintCounter(showRepaintCounter);
  222. if (m_ancestorClippingLayer)
  223. m_ancestorClippingLayer->setShowDebugBorder(showBorder);
  224. if (m_foregroundLayer) {
  225. m_foregroundLayer->setShowDebugBorder(showBorder);
  226. m_foregroundLayer->setShowRepaintCounter(showRepaintCounter);
  227. }
  228. if (m_contentsContainmentLayer)
  229. m_contentsContainmentLayer->setShowDebugBorder(showBorder);
  230. if (m_backgroundLayer) {
  231. m_backgroundLayer->setShowDebugBorder(showBorder);
  232. m_backgroundLayer->setShowRepaintCounter(showRepaintCounter);
  233. }
  234. if (m_maskLayer) {
  235. m_maskLayer->setShowDebugBorder(showBorder);
  236. m_maskLayer->setShowRepaintCounter(showRepaintCounter);
  237. }
  238. if (m_layerForHorizontalScrollbar)
  239. m_layerForHorizontalScrollbar->setShowDebugBorder(showBorder);
  240. if (m_layerForVerticalScrollbar)
  241. m_layerForVerticalScrollbar->setShowDebugBorder(showBorder);
  242. if (m_layerForScrollCorner)
  243. m_layerForScrollCorner->setShowDebugBorder(showBorder);
  244. if (m_scrollingLayer)
  245. m_scrollingLayer->setShowDebugBorder(showBorder);
  246. if (m_scrollingContentsLayer) {
  247. m_scrollingContentsLayer->setShowDebugBorder(showBorder);
  248. m_scrollingContentsLayer->setShowRepaintCounter(showRepaintCounter);
  249. }
  250. }
  251. void RenderLayerBacking::createPrimaryGraphicsLayer()
  252. {
  253. String layerName;
  254. #ifndef NDEBUG
  255. layerName = m_owningLayer->name();
  256. #endif
  257. // The call to createGraphicsLayer ends calling back into here as
  258. // a GraphicsLayerClient to ask if it shouldUseTiledBacking(). We only want
  259. // the tile cache on our main layer. This is pretty ugly, but saves us from
  260. // exposing the API to all clients.
  261. m_creatingPrimaryGraphicsLayer = true;
  262. m_graphicsLayer = createGraphicsLayer(layerName);
  263. m_creatingPrimaryGraphicsLayer = false;
  264. if (m_usingTiledCacheLayer)
  265. m_childContainmentLayer = createGraphicsLayer("TiledBacking Flattening Layer");
  266. if (m_isMainFrameRenderViewLayer) {
  267. m_graphicsLayer->setContentsOpaque(true);
  268. m_graphicsLayer->setAppliesPageScale();
  269. }
  270. #if PLATFORM(MAC) && USE(CA)
  271. if (!compositor()->acceleratedDrawingEnabled() && renderer()->isCanvas()) {
  272. HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer()->node());
  273. if (canvas->shouldAccelerate(canvas->size()))
  274. m_graphicsLayer->setAcceleratesDrawing(true);
  275. }
  276. #endif
  277. updateOpacity(renderer()->style());
  278. updateTransform(renderer()->style());
  279. #if ENABLE(CSS_FILTERS)
  280. updateFilters(renderer()->style());
  281. #endif
  282. #if ENABLE(CSS_COMPOSITING)
  283. updateLayerBlendMode(renderer()->style());
  284. #endif
  285. }
  286. void RenderLayerBacking::destroyGraphicsLayers()
  287. {
  288. if (m_graphicsLayer) {
  289. willDestroyLayer(m_graphicsLayer.get());
  290. m_graphicsLayer->removeFromParent();
  291. }
  292. m_ancestorClippingLayer = nullptr;
  293. m_contentsContainmentLayer = nullptr;
  294. m_graphicsLayer = nullptr;
  295. m_foregroundLayer = nullptr;
  296. m_backgroundLayer = nullptr;
  297. m_childContainmentLayer = nullptr;
  298. m_maskLayer = nullptr;
  299. m_scrollingLayer = nullptr;
  300. m_scrollingContentsLayer = nullptr;
  301. }
  302. void RenderLayerBacking::updateOpacity(const RenderStyle* style)
  303. {
  304. m_graphicsLayer->setOpacity(compositingOpacity(style->opacity()));
  305. }
  306. void RenderLayerBacking::updateTransform(const RenderStyle* style)
  307. {
  308. // FIXME: This could use m_owningLayer->transform(), but that currently has transform-origin
  309. // baked into it, and we don't want that.
  310. TransformationMatrix t;
  311. if (m_owningLayer->hasTransform()) {
  312. style->applyTransform(t, toRenderBox(renderer())->pixelSnappedBorderBoxRect().size(), RenderStyle::ExcludeTransformOrigin);
  313. makeMatrixRenderable(t, compositor()->canRender3DTransforms());
  314. }
  315. if (m_contentsContainmentLayer) {
  316. m_contentsContainmentLayer->setTransform(t);
  317. m_graphicsLayer->setTransform(TransformationMatrix());
  318. } else
  319. m_graphicsLayer->setTransform(t);
  320. }
  321. #if ENABLE(CSS_FILTERS)
  322. void RenderLayerBacking::updateFilters(const RenderStyle* style)
  323. {
  324. m_canCompositeFilters = m_graphicsLayer->setFilters(owningLayer()->computeFilterOperations(style));
  325. }
  326. #endif
  327. #if ENABLE(CSS_COMPOSITING)
  328. void RenderLayerBacking::updateLayerBlendMode(const RenderStyle*)
  329. {
  330. }
  331. #endif
  332. static bool hasNonZeroTransformOrigin(const RenderObject* renderer)
  333. {
  334. RenderStyle* style = renderer->style();
  335. return (style->transformOriginX().type() == Fixed && style->transformOriginX().value())
  336. || (style->transformOriginY().type() == Fixed && style->transformOriginY().value());
  337. }
  338. static bool layerOrAncestorIsTransformedOrUsingCompositedScrolling(RenderLayer* layer)
  339. {
  340. for (RenderLayer* curr = layer; curr; curr = curr->parent()) {
  341. if (curr->hasTransform() || curr->needsCompositedScrolling())
  342. return true;
  343. }
  344. return false;
  345. }
  346. bool RenderLayerBacking::shouldClipCompositedBounds() const
  347. {
  348. // Scrollbar layers use this layer for relative positioning, so don't clip.
  349. if (layerForHorizontalScrollbar() || layerForVerticalScrollbar())
  350. return false;
  351. if (m_usingTiledCacheLayer)
  352. return false;
  353. if (layerOrAncestorIsTransformedOrUsingCompositedScrolling(m_owningLayer))
  354. return false;
  355. return true;
  356. }
  357. void RenderLayerBacking::updateCompositedBounds()
  358. {
  359. LayoutRect layerBounds = compositor()->calculateCompositedBounds(m_owningLayer, m_owningLayer);
  360. // Clip to the size of the document or enclosing overflow-scroll layer.
  361. // If this or an ancestor is transformed, we can't currently compute the correct rect to intersect with.
  362. // We'd need RenderObject::convertContainerToLocalQuad(), which doesn't yet exist.
  363. if (shouldClipCompositedBounds()) {
  364. RenderView* view = m_owningLayer->renderer()->view();
  365. RenderLayer* rootLayer = view->layer();
  366. LayoutRect clippingBounds;
  367. if (renderer()->style()->position() == FixedPosition && renderer()->container() == view)
  368. clippingBounds = view->frameView()->viewportConstrainedVisibleContentRect();
  369. else
  370. clippingBounds = view->unscaledDocumentRect();
  371. if (m_owningLayer != rootLayer)
  372. clippingBounds.intersect(m_owningLayer->backgroundClipRect(RenderLayer::ClipRectsContext(rootLayer, 0, AbsoluteClipRects)).rect()); // FIXME: Incorrect for CSS regions.
  373. LayoutPoint delta;
  374. m_owningLayer->convertToLayerCoords(rootLayer, delta, RenderLayer::AdjustForColumns);
  375. clippingBounds.move(-delta.x(), -delta.y());
  376. layerBounds.intersect(clippingBounds);
  377. m_boundsConstrainedByClipping = true;
  378. } else
  379. m_boundsConstrainedByClipping = false;
  380. // If the element has a transform-origin that has fixed lengths, and the renderer has zero size,
  381. // then we need to ensure that the compositing layer has non-zero size so that we can apply
  382. // the transform-origin via the GraphicsLayer anchorPoint (which is expressed as a fractional value).
  383. if (layerBounds.isEmpty() && hasNonZeroTransformOrigin(renderer())) {
  384. layerBounds.setWidth(1);
  385. layerBounds.setHeight(1);
  386. m_artificiallyInflatedBounds = true;
  387. } else
  388. m_artificiallyInflatedBounds = false;
  389. setCompositedBounds(layerBounds);
  390. }
  391. void RenderLayerBacking::updateAfterWidgetResize()
  392. {
  393. if (renderer()->isRenderPart()) {
  394. if (RenderLayerCompositor* innerCompositor = RenderLayerCompositor::frameContentsCompositor(toRenderPart(renderer()))) {
  395. innerCompositor->frameViewDidChangeSize();
  396. innerCompositor->frameViewDidChangeLocation(flooredIntPoint(contentsBox().location()));
  397. }
  398. }
  399. }
  400. void RenderLayerBacking::updateAfterLayout(UpdateAfterLayoutFlags flags)
  401. {
  402. RenderLayerCompositor* layerCompositor = compositor();
  403. if (!layerCompositor->compositingLayersNeedRebuild()) {
  404. // Calling updateGraphicsLayerGeometry() here gives incorrect results, because the
  405. // position of this layer's GraphicsLayer depends on the position of our compositing
  406. // ancestor's GraphicsLayer. That cannot be determined until all the descendant
  407. // RenderLayers of that ancestor have been processed via updateLayerPositions().
  408. //
  409. // The solution is to update compositing children of this layer here,
  410. // via updateCompositingChildrenGeometry().
  411. updateCompositedBounds();
  412. layerCompositor->updateCompositingDescendantGeometry(m_owningLayer, m_owningLayer, flags & CompositingChildrenOnly);
  413. if (flags & IsUpdateRoot) {
  414. updateGraphicsLayerGeometry();
  415. layerCompositor->updateRootLayerPosition();
  416. RenderLayer* stackingContainer = m_owningLayer->enclosingStackingContainer();
  417. if (!layerCompositor->compositingLayersNeedRebuild() && stackingContainer && (stackingContainer != m_owningLayer))
  418. layerCompositor->updateCompositingDescendantGeometry(stackingContainer, stackingContainer, flags & CompositingChildrenOnly);
  419. }
  420. }
  421. if (flags & NeedsFullRepaint && !paintsIntoWindow() && !paintsIntoCompositedAncestor())
  422. setContentsNeedDisplay();
  423. }
  424. bool RenderLayerBacking::updateGraphicsLayerConfiguration()
  425. {
  426. RenderLayerCompositor* compositor = this->compositor();
  427. RenderObject* renderer = this->renderer();
  428. m_owningLayer->updateDescendantDependentFlags();
  429. m_owningLayer->updateZOrderLists();
  430. bool layerConfigChanged = false;
  431. setBackgroundLayerPaintsFixedRootBackground(compositor->needsFixedRootBackgroundLayer(m_owningLayer));
  432. // The background layer is currently only used for fixed root backgrounds.
  433. if (updateBackgroundLayer(m_backgroundLayerPaintsFixedRootBackground))
  434. layerConfigChanged = true;
  435. if (updateForegroundLayer(compositor->needsContentsCompositingLayer(m_owningLayer)))
  436. layerConfigChanged = true;
  437. bool needsDescendentsClippingLayer = compositor->clipsCompositingDescendants(m_owningLayer);
  438. // Our scrolling layer will clip.
  439. if (m_owningLayer->needsCompositedScrolling())
  440. needsDescendentsClippingLayer = false;
  441. if (updateClippingLayers(compositor->clippedByAncestor(m_owningLayer), needsDescendentsClippingLayer))
  442. layerConfigChanged = true;
  443. if (updateOverflowControlsLayers(requiresHorizontalScrollbarLayer(), requiresVerticalScrollbarLayer(), requiresScrollCornerLayer()))
  444. layerConfigChanged = true;
  445. if (updateScrollingLayers(m_owningLayer->needsCompositedScrolling()))
  446. layerConfigChanged = true;
  447. if (layerConfigChanged)
  448. updateInternalHierarchy();
  449. if (GraphicsLayer* flatteningLayer = tileCacheFlatteningLayer()) {
  450. flatteningLayer->removeFromParent();
  451. m_graphicsLayer->addChild(flatteningLayer);
  452. }
  453. if (updateMaskLayer(renderer->hasMask()))
  454. m_graphicsLayer->setMaskLayer(m_maskLayer.get());
  455. if (m_owningLayer->hasReflection()) {
  456. if (m_owningLayer->reflectionLayer()->backing()) {
  457. GraphicsLayer* reflectionLayer = m_owningLayer->reflectionLayer()->backing()->graphicsLayer();
  458. m_graphicsLayer->setReplicatedByLayer(reflectionLayer);
  459. }
  460. } else
  461. m_graphicsLayer->setReplicatedByLayer(0);
  462. bool isSimpleContainer = isSimpleContainerCompositingLayer();
  463. bool didUpdateContentsRect = false;
  464. updateDirectlyCompositedContents(isSimpleContainer, didUpdateContentsRect);
  465. updateRootLayerConfiguration();
  466. if (isDirectlyCompositedImage())
  467. updateImageContents();
  468. if (renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing()) {
  469. PluginViewBase* pluginViewBase = toPluginViewBase(toRenderWidget(renderer)->widget());
  470. if (!pluginViewBase->shouldNotAddLayer())
  471. m_graphicsLayer->setContentsToMedia(pluginViewBase->platformLayer());
  472. }
  473. #if ENABLE(VIDEO)
  474. else if (renderer->isVideo()) {
  475. HTMLMediaElement* mediaElement = toHTMLMediaElement(renderer->node());
  476. m_graphicsLayer->setContentsToMedia(mediaElement->platformLayer());
  477. }
  478. #endif
  479. #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
  480. else if (isAcceleratedCanvas(renderer)) {
  481. HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer->node());
  482. if (CanvasRenderingContext* context = canvas->renderingContext())
  483. m_graphicsLayer->setContentsToCanvas(context->platformLayer());
  484. layerConfigChanged = true;
  485. }
  486. #endif
  487. if (renderer->isRenderPart())
  488. layerConfigChanged = RenderLayerCompositor::parentFrameContentLayers(toRenderPart(renderer));
  489. return layerConfigChanged;
  490. }
  491. static IntRect clipBox(RenderBox* renderer)
  492. {
  493. LayoutRect result = PaintInfo::infiniteRect();
  494. if (renderer->hasOverflowClip())
  495. result = renderer->overflowClipRect(LayoutPoint(), 0); // FIXME: Incorrect for CSS regions.
  496. if (renderer->hasClip())
  497. result.intersect(renderer->clipRect(LayoutPoint(), 0)); // FIXME: Incorrect for CSS regions.
  498. return pixelSnappedIntRect(result);
  499. }
  500. void RenderLayerBacking::updateGraphicsLayerGeometry()
  501. {
  502. // If we haven't built z-order lists yet, wait until later.
  503. if (m_owningLayer->isStackingContainer() && m_owningLayer->m_zOrderListsDirty)
  504. return;
  505. // Set transform property, if it is not animating. We have to do this here because the transform
  506. // is affected by the layer dimensions.
  507. if (!renderer()->animation()->isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyWebkitTransform))
  508. updateTransform(renderer()->style());
  509. // Set opacity, if it is not animating.
  510. if (!renderer()->animation()->isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyOpacity))
  511. updateOpacity(renderer()->style());
  512. #if ENABLE(CSS_FILTERS)
  513. updateFilters(renderer()->style());
  514. #endif
  515. #if ENABLE(CSS_COMPOSITING)
  516. updateLayerBlendMode(renderer()->style());
  517. #endif
  518. bool isSimpleContainer = isSimpleContainerCompositingLayer();
  519. m_owningLayer->updateDescendantDependentFlags();
  520. // m_graphicsLayer is the corresponding GraphicsLayer for this RenderLayer and its non-compositing
  521. // descendants. So, the visibility flag for m_graphicsLayer should be true if there are any
  522. // non-compositing visible layers.
  523. m_graphicsLayer->setContentsVisible(m_owningLayer->hasVisibleContent() || hasVisibleNonCompositingDescendantLayers());
  524. RenderStyle* style = renderer()->style();
  525. // FIXME: reflections should force transform-style to be flat in the style: https://bugs.webkit.org/show_bug.cgi?id=106959
  526. bool preserves3D = style->transformStyle3D() == TransformStyle3DPreserve3D && !renderer()->hasReflection();
  527. m_graphicsLayer->setPreserves3D(preserves3D);
  528. m_graphicsLayer->setBackfaceVisibility(style->backfaceVisibility() == BackfaceVisibilityVisible);
  529. RenderLayer* compAncestor = m_owningLayer->ancestorCompositingLayer();
  530. // We compute everything relative to the enclosing compositing layer.
  531. IntRect ancestorCompositingBounds;
  532. if (compAncestor) {
  533. ASSERT(compAncestor->backing());
  534. ancestorCompositingBounds = pixelSnappedIntRect(compAncestor->backing()->compositedBounds());
  535. }
  536. LayoutRect localRawCompositingBounds = compositedBounds();
  537. LayoutPoint rawDelta;
  538. m_owningLayer->convertToLayerCoords(compAncestor, rawDelta, RenderLayer::AdjustForColumns);
  539. IntPoint delta = flooredIntPoint(rawDelta);
  540. m_subpixelAccumulation = toLayoutSize(rawDelta.fraction());
  541. // Move the bounds by the subpixel accumulation so that it pixel-snaps relative to absolute pixels instead of local coordinates.
  542. localRawCompositingBounds.move(m_subpixelAccumulation);
  543. IntRect localCompositingBounds = pixelSnappedIntRect(localRawCompositingBounds);
  544. IntRect relativeCompositingBounds(localCompositingBounds);
  545. relativeCompositingBounds.moveBy(delta);
  546. IntPoint graphicsLayerParentLocation;
  547. if (compAncestor && compAncestor->backing()->hasClippingLayer()) {
  548. // If the compositing ancestor has a layer to clip children, we parent in that, and therefore
  549. // position relative to it.
  550. IntRect clippingBox = clipBox(toRenderBox(compAncestor->renderer()));
  551. graphicsLayerParentLocation = clippingBox.location();
  552. } else if (compAncestor)
  553. graphicsLayerParentLocation = ancestorCompositingBounds.location();
  554. else
  555. graphicsLayerParentLocation = renderer()->view()->documentRect().location();
  556. if (compAncestor && compAncestor->needsCompositedScrolling()) {
  557. RenderBox* renderBox = toRenderBox(compAncestor->renderer());
  558. IntSize scrollOffset = compAncestor->scrolledContentOffset();
  559. IntPoint scrollOrigin(renderBox->borderLeft(), renderBox->borderTop());
  560. graphicsLayerParentLocation = scrollOrigin - scrollOffset;
  561. }
  562. if (compAncestor && m_ancestorClippingLayer) {
  563. // Call calculateRects to get the backgroundRect which is what is used to clip the contents of this
  564. // layer. Note that we call it with temporaryClipRects = true because normally when computing clip rects
  565. // for a compositing layer, rootLayer is the layer itself.
  566. RenderLayer::ClipRectsContext clipRectsContext(compAncestor, 0, TemporaryClipRects, IgnoreOverlayScrollbarSize, IgnoreOverflowClip);
  567. IntRect parentClipRect = pixelSnappedIntRect(m_owningLayer->backgroundClipRect(clipRectsContext).rect()); // FIXME: Incorrect for CSS regions.
  568. ASSERT(parentClipRect != PaintInfo::infiniteRect());
  569. m_ancestorClippingLayer->setPosition(FloatPoint(parentClipRect.location() - graphicsLayerParentLocation));
  570. m_ancestorClippingLayer->setSize(parentClipRect.size());
  571. // backgroundRect is relative to compAncestor, so subtract deltaX/deltaY to get back to local coords.
  572. m_ancestorClippingLayer->setOffsetFromRenderer(parentClipRect.location() - delta);
  573. // The primary layer is then parented in, and positioned relative to this clipping layer.
  574. graphicsLayerParentLocation = parentClipRect.location();
  575. }
  576. FloatSize contentsSize = relativeCompositingBounds.size();
  577. if (m_contentsContainmentLayer) {
  578. m_contentsContainmentLayer->setPreserves3D(preserves3D);
  579. m_contentsContainmentLayer->setPosition(FloatPoint(relativeCompositingBounds.location() - graphicsLayerParentLocation));
  580. // Use the same size as m_graphicsLayer so transforms behave correctly.
  581. m_contentsContainmentLayer->setSize(contentsSize);
  582. graphicsLayerParentLocation = relativeCompositingBounds.location();
  583. }
  584. m_graphicsLayer->setPosition(FloatPoint(relativeCompositingBounds.location() - graphicsLayerParentLocation));
  585. m_graphicsLayer->setOffsetFromRenderer(toIntSize(localCompositingBounds.location()));
  586. FloatSize oldSize = m_graphicsLayer->size();
  587. if (oldSize != contentsSize) {
  588. m_graphicsLayer->setSize(contentsSize);
  589. // Usually invalidation will happen via layout etc, but if we've affected the layer
  590. // size by constraining relative to a clipping ancestor or the viewport, we
  591. // have to invalidate to avoid showing stretched content.
  592. if (m_boundsConstrainedByClipping)
  593. m_graphicsLayer->setNeedsDisplay();
  594. }
  595. if (!m_isMainFrameRenderViewLayer) {
  596. // For non-root layers, background is always painted by the primary graphics layer.
  597. ASSERT(!m_backgroundLayer);
  598. m_graphicsLayer->setContentsOpaque(m_owningLayer->backgroundIsKnownToBeOpaqueInRect(localCompositingBounds));
  599. }
  600. // If we have a layer that clips children, position it.
  601. IntRect clippingBox;
  602. if (GraphicsLayer* clipLayer = clippingLayer()) {
  603. clippingBox = clipBox(toRenderBox(renderer()));
  604. clipLayer->setPosition(FloatPoint(clippingBox.location() - localCompositingBounds.location()));
  605. clipLayer->setSize(clippingBox.size());
  606. clipLayer->setOffsetFromRenderer(toIntSize(clippingBox.location()));
  607. }
  608. if (m_maskLayer) {
  609. if (m_maskLayer->size() != m_graphicsLayer->size()) {
  610. m_maskLayer->setSize(m_graphicsLayer->size());
  611. m_maskLayer->setNeedsDisplay();
  612. }
  613. m_maskLayer->setPosition(FloatPoint());
  614. m_maskLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
  615. }
  616. if (m_owningLayer->hasTransform()) {
  617. const IntRect borderBox = toRenderBox(renderer())->pixelSnappedBorderBoxRect();
  618. // Get layout bounds in the coords of compAncestor to match relativeCompositingBounds.
  619. IntRect layerBounds(delta, borderBox.size());
  620. // Update properties that depend on layer dimensions
  621. FloatPoint3D transformOrigin = computeTransformOrigin(borderBox);
  622. // Compute the anchor point, which is in the center of the renderer box unless transform-origin is set.
  623. FloatPoint3D anchor(relativeCompositingBounds.width() != 0.0f ? ((layerBounds.x() - relativeCompositingBounds.x()) + transformOrigin.x()) / relativeCompositingBounds.width() : 0.5f,
  624. relativeCompositingBounds.height() != 0.0f ? ((layerBounds.y() - relativeCompositingBounds.y()) + transformOrigin.y()) / relativeCompositingBounds.height() : 0.5f,
  625. transformOrigin.z());
  626. if (m_contentsContainmentLayer)
  627. m_contentsContainmentLayer->setAnchorPoint(anchor);
  628. else
  629. m_graphicsLayer->setAnchorPoint(anchor);
  630. RenderStyle* style = renderer()->style();
  631. GraphicsLayer* clipLayer = clippingLayer();
  632. if (style->hasPerspective()) {
  633. TransformationMatrix t = owningLayer()->perspectiveTransform();
  634. if (clipLayer) {
  635. clipLayer->setChildrenTransform(t);
  636. m_graphicsLayer->setChildrenTransform(TransformationMatrix());
  637. }
  638. else
  639. m_graphicsLayer->setChildrenTransform(t);
  640. } else {
  641. if (clipLayer)
  642. clipLayer->setChildrenTransform(TransformationMatrix());
  643. else
  644. m_graphicsLayer->setChildrenTransform(TransformationMatrix());
  645. }
  646. } else {
  647. m_graphicsLayer->setAnchorPoint(FloatPoint3D(0.5f, 0.5f, 0));
  648. if (m_contentsContainmentLayer)
  649. m_contentsContainmentLayer->setAnchorPoint(FloatPoint3D(0.5f, 0.5f, 0));
  650. }
  651. if (m_foregroundLayer) {
  652. FloatPoint foregroundPosition;
  653. FloatSize foregroundSize = contentsSize;
  654. IntSize foregroundOffset = m_graphicsLayer->offsetFromRenderer();
  655. if (hasClippingLayer()) {
  656. // If we have a clipping layer (which clips descendants), then the foreground layer is a child of it,
  657. // so that it gets correctly sorted with children. In that case, position relative to the clipping layer.
  658. foregroundSize = FloatSize(clippingBox.size());
  659. foregroundOffset = toIntSize(clippingBox.location());
  660. }
  661. m_foregroundLayer->setPosition(foregroundPosition);
  662. if (foregroundSize != m_foregroundLayer->size()) {
  663. m_foregroundLayer->setSize(foregroundSize);
  664. m_foregroundLayer->setNeedsDisplay();
  665. }
  666. m_foregroundLayer->setOffsetFromRenderer(foregroundOffset);
  667. }
  668. if (m_backgroundLayer) {
  669. FloatPoint backgroundPosition;
  670. FloatSize backgroundSize = contentsSize;
  671. if (backgroundLayerPaintsFixedRootBackground()) {
  672. FrameView* frameView = toRenderView(renderer())->frameView();
  673. backgroundPosition = IntPoint(frameView->scrollOffsetForFixedPosition());
  674. backgroundSize = frameView->visibleContentRect().size();
  675. }
  676. m_backgroundLayer->setPosition(backgroundPosition);
  677. if (backgroundSize != m_backgroundLayer->size()) {
  678. m_backgroundLayer->setSize(backgroundSize);
  679. m_backgroundLayer->setNeedsDisplay();
  680. }
  681. m_backgroundLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
  682. }
  683. if (m_owningLayer->reflectionLayer() && m_owningLayer->reflectionLayer()->isComposited()) {
  684. RenderLayerBacking* reflectionBacking = m_owningLayer->reflectionLayer()->backing();
  685. reflectionBacking->updateGraphicsLayerGeometry();
  686. // The reflection layer has the bounds of m_owningLayer->reflectionLayer(),
  687. // but the reflected layer is the bounds of this layer, so we need to position it appropriately.
  688. FloatRect layerBounds = compositedBounds();
  689. FloatRect reflectionLayerBounds = reflectionBacking->compositedBounds();
  690. reflectionBacking->graphicsLayer()->setReplicatedLayerPosition(FloatPoint(layerBounds.location() - reflectionLayerBounds.location()));
  691. }
  692. if (m_scrollingLayer) {
  693. ASSERT(m_scrollingContentsLayer);
  694. RenderBox* renderBox = toRenderBox(renderer());
  695. IntRect paddingBox(renderBox->borderLeft(), renderBox->borderTop(), renderBox->width() - renderBox->borderLeft() - renderBox->borderRight(), renderBox->height() - renderBox->borderTop() - renderBox->borderBottom());
  696. IntSize scrollOffset = m_owningLayer->scrollOffset();
  697. m_scrollingLayer->setPosition(FloatPoint(paddingBox.location() - localCompositingBounds.location()));
  698. m_scrollingLayer->setSize(paddingBox.size());
  699. m_scrollingContentsLayer->setPosition(FloatPoint(-scrollOffset.width(), -scrollOffset.height()));
  700. IntSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
  701. m_scrollingLayer->setOffsetFromRenderer(-toIntSize(paddingBox.location()));
  702. bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
  703. IntSize scrollSize(m_owningLayer->scrollWidth(), m_owningLayer->scrollHeight());
  704. if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
  705. m_scrollingContentsLayer->setNeedsDisplay();
  706. IntSize scrollingContentsOffset = toIntSize(paddingBox.location() - scrollOffset);
  707. if (scrollingContentsOffset != m_scrollingContentsLayer->offsetFromRenderer() || scrollSize != m_scrollingContentsLayer->size())
  708. compositor()->scrollingLayerDidChange(m_owningLayer);
  709. m_scrollingContentsLayer->setSize(scrollSize);
  710. // FIXME: The paint offset and the scroll offset should really be separate concepts.
  711. m_scrollingContentsLayer->setOffsetFromRenderer(scrollingContentsOffset, GraphicsLayer::DontSetNeedsDisplay);
  712. if (m_foregroundLayer) {
  713. if (m_foregroundLayer->size() != m_scrollingContentsLayer->size())
  714. m_foregroundLayer->setSize(m_scrollingContentsLayer->size());
  715. m_foregroundLayer->setNeedsDisplay();
  716. m_foregroundLayer->setOffsetFromRenderer(m_scrollingContentsLayer->offsetFromRenderer());
  717. }
  718. }
  719. // If this layer was created just for clipping or to apply perspective, it doesn't need its own backing store.
  720. setRequiresOwnBackingStore(compositor()->requiresOwnBackingStore(m_owningLayer, compAncestor, relativeCompositingBounds, ancestorCompositingBounds));
  721. bool didUpdateContentsRect = false;
  722. updateDirectlyCompositedContents(isSimpleContainer, didUpdateContentsRect);
  723. if (!didUpdateContentsRect && m_graphicsLayer->hasContentsLayer())
  724. resetContentsRect();
  725. updateDrawsContent(isSimpleContainer);
  726. updateAfterWidgetResize();
  727. registerScrollingLayers();
  728. }
  729. void RenderLayerBacking::updateDirectlyCompositedContents(bool isSimpleContainer, bool& didUpdateContentsRect)
  730. {
  731. if (!m_owningLayer->hasVisibleContent())
  732. return;
  733. // The order of operations here matters, since the last valid type of contents needs
  734. // to also update the contentsRect.
  735. updateDirectlyCompositedBackgroundColor(isSimpleContainer, didUpdateContentsRect);
  736. updateDirectlyCompositedBackgroundImage(isSimpleContainer, didUpdateContentsRect);
  737. }
  738. void RenderLayerBacking::registerScrollingLayers()
  739. {
  740. // Register fixed position layers and their containers with the scrolling coordinator.
  741. ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
  742. if (!scrollingCoordinator)
  743. return;
  744. compositor()->updateViewportConstraintStatus(m_owningLayer);
  745. if (!scrollingCoordinator->supportsFixedPositionLayers())
  746. return;
  747. scrollingCoordinator->updateLayerPositionConstraint(m_owningLayer);
  748. // Page scale is applied as a transform on the root render view layer. Because the scroll
  749. // layer is further up in the hierarchy, we need to avoid marking the root render view
  750. // layer as a container.
  751. bool isContainer = m_owningLayer->hasTransform() && !m_owningLayer->isRootLayer();
  752. scrollingCoordinator->setLayerIsContainerForFixedPositionLayers(childForSuperlayers(), isContainer);
  753. }
  754. void RenderLayerBacking::updateInternalHierarchy()
  755. {
  756. // m_foregroundLayer has to be inserted in the correct order with child layers,
  757. // so it's not inserted here.
  758. if (m_ancestorClippingLayer)
  759. m_ancestorClippingLayer->removeAllChildren();
  760. if (m_contentsContainmentLayer) {
  761. m_contentsContainmentLayer->removeAllChildren();
  762. if (m_ancestorClippingLayer)
  763. m_ancestorClippingLayer->addChild(m_contentsContainmentLayer.get());
  764. }
  765. if (m_backgroundLayer)
  766. m_contentsContainmentLayer->addChild(m_backgroundLayer.get());
  767. m_graphicsLayer->removeFromParent();
  768. if (m_contentsContainmentLayer)
  769. m_contentsContainmentLayer->addChild(m_graphicsLayer.get());
  770. else if (m_ancestorClippingLayer)
  771. m_ancestorClippingLayer->addChild(m_graphicsLayer.get());
  772. if (m_childContainmentLayer) {
  773. m_childContainmentLayer->removeFromParent();
  774. m_graphicsLayer->addChild(m_childContainmentLayer.get());
  775. }
  776. if (m_scrollingLayer) {
  777. GraphicsLayer* superlayer = m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
  778. m_scrollingLayer->removeFromParent();
  779. superlayer->addChild(m_scrollingLayer.get());
  780. }
  781. // The clip for child layers does not include space for overflow controls, so they exist as
  782. // siblings of the clipping layer if we have one. Normal children of this layer are set as
  783. // children of the clipping layer.
  784. if (m_layerForHorizontalScrollbar) {
  785. m_layerForHorizontalScrollbar->removeFromParent();
  786. m_graphicsLayer->addChild(m_layerForHorizontalScrollbar.get());
  787. }
  788. if (m_layerForVerticalScrollbar) {
  789. m_layerForVerticalScrollbar->removeFromParent();
  790. m_graphicsLayer->addChild(m_layerForVerticalScrollbar.get());
  791. }
  792. if (m_layerForScrollCorner) {
  793. m_layerForScrollCorner->removeFromParent();
  794. m_graphicsLayer->addChild(m_layerForScrollCorner.get());
  795. }
  796. }
  797. void RenderLayerBacking::resetContentsRect()
  798. {
  799. IntRect rect = pixelSnappedIntRect(contentsBox());
  800. m_graphicsLayer->setContentsRect(rect);
  801. m_graphicsLayer->setContentsTileSize(IntSize());
  802. m_graphicsLayer->setContentsTilePhase(IntPoint());
  803. }
  804. void RenderLayerBacking::updateDrawsContent()
  805. {
  806. updateDrawsContent(isSimpleContainerCompositingLayer());
  807. }
  808. void RenderLayerBacking::updateDrawsContent(bool isSimpleContainer)
  809. {
  810. if (m_scrollingLayer) {
  811. // We don't have to consider overflow controls, because we know that the scrollbars are drawn elsewhere.
  812. // m_graphicsLayer only needs backing store if the non-scrolling parts (background, outlines, borders, shadows etc) need to paint.
  813. // m_scrollingLayer never has backing store.
  814. // m_scrollingContentsLayer only needs backing store if the scrolled contents need to paint.
  815. bool hasNonScrollingPaintedContent = m_owningLayer->hasVisibleContent() && m_owningLayer->hasBoxDecorationsOrBackground();
  816. m_graphicsLayer->setDrawsContent(hasNonScrollingPaintedContent);
  817. bool hasScrollingPaintedContent = m_owningLayer->hasVisibleContent() && (renderer()->hasBackground() || paintsChildren());
  818. m_scrollingContentsLayer->setDrawsContent(hasScrollingPaintedContent);
  819. return;
  820. }
  821. bool hasPaintedContent = containsPaintedContent(isSimpleContainer);
  822. // FIXME: we could refine this to only allocate backing for one of these layers if possible.
  823. m_graphicsLayer->setDrawsContent(hasPaintedContent);
  824. if (m_foregroundLayer)
  825. m_foregroundLayer->setDrawsContent(hasPaintedContent);
  826. if (m_backgroundLayer)
  827. m_backgroundLayer->setDrawsContent(hasPaintedContent);
  828. }
  829. // Return true if the layers changed.
  830. bool RenderLayerBacking::updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip)
  831. {
  832. bool layersChanged = false;
  833. if (needsAncestorClip) {
  834. if (!m_ancestorClippingLayer) {
  835. m_ancestorClippingLayer = createGraphicsLayer("Ancestor clipping Layer");
  836. m_ancestorClippingLayer->setMasksToBounds(true);
  837. layersChanged = true;
  838. }
  839. } else if (m_ancestorClippingLayer) {
  840. willDestroyLayer(m_ancestorClippingLayer.get());
  841. m_ancestorClippingLayer->removeFromParent();
  842. m_ancestorClippingLayer = nullptr;
  843. layersChanged = true;
  844. }
  845. if (needsDescendantClip) {
  846. if (!m_childContainmentLayer && !m_usingTiledCacheLayer) {
  847. m_childContainmentLayer = createGraphicsLayer("Child clipping Layer");
  848. m_childContainmentLayer->setMasksToBounds(true);
  849. layersChanged = true;
  850. }
  851. } else if (hasClippingLayer()) {
  852. willDestroyLayer(m_childContainmentLayer.get());
  853. m_childContainmentLayer->removeFromParent();
  854. m_childContainmentLayer = nullptr;
  855. layersChanged = true;
  856. }
  857. return layersChanged;
  858. }
  859. void RenderLayerBacking::setBackgroundLayerPaintsFixedRootBackground(bool backgroundLayerPaintsFixedRootBackground)
  860. {
  861. m_backgroundLayerPaintsFixedRootBackground = backgroundLayerPaintsFixedRootBackground;
  862. }
  863. bool RenderLayerBacking::requiresHorizontalScrollbarLayer() const
  864. {
  865. if (!m_owningLayer->hasOverlayScrollbars() && !m_owningLayer->needsCompositedScrolling())
  866. return false;
  867. return m_owningLayer->horizontalScrollbar();
  868. }
  869. bool RenderLayerBacking::requiresVerticalScrollbarLayer() const
  870. {
  871. if (!m_owningLayer->hasOverlayScrollbars() && !m_owningLayer->needsCompositedScrolling())
  872. return false;
  873. return m_owningLayer->verticalScrollbar();
  874. }
  875. bool RenderLayerBacking::requiresScrollCornerLayer() const
  876. {
  877. if (!m_owningLayer->hasOverlayScrollbars() && !m_owningLayer->needsCompositedScrolling())
  878. return false;
  879. return !m_owningLayer->scrollCornerAndResizerRect().isEmpty();
  880. }
  881. bool RenderLayerBacking::updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer)
  882. {
  883. bool horizontalScrollbarLayerChanged = false;
  884. if (needsHorizontalScrollbarLayer) {
  885. if (!m_layerForHorizontalScrollbar) {
  886. m_layerForHorizontalScrollbar = createGraphicsLayer("horizontal scrollbar");
  887. horizontalScrollbarLayerChanged = true;
  888. }
  889. } else if (m_layerForHorizontalScrollbar) {
  890. willDestroyLayer(m_layerForHorizontalScrollbar.get());
  891. m_layerForHorizontalScrollbar = nullptr;
  892. horizontalScrollbarLayerChanged = true;
  893. }
  894. bool verticalScrollbarLayerChanged = false;
  895. if (needsVerticalScrollbarLayer) {
  896. if (!m_layerForVerticalScrollbar) {
  897. m_layerForVerticalScrollbar = createGraphicsLayer("vertical scrollbar");
  898. verticalScrollbarLayerChanged = true;
  899. }
  900. } else if (m_layerForVerticalScrollbar) {
  901. willDestroyLayer(m_layerForVerticalScrollbar.get());
  902. m_layerForVerticalScrollbar = nullptr;
  903. verticalScrollbarLayerChanged = true;
  904. }
  905. bool scrollCornerLayerChanged = false;
  906. if (needsScrollCornerLayer) {
  907. if (!m_layerForScrollCorner) {
  908. m_layerForScrollCorner = createGraphicsLayer("scroll corner");
  909. scrollCornerLayerChanged = true;
  910. }
  911. } else if (m_layerForScrollCorner) {
  912. willDestroyLayer(m_layerForScrollCorner.get());
  913. m_layerForScrollCorner = nullptr;
  914. scrollCornerLayerChanged = true;
  915. }
  916. if (ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer)) {
  917. if (horizontalScrollbarLayerChanged)
  918. scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_owningLayer, HorizontalScrollbar);
  919. if (verticalScrollbarLayerChanged)
  920. scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_owningLayer, VerticalScrollbar);
  921. }
  922. return horizontalScrollbarLayerChanged || verticalScrollbarLayerChanged || scrollCornerLayerChanged;
  923. }
  924. void RenderLayerBacking::positionOverflowControlsLayers(const IntSize& offsetFromRoot)
  925. {
  926. IntSize offsetFromRenderer = m_graphicsLayer->offsetFromRenderer();
  927. if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
  928. Scrollbar* hBar = m_owningLayer->horizontalScrollbar();
  929. if (hBar) {
  930. layer->setPosition(hBar->frameRect().location() - offsetFromRoot - offsetFromRenderer);
  931. layer->setSize(hBar->frameRect().size());
  932. if (layer->hasContentsLayer())
  933. layer->setContentsRect(IntRect(IntPoint(), hBar->frameRect().size()));
  934. }
  935. layer->setDrawsContent(hBar && !layer->hasContentsLayer());
  936. }
  937. if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
  938. Scrollbar* vBar = m_owningLayer->verticalScrollbar();
  939. if (vBar) {
  940. layer->setPosition(vBar->frameRect().location() - offsetFromRoot - offsetFromRenderer);
  941. layer->setSize(vBar->frameRect().size());
  942. if (layer->hasContentsLayer())
  943. layer->setContentsRect(IntRect(IntPoint(), vBar->frameRect().size()));
  944. }
  945. layer->setDrawsContent(vBar && !layer->hasContentsLayer());
  946. }
  947. if (GraphicsLayer* layer = layerForScrollCorner()) {
  948. const LayoutRect& scrollCornerAndResizer = m_owningLayer->scrollCornerAndResizerRect();
  949. layer->setPosition(scrollCornerAndResizer.location() - offsetFromRenderer);
  950. layer->setSize(scrollCornerAndResizer.size());
  951. layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
  952. }
  953. }
  954. bool RenderLayerBacking::hasUnpositionedOverflowControlsLayers() const
  955. {
  956. if (GraphicsLayer* layer = layerForHorizontalScrollbar())
  957. if (!layer->drawsContent())
  958. return true;
  959. if (GraphicsLayer* layer = layerForVerticalScrollbar())
  960. if (!layer->drawsContent())
  961. return true;
  962. if (GraphicsLayer* layer = layerForScrollCorner())
  963. if (!layer->drawsContent())
  964. return true;
  965. return false;
  966. }
  967. bool RenderLayerBacking::updateForegroundLayer(bool needsForegroundLayer)
  968. {
  969. bool layerChanged = false;
  970. if (needsForegroundLayer) {
  971. if (!m_foregroundLayer) {
  972. String layerName;
  973. #ifndef NDEBUG
  974. layerName = m_owningLayer->name() + " (foreground)";
  975. #endif
  976. m_foregroundLayer = createGraphicsLayer(layerName);
  977. m_foregroundLayer->setDrawsContent(true);
  978. m_foregroundLayer->setPaintingPhase(GraphicsLayerPaintForeground);
  979. layerChanged = true;
  980. }
  981. } else if (m_foregroundLayer) {
  982. willDestroyLayer(m_foregroundLayer.get());
  983. m_foregroundLayer->removeFromParent();
  984. m_foregroundLayer = nullptr;
  985. layerChanged = true;
  986. }
  987. if (layerChanged) {
  988. m_graphicsLayer->setNeedsDisplay();
  989. m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
  990. }
  991. return layerChanged;
  992. }
  993. bool RenderLayerBacking::updateBackgroundLayer(bool needsBackgroundLayer)
  994. {
  995. bool layerChanged = false;
  996. if (needsBackgroundLayer) {
  997. if (!m_backgroundLayer) {
  998. String layerName;
  999. #ifndef NDEBUG
  1000. layerName = m_owningLayer->name() + " (background)";
  1001. #endif
  1002. m_backgroundLayer = createGraphicsLayer(layerName);
  1003. m_backgroundLayer->setDrawsContent(true);
  1004. m_backgroundLayer->setAnchorPoint(FloatPoint3D());
  1005. m_backgroundLayer->setPaintingPhase(GraphicsLayerPaintBackground);
  1006. layerChanged = true;
  1007. }
  1008. if (!m_contentsContainmentLayer) {
  1009. String layerName;
  1010. #ifndef NDEBUG
  1011. layerName = m_owningLayer->name() + " (contents containment)";
  1012. #endif
  1013. m_contentsContainmentLayer = createGraphicsLayer(layerName);
  1014. m_contents