DrawingAreaImpl.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * Copyright (C) 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. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "DrawingAreaImpl.h"
  27. #include "DrawingAreaProxyMessages.h"
  28. #include "LayerTreeContext.h"
  29. #include "ShareableBitmap.h"
  30. #include "UpdateInfo.h"
  31. #include "WebPage.h"
  32. #include "WebPageCreationParameters.h"
  33. #include "WebProcess.h"
  34. #include <WebCore/GraphicsContext.h>
  35. #include <WebCore/Page.h>
  36. #include <WebCore/Settings.h>
  37. using namespace WebCore;
  38. namespace WebKit {
  39. PassOwnPtr<DrawingAreaImpl> DrawingAreaImpl::create(WebPage* webPage, const WebPageCreationParameters& parameters)
  40. {
  41. return adoptPtr(new DrawingAreaImpl(webPage, parameters));
  42. }
  43. DrawingAreaImpl::~DrawingAreaImpl()
  44. {
  45. if (m_layerTreeHost)
  46. m_layerTreeHost->invalidate();
  47. }
  48. DrawingAreaImpl::DrawingAreaImpl(WebPage* webPage, const WebPageCreationParameters& parameters)
  49. : DrawingArea(DrawingAreaTypeImpl, webPage)
  50. , m_backingStoreStateID(0)
  51. , m_isPaintingEnabled(true)
  52. , m_inUpdateBackingStoreState(false)
  53. , m_shouldSendDidUpdateBackingStoreState(false)
  54. , m_isWaitingForDidUpdate(false)
  55. , m_compositingAccordingToProxyMessages(false)
  56. , m_layerTreeStateIsFrozen(false)
  57. , m_wantsToExitAcceleratedCompositingMode(false)
  58. , m_isPaintingSuspended(!parameters.isVisible)
  59. , m_alwaysUseCompositing(false)
  60. , m_displayTimer(RunLoop::main(), this, &DrawingAreaImpl::displayTimerFired)
  61. , m_exitCompositingTimer(RunLoop::main(), this, &DrawingAreaImpl::exitAcceleratedCompositingMode)
  62. {
  63. if (webPage->corePage()->settings()->acceleratedDrawingEnabled() || webPage->corePage()->settings()->forceCompositingMode())
  64. m_alwaysUseCompositing = true;
  65. #if USE(COORDINATED_GRAPHICS)
  66. m_alwaysUseCompositing = true;
  67. #endif
  68. #if PLATFORM(MANX)
  69. // [Bug 65774]
  70. // We always use AC if enabled. At this point, the settings may not be updated by preferences
  71. // thus we refer to PreferencesStore here.
  72. if (parameters.store.getBoolValueForKey(WebPreferencesKey::acceleratedCompositingEnabledKey()))
  73. m_alwaysUseCompositing = true;
  74. else
  75. m_alwaysUseCompositing = false;
  76. #endif
  77. if (m_alwaysUseCompositing)
  78. enterAcceleratedCompositingMode(0);
  79. }
  80. void DrawingAreaImpl::setNeedsDisplay()
  81. {
  82. if (!m_isPaintingEnabled)
  83. return;
  84. if (m_layerTreeHost) {
  85. ASSERT(m_dirtyRegion.isEmpty());
  86. m_layerTreeHost->setNonCompositedContentsNeedDisplay();
  87. return;
  88. }
  89. setNeedsDisplayInRect(m_webPage->bounds());
  90. }
  91. void DrawingAreaImpl::setNeedsDisplayInRect(const IntRect& rect)
  92. {
  93. if (!m_isPaintingEnabled)
  94. return;
  95. if (m_layerTreeHost) {
  96. ASSERT(m_dirtyRegion.isEmpty());
  97. m_layerTreeHost->setNonCompositedContentsNeedDisplayInRect(rect);
  98. return;
  99. }
  100. IntRect dirtyRect = rect;
  101. dirtyRect.intersect(m_webPage->bounds());
  102. if (dirtyRect.isEmpty())
  103. return;
  104. if (m_webPage->mainFrameHasCustomRepresentation())
  105. return;
  106. m_dirtyRegion.unite(dirtyRect);
  107. scheduleDisplay();
  108. }
  109. void DrawingAreaImpl::scroll(const IntRect& scrollRect, const IntSize& scrollDelta)
  110. {
  111. if (!m_isPaintingEnabled)
  112. return;
  113. if (m_layerTreeHost) {
  114. ASSERT(m_scrollRect.isEmpty());
  115. ASSERT(m_scrollOffset.isEmpty());
  116. ASSERT(m_dirtyRegion.isEmpty());
  117. m_layerTreeHost->scrollNonCompositedContents(scrollRect);
  118. return;
  119. }
  120. if (m_webPage->mainFrameHasCustomRepresentation())
  121. return;
  122. if (scrollRect.isEmpty())
  123. return;
  124. if (!m_scrollRect.isEmpty() && scrollRect != m_scrollRect) {
  125. unsigned scrollArea = scrollRect.width() * scrollRect.height();
  126. unsigned currentScrollArea = m_scrollRect.width() * m_scrollRect.height();
  127. if (currentScrollArea >= scrollArea) {
  128. // The rect being scrolled is at least as large as the rect we'd like to scroll.
  129. // Go ahead and just invalidate the scroll rect.
  130. setNeedsDisplayInRect(scrollRect);
  131. return;
  132. }
  133. // Just repaint the entire current scroll rect, we'll scroll the new rect instead.
  134. setNeedsDisplayInRect(m_scrollRect);
  135. m_scrollRect = IntRect();
  136. m_scrollOffset = IntSize();
  137. }
  138. // Get the part of the dirty region that is in the scroll rect.
  139. Region dirtyRegionInScrollRect = intersect(scrollRect, m_dirtyRegion);
  140. if (!dirtyRegionInScrollRect.isEmpty()) {
  141. // There are parts of the dirty region that are inside the scroll rect.
  142. // We need to subtract them from the region, move them and re-add them.
  143. m_dirtyRegion.subtract(scrollRect);
  144. // Move the dirty parts.
  145. Region movedDirtyRegionInScrollRect = intersect(translate(dirtyRegionInScrollRect, scrollDelta), scrollRect);
  146. // And add them back.
  147. m_dirtyRegion.unite(movedDirtyRegionInScrollRect);
  148. }
  149. // Compute the scroll repaint region.
  150. Region scrollRepaintRegion = subtract(scrollRect, translate(scrollRect, scrollDelta));
  151. m_dirtyRegion.unite(scrollRepaintRegion);
  152. scheduleDisplay();
  153. m_scrollRect = scrollRect;
  154. m_scrollOffset += scrollDelta;
  155. }
  156. void DrawingAreaImpl::pageBackgroundTransparencyChanged()
  157. {
  158. if (m_layerTreeHost)
  159. m_layerTreeHost->pageBackgroundTransparencyChanged();
  160. }
  161. void DrawingAreaImpl::setLayerTreeStateIsFrozen(bool isFrozen)
  162. {
  163. if (m_layerTreeStateIsFrozen == isFrozen)
  164. return;
  165. m_layerTreeStateIsFrozen = isFrozen;
  166. if (m_layerTreeHost)
  167. m_layerTreeHost->setLayerFlushSchedulingEnabled(!isFrozen);
  168. if (isFrozen)
  169. m_exitCompositingTimer.stop();
  170. else if (m_wantsToExitAcceleratedCompositingMode)
  171. exitAcceleratedCompositingModeSoon();
  172. }
  173. void DrawingAreaImpl::forceRepaint()
  174. {
  175. setNeedsDisplay();
  176. m_webPage->layoutIfNeeded();
  177. if (m_layerTreeHost) {
  178. // FIXME: We need to do the same work as the layerHostDidFlushLayers function here,
  179. // but clearly it doesn't make sense to call the function with that name.
  180. // Consider refactoring and renaming it.
  181. if (m_compositingAccordingToProxyMessages)
  182. m_layerTreeHost->forceRepaint();
  183. else {
  184. // Call setShouldNotifyAfterNextScheduledLayerFlush(false) here to
  185. // prevent layerHostDidFlushLayers() from being called a second time.
  186. m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(false);
  187. layerHostDidFlushLayers();
  188. }
  189. return;
  190. }
  191. m_isWaitingForDidUpdate = false;
  192. display();
  193. }
  194. bool DrawingAreaImpl::forceRepaintAsync(uint64_t callbackID)
  195. {
  196. return m_layerTreeHost && m_layerTreeHost->forceRepaintAsync(callbackID);
  197. }
  198. void DrawingAreaImpl::didInstallPageOverlay(PageOverlay* pageOverlay)
  199. {
  200. if (m_layerTreeHost)
  201. m_layerTreeHost->didInstallPageOverlay(pageOverlay);
  202. }
  203. void DrawingAreaImpl::didUninstallPageOverlay(PageOverlay* pageOverlay)
  204. {
  205. if (m_layerTreeHost)
  206. m_layerTreeHost->didUninstallPageOverlay(pageOverlay);
  207. setNeedsDisplay();
  208. }
  209. void DrawingAreaImpl::setPageOverlayNeedsDisplay(PageOverlay* pageOverlay, const IntRect& rect)
  210. {
  211. if (m_layerTreeHost) {
  212. m_layerTreeHost->setPageOverlayNeedsDisplay(pageOverlay, rect);
  213. return;
  214. }
  215. setNeedsDisplayInRect(rect);
  216. }
  217. void DrawingAreaImpl::setPageOverlayOpacity(PageOverlay* pageOverlay, float value)
  218. {
  219. if (m_layerTreeHost)
  220. m_layerTreeHost->setPageOverlayOpacity(pageOverlay, value);
  221. }
  222. bool DrawingAreaImpl::pageOverlayShouldApplyFadeWhenPainting() const
  223. {
  224. if (m_layerTreeHost && !m_layerTreeHost->pageOverlayShouldApplyFadeWhenPainting())
  225. return false;
  226. return true;
  227. }
  228. void DrawingAreaImpl::pageCustomRepresentationChanged()
  229. {
  230. if (!m_alwaysUseCompositing)
  231. return;
  232. if (m_webPage->mainFrameHasCustomRepresentation()) {
  233. if (m_layerTreeHost)
  234. exitAcceleratedCompositingMode();
  235. } else if (!m_layerTreeHost)
  236. enterAcceleratedCompositingMode(0);
  237. }
  238. void DrawingAreaImpl::setPaintingEnabled(bool paintingEnabled)
  239. {
  240. m_isPaintingEnabled = paintingEnabled;
  241. }
  242. void DrawingAreaImpl::updatePreferences(const WebPreferencesStore& store)
  243. {
  244. #if PLATFORM(MAC)
  245. // Soon we want pages with fixed positioned elements to be able to be scrolled by the ScrollingCoordinator.
  246. // As a part of that work, we have to composite fixed position elements, and we have to allow those
  247. // elements to create a stacking context.
  248. m_webPage->corePage()->settings()->setAcceleratedCompositingForFixedPositionEnabled(true);
  249. m_webPage->corePage()->settings()->setFixedPositionCreatesStackingContext(true);
  250. // <rdar://problem/10697417>: It is necessary to force compositing when accelerate drawing
  251. // is enabled on Mac so that scrollbars are always in their own layers.
  252. if (m_webPage->corePage()->settings()->acceleratedDrawingEnabled())
  253. m_webPage->corePage()->settings()->setForceCompositingMode(LayerTreeHost::supportsAcceleratedCompositing());
  254. else
  255. #elif PLATFORM(MANX) && USE(COORDINATED_GRAPHICS) && ENABLE(MANX_AC_PROCESS)
  256. if (m_webPage->corePage()->settings()->acceleratedCompositingEnabled())
  257. m_webPage->corePage()->settings()->setForceCompositingMode(LayerTreeHost::supportsAcceleratedCompositing());
  258. else
  259. #endif
  260. m_webPage->corePage()->settings()->setForceCompositingMode(store.getBoolValueForKey(WebPreferencesKey::forceCompositingModeKey()) && LayerTreeHost::supportsAcceleratedCompositing());
  261. }
  262. void DrawingAreaImpl::layerHostDidFlushLayers()
  263. {
  264. ASSERT(m_layerTreeHost);
  265. m_layerTreeHost->forceRepaint();
  266. if (m_shouldSendDidUpdateBackingStoreState && !exitAcceleratedCompositingModePending()) {
  267. sendDidUpdateBackingStoreState();
  268. return;
  269. }
  270. if (!m_layerTreeHost)
  271. return;
  272. #if USE(ACCELERATED_COMPOSITING)
  273. ASSERT(!m_compositingAccordingToProxyMessages);
  274. if (!exitAcceleratedCompositingModePending()) {
  275. m_webPage->send(Messages::DrawingAreaProxy::EnterAcceleratedCompositingMode(m_backingStoreStateID, m_layerTreeHost->layerTreeContext()));
  276. m_compositingAccordingToProxyMessages = true;
  277. }
  278. #endif
  279. }
  280. #if USE(ACCELERATED_COMPOSITING)
  281. GraphicsLayerFactory* DrawingAreaImpl::graphicsLayerFactory()
  282. {
  283. if (m_layerTreeHost)
  284. return m_layerTreeHost->graphicsLayerFactory();
  285. return 0;
  286. }
  287. void DrawingAreaImpl::setRootCompositingLayer(GraphicsLayer* graphicsLayer)
  288. {
  289. // FIXME: Instead of using nested if statements, we should keep a compositing state
  290. // enum in the DrawingAreaImpl object and have a changeAcceleratedCompositingState function
  291. // that takes the new state.
  292. if (graphicsLayer) {
  293. if (!m_layerTreeHost) {
  294. // We're actually entering accelerated compositing mode.
  295. enterAcceleratedCompositingMode(graphicsLayer);
  296. } else {
  297. // We're already in accelerated compositing mode, but the root compositing layer changed.
  298. m_exitCompositingTimer.stop();
  299. m_wantsToExitAcceleratedCompositingMode = false;
  300. // If we haven't sent the EnterAcceleratedCompositingMode message, make sure that the
  301. // layer tree host calls us back after the next layer flush so we can send it then.
  302. if (!m_compositingAccordingToProxyMessages)
  303. m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(true);
  304. m_layerTreeHost->setRootCompositingLayer(graphicsLayer);
  305. }
  306. } else {
  307. if (m_layerTreeHost) {
  308. m_layerTreeHost->setRootCompositingLayer(0);
  309. if (!m_alwaysUseCompositing) {
  310. // We'll exit accelerated compositing mode on a timer, to avoid re-entering
  311. // compositing code via display() and layout.
  312. // If we're leaving compositing mode because of a setSize, it is safe to
  313. // exit accelerated compositing mode right away.
  314. if (m_inUpdateBackingStoreState)
  315. exitAcceleratedCompositingMode();
  316. else
  317. exitAcceleratedCompositingModeSoon();
  318. }
  319. }
  320. }
  321. }
  322. void DrawingAreaImpl::scheduleCompositingLayerFlush()
  323. {
  324. if (!m_layerTreeHost)
  325. return;
  326. m_layerTreeHost->scheduleLayerFlush();
  327. }
  328. #endif
  329. void DrawingAreaImpl::updateBackingStoreState(uint64_t stateID, bool respondImmediately, float deviceScaleFactor, const WebCore::IntSize& size, const WebCore::IntSize& scrollOffset)
  330. {
  331. ASSERT(!m_inUpdateBackingStoreState);
  332. m_inUpdateBackingStoreState = true;
  333. ASSERT_ARG(stateID, stateID >= m_backingStoreStateID);
  334. if (stateID != m_backingStoreStateID) {
  335. m_backingStoreStateID = stateID;
  336. m_shouldSendDidUpdateBackingStoreState = true;
  337. m_webPage->setDeviceScaleFactor(deviceScaleFactor);
  338. m_webPage->setSize(size);
  339. m_webPage->layoutIfNeeded();
  340. m_webPage->scrollMainFrameIfNotAtMaxScrollPosition(scrollOffset);
  341. if (m_layerTreeHost) {
  342. #if USE(COORDINATED_GRAPHICS)
  343. // Coordinated Graphics sets the size of the root layer to contents size.
  344. if (!m_webPage->useFixedLayout())
  345. #endif
  346. m_layerTreeHost->sizeDidChange(m_webPage->size());
  347. } else
  348. m_dirtyRegion = m_webPage->bounds();
  349. } else {
  350. ASSERT(size == m_webPage->size());
  351. if (!m_shouldSendDidUpdateBackingStoreState) {
  352. // We've already sent a DidUpdateBackingStoreState message for this state. We have nothing more to do.
  353. m_inUpdateBackingStoreState = false;
  354. return;
  355. }
  356. }
  357. // The UI process has updated to a new backing store state. Any Update messages we sent before
  358. // this point will be ignored. We wait to set this to false until after updating the page's
  359. // size so that any displays triggered by the relayout will be ignored. If we're supposed to
  360. // respond to the UpdateBackingStoreState message immediately, we'll do a display anyway in
  361. // sendDidUpdateBackingStoreState; otherwise we shouldn't do one right now.
  362. m_isWaitingForDidUpdate = false;
  363. if (respondImmediately) {
  364. // Make sure to resume painting if we're supposed to respond immediately, otherwise we'll just
  365. // send back an empty UpdateInfo struct.
  366. if (m_isPaintingSuspended)
  367. resumePainting();
  368. sendDidUpdateBackingStoreState();
  369. }
  370. m_inUpdateBackingStoreState = false;
  371. }
  372. void DrawingAreaImpl::sendDidUpdateBackingStoreState()
  373. {
  374. ASSERT(!m_isWaitingForDidUpdate);
  375. ASSERT(m_shouldSendDidUpdateBackingStoreState);
  376. m_shouldSendDidUpdateBackingStoreState = false;
  377. UpdateInfo updateInfo;
  378. if (!m_isPaintingSuspended && !m_layerTreeHost)
  379. display(updateInfo);
  380. LayerTreeContext layerTreeContext;
  381. if (m_isPaintingSuspended || m_layerTreeHost) {
  382. updateInfo.viewSize = m_webPage->size();
  383. updateInfo.deviceScaleFactor = m_webPage->corePage()->deviceScaleFactor();
  384. if (m_layerTreeHost) {
  385. layerTreeContext = m_layerTreeHost->layerTreeContext();
  386. // We don't want the layer tree host to notify after the next scheduled
  387. // layer flush because that might end up sending an EnterAcceleratedCompositingMode
  388. // message back to the UI process, but the updated layer tree context
  389. // will be sent back in the DidUpdateBackingStoreState message.
  390. m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(false);
  391. m_layerTreeHost->forceRepaint();
  392. }
  393. }
  394. m_webPage->send(Messages::DrawingAreaProxy::DidUpdateBackingStoreState(m_backingStoreStateID, updateInfo, layerTreeContext));
  395. m_compositingAccordingToProxyMessages = !layerTreeContext.isEmpty();
  396. }
  397. void DrawingAreaImpl::didUpdate()
  398. {
  399. // We might get didUpdate messages from the UI process even after we've
  400. // entered accelerated compositing mode. Ignore them.
  401. if (m_layerTreeHost)
  402. return;
  403. m_isWaitingForDidUpdate = false;
  404. // Display if needed. We call displayTimerFired here since it will throttle updates to 60fps.
  405. displayTimerFired();
  406. }
  407. void DrawingAreaImpl::suspendPainting()
  408. {
  409. ASSERT(!m_isPaintingSuspended);
  410. if (m_layerTreeHost)
  411. m_layerTreeHost->pauseRendering();
  412. m_isPaintingSuspended = true;
  413. m_displayTimer.stop();
  414. m_webPage->corePage()->suspendScriptedAnimations();
  415. }
  416. void DrawingAreaImpl::resumePainting()
  417. {
  418. if (!m_isPaintingSuspended) {
  419. // FIXME: We can get a call to resumePainting when painting is not suspended.
  420. // This happens when sending a synchronous message to create a new page. See <rdar://problem/8976531>.
  421. return;
  422. }
  423. if (m_layerTreeHost)
  424. m_layerTreeHost->resumeRendering();
  425. m_isPaintingSuspended = false;
  426. // FIXME: We shouldn't always repaint everything here.
  427. setNeedsDisplay();
  428. #if PLATFORM(MAC)
  429. if (m_webPage->windowIsVisible())
  430. m_webPage->corePage()->resumeScriptedAnimations();
  431. #else
  432. m_webPage->corePage()->resumeScriptedAnimations();
  433. #endif
  434. }
  435. void DrawingAreaImpl::enterAcceleratedCompositingMode(GraphicsLayer* graphicsLayer)
  436. {
  437. m_exitCompositingTimer.stop();
  438. m_wantsToExitAcceleratedCompositingMode = false;
  439. ASSERT(!m_layerTreeHost);
  440. m_layerTreeHost = LayerTreeHost::create(m_webPage);
  441. if (!m_inUpdateBackingStoreState)
  442. m_layerTreeHost->setShouldNotifyAfterNextScheduledLayerFlush(true);
  443. m_layerTreeHost->setRootCompositingLayer(graphicsLayer);
  444. // Non-composited content will now be handled exclusively by the layer tree host.
  445. m_dirtyRegion = Region();
  446. m_scrollRect = IntRect();
  447. m_scrollOffset = IntSize();
  448. m_displayTimer.stop();
  449. m_isWaitingForDidUpdate = false;
  450. }
  451. void DrawingAreaImpl::exitAcceleratedCompositingMode()
  452. {
  453. if (m_alwaysUseCompositing && !m_webPage->mainFrameHasCustomRepresentation())
  454. return;
  455. ASSERT(!m_layerTreeStateIsFrozen);
  456. m_exitCompositingTimer.stop();
  457. m_wantsToExitAcceleratedCompositingMode = false;
  458. ASSERT(m_layerTreeHost);
  459. m_layerTreeHost->invalidate();
  460. m_layerTreeHost = nullptr;
  461. m_dirtyRegion = m_webPage->bounds();
  462. if (m_inUpdateBackingStoreState)
  463. return;
  464. if (m_shouldSendDidUpdateBackingStoreState) {
  465. sendDidUpdateBackingStoreState();
  466. return;
  467. }
  468. UpdateInfo updateInfo;
  469. if (m_isPaintingSuspended) {
  470. updateInfo.viewSize = m_webPage->size();
  471. updateInfo.deviceScaleFactor = m_webPage->corePage()->deviceScaleFactor();
  472. } else
  473. display(updateInfo);
  474. #if USE(ACCELERATED_COMPOSITING)
  475. // Send along a complete update of the page so we can paint the contents right after we exit the
  476. // accelerated compositing mode, eliminiating flicker.
  477. if (m_compositingAccordingToProxyMessages) {
  478. m_webPage->send(Messages::DrawingAreaProxy::ExitAcceleratedCompositingMode(m_backingStoreStateID, updateInfo));
  479. m_compositingAccordingToProxyMessages = false;
  480. } else {
  481. // If we left accelerated compositing mode before we sent an EnterAcceleratedCompositingMode message to the
  482. // UI process, we still need to let it know about the new contents, so send an Update message.
  483. m_webPage->send(Messages::DrawingAreaProxy::Update(m_backingStoreStateID, updateInfo));
  484. }
  485. #endif
  486. }
  487. void DrawingAreaImpl::exitAcceleratedCompositingModeSoon()
  488. {
  489. if (m_layerTreeStateIsFrozen) {
  490. m_wantsToExitAcceleratedCompositingMode = true;
  491. return;
  492. }
  493. if (exitAcceleratedCompositingModePending())
  494. return;
  495. m_exitCompositingTimer.startOneShot(0);
  496. }
  497. void DrawingAreaImpl::scheduleDisplay()
  498. {
  499. ASSERT(!m_layerTreeHost);
  500. if (m_isWaitingForDidUpdate)
  501. return;
  502. if (m_isPaintingSuspended)
  503. return;
  504. if (m_dirtyRegion.isEmpty())
  505. return;
  506. if (m_displayTimer.isActive())
  507. return;
  508. m_displayTimer.startOneShot(0);
  509. }
  510. void DrawingAreaImpl::displayTimerFired()
  511. {
  512. display();
  513. }
  514. void DrawingAreaImpl::display()
  515. {
  516. ASSERT(!m_layerTreeHost);
  517. ASSERT(!m_isWaitingForDidUpdate);
  518. ASSERT(!m_inUpdateBackingStoreState);
  519. if (m_isPaintingSuspended)
  520. return;
  521. if (m_dirtyRegion.isEmpty())
  522. return;
  523. if (m_shouldSendDidUpdateBackingStoreState) {
  524. sendDidUpdateBackingStoreState();
  525. return;
  526. }
  527. UpdateInfo updateInfo;
  528. display(updateInfo);
  529. if (m_layerTreeHost) {
  530. // The call to update caused layout which turned on accelerated compositing.
  531. // Don't send an Update message in this case.
  532. return;
  533. }
  534. m_webPage->send(Messages::DrawingAreaProxy::Update(m_backingStoreStateID, updateInfo));
  535. m_isWaitingForDidUpdate = true;
  536. }
  537. static bool shouldPaintBoundsRect(const IntRect& bounds, const Vector<IntRect>& rects)
  538. {
  539. const size_t rectThreshold = 10;
  540. const double wastedSpaceThreshold = 0.75;
  541. if (rects.size() <= 1 || rects.size() > rectThreshold)
  542. return true;
  543. // Attempt to guess whether or not we should use the region bounds rect or the individual rects.
  544. // We do this by computing the percentage of "wasted space" in the bounds. If that wasted space
  545. // is too large, then we will do individual rect painting instead.
  546. unsigned boundsArea = bounds.width() * bounds.height();
  547. unsigned rectsArea = 0;
  548. for (size_t i = 0; i < rects.size(); ++i)
  549. rectsArea += rects[i].width() * rects[i].height();
  550. double wastedSpace = 1 - (static_cast<double>(rectsArea) / boundsArea);
  551. return wastedSpace <= wastedSpaceThreshold;
  552. }
  553. void DrawingAreaImpl::display(UpdateInfo& updateInfo)
  554. {
  555. ASSERT(!m_isPaintingSuspended);
  556. ASSERT(!m_layerTreeHost);
  557. ASSERT(!m_webPage->size().isEmpty());
  558. // FIXME: It would be better if we could avoid painting altogether when there is a custom representation.
  559. if (m_webPage->mainFrameHasCustomRepresentation()) {
  560. // ASSUMPTION: the custom representation will be painting the dirty region for us.
  561. m_dirtyRegion = Region();
  562. return;
  563. }
  564. m_webPage->layoutIfNeeded();
  565. // The layout may have put the page into accelerated compositing mode. If the LayerTreeHost is
  566. // in charge of displaying, we have nothing more to do.
  567. if (m_layerTreeHost)
  568. return;
  569. updateInfo.viewSize = m_webPage->size();
  570. updateInfo.deviceScaleFactor = m_webPage->corePage()->deviceScaleFactor();
  571. IntRect bounds = m_dirtyRegion.bounds();
  572. ASSERT(m_webPage->bounds().contains(bounds));
  573. IntSize bitmapSize = bounds.size();
  574. float deviceScaleFactor = m_webPage->corePage()->deviceScaleFactor();
  575. bitmapSize.scale(deviceScaleFactor);
  576. RefPtr<ShareableBitmap> bitmap = ShareableBitmap::createShareable(bitmapSize, ShareableBitmap::SupportsAlpha);
  577. if (!bitmap)
  578. return;
  579. if (!bitmap->createHandle(updateInfo.bitmapHandle))
  580. return;
  581. Vector<IntRect> rects = m_dirtyRegion.rects();
  582. if (shouldPaintBoundsRect(bounds, rects)) {
  583. rects.clear();
  584. rects.append(bounds);
  585. }
  586. updateInfo.scrollRect = m_scrollRect;
  587. updateInfo.scrollOffset = m_scrollOffset;
  588. m_dirtyRegion = Region();
  589. m_scrollRect = IntRect();
  590. m_scrollOffset = IntSize();
  591. OwnPtr<GraphicsContext> graphicsContext = bitmap->createGraphicsContext();
  592. graphicsContext->applyDeviceScaleFactor(deviceScaleFactor);
  593. updateInfo.updateRectBounds = bounds;
  594. graphicsContext->translate(-bounds.x(), -bounds.y());
  595. for (size_t i = 0; i < rects.size(); ++i) {
  596. m_webPage->drawRect(*graphicsContext, rects[i]);
  597. if (m_webPage->hasPageOverlay()) {
  598. PageOverlayList& pageOverlays = m_webPage->pageOverlays();
  599. PageOverlayList::iterator end = pageOverlays.end();
  600. for (PageOverlayList::iterator it = pageOverlays.begin(); it != end; ++it)
  601. m_webPage->drawPageOverlay(it->get(), *graphicsContext, rects[i]);
  602. }
  603. updateInfo.updateRects.append(rects[i]);
  604. }
  605. // Layout can trigger more calls to setNeedsDisplay and we don't want to process them
  606. // until the UI process has painted the update, so we stop the timer here.
  607. m_displayTimer.stop();
  608. }
  609. #if USE(COORDINATED_GRAPHICS)
  610. void DrawingAreaImpl::didReceiveCoordinatedLayerTreeHostMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder)
  611. {
  612. if (m_layerTreeHost)
  613. m_layerTreeHost->didReceiveCoordinatedLayerTreeHostMessage(connection, decoder);
  614. }
  615. #endif
  616. #if PLATFORM(MAC)
  617. void DrawingAreaImpl::setLayerHostingMode(uint32_t opaqueLayerHostingMode)
  618. {
  619. LayerHostingMode layerHostingMode = static_cast<LayerHostingMode>(opaqueLayerHostingMode);
  620. m_webPage->setLayerHostingMode(layerHostingMode);
  621. if (!m_layerTreeHost)
  622. return;
  623. LayerTreeContext oldLayerTreeContext = m_layerTreeHost->layerTreeContext();
  624. m_layerTreeHost->setLayerHostingMode(layerHostingMode);
  625. if (m_layerTreeHost->layerTreeContext() != oldLayerTreeContext)
  626. m_webPage->send(Messages::DrawingAreaProxy::UpdateAcceleratedCompositingMode(m_backingStoreStateID, m_layerTreeHost->layerTreeContext()));
  627. }
  628. #endif
  629. #if USE(ACCELERATED_COMPOSITING) && PLATFORM(MANX)
  630. void DrawingAreaImpl::updateAcceleratedCompositingMode()
  631. {
  632. if (exitAcceleratedCompositingModePending())
  633. return;
  634. if (!m_layerTreeHost)
  635. return;
  636. if (m_isWaitingForDidUpdate)
  637. return;
  638. if (m_shouldSendDidUpdateBackingStoreState)
  639. sendDidUpdateBackingStoreState();
  640. m_webPage->send(Messages::DrawingAreaProxy::UpdateAcceleratedCompositingMode(m_backingStoreStateID, m_layerTreeHost->layerTreeContext()));
  641. }
  642. #endif
  643. } // namespace WebKit