AcceleratedCompositingContext.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2012 Igalia, S.L.
  3. * Copyright (C) 2014 Sony Computer Entertainment Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "config.h"
  20. #include "AcceleratedCompositingContext.h"
  21. #if USE(ACCELERATED_COMPOSITING) && USE(ACAGI)
  22. #include "AcagiCompositor.h"
  23. #include "AcagiLayer.h"
  24. #include "CairoUtilities.h"
  25. #include "Chrome.h"
  26. #include "ChromeClientManx.h"
  27. #include "Frame.h"
  28. #include "FrameView.h"
  29. #include "GraphicsLayerAcagi.h"
  30. #include "Page.h"
  31. #include "PlatformContextCairo.h"
  32. #include "Settings.h"
  33. #include "WebViewPrivate.h"
  34. #include <wtf/CurrentTime.h>
  35. #define DBG_TRACE printf("@@@ %s %d\n", __PRETTY_FUNCTION__, __LINE__)
  36. // #define DBG_TRACE
  37. using namespace WebCore;
  38. namespace WebKit {
  39. AcceleratedCompositingContext::AcceleratedCompositingContext(WebViewPrivate* webViewPrivate)
  40. : m_webViewPrivate(webViewPrivate)
  41. , m_layerFlushTimer(this, &AcceleratedCompositingContext::layerFlushTimerFired)
  42. , m_contentsScale(1)
  43. {
  44. DBG_TRACE;
  45. }
  46. void AcceleratedCompositingContext::initialize()
  47. {
  48. DBG_TRACE;
  49. if (m_rootLayer)
  50. return;
  51. IntSize pageSize = m_webViewPrivate->m_viewSize;
  52. m_rootLayer = GraphicsLayer::create(0, this);
  53. m_rootLayer->setDrawsContent(false);
  54. m_rootLayer->setSize(pageSize);
  55. // The non-composited contents are a child of the root layer.
  56. m_nonCompositedContentLayer = GraphicsLayer::create(0, this);
  57. m_nonCompositedContentLayer->setDrawsContent(true);
  58. m_nonCompositedContentLayer->setContentsOpaque(true);
  59. m_nonCompositedContentLayer->setSize(pageSize);
  60. if (corePage(m_webViewPrivate)->settings()->acceleratedDrawingEnabled())
  61. m_nonCompositedContentLayer->setAcceleratesDrawing(true);
  62. #ifndef NDEBUG
  63. m_rootLayer->setName("Root layer");
  64. m_nonCompositedContentLayer->setName("Non-composited content");
  65. #endif
  66. m_rootLayer->addChild(m_nonCompositedContentLayer.get());
  67. m_nonCompositedContentLayer->setNeedsDisplayInRect(IntRect(IntPoint(), pageSize));
  68. m_commandBuffer = SigreCommandBuffer::create(m_webViewPrivate->m_pool, m_webViewPrivate->m_painter);
  69. m_compositor = AcagiCompositor::create(m_commandBuffer.get());
  70. toAcagiLayer(m_rootLayer.get())->setCompositor(m_compositor.get());
  71. ((GraphicsLayerAcagi*)m_nonCompositedContentLayer.get())->setActualVisibleRect(IntRect(IntPoint::zero(), pageSize));
  72. scheduleLayerFlush();
  73. }
  74. AcceleratedCompositingContext::~AcceleratedCompositingContext()
  75. {
  76. stopAnyPendingLayerFlush();
  77. }
  78. void AcceleratedCompositingContext::stopAnyPendingLayerFlush()
  79. {
  80. DBG_TRACE;
  81. m_layerFlushTimer.stop();
  82. }
  83. bool AcceleratedCompositingContext::enabled()
  84. {
  85. return m_rootLayer && m_compositor && m_commandBuffer;
  86. }
  87. void AcceleratedCompositingContext::patchAndSubmitDrawCommands(const WebCore::IntRect& clipRect, WebCore::SigreCommandBuffer::TargetSurface* targetSurface)
  88. {
  89. if (!enabled())
  90. return;
  91. m_commandBuffer->patchAndSubmitDrawCommands(clipRect, targetSurface);
  92. }
  93. void AcceleratedCompositingContext::compositeLayersToContext(CompositePurpose purpose)
  94. {
  95. m_compositor->beginPainting();
  96. toAcagiLayer(m_rootLayer.get())->paint();
  97. m_compositor->endPainting();
  98. }
  99. void AcceleratedCompositingContext::clearEverywhere()
  100. {
  101. DBG_TRACE;
  102. if (m_commandBuffer)
  103. m_commandBuffer->clearCommands();
  104. }
  105. void AcceleratedCompositingContext::setRootCompositingLayer(GraphicsLayer* graphicsLayer)
  106. {
  107. DBG_TRACE;
  108. // Clearing everywhere when turning on or off the layer tree prevents us from flashing
  109. // old content before the first flush.
  110. clearEverywhere();
  111. if (!graphicsLayer) {
  112. stopAnyPendingLayerFlush();
  113. m_rootLayer = nullptr;
  114. m_nonCompositedContentLayer = nullptr;
  115. m_compositor = nullptr;
  116. return;
  117. }
  118. // Add the accelerated layer tree hierarchy.
  119. initialize();
  120. m_nonCompositedContentLayer->removeAllChildren();
  121. m_nonCompositedContentLayer->addChild(graphicsLayer);
  122. stopAnyPendingLayerFlush();
  123. scheduleLayerFlush();
  124. }
  125. void AcceleratedCompositingContext::setNonCompositedContentsNeedDisplay(const IntRect& rect)
  126. {
  127. DBG_TRACE;
  128. if (!m_rootLayer)
  129. return;
  130. if (rect.isEmpty()) {
  131. m_rootLayer->setNeedsDisplay();
  132. return;
  133. }
  134. m_nonCompositedContentLayer->setNeedsDisplayInRect(rect);
  135. scheduleLayerFlush();
  136. }
  137. void AcceleratedCompositingContext::setActualVisibleRect(const WebCore::IntRect& rect)
  138. {
  139. DBG_TRACE;
  140. ASSERT(!rect.isEmpty());
  141. ((GraphicsLayerAcagi*)m_nonCompositedContentLayer.get())->setActualVisibleRect(rect);
  142. m_contentsScale = (float)m_webViewPrivate->m_viewSize.height() / rect.height();
  143. m_compositor->setContentsScale(m_contentsScale);
  144. scheduleLayerFlush();
  145. }
  146. void AcceleratedCompositingContext::resizeRootLayer(const IntSize& newSize)
  147. {
  148. DBG_TRACE;
  149. if (!enabled())
  150. return;
  151. if (m_rootLayer->size() == newSize)
  152. return;
  153. m_rootLayer->setSize(newSize);
  154. // If the newSize exposes new areas of the non-composited content a setNeedsDisplay is needed
  155. // for those newly exposed areas.
  156. FloatSize oldSize = m_nonCompositedContentLayer->size();
  157. m_nonCompositedContentLayer->setSize(newSize);
  158. if (newSize.width() > oldSize.width()) {
  159. float height = std::min(static_cast<float>(newSize.height()), oldSize.height());
  160. m_nonCompositedContentLayer->setNeedsDisplayInRect(FloatRect(oldSize.width(), 0, newSize.width() - oldSize.width(), height));
  161. }
  162. if (newSize.height() > oldSize.height())
  163. m_nonCompositedContentLayer->setNeedsDisplayInRect(FloatRect(0, oldSize.height(), newSize.width(), newSize.height() - oldSize.height()));
  164. m_nonCompositedContentLayer->setNeedsDisplayInRect(IntRect(IntPoint(), newSize));
  165. compositeLayersToContext(ForResize);
  166. scheduleLayerFlush();
  167. }
  168. void AcceleratedCompositingContext::scheduleLayerFlush()
  169. {
  170. DBG_TRACE;
  171. if (!enabled())
  172. return;
  173. if (!m_layerFlushTimer.isActive())
  174. m_layerFlushTimer.startOneShot(0);
  175. }
  176. bool AcceleratedCompositingContext::flushPendingLayerChanges()
  177. {
  178. toAcagiLayer(m_rootLayer.get())->computeTransformsRecursive();
  179. m_rootLayer->flushCompositingStateForThisLayerOnly();
  180. m_nonCompositedContentLayer->flushCompositingStateForThisLayerOnly();
  181. return corePage(m_webViewPrivate)->mainFrame()->view()->flushCompositingStateIncludingSubframes();
  182. }
  183. void AcceleratedCompositingContext::flushAndRenderLayers()
  184. {
  185. if (!enabled())
  186. return;
  187. Frame* frame = corePage(m_webViewPrivate)->mainFrame();
  188. if (!frame || !frame->contentRenderer() || !frame->view())
  189. return;
  190. frame->view()->updateLayoutAndStyleIfNeededRecursive();
  191. if (!enabled())
  192. return;
  193. if (!flushPendingLayerChanges())
  194. return;
  195. compositeLayersToContext();
  196. // Notify client
  197. m_webViewPrivate->client().didUpdateDrawing(0, 0);
  198. }
  199. void AcceleratedCompositingContext::layerFlushTimerFired(Timer<AcceleratedCompositingContext>*)
  200. {
  201. layerFlushTimerFired();
  202. // m_rootLayer can be destroyed in layerFlushTimerFired() thus we need to check at this point.
  203. if (!enabled())
  204. return;
  205. if (toAcagiLayer(m_rootLayer.get())->descendantsOrSelfHaveRunningAnimations() && !m_layerFlushTimer.isActive())
  206. m_layerFlushTimer.startOneShot(1.0 / 60.0);
  207. }
  208. void AcceleratedCompositingContext::layerFlushTimerFired()
  209. {
  210. flushAndRenderLayers();
  211. }
  212. void AcceleratedCompositingContext::notifyAnimationStarted(const GraphicsLayer*, double time)
  213. {
  214. DBG_TRACE;
  215. }
  216. void AcceleratedCompositingContext::notifyFlushRequired(const GraphicsLayer*)
  217. {
  218. DBG_TRACE;
  219. }
  220. void AcceleratedCompositingContext::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase, const IntRect& rectToPaint)
  221. {
  222. if (graphicsLayer != m_nonCompositedContentLayer)
  223. return;
  224. WebCore::FrameView* view = corePage(m_webViewPrivate)->mainFrame()->view();
  225. bool paintsEntireContents = view->paintsEntireContents();
  226. bool scrollbarsSuppressed = view->scrollbarsSuppressed();
  227. view->setPaintsEntireContents(true);
  228. view->setScrollbarsSuppressed(true, false);
  229. context.save();
  230. context.clip(rectToPaint);
  231. view->paint(&context, rectToPaint);
  232. context.restore();
  233. view->setPaintsEntireContents(paintsEntireContents);
  234. view->setScrollbarsSuppressed(scrollbarsSuppressed, false);
  235. }
  236. } // namespace WebKit
  237. #endif // USE(ACCELERATED_COMPOSITING) && USE(ACAGI)