[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "config.h" |
| 6 | |
| 7 | #include "CCLayerTreeHostImpl.h" |
| 8 | |
[email protected] | 8922820 | 2012-08-29 03:20:30 | [diff] [blame] | 9 | #include "CCAppendQuadsData.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 10 | #include "CCDamageTracker.h" |
| 11 | #include "CCDebugRectHistory.h" |
| 12 | #include "CCDelayBasedTimeSource.h" |
| 13 | #include "CCFontAtlas.h" |
| 14 | #include "CCFrameRateCounter.h" |
| 15 | #include "CCHeadsUpDisplayLayerImpl.h" |
| 16 | #include "CCLayerIterator.h" |
| 17 | #include "CCLayerTreeHost.h" |
| 18 | #include "CCLayerTreeHostCommon.h" |
[email protected] | 1c0088f | 2012-10-17 00:29:30 | [diff] [blame] | 19 | #include "CCMathUtil.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 20 | #include "CCOverdrawMetrics.h" |
| 21 | #include "CCPageScaleAnimation.h" |
| 22 | #include "CCPrioritizedTextureManager.h" |
| 23 | #include "CCRenderPassDrawQuad.h" |
[email protected] | 1c0088f | 2012-10-17 00:29:30 | [diff] [blame] | 24 | #include "CCRendererGL.h" |
| 25 | #include "CCRendererSoftware.h" |
| 26 | #include "CCRenderingStats.h" |
| 27 | #include "CCScrollbarAnimationController.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 28 | #include "CCScrollbarLayerImpl.h" |
| 29 | #include "CCSettings.h" |
| 30 | #include "CCSingleThreadProxy.h" |
[email protected] | 6331a117 | 2012-10-18 11:35:13 | [diff] [blame] | 31 | #include "base/debug/trace_event.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 32 | #include "base/basictypes.h" |
| 33 | #include "cc/texture_uploader.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 34 | #include <wtf/CurrentTime.h> |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 35 | #include <algorithm> |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 36 | |
| 37 | using WebKit::WebTransformationMatrix; |
| 38 | |
| 39 | namespace { |
| 40 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 41 | void didVisibilityChange(cc::CCLayerTreeHostImpl* id, bool visible) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 42 | { |
| 43 | if (visible) { |
| 44 | TRACE_EVENT_ASYNC_BEGIN1("webkit", "CCLayerTreeHostImpl::setVisible", id, "CCLayerTreeHostImpl", id); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | TRACE_EVENT_ASYNC_END0("webkit", "CCLayerTreeHostImpl::setVisible", id); |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 53 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 54 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 55 | CCPinchZoomViewport::CCPinchZoomViewport() |
| 56 | : m_pageScaleFactor(1) |
| 57 | , m_pageScaleDelta(1) |
| 58 | , m_sentPageScaleDelta(1) |
| 59 | , m_minPageScaleFactor(0) |
| 60 | , m_maxPageScaleFactor(0) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | float CCPinchZoomViewport::totalPageScaleFactor() const |
| 65 | { |
| 66 | return m_pageScaleFactor * m_pageScaleDelta; |
| 67 | } |
| 68 | |
| 69 | void CCPinchZoomViewport::setPageScaleDelta(float delta) |
| 70 | { |
| 71 | // Clamp to the current min/max limits. |
| 72 | float totalPageScaleFactor = m_pageScaleFactor * delta; |
| 73 | if (m_minPageScaleFactor && totalPageScaleFactor < m_minPageScaleFactor) |
| 74 | delta = m_minPageScaleFactor / m_pageScaleFactor; |
| 75 | else if (m_maxPageScaleFactor && totalPageScaleFactor > m_maxPageScaleFactor) |
| 76 | delta = m_maxPageScaleFactor / m_pageScaleFactor; |
| 77 | |
| 78 | if (delta == m_pageScaleDelta) |
| 79 | return; |
| 80 | |
| 81 | m_pageScaleDelta = delta; |
| 82 | } |
| 83 | |
| 84 | bool CCPinchZoomViewport::setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor) |
| 85 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 86 | DCHECK(pageScaleFactor); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 87 | |
| 88 | if (m_sentPageScaleDelta == 1 && pageScaleFactor == m_pageScaleFactor && minPageScaleFactor == m_minPageScaleFactor && maxPageScaleFactor == m_maxPageScaleFactor) |
| 89 | return false; |
| 90 | |
| 91 | m_minPageScaleFactor = minPageScaleFactor; |
| 92 | m_maxPageScaleFactor = maxPageScaleFactor; |
| 93 | |
| 94 | m_pageScaleFactor = pageScaleFactor; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | FloatRect CCPinchZoomViewport::bounds() const |
| 99 | { |
| 100 | FloatSize scaledViewportSize = m_layoutViewportSize; |
| 101 | scaledViewportSize.scale(1 / totalPageScaleFactor()); |
| 102 | |
| 103 | FloatRect bounds(FloatPoint(0, 0), scaledViewportSize); |
| 104 | bounds.setLocation(m_pinchViewportScrollDelta); |
| 105 | |
| 106 | return bounds; |
| 107 | } |
| 108 | |
| 109 | FloatSize CCPinchZoomViewport::applyScroll(FloatSize& delta) |
| 110 | { |
| 111 | FloatSize overflow; |
| 112 | FloatRect pinchedBounds = bounds(); |
| 113 | |
| 114 | pinchedBounds.move(delta); |
| 115 | if (pinchedBounds.x() < 0) { |
| 116 | overflow.setWidth(pinchedBounds.x()); |
| 117 | pinchedBounds.setX(0); |
| 118 | } |
| 119 | |
| 120 | if (pinchedBounds.y() < 0) { |
| 121 | overflow.setHeight(pinchedBounds.y()); |
| 122 | pinchedBounds.setY(0); |
| 123 | } |
| 124 | |
| 125 | if (pinchedBounds.maxX() > m_layoutViewportSize.width()) { |
| 126 | overflow.setWidth( |
| 127 | pinchedBounds.maxX() - m_layoutViewportSize.width()); |
| 128 | pinchedBounds.move( |
| 129 | m_layoutViewportSize.width() - pinchedBounds.maxX(), 0); |
| 130 | } |
| 131 | |
| 132 | if (pinchedBounds.maxY() > m_layoutViewportSize.height()) { |
| 133 | overflow.setHeight( |
| 134 | pinchedBounds.maxY() - m_layoutViewportSize.height()); |
| 135 | pinchedBounds.move( |
| 136 | 0, m_layoutViewportSize.height() - pinchedBounds.maxY()); |
| 137 | } |
| 138 | m_pinchViewportScrollDelta = pinchedBounds.location(); |
| 139 | |
| 140 | return overflow; |
| 141 | } |
| 142 | |
| 143 | WebTransformationMatrix CCPinchZoomViewport::implTransform() const |
| 144 | { |
| 145 | WebTransformationMatrix transform; |
| 146 | transform.scale(m_pageScaleDelta); |
| 147 | |
| 148 | // If the pinch state is applied in the impl, then push it to the |
| 149 | // impl transform, otherwise the scale is handled by WebCore. |
[email protected] | 65bfd997 | 2012-10-19 03:39:37 | [diff] [blame] | 150 | if (Settings::pageScalePinchZoomEnabled()) { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 151 | transform.scale(m_pageScaleFactor); |
| 152 | transform.translate(-m_pinchViewportScrollDelta.x(), |
| 153 | -m_pinchViewportScrollDelta.y()); |
| 154 | } |
| 155 | |
| 156 | return transform; |
| 157 | } |
| 158 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 159 | class CCLayerTreeHostImplTimeSourceAdapter : public CCTimeSourceClient { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 160 | public: |
[email protected] | 357b517 | 2012-10-18 17:39:33 | [diff] [blame] | 161 | static scoped_ptr<CCLayerTreeHostImplTimeSourceAdapter> create(CCLayerTreeHostImpl* layerTreeHostImpl, scoped_refptr<CCDelayBasedTimeSource> timeSource) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 162 | { |
[email protected] | 0023e8b | 2012-10-15 12:52:45 | [diff] [blame] | 163 | return make_scoped_ptr(new CCLayerTreeHostImplTimeSourceAdapter(layerTreeHostImpl, timeSource)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 164 | } |
| 165 | virtual ~CCLayerTreeHostImplTimeSourceAdapter() |
| 166 | { |
| 167 | m_timeSource->setClient(0); |
| 168 | m_timeSource->setActive(false); |
| 169 | } |
| 170 | |
| 171 | virtual void onTimerTick() OVERRIDE |
| 172 | { |
| 173 | // FIXME: We require that animate be called on the impl thread. This |
| 174 | // avoids asserts in single threaded mode. Ideally background ticking |
| 175 | // would be handled by the proxy/scheduler and this could be removed. |
| 176 | DebugScopedSetImplThread impl; |
| 177 | |
| 178 | m_layerTreeHostImpl->animate(monotonicallyIncreasingTime(), currentTime()); |
| 179 | } |
| 180 | |
| 181 | void setActive(bool active) |
| 182 | { |
| 183 | if (active != m_timeSource->active()) |
| 184 | m_timeSource->setActive(active); |
| 185 | } |
| 186 | |
| 187 | private: |
[email protected] | 357b517 | 2012-10-18 17:39:33 | [diff] [blame] | 188 | CCLayerTreeHostImplTimeSourceAdapter(CCLayerTreeHostImpl* layerTreeHostImpl, scoped_refptr<CCDelayBasedTimeSource> timeSource) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 189 | : m_layerTreeHostImpl(layerTreeHostImpl) |
| 190 | , m_timeSource(timeSource) |
| 191 | { |
| 192 | m_timeSource->setClient(this); |
| 193 | } |
| 194 | |
| 195 | CCLayerTreeHostImpl* m_layerTreeHostImpl; |
[email protected] | 357b517 | 2012-10-18 17:39:33 | [diff] [blame] | 196 | scoped_refptr<CCDelayBasedTimeSource> m_timeSource; |
[email protected] | fd2d4f2 | 2012-09-28 22:57:20 | [diff] [blame] | 197 | |
| 198 | DISALLOW_COPY_AND_ASSIGN(CCLayerTreeHostImplTimeSourceAdapter); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 199 | }; |
| 200 | |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 201 | CCLayerTreeHostImpl::FrameData::FrameData() |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | CCLayerTreeHostImpl::FrameData::~FrameData() |
| 206 | { |
| 207 | } |
| 208 | |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 209 | scoped_ptr<CCLayerTreeHostImpl> CCLayerTreeHostImpl::create(const CCLayerTreeSettings& settings, CCLayerTreeHostImplClient* client) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 210 | { |
[email protected] | db12400 | 2012-10-09 02:34:42 | [diff] [blame] | 211 | return make_scoped_ptr(new CCLayerTreeHostImpl(settings, client)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | CCLayerTreeHostImpl::CCLayerTreeHostImpl(const CCLayerTreeSettings& settings, CCLayerTreeHostImplClient* client) |
| 215 | : m_client(client) |
| 216 | , m_sourceFrameNumber(-1) |
| 217 | , m_rootScrollLayerImpl(0) |
| 218 | , m_currentlyScrollingLayerImpl(0) |
| 219 | , m_hudLayerImpl(0) |
| 220 | , m_scrollingLayerIdFromPreviousTree(-1) |
| 221 | , m_scrollDeltaIsInScreenSpace(false) |
| 222 | , m_settings(settings) |
| 223 | , m_deviceScaleFactor(1) |
| 224 | , m_visible(true) |
| 225 | , m_contentsTexturesPurged(false) |
| 226 | , m_memoryAllocationLimitBytes(CCPrioritizedTextureManager::defaultMemoryAllocationLimit()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 227 | , m_backgroundColor(0) |
| 228 | , m_hasTransparentBackground(false) |
| 229 | , m_needsAnimateLayers(false) |
| 230 | , m_pinchGestureActive(false) |
| 231 | , m_fpsCounter(CCFrameRateCounter::create()) |
| 232 | , m_debugRectHistory(CCDebugRectHistory::create()) |
[email protected] | 5c6fe1f8 | 2012-10-03 18:00:27 | [diff] [blame] | 233 | , m_numImplThreadScrolls(0) |
| 234 | , m_numMainThreadScrolls(0) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 235 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 236 | DCHECK(CCProxy::isImplThread()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 237 | didVisibilityChange(this, m_visible); |
| 238 | } |
| 239 | |
| 240 | CCLayerTreeHostImpl::~CCLayerTreeHostImpl() |
| 241 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 242 | DCHECK(CCProxy::isImplThread()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 243 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::~CCLayerTreeHostImpl()"); |
| 244 | |
| 245 | if (m_rootLayerImpl) |
| 246 | clearRenderSurfaces(); |
| 247 | } |
| 248 | |
| 249 | void CCLayerTreeHostImpl::beginCommit() |
| 250 | { |
| 251 | } |
| 252 | |
| 253 | void CCLayerTreeHostImpl::commitComplete() |
| 254 | { |
| 255 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::commitComplete"); |
| 256 | // Recompute max scroll position; must be after layer content bounds are |
| 257 | // updated. |
| 258 | updateMaxScrollPosition(); |
| 259 | } |
| 260 | |
| 261 | bool CCLayerTreeHostImpl::canDraw() |
| 262 | { |
[email protected] | 8db2213c | 2012-09-05 22:08:21 | [diff] [blame] | 263 | // Note: If you are changing this function or any other function that might |
| 264 | // affect the result of canDraw, make sure to call m_client->onCanDrawStateChanged |
| 265 | // in the proper places and update the notifyIfCanDrawChanged test. |
| 266 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 267 | if (!m_rootLayerImpl) { |
| 268 | TRACE_EVENT_INSTANT0("cc", "CCLayerTreeHostImpl::canDraw no root layer"); |
| 269 | return false; |
| 270 | } |
| 271 | if (deviceViewportSize().isEmpty()) { |
| 272 | TRACE_EVENT_INSTANT0("cc", "CCLayerTreeHostImpl::canDraw empty viewport"); |
| 273 | return false; |
| 274 | } |
| 275 | if (!m_renderer) { |
| 276 | TRACE_EVENT_INSTANT0("cc", "CCLayerTreeHostImpl::canDraw no renderer"); |
| 277 | return false; |
| 278 | } |
| 279 | if (m_contentsTexturesPurged) { |
| 280 | TRACE_EVENT_INSTANT0("cc", "CCLayerTreeHostImpl::canDraw contents textures purged"); |
| 281 | return false; |
| 282 | } |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | CCGraphicsContext* CCLayerTreeHostImpl::context() const |
| 287 | { |
| 288 | return m_context.get(); |
| 289 | } |
| 290 | |
| 291 | void CCLayerTreeHostImpl::animate(double monotonicTime, double wallClockTime) |
| 292 | { |
| 293 | animatePageScale(monotonicTime); |
| 294 | animateLayers(monotonicTime, wallClockTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 295 | animateScrollbars(monotonicTime); |
| 296 | } |
| 297 | |
| 298 | void CCLayerTreeHostImpl::startPageScaleAnimation(const IntSize& targetPosition, bool anchorPoint, float pageScale, double startTime, double duration) |
| 299 | { |
| 300 | if (!m_rootScrollLayerImpl) |
| 301 | return; |
| 302 | |
| 303 | IntSize scrollTotal = flooredIntSize(m_rootScrollLayerImpl->scrollPosition() + m_rootScrollLayerImpl->scrollDelta()); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 304 | scrollTotal.scale(m_pinchZoomViewport.pageScaleDelta()); |
| 305 | float scaleTotal = m_pinchZoomViewport.totalPageScaleFactor(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 306 | IntSize scaledContentSize = contentSize(); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 307 | scaledContentSize.scale(m_pinchZoomViewport.pageScaleDelta()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 308 | |
| 309 | m_pageScaleAnimation = CCPageScaleAnimation::create(scrollTotal, scaleTotal, m_deviceViewportSize, scaledContentSize, startTime); |
| 310 | |
| 311 | if (anchorPoint) { |
| 312 | IntSize windowAnchor(targetPosition); |
| 313 | windowAnchor.scale(scaleTotal / pageScale); |
| 314 | windowAnchor -= scrollTotal; |
| 315 | m_pageScaleAnimation->zoomWithAnchor(windowAnchor, pageScale, duration); |
| 316 | } else |
| 317 | m_pageScaleAnimation->zoomTo(targetPosition, pageScale, duration); |
| 318 | |
| 319 | m_client->setNeedsRedrawOnImplThread(); |
| 320 | m_client->setNeedsCommitOnImplThread(); |
| 321 | } |
| 322 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 323 | void CCLayerTreeHostImpl::scheduleAnimation() |
| 324 | { |
| 325 | m_client->setNeedsRedrawOnImplThread(); |
| 326 | } |
| 327 | |
| 328 | void CCLayerTreeHostImpl::trackDamageForAllSurfaces(CCLayerImpl* rootDrawLayer, const CCLayerList& renderSurfaceLayerList) |
| 329 | { |
| 330 | // For now, we use damage tracking to compute a global scissor. To do this, we must |
| 331 | // compute all damage tracking before drawing anything, so that we know the root |
| 332 | // damage rect. The root damage rect is then used to scissor each surface. |
| 333 | |
| 334 | for (int surfaceIndex = renderSurfaceLayerList.size() - 1; surfaceIndex >= 0 ; --surfaceIndex) { |
| 335 | CCLayerImpl* renderSurfaceLayer = renderSurfaceLayerList[surfaceIndex]; |
| 336 | CCRenderSurface* renderSurface = renderSurfaceLayer->renderSurface(); |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 337 | DCHECK(renderSurface); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 338 | renderSurface->damageTracker()->updateDamageTrackingState(renderSurface->layerList(), renderSurfaceLayer->id(), renderSurface->surfacePropertyChangedOnlyFromDescendant(), renderSurface->contentRect(), renderSurfaceLayer->maskLayer(), renderSurfaceLayer->filters()); |
| 339 | } |
| 340 | } |
| 341 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 342 | void CCLayerTreeHostImpl::updateRootScrollLayerImplTransform() |
| 343 | { |
| 344 | if (m_rootScrollLayerImpl) { |
| 345 | m_rootScrollLayerImpl->setImplTransform(implTransform()); |
| 346 | } |
| 347 | } |
| 348 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 349 | void CCLayerTreeHostImpl::calculateRenderSurfaceLayerList(CCLayerList& renderSurfaceLayerList) |
| 350 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 351 | DCHECK(renderSurfaceLayerList.empty()); |
| 352 | DCHECK(m_rootLayerImpl); |
| 353 | DCHECK(m_renderer); // For maxTextureSize. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 354 | |
| 355 | { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 356 | updateRootScrollLayerImplTransform(); |
| 357 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 358 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::calcDrawEtc"); |
[email protected] | f3922f2 | 2012-10-12 09:20:38 | [diff] [blame] | 359 | CCLayerTreeHostCommon::calculateDrawTransforms(m_rootLayerImpl.get(), deviceViewportSize(), m_deviceScaleFactor, &m_layerSorter, rendererCapabilities().maxTextureSize, renderSurfaceLayerList); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 360 | |
| 361 | trackDamageForAllSurfaces(m_rootLayerImpl.get(), renderSurfaceLayerList); |
| 362 | } |
| 363 | } |
| 364 | |
[email protected] | 87cea537 | 2012-09-26 18:59:56 | [diff] [blame] | 365 | void CCLayerTreeHostImpl::FrameData::appendRenderPass(scoped_ptr<CCRenderPass> renderPass) |
[email protected] | 467b361 | 2012-08-28 07:41:16 | [diff] [blame] | 366 | { |
| 367 | CCRenderPass* pass = renderPass.get(); |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 368 | renderPasses.push_back(pass); |
[email protected] | 87cea537 | 2012-09-26 18:59:56 | [diff] [blame] | 369 | renderPassesById.set(pass->id(), renderPass.Pass()); |
[email protected] | 467b361 | 2012-08-28 07:41:16 | [diff] [blame] | 370 | } |
| 371 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 372 | bool CCLayerTreeHostImpl::calculateRenderPasses(FrameData& frame) |
| 373 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 374 | DCHECK(frame.renderPasses.empty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 375 | |
| 376 | calculateRenderSurfaceLayerList(*frame.renderSurfaceLayerList); |
| 377 | |
| 378 | TRACE_EVENT1("cc", "CCLayerTreeHostImpl::calculateRenderPasses", "renderSurfaceLayerList.size()", static_cast<long long unsigned>(frame.renderSurfaceLayerList->size())); |
| 379 | |
| 380 | // Create the render passes in dependency order. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 381 | for (int surfaceIndex = frame.renderSurfaceLayerList->size() - 1; surfaceIndex >= 0 ; --surfaceIndex) { |
| 382 | CCLayerImpl* renderSurfaceLayer = (*frame.renderSurfaceLayerList)[surfaceIndex]; |
[email protected] | 467b361 | 2012-08-28 07:41:16 | [diff] [blame] | 383 | renderSurfaceLayer->renderSurface()->appendRenderPasses(frame); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | bool recordMetricsForFrame = true; // FIXME: In the future, disable this when about:tracing is off. |
| 387 | CCOcclusionTrackerImpl occlusionTracker(m_rootLayerImpl->renderSurface()->contentRect(), recordMetricsForFrame); |
| 388 | occlusionTracker.setMinimumTrackingSize(m_settings.minimumOcclusionTrackingSize); |
| 389 | |
| 390 | if (settings().showOccludingRects) |
| 391 | occlusionTracker.setOccludingScreenSpaceRectsContainer(&frame.occludingScreenSpaceRects); |
| 392 | |
| 393 | // Add quads to the Render passes in FrontToBack order to allow for testing occlusion and performing culling during the tree walk. |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 394 | typedef CCLayerIterator<CCLayerImpl, std::vector<CCLayerImpl*>, CCRenderSurface, CCLayerIteratorActions::FrontToBack> CCLayerIteratorType; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 395 | |
| 396 | // Typically when we are missing a texture and use a checkerboard quad, we still draw the frame. However when the layer being |
| 397 | // checkerboarded is moving due to an impl-animation, we drop the frame to avoid flashing due to the texture suddenly appearing |
| 398 | // in the future. |
| 399 | bool drawFrame = true; |
| 400 | |
| 401 | CCLayerIteratorType end = CCLayerIteratorType::end(frame.renderSurfaceLayerList); |
| 402 | for (CCLayerIteratorType it = CCLayerIteratorType::begin(frame.renderSurfaceLayerList); it != end; ++it) { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 403 | CCRenderPass::Id targetRenderPassId = it.targetRenderSurfaceLayer()->renderSurface()->renderPassId(); |
[email protected] | 467b361 | 2012-08-28 07:41:16 | [diff] [blame] | 404 | CCRenderPass* targetRenderPass = frame.renderPassesById.get(targetRenderPassId); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 405 | |
| 406 | occlusionTracker.enterLayer(it); |
| 407 | |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 408 | CCAppendQuadsData appendQuadsData(targetRenderPass->id()); |
[email protected] | 8922820 | 2012-08-29 03:20:30 | [diff] [blame] | 409 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 410 | if (it.representsContributingRenderSurface()) { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 411 | CCRenderPass::Id contributingRenderPassId = it->renderSurface()->renderPassId(); |
[email protected] | 467b361 | 2012-08-28 07:41:16 | [diff] [blame] | 412 | CCRenderPass* contributingRenderPass = frame.renderPassesById.get(contributingRenderPassId); |
[email protected] | 8922820 | 2012-08-29 03:20:30 | [diff] [blame] | 413 | targetRenderPass->appendQuadsForRenderSurfaceLayer(*it, contributingRenderPass, &occlusionTracker, appendQuadsData); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 414 | } else if (it.representsItself() && !it->visibleContentRect().isEmpty()) { |
| 415 | bool hasOcclusionFromOutsideTargetSurface; |
[email protected] | 8922820 | 2012-08-29 03:20:30 | [diff] [blame] | 416 | if (occlusionTracker.occluded(*it, it->visibleContentRect(), &hasOcclusionFromOutsideTargetSurface)) |
| 417 | appendQuadsData.hadOcclusionFromOutsideTargetSurface |= hasOcclusionFromOutsideTargetSurface; |
| 418 | else { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 419 | it->willDraw(m_resourceProvider.get()); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 420 | frame.willDrawLayers.push_back(*it); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 421 | |
| 422 | if (it->hasContributingDelegatedRenderPasses()) { |
| 423 | CCRenderPass::Id contributingRenderPassId = it->firstContributingRenderPassId(); |
| 424 | while (frame.renderPassesById.contains(contributingRenderPassId)) { |
| 425 | CCRenderPass* renderPass = frame.renderPassesById.get(contributingRenderPassId); |
| 426 | |
| 427 | CCAppendQuadsData appendQuadsData(renderPass->id()); |
| 428 | renderPass->appendQuadsForLayer(*it, &occlusionTracker, appendQuadsData); |
| 429 | |
| 430 | contributingRenderPassId = it->nextContributingRenderPassId(contributingRenderPassId); |
| 431 | } |
| 432 | } |
| 433 | |
[email protected] | 8922820 | 2012-08-29 03:20:30 | [diff] [blame] | 434 | targetRenderPass->appendQuadsForLayer(*it, &occlusionTracker, appendQuadsData); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
[email protected] | 8922820 | 2012-08-29 03:20:30 | [diff] [blame] | 438 | if (appendQuadsData.hadOcclusionFromOutsideTargetSurface) |
| 439 | targetRenderPass->setHasOcclusionFromOutsideTargetSurface(true); |
| 440 | |
| 441 | if (appendQuadsData.hadMissingTiles) { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 442 | bool layerHasAnimatingTransform = it->screenSpaceTransformIsAnimating() || it->drawTransformIsAnimating(); |
[email protected] | 65bfd997 | 2012-10-19 03:39:37 | [diff] [blame] | 443 | if (layerHasAnimatingTransform || Settings::jankInsteadOfCheckerboard()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 444 | drawFrame = false; |
| 445 | } |
| 446 | |
| 447 | occlusionTracker.leaveLayer(it); |
| 448 | } |
| 449 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 450 | #ifndef NDEBUG |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 451 | for (size_t i = 0; i < frame.renderPasses.size(); ++i) { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 452 | for (size_t j = 0; j < frame.renderPasses[i]->quadList().size(); ++j) |
| 453 | DCHECK(frame.renderPasses[i]->quadList()[j]->sharedQuadStateId() >= 0); |
| 454 | DCHECK(frame.renderPassesById.contains(frame.renderPasses[i]->id())); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 455 | } |
| 456 | #endif |
| 457 | |
| 458 | if (!m_hasTransparentBackground) { |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 459 | frame.renderPasses.back()->setHasTransparentBackground(false); |
| 460 | frame.renderPasses.back()->appendQuadsToFillScreen(m_rootLayerImpl.get(), m_backgroundColor, occlusionTracker); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | if (drawFrame) |
| 464 | occlusionTracker.overdrawMetrics().recordMetrics(this); |
| 465 | |
| 466 | removeRenderPasses(CullRenderPassesWithNoQuads(), frame); |
| 467 | m_renderer->decideRenderPassAllocationsForFrame(frame.renderPasses); |
| 468 | removeRenderPasses(CullRenderPassesWithCachedTextures(*m_renderer), frame); |
| 469 | |
| 470 | return drawFrame; |
| 471 | } |
| 472 | |
| 473 | void CCLayerTreeHostImpl::animateLayersRecursive(CCLayerImpl* current, double monotonicTime, double wallClockTime, CCAnimationEventsVector* events, bool& didAnimate, bool& needsAnimateLayers) |
| 474 | { |
| 475 | bool subtreeNeedsAnimateLayers = false; |
| 476 | |
| 477 | CCLayerAnimationController* currentController = current->layerAnimationController(); |
| 478 | |
| 479 | bool hadActiveAnimation = currentController->hasActiveAnimation(); |
| 480 | currentController->animate(monotonicTime, events); |
| 481 | bool startedAnimation = events->size() > 0; |
| 482 | |
| 483 | // We animated if we either ticked a running animation, or started a new animation. |
| 484 | if (hadActiveAnimation || startedAnimation) |
| 485 | didAnimate = true; |
| 486 | |
| 487 | // If the current controller still has an active animation, we must continue animating layers. |
| 488 | if (currentController->hasActiveAnimation()) |
| 489 | subtreeNeedsAnimateLayers = true; |
| 490 | |
| 491 | for (size_t i = 0; i < current->children().size(); ++i) { |
| 492 | bool childNeedsAnimateLayers = false; |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 493 | animateLayersRecursive(current->children()[i], monotonicTime, wallClockTime, events, didAnimate, childNeedsAnimateLayers); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 494 | if (childNeedsAnimateLayers) |
| 495 | subtreeNeedsAnimateLayers = true; |
| 496 | } |
| 497 | |
| 498 | needsAnimateLayers = subtreeNeedsAnimateLayers; |
| 499 | } |
| 500 | |
| 501 | void CCLayerTreeHostImpl::setBackgroundTickingEnabled(bool enabled) |
| 502 | { |
| 503 | // Lazily create the timeSource adapter so that we can vary the interval for testing. |
| 504 | if (!m_timeSourceClientAdapter) |
| 505 | m_timeSourceClientAdapter = CCLayerTreeHostImplTimeSourceAdapter::create(this, CCDelayBasedTimeSource::create(lowFrequencyAnimationInterval(), CCProxy::currentThread())); |
| 506 | |
| 507 | m_timeSourceClientAdapter->setActive(enabled); |
| 508 | } |
| 509 | |
| 510 | IntSize CCLayerTreeHostImpl::contentSize() const |
| 511 | { |
| 512 | // TODO(aelias): Hardcoding the first child here is weird. Think of |
| 513 | // a cleaner way to get the contentBounds on the Impl side. |
| 514 | if (!m_rootScrollLayerImpl || m_rootScrollLayerImpl->children().isEmpty()) |
| 515 | return IntSize(); |
| 516 | return m_rootScrollLayerImpl->children()[0]->contentBounds(); |
| 517 | } |
| 518 | |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 519 | static inline CCRenderPass* findRenderPassById(CCRenderPass::Id renderPassId, const CCLayerTreeHostImpl::FrameData& frame) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 520 | { |
| 521 | CCRenderPassIdHashMap::const_iterator it = frame.renderPassesById.find(renderPassId); |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 522 | DCHECK(it != frame.renderPassesById.end()); |
[email protected] | 87cea537 | 2012-09-26 18:59:56 | [diff] [blame] | 523 | return it->second; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 524 | } |
| 525 | |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 526 | static void removeRenderPassesRecursive(CCRenderPass::Id removeRenderPassId, CCLayerTreeHostImpl::FrameData& frame) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 527 | { |
| 528 | CCRenderPass* removeRenderPass = findRenderPassById(removeRenderPassId, frame); |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 529 | CCRenderPassList& renderPasses = frame.renderPasses; |
| 530 | CCRenderPassList::iterator toRemove = std::find(renderPasses.begin(), renderPasses.end(), removeRenderPass); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 531 | |
| 532 | // The pass was already removed by another quad - probably the original, and we are the replica. |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 533 | if (toRemove == renderPasses.end()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 534 | return; |
| 535 | |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 536 | const CCRenderPass* removedPass = *toRemove; |
| 537 | frame.renderPasses.erase(toRemove); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 538 | |
| 539 | // Now follow up for all RenderPass quads and remove their RenderPasses recursively. |
| 540 | const CCQuadList& quadList = removedPass->quadList(); |
| 541 | CCQuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin(); |
| 542 | for (; quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) { |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 543 | CCDrawQuad* currentQuad = (*quadListIterator); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 544 | if (currentQuad->material() != CCDrawQuad::RenderPass) |
| 545 | continue; |
| 546 | |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 547 | CCRenderPass::Id nextRemoveRenderPassId = CCRenderPassDrawQuad::materialCast(currentQuad)->renderPassId(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 548 | removeRenderPassesRecursive(nextRemoveRenderPassId, frame); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | bool CCLayerTreeHostImpl::CullRenderPassesWithCachedTextures::shouldRemoveRenderPass(const CCRenderPassDrawQuad& quad, const FrameData&) const |
| 553 | { |
| 554 | return quad.contentsChangedSinceLastFrame().isEmpty() && m_renderer.haveCachedResourcesForRenderPassId(quad.renderPassId()); |
| 555 | } |
| 556 | |
| 557 | bool CCLayerTreeHostImpl::CullRenderPassesWithNoQuads::shouldRemoveRenderPass(const CCRenderPassDrawQuad& quad, const FrameData& frame) const |
| 558 | { |
| 559 | const CCRenderPass* renderPass = findRenderPassById(quad.renderPassId(), frame); |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 560 | const CCRenderPassList& renderPasses = frame.renderPasses; |
| 561 | CCRenderPassList::const_iterator foundPass = std::find(renderPasses.begin(), renderPasses.end(), renderPass); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 562 | |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 563 | bool renderPassAlreadyRemoved = foundPass == renderPasses.end(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 564 | if (renderPassAlreadyRemoved) |
| 565 | return false; |
| 566 | |
| 567 | // If any quad or RenderPass draws into this RenderPass, then keep it. |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 568 | const CCQuadList& quadList = (*foundPass)->quadList(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 569 | for (CCQuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin(); quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) { |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 570 | CCDrawQuad* currentQuad = *quadListIterator; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 571 | |
| 572 | if (currentQuad->material() != CCDrawQuad::RenderPass) |
| 573 | return false; |
| 574 | |
| 575 | const CCRenderPass* contributingPass = findRenderPassById(CCRenderPassDrawQuad::materialCast(currentQuad)->renderPassId(), frame); |
[email protected] | f8ad834 | 2012-09-27 20:07:02 | [diff] [blame] | 576 | CCRenderPassList::const_iterator foundContributingPass = std::find(renderPasses.begin(), renderPasses.end(), contributingPass); |
| 577 | if (foundContributingPass != renderPasses.end()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 578 | return false; |
| 579 | } |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | // Defined for linking tests. |
| 584 | template void CCLayerTreeHostImpl::removeRenderPasses<CCLayerTreeHostImpl::CullRenderPassesWithCachedTextures>(CullRenderPassesWithCachedTextures, FrameData&); |
| 585 | template void CCLayerTreeHostImpl::removeRenderPasses<CCLayerTreeHostImpl::CullRenderPassesWithNoQuads>(CullRenderPassesWithNoQuads, FrameData&); |
| 586 | |
| 587 | // static |
| 588 | template<typename RenderPassCuller> |
| 589 | void CCLayerTreeHostImpl::removeRenderPasses(RenderPassCuller culler, FrameData& frame) |
| 590 | { |
| 591 | for (size_t it = culler.renderPassListBegin(frame.renderPasses); it != culler.renderPassListEnd(frame.renderPasses); it = culler.renderPassListNext(it)) { |
| 592 | const CCRenderPass* currentPass = frame.renderPasses[it]; |
| 593 | const CCQuadList& quadList = currentPass->quadList(); |
| 594 | CCQuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin(); |
| 595 | |
| 596 | for (; quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) { |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 597 | CCDrawQuad* currentQuad = *quadListIterator; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 598 | |
| 599 | if (currentQuad->material() != CCDrawQuad::RenderPass) |
| 600 | continue; |
| 601 | |
| 602 | CCRenderPassDrawQuad* renderPassQuad = static_cast<CCRenderPassDrawQuad*>(currentQuad); |
| 603 | if (!culler.shouldRemoveRenderPass(*renderPassQuad, frame)) |
| 604 | continue; |
| 605 | |
| 606 | // We are changing the vector in the middle of iteration. Because we |
| 607 | // delete render passes that draw into the current pass, we are |
| 608 | // guaranteed that any data from the iterator to the end will not |
| 609 | // change. So, capture the iterator position from the end of the |
| 610 | // list, and restore it after the change. |
| 611 | int positionFromEnd = frame.renderPasses.size() - it; |
| 612 | removeRenderPassesRecursive(renderPassQuad->renderPassId(), frame); |
| 613 | it = frame.renderPasses.size() - positionFromEnd; |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 614 | DCHECK(it >= 0); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | bool CCLayerTreeHostImpl::prepareToDraw(FrameData& frame) |
| 620 | { |
| 621 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::prepareToDraw"); |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 622 | DCHECK(canDraw()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 623 | |
| 624 | frame.renderSurfaceLayerList = &m_renderSurfaceLayerList; |
| 625 | frame.renderPasses.clear(); |
| 626 | frame.renderPassesById.clear(); |
| 627 | frame.renderSurfaceLayerList->clear(); |
| 628 | frame.willDrawLayers.clear(); |
| 629 | |
| 630 | if (!calculateRenderPasses(frame)) |
| 631 | return false; |
| 632 | |
| 633 | // If we return true, then we expect drawLayers() to be called before this function is called again. |
| 634 | return true; |
| 635 | } |
| 636 | |
[email protected] | b1969fa | 2012-10-17 20:16:29 | [diff] [blame] | 637 | void CCLayerTreeHostImpl::reduceContentsTextureMemoryOnImplThread(size_t limitBytes) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 638 | { |
[email protected] | b1969fa | 2012-10-17 20:16:29 | [diff] [blame] | 639 | bool evictedResources = m_client->reduceContentsTextureMemoryOnImplThread(limitBytes); |
| 640 | if (evictedResources) { |
| 641 | setContentsTexturesPurged(); |
| 642 | m_client->setNeedsCommitOnImplThread(); |
| 643 | m_client->onCanDrawStateChanged(canDraw()); |
| 644 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void CCLayerTreeHostImpl::setMemoryAllocationLimitBytes(size_t bytes) |
| 648 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 649 | DCHECK(bytes); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 650 | if (m_memoryAllocationLimitBytes == bytes) |
| 651 | return; |
| 652 | m_memoryAllocationLimitBytes = bytes; |
[email protected] | b1969fa | 2012-10-17 20:16:29 | [diff] [blame] | 653 | reduceContentsTextureMemoryOnImplThread(m_visible ? m_memoryAllocationLimitBytes : 0); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 654 | m_client->setNeedsCommitOnImplThread(); |
| 655 | } |
| 656 | |
| 657 | void CCLayerTreeHostImpl::onVSyncParametersChanged(double monotonicTimebase, double intervalInSeconds) |
| 658 | { |
| 659 | m_client->onVSyncParametersChanged(monotonicTimebase, intervalInSeconds); |
| 660 | } |
| 661 | |
| 662 | void CCLayerTreeHostImpl::drawLayers(const FrameData& frame) |
| 663 | { |
| 664 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::drawLayers"); |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 665 | DCHECK(canDraw()); |
| 666 | DCHECK(!frame.renderPasses.empty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 667 | |
| 668 | // FIXME: use the frame begin time from the overall compositor scheduler. |
| 669 | // This value is currently inaccessible because it is up in Chromium's |
| 670 | // RenderWidget. |
[email protected] | 6bea87c | 2012-10-13 00:15:21 | [diff] [blame] | 671 | m_fpsCounter->markBeginningOfFrame(base::TimeTicks::Now()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 672 | |
| 673 | if (m_settings.showDebugRects()) |
| 674 | m_debugRectHistory->saveDebugRectsForCurrentFrame(m_rootLayerImpl.get(), *frame.renderSurfaceLayerList, frame.occludingScreenSpaceRects, settings()); |
| 675 | |
| 676 | // Because the contents of the HUD depend on everything else in the frame, the contents |
| 677 | // of its texture are updated as the last thing before the frame is drawn. |
| 678 | if (m_hudLayerImpl) |
| 679 | m_hudLayerImpl->updateHudTexture(m_resourceProvider.get()); |
| 680 | |
| 681 | m_renderer->drawFrame(frame.renderPasses, frame.renderPassesById); |
| 682 | |
| 683 | // Once a RenderPass has been drawn, its damage should be cleared in |
| 684 | // case the RenderPass will be reused next frame. |
| 685 | for (unsigned int i = 0; i < frame.renderPasses.size(); i++) |
| 686 | frame.renderPasses[i]->setDamageRect(FloatRect()); |
| 687 | |
| 688 | // The next frame should start by assuming nothing has changed, and changes are noted as they occur. |
| 689 | for (unsigned int i = 0; i < frame.renderSurfaceLayerList->size(); i++) |
| 690 | (*frame.renderSurfaceLayerList)[i]->renderSurface()->damageTracker()->didDrawDamagedArea(); |
| 691 | m_rootLayerImpl->resetAllChangeTrackingForSubtree(); |
| 692 | } |
| 693 | |
| 694 | void CCLayerTreeHostImpl::didDrawAllLayers(const FrameData& frame) |
| 695 | { |
| 696 | for (size_t i = 0; i < frame.willDrawLayers.size(); ++i) |
| 697 | frame.willDrawLayers[i]->didDraw(m_resourceProvider.get()); |
[email protected] | b914e10 | 2012-10-02 08:11:52 | [diff] [blame] | 698 | |
| 699 | // Once all layers have been drawn, pending texture uploads should no |
| 700 | // longer block future uploads. |
[email protected] | e224959 | 2012-10-19 06:59:09 | [diff] [blame^] | 701 | m_resourceProvider->markPendingUploadsAsNonBlocking(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | void CCLayerTreeHostImpl::finishAllRendering() |
| 705 | { |
| 706 | if (m_renderer) |
| 707 | m_renderer->finish(); |
| 708 | } |
| 709 | |
| 710 | bool CCLayerTreeHostImpl::isContextLost() |
| 711 | { |
| 712 | return m_renderer && m_renderer->isContextLost(); |
| 713 | } |
| 714 | |
| 715 | const RendererCapabilities& CCLayerTreeHostImpl::rendererCapabilities() const |
| 716 | { |
| 717 | return m_renderer->capabilities(); |
| 718 | } |
| 719 | |
| 720 | bool CCLayerTreeHostImpl::swapBuffers() |
| 721 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 722 | DCHECK(m_renderer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 723 | |
| 724 | m_fpsCounter->markEndOfFrame(); |
| 725 | return m_renderer->swapBuffers(); |
| 726 | } |
| 727 | |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 728 | const IntSize& CCLayerTreeHostImpl::deviceViewportSize() const |
| 729 | { |
| 730 | return m_deviceViewportSize; |
| 731 | } |
| 732 | |
| 733 | const CCLayerTreeSettings& CCLayerTreeHostImpl::settings() const |
| 734 | { |
| 735 | return m_settings; |
| 736 | } |
| 737 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 738 | void CCLayerTreeHostImpl::didLoseContext() |
| 739 | { |
| 740 | m_client->didLoseContextOnImplThread(); |
| 741 | } |
| 742 | |
| 743 | void CCLayerTreeHostImpl::onSwapBuffersComplete() |
| 744 | { |
| 745 | m_client->onSwapBuffersCompleteOnImplThread(); |
| 746 | } |
| 747 | |
| 748 | void CCLayerTreeHostImpl::readback(void* pixels, const IntRect& rect) |
| 749 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 750 | DCHECK(m_renderer); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 751 | m_renderer->getFramebufferPixels(pixels, rect); |
| 752 | } |
| 753 | |
| 754 | static CCLayerImpl* findRootScrollLayer(CCLayerImpl* layer) |
| 755 | { |
| 756 | if (!layer) |
| 757 | return 0; |
| 758 | |
| 759 | if (layer->scrollable()) |
| 760 | return layer; |
| 761 | |
| 762 | for (size_t i = 0; i < layer->children().size(); ++i) { |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 763 | CCLayerImpl* found = findRootScrollLayer(layer->children()[i]); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 764 | if (found) |
| 765 | return found; |
| 766 | } |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | // Content layers can be either directly scrollable or contained in an outer |
| 772 | // scrolling layer which applies the scroll transform. Given a content layer, |
| 773 | // this function returns the associated scroll layer if any. |
| 774 | static CCLayerImpl* findScrollLayerForContentLayer(CCLayerImpl* layerImpl) |
| 775 | { |
| 776 | if (!layerImpl) |
| 777 | return 0; |
| 778 | |
| 779 | if (layerImpl->scrollable()) |
| 780 | return layerImpl; |
| 781 | |
| 782 | if (layerImpl->drawsContent() && layerImpl->parent() && layerImpl->parent()->scrollable()) |
| 783 | return layerImpl->parent(); |
| 784 | |
| 785 | return 0; |
| 786 | } |
| 787 | |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 788 | void CCLayerTreeHostImpl::setRootLayer(scoped_ptr<CCLayerImpl> layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 789 | { |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 790 | m_rootLayerImpl = layer.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 791 | m_rootScrollLayerImpl = findRootScrollLayer(m_rootLayerImpl.get()); |
| 792 | m_currentlyScrollingLayerImpl = 0; |
| 793 | |
| 794 | if (m_rootLayerImpl && m_scrollingLayerIdFromPreviousTree != -1) |
| 795 | m_currentlyScrollingLayerImpl = CCLayerTreeHostCommon::findLayerInSubtree(m_rootLayerImpl.get(), m_scrollingLayerIdFromPreviousTree); |
| 796 | |
| 797 | m_scrollingLayerIdFromPreviousTree = -1; |
[email protected] | 8db2213c | 2012-09-05 22:08:21 | [diff] [blame] | 798 | |
| 799 | m_client->onCanDrawStateChanged(canDraw()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 800 | } |
| 801 | |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 802 | scoped_ptr<CCLayerImpl> CCLayerTreeHostImpl::detachLayerTree() |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 803 | { |
| 804 | // Clear all data structures that have direct references to the layer tree. |
| 805 | m_scrollingLayerIdFromPreviousTree = m_currentlyScrollingLayerImpl ? m_currentlyScrollingLayerImpl->id() : -1; |
| 806 | m_currentlyScrollingLayerImpl = 0; |
| 807 | m_renderSurfaceLayerList.clear(); |
| 808 | |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 809 | return m_rootLayerImpl.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | void CCLayerTreeHostImpl::setVisible(bool visible) |
| 813 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 814 | DCHECK(CCProxy::isImplThread()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 815 | |
| 816 | if (m_visible == visible) |
| 817 | return; |
| 818 | m_visible = visible; |
| 819 | didVisibilityChange(this, m_visible); |
[email protected] | b1969fa | 2012-10-17 20:16:29 | [diff] [blame] | 820 | reduceContentsTextureMemoryOnImplThread(m_visible ? m_memoryAllocationLimitBytes : 0); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 821 | |
| 822 | if (!m_renderer) |
| 823 | return; |
| 824 | |
| 825 | m_renderer->setVisible(visible); |
| 826 | |
| 827 | setBackgroundTickingEnabled(!m_visible && m_needsAnimateLayers); |
| 828 | } |
| 829 | |
[email protected] | e28efacd | 2012-10-06 17:07:49 | [diff] [blame] | 830 | bool CCLayerTreeHostImpl::initializeRenderer(scoped_ptr<CCGraphicsContext> context) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 831 | { |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 832 | // Since we will create a new resource provider, we cannot continue to use |
| 833 | // the old resources (i.e. renderSurfaces and texture IDs). Clear them |
| 834 | // before we destroy the old resource provider. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 835 | if (m_rootLayerImpl) { |
| 836 | clearRenderSurfaces(); |
| 837 | sendDidLoseContextRecursive(m_rootLayerImpl.get()); |
| 838 | } |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 839 | // Note: order is important here. |
[email protected] | 0704caf | 2012-10-16 03:39:47 | [diff] [blame] | 840 | m_renderer.reset(); |
[email protected] | a7aa556 | 2012-10-17 14:12:44 | [diff] [blame] | 841 | m_resourceProvider.reset(); |
[email protected] | e28efacd | 2012-10-06 17:07:49 | [diff] [blame] | 842 | m_context.reset(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 843 | |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 844 | if (!context->bindToClient(this)) |
| 845 | return false; |
| 846 | |
[email protected] | a7aa556 | 2012-10-17 14:12:44 | [diff] [blame] | 847 | scoped_ptr<CCResourceProvider> resourceProvider = CCResourceProvider::create(context.get()); |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 848 | if (!resourceProvider) |
| 849 | return false; |
| 850 | |
| 851 | if (context->context3D()) |
| 852 | m_renderer = CCRendererGL::create(this, resourceProvider.get()); |
| 853 | else if (context->softwareDevice()) |
| 854 | m_renderer = CCRendererSoftware::create(this, resourceProvider.get(), context->softwareDevice()); |
| 855 | if (!m_renderer) |
| 856 | return false; |
| 857 | |
[email protected] | a7aa556 | 2012-10-17 14:12:44 | [diff] [blame] | 858 | m_resourceProvider = resourceProvider.Pass(); |
[email protected] | e28efacd | 2012-10-06 17:07:49 | [diff] [blame] | 859 | m_context = context.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 860 | |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 861 | if (!m_visible) |
| 862 | m_renderer->setVisible(m_visible); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 863 | |
[email protected] | 8db2213c | 2012-09-05 22:08:21 | [diff] [blame] | 864 | m_client->onCanDrawStateChanged(canDraw()); |
| 865 | |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 866 | return true; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 867 | } |
| 868 | |
[email protected] | e1fc8b3 | 2012-09-18 20:29:09 | [diff] [blame] | 869 | void CCLayerTreeHostImpl::setContentsTexturesPurged() |
| 870 | { |
| 871 | m_contentsTexturesPurged = true; |
| 872 | m_client->onCanDrawStateChanged(canDraw()); |
| 873 | } |
| 874 | |
[email protected] | 8db2213c | 2012-09-05 22:08:21 | [diff] [blame] | 875 | void CCLayerTreeHostImpl::resetContentsTexturesPurged() |
| 876 | { |
| 877 | m_contentsTexturesPurged = false; |
| 878 | m_client->onCanDrawStateChanged(canDraw()); |
| 879 | } |
| 880 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 881 | void CCLayerTreeHostImpl::setViewportSize(const IntSize& layoutViewportSize, const IntSize& deviceViewportSize) |
| 882 | { |
| 883 | if (layoutViewportSize == m_layoutViewportSize && deviceViewportSize == m_deviceViewportSize) |
| 884 | return; |
| 885 | |
| 886 | m_layoutViewportSize = layoutViewportSize; |
| 887 | m_deviceViewportSize = deviceViewportSize; |
| 888 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 889 | m_pinchZoomViewport.setLayoutViewportSize(FloatSize(layoutViewportSize)); |
| 890 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 891 | updateMaxScrollPosition(); |
| 892 | |
| 893 | if (m_renderer) |
| 894 | m_renderer->viewportChanged(); |
[email protected] | 8db2213c | 2012-09-05 22:08:21 | [diff] [blame] | 895 | |
| 896 | m_client->onCanDrawStateChanged(canDraw()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | static void adjustScrollsForPageScaleChange(CCLayerImpl* layerImpl, float pageScaleChange) |
| 900 | { |
| 901 | if (!layerImpl) |
| 902 | return; |
| 903 | |
| 904 | if (layerImpl->scrollable()) { |
| 905 | // We need to convert impl-side scroll deltas to pageScale space. |
| 906 | FloatSize scrollDelta = layerImpl->scrollDelta(); |
| 907 | scrollDelta.scale(pageScaleChange); |
| 908 | layerImpl->setScrollDelta(scrollDelta); |
| 909 | } |
| 910 | |
| 911 | for (size_t i = 0; i < layerImpl->children().size(); ++i) |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 912 | adjustScrollsForPageScaleChange(layerImpl->children()[i], pageScaleChange); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void CCLayerTreeHostImpl::setDeviceScaleFactor(float deviceScaleFactor) |
| 916 | { |
| 917 | if (deviceScaleFactor == m_deviceScaleFactor) |
| 918 | return; |
| 919 | m_deviceScaleFactor = deviceScaleFactor; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 920 | |
| 921 | updateMaxScrollPosition(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 922 | } |
| 923 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 924 | float CCLayerTreeHostImpl::pageScaleFactor() const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 925 | { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 926 | return m_pinchZoomViewport.pageScaleFactor(); |
| 927 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 928 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 929 | void CCLayerTreeHostImpl::setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor) |
| 930 | { |
| 931 | if (!pageScaleFactor) |
| 932 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 933 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 934 | float pageScaleChange = pageScaleFactor / m_pinchZoomViewport.pageScaleFactor(); |
| 935 | m_pinchZoomViewport.setPageScaleFactorAndLimits(pageScaleFactor, minPageScaleFactor, maxPageScaleFactor); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 936 | |
[email protected] | 65bfd997 | 2012-10-19 03:39:37 | [diff] [blame] | 937 | if (!Settings::pageScalePinchZoomEnabled()) { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 938 | if (pageScaleChange != 1) |
| 939 | adjustScrollsForPageScaleChange(m_rootScrollLayerImpl, pageScaleChange); |
| 940 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 941 | |
| 942 | // Clamp delta to limits and refresh display matrix. |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 943 | setPageScaleDelta(m_pinchZoomViewport.pageScaleDelta() / m_pinchZoomViewport.sentPageScaleDelta()); |
| 944 | m_pinchZoomViewport.setSentPageScaleDelta(1); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | void CCLayerTreeHostImpl::setPageScaleDelta(float delta) |
| 948 | { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 949 | m_pinchZoomViewport.setPageScaleDelta(delta); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 950 | |
| 951 | updateMaxScrollPosition(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | void CCLayerTreeHostImpl::updateMaxScrollPosition() |
| 955 | { |
| 956 | if (!m_rootScrollLayerImpl || !m_rootScrollLayerImpl->children().size()) |
| 957 | return; |
| 958 | |
| 959 | FloatSize viewBounds = m_deviceViewportSize; |
| 960 | if (CCLayerImpl* clipLayer = m_rootScrollLayerImpl->parent()) { |
| 961 | // Compensate for non-overlay scrollbars. |
| 962 | if (clipLayer->masksToBounds()) { |
| 963 | viewBounds = clipLayer->bounds(); |
| 964 | viewBounds.scale(m_deviceScaleFactor); |
| 965 | } |
| 966 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 967 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 968 | IntSize contentBounds = contentSize(); |
[email protected] | 65bfd997 | 2012-10-19 03:39:37 | [diff] [blame] | 969 | if (Settings::pageScalePinchZoomEnabled()) { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 970 | // Pinch with pageScale scrolls entirely in layout space. contentSize |
| 971 | // returns the bounds including the page scale factor, so calculate the |
| 972 | // pre page-scale layout size here. |
| 973 | float pageScaleFactor = m_pinchZoomViewport.pageScaleFactor(); |
| 974 | contentBounds.setWidth(contentBounds.width() / pageScaleFactor); |
| 975 | contentBounds.setHeight(contentBounds.height() / pageScaleFactor); |
| 976 | } else { |
| 977 | viewBounds.scale(1 / m_pinchZoomViewport.pageScaleDelta()); |
| 978 | } |
| 979 | |
| 980 | IntSize maxScroll = contentBounds - expandedIntSize(viewBounds); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 981 | maxScroll.scale(1 / m_deviceScaleFactor); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 982 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 983 | // The viewport may be larger than the contents in some cases, such as |
| 984 | // having a vertical scrollbar but no horizontal overflow. |
| 985 | maxScroll.clampNegativeToZero(); |
| 986 | |
| 987 | m_rootScrollLayerImpl->setMaxScrollPosition(maxScroll); |
| 988 | } |
| 989 | |
| 990 | void CCLayerTreeHostImpl::setNeedsRedraw() |
| 991 | { |
| 992 | m_client->setNeedsRedrawOnImplThread(); |
| 993 | } |
| 994 | |
| 995 | bool CCLayerTreeHostImpl::ensureRenderSurfaceLayerList() |
| 996 | { |
| 997 | if (!m_rootLayerImpl) |
| 998 | return false; |
| 999 | if (!m_renderer) |
| 1000 | return false; |
| 1001 | |
| 1002 | // We need both a non-empty render surface layer list and a root render |
| 1003 | // surface to be able to iterate over the visible layers. |
| 1004 | if (m_renderSurfaceLayerList.size() && m_rootLayerImpl->renderSurface()) |
| 1005 | return true; |
| 1006 | |
| 1007 | // If we are called after setRootLayer() but before prepareToDraw(), we need |
| 1008 | // to recalculate the visible layers. This prevents being unable to scroll |
| 1009 | // during part of a commit. |
| 1010 | m_renderSurfaceLayerList.clear(); |
| 1011 | calculateRenderSurfaceLayerList(m_renderSurfaceLayerList); |
| 1012 | |
| 1013 | return m_renderSurfaceLayerList.size(); |
| 1014 | } |
| 1015 | |
| 1016 | CCInputHandlerClient::ScrollStatus CCLayerTreeHostImpl::scrollBegin(const IntPoint& viewportPoint, CCInputHandlerClient::ScrollInputType type) |
| 1017 | { |
| 1018 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::scrollBegin"); |
| 1019 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 1020 | DCHECK(!m_currentlyScrollingLayerImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1021 | clearCurrentlyScrollingLayer(); |
| 1022 | |
| 1023 | if (!ensureRenderSurfaceLayerList()) |
| 1024 | return ScrollIgnored; |
| 1025 | |
| 1026 | IntPoint deviceViewportPoint = viewportPoint; |
| 1027 | deviceViewportPoint.scale(m_deviceScaleFactor, m_deviceScaleFactor); |
| 1028 | |
| 1029 | // First find out which layer was hit from the saved list of visible layers |
| 1030 | // in the most recent frame. |
| 1031 | CCLayerImpl* layerImpl = CCLayerTreeHostCommon::findLayerThatIsHitByPoint(viewportPoint, m_renderSurfaceLayerList); |
| 1032 | |
| 1033 | // Walk up the hierarchy and look for a scrollable layer. |
| 1034 | CCLayerImpl* potentiallyScrollingLayerImpl = 0; |
| 1035 | for (; layerImpl; layerImpl = layerImpl->parent()) { |
| 1036 | // The content layer can also block attempts to scroll outside the main thread. |
[email protected] | 5c6fe1f8 | 2012-10-03 18:00:27 | [diff] [blame] | 1037 | if (layerImpl->tryScroll(deviceViewportPoint, type) == ScrollOnMainThread) { |
| 1038 | m_numMainThreadScrolls++; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1039 | return ScrollOnMainThread; |
[email protected] | 5c6fe1f8 | 2012-10-03 18:00:27 | [diff] [blame] | 1040 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1041 | |
| 1042 | CCLayerImpl* scrollLayerImpl = findScrollLayerForContentLayer(layerImpl); |
| 1043 | if (!scrollLayerImpl) |
| 1044 | continue; |
| 1045 | |
| 1046 | ScrollStatus status = scrollLayerImpl->tryScroll(viewportPoint, type); |
| 1047 | |
| 1048 | // If any layer wants to divert the scroll event to the main thread, abort. |
[email protected] | 5c6fe1f8 | 2012-10-03 18:00:27 | [diff] [blame] | 1049 | if (status == ScrollOnMainThread) { |
| 1050 | m_numMainThreadScrolls++; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1051 | return ScrollOnMainThread; |
[email protected] | 5c6fe1f8 | 2012-10-03 18:00:27 | [diff] [blame] | 1052 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1053 | |
| 1054 | if (status == ScrollStarted && !potentiallyScrollingLayerImpl) |
| 1055 | potentiallyScrollingLayerImpl = scrollLayerImpl; |
| 1056 | } |
| 1057 | |
| 1058 | if (potentiallyScrollingLayerImpl) { |
| 1059 | m_currentlyScrollingLayerImpl = potentiallyScrollingLayerImpl; |
| 1060 | // Gesture events need to be transformed from screen coordinates to local layer coordinates |
| 1061 | // so that the scrolling contents exactly follow the user's finger. In contrast, wheel |
| 1062 | // events are already in local layer coordinates so we can just apply them directly. |
| 1063 | m_scrollDeltaIsInScreenSpace = (type == Gesture); |
[email protected] | 5c6fe1f8 | 2012-10-03 18:00:27 | [diff] [blame] | 1064 | m_numImplThreadScrolls++; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1065 | return ScrollStarted; |
| 1066 | } |
| 1067 | return ScrollIgnored; |
| 1068 | } |
| 1069 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1070 | static FloatSize scrollLayerWithScreenSpaceDelta(CCPinchZoomViewport* viewport, CCLayerImpl& layerImpl, const FloatPoint& screenSpacePoint, const FloatSize& screenSpaceDelta) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1071 | { |
| 1072 | // Layers with non-invertible screen space transforms should not have passed the scroll hit |
| 1073 | // test in the first place. |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 1074 | DCHECK(layerImpl.screenSpaceTransform().isInvertible()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1075 | WebTransformationMatrix inverseScreenSpaceTransform = layerImpl.screenSpaceTransform().inverse(); |
| 1076 | |
| 1077 | // First project the scroll start and end points to local layer space to find the scroll delta |
| 1078 | // in layer coordinates. |
| 1079 | bool startClipped, endClipped; |
| 1080 | FloatPoint screenSpaceEndPoint = screenSpacePoint + screenSpaceDelta; |
| 1081 | FloatPoint localStartPoint = CCMathUtil::projectPoint(inverseScreenSpaceTransform, screenSpacePoint, startClipped); |
| 1082 | FloatPoint localEndPoint = CCMathUtil::projectPoint(inverseScreenSpaceTransform, screenSpaceEndPoint, endClipped); |
| 1083 | |
| 1084 | // In general scroll point coordinates should not get clipped. |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 1085 | DCHECK(!startClipped); |
| 1086 | DCHECK(!endClipped); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1087 | if (startClipped || endClipped) |
| 1088 | return FloatSize(); |
| 1089 | |
| 1090 | // Apply the scroll delta. |
| 1091 | FloatSize previousDelta(layerImpl.scrollDelta()); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1092 | FloatSize unscrolled = layerImpl.scrollBy(localEndPoint - localStartPoint); |
| 1093 | |
| 1094 | if (viewport) |
| 1095 | viewport->applyScroll(unscrolled); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1096 | |
| 1097 | // Calculate the applied scroll delta in screen space coordinates. |
| 1098 | FloatPoint actualLocalEndPoint = localStartPoint + layerImpl.scrollDelta() - previousDelta; |
| 1099 | FloatPoint actualScreenSpaceEndPoint = CCMathUtil::mapPoint(layerImpl.screenSpaceTransform(), actualLocalEndPoint, endClipped); |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 1100 | DCHECK(!endClipped); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1101 | if (endClipped) |
| 1102 | return FloatSize(); |
| 1103 | return actualScreenSpaceEndPoint - screenSpacePoint; |
| 1104 | } |
| 1105 | |
| 1106 | static FloatSize scrollLayerWithLocalDelta(CCLayerImpl& layerImpl, const FloatSize& localDelta) |
| 1107 | { |
| 1108 | FloatSize previousDelta(layerImpl.scrollDelta()); |
| 1109 | layerImpl.scrollBy(localDelta); |
| 1110 | return layerImpl.scrollDelta() - previousDelta; |
| 1111 | } |
| 1112 | |
| 1113 | void CCLayerTreeHostImpl::scrollBy(const IntPoint& viewportPoint, const IntSize& scrollDelta) |
| 1114 | { |
| 1115 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::scrollBy"); |
| 1116 | if (!m_currentlyScrollingLayerImpl) |
| 1117 | return; |
| 1118 | |
| 1119 | FloatSize pendingDelta(scrollDelta); |
| 1120 | |
| 1121 | pendingDelta.scale(m_deviceScaleFactor); |
| 1122 | |
| 1123 | for (CCLayerImpl* layerImpl = m_currentlyScrollingLayerImpl; layerImpl; layerImpl = layerImpl->parent()) { |
| 1124 | if (!layerImpl->scrollable()) |
| 1125 | continue; |
| 1126 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1127 | CCPinchZoomViewport* viewport = layerImpl == m_rootScrollLayerImpl ? &m_pinchZoomViewport : 0; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1128 | FloatSize appliedDelta; |
| 1129 | if (m_scrollDeltaIsInScreenSpace) |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1130 | appliedDelta = scrollLayerWithScreenSpaceDelta(viewport, *layerImpl, viewportPoint, pendingDelta); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1131 | else |
| 1132 | appliedDelta = scrollLayerWithLocalDelta(*layerImpl, pendingDelta); |
| 1133 | |
| 1134 | // If the layer wasn't able to move, try the next one in the hierarchy. |
[email protected] | 23bbb41 | 2012-08-30 20:03:38 | [diff] [blame] | 1135 | float moveThresholdSquared = 0.1f * 0.1f; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1136 | if (appliedDelta.diagonalLengthSquared() < moveThresholdSquared) |
| 1137 | continue; |
| 1138 | |
| 1139 | // If the applied delta is within 45 degrees of the input delta, bail out to make it easier |
| 1140 | // to scroll just one layer in one direction without affecting any of its parents. |
| 1141 | float angleThreshold = 45; |
| 1142 | if (CCMathUtil::smallestAngleBetweenVectors(appliedDelta, pendingDelta) < angleThreshold) { |
| 1143 | pendingDelta = FloatSize(); |
| 1144 | break; |
| 1145 | } |
| 1146 | |
| 1147 | // Allow further movement only on an axis perpendicular to the direction in which the layer |
| 1148 | // moved. |
| 1149 | FloatSize perpendicularAxis(-appliedDelta.height(), appliedDelta.width()); |
| 1150 | pendingDelta = CCMathUtil::projectVector(pendingDelta, perpendicularAxis); |
| 1151 | |
| 1152 | if (flooredIntSize(pendingDelta).isZero()) |
| 1153 | break; |
| 1154 | } |
| 1155 | |
| 1156 | if (!scrollDelta.isZero() && flooredIntSize(pendingDelta).isEmpty()) { |
| 1157 | m_client->setNeedsCommitOnImplThread(); |
| 1158 | m_client->setNeedsRedrawOnImplThread(); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | void CCLayerTreeHostImpl::clearCurrentlyScrollingLayer() |
| 1163 | { |
| 1164 | m_currentlyScrollingLayerImpl = 0; |
| 1165 | m_scrollingLayerIdFromPreviousTree = -1; |
| 1166 | } |
| 1167 | |
| 1168 | void CCLayerTreeHostImpl::scrollEnd() |
| 1169 | { |
| 1170 | clearCurrentlyScrollingLayer(); |
| 1171 | } |
| 1172 | |
| 1173 | void CCLayerTreeHostImpl::pinchGestureBegin() |
| 1174 | { |
| 1175 | m_pinchGestureActive = true; |
| 1176 | m_previousPinchAnchor = IntPoint(); |
| 1177 | |
| 1178 | if (m_rootScrollLayerImpl && m_rootScrollLayerImpl->scrollbarAnimationController()) |
| 1179 | m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureBegin(); |
| 1180 | } |
| 1181 | |
| 1182 | void CCLayerTreeHostImpl::pinchGestureUpdate(float magnifyDelta, |
| 1183 | const IntPoint& anchor) |
| 1184 | { |
| 1185 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::pinchGestureUpdate"); |
| 1186 | |
| 1187 | if (!m_rootScrollLayerImpl) |
| 1188 | return; |
| 1189 | |
| 1190 | if (m_previousPinchAnchor == IntPoint::zero()) |
| 1191 | m_previousPinchAnchor = anchor; |
| 1192 | |
| 1193 | // Keep the center-of-pinch anchor specified by (x, y) in a stable |
| 1194 | // position over the course of the magnify. |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1195 | float pageScaleDelta = m_pinchZoomViewport.pageScaleDelta(); |
| 1196 | FloatPoint previousScaleAnchor(m_previousPinchAnchor.x() / pageScaleDelta, |
| 1197 | m_previousPinchAnchor.y() / pageScaleDelta); |
| 1198 | setPageScaleDelta(pageScaleDelta * magnifyDelta); |
| 1199 | pageScaleDelta = m_pinchZoomViewport.pageScaleDelta(); |
| 1200 | FloatPoint newScaleAnchor(anchor.x() / pageScaleDelta, anchor.y() / pageScaleDelta); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1201 | FloatSize move = previousScaleAnchor - newScaleAnchor; |
| 1202 | |
| 1203 | m_previousPinchAnchor = anchor; |
| 1204 | |
[email protected] | 65bfd997 | 2012-10-19 03:39:37 | [diff] [blame] | 1205 | if (Settings::pageScalePinchZoomEnabled()) { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1206 | // Compute the application of the delta with respect to the current page zoom of the page. |
| 1207 | move.scale(1 / (m_pinchZoomViewport.pageScaleFactor() * m_deviceScaleFactor)); |
| 1208 | } |
| 1209 | |
[email protected] | 65bfd997 | 2012-10-19 03:39:37 | [diff] [blame] | 1210 | FloatSize scrollOverflow = Settings::pageScalePinchZoomEnabled() ? m_pinchZoomViewport.applyScroll(move) : move; |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1211 | m_rootScrollLayerImpl->scrollBy(roundedIntSize(scrollOverflow)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1212 | |
| 1213 | if (m_rootScrollLayerImpl->scrollbarAnimationController()) |
| 1214 | m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureUpdate(); |
| 1215 | |
| 1216 | m_client->setNeedsCommitOnImplThread(); |
| 1217 | m_client->setNeedsRedrawOnImplThread(); |
| 1218 | } |
| 1219 | |
| 1220 | void CCLayerTreeHostImpl::pinchGestureEnd() |
| 1221 | { |
| 1222 | m_pinchGestureActive = false; |
| 1223 | |
| 1224 | if (m_rootScrollLayerImpl && m_rootScrollLayerImpl->scrollbarAnimationController()) |
| 1225 | m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureEnd(); |
| 1226 | |
| 1227 | m_client->setNeedsCommitOnImplThread(); |
| 1228 | } |
| 1229 | |
| 1230 | void CCLayerTreeHostImpl::computeDoubleTapZoomDeltas(CCScrollAndScaleSet* scrollInfo) |
| 1231 | { |
| 1232 | float pageScale = m_pageScaleAnimation->finalPageScale(); |
| 1233 | IntSize scrollOffset = m_pageScaleAnimation->finalScrollOffset(); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1234 | scrollOffset.scale(m_pinchZoomViewport.pageScaleFactor() / pageScale); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1235 | makeScrollAndScaleSet(scrollInfo, scrollOffset, pageScale); |
| 1236 | } |
| 1237 | |
| 1238 | void CCLayerTreeHostImpl::computePinchZoomDeltas(CCScrollAndScaleSet* scrollInfo) |
| 1239 | { |
| 1240 | if (!m_rootScrollLayerImpl) |
| 1241 | return; |
| 1242 | |
| 1243 | // Only send fake scroll/zoom deltas if we're pinch zooming out by a |
| 1244 | // significant amount. This also ensures only one fake delta set will be |
| 1245 | // sent. |
[email protected] | 23bbb41 | 2012-08-30 20:03:38 | [diff] [blame] | 1246 | const float pinchZoomOutSensitivity = 0.95f; |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1247 | if (m_pinchZoomViewport.pageScaleDelta() > pinchZoomOutSensitivity) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1248 | return; |
| 1249 | |
| 1250 | // Compute where the scroll offset/page scale would be if fully pinch-zoomed |
| 1251 | // out from the anchor point. |
| 1252 | IntSize scrollBegin = flooredIntSize(m_rootScrollLayerImpl->scrollPosition() + m_rootScrollLayerImpl->scrollDelta()); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1253 | scrollBegin.scale(m_pinchZoomViewport.pageScaleDelta()); |
| 1254 | float scaleBegin = m_pinchZoomViewport.totalPageScaleFactor(); |
| 1255 | float pageScaleDeltaToSend = m_pinchZoomViewport.minPageScaleFactor() / m_pinchZoomViewport.pageScaleFactor(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1256 | FloatSize scaledContentsSize = contentSize(); |
| 1257 | scaledContentsSize.scale(pageScaleDeltaToSend); |
| 1258 | |
| 1259 | FloatSize anchor = toSize(m_previousPinchAnchor); |
| 1260 | FloatSize scrollEnd = scrollBegin + anchor; |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1261 | scrollEnd.scale(m_pinchZoomViewport.minPageScaleFactor() / scaleBegin); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1262 | scrollEnd -= anchor; |
| 1263 | scrollEnd = scrollEnd.shrunkTo(roundedIntSize(scaledContentsSize - m_deviceViewportSize)).expandedTo(FloatSize(0, 0)); |
| 1264 | scrollEnd.scale(1 / pageScaleDeltaToSend); |
| 1265 | scrollEnd.scale(m_deviceScaleFactor); |
| 1266 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1267 | makeScrollAndScaleSet(scrollInfo, roundedIntSize(scrollEnd), m_pinchZoomViewport.minPageScaleFactor()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | void CCLayerTreeHostImpl::makeScrollAndScaleSet(CCScrollAndScaleSet* scrollInfo, const IntSize& scrollOffset, float pageScale) |
| 1271 | { |
| 1272 | if (!m_rootScrollLayerImpl) |
| 1273 | return; |
| 1274 | |
| 1275 | CCLayerTreeHostCommon::ScrollUpdateInfo scroll; |
| 1276 | scroll.layerId = m_rootScrollLayerImpl->id(); |
| 1277 | scroll.scrollDelta = scrollOffset - toSize(m_rootScrollLayerImpl->scrollPosition()); |
| 1278 | scrollInfo->scrolls.append(scroll); |
| 1279 | m_rootScrollLayerImpl->setSentScrollDelta(scroll.scrollDelta); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1280 | scrollInfo->pageScaleDelta = pageScale / m_pinchZoomViewport.pageScaleFactor(); |
| 1281 | m_pinchZoomViewport.setSentPageScaleDelta(scrollInfo->pageScaleDelta); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static void collectScrollDeltas(CCScrollAndScaleSet* scrollInfo, CCLayerImpl* layerImpl) |
| 1285 | { |
| 1286 | if (!layerImpl) |
| 1287 | return; |
| 1288 | |
| 1289 | if (!layerImpl->scrollDelta().isZero()) { |
| 1290 | IntSize scrollDelta = flooredIntSize(layerImpl->scrollDelta()); |
| 1291 | CCLayerTreeHostCommon::ScrollUpdateInfo scroll; |
| 1292 | scroll.layerId = layerImpl->id(); |
| 1293 | scroll.scrollDelta = scrollDelta; |
| 1294 | scrollInfo->scrolls.append(scroll); |
| 1295 | layerImpl->setSentScrollDelta(scrollDelta); |
| 1296 | } |
| 1297 | |
| 1298 | for (size_t i = 0; i < layerImpl->children().size(); ++i) |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 1299 | collectScrollDeltas(scrollInfo, layerImpl->children()[i]); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1300 | } |
| 1301 | |
[email protected] | a9f4bf2 | 2012-10-11 23:39:21 | [diff] [blame] | 1302 | scoped_ptr<CCScrollAndScaleSet> CCLayerTreeHostImpl::processScrollDeltas() |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1303 | { |
[email protected] | a9f4bf2 | 2012-10-11 23:39:21 | [diff] [blame] | 1304 | scoped_ptr<CCScrollAndScaleSet> scrollInfo(new CCScrollAndScaleSet()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1305 | |
| 1306 | if (m_pinchGestureActive || m_pageScaleAnimation) { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1307 | scrollInfo->pageScaleDelta = 1; |
| 1308 | m_pinchZoomViewport.setSentPageScaleDelta(1); |
| 1309 | // FIXME(aelias): Make these painting optimizations compatible with |
| 1310 | // compositor-side scaling. |
[email protected] | 65bfd997 | 2012-10-19 03:39:37 | [diff] [blame] | 1311 | if (!Settings::pageScalePinchZoomEnabled()) { |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1312 | if (m_pinchGestureActive) |
| 1313 | computePinchZoomDeltas(scrollInfo.get()); |
| 1314 | else if (m_pageScaleAnimation.get()) |
| 1315 | computeDoubleTapZoomDeltas(scrollInfo.get()); |
| 1316 | } |
[email protected] | a9f4bf2 | 2012-10-11 23:39:21 | [diff] [blame] | 1317 | return scrollInfo.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | collectScrollDeltas(scrollInfo.get(), m_rootLayerImpl.get()); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1321 | scrollInfo->pageScaleDelta = m_pinchZoomViewport.pageScaleDelta(); |
| 1322 | m_pinchZoomViewport.setSentPageScaleDelta(scrollInfo->pageScaleDelta); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1323 | |
[email protected] | a9f4bf2 | 2012-10-11 23:39:21 | [diff] [blame] | 1324 | return scrollInfo.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1325 | } |
| 1326 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1327 | WebTransformationMatrix CCLayerTreeHostImpl::implTransform() const |
| 1328 | { |
| 1329 | return m_pinchZoomViewport.implTransform(); |
| 1330 | } |
| 1331 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1332 | void CCLayerTreeHostImpl::setFullRootLayerDamage() |
| 1333 | { |
| 1334 | if (m_rootLayerImpl) { |
| 1335 | CCRenderSurface* renderSurface = m_rootLayerImpl->renderSurface(); |
| 1336 | if (renderSurface) |
| 1337 | renderSurface->damageTracker()->forceFullDamageNextUpdate(); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | void CCLayerTreeHostImpl::animatePageScale(double monotonicTime) |
| 1342 | { |
| 1343 | if (!m_pageScaleAnimation || !m_rootScrollLayerImpl) |
| 1344 | return; |
| 1345 | |
| 1346 | IntSize scrollTotal = flooredIntSize(m_rootScrollLayerImpl->scrollPosition() + m_rootScrollLayerImpl->scrollDelta()); |
| 1347 | |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1348 | setPageScaleDelta(m_pageScaleAnimation->pageScaleAtTime(monotonicTime) / m_pinchZoomViewport.pageScaleFactor()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1349 | IntSize nextScroll = m_pageScaleAnimation->scrollOffsetAtTime(monotonicTime); |
[email protected] | 1c0c9bc | 2012-10-08 22:41:48 | [diff] [blame] | 1350 | nextScroll.scale(1 / m_pinchZoomViewport.pageScaleDelta()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1351 | m_rootScrollLayerImpl->scrollBy(nextScroll - scrollTotal); |
| 1352 | m_client->setNeedsRedrawOnImplThread(); |
| 1353 | |
| 1354 | if (m_pageScaleAnimation->isAnimationCompleteAtTime(monotonicTime)) { |
[email protected] | 0023e8b | 2012-10-15 12:52:45 | [diff] [blame] | 1355 | m_pageScaleAnimation.reset(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1356 | m_client->setNeedsCommitOnImplThread(); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | void CCLayerTreeHostImpl::animateLayers(double monotonicTime, double wallClockTime) |
| 1361 | { |
[email protected] | 65bfd997 | 2012-10-19 03:39:37 | [diff] [blame] | 1362 | if (!Settings::acceleratedAnimationEnabled() || !m_needsAnimateLayers || !m_rootLayerImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1363 | return; |
| 1364 | |
| 1365 | TRACE_EVENT0("cc", "CCLayerTreeHostImpl::animateLayers"); |
| 1366 | |
[email protected] | ec1d6d5 | 2012-10-10 01:28:57 | [diff] [blame] | 1367 | scoped_ptr<CCAnimationEventsVector> events(make_scoped_ptr(new CCAnimationEventsVector)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1368 | |
| 1369 | bool didAnimate = false; |
| 1370 | animateLayersRecursive(m_rootLayerImpl.get(), monotonicTime, wallClockTime, events.get(), didAnimate, m_needsAnimateLayers); |
| 1371 | |
[email protected] | d3143c73 | 2012-10-05 19:17:59 | [diff] [blame] | 1372 | if (!events->empty()) |
[email protected] | ec1d6d5 | 2012-10-10 01:28:57 | [diff] [blame] | 1373 | m_client->postAnimationEventsToMainThreadOnImplThread(events.Pass(), wallClockTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1374 | |
| 1375 | if (didAnimate) |
| 1376 | m_client->setNeedsRedrawOnImplThread(); |
| 1377 | |
| 1378 | setBackgroundTickingEnabled(!m_visible && m_needsAnimateLayers); |
| 1379 | } |
| 1380 | |
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 1381 | base::TimeDelta CCLayerTreeHostImpl::lowFrequencyAnimationInterval() const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1382 | { |
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 1383 | return base::TimeDelta::FromSeconds(1); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | void CCLayerTreeHostImpl::sendDidLoseContextRecursive(CCLayerImpl* current) |
| 1387 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 1388 | DCHECK(current); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1389 | current->didLoseContext(); |
| 1390 | if (current->maskLayer()) |
| 1391 | sendDidLoseContextRecursive(current->maskLayer()); |
| 1392 | if (current->replicaLayer()) |
| 1393 | sendDidLoseContextRecursive(current->replicaLayer()); |
| 1394 | for (size_t i = 0; i < current->children().size(); ++i) |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 1395 | sendDidLoseContextRecursive(current->children()[i]); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | static void clearRenderSurfacesOnCCLayerImplRecursive(CCLayerImpl* current) |
| 1399 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 1400 | DCHECK(current); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1401 | for (size_t i = 0; i < current->children().size(); ++i) |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 1402 | clearRenderSurfacesOnCCLayerImplRecursive(current->children()[i]); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1403 | current->clearRenderSurface(); |
| 1404 | } |
| 1405 | |
| 1406 | void CCLayerTreeHostImpl::clearRenderSurfaces() |
| 1407 | { |
| 1408 | clearRenderSurfacesOnCCLayerImplRecursive(m_rootLayerImpl.get()); |
| 1409 | m_renderSurfaceLayerList.clear(); |
| 1410 | } |
| 1411 | |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 1412 | std::string CCLayerTreeHostImpl::layerTreeAsText() const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1413 | { |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 1414 | std::string str; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1415 | if (m_rootLayerImpl) { |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 1416 | str = m_rootLayerImpl->layerTreeAsText(); |
| 1417 | str += "RenderSurfaces:\n"; |
| 1418 | dumpRenderSurfaces(&str, 1, m_rootLayerImpl.get()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1419 | } |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 1420 | return str; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1421 | } |
| 1422 | |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 1423 | void CCLayerTreeHostImpl::dumpRenderSurfaces(std::string* str, int indent, const CCLayerImpl* layer) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1424 | { |
| 1425 | if (layer->renderSurface()) |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 1426 | layer->renderSurface()->dumpSurface(str, indent); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1427 | |
| 1428 | for (size_t i = 0; i < layer->children().size(); ++i) |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 1429 | dumpRenderSurfaces(str, indent, layer->children()[i]); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1430 | } |
| 1431 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1432 | int CCLayerTreeHostImpl::sourceAnimationFrameNumber() const |
| 1433 | { |
| 1434 | return fpsCounter()->currentFrameNumber(); |
| 1435 | } |
| 1436 | |
[email protected] | 8b9af6b | 2012-09-27 00:36:36 | [diff] [blame] | 1437 | void CCLayerTreeHostImpl::renderingStats(CCRenderingStats* stats) const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1438 | { |
[email protected] | 8b9af6b | 2012-09-27 00:36:36 | [diff] [blame] | 1439 | stats->numFramesSentToScreen = fpsCounter()->currentFrameNumber(); |
| 1440 | stats->droppedFrameCount = fpsCounter()->droppedFrameCount(); |
[email protected] | 5c6fe1f8 | 2012-10-03 18:00:27 | [diff] [blame] | 1441 | stats->numImplThreadScrolls = m_numImplThreadScrolls; |
| 1442 | stats->numMainThreadScrolls = m_numMainThreadScrolls; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | void CCLayerTreeHostImpl::animateScrollbars(double monotonicTime) |
| 1446 | { |
| 1447 | animateScrollbarsRecursive(m_rootLayerImpl.get(), monotonicTime); |
| 1448 | } |
| 1449 | |
| 1450 | void CCLayerTreeHostImpl::animateScrollbarsRecursive(CCLayerImpl* layer, double monotonicTime) |
| 1451 | { |
| 1452 | if (!layer) |
| 1453 | return; |
| 1454 | |
| 1455 | CCScrollbarAnimationController* scrollbarController = layer->scrollbarAnimationController(); |
| 1456 | if (scrollbarController && scrollbarController->animate(monotonicTime)) |
| 1457 | m_client->setNeedsRedrawOnImplThread(); |
| 1458 | |
| 1459 | for (size_t i = 0; i < layer->children().size(); ++i) |
[email protected] | 0920e24f | 2012-09-20 03:34:03 | [diff] [blame] | 1460 | animateScrollbarsRecursive(layer->children()[i], monotonicTime); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1461 | } |
| 1462 | |
[email protected] | d3143c73 | 2012-10-05 19:17:59 | [diff] [blame] | 1463 | } // namespace cc |