blob: d228ba1edad376961c674eb518f8ee7bceecc218 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// 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
[email protected]55a124d02012-10-22 03:07:137#include "cc/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:148
[email protected]ad5d1422012-10-19 13:40:299#include "base/basictypes.h"
[email protected]4456eee22012-10-19 18:16:3810#include "base/debug/trace_event.h"
[email protected]aa0a9d32012-10-24 01:58:1011#include "cc/append_quads_data.h"
12#include "cc/damage_tracker.h"
13#include "cc/debug_rect_history.h"
14#include "cc/delay_based_time_source.h"
15#include "cc/font_atlas.h"
16#include "cc/frame_rate_counter.h"
[email protected]c4040a522012-10-21 15:01:4017#include "cc/gl_renderer.h"
[email protected]d50c6862012-10-23 02:08:3118#include "cc/heads_up_display_layer_impl.h"
19#include "cc/layer_iterator.h"
20#include "cc/layer_tree_host.h"
21#include "cc/layer_tree_host_common.h"
[email protected]55a124d02012-10-22 03:07:1322#include "cc/math_util.h"
23#include "cc/overdraw_metrics.h"
24#include "cc/page_scale_animation.h"
25#include "cc/prioritized_texture_manager.h"
26#include "cc/render_pass_draw_quad.h"
[email protected]c4040a522012-10-21 15:01:4027#include "cc/rendering_stats.h"
28#include "cc/scrollbar_animation_controller.h"
29#include "cc/scrollbar_layer_impl.h"
30#include "cc/settings.h"
[email protected]4456eee22012-10-19 18:16:3831#include "cc/single_thread_proxy.h"
[email protected]c4040a522012-10-21 15:01:4032#include "cc/software_renderer.h"
[email protected]a8461d82012-10-16 21:11:1433#include "cc/texture_uploader.h"
[email protected]94f206c12012-08-25 00:09:1434#include <wtf/CurrentTime.h>
[email protected]f8ad8342012-09-27 20:07:0235#include <algorithm>
[email protected]94f206c12012-08-25 00:09:1436
37using WebKit::WebTransformationMatrix;
38
39namespace {
40
[email protected]96baf3e2012-10-22 23:09:5541void didVisibilityChange(cc::LayerTreeHostImpl* id, bool visible)
[email protected]94f206c12012-08-25 00:09:1442{
43 if (visible) {
[email protected]96baf3e2012-10-22 23:09:5544 TRACE_EVENT_ASYNC_BEGIN1("webkit", "LayerTreeHostImpl::setVisible", id, "LayerTreeHostImpl", id);
[email protected]94f206c12012-08-25 00:09:1445 return;
46 }
47
[email protected]96baf3e2012-10-22 23:09:5548 TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::setVisible", id);
[email protected]94f206c12012-08-25 00:09:1449}
50
51} // namespace
52
[email protected]9c88e562012-09-14 22:21:3053namespace cc {
[email protected]94f206c12012-08-25 00:09:1454
[email protected]96baf3e2012-10-22 23:09:5555PinchZoomViewport::PinchZoomViewport()
[email protected]1c0c9bc2012-10-08 22:41:4856 : m_pageScaleFactor(1)
57 , m_pageScaleDelta(1)
58 , m_sentPageScaleDelta(1)
59 , m_minPageScaleFactor(0)
60 , m_maxPageScaleFactor(0)
61{
62}
63
[email protected]96baf3e2012-10-22 23:09:5564float PinchZoomViewport::totalPageScaleFactor() const
[email protected]1c0c9bc2012-10-08 22:41:4865{
66 return m_pageScaleFactor * m_pageScaleDelta;
67}
68
[email protected]96baf3e2012-10-22 23:09:5569void PinchZoomViewport::setPageScaleDelta(float delta)
[email protected]1c0c9bc2012-10-08 22:41:4870{
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
[email protected]96baf3e2012-10-22 23:09:5584bool PinchZoomViewport::setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor)
[email protected]1c0c9bc2012-10-08 22:41:4885{
[email protected]1d993172012-10-18 18:15:0486 DCHECK(pageScaleFactor);
[email protected]1c0c9bc2012-10-08 22:41:4887
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
[email protected]96baf3e2012-10-22 23:09:5598FloatRect PinchZoomViewport::bounds() const
[email protected]1c0c9bc2012-10-08 22:41:4899{
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
[email protected]96baf3e2012-10-22 23:09:55109FloatSize PinchZoomViewport::applyScroll(FloatSize& delta)
[email protected]1c0c9bc2012-10-08 22:41:48110{
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
[email protected]96baf3e2012-10-22 23:09:55143WebTransformationMatrix PinchZoomViewport::implTransform() const
[email protected]1c0c9bc2012-10-08 22:41:48144{
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]65bfd9972012-10-19 03:39:37150 if (Settings::pageScalePinchZoomEnabled()) {
[email protected]1c0c9bc2012-10-08 22:41:48151 transform.scale(m_pageScaleFactor);
152 transform.translate(-m_pinchViewportScrollDelta.x(),
153 -m_pinchViewportScrollDelta.y());
154 }
155
156 return transform;
157}
158
[email protected]96baf3e2012-10-22 23:09:55159class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]94f206c12012-08-25 00:09:14160public:
[email protected]96baf3e2012-10-22 23:09:55161 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> create(LayerTreeHostImpl* layerTreeHostImpl, scoped_refptr<DelayBasedTimeSource> timeSource)
[email protected]94f206c12012-08-25 00:09:14162 {
[email protected]96baf3e2012-10-22 23:09:55163 return make_scoped_ptr(new LayerTreeHostImplTimeSourceAdapter(layerTreeHostImpl, timeSource));
[email protected]94f206c12012-08-25 00:09:14164 }
[email protected]96baf3e2012-10-22 23:09:55165 virtual ~LayerTreeHostImplTimeSourceAdapter()
[email protected]94f206c12012-08-25 00:09:14166 {
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
187private:
[email protected]96baf3e2012-10-22 23:09:55188 LayerTreeHostImplTimeSourceAdapter(LayerTreeHostImpl* layerTreeHostImpl, scoped_refptr<DelayBasedTimeSource> timeSource)
[email protected]94f206c12012-08-25 00:09:14189 : m_layerTreeHostImpl(layerTreeHostImpl)
190 , m_timeSource(timeSource)
191 {
192 m_timeSource->setClient(this);
193 }
194
[email protected]96baf3e2012-10-22 23:09:55195 LayerTreeHostImpl* m_layerTreeHostImpl;
196 scoped_refptr<DelayBasedTimeSource> m_timeSource;
[email protected]fd2d4f22012-09-28 22:57:20197
[email protected]96baf3e2012-10-22 23:09:55198 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14199};
200
[email protected]96baf3e2012-10-22 23:09:55201LayerTreeHostImpl::FrameData::FrameData()
[email protected]493067512012-09-19 23:34:10202{
203}
204
[email protected]96baf3e2012-10-22 23:09:55205LayerTreeHostImpl::FrameData::~FrameData()
[email protected]493067512012-09-19 23:34:10206{
207}
208
[email protected]96baf3e2012-10-22 23:09:55209scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::create(const LayerTreeSettings& settings, LayerTreeHostImplClient* client)
[email protected]94f206c12012-08-25 00:09:14210{
[email protected]96baf3e2012-10-22 23:09:55211 return make_scoped_ptr(new LayerTreeHostImpl(settings, client));
[email protected]94f206c12012-08-25 00:09:14212}
213
[email protected]96baf3e2012-10-22 23:09:55214LayerTreeHostImpl::LayerTreeHostImpl(const LayerTreeSettings& settings, LayerTreeHostImplClient* client)
[email protected]94f206c12012-08-25 00:09:14215 : m_client(client)
216 , m_sourceFrameNumber(-1)
217 , m_rootScrollLayerImpl(0)
218 , m_currentlyScrollingLayerImpl(0)
219 , m_hudLayerImpl(0)
220 , m_scrollingLayerIdFromPreviousTree(-1)
[email protected]31bfe272012-10-19 18:49:52221 , m_scrollDeltaIsInViewportSpace(false)
[email protected]94f206c12012-08-25 00:09:14222 , m_settings(settings)
223 , m_deviceScaleFactor(1)
224 , m_visible(true)
225 , m_contentsTexturesPurged(false)
[email protected]96baf3e2012-10-22 23:09:55226 , m_managedMemoryPolicy(PrioritizedTextureManager::defaultMemoryAllocationLimit(),
227 PriorityCalculator::allowEverythingCutoff(),
[email protected]a0a00842012-10-22 22:50:28228 0,
[email protected]96baf3e2012-10-22 23:09:55229 PriorityCalculator::allowNothingCutoff())
[email protected]94f206c12012-08-25 00:09:14230 , m_backgroundColor(0)
231 , m_hasTransparentBackground(false)
232 , m_needsAnimateLayers(false)
233 , m_pinchGestureActive(false)
[email protected]96baf3e2012-10-22 23:09:55234 , m_fpsCounter(FrameRateCounter::create())
235 , m_debugRectHistory(DebugRectHistory::create())
[email protected]5c6fe1f82012-10-03 18:00:27236 , m_numImplThreadScrolls(0)
237 , m_numMainThreadScrolls(0)
[email protected]94f206c12012-08-25 00:09:14238{
[email protected]96baf3e2012-10-22 23:09:55239 DCHECK(Proxy::isImplThread());
[email protected]94f206c12012-08-25 00:09:14240 didVisibilityChange(this, m_visible);
241}
242
[email protected]96baf3e2012-10-22 23:09:55243LayerTreeHostImpl::~LayerTreeHostImpl()
[email protected]94f206c12012-08-25 00:09:14244{
[email protected]96baf3e2012-10-22 23:09:55245 DCHECK(Proxy::isImplThread());
246 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]94f206c12012-08-25 00:09:14247
248 if (m_rootLayerImpl)
249 clearRenderSurfaces();
250}
251
[email protected]96baf3e2012-10-22 23:09:55252void LayerTreeHostImpl::beginCommit()
[email protected]94f206c12012-08-25 00:09:14253{
254}
255
[email protected]96baf3e2012-10-22 23:09:55256void LayerTreeHostImpl::commitComplete()
[email protected]94f206c12012-08-25 00:09:14257{
[email protected]96baf3e2012-10-22 23:09:55258 TRACE_EVENT0("cc", "LayerTreeHostImpl::commitComplete");
[email protected]94f206c12012-08-25 00:09:14259 // Recompute max scroll position; must be after layer content bounds are
260 // updated.
261 updateMaxScrollPosition();
262}
263
[email protected]96baf3e2012-10-22 23:09:55264bool LayerTreeHostImpl::canDraw()
[email protected]94f206c12012-08-25 00:09:14265{
[email protected]8db2213c2012-09-05 22:08:21266 // Note: If you are changing this function or any other function that might
267 // affect the result of canDraw, make sure to call m_client->onCanDrawStateChanged
268 // in the proper places and update the notifyIfCanDrawChanged test.
269
[email protected]94f206c12012-08-25 00:09:14270 if (!m_rootLayerImpl) {
[email protected]96baf3e2012-10-22 23:09:55271 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw no root layer");
[email protected]94f206c12012-08-25 00:09:14272 return false;
273 }
274 if (deviceViewportSize().isEmpty()) {
[email protected]96baf3e2012-10-22 23:09:55275 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw empty viewport");
[email protected]94f206c12012-08-25 00:09:14276 return false;
277 }
278 if (!m_renderer) {
[email protected]96baf3e2012-10-22 23:09:55279 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw no renderer");
[email protected]94f206c12012-08-25 00:09:14280 return false;
281 }
282 if (m_contentsTexturesPurged) {
[email protected]96baf3e2012-10-22 23:09:55283 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw contents textures purged");
[email protected]94f206c12012-08-25 00:09:14284 return false;
285 }
286 return true;
287}
288
[email protected]96baf3e2012-10-22 23:09:55289GraphicsContext* LayerTreeHostImpl::context() const
[email protected]94f206c12012-08-25 00:09:14290{
291 return m_context.get();
292}
293
[email protected]96baf3e2012-10-22 23:09:55294void LayerTreeHostImpl::animate(double monotonicTime, double wallClockTime)
[email protected]94f206c12012-08-25 00:09:14295{
296 animatePageScale(monotonicTime);
297 animateLayers(monotonicTime, wallClockTime);
[email protected]94f206c12012-08-25 00:09:14298 animateScrollbars(monotonicTime);
299}
300
[email protected]96baf3e2012-10-22 23:09:55301void LayerTreeHostImpl::startPageScaleAnimation(const IntSize& targetPosition, bool anchorPoint, float pageScale, double startTime, double duration)
[email protected]94f206c12012-08-25 00:09:14302{
303 if (!m_rootScrollLayerImpl)
304 return;
305
306 IntSize scrollTotal = flooredIntSize(m_rootScrollLayerImpl->scrollPosition() + m_rootScrollLayerImpl->scrollDelta());
[email protected]1c0c9bc2012-10-08 22:41:48307 scrollTotal.scale(m_pinchZoomViewport.pageScaleDelta());
308 float scaleTotal = m_pinchZoomViewport.totalPageScaleFactor();
[email protected]94f206c12012-08-25 00:09:14309 IntSize scaledContentSize = contentSize();
[email protected]1c0c9bc2012-10-08 22:41:48310 scaledContentSize.scale(m_pinchZoomViewport.pageScaleDelta());
[email protected]94f206c12012-08-25 00:09:14311
[email protected]96baf3e2012-10-22 23:09:55312 m_pageScaleAnimation = PageScaleAnimation::create(scrollTotal, scaleTotal, m_deviceViewportSize, scaledContentSize, startTime);
[email protected]94f206c12012-08-25 00:09:14313
314 if (anchorPoint) {
315 IntSize windowAnchor(targetPosition);
316 windowAnchor.scale(scaleTotal / pageScale);
317 windowAnchor -= scrollTotal;
318 m_pageScaleAnimation->zoomWithAnchor(windowAnchor, pageScale, duration);
319 } else
320 m_pageScaleAnimation->zoomTo(targetPosition, pageScale, duration);
321
322 m_client->setNeedsRedrawOnImplThread();
323 m_client->setNeedsCommitOnImplThread();
324}
325
[email protected]96baf3e2012-10-22 23:09:55326void LayerTreeHostImpl::scheduleAnimation()
[email protected]94f206c12012-08-25 00:09:14327{
328 m_client->setNeedsRedrawOnImplThread();
329}
330
[email protected]96baf3e2012-10-22 23:09:55331void LayerTreeHostImpl::trackDamageForAllSurfaces(LayerImpl* rootDrawLayer, const LayerList& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14332{
333 // For now, we use damage tracking to compute a global scissor. To do this, we must
334 // compute all damage tracking before drawing anything, so that we know the root
335 // damage rect. The root damage rect is then used to scissor each surface.
336
337 for (int surfaceIndex = renderSurfaceLayerList.size() - 1; surfaceIndex >= 0 ; --surfaceIndex) {
[email protected]96baf3e2012-10-22 23:09:55338 LayerImpl* renderSurfaceLayer = renderSurfaceLayerList[surfaceIndex];
339 RenderSurfaceImpl* renderSurface = renderSurfaceLayer->renderSurface();
[email protected]1d993172012-10-18 18:15:04340 DCHECK(renderSurface);
[email protected]4000abf2012-10-23 04:45:45341 renderSurface->damageTracker()->updateDamageTrackingState(renderSurface->layerList(), renderSurfaceLayer->id(), renderSurface->surfacePropertyChangedOnlyFromDescendant(), renderSurface->contentRect(), renderSurfaceLayer->maskLayer(), renderSurfaceLayer->filters(), renderSurfaceLayer->filter());
[email protected]94f206c12012-08-25 00:09:14342 }
343}
344
[email protected]96baf3e2012-10-22 23:09:55345void LayerTreeHostImpl::updateRootScrollLayerImplTransform()
[email protected]1c0c9bc2012-10-08 22:41:48346{
347 if (m_rootScrollLayerImpl) {
348 m_rootScrollLayerImpl->setImplTransform(implTransform());
349 }
350}
351
[email protected]96baf3e2012-10-22 23:09:55352void LayerTreeHostImpl::calculateRenderSurfaceLayerList(LayerList& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14353{
[email protected]1d993172012-10-18 18:15:04354 DCHECK(renderSurfaceLayerList.empty());
355 DCHECK(m_rootLayerImpl);
356 DCHECK(m_renderer); // For maxTextureSize.
[email protected]94f206c12012-08-25 00:09:14357
358 {
[email protected]1c0c9bc2012-10-08 22:41:48359 updateRootScrollLayerImplTransform();
360
[email protected]96baf3e2012-10-22 23:09:55361 TRACE_EVENT0("cc", "LayerTreeHostImpl::calcDrawEtc");
[email protected]518ee582012-10-24 18:29:44362 float pageScaleFactor = m_pinchZoomViewport.pageScaleFactor();
363 LayerTreeHostCommon::calculateDrawTransforms(m_rootLayerImpl.get(), deviceViewportSize(), m_deviceScaleFactor, pageScaleFactor, &m_layerSorter, rendererCapabilities().maxTextureSize, renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14364
365 trackDamageForAllSurfaces(m_rootLayerImpl.get(), renderSurfaceLayerList);
366 }
367}
368
[email protected]96baf3e2012-10-22 23:09:55369void LayerTreeHostImpl::FrameData::appendRenderPass(scoped_ptr<RenderPass> renderPass)
[email protected]467b3612012-08-28 07:41:16370{
[email protected]96baf3e2012-10-22 23:09:55371 RenderPass* pass = renderPass.get();
[email protected]f8ad8342012-09-27 20:07:02372 renderPasses.push_back(pass);
[email protected]87cea5372012-09-26 18:59:56373 renderPassesById.set(pass->id(), renderPass.Pass());
[email protected]467b3612012-08-28 07:41:16374}
375
[email protected]96baf3e2012-10-22 23:09:55376bool LayerTreeHostImpl::calculateRenderPasses(FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14377{
[email protected]1d993172012-10-18 18:15:04378 DCHECK(frame.renderPasses.empty());
[email protected]94f206c12012-08-25 00:09:14379
380 calculateRenderSurfaceLayerList(*frame.renderSurfaceLayerList);
381
[email protected]96baf3e2012-10-22 23:09:55382 TRACE_EVENT1("cc", "LayerTreeHostImpl::calculateRenderPasses", "renderSurfaceLayerList.size()", static_cast<long long unsigned>(frame.renderSurfaceLayerList->size()));
[email protected]94f206c12012-08-25 00:09:14383
384 // Create the render passes in dependency order.
[email protected]94f206c12012-08-25 00:09:14385 for (int surfaceIndex = frame.renderSurfaceLayerList->size() - 1; surfaceIndex >= 0 ; --surfaceIndex) {
[email protected]96baf3e2012-10-22 23:09:55386 LayerImpl* renderSurfaceLayer = (*frame.renderSurfaceLayerList)[surfaceIndex];
[email protected]467b3612012-08-28 07:41:16387 renderSurfaceLayer->renderSurface()->appendRenderPasses(frame);
[email protected]94f206c12012-08-25 00:09:14388 }
389
390 bool recordMetricsForFrame = true; // FIXME: In the future, disable this when about:tracing is off.
[email protected]96baf3e2012-10-22 23:09:55391 OcclusionTrackerImpl occlusionTracker(m_rootLayerImpl->renderSurface()->contentRect(), recordMetricsForFrame);
[email protected]94f206c12012-08-25 00:09:14392 occlusionTracker.setMinimumTrackingSize(m_settings.minimumOcclusionTrackingSize);
393
394 if (settings().showOccludingRects)
395 occlusionTracker.setOccludingScreenSpaceRectsContainer(&frame.occludingScreenSpaceRects);
396
397 // Add quads to the Render passes in FrontToBack order to allow for testing occlusion and performing culling during the tree walk.
[email protected]96baf3e2012-10-22 23:09:55398 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14399
400 // Typically when we are missing a texture and use a checkerboard quad, we still draw the frame. However when the layer being
401 // checkerboarded is moving due to an impl-animation, we drop the frame to avoid flashing due to the texture suddenly appearing
402 // in the future.
403 bool drawFrame = true;
404
[email protected]96baf3e2012-10-22 23:09:55405 LayerIteratorType end = LayerIteratorType::end(frame.renderSurfaceLayerList);
406 for (LayerIteratorType it = LayerIteratorType::begin(frame.renderSurfaceLayerList); it != end; ++it) {
407 RenderPass::Id targetRenderPassId = it.targetRenderSurfaceLayer()->renderSurface()->renderPassId();
408 RenderPass* targetRenderPass = frame.renderPassesById.get(targetRenderPassId);
[email protected]94f206c12012-08-25 00:09:14409
410 occlusionTracker.enterLayer(it);
411
[email protected]96baf3e2012-10-22 23:09:55412 AppendQuadsData appendQuadsData(targetRenderPass->id());
[email protected]89228202012-08-29 03:20:30413
[email protected]94f206c12012-08-25 00:09:14414 if (it.representsContributingRenderSurface()) {
[email protected]96baf3e2012-10-22 23:09:55415 RenderPass::Id contributingRenderPassId = it->renderSurface()->renderPassId();
416 RenderPass* contributingRenderPass = frame.renderPassesById.get(contributingRenderPassId);
[email protected]89228202012-08-29 03:20:30417 targetRenderPass->appendQuadsForRenderSurfaceLayer(*it, contributingRenderPass, &occlusionTracker, appendQuadsData);
[email protected]94f206c12012-08-25 00:09:14418 } else if (it.representsItself() && !it->visibleContentRect().isEmpty()) {
419 bool hasOcclusionFromOutsideTargetSurface;
[email protected]89228202012-08-29 03:20:30420 if (occlusionTracker.occluded(*it, it->visibleContentRect(), &hasOcclusionFromOutsideTargetSurface))
421 appendQuadsData.hadOcclusionFromOutsideTargetSurface |= hasOcclusionFromOutsideTargetSurface;
422 else {
[email protected]94f206c12012-08-25 00:09:14423 it->willDraw(m_resourceProvider.get());
[email protected]d58499a2012-10-09 22:27:47424 frame.willDrawLayers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57425
426 if (it->hasContributingDelegatedRenderPasses()) {
[email protected]96baf3e2012-10-22 23:09:55427 RenderPass::Id contributingRenderPassId = it->firstContributingRenderPassId();
[email protected]7d929c02012-09-20 17:26:57428 while (frame.renderPassesById.contains(contributingRenderPassId)) {
[email protected]96baf3e2012-10-22 23:09:55429 RenderPass* renderPass = frame.renderPassesById.get(contributingRenderPassId);
[email protected]7d929c02012-09-20 17:26:57430
[email protected]96baf3e2012-10-22 23:09:55431 AppendQuadsData appendQuadsData(renderPass->id());
[email protected]7d929c02012-09-20 17:26:57432 renderPass->appendQuadsForLayer(*it, &occlusionTracker, appendQuadsData);
433
434 contributingRenderPassId = it->nextContributingRenderPassId(contributingRenderPassId);
435 }
436 }
437
[email protected]89228202012-08-29 03:20:30438 targetRenderPass->appendQuadsForLayer(*it, &occlusionTracker, appendQuadsData);
[email protected]94f206c12012-08-25 00:09:14439 }
440 }
441
[email protected]89228202012-08-29 03:20:30442 if (appendQuadsData.hadOcclusionFromOutsideTargetSurface)
443 targetRenderPass->setHasOcclusionFromOutsideTargetSurface(true);
444
445 if (appendQuadsData.hadMissingTiles) {
[email protected]94f206c12012-08-25 00:09:14446 bool layerHasAnimatingTransform = it->screenSpaceTransformIsAnimating() || it->drawTransformIsAnimating();
[email protected]65bfd9972012-10-19 03:39:37447 if (layerHasAnimatingTransform || Settings::jankInsteadOfCheckerboard())
[email protected]94f206c12012-08-25 00:09:14448 drawFrame = false;
449 }
450
451 occlusionTracker.leaveLayer(it);
452 }
453
[email protected]1d993172012-10-18 18:15:04454#ifndef NDEBUG
[email protected]94f206c12012-08-25 00:09:14455 for (size_t i = 0; i < frame.renderPasses.size(); ++i) {
[email protected]1d993172012-10-18 18:15:04456 for (size_t j = 0; j < frame.renderPasses[i]->quadList().size(); ++j)
457 DCHECK(frame.renderPasses[i]->quadList()[j]->sharedQuadStateId() >= 0);
458 DCHECK(frame.renderPassesById.contains(frame.renderPasses[i]->id()));
[email protected]94f206c12012-08-25 00:09:14459 }
460#endif
461
462 if (!m_hasTransparentBackground) {
[email protected]f8ad8342012-09-27 20:07:02463 frame.renderPasses.back()->setHasTransparentBackground(false);
464 frame.renderPasses.back()->appendQuadsToFillScreen(m_rootLayerImpl.get(), m_backgroundColor, occlusionTracker);
[email protected]94f206c12012-08-25 00:09:14465 }
466
467 if (drawFrame)
468 occlusionTracker.overdrawMetrics().recordMetrics(this);
469
470 removeRenderPasses(CullRenderPassesWithNoQuads(), frame);
471 m_renderer->decideRenderPassAllocationsForFrame(frame.renderPasses);
472 removeRenderPasses(CullRenderPassesWithCachedTextures(*m_renderer), frame);
473
474 return drawFrame;
475}
476
[email protected]96baf3e2012-10-22 23:09:55477void LayerTreeHostImpl::animateLayersRecursive(LayerImpl* current, double monotonicTime, double wallClockTime, AnimationEventsVector* events, bool& didAnimate, bool& needsAnimateLayers)
[email protected]94f206c12012-08-25 00:09:14478{
479 bool subtreeNeedsAnimateLayers = false;
480
[email protected]96baf3e2012-10-22 23:09:55481 LayerAnimationController* currentController = current->layerAnimationController();
[email protected]94f206c12012-08-25 00:09:14482
483 bool hadActiveAnimation = currentController->hasActiveAnimation();
484 currentController->animate(monotonicTime, events);
485 bool startedAnimation = events->size() > 0;
486
487 // We animated if we either ticked a running animation, or started a new animation.
488 if (hadActiveAnimation || startedAnimation)
489 didAnimate = true;
490
491 // If the current controller still has an active animation, we must continue animating layers.
492 if (currentController->hasActiveAnimation())
493 subtreeNeedsAnimateLayers = true;
494
495 for (size_t i = 0; i < current->children().size(); ++i) {
496 bool childNeedsAnimateLayers = false;
[email protected]0920e24f2012-09-20 03:34:03497 animateLayersRecursive(current->children()[i], monotonicTime, wallClockTime, events, didAnimate, childNeedsAnimateLayers);
[email protected]94f206c12012-08-25 00:09:14498 if (childNeedsAnimateLayers)
499 subtreeNeedsAnimateLayers = true;
500 }
501
502 needsAnimateLayers = subtreeNeedsAnimateLayers;
503}
504
[email protected]96baf3e2012-10-22 23:09:55505void LayerTreeHostImpl::setBackgroundTickingEnabled(bool enabled)
[email protected]94f206c12012-08-25 00:09:14506{
507 // Lazily create the timeSource adapter so that we can vary the interval for testing.
508 if (!m_timeSourceClientAdapter)
[email protected]96baf3e2012-10-22 23:09:55509 m_timeSourceClientAdapter = LayerTreeHostImplTimeSourceAdapter::create(this, DelayBasedTimeSource::create(lowFrequencyAnimationInterval(), Proxy::currentThread()));
[email protected]94f206c12012-08-25 00:09:14510
511 m_timeSourceClientAdapter->setActive(enabled);
512}
513
[email protected]96baf3e2012-10-22 23:09:55514IntSize LayerTreeHostImpl::contentSize() const
[email protected]94f206c12012-08-25 00:09:14515{
516 // TODO(aelias): Hardcoding the first child here is weird. Think of
517 // a cleaner way to get the contentBounds on the Impl side.
518 if (!m_rootScrollLayerImpl || m_rootScrollLayerImpl->children().isEmpty())
519 return IntSize();
520 return m_rootScrollLayerImpl->children()[0]->contentBounds();
521}
522
[email protected]96baf3e2012-10-22 23:09:55523static inline RenderPass* findRenderPassById(RenderPass::Id renderPassId, const LayerTreeHostImpl::FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14524{
[email protected]96baf3e2012-10-22 23:09:55525 RenderPassIdHashMap::const_iterator it = frame.renderPassesById.find(renderPassId);
[email protected]1d993172012-10-18 18:15:04526 DCHECK(it != frame.renderPassesById.end());
[email protected]87cea5372012-09-26 18:59:56527 return it->second;
[email protected]94f206c12012-08-25 00:09:14528}
529
[email protected]96baf3e2012-10-22 23:09:55530static void removeRenderPassesRecursive(RenderPass::Id removeRenderPassId, LayerTreeHostImpl::FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14531{
[email protected]96baf3e2012-10-22 23:09:55532 RenderPass* removeRenderPass = findRenderPassById(removeRenderPassId, frame);
533 RenderPassList& renderPasses = frame.renderPasses;
534 RenderPassList::iterator toRemove = std::find(renderPasses.begin(), renderPasses.end(), removeRenderPass);
[email protected]94f206c12012-08-25 00:09:14535
536 // The pass was already removed by another quad - probably the original, and we are the replica.
[email protected]f8ad8342012-09-27 20:07:02537 if (toRemove == renderPasses.end())
[email protected]94f206c12012-08-25 00:09:14538 return;
539
[email protected]96baf3e2012-10-22 23:09:55540 const RenderPass* removedPass = *toRemove;
[email protected]f8ad8342012-09-27 20:07:02541 frame.renderPasses.erase(toRemove);
[email protected]94f206c12012-08-25 00:09:14542
543 // Now follow up for all RenderPass quads and remove their RenderPasses recursively.
[email protected]96baf3e2012-10-22 23:09:55544 const QuadList& quadList = removedPass->quadList();
545 QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14546 for (; quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
[email protected]96baf3e2012-10-22 23:09:55547 DrawQuad* currentQuad = (*quadListIterator);
548 if (currentQuad->material() != DrawQuad::RenderPass)
[email protected]94f206c12012-08-25 00:09:14549 continue;
550
[email protected]96baf3e2012-10-22 23:09:55551 RenderPass::Id nextRemoveRenderPassId = RenderPassDrawQuad::materialCast(currentQuad)->renderPassId();
[email protected]94f206c12012-08-25 00:09:14552 removeRenderPassesRecursive(nextRemoveRenderPassId, frame);
553 }
554}
555
[email protected]96baf3e2012-10-22 23:09:55556bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::shouldRemoveRenderPass(const RenderPassDrawQuad& quad, const FrameData&) const
[email protected]94f206c12012-08-25 00:09:14557{
[email protected]1fea8142012-10-20 04:12:41558 return quad.contentsChangedSinceLastFrame().IsEmpty() && m_renderer.haveCachedResourcesForRenderPassId(quad.renderPassId());
[email protected]94f206c12012-08-25 00:09:14559}
560
[email protected]96baf3e2012-10-22 23:09:55561bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::shouldRemoveRenderPass(const RenderPassDrawQuad& quad, const FrameData& frame) const
[email protected]94f206c12012-08-25 00:09:14562{
[email protected]96baf3e2012-10-22 23:09:55563 const RenderPass* renderPass = findRenderPassById(quad.renderPassId(), frame);
564 const RenderPassList& renderPasses = frame.renderPasses;
565 RenderPassList::const_iterator foundPass = std::find(renderPasses.begin(), renderPasses.end(), renderPass);
[email protected]94f206c12012-08-25 00:09:14566
[email protected]f8ad8342012-09-27 20:07:02567 bool renderPassAlreadyRemoved = foundPass == renderPasses.end();
[email protected]94f206c12012-08-25 00:09:14568 if (renderPassAlreadyRemoved)
569 return false;
570
571 // If any quad or RenderPass draws into this RenderPass, then keep it.
[email protected]96baf3e2012-10-22 23:09:55572 const QuadList& quadList = (*foundPass)->quadList();
573 for (QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin(); quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
574 DrawQuad* currentQuad = *quadListIterator;
[email protected]94f206c12012-08-25 00:09:14575
[email protected]96baf3e2012-10-22 23:09:55576 if (currentQuad->material() != DrawQuad::RenderPass)
[email protected]94f206c12012-08-25 00:09:14577 return false;
578
[email protected]96baf3e2012-10-22 23:09:55579 const RenderPass* contributingPass = findRenderPassById(RenderPassDrawQuad::materialCast(currentQuad)->renderPassId(), frame);
580 RenderPassList::const_iterator foundContributingPass = std::find(renderPasses.begin(), renderPasses.end(), contributingPass);
[email protected]f8ad8342012-09-27 20:07:02581 if (foundContributingPass != renderPasses.end())
[email protected]94f206c12012-08-25 00:09:14582 return false;
583 }
584 return true;
585}
586
587// Defined for linking tests.
[email protected]96baf3e2012-10-22 23:09:55588template void LayerTreeHostImpl::removeRenderPasses<LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(CullRenderPassesWithCachedTextures, FrameData&);
589template void LayerTreeHostImpl::removeRenderPasses<LayerTreeHostImpl::CullRenderPassesWithNoQuads>(CullRenderPassesWithNoQuads, FrameData&);
[email protected]94f206c12012-08-25 00:09:14590
591// static
592template<typename RenderPassCuller>
[email protected]96baf3e2012-10-22 23:09:55593void LayerTreeHostImpl::removeRenderPasses(RenderPassCuller culler, FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14594{
595 for (size_t it = culler.renderPassListBegin(frame.renderPasses); it != culler.renderPassListEnd(frame.renderPasses); it = culler.renderPassListNext(it)) {
[email protected]96baf3e2012-10-22 23:09:55596 const RenderPass* currentPass = frame.renderPasses[it];
597 const QuadList& quadList = currentPass->quadList();
598 QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14599
600 for (; quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
[email protected]96baf3e2012-10-22 23:09:55601 DrawQuad* currentQuad = *quadListIterator;
[email protected]94f206c12012-08-25 00:09:14602
[email protected]96baf3e2012-10-22 23:09:55603 if (currentQuad->material() != DrawQuad::RenderPass)
[email protected]94f206c12012-08-25 00:09:14604 continue;
605
[email protected]96baf3e2012-10-22 23:09:55606 RenderPassDrawQuad* renderPassQuad = static_cast<RenderPassDrawQuad*>(currentQuad);
[email protected]94f206c12012-08-25 00:09:14607 if (!culler.shouldRemoveRenderPass(*renderPassQuad, frame))
608 continue;
609
610 // We are changing the vector in the middle of iteration. Because we
611 // delete render passes that draw into the current pass, we are
612 // guaranteed that any data from the iterator to the end will not
613 // change. So, capture the iterator position from the end of the
614 // list, and restore it after the change.
615 int positionFromEnd = frame.renderPasses.size() - it;
616 removeRenderPassesRecursive(renderPassQuad->renderPassId(), frame);
617 it = frame.renderPasses.size() - positionFromEnd;
[email protected]1d993172012-10-18 18:15:04618 DCHECK(it >= 0);
[email protected]94f206c12012-08-25 00:09:14619 }
620 }
621}
622
[email protected]96baf3e2012-10-22 23:09:55623bool LayerTreeHostImpl::prepareToDraw(FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14624{
[email protected]96baf3e2012-10-22 23:09:55625 TRACE_EVENT0("cc", "LayerTreeHostImpl::prepareToDraw");
[email protected]1d993172012-10-18 18:15:04626 DCHECK(canDraw());
[email protected]94f206c12012-08-25 00:09:14627
628 frame.renderSurfaceLayerList = &m_renderSurfaceLayerList;
629 frame.renderPasses.clear();
630 frame.renderPassesById.clear();
631 frame.renderSurfaceLayerList->clear();
632 frame.willDrawLayers.clear();
633
634 if (!calculateRenderPasses(frame))
635 return false;
636
637 // If we return true, then we expect drawLayers() to be called before this function is called again.
638 return true;
639}
640
[email protected]96baf3e2012-10-22 23:09:55641void LayerTreeHostImpl::enforceManagedMemoryPolicy(const ManagedMemoryPolicy& policy)
[email protected]94f206c12012-08-25 00:09:14642{
[email protected]a0a00842012-10-22 22:50:28643 bool evictedResources = m_client->reduceContentsTextureMemoryOnImplThread(
644 m_visible ? policy.bytesLimitWhenVisible : policy.bytesLimitWhenNotVisible,
645 m_visible ? policy.priorityCutoffWhenVisible : policy.priorityCutoffWhenNotVisible);
[email protected]b1969fa2012-10-17 20:16:29646 if (evictedResources) {
647 setContentsTexturesPurged();
648 m_client->setNeedsCommitOnImplThread();
649 m_client->onCanDrawStateChanged(canDraw());
650 }
[email protected]94f206c12012-08-25 00:09:14651}
652
[email protected]96baf3e2012-10-22 23:09:55653void LayerTreeHostImpl::setManagedMemoryPolicy(const ManagedMemoryPolicy& policy)
[email protected]94f206c12012-08-25 00:09:14654{
[email protected]a0a00842012-10-22 22:50:28655 if (m_managedMemoryPolicy == policy)
[email protected]94f206c12012-08-25 00:09:14656 return;
[email protected]a0a00842012-10-22 22:50:28657 m_managedMemoryPolicy = policy;
658 enforceManagedMemoryPolicy(m_managedMemoryPolicy);
659 // We always need to commit after changing the memory policy because the new
660 // limit can result in more or less content having texture allocated for it.
[email protected]94f206c12012-08-25 00:09:14661 m_client->setNeedsCommitOnImplThread();
662}
663
[email protected]96baf3e2012-10-22 23:09:55664void LayerTreeHostImpl::onVSyncParametersChanged(double monotonicTimebase, double intervalInSeconds)
[email protected]94f206c12012-08-25 00:09:14665{
666 m_client->onVSyncParametersChanged(monotonicTimebase, intervalInSeconds);
667}
668
[email protected]96baf3e2012-10-22 23:09:55669void LayerTreeHostImpl::drawLayers(const FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14670{
[email protected]96baf3e2012-10-22 23:09:55671 TRACE_EVENT0("cc", "LayerTreeHostImpl::drawLayers");
[email protected]1d993172012-10-18 18:15:04672 DCHECK(canDraw());
673 DCHECK(!frame.renderPasses.empty());
[email protected]94f206c12012-08-25 00:09:14674
675 // FIXME: use the frame begin time from the overall compositor scheduler.
676 // This value is currently inaccessible because it is up in Chromium's
677 // RenderWidget.
[email protected]6bea87c2012-10-13 00:15:21678 m_fpsCounter->markBeginningOfFrame(base::TimeTicks::Now());
[email protected]94f206c12012-08-25 00:09:14679
680 if (m_settings.showDebugRects())
681 m_debugRectHistory->saveDebugRectsForCurrentFrame(m_rootLayerImpl.get(), *frame.renderSurfaceLayerList, frame.occludingScreenSpaceRects, settings());
682
683 // Because the contents of the HUD depend on everything else in the frame, the contents
684 // of its texture are updated as the last thing before the frame is drawn.
685 if (m_hudLayerImpl)
686 m_hudLayerImpl->updateHudTexture(m_resourceProvider.get());
687
688 m_renderer->drawFrame(frame.renderPasses, frame.renderPassesById);
689
690 // Once a RenderPass has been drawn, its damage should be cleared in
691 // case the RenderPass will be reused next frame.
692 for (unsigned int i = 0; i < frame.renderPasses.size(); i++)
693 frame.renderPasses[i]->setDamageRect(FloatRect());
694
695 // The next frame should start by assuming nothing has changed, and changes are noted as they occur.
696 for (unsigned int i = 0; i < frame.renderSurfaceLayerList->size(); i++)
697 (*frame.renderSurfaceLayerList)[i]->renderSurface()->damageTracker()->didDrawDamagedArea();
698 m_rootLayerImpl->resetAllChangeTrackingForSubtree();
699}
700
[email protected]96baf3e2012-10-22 23:09:55701void LayerTreeHostImpl::didDrawAllLayers(const FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14702{
703 for (size_t i = 0; i < frame.willDrawLayers.size(); ++i)
704 frame.willDrawLayers[i]->didDraw(m_resourceProvider.get());
[email protected]b914e102012-10-02 08:11:52705
706 // Once all layers have been drawn, pending texture uploads should no
707 // longer block future uploads.
[email protected]e2249592012-10-19 06:59:09708 m_resourceProvider->markPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:14709}
710
[email protected]96baf3e2012-10-22 23:09:55711void LayerTreeHostImpl::finishAllRendering()
[email protected]94f206c12012-08-25 00:09:14712{
713 if (m_renderer)
714 m_renderer->finish();
715}
716
[email protected]96baf3e2012-10-22 23:09:55717bool LayerTreeHostImpl::isContextLost()
[email protected]94f206c12012-08-25 00:09:14718{
719 return m_renderer && m_renderer->isContextLost();
720}
721
[email protected]96baf3e2012-10-22 23:09:55722const RendererCapabilities& LayerTreeHostImpl::rendererCapabilities() const
[email protected]94f206c12012-08-25 00:09:14723{
724 return m_renderer->capabilities();
725}
726
[email protected]96baf3e2012-10-22 23:09:55727bool LayerTreeHostImpl::swapBuffers()
[email protected]94f206c12012-08-25 00:09:14728{
[email protected]1d993172012-10-18 18:15:04729 DCHECK(m_renderer);
[email protected]94f206c12012-08-25 00:09:14730
731 m_fpsCounter->markEndOfFrame();
732 return m_renderer->swapBuffers();
733}
734
[email protected]96baf3e2012-10-22 23:09:55735const IntSize& LayerTreeHostImpl::deviceViewportSize() const
[email protected]493067512012-09-19 23:34:10736{
737 return m_deviceViewportSize;
738}
739
[email protected]96baf3e2012-10-22 23:09:55740const LayerTreeSettings& LayerTreeHostImpl::settings() const
[email protected]493067512012-09-19 23:34:10741{
742 return m_settings;
743}
744
[email protected]96baf3e2012-10-22 23:09:55745void LayerTreeHostImpl::didLoseContext()
[email protected]94f206c12012-08-25 00:09:14746{
747 m_client->didLoseContextOnImplThread();
748}
749
[email protected]96baf3e2012-10-22 23:09:55750void LayerTreeHostImpl::onSwapBuffersComplete()
[email protected]94f206c12012-08-25 00:09:14751{
752 m_client->onSwapBuffersCompleteOnImplThread();
753}
754
[email protected]96baf3e2012-10-22 23:09:55755void LayerTreeHostImpl::readback(void* pixels, const IntRect& rect)
[email protected]94f206c12012-08-25 00:09:14756{
[email protected]1d993172012-10-18 18:15:04757 DCHECK(m_renderer);
[email protected]94f206c12012-08-25 00:09:14758 m_renderer->getFramebufferPixels(pixels, rect);
759}
760
[email protected]96baf3e2012-10-22 23:09:55761static LayerImpl* findRootScrollLayer(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14762{
763 if (!layer)
764 return 0;
765
766 if (layer->scrollable())
767 return layer;
768
769 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55770 LayerImpl* found = findRootScrollLayer(layer->children()[i]);
[email protected]94f206c12012-08-25 00:09:14771 if (found)
772 return found;
773 }
774
775 return 0;
776}
777
778// Content layers can be either directly scrollable or contained in an outer
779// scrolling layer which applies the scroll transform. Given a content layer,
780// this function returns the associated scroll layer if any.
[email protected]96baf3e2012-10-22 23:09:55781static LayerImpl* findScrollLayerForContentLayer(LayerImpl* layerImpl)
[email protected]94f206c12012-08-25 00:09:14782{
783 if (!layerImpl)
784 return 0;
785
786 if (layerImpl->scrollable())
787 return layerImpl;
788
789 if (layerImpl->drawsContent() && layerImpl->parent() && layerImpl->parent()->scrollable())
790 return layerImpl->parent();
791
792 return 0;
793}
794
[email protected]96baf3e2012-10-22 23:09:55795void LayerTreeHostImpl::setRootLayer(scoped_ptr<LayerImpl> layer)
[email protected]94f206c12012-08-25 00:09:14796{
[email protected]e0bd43a2012-10-12 16:54:21797 m_rootLayerImpl = layer.Pass();
[email protected]94f206c12012-08-25 00:09:14798 m_rootScrollLayerImpl = findRootScrollLayer(m_rootLayerImpl.get());
799 m_currentlyScrollingLayerImpl = 0;
800
801 if (m_rootLayerImpl && m_scrollingLayerIdFromPreviousTree != -1)
[email protected]96baf3e2012-10-22 23:09:55802 m_currentlyScrollingLayerImpl = LayerTreeHostCommon::findLayerInSubtree(m_rootLayerImpl.get(), m_scrollingLayerIdFromPreviousTree);
[email protected]94f206c12012-08-25 00:09:14803
804 m_scrollingLayerIdFromPreviousTree = -1;
[email protected]8db2213c2012-09-05 22:08:21805
806 m_client->onCanDrawStateChanged(canDraw());
[email protected]94f206c12012-08-25 00:09:14807}
808
[email protected]96baf3e2012-10-22 23:09:55809scoped_ptr<LayerImpl> LayerTreeHostImpl::detachLayerTree()
[email protected]94f206c12012-08-25 00:09:14810{
811 // Clear all data structures that have direct references to the layer tree.
812 m_scrollingLayerIdFromPreviousTree = m_currentlyScrollingLayerImpl ? m_currentlyScrollingLayerImpl->id() : -1;
813 m_currentlyScrollingLayerImpl = 0;
814 m_renderSurfaceLayerList.clear();
815
[email protected]e0bd43a2012-10-12 16:54:21816 return m_rootLayerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:14817}
818
[email protected]96baf3e2012-10-22 23:09:55819void LayerTreeHostImpl::setVisible(bool visible)
[email protected]94f206c12012-08-25 00:09:14820{
[email protected]96baf3e2012-10-22 23:09:55821 DCHECK(Proxy::isImplThread());
[email protected]94f206c12012-08-25 00:09:14822
823 if (m_visible == visible)
824 return;
825 m_visible = visible;
826 didVisibilityChange(this, m_visible);
[email protected]a0a00842012-10-22 22:50:28827 enforceManagedMemoryPolicy(m_managedMemoryPolicy);
[email protected]94f206c12012-08-25 00:09:14828
829 if (!m_renderer)
830 return;
831
832 m_renderer->setVisible(visible);
833
834 setBackgroundTickingEnabled(!m_visible && m_needsAnimateLayers);
835}
836
[email protected]96baf3e2012-10-22 23:09:55837bool LayerTreeHostImpl::initializeRenderer(scoped_ptr<GraphicsContext> context)
[email protected]94f206c12012-08-25 00:09:14838{
[email protected]be3181652012-09-25 13:02:13839 // Since we will create a new resource provider, we cannot continue to use
840 // the old resources (i.e. renderSurfaces and texture IDs). Clear them
841 // before we destroy the old resource provider.
[email protected]94f206c12012-08-25 00:09:14842 if (m_rootLayerImpl) {
843 clearRenderSurfaces();
844 sendDidLoseContextRecursive(m_rootLayerImpl.get());
845 }
[email protected]be3181652012-09-25 13:02:13846 // Note: order is important here.
[email protected]0704caf2012-10-16 03:39:47847 m_renderer.reset();
[email protected]a7aa5562012-10-17 14:12:44848 m_resourceProvider.reset();
[email protected]e28efacd2012-10-06 17:07:49849 m_context.reset();
[email protected]94f206c12012-08-25 00:09:14850
[email protected]be3181652012-09-25 13:02:13851 if (!context->bindToClient(this))
852 return false;
853
[email protected]96baf3e2012-10-22 23:09:55854 scoped_ptr<ResourceProvider> resourceProvider = ResourceProvider::create(context.get());
[email protected]be3181652012-09-25 13:02:13855 if (!resourceProvider)
856 return false;
857
858 if (context->context3D())
[email protected]96baf3e2012-10-22 23:09:55859 m_renderer = GLRenderer::create(this, resourceProvider.get());
[email protected]be3181652012-09-25 13:02:13860 else if (context->softwareDevice())
[email protected]96baf3e2012-10-22 23:09:55861 m_renderer = SoftwareRenderer::create(this, resourceProvider.get(), context->softwareDevice());
[email protected]be3181652012-09-25 13:02:13862 if (!m_renderer)
863 return false;
864
[email protected]a7aa5562012-10-17 14:12:44865 m_resourceProvider = resourceProvider.Pass();
[email protected]e28efacd2012-10-06 17:07:49866 m_context = context.Pass();
[email protected]94f206c12012-08-25 00:09:14867
[email protected]be3181652012-09-25 13:02:13868 if (!m_visible)
869 m_renderer->setVisible(m_visible);
[email protected]94f206c12012-08-25 00:09:14870
[email protected]8db2213c2012-09-05 22:08:21871 m_client->onCanDrawStateChanged(canDraw());
872
[email protected]be3181652012-09-25 13:02:13873 return true;
[email protected]94f206c12012-08-25 00:09:14874}
875
[email protected]96baf3e2012-10-22 23:09:55876void LayerTreeHostImpl::setContentsTexturesPurged()
[email protected]e1fc8b32012-09-18 20:29:09877{
878 m_contentsTexturesPurged = true;
879 m_client->onCanDrawStateChanged(canDraw());
880}
881
[email protected]96baf3e2012-10-22 23:09:55882void LayerTreeHostImpl::resetContentsTexturesPurged()
[email protected]8db2213c2012-09-05 22:08:21883{
884 m_contentsTexturesPurged = false;
885 m_client->onCanDrawStateChanged(canDraw());
886}
887
[email protected]96baf3e2012-10-22 23:09:55888void LayerTreeHostImpl::setViewportSize(const IntSize& layoutViewportSize, const IntSize& deviceViewportSize)
[email protected]94f206c12012-08-25 00:09:14889{
890 if (layoutViewportSize == m_layoutViewportSize && deviceViewportSize == m_deviceViewportSize)
891 return;
892
893 m_layoutViewportSize = layoutViewportSize;
894 m_deviceViewportSize = deviceViewportSize;
895
[email protected]1c0c9bc2012-10-08 22:41:48896 m_pinchZoomViewport.setLayoutViewportSize(FloatSize(layoutViewportSize));
897
[email protected]94f206c12012-08-25 00:09:14898 updateMaxScrollPosition();
899
900 if (m_renderer)
901 m_renderer->viewportChanged();
[email protected]8db2213c2012-09-05 22:08:21902
903 m_client->onCanDrawStateChanged(canDraw());
[email protected]94f206c12012-08-25 00:09:14904}
905
[email protected]96baf3e2012-10-22 23:09:55906static void adjustScrollsForPageScaleChange(LayerImpl* layerImpl, float pageScaleChange)
[email protected]94f206c12012-08-25 00:09:14907{
908 if (!layerImpl)
909 return;
910
911 if (layerImpl->scrollable()) {
912 // We need to convert impl-side scroll deltas to pageScale space.
913 FloatSize scrollDelta = layerImpl->scrollDelta();
914 scrollDelta.scale(pageScaleChange);
915 layerImpl->setScrollDelta(scrollDelta);
916 }
917
918 for (size_t i = 0; i < layerImpl->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:03919 adjustScrollsForPageScaleChange(layerImpl->children()[i], pageScaleChange);
[email protected]94f206c12012-08-25 00:09:14920}
921
[email protected]96baf3e2012-10-22 23:09:55922void LayerTreeHostImpl::setDeviceScaleFactor(float deviceScaleFactor)
[email protected]94f206c12012-08-25 00:09:14923{
924 if (deviceScaleFactor == m_deviceScaleFactor)
925 return;
926 m_deviceScaleFactor = deviceScaleFactor;
[email protected]c0dd24c2012-08-30 23:25:27927
928 updateMaxScrollPosition();
[email protected]94f206c12012-08-25 00:09:14929}
930
[email protected]96baf3e2012-10-22 23:09:55931float LayerTreeHostImpl::pageScaleFactor() const
[email protected]94f206c12012-08-25 00:09:14932{
[email protected]1c0c9bc2012-10-08 22:41:48933 return m_pinchZoomViewport.pageScaleFactor();
934}
[email protected]94f206c12012-08-25 00:09:14935
[email protected]96baf3e2012-10-22 23:09:55936void LayerTreeHostImpl::setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor)
[email protected]1c0c9bc2012-10-08 22:41:48937{
938 if (!pageScaleFactor)
939 return;
[email protected]94f206c12012-08-25 00:09:14940
[email protected]1c0c9bc2012-10-08 22:41:48941 float pageScaleChange = pageScaleFactor / m_pinchZoomViewport.pageScaleFactor();
942 m_pinchZoomViewport.setPageScaleFactorAndLimits(pageScaleFactor, minPageScaleFactor, maxPageScaleFactor);
[email protected]94f206c12012-08-25 00:09:14943
[email protected]65bfd9972012-10-19 03:39:37944 if (!Settings::pageScalePinchZoomEnabled()) {
[email protected]1c0c9bc2012-10-08 22:41:48945 if (pageScaleChange != 1)
946 adjustScrollsForPageScaleChange(m_rootScrollLayerImpl, pageScaleChange);
947 }
[email protected]94f206c12012-08-25 00:09:14948
949 // Clamp delta to limits and refresh display matrix.
[email protected]1c0c9bc2012-10-08 22:41:48950 setPageScaleDelta(m_pinchZoomViewport.pageScaleDelta() / m_pinchZoomViewport.sentPageScaleDelta());
951 m_pinchZoomViewport.setSentPageScaleDelta(1);
[email protected]94f206c12012-08-25 00:09:14952}
953
[email protected]96baf3e2012-10-22 23:09:55954void LayerTreeHostImpl::setPageScaleDelta(float delta)
[email protected]94f206c12012-08-25 00:09:14955{
[email protected]1c0c9bc2012-10-08 22:41:48956 m_pinchZoomViewport.setPageScaleDelta(delta);
[email protected]94f206c12012-08-25 00:09:14957
958 updateMaxScrollPosition();
[email protected]94f206c12012-08-25 00:09:14959}
960
[email protected]96baf3e2012-10-22 23:09:55961void LayerTreeHostImpl::updateMaxScrollPosition()
[email protected]94f206c12012-08-25 00:09:14962{
963 if (!m_rootScrollLayerImpl || !m_rootScrollLayerImpl->children().size())
964 return;
965
966 FloatSize viewBounds = m_deviceViewportSize;
[email protected]96baf3e2012-10-22 23:09:55967 if (LayerImpl* clipLayer = m_rootScrollLayerImpl->parent()) {
[email protected]94f206c12012-08-25 00:09:14968 // Compensate for non-overlay scrollbars.
969 if (clipLayer->masksToBounds()) {
970 viewBounds = clipLayer->bounds();
971 viewBounds.scale(m_deviceScaleFactor);
972 }
973 }
[email protected]94f206c12012-08-25 00:09:14974
[email protected]1c0c9bc2012-10-08 22:41:48975 IntSize contentBounds = contentSize();
[email protected]65bfd9972012-10-19 03:39:37976 if (Settings::pageScalePinchZoomEnabled()) {
[email protected]1c0c9bc2012-10-08 22:41:48977 // Pinch with pageScale scrolls entirely in layout space. contentSize
978 // returns the bounds including the page scale factor, so calculate the
979 // pre page-scale layout size here.
980 float pageScaleFactor = m_pinchZoomViewport.pageScaleFactor();
981 contentBounds.setWidth(contentBounds.width() / pageScaleFactor);
982 contentBounds.setHeight(contentBounds.height() / pageScaleFactor);
983 } else {
984 viewBounds.scale(1 / m_pinchZoomViewport.pageScaleDelta());
985 }
986
987 IntSize maxScroll = contentBounds - expandedIntSize(viewBounds);
[email protected]94f206c12012-08-25 00:09:14988 maxScroll.scale(1 / m_deviceScaleFactor);
[email protected]1c0c9bc2012-10-08 22:41:48989
[email protected]94f206c12012-08-25 00:09:14990 // The viewport may be larger than the contents in some cases, such as
991 // having a vertical scrollbar but no horizontal overflow.
992 maxScroll.clampNegativeToZero();
993
994 m_rootScrollLayerImpl->setMaxScrollPosition(maxScroll);
995}
996
[email protected]96baf3e2012-10-22 23:09:55997void LayerTreeHostImpl::setNeedsRedraw()
[email protected]94f206c12012-08-25 00:09:14998{
999 m_client->setNeedsRedrawOnImplThread();
1000}
1001
[email protected]96baf3e2012-10-22 23:09:551002bool LayerTreeHostImpl::ensureRenderSurfaceLayerList()
[email protected]94f206c12012-08-25 00:09:141003{
1004 if (!m_rootLayerImpl)
1005 return false;
1006 if (!m_renderer)
1007 return false;
1008
1009 // We need both a non-empty render surface layer list and a root render
1010 // surface to be able to iterate over the visible layers.
1011 if (m_renderSurfaceLayerList.size() && m_rootLayerImpl->renderSurface())
1012 return true;
1013
1014 // If we are called after setRootLayer() but before prepareToDraw(), we need
1015 // to recalculate the visible layers. This prevents being unable to scroll
1016 // during part of a commit.
1017 m_renderSurfaceLayerList.clear();
1018 calculateRenderSurfaceLayerList(m_renderSurfaceLayerList);
1019
1020 return m_renderSurfaceLayerList.size();
1021}
1022
[email protected]96baf3e2012-10-22 23:09:551023InputHandlerClient::ScrollStatus LayerTreeHostImpl::scrollBegin(const IntPoint& viewportPoint, InputHandlerClient::ScrollInputType type)
[email protected]94f206c12012-08-25 00:09:141024{
[email protected]96baf3e2012-10-22 23:09:551025 TRACE_EVENT0("cc", "LayerTreeHostImpl::scrollBegin");
[email protected]94f206c12012-08-25 00:09:141026
[email protected]1d993172012-10-18 18:15:041027 DCHECK(!m_currentlyScrollingLayerImpl);
[email protected]94f206c12012-08-25 00:09:141028 clearCurrentlyScrollingLayer();
1029
1030 if (!ensureRenderSurfaceLayerList())
1031 return ScrollIgnored;
1032
1033 IntPoint deviceViewportPoint = viewportPoint;
1034 deviceViewportPoint.scale(m_deviceScaleFactor, m_deviceScaleFactor);
1035
1036 // First find out which layer was hit from the saved list of visible layers
1037 // in the most recent frame.
[email protected]96baf3e2012-10-22 23:09:551038 LayerImpl* layerImpl = LayerTreeHostCommon::findLayerThatIsHitByPoint(deviceViewportPoint, m_renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:141039
1040 // Walk up the hierarchy and look for a scrollable layer.
[email protected]96baf3e2012-10-22 23:09:551041 LayerImpl* potentiallyScrollingLayerImpl = 0;
[email protected]94f206c12012-08-25 00:09:141042 for (; layerImpl; layerImpl = layerImpl->parent()) {
1043 // The content layer can also block attempts to scroll outside the main thread.
[email protected]5c6fe1f82012-10-03 18:00:271044 if (layerImpl->tryScroll(deviceViewportPoint, type) == ScrollOnMainThread) {
1045 m_numMainThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141046 return ScrollOnMainThread;
[email protected]5c6fe1f82012-10-03 18:00:271047 }
[email protected]94f206c12012-08-25 00:09:141048
[email protected]96baf3e2012-10-22 23:09:551049 LayerImpl* scrollLayerImpl = findScrollLayerForContentLayer(layerImpl);
[email protected]94f206c12012-08-25 00:09:141050 if (!scrollLayerImpl)
1051 continue;
1052
[email protected]31bfe272012-10-19 18:49:521053 ScrollStatus status = scrollLayerImpl->tryScroll(deviceViewportPoint, type);
[email protected]94f206c12012-08-25 00:09:141054
1055 // If any layer wants to divert the scroll event to the main thread, abort.
[email protected]5c6fe1f82012-10-03 18:00:271056 if (status == ScrollOnMainThread) {
1057 m_numMainThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141058 return ScrollOnMainThread;
[email protected]5c6fe1f82012-10-03 18:00:271059 }
[email protected]94f206c12012-08-25 00:09:141060
1061 if (status == ScrollStarted && !potentiallyScrollingLayerImpl)
1062 potentiallyScrollingLayerImpl = scrollLayerImpl;
1063 }
1064
1065 if (potentiallyScrollingLayerImpl) {
1066 m_currentlyScrollingLayerImpl = potentiallyScrollingLayerImpl;
[email protected]31bfe272012-10-19 18:49:521067 // Gesture events need to be transformed from viewport coordinates to local layer coordinates
[email protected]94f206c12012-08-25 00:09:141068 // so that the scrolling contents exactly follow the user's finger. In contrast, wheel
1069 // events are already in local layer coordinates so we can just apply them directly.
[email protected]31bfe272012-10-19 18:49:521070 m_scrollDeltaIsInViewportSpace = (type == Gesture);
[email protected]5c6fe1f82012-10-03 18:00:271071 m_numImplThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141072 return ScrollStarted;
1073 }
1074 return ScrollIgnored;
1075}
1076
[email protected]96baf3e2012-10-22 23:09:551077static FloatSize scrollLayerWithViewportSpaceDelta(PinchZoomViewport* viewport, LayerImpl& layerImpl, float scaleFromViewportToScreenSpace, const FloatPoint& viewportPoint, const FloatSize& viewportDelta)
[email protected]94f206c12012-08-25 00:09:141078{
1079 // Layers with non-invertible screen space transforms should not have passed the scroll hit
1080 // test in the first place.
[email protected]1d993172012-10-18 18:15:041081 DCHECK(layerImpl.screenSpaceTransform().isInvertible());
[email protected]94f206c12012-08-25 00:09:141082 WebTransformationMatrix inverseScreenSpaceTransform = layerImpl.screenSpaceTransform().inverse();
1083
[email protected]31bfe272012-10-19 18:49:521084 FloatPoint screenSpacePoint = viewportPoint;
1085 screenSpacePoint.scale(scaleFromViewportToScreenSpace, scaleFromViewportToScreenSpace);
1086
1087 FloatSize screenSpaceDelta = viewportDelta;
1088 screenSpaceDelta.scale(scaleFromViewportToScreenSpace, scaleFromViewportToScreenSpace);
1089
[email protected]94f206c12012-08-25 00:09:141090 // First project the scroll start and end points to local layer space to find the scroll delta
1091 // in layer coordinates.
1092 bool startClipped, endClipped;
1093 FloatPoint screenSpaceEndPoint = screenSpacePoint + screenSpaceDelta;
[email protected]96baf3e2012-10-22 23:09:551094 FloatPoint localStartPoint = MathUtil::projectPoint(inverseScreenSpaceTransform, screenSpacePoint, startClipped);
1095 FloatPoint localEndPoint = MathUtil::projectPoint(inverseScreenSpaceTransform, screenSpaceEndPoint, endClipped);
[email protected]94f206c12012-08-25 00:09:141096
1097 // In general scroll point coordinates should not get clipped.
[email protected]1d993172012-10-18 18:15:041098 DCHECK(!startClipped);
1099 DCHECK(!endClipped);
[email protected]94f206c12012-08-25 00:09:141100 if (startClipped || endClipped)
1101 return FloatSize();
1102
[email protected]31bfe272012-10-19 18:49:521103 // localStartPoint and localEndPoint are in content space but we want to move them to layer space for scrolling.
1104 float widthScale = 1;
1105 float heightScale = 1;
1106 if (!layerImpl.contentBounds().isEmpty() && !layerImpl.bounds().isEmpty()) {
1107 widthScale = layerImpl.bounds().width() / static_cast<float>(layerImpl.contentBounds().width());
1108 heightScale = layerImpl.bounds().height() / static_cast<float>(layerImpl.contentBounds().height());
1109 }
1110 localStartPoint.scale(widthScale, heightScale);
1111 localEndPoint.scale(widthScale, heightScale);
1112
[email protected]94f206c12012-08-25 00:09:141113 // Apply the scroll delta.
1114 FloatSize previousDelta(layerImpl.scrollDelta());
[email protected]1c0c9bc2012-10-08 22:41:481115 FloatSize unscrolled = layerImpl.scrollBy(localEndPoint - localStartPoint);
1116
1117 if (viewport)
1118 viewport->applyScroll(unscrolled);
[email protected]94f206c12012-08-25 00:09:141119
[email protected]31bfe272012-10-19 18:49:521120 // Get the end point in the layer's content space so we can apply its screenSpaceTransform.
[email protected]94f206c12012-08-25 00:09:141121 FloatPoint actualLocalEndPoint = localStartPoint + layerImpl.scrollDelta() - previousDelta;
[email protected]31bfe272012-10-19 18:49:521122 FloatPoint actualLocalContentEndPoint = actualLocalEndPoint;
1123 actualLocalContentEndPoint.scale(1 / widthScale, 1 / heightScale);
1124
1125 // Calculate the applied scroll delta in viewport space coordinates.
[email protected]96baf3e2012-10-22 23:09:551126 FloatPoint actualScreenSpaceEndPoint = MathUtil::mapPoint(layerImpl.screenSpaceTransform(), actualLocalContentEndPoint, endClipped);
[email protected]1d993172012-10-18 18:15:041127 DCHECK(!endClipped);
[email protected]94f206c12012-08-25 00:09:141128 if (endClipped)
1129 return FloatSize();
[email protected]31bfe272012-10-19 18:49:521130 FloatPoint actualViewportEndPoint = actualScreenSpaceEndPoint;
1131 actualViewportEndPoint.scale(1 / scaleFromViewportToScreenSpace, 1 / scaleFromViewportToScreenSpace);
1132 return actualViewportEndPoint - viewportPoint;
[email protected]94f206c12012-08-25 00:09:141133}
1134
[email protected]96baf3e2012-10-22 23:09:551135static FloatSize scrollLayerWithLocalDelta(LayerImpl& layerImpl, const FloatSize& localDelta)
[email protected]94f206c12012-08-25 00:09:141136{
1137 FloatSize previousDelta(layerImpl.scrollDelta());
1138 layerImpl.scrollBy(localDelta);
1139 return layerImpl.scrollDelta() - previousDelta;
1140}
1141
[email protected]96baf3e2012-10-22 23:09:551142void LayerTreeHostImpl::scrollBy(const IntPoint& viewportPoint, const IntSize& scrollDelta)
[email protected]94f206c12012-08-25 00:09:141143{
[email protected]96baf3e2012-10-22 23:09:551144 TRACE_EVENT0("cc", "LayerTreeHostImpl::scrollBy");
[email protected]94f206c12012-08-25 00:09:141145 if (!m_currentlyScrollingLayerImpl)
1146 return;
1147
1148 FloatSize pendingDelta(scrollDelta);
1149
[email protected]96baf3e2012-10-22 23:09:551150 for (LayerImpl* layerImpl = m_currentlyScrollingLayerImpl; layerImpl; layerImpl = layerImpl->parent()) {
[email protected]94f206c12012-08-25 00:09:141151 if (!layerImpl->scrollable())
1152 continue;
1153
[email protected]96baf3e2012-10-22 23:09:551154 PinchZoomViewport* viewport = layerImpl == m_rootScrollLayerImpl ? &m_pinchZoomViewport : 0;
[email protected]94f206c12012-08-25 00:09:141155 FloatSize appliedDelta;
[email protected]31bfe272012-10-19 18:49:521156 if (m_scrollDeltaIsInViewportSpace) {
1157 float scaleFromViewportToScreenSpace = m_deviceScaleFactor;
1158 appliedDelta = scrollLayerWithViewportSpaceDelta(viewport, *layerImpl, scaleFromViewportToScreenSpace, viewportPoint, pendingDelta);
1159 } else
[email protected]94f206c12012-08-25 00:09:141160 appliedDelta = scrollLayerWithLocalDelta(*layerImpl, pendingDelta);
1161
1162 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]23bbb412012-08-30 20:03:381163 float moveThresholdSquared = 0.1f * 0.1f;
[email protected]94f206c12012-08-25 00:09:141164 if (appliedDelta.diagonalLengthSquared() < moveThresholdSquared)
1165 continue;
1166
1167 // If the applied delta is within 45 degrees of the input delta, bail out to make it easier
1168 // to scroll just one layer in one direction without affecting any of its parents.
1169 float angleThreshold = 45;
[email protected]96baf3e2012-10-22 23:09:551170 if (MathUtil::smallestAngleBetweenVectors(appliedDelta, pendingDelta) < angleThreshold) {
[email protected]94f206c12012-08-25 00:09:141171 pendingDelta = FloatSize();
1172 break;
1173 }
1174
1175 // Allow further movement only on an axis perpendicular to the direction in which the layer
1176 // moved.
1177 FloatSize perpendicularAxis(-appliedDelta.height(), appliedDelta.width());
[email protected]96baf3e2012-10-22 23:09:551178 pendingDelta = MathUtil::projectVector(pendingDelta, perpendicularAxis);
[email protected]94f206c12012-08-25 00:09:141179
1180 if (flooredIntSize(pendingDelta).isZero())
1181 break;
1182 }
1183
1184 if (!scrollDelta.isZero() && flooredIntSize(pendingDelta).isEmpty()) {
1185 m_client->setNeedsCommitOnImplThread();
1186 m_client->setNeedsRedrawOnImplThread();
1187 }
1188}
1189
[email protected]96baf3e2012-10-22 23:09:551190void LayerTreeHostImpl::clearCurrentlyScrollingLayer()
[email protected]94f206c12012-08-25 00:09:141191{
1192 m_currentlyScrollingLayerImpl = 0;
1193 m_scrollingLayerIdFromPreviousTree = -1;
1194}
1195
[email protected]96baf3e2012-10-22 23:09:551196void LayerTreeHostImpl::scrollEnd()
[email protected]94f206c12012-08-25 00:09:141197{
1198 clearCurrentlyScrollingLayer();
1199}
1200
[email protected]96baf3e2012-10-22 23:09:551201void LayerTreeHostImpl::pinchGestureBegin()
[email protected]94f206c12012-08-25 00:09:141202{
1203 m_pinchGestureActive = true;
1204 m_previousPinchAnchor = IntPoint();
1205
1206 if (m_rootScrollLayerImpl && m_rootScrollLayerImpl->scrollbarAnimationController())
1207 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureBegin();
1208}
1209
[email protected]96baf3e2012-10-22 23:09:551210void LayerTreeHostImpl::pinchGestureUpdate(float magnifyDelta,
[email protected]94f206c12012-08-25 00:09:141211 const IntPoint& anchor)
1212{
[email protected]96baf3e2012-10-22 23:09:551213 TRACE_EVENT0("cc", "LayerTreeHostImpl::pinchGestureUpdate");
[email protected]94f206c12012-08-25 00:09:141214
1215 if (!m_rootScrollLayerImpl)
1216 return;
1217
1218 if (m_previousPinchAnchor == IntPoint::zero())
1219 m_previousPinchAnchor = anchor;
1220
1221 // Keep the center-of-pinch anchor specified by (x, y) in a stable
1222 // position over the course of the magnify.
[email protected]1c0c9bc2012-10-08 22:41:481223 float pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
1224 FloatPoint previousScaleAnchor(m_previousPinchAnchor.x() / pageScaleDelta,
1225 m_previousPinchAnchor.y() / pageScaleDelta);
1226 setPageScaleDelta(pageScaleDelta * magnifyDelta);
1227 pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
1228 FloatPoint newScaleAnchor(anchor.x() / pageScaleDelta, anchor.y() / pageScaleDelta);
[email protected]94f206c12012-08-25 00:09:141229 FloatSize move = previousScaleAnchor - newScaleAnchor;
1230
1231 m_previousPinchAnchor = anchor;
1232
[email protected]65bfd9972012-10-19 03:39:371233 if (Settings::pageScalePinchZoomEnabled()) {
[email protected]1c0c9bc2012-10-08 22:41:481234 // Compute the application of the delta with respect to the current page zoom of the page.
1235 move.scale(1 / (m_pinchZoomViewport.pageScaleFactor() * m_deviceScaleFactor));
1236 }
1237
[email protected]65bfd9972012-10-19 03:39:371238 FloatSize scrollOverflow = Settings::pageScalePinchZoomEnabled() ? m_pinchZoomViewport.applyScroll(move) : move;
[email protected]1c0c9bc2012-10-08 22:41:481239 m_rootScrollLayerImpl->scrollBy(roundedIntSize(scrollOverflow));
[email protected]94f206c12012-08-25 00:09:141240
1241 if (m_rootScrollLayerImpl->scrollbarAnimationController())
1242 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureUpdate();
1243
1244 m_client->setNeedsCommitOnImplThread();
1245 m_client->setNeedsRedrawOnImplThread();
1246}
1247
[email protected]96baf3e2012-10-22 23:09:551248void LayerTreeHostImpl::pinchGestureEnd()
[email protected]94f206c12012-08-25 00:09:141249{
1250 m_pinchGestureActive = false;
1251
1252 if (m_rootScrollLayerImpl && m_rootScrollLayerImpl->scrollbarAnimationController())
1253 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureEnd();
1254
1255 m_client->setNeedsCommitOnImplThread();
1256}
1257
[email protected]96baf3e2012-10-22 23:09:551258void LayerTreeHostImpl::computeDoubleTapZoomDeltas(ScrollAndScaleSet* scrollInfo)
[email protected]94f206c12012-08-25 00:09:141259{
1260 float pageScale = m_pageScaleAnimation->finalPageScale();
1261 IntSize scrollOffset = m_pageScaleAnimation->finalScrollOffset();
[email protected]1c0c9bc2012-10-08 22:41:481262 scrollOffset.scale(m_pinchZoomViewport.pageScaleFactor() / pageScale);
[email protected]94f206c12012-08-25 00:09:141263 makeScrollAndScaleSet(scrollInfo, scrollOffset, pageScale);
1264}
1265
[email protected]96baf3e2012-10-22 23:09:551266void LayerTreeHostImpl::computePinchZoomDeltas(ScrollAndScaleSet* scrollInfo)
[email protected]94f206c12012-08-25 00:09:141267{
1268 if (!m_rootScrollLayerImpl)
1269 return;
1270
1271 // Only send fake scroll/zoom deltas if we're pinch zooming out by a
1272 // significant amount. This also ensures only one fake delta set will be
1273 // sent.
[email protected]23bbb412012-08-30 20:03:381274 const float pinchZoomOutSensitivity = 0.95f;
[email protected]1c0c9bc2012-10-08 22:41:481275 if (m_pinchZoomViewport.pageScaleDelta() > pinchZoomOutSensitivity)
[email protected]94f206c12012-08-25 00:09:141276 return;
1277
1278 // Compute where the scroll offset/page scale would be if fully pinch-zoomed
1279 // out from the anchor point.
1280 IntSize scrollBegin = flooredIntSize(m_rootScrollLayerImpl->scrollPosition() + m_rootScrollLayerImpl->scrollDelta());
[email protected]1c0c9bc2012-10-08 22:41:481281 scrollBegin.scale(m_pinchZoomViewport.pageScaleDelta());
1282 float scaleBegin = m_pinchZoomViewport.totalPageScaleFactor();
1283 float pageScaleDeltaToSend = m_pinchZoomViewport.minPageScaleFactor() / m_pinchZoomViewport.pageScaleFactor();
[email protected]94f206c12012-08-25 00:09:141284 FloatSize scaledContentsSize = contentSize();
1285 scaledContentsSize.scale(pageScaleDeltaToSend);
1286
1287 FloatSize anchor = toSize(m_previousPinchAnchor);
1288 FloatSize scrollEnd = scrollBegin + anchor;
[email protected]1c0c9bc2012-10-08 22:41:481289 scrollEnd.scale(m_pinchZoomViewport.minPageScaleFactor() / scaleBegin);
[email protected]94f206c12012-08-25 00:09:141290 scrollEnd -= anchor;
1291 scrollEnd = scrollEnd.shrunkTo(roundedIntSize(scaledContentsSize - m_deviceViewportSize)).expandedTo(FloatSize(0, 0));
1292 scrollEnd.scale(1 / pageScaleDeltaToSend);
1293 scrollEnd.scale(m_deviceScaleFactor);
1294
[email protected]1c0c9bc2012-10-08 22:41:481295 makeScrollAndScaleSet(scrollInfo, roundedIntSize(scrollEnd), m_pinchZoomViewport.minPageScaleFactor());
[email protected]94f206c12012-08-25 00:09:141296}
1297
[email protected]96baf3e2012-10-22 23:09:551298void LayerTreeHostImpl::makeScrollAndScaleSet(ScrollAndScaleSet* scrollInfo, const IntSize& scrollOffset, float pageScale)
[email protected]94f206c12012-08-25 00:09:141299{
1300 if (!m_rootScrollLayerImpl)
1301 return;
1302
[email protected]96baf3e2012-10-22 23:09:551303 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]94f206c12012-08-25 00:09:141304 scroll.layerId = m_rootScrollLayerImpl->id();
1305 scroll.scrollDelta = scrollOffset - toSize(m_rootScrollLayerImpl->scrollPosition());
1306 scrollInfo->scrolls.append(scroll);
1307 m_rootScrollLayerImpl->setSentScrollDelta(scroll.scrollDelta);
[email protected]1c0c9bc2012-10-08 22:41:481308 scrollInfo->pageScaleDelta = pageScale / m_pinchZoomViewport.pageScaleFactor();
1309 m_pinchZoomViewport.setSentPageScaleDelta(scrollInfo->pageScaleDelta);
[email protected]94f206c12012-08-25 00:09:141310}
1311
[email protected]96baf3e2012-10-22 23:09:551312static void collectScrollDeltas(ScrollAndScaleSet* scrollInfo, LayerImpl* layerImpl)
[email protected]94f206c12012-08-25 00:09:141313{
1314 if (!layerImpl)
1315 return;
1316
1317 if (!layerImpl->scrollDelta().isZero()) {
1318 IntSize scrollDelta = flooredIntSize(layerImpl->scrollDelta());
[email protected]96baf3e2012-10-22 23:09:551319 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]94f206c12012-08-25 00:09:141320 scroll.layerId = layerImpl->id();
1321 scroll.scrollDelta = scrollDelta;
1322 scrollInfo->scrolls.append(scroll);
1323 layerImpl->setSentScrollDelta(scrollDelta);
1324 }
1325
1326 for (size_t i = 0; i < layerImpl->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031327 collectScrollDeltas(scrollInfo, layerImpl->children()[i]);
[email protected]94f206c12012-08-25 00:09:141328}
1329
[email protected]96baf3e2012-10-22 23:09:551330scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::processScrollDeltas()
[email protected]94f206c12012-08-25 00:09:141331{
[email protected]96baf3e2012-10-22 23:09:551332 scoped_ptr<ScrollAndScaleSet> scrollInfo(new ScrollAndScaleSet());
[email protected]94f206c12012-08-25 00:09:141333
1334 if (m_pinchGestureActive || m_pageScaleAnimation) {
[email protected]1c0c9bc2012-10-08 22:41:481335 scrollInfo->pageScaleDelta = 1;
1336 m_pinchZoomViewport.setSentPageScaleDelta(1);
1337 // FIXME(aelias): Make these painting optimizations compatible with
1338 // compositor-side scaling.
[email protected]65bfd9972012-10-19 03:39:371339 if (!Settings::pageScalePinchZoomEnabled()) {
[email protected]1c0c9bc2012-10-08 22:41:481340 if (m_pinchGestureActive)
1341 computePinchZoomDeltas(scrollInfo.get());
1342 else if (m_pageScaleAnimation.get())
1343 computeDoubleTapZoomDeltas(scrollInfo.get());
1344 }
[email protected]a9f4bf22012-10-11 23:39:211345 return scrollInfo.Pass();
[email protected]94f206c12012-08-25 00:09:141346 }
1347
1348 collectScrollDeltas(scrollInfo.get(), m_rootLayerImpl.get());
[email protected]1c0c9bc2012-10-08 22:41:481349 scrollInfo->pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
1350 m_pinchZoomViewport.setSentPageScaleDelta(scrollInfo->pageScaleDelta);
[email protected]94f206c12012-08-25 00:09:141351
[email protected]a9f4bf22012-10-11 23:39:211352 return scrollInfo.Pass();
[email protected]94f206c12012-08-25 00:09:141353}
1354
[email protected]96baf3e2012-10-22 23:09:551355WebTransformationMatrix LayerTreeHostImpl::implTransform() const
[email protected]1c0c9bc2012-10-08 22:41:481356{
1357 return m_pinchZoomViewport.implTransform();
1358}
1359
[email protected]96baf3e2012-10-22 23:09:551360void LayerTreeHostImpl::setFullRootLayerDamage()
[email protected]94f206c12012-08-25 00:09:141361{
1362 if (m_rootLayerImpl) {
[email protected]96baf3e2012-10-22 23:09:551363 RenderSurfaceImpl* renderSurface = m_rootLayerImpl->renderSurface();
[email protected]94f206c12012-08-25 00:09:141364 if (renderSurface)
1365 renderSurface->damageTracker()->forceFullDamageNextUpdate();
1366 }
1367}
1368
[email protected]96baf3e2012-10-22 23:09:551369void LayerTreeHostImpl::animatePageScale(double monotonicTime)
[email protected]94f206c12012-08-25 00:09:141370{
1371 if (!m_pageScaleAnimation || !m_rootScrollLayerImpl)
1372 return;
1373
1374 IntSize scrollTotal = flooredIntSize(m_rootScrollLayerImpl->scrollPosition() + m_rootScrollLayerImpl->scrollDelta());
1375
[email protected]1c0c9bc2012-10-08 22:41:481376 setPageScaleDelta(m_pageScaleAnimation->pageScaleAtTime(monotonicTime) / m_pinchZoomViewport.pageScaleFactor());
[email protected]94f206c12012-08-25 00:09:141377 IntSize nextScroll = m_pageScaleAnimation->scrollOffsetAtTime(monotonicTime);
[email protected]1c0c9bc2012-10-08 22:41:481378 nextScroll.scale(1 / m_pinchZoomViewport.pageScaleDelta());
[email protected]94f206c12012-08-25 00:09:141379 m_rootScrollLayerImpl->scrollBy(nextScroll - scrollTotal);
1380 m_client->setNeedsRedrawOnImplThread();
1381
1382 if (m_pageScaleAnimation->isAnimationCompleteAtTime(monotonicTime)) {
[email protected]0023e8b2012-10-15 12:52:451383 m_pageScaleAnimation.reset();
[email protected]94f206c12012-08-25 00:09:141384 m_client->setNeedsCommitOnImplThread();
1385 }
1386}
1387
[email protected]96baf3e2012-10-22 23:09:551388void LayerTreeHostImpl::animateLayers(double monotonicTime, double wallClockTime)
[email protected]94f206c12012-08-25 00:09:141389{
[email protected]65bfd9972012-10-19 03:39:371390 if (!Settings::acceleratedAnimationEnabled() || !m_needsAnimateLayers || !m_rootLayerImpl)
[email protected]94f206c12012-08-25 00:09:141391 return;
1392
[email protected]96baf3e2012-10-22 23:09:551393 TRACE_EVENT0("cc", "LayerTreeHostImpl::animateLayers");
[email protected]94f206c12012-08-25 00:09:141394
[email protected]96baf3e2012-10-22 23:09:551395 scoped_ptr<AnimationEventsVector> events(make_scoped_ptr(new AnimationEventsVector));
[email protected]94f206c12012-08-25 00:09:141396
1397 bool didAnimate = false;
1398 animateLayersRecursive(m_rootLayerImpl.get(), monotonicTime, wallClockTime, events.get(), didAnimate, m_needsAnimateLayers);
1399
[email protected]d3143c732012-10-05 19:17:591400 if (!events->empty())
[email protected]ec1d6d52012-10-10 01:28:571401 m_client->postAnimationEventsToMainThreadOnImplThread(events.Pass(), wallClockTime);
[email protected]94f206c12012-08-25 00:09:141402
1403 if (didAnimate)
1404 m_client->setNeedsRedrawOnImplThread();
1405
1406 setBackgroundTickingEnabled(!m_visible && m_needsAnimateLayers);
1407}
1408
[email protected]96baf3e2012-10-22 23:09:551409base::TimeDelta LayerTreeHostImpl::lowFrequencyAnimationInterval() const
[email protected]94f206c12012-08-25 00:09:141410{
[email protected]4481ddb622012-09-20 16:33:471411 return base::TimeDelta::FromSeconds(1);
[email protected]94f206c12012-08-25 00:09:141412}
1413
[email protected]96baf3e2012-10-22 23:09:551414void LayerTreeHostImpl::sendDidLoseContextRecursive(LayerImpl* current)
[email protected]94f206c12012-08-25 00:09:141415{
[email protected]1d993172012-10-18 18:15:041416 DCHECK(current);
[email protected]94f206c12012-08-25 00:09:141417 current->didLoseContext();
1418 if (current->maskLayer())
1419 sendDidLoseContextRecursive(current->maskLayer());
1420 if (current->replicaLayer())
1421 sendDidLoseContextRecursive(current->replicaLayer());
1422 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031423 sendDidLoseContextRecursive(current->children()[i]);
[email protected]94f206c12012-08-25 00:09:141424}
1425
[email protected]96baf3e2012-10-22 23:09:551426static void clearRenderSurfacesOnLayerImplRecursive(LayerImpl* current)
[email protected]94f206c12012-08-25 00:09:141427{
[email protected]1d993172012-10-18 18:15:041428 DCHECK(current);
[email protected]94f206c12012-08-25 00:09:141429 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]96baf3e2012-10-22 23:09:551430 clearRenderSurfacesOnLayerImplRecursive(current->children()[i]);
[email protected]94f206c12012-08-25 00:09:141431 current->clearRenderSurface();
1432}
1433
[email protected]96baf3e2012-10-22 23:09:551434void LayerTreeHostImpl::clearRenderSurfaces()
[email protected]94f206c12012-08-25 00:09:141435{
[email protected]96baf3e2012-10-22 23:09:551436 clearRenderSurfacesOnLayerImplRecursive(m_rootLayerImpl.get());
[email protected]94f206c12012-08-25 00:09:141437 m_renderSurfaceLayerList.clear();
1438}
1439
[email protected]96baf3e2012-10-22 23:09:551440std::string LayerTreeHostImpl::layerTreeAsText() const
[email protected]94f206c12012-08-25 00:09:141441{
[email protected]515e8d232012-09-10 19:15:271442 std::string str;
[email protected]94f206c12012-08-25 00:09:141443 if (m_rootLayerImpl) {
[email protected]515e8d232012-09-10 19:15:271444 str = m_rootLayerImpl->layerTreeAsText();
1445 str += "RenderSurfaces:\n";
1446 dumpRenderSurfaces(&str, 1, m_rootLayerImpl.get());
[email protected]94f206c12012-08-25 00:09:141447 }
[email protected]515e8d232012-09-10 19:15:271448 return str;
[email protected]94f206c12012-08-25 00:09:141449}
1450
[email protected]96baf3e2012-10-22 23:09:551451void LayerTreeHostImpl::dumpRenderSurfaces(std::string* str, int indent, const LayerImpl* layer) const
[email protected]94f206c12012-08-25 00:09:141452{
1453 if (layer->renderSurface())
[email protected]515e8d232012-09-10 19:15:271454 layer->renderSurface()->dumpSurface(str, indent);
[email protected]94f206c12012-08-25 00:09:141455
1456 for (size_t i = 0; i < layer->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031457 dumpRenderSurfaces(str, indent, layer->children()[i]);
[email protected]94f206c12012-08-25 00:09:141458}
1459
[email protected]96baf3e2012-10-22 23:09:551460int LayerTreeHostImpl::sourceAnimationFrameNumber() const
[email protected]94f206c12012-08-25 00:09:141461{
1462 return fpsCounter()->currentFrameNumber();
1463}
1464
[email protected]96baf3e2012-10-22 23:09:551465void LayerTreeHostImpl::renderingStats(RenderingStats* stats) const
[email protected]94f206c12012-08-25 00:09:141466{
[email protected]8b9af6b2012-09-27 00:36:361467 stats->numFramesSentToScreen = fpsCounter()->currentFrameNumber();
1468 stats->droppedFrameCount = fpsCounter()->droppedFrameCount();
[email protected]5c6fe1f82012-10-03 18:00:271469 stats->numImplThreadScrolls = m_numImplThreadScrolls;
1470 stats->numMainThreadScrolls = m_numMainThreadScrolls;
[email protected]94f206c12012-08-25 00:09:141471}
1472
[email protected]96baf3e2012-10-22 23:09:551473void LayerTreeHostImpl::animateScrollbars(double monotonicTime)
[email protected]94f206c12012-08-25 00:09:141474{
1475 animateScrollbarsRecursive(m_rootLayerImpl.get(), monotonicTime);
1476}
1477
[email protected]96baf3e2012-10-22 23:09:551478void LayerTreeHostImpl::animateScrollbarsRecursive(LayerImpl* layer, double monotonicTime)
[email protected]94f206c12012-08-25 00:09:141479{
1480 if (!layer)
1481 return;
1482
[email protected]96baf3e2012-10-22 23:09:551483 ScrollbarAnimationController* scrollbarController = layer->scrollbarAnimationController();
[email protected]94f206c12012-08-25 00:09:141484 if (scrollbarController && scrollbarController->animate(monotonicTime))
1485 m_client->setNeedsRedrawOnImplThread();
1486
1487 for (size_t i = 0; i < layer->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031488 animateScrollbarsRecursive(layer->children()[i], monotonicTime);
[email protected]94f206c12012-08-25 00:09:141489}
1490
[email protected]d3143c732012-10-05 19:17:591491} // namespace cc