blob: e8ff61f8713282f897df59dd3571e1df1315dee9 [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
[email protected]55a124d02012-10-22 03:07:135#include "cc/layer_tree_host_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
8
[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"
[email protected]3b10a302012-11-07 21:16:4025#include "cc/prioritized_resource_manager.h"
[email protected]f57bbc02012-11-21 07:02:1526#include "cc/quad_culler.h"
[email protected]55a124d02012-10-22 03:07:1327#include "cc/render_pass_draw_quad.h"
[email protected]c4040a522012-10-21 15:01:4028#include "cc/rendering_stats.h"
29#include "cc/scrollbar_animation_controller.h"
30#include "cc/scrollbar_layer_impl.h"
[email protected]f57bbc02012-11-21 07:02:1531#include "cc/shared_quad_state.h"
[email protected]4456eee22012-10-19 18:16:3832#include "cc/single_thread_proxy.h"
[email protected]c4040a522012-10-21 15:01:4033#include "cc/software_renderer.h"
[email protected]f57bbc02012-11-21 07:02:1534#include "cc/solid_color_draw_quad.h"
[email protected]a8461d82012-10-16 21:11:1435#include "cc/texture_uploader.h"
[email protected]d455d552012-11-02 00:19:0636#include "ui/gfx/size_conversions.h"
[email protected]c9c1ebe2012-11-05 20:46:1337#include "ui/gfx/vector2d_conversions.h"
[email protected]94f206c12012-08-25 00:09:1438
[email protected]94f206c12012-08-25 00:09:1439namespace {
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)
[email protected]e39bf212012-11-22 21:04:0361 , m_deviceScaleFactor(1)
[email protected]1c0c9bc2012-10-08 22:41:4862{
63}
64
[email protected]96baf3e2012-10-22 23:09:5565float PinchZoomViewport::totalPageScaleFactor() const
[email protected]1c0c9bc2012-10-08 22:41:4866{
67 return m_pageScaleFactor * m_pageScaleDelta;
68}
69
[email protected]96baf3e2012-10-22 23:09:5570void PinchZoomViewport::setPageScaleDelta(float delta)
[email protected]1c0c9bc2012-10-08 22:41:4871{
72 // Clamp to the current min/max limits.
73 float totalPageScaleFactor = m_pageScaleFactor * delta;
74 if (m_minPageScaleFactor && totalPageScaleFactor < m_minPageScaleFactor)
75 delta = m_minPageScaleFactor / m_pageScaleFactor;
76 else if (m_maxPageScaleFactor && totalPageScaleFactor > m_maxPageScaleFactor)
77 delta = m_maxPageScaleFactor / m_pageScaleFactor;
78
79 if (delta == m_pageScaleDelta)
80 return;
81
82 m_pageScaleDelta = delta;
83}
84
[email protected]96baf3e2012-10-22 23:09:5585bool PinchZoomViewport::setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor)
[email protected]1c0c9bc2012-10-08 22:41:4886{
[email protected]1d993172012-10-18 18:15:0487 DCHECK(pageScaleFactor);
[email protected]1c0c9bc2012-10-08 22:41:4888
89 if (m_sentPageScaleDelta == 1 && pageScaleFactor == m_pageScaleFactor && minPageScaleFactor == m_minPageScaleFactor && maxPageScaleFactor == m_maxPageScaleFactor)
90 return false;
91
92 m_minPageScaleFactor = minPageScaleFactor;
93 m_maxPageScaleFactor = maxPageScaleFactor;
94
95 m_pageScaleFactor = pageScaleFactor;
96 return true;
97}
98
[email protected]aad0a0072012-11-01 18:15:5899gfx::RectF PinchZoomViewport::bounds() const
[email protected]1c0c9bc2012-10-08 22:41:48100{
[email protected]2e6486012012-11-10 00:03:19101 gfx::RectF bounds(gfx::PointF(), m_layoutViewportSize);
102 bounds.Scale(1 / totalPageScaleFactor());
[email protected]e39bf212012-11-22 21:04:03103 bounds += m_zoomedViewportOffset;
[email protected]1c0c9bc2012-10-08 22:41:48104 return bounds;
105}
106
[email protected]c9c1ebe2012-11-05 20:46:13107gfx::Vector2dF PinchZoomViewport::applyScroll(const gfx::Vector2dF& delta)
[email protected]1c0c9bc2012-10-08 22:41:48108{
[email protected]d0f98362012-11-01 23:02:38109 gfx::Vector2dF overflow;
[email protected]2e6486012012-11-10 00:03:19110 gfx::RectF pinchedBounds = bounds() + delta;
[email protected]1c0c9bc2012-10-08 22:41:48111
[email protected]1c0c9bc2012-10-08 22:41:48112 if (pinchedBounds.x() < 0) {
[email protected]d0f98362012-11-01 23:02:38113 overflow.set_x(pinchedBounds.x());
114 pinchedBounds.set_x(0);
[email protected]1c0c9bc2012-10-08 22:41:48115 }
116
117 if (pinchedBounds.y() < 0) {
[email protected]d0f98362012-11-01 23:02:38118 overflow.set_y(pinchedBounds.y());
119 pinchedBounds.set_y(0);
[email protected]1c0c9bc2012-10-08 22:41:48120 }
121
[email protected]d0f98362012-11-01 23:02:38122 if (pinchedBounds.right() > m_layoutViewportSize.width()) {
123 overflow.set_x(pinchedBounds.right() - m_layoutViewportSize.width());
[email protected]2e6486012012-11-10 00:03:19124 pinchedBounds += gfx::Vector2dF(m_layoutViewportSize.width() - pinchedBounds.right(), 0);
[email protected]1c0c9bc2012-10-08 22:41:48125 }
126
[email protected]d0f98362012-11-01 23:02:38127 if (pinchedBounds.bottom() > m_layoutViewportSize.height()) {
128 overflow.set_y(pinchedBounds.bottom() - m_layoutViewportSize.height());
[email protected]2e6486012012-11-10 00:03:19129 pinchedBounds += gfx::Vector2dF(0, m_layoutViewportSize.height() - pinchedBounds.bottom());
[email protected]1c0c9bc2012-10-08 22:41:48130 }
[email protected]e39bf212012-11-22 21:04:03131 m_zoomedViewportOffset = pinchedBounds.OffsetFromOrigin();
[email protected]1c0c9bc2012-10-08 22:41:48132
[email protected]c9c1ebe2012-11-05 20:46:13133 return overflow;
[email protected]1c0c9bc2012-10-08 22:41:48134}
135
[email protected]c8686a02012-11-27 08:29:00136gfx::Transform PinchZoomViewport::implTransform(bool pageScalePinchZoomEnabled) const
[email protected]1c0c9bc2012-10-08 22:41:48137{
[email protected]c8686a02012-11-27 08:29:00138 gfx::Transform transform;
139 transform.Scale(m_pageScaleDelta, m_pageScaleDelta);
[email protected]1c0c9bc2012-10-08 22:41:48140
141 // If the pinch state is applied in the impl, then push it to the
142 // impl transform, otherwise the scale is handled by WebCore.
[email protected]9bdcfd642012-11-14 21:24:26143 if (pageScalePinchZoomEnabled) {
[email protected]c8686a02012-11-27 08:29:00144 transform.Scale(m_pageScaleFactor, m_pageScaleFactor);
[email protected]e39bf212012-11-22 21:04:03145 // The offset needs to be scaled by deviceScaleFactor as this transform
146 // needs to work with physical pixels.
147 gfx::Vector2dF zoomedDeviceViewportOffset = gfx::ScaleVector2d(m_zoomedViewportOffset, m_deviceScaleFactor);
[email protected]c8686a02012-11-27 08:29:00148 transform.Translate(-zoomedDeviceViewportOffset.x(), -zoomedDeviceViewportOffset.y());
[email protected]1c0c9bc2012-10-08 22:41:48149 }
150
151 return transform;
152}
153
[email protected]96baf3e2012-10-22 23:09:55154class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient {
[email protected]94f206c12012-08-25 00:09:14155public:
[email protected]96baf3e2012-10-22 23:09:55156 static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> create(LayerTreeHostImpl* layerTreeHostImpl, scoped_refptr<DelayBasedTimeSource> timeSource)
[email protected]94f206c12012-08-25 00:09:14157 {
[email protected]96baf3e2012-10-22 23:09:55158 return make_scoped_ptr(new LayerTreeHostImplTimeSourceAdapter(layerTreeHostImpl, timeSource));
[email protected]94f206c12012-08-25 00:09:14159 }
[email protected]96baf3e2012-10-22 23:09:55160 virtual ~LayerTreeHostImplTimeSourceAdapter()
[email protected]94f206c12012-08-25 00:09:14161 {
162 m_timeSource->setClient(0);
163 m_timeSource->setActive(false);
164 }
165
166 virtual void onTimerTick() OVERRIDE
167 {
[email protected]30faac92012-10-29 00:06:29168 m_layerTreeHostImpl->animate(base::TimeTicks::Now(), base::Time::Now());
[email protected]94f206c12012-08-25 00:09:14169 }
170
171 void setActive(bool active)
172 {
173 if (active != m_timeSource->active())
174 m_timeSource->setActive(active);
175 }
176
177private:
[email protected]96baf3e2012-10-22 23:09:55178 LayerTreeHostImplTimeSourceAdapter(LayerTreeHostImpl* layerTreeHostImpl, scoped_refptr<DelayBasedTimeSource> timeSource)
[email protected]94f206c12012-08-25 00:09:14179 : m_layerTreeHostImpl(layerTreeHostImpl)
180 , m_timeSource(timeSource)
181 {
182 m_timeSource->setClient(this);
183 }
184
[email protected]96baf3e2012-10-22 23:09:55185 LayerTreeHostImpl* m_layerTreeHostImpl;
186 scoped_refptr<DelayBasedTimeSource> m_timeSource;
[email protected]fd2d4f22012-09-28 22:57:20187
[email protected]96baf3e2012-10-22 23:09:55188 DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter);
[email protected]94f206c12012-08-25 00:09:14189};
190
[email protected]96baf3e2012-10-22 23:09:55191LayerTreeHostImpl::FrameData::FrameData()
[email protected]493067512012-09-19 23:34:10192{
193}
194
[email protected]96baf3e2012-10-22 23:09:55195LayerTreeHostImpl::FrameData::~FrameData()
[email protected]493067512012-09-19 23:34:10196{
197}
198
[email protected]61de5812012-11-08 07:03:44199scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::create(const LayerTreeSettings& settings, LayerTreeHostImplClient* client, Proxy* proxy)
[email protected]94f206c12012-08-25 00:09:14200{
[email protected]61de5812012-11-08 07:03:44201 return make_scoped_ptr(new LayerTreeHostImpl(settings, client, proxy));
[email protected]94f206c12012-08-25 00:09:14202}
203
[email protected]61de5812012-11-08 07:03:44204LayerTreeHostImpl::LayerTreeHostImpl(const LayerTreeSettings& settings, LayerTreeHostImplClient* client, Proxy* proxy)
[email protected]94f206c12012-08-25 00:09:14205 : m_client(client)
[email protected]61de5812012-11-08 07:03:44206 , m_proxy(proxy)
[email protected]94f206c12012-08-25 00:09:14207 , m_sourceFrameNumber(-1)
208 , m_rootScrollLayerImpl(0)
209 , m_currentlyScrollingLayerImpl(0)
210 , m_hudLayerImpl(0)
211 , m_scrollingLayerIdFromPreviousTree(-1)
[email protected]31bfe272012-10-19 18:49:52212 , m_scrollDeltaIsInViewportSpace(false)
[email protected]94f206c12012-08-25 00:09:14213 , m_settings(settings)
214 , m_deviceScaleFactor(1)
215 , m_visible(true)
216 , m_contentsTexturesPurged(false)
[email protected]3b10a302012-11-07 21:16:40217 , m_managedMemoryPolicy(PrioritizedResourceManager::defaultMemoryAllocationLimit(),
[email protected]96baf3e2012-10-22 23:09:55218 PriorityCalculator::allowEverythingCutoff(),
[email protected]a0a00842012-10-22 22:50:28219 0,
[email protected]96baf3e2012-10-22 23:09:55220 PriorityCalculator::allowNothingCutoff())
[email protected]94f206c12012-08-25 00:09:14221 , m_backgroundColor(0)
222 , m_hasTransparentBackground(false)
223 , m_needsAnimateLayers(false)
224 , m_pinchGestureActive(false)
[email protected]61de5812012-11-08 07:03:44225 , m_fpsCounter(FrameRateCounter::create(m_proxy->hasImplThread()))
[email protected]96baf3e2012-10-22 23:09:55226 , m_debugRectHistory(DebugRectHistory::create())
[email protected]5c6fe1f82012-10-03 18:00:27227 , m_numImplThreadScrolls(0)
228 , m_numMainThreadScrolls(0)
[email protected]9c2be6a2012-11-27 19:16:10229 , m_cumulativeNumLayersInLayerTree(0)
[email protected]94f206c12012-08-25 00:09:14230{
[email protected]61de5812012-11-08 07:03:44231 DCHECK(m_proxy->isImplThread());
[email protected]94f206c12012-08-25 00:09:14232 didVisibilityChange(this, m_visible);
233}
234
[email protected]96baf3e2012-10-22 23:09:55235LayerTreeHostImpl::~LayerTreeHostImpl()
[email protected]94f206c12012-08-25 00:09:14236{
[email protected]61de5812012-11-08 07:03:44237 DCHECK(m_proxy->isImplThread());
[email protected]96baf3e2012-10-22 23:09:55238 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]94f206c12012-08-25 00:09:14239
240 if (m_rootLayerImpl)
241 clearRenderSurfaces();
242}
243
[email protected]96baf3e2012-10-22 23:09:55244void LayerTreeHostImpl::beginCommit()
[email protected]94f206c12012-08-25 00:09:14245{
246}
247
[email protected]96baf3e2012-10-22 23:09:55248void LayerTreeHostImpl::commitComplete()
[email protected]94f206c12012-08-25 00:09:14249{
[email protected]96baf3e2012-10-22 23:09:55250 TRACE_EVENT0("cc", "LayerTreeHostImpl::commitComplete");
[email protected]94f206c12012-08-25 00:09:14251 // Recompute max scroll position; must be after layer content bounds are
252 // updated.
[email protected]c9c1ebe2012-11-05 20:46:13253 updateMaxScrollOffset();
[email protected]3d21e022012-10-25 20:03:08254 m_client->sendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14255}
256
[email protected]96baf3e2012-10-22 23:09:55257bool LayerTreeHostImpl::canDraw()
[email protected]94f206c12012-08-25 00:09:14258{
[email protected]8db2213c2012-09-05 22:08:21259 // Note: If you are changing this function or any other function that might
260 // affect the result of canDraw, make sure to call m_client->onCanDrawStateChanged
261 // in the proper places and update the notifyIfCanDrawChanged test.
262
[email protected]94f206c12012-08-25 00:09:14263 if (!m_rootLayerImpl) {
[email protected]96baf3e2012-10-22 23:09:55264 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw no root layer");
[email protected]94f206c12012-08-25 00:09:14265 return false;
266 }
[email protected]aad0a0072012-11-01 18:15:58267 if (deviceViewportSize().IsEmpty()) {
[email protected]96baf3e2012-10-22 23:09:55268 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw empty viewport");
[email protected]94f206c12012-08-25 00:09:14269 return false;
270 }
271 if (!m_renderer) {
[email protected]96baf3e2012-10-22 23:09:55272 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw no renderer");
[email protected]94f206c12012-08-25 00:09:14273 return false;
274 }
275 if (m_contentsTexturesPurged) {
[email protected]96baf3e2012-10-22 23:09:55276 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw contents textures purged");
[email protected]94f206c12012-08-25 00:09:14277 return false;
278 }
279 return true;
280}
281
[email protected]96baf3e2012-10-22 23:09:55282GraphicsContext* LayerTreeHostImpl::context() const
[email protected]94f206c12012-08-25 00:09:14283{
284 return m_context.get();
285}
286
[email protected]30faac92012-10-29 00:06:29287void LayerTreeHostImpl::animate(base::TimeTicks monotonicTime, base::Time wallClockTime)
[email protected]94f206c12012-08-25 00:09:14288{
289 animatePageScale(monotonicTime);
290 animateLayers(monotonicTime, wallClockTime);
[email protected]94f206c12012-08-25 00:09:14291 animateScrollbars(monotonicTime);
292}
293
[email protected]69a2a5be2012-11-14 06:51:44294void LayerTreeHostImpl::startPageScaleAnimation(gfx::Vector2d targetOffset, bool anchorPoint, float pageScale, base::TimeTicks startTime, base::TimeDelta duration)
[email protected]94f206c12012-08-25 00:09:14295{
296 if (!m_rootScrollLayerImpl)
297 return;
298
[email protected]c9c1ebe2012-11-05 20:46:13299 gfx::Vector2dF scrollTotal = m_rootScrollLayerImpl->scrollOffset() + m_rootScrollLayerImpl->scrollDelta();
[email protected]f6250742012-11-09 04:46:56300 gfx::SizeF scaledContentSize = contentSize();
[email protected]9bdcfd642012-11-14 21:24:26301 if (!m_settings.pageScalePinchZoomEnabled) {
[email protected]f6250742012-11-09 04:46:56302 scrollTotal.Scale(1 / m_pinchZoomViewport.pageScaleFactor());
[email protected]01a15a72012-11-10 09:34:28303 scaledContentSize.Scale(1 / m_pinchZoomViewport.pageScaleFactor());
[email protected]f6250742012-11-09 04:46:56304 }
[email protected]01a15a72012-11-10 09:34:28305 gfx::SizeF viewportSize = gfx::ScaleSize(m_deviceViewportSize, 1 / m_deviceScaleFactor);
[email protected]94f206c12012-08-25 00:09:14306
[email protected]30faac92012-10-29 00:06:29307 double startTimeSeconds = (startTime - base::TimeTicks()).InSecondsF();
[email protected]f6250742012-11-09 04:46:56308 m_pageScaleAnimation = PageScaleAnimation::create(scrollTotal, m_pinchZoomViewport.totalPageScaleFactor(), viewportSize, scaledContentSize, startTimeSeconds);
[email protected]94f206c12012-08-25 00:09:14309
310 if (anchorPoint) {
[email protected]69a2a5be2012-11-14 06:51:44311 gfx::Vector2dF anchor(targetOffset);
[email protected]9bdcfd642012-11-14 21:24:26312 if (!m_settings.pageScalePinchZoomEnabled)
[email protected]f6250742012-11-09 04:46:56313 anchor.Scale(1 / pageScale);
314 m_pageScaleAnimation->zoomWithAnchor(anchor, pageScale, duration.InSecondsF());
315 } else {
[email protected]69a2a5be2012-11-14 06:51:44316 gfx::Vector2dF scaledTargetOffset = targetOffset;
[email protected]9bdcfd642012-11-14 21:24:26317 if (!m_settings.pageScalePinchZoomEnabled)
[email protected]69a2a5be2012-11-14 06:51:44318 scaledTargetOffset.Scale(1 / pageScale);
319 m_pageScaleAnimation->zoomTo(scaledTargetOffset, pageScale, duration.InSecondsF());
[email protected]f6250742012-11-09 04:46:56320 }
[email protected]94f206c12012-08-25 00:09:14321
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]2f1acc262012-11-16 21:42:22331bool LayerTreeHostImpl::haveTouchEventHandlersAt(const gfx::Point& viewportPoint)
332{
333
334 gfx::PointF deviceViewportPoint = gfx::ScalePoint(viewportPoint, m_deviceScaleFactor);
335
336 // First find out which layer was hit from the saved list of visible layers
337 // in the most recent frame.
338 LayerImpl* layerImplHitByPointInTouchHandlerRegion = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(deviceViewportPoint, m_renderSurfaceLayerList);
339
340 if (layerImplHitByPointInTouchHandlerRegion)
341 return true;
342
343 return false;
344}
345
[email protected]96baf3e2012-10-22 23:09:55346void LayerTreeHostImpl::trackDamageForAllSurfaces(LayerImpl* rootDrawLayer, const LayerList& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14347{
348 // For now, we use damage tracking to compute a global scissor. To do this, we must
349 // compute all damage tracking before drawing anything, so that we know the root
350 // damage rect. The root damage rect is then used to scissor each surface.
351
352 for (int surfaceIndex = renderSurfaceLayerList.size() - 1; surfaceIndex >= 0 ; --surfaceIndex) {
[email protected]96baf3e2012-10-22 23:09:55353 LayerImpl* renderSurfaceLayer = renderSurfaceLayerList[surfaceIndex];
354 RenderSurfaceImpl* renderSurface = renderSurfaceLayer->renderSurface();
[email protected]1d993172012-10-18 18:15:04355 DCHECK(renderSurface);
[email protected]4000abf2012-10-23 04:45:45356 renderSurface->damageTracker()->updateDamageTrackingState(renderSurface->layerList(), renderSurfaceLayer->id(), renderSurface->surfacePropertyChangedOnlyFromDescendant(), renderSurface->contentRect(), renderSurfaceLayer->maskLayer(), renderSurfaceLayer->filters(), renderSurfaceLayer->filter());
[email protected]94f206c12012-08-25 00:09:14357 }
358}
359
[email protected]96baf3e2012-10-22 23:09:55360void LayerTreeHostImpl::updateRootScrollLayerImplTransform()
[email protected]1c0c9bc2012-10-08 22:41:48361{
362 if (m_rootScrollLayerImpl) {
363 m_rootScrollLayerImpl->setImplTransform(implTransform());
364 }
365}
366
[email protected]96baf3e2012-10-22 23:09:55367void LayerTreeHostImpl::calculateRenderSurfaceLayerList(LayerList& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14368{
[email protected]1d993172012-10-18 18:15:04369 DCHECK(renderSurfaceLayerList.empty());
370 DCHECK(m_rootLayerImpl);
371 DCHECK(m_renderer); // For maxTextureSize.
[email protected]94f206c12012-08-25 00:09:14372
373 {
[email protected]1c0c9bc2012-10-08 22:41:48374 updateRootScrollLayerImplTransform();
375
[email protected]96baf3e2012-10-22 23:09:55376 TRACE_EVENT0("cc", "LayerTreeHostImpl::calcDrawEtc");
[email protected]518ee582012-10-24 18:29:44377 float pageScaleFactor = m_pinchZoomViewport.pageScaleFactor();
378 LayerTreeHostCommon::calculateDrawTransforms(m_rootLayerImpl.get(), deviceViewportSize(), m_deviceScaleFactor, pageScaleFactor, &m_layerSorter, rendererCapabilities().maxTextureSize, renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14379
380 trackDamageForAllSurfaces(m_rootLayerImpl.get(), renderSurfaceLayerList);
381 }
382}
383
[email protected]96baf3e2012-10-22 23:09:55384void LayerTreeHostImpl::FrameData::appendRenderPass(scoped_ptr<RenderPass> renderPass)
[email protected]467b3612012-08-28 07:41:16385{
[email protected]96baf3e2012-10-22 23:09:55386 RenderPass* pass = renderPass.get();
[email protected]f8ad8342012-09-27 20:07:02387 renderPasses.push_back(pass);
[email protected]f57bbc02012-11-21 07:02:15388 renderPassesById.set(pass->id, renderPass.Pass());
389}
390
391static void appendQuadsForLayer(RenderPass* targetRenderPass, LayerImpl* layer, OcclusionTrackerImpl& occlusionTracker, AppendQuadsData& appendQuadsData)
392{
393 bool forSurface = false;
394 QuadCuller quadCuller(targetRenderPass->quad_list,
395 targetRenderPass->shared_quad_state_list,
396 layer,
397 occlusionTracker,
398 layer->showDebugBorders(),
399 forSurface);
400 layer->appendQuads(quadCuller, appendQuadsData);
401}
402
403static void appendQuadsForRenderSurfaceLayer(RenderPass* targetRenderPass, LayerImpl* layer, const RenderPass* contributingRenderPass, OcclusionTrackerImpl& occlusionTracker, AppendQuadsData& appendQuadsData)
404{
405 bool forSurface = true;
406 QuadCuller quadCuller(targetRenderPass->quad_list,
407 targetRenderPass->shared_quad_state_list,
408 layer,
409 occlusionTracker,
410 layer->showDebugBorders(),
411 forSurface);
412
413 bool isReplica = false;
414 layer->renderSurface()->appendQuads(quadCuller,
415 appendQuadsData,
416 isReplica,
417 contributingRenderPass->id);
418
419 // Add replica after the surface so that it appears below the surface.
420 if (layer->hasReplica()) {
421 isReplica = true;
422 layer->renderSurface()->appendQuads(quadCuller,
423 appendQuadsData,
424 isReplica,
425 contributingRenderPass->id);
426 }
427}
428
429static void appendQuadsToFillScreen(RenderPass* targetRenderPass, LayerImpl* rootLayer, SkColor screenBackgroundColor, const OcclusionTrackerImpl& occlusionTracker)
430{
431 if (!rootLayer || !SkColorGetA(screenBackgroundColor))
432 return;
433
434 Region fillRegion = occlusionTracker.computeVisibleRegionInScreen();
435 if (fillRegion.IsEmpty())
436 return;
437
438 bool forSurface = false;
439 QuadCuller quadCuller(targetRenderPass->quad_list,
440 targetRenderPass->shared_quad_state_list,
441 rootLayer,
442 occlusionTracker,
443 rootLayer->showDebugBorders(),
444 forSurface);
445
446 // Manually create the quad state for the gutter quads, as the root layer
447 // doesn't have any bounds and so can't generate this itself.
448 // FIXME: Make the gutter quads generated by the solid color layer (make it smarter about generating quads to fill unoccluded areas).
449
[email protected]c8686a02012-11-27 08:29:00450 DCHECK(rootLayer->screenSpaceTransform().IsInvertible());
[email protected]f57bbc02012-11-21 07:02:15451
452 gfx::Rect rootTargetRect = rootLayer->renderSurface()->contentRect();
453 float opacity = 1;
454 SharedQuadState* sharedQuadState = quadCuller.useSharedQuadState(SharedQuadState::Create());
455 sharedQuadState->SetAll(rootLayer->drawTransform(),
456 rootTargetRect,
457 rootTargetRect,
[email protected]dc462d782012-11-21 21:43:01458 rootTargetRect,
459 false,
[email protected]f57bbc02012-11-21 07:02:15460 opacity);
461
462 AppendQuadsData appendQuadsData;
[email protected]c8686a02012-11-27 08:29:00463 gfx::Transform transformToLayerSpace = MathUtil::inverse(rootLayer->screenSpaceTransform());
[email protected]f57bbc02012-11-21 07:02:15464 for (Region::Iterator fillRects(fillRegion); fillRects.has_rect(); fillRects.next()) {
465 // The root layer transform is composed of translations and scales only,
466 // no perspective, so mapping is sufficient.
467 gfx::Rect layerRect = MathUtil::mapClippedRect(transformToLayerSpace, fillRects.rect());
468 // Skip the quad culler and just append the quads directly to avoid
469 // occlusion checks.
470 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
471 quad->SetNew(sharedQuadState, layerRect, screenBackgroundColor);
472 quadCuller.append(quad.PassAs<DrawQuad>(), appendQuadsData);
473 }
[email protected]467b3612012-08-28 07:41:16474}
475
[email protected]96baf3e2012-10-22 23:09:55476bool LayerTreeHostImpl::calculateRenderPasses(FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14477{
[email protected]1d993172012-10-18 18:15:04478 DCHECK(frame.renderPasses.empty());
[email protected]94f206c12012-08-25 00:09:14479
480 calculateRenderSurfaceLayerList(*frame.renderSurfaceLayerList);
481
[email protected]96baf3e2012-10-22 23:09:55482 TRACE_EVENT1("cc", "LayerTreeHostImpl::calculateRenderPasses", "renderSurfaceLayerList.size()", static_cast<long long unsigned>(frame.renderSurfaceLayerList->size()));
[email protected]94f206c12012-08-25 00:09:14483
484 // Create the render passes in dependency order.
[email protected]94f206c12012-08-25 00:09:14485 for (int surfaceIndex = frame.renderSurfaceLayerList->size() - 1; surfaceIndex >= 0 ; --surfaceIndex) {
[email protected]96baf3e2012-10-22 23:09:55486 LayerImpl* renderSurfaceLayer = (*frame.renderSurfaceLayerList)[surfaceIndex];
[email protected]467b3612012-08-28 07:41:16487 renderSurfaceLayer->renderSurface()->appendRenderPasses(frame);
[email protected]94f206c12012-08-25 00:09:14488 }
489
[email protected]9bdcfd642012-11-14 21:24:26490 bool recordMetricsForFrame = m_settings.showOverdrawInTracing && base::debug::TraceLog::GetInstance() && base::debug::TraceLog::GetInstance()->IsEnabled();
[email protected]96baf3e2012-10-22 23:09:55491 OcclusionTrackerImpl occlusionTracker(m_rootLayerImpl->renderSurface()->contentRect(), recordMetricsForFrame);
[email protected]94f206c12012-08-25 00:09:14492 occlusionTracker.setMinimumTrackingSize(m_settings.minimumOcclusionTrackingSize);
493
494 if (settings().showOccludingRects)
495 occlusionTracker.setOccludingScreenSpaceRectsContainer(&frame.occludingScreenSpaceRects);
[email protected]4d8804e2012-11-15 01:51:10496 if (settings().showNonOccludingRects)
497 occlusionTracker.setNonOccludingScreenSpaceRectsContainer(&frame.nonOccludingScreenSpaceRects);
[email protected]94f206c12012-08-25 00:09:14498
499 // 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:55500 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14501
502 // Typically when we are missing a texture and use a checkerboard quad, we still draw the frame. However when the layer being
503 // checkerboarded is moving due to an impl-animation, we drop the frame to avoid flashing due to the texture suddenly appearing
504 // in the future.
505 bool drawFrame = true;
506
[email protected]96baf3e2012-10-22 23:09:55507 LayerIteratorType end = LayerIteratorType::end(frame.renderSurfaceLayerList);
508 for (LayerIteratorType it = LayerIteratorType::begin(frame.renderSurfaceLayerList); it != end; ++it) {
509 RenderPass::Id targetRenderPassId = it.targetRenderSurfaceLayer()->renderSurface()->renderPassId();
510 RenderPass* targetRenderPass = frame.renderPassesById.get(targetRenderPassId);
[email protected]94f206c12012-08-25 00:09:14511
512 occlusionTracker.enterLayer(it);
513
[email protected]f57bbc02012-11-21 07:02:15514 AppendQuadsData appendQuadsData(targetRenderPass->id);
[email protected]89228202012-08-29 03:20:30515
[email protected]94f206c12012-08-25 00:09:14516 if (it.representsContributingRenderSurface()) {
[email protected]96baf3e2012-10-22 23:09:55517 RenderPass::Id contributingRenderPassId = it->renderSurface()->renderPassId();
518 RenderPass* contributingRenderPass = frame.renderPassesById.get(contributingRenderPassId);
[email protected]f57bbc02012-11-21 07:02:15519 appendQuadsForRenderSurfaceLayer(targetRenderPass, *it, contributingRenderPass, occlusionTracker, appendQuadsData);
[email protected]aad0a0072012-11-01 18:15:58520 } else if (it.representsItself() && !it->visibleContentRect().IsEmpty()) {
[email protected]94f206c12012-08-25 00:09:14521 bool hasOcclusionFromOutsideTargetSurface;
[email protected]710ffc02012-10-30 21:42:02522 bool implDrawTransformIsUnknown = false;
523 if (occlusionTracker.occluded(it->renderTarget(), it->visibleContentRect(), it->drawTransform(), implDrawTransformIsUnknown, it->drawableContentRect(), &hasOcclusionFromOutsideTargetSurface))
[email protected]89228202012-08-29 03:20:30524 appendQuadsData.hadOcclusionFromOutsideTargetSurface |= hasOcclusionFromOutsideTargetSurface;
525 else {
[email protected]94f206c12012-08-25 00:09:14526 it->willDraw(m_resourceProvider.get());
[email protected]d58499a2012-10-09 22:27:47527 frame.willDrawLayers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57528
529 if (it->hasContributingDelegatedRenderPasses()) {
[email protected]96baf3e2012-10-22 23:09:55530 RenderPass::Id contributingRenderPassId = it->firstContributingRenderPassId();
[email protected]7d929c02012-09-20 17:26:57531 while (frame.renderPassesById.contains(contributingRenderPassId)) {
[email protected]96baf3e2012-10-22 23:09:55532 RenderPass* renderPass = frame.renderPassesById.get(contributingRenderPassId);
[email protected]7d929c02012-09-20 17:26:57533
[email protected]f57bbc02012-11-21 07:02:15534 AppendQuadsData appendQuadsData(renderPass->id);
535 appendQuadsForLayer(renderPass, *it, occlusionTracker, appendQuadsData);
[email protected]7d929c02012-09-20 17:26:57536
537 contributingRenderPassId = it->nextContributingRenderPassId(contributingRenderPassId);
538 }
539 }
540
[email protected]f57bbc02012-11-21 07:02:15541 appendQuadsForLayer(targetRenderPass, *it, occlusionTracker, appendQuadsData);
[email protected]94f206c12012-08-25 00:09:14542 }
[email protected]9c2be6a2012-11-27 19:16:10543
544 ++m_cumulativeNumLayersInLayerTree;
[email protected]94f206c12012-08-25 00:09:14545 }
546
[email protected]89228202012-08-29 03:20:30547 if (appendQuadsData.hadOcclusionFromOutsideTargetSurface)
[email protected]f57bbc02012-11-21 07:02:15548 targetRenderPass->has_occlusion_from_outside_target_surface = true;
[email protected]89228202012-08-29 03:20:30549
550 if (appendQuadsData.hadMissingTiles) {
[email protected]94f206c12012-08-25 00:09:14551 bool layerHasAnimatingTransform = it->screenSpaceTransformIsAnimating() || it->drawTransformIsAnimating();
[email protected]22619922012-11-14 17:58:10552 if (layerHasAnimatingTransform)
[email protected]94f206c12012-08-25 00:09:14553 drawFrame = false;
554 }
555
556 occlusionTracker.leaveLayer(it);
557 }
558
[email protected]1d993172012-10-18 18:15:04559#ifndef NDEBUG
[email protected]94f206c12012-08-25 00:09:14560 for (size_t i = 0; i < frame.renderPasses.size(); ++i) {
[email protected]f57bbc02012-11-21 07:02:15561 for (size_t j = 0; j < frame.renderPasses[i]->quad_list.size(); ++j)
562 DCHECK(frame.renderPasses[i]->quad_list[j]->shared_quad_state);
563 DCHECK(frame.renderPassesById.contains(frame.renderPasses[i]->id));
[email protected]94f206c12012-08-25 00:09:14564 }
565#endif
566
567 if (!m_hasTransparentBackground) {
[email protected]f57bbc02012-11-21 07:02:15568 frame.renderPasses.back()->has_transparent_background = false;
569 appendQuadsToFillScreen(frame.renderPasses.back(), m_rootLayerImpl.get(), m_backgroundColor, occlusionTracker);
[email protected]94f206c12012-08-25 00:09:14570 }
571
572 if (drawFrame)
573 occlusionTracker.overdrawMetrics().recordMetrics(this);
574
575 removeRenderPasses(CullRenderPassesWithNoQuads(), frame);
576 m_renderer->decideRenderPassAllocationsForFrame(frame.renderPasses);
577 removeRenderPasses(CullRenderPassesWithCachedTextures(*m_renderer), frame);
578
579 return drawFrame;
580}
581
[email protected]30faac92012-10-29 00:06:29582void LayerTreeHostImpl::animateLayersRecursive(LayerImpl* current, base::TimeTicks monotonicTime, base::Time wallClockTime, AnimationEventsVector* events, bool& didAnimate, bool& needsAnimateLayers)
[email protected]94f206c12012-08-25 00:09:14583{
584 bool subtreeNeedsAnimateLayers = false;
585
[email protected]96baf3e2012-10-22 23:09:55586 LayerAnimationController* currentController = current->layerAnimationController();
[email protected]94f206c12012-08-25 00:09:14587
588 bool hadActiveAnimation = currentController->hasActiveAnimation();
[email protected]30faac92012-10-29 00:06:29589 double monotonicTimeSeconds = (monotonicTime - base::TimeTicks()).InSecondsF();
590 currentController->animate(monotonicTimeSeconds, events);
[email protected]94f206c12012-08-25 00:09:14591 bool startedAnimation = events->size() > 0;
592
593 // We animated if we either ticked a running animation, or started a new animation.
594 if (hadActiveAnimation || startedAnimation)
595 didAnimate = true;
596
597 // If the current controller still has an active animation, we must continue animating layers.
598 if (currentController->hasActiveAnimation())
599 subtreeNeedsAnimateLayers = true;
600
601 for (size_t i = 0; i < current->children().size(); ++i) {
602 bool childNeedsAnimateLayers = false;
[email protected]0920e24f2012-09-20 03:34:03603 animateLayersRecursive(current->children()[i], monotonicTime, wallClockTime, events, didAnimate, childNeedsAnimateLayers);
[email protected]94f206c12012-08-25 00:09:14604 if (childNeedsAnimateLayers)
605 subtreeNeedsAnimateLayers = true;
606 }
607
608 needsAnimateLayers = subtreeNeedsAnimateLayers;
609}
610
[email protected]96baf3e2012-10-22 23:09:55611void LayerTreeHostImpl::setBackgroundTickingEnabled(bool enabled)
[email protected]94f206c12012-08-25 00:09:14612{
613 // Lazily create the timeSource adapter so that we can vary the interval for testing.
614 if (!m_timeSourceClientAdapter)
[email protected]61de5812012-11-08 07:03:44615 m_timeSourceClientAdapter = LayerTreeHostImplTimeSourceAdapter::create(this, DelayBasedTimeSource::create(lowFrequencyAnimationInterval(), m_proxy->currentThread()));
[email protected]94f206c12012-08-25 00:09:14616
617 m_timeSourceClientAdapter->setActive(enabled);
618}
619
[email protected]aad0a0072012-11-01 18:15:58620gfx::Size LayerTreeHostImpl::contentSize() const
[email protected]94f206c12012-08-25 00:09:14621{
622 // TODO(aelias): Hardcoding the first child here is weird. Think of
623 // a cleaner way to get the contentBounds on the Impl side.
624 if (!m_rootScrollLayerImpl || m_rootScrollLayerImpl->children().isEmpty())
[email protected]aad0a0072012-11-01 18:15:58625 return gfx::Size();
[email protected]94f206c12012-08-25 00:09:14626 return m_rootScrollLayerImpl->children()[0]->contentBounds();
627}
628
[email protected]96baf3e2012-10-22 23:09:55629static inline RenderPass* findRenderPassById(RenderPass::Id renderPassId, const LayerTreeHostImpl::FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14630{
[email protected]96baf3e2012-10-22 23:09:55631 RenderPassIdHashMap::const_iterator it = frame.renderPassesById.find(renderPassId);
[email protected]1d993172012-10-18 18:15:04632 DCHECK(it != frame.renderPassesById.end());
[email protected]87cea5372012-09-26 18:59:56633 return it->second;
[email protected]94f206c12012-08-25 00:09:14634}
635
[email protected]96baf3e2012-10-22 23:09:55636static void removeRenderPassesRecursive(RenderPass::Id removeRenderPassId, LayerTreeHostImpl::FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14637{
[email protected]96baf3e2012-10-22 23:09:55638 RenderPass* removeRenderPass = findRenderPassById(removeRenderPassId, frame);
639 RenderPassList& renderPasses = frame.renderPasses;
640 RenderPassList::iterator toRemove = std::find(renderPasses.begin(), renderPasses.end(), removeRenderPass);
[email protected]94f206c12012-08-25 00:09:14641
642 // The pass was already removed by another quad - probably the original, and we are the replica.
[email protected]f8ad8342012-09-27 20:07:02643 if (toRemove == renderPasses.end())
[email protected]94f206c12012-08-25 00:09:14644 return;
645
[email protected]96baf3e2012-10-22 23:09:55646 const RenderPass* removedPass = *toRemove;
[email protected]f8ad8342012-09-27 20:07:02647 frame.renderPasses.erase(toRemove);
[email protected]94f206c12012-08-25 00:09:14648
649 // Now follow up for all RenderPass quads and remove their RenderPasses recursively.
[email protected]f57bbc02012-11-21 07:02:15650 const QuadList& quadList = removedPass->quad_list;
[email protected]96baf3e2012-10-22 23:09:55651 QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14652 for (; quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
[email protected]96baf3e2012-10-22 23:09:55653 DrawQuad* currentQuad = (*quadListIterator);
[email protected]1bc93f62012-11-17 19:29:50654 if (currentQuad->material != DrawQuad::RENDER_PASS)
[email protected]94f206c12012-08-25 00:09:14655 continue;
656
[email protected]c22418b2012-11-20 23:06:26657 RenderPass::Id nextRemoveRenderPassId = RenderPassDrawQuad::MaterialCast(currentQuad)->render_pass_id;
[email protected]94f206c12012-08-25 00:09:14658 removeRenderPassesRecursive(nextRemoveRenderPassId, frame);
659 }
660}
661
[email protected]96baf3e2012-10-22 23:09:55662bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::shouldRemoveRenderPass(const RenderPassDrawQuad& quad, const FrameData&) const
[email protected]94f206c12012-08-25 00:09:14663{
[email protected]c22418b2012-11-20 23:06:26664 return quad.contents_changed_since_last_frame.IsEmpty() && m_renderer.haveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]94f206c12012-08-25 00:09:14665}
666
[email protected]96baf3e2012-10-22 23:09:55667bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::shouldRemoveRenderPass(const RenderPassDrawQuad& quad, const FrameData& frame) const
[email protected]94f206c12012-08-25 00:09:14668{
[email protected]c22418b2012-11-20 23:06:26669 const RenderPass* renderPass = findRenderPassById(quad.render_pass_id, frame);
[email protected]96baf3e2012-10-22 23:09:55670 const RenderPassList& renderPasses = frame.renderPasses;
671 RenderPassList::const_iterator foundPass = std::find(renderPasses.begin(), renderPasses.end(), renderPass);
[email protected]94f206c12012-08-25 00:09:14672
[email protected]f8ad8342012-09-27 20:07:02673 bool renderPassAlreadyRemoved = foundPass == renderPasses.end();
[email protected]94f206c12012-08-25 00:09:14674 if (renderPassAlreadyRemoved)
675 return false;
676
677 // If any quad or RenderPass draws into this RenderPass, then keep it.
[email protected]f57bbc02012-11-21 07:02:15678 const QuadList& quadList = (*foundPass)->quad_list;
[email protected]96baf3e2012-10-22 23:09:55679 for (QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin(); quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
680 DrawQuad* currentQuad = *quadListIterator;
[email protected]94f206c12012-08-25 00:09:14681
[email protected]1bc93f62012-11-17 19:29:50682 if (currentQuad->material != DrawQuad::RENDER_PASS)
[email protected]94f206c12012-08-25 00:09:14683 return false;
684
[email protected]c22418b2012-11-20 23:06:26685 const RenderPass* contributingPass = findRenderPassById(RenderPassDrawQuad::MaterialCast(currentQuad)->render_pass_id, frame);
[email protected]96baf3e2012-10-22 23:09:55686 RenderPassList::const_iterator foundContributingPass = std::find(renderPasses.begin(), renderPasses.end(), contributingPass);
[email protected]f8ad8342012-09-27 20:07:02687 if (foundContributingPass != renderPasses.end())
[email protected]94f206c12012-08-25 00:09:14688 return false;
689 }
690 return true;
691}
692
693// Defined for linking tests.
[email protected]52347c842012-11-02 21:06:20694template CC_EXPORT void LayerTreeHostImpl::removeRenderPasses<LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(CullRenderPassesWithCachedTextures, FrameData&);
695template CC_EXPORT void LayerTreeHostImpl::removeRenderPasses<LayerTreeHostImpl::CullRenderPassesWithNoQuads>(CullRenderPassesWithNoQuads, FrameData&);
[email protected]94f206c12012-08-25 00:09:14696
697// static
698template<typename RenderPassCuller>
[email protected]96baf3e2012-10-22 23:09:55699void LayerTreeHostImpl::removeRenderPasses(RenderPassCuller culler, FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14700{
701 for (size_t it = culler.renderPassListBegin(frame.renderPasses); it != culler.renderPassListEnd(frame.renderPasses); it = culler.renderPassListNext(it)) {
[email protected]96baf3e2012-10-22 23:09:55702 const RenderPass* currentPass = frame.renderPasses[it];
[email protected]f57bbc02012-11-21 07:02:15703 const QuadList& quadList = currentPass->quad_list;
[email protected]96baf3e2012-10-22 23:09:55704 QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14705
706 for (; quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
[email protected]96baf3e2012-10-22 23:09:55707 DrawQuad* currentQuad = *quadListIterator;
[email protected]94f206c12012-08-25 00:09:14708
[email protected]1bc93f62012-11-17 19:29:50709 if (currentQuad->material != DrawQuad::RENDER_PASS)
[email protected]94f206c12012-08-25 00:09:14710 continue;
711
[email protected]96baf3e2012-10-22 23:09:55712 RenderPassDrawQuad* renderPassQuad = static_cast<RenderPassDrawQuad*>(currentQuad);
[email protected]94f206c12012-08-25 00:09:14713 if (!culler.shouldRemoveRenderPass(*renderPassQuad, frame))
714 continue;
715
716 // We are changing the vector in the middle of iteration. Because we
717 // delete render passes that draw into the current pass, we are
718 // guaranteed that any data from the iterator to the end will not
719 // change. So, capture the iterator position from the end of the
720 // list, and restore it after the change.
721 int positionFromEnd = frame.renderPasses.size() - it;
[email protected]c22418b2012-11-20 23:06:26722 removeRenderPassesRecursive(renderPassQuad->render_pass_id, frame);
[email protected]94f206c12012-08-25 00:09:14723 it = frame.renderPasses.size() - positionFromEnd;
[email protected]1d993172012-10-18 18:15:04724 DCHECK(it >= 0);
[email protected]94f206c12012-08-25 00:09:14725 }
726 }
727}
728
[email protected]96baf3e2012-10-22 23:09:55729bool LayerTreeHostImpl::prepareToDraw(FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14730{
[email protected]96baf3e2012-10-22 23:09:55731 TRACE_EVENT0("cc", "LayerTreeHostImpl::prepareToDraw");
[email protected]1d993172012-10-18 18:15:04732 DCHECK(canDraw());
[email protected]94f206c12012-08-25 00:09:14733
734 frame.renderSurfaceLayerList = &m_renderSurfaceLayerList;
735 frame.renderPasses.clear();
736 frame.renderPassesById.clear();
737 frame.renderSurfaceLayerList->clear();
738 frame.willDrawLayers.clear();
739
740 if (!calculateRenderPasses(frame))
741 return false;
742
743 // If we return true, then we expect drawLayers() to be called before this function is called again.
744 return true;
745}
746
[email protected]96baf3e2012-10-22 23:09:55747void LayerTreeHostImpl::enforceManagedMemoryPolicy(const ManagedMemoryPolicy& policy)
[email protected]94f206c12012-08-25 00:09:14748{
[email protected]a0a00842012-10-22 22:50:28749 bool evictedResources = m_client->reduceContentsTextureMemoryOnImplThread(
750 m_visible ? policy.bytesLimitWhenVisible : policy.bytesLimitWhenNotVisible,
751 m_visible ? policy.priorityCutoffWhenVisible : policy.priorityCutoffWhenNotVisible);
[email protected]b1969fa2012-10-17 20:16:29752 if (evictedResources) {
753 setContentsTexturesPurged();
754 m_client->setNeedsCommitOnImplThread();
755 m_client->onCanDrawStateChanged(canDraw());
756 }
[email protected]3d21e022012-10-25 20:03:08757 m_client->sendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14758}
759
[email protected]61de5812012-11-08 07:03:44760bool LayerTreeHostImpl::hasImplThread() const
761{
762 return m_proxy->hasImplThread();
763}
764
[email protected]96baf3e2012-10-22 23:09:55765void LayerTreeHostImpl::setManagedMemoryPolicy(const ManagedMemoryPolicy& policy)
[email protected]94f206c12012-08-25 00:09:14766{
[email protected]a0a00842012-10-22 22:50:28767 if (m_managedMemoryPolicy == policy)
[email protected]94f206c12012-08-25 00:09:14768 return;
[email protected]61de5812012-11-08 07:03:44769
[email protected]a0a00842012-10-22 22:50:28770 m_managedMemoryPolicy = policy;
[email protected]61de5812012-11-08 07:03:44771 if (!m_proxy->hasImplThread()) {
772 // FIXME: In single-thread mode, this can be called on the main thread
773 // by GLRenderer::onMemoryAllocationChanged.
774 DebugScopedSetImplThread implThread(m_proxy);
775 enforceManagedMemoryPolicy(m_managedMemoryPolicy);
776 } else {
777 DCHECK(m_proxy->isImplThread());
778 enforceManagedMemoryPolicy(m_managedMemoryPolicy);
779 }
[email protected]a0a00842012-10-22 22:50:28780 // We always need to commit after changing the memory policy because the new
781 // limit can result in more or less content having texture allocated for it.
[email protected]94f206c12012-08-25 00:09:14782 m_client->setNeedsCommitOnImplThread();
783}
784
[email protected]96baf3e2012-10-22 23:09:55785void LayerTreeHostImpl::onVSyncParametersChanged(double monotonicTimebase, double intervalInSeconds)
[email protected]94f206c12012-08-25 00:09:14786{
[email protected]30faac92012-10-29 00:06:29787 base::TimeTicks timebase = base::TimeTicks::FromInternalValue(monotonicTimebase * base::Time::kMicrosecondsPerSecond);
788 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(intervalInSeconds * base::Time::kMicrosecondsPerSecond);
789 m_client->onVSyncParametersChanged(timebase, interval);
[email protected]94f206c12012-08-25 00:09:14790}
791
[email protected]96baf3e2012-10-22 23:09:55792void LayerTreeHostImpl::drawLayers(const FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14793{
[email protected]96baf3e2012-10-22 23:09:55794 TRACE_EVENT0("cc", "LayerTreeHostImpl::drawLayers");
[email protected]1d993172012-10-18 18:15:04795 DCHECK(canDraw());
796 DCHECK(!frame.renderPasses.empty());
[email protected]94f206c12012-08-25 00:09:14797
798 // FIXME: use the frame begin time from the overall compositor scheduler.
799 // This value is currently inaccessible because it is up in Chromium's
800 // RenderWidget.
[email protected]6bea87c2012-10-13 00:15:21801 m_fpsCounter->markBeginningOfFrame(base::TimeTicks::Now());
[email protected]94f206c12012-08-25 00:09:14802
803 if (m_settings.showDebugRects())
[email protected]4d8804e2012-11-15 01:51:10804 m_debugRectHistory->saveDebugRectsForCurrentFrame(m_rootLayerImpl.get(), *frame.renderSurfaceLayerList, frame.occludingScreenSpaceRects, frame.nonOccludingScreenSpaceRects, settings());
[email protected]94f206c12012-08-25 00:09:14805
806 // Because the contents of the HUD depend on everything else in the frame, the contents
807 // of its texture are updated as the last thing before the frame is drawn.
808 if (m_hudLayerImpl)
809 m_hudLayerImpl->updateHudTexture(m_resourceProvider.get());
810
811 m_renderer->drawFrame(frame.renderPasses, frame.renderPassesById);
812
813 // Once a RenderPass has been drawn, its damage should be cleared in
814 // case the RenderPass will be reused next frame.
815 for (unsigned int i = 0; i < frame.renderPasses.size(); i++)
[email protected]f57bbc02012-11-21 07:02:15816 frame.renderPasses[i]->damage_rect = gfx::RectF();
[email protected]94f206c12012-08-25 00:09:14817
818 // The next frame should start by assuming nothing has changed, and changes are noted as they occur.
819 for (unsigned int i = 0; i < frame.renderSurfaceLayerList->size(); i++)
820 (*frame.renderSurfaceLayerList)[i]->renderSurface()->damageTracker()->didDrawDamagedArea();
821 m_rootLayerImpl->resetAllChangeTrackingForSubtree();
822}
823
[email protected]96baf3e2012-10-22 23:09:55824void LayerTreeHostImpl::didDrawAllLayers(const FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14825{
826 for (size_t i = 0; i < frame.willDrawLayers.size(); ++i)
827 frame.willDrawLayers[i]->didDraw(m_resourceProvider.get());
[email protected]b914e102012-10-02 08:11:52828
829 // Once all layers have been drawn, pending texture uploads should no
830 // longer block future uploads.
[email protected]e2249592012-10-19 06:59:09831 m_resourceProvider->markPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:14832}
833
[email protected]96baf3e2012-10-22 23:09:55834void LayerTreeHostImpl::finishAllRendering()
[email protected]94f206c12012-08-25 00:09:14835{
836 if (m_renderer)
837 m_renderer->finish();
838}
839
[email protected]96baf3e2012-10-22 23:09:55840bool LayerTreeHostImpl::isContextLost()
[email protected]94f206c12012-08-25 00:09:14841{
842 return m_renderer && m_renderer->isContextLost();
843}
844
[email protected]96baf3e2012-10-22 23:09:55845const RendererCapabilities& LayerTreeHostImpl::rendererCapabilities() const
[email protected]94f206c12012-08-25 00:09:14846{
847 return m_renderer->capabilities();
848}
849
[email protected]96baf3e2012-10-22 23:09:55850bool LayerTreeHostImpl::swapBuffers()
[email protected]94f206c12012-08-25 00:09:14851{
[email protected]1d993172012-10-18 18:15:04852 DCHECK(m_renderer);
[email protected]94f206c12012-08-25 00:09:14853
854 m_fpsCounter->markEndOfFrame();
855 return m_renderer->swapBuffers();
856}
857
[email protected]aad0a0072012-11-01 18:15:58858const gfx::Size& LayerTreeHostImpl::deviceViewportSize() const
[email protected]493067512012-09-19 23:34:10859{
860 return m_deviceViewportSize;
861}
862
[email protected]96baf3e2012-10-22 23:09:55863const LayerTreeSettings& LayerTreeHostImpl::settings() const
[email protected]493067512012-09-19 23:34:10864{
865 return m_settings;
866}
867
[email protected]96baf3e2012-10-22 23:09:55868void LayerTreeHostImpl::didLoseContext()
[email protected]94f206c12012-08-25 00:09:14869{
870 m_client->didLoseContextOnImplThread();
871}
872
[email protected]96baf3e2012-10-22 23:09:55873void LayerTreeHostImpl::onSwapBuffersComplete()
[email protected]94f206c12012-08-25 00:09:14874{
875 m_client->onSwapBuffersCompleteOnImplThread();
876}
877
[email protected]aad0a0072012-11-01 18:15:58878void LayerTreeHostImpl::readback(void* pixels, const gfx::Rect& rect)
[email protected]94f206c12012-08-25 00:09:14879{
[email protected]1d993172012-10-18 18:15:04880 DCHECK(m_renderer);
[email protected]94f206c12012-08-25 00:09:14881 m_renderer->getFramebufferPixels(pixels, rect);
882}
883
[email protected]96baf3e2012-10-22 23:09:55884static LayerImpl* findRootScrollLayer(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14885{
886 if (!layer)
887 return 0;
888
889 if (layer->scrollable())
890 return layer;
891
892 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55893 LayerImpl* found = findRootScrollLayer(layer->children()[i]);
[email protected]94f206c12012-08-25 00:09:14894 if (found)
895 return found;
896 }
897
898 return 0;
899}
900
901// Content layers can be either directly scrollable or contained in an outer
902// scrolling layer which applies the scroll transform. Given a content layer,
903// this function returns the associated scroll layer if any.
[email protected]96baf3e2012-10-22 23:09:55904static LayerImpl* findScrollLayerForContentLayer(LayerImpl* layerImpl)
[email protected]94f206c12012-08-25 00:09:14905{
906 if (!layerImpl)
907 return 0;
908
909 if (layerImpl->scrollable())
910 return layerImpl;
911
912 if (layerImpl->drawsContent() && layerImpl->parent() && layerImpl->parent()->scrollable())
913 return layerImpl->parent();
914
915 return 0;
916}
917
[email protected]96baf3e2012-10-22 23:09:55918void LayerTreeHostImpl::setRootLayer(scoped_ptr<LayerImpl> layer)
[email protected]94f206c12012-08-25 00:09:14919{
[email protected]e0bd43a2012-10-12 16:54:21920 m_rootLayerImpl = layer.Pass();
[email protected]94f206c12012-08-25 00:09:14921 m_rootScrollLayerImpl = findRootScrollLayer(m_rootLayerImpl.get());
922 m_currentlyScrollingLayerImpl = 0;
923
924 if (m_rootLayerImpl && m_scrollingLayerIdFromPreviousTree != -1)
[email protected]96baf3e2012-10-22 23:09:55925 m_currentlyScrollingLayerImpl = LayerTreeHostCommon::findLayerInSubtree(m_rootLayerImpl.get(), m_scrollingLayerIdFromPreviousTree);
[email protected]94f206c12012-08-25 00:09:14926
927 m_scrollingLayerIdFromPreviousTree = -1;
[email protected]8db2213c2012-09-05 22:08:21928
929 m_client->onCanDrawStateChanged(canDraw());
[email protected]94f206c12012-08-25 00:09:14930}
931
[email protected]96baf3e2012-10-22 23:09:55932scoped_ptr<LayerImpl> LayerTreeHostImpl::detachLayerTree()
[email protected]94f206c12012-08-25 00:09:14933{
934 // Clear all data structures that have direct references to the layer tree.
935 m_scrollingLayerIdFromPreviousTree = m_currentlyScrollingLayerImpl ? m_currentlyScrollingLayerImpl->id() : -1;
936 m_currentlyScrollingLayerImpl = 0;
937 m_renderSurfaceLayerList.clear();
938
[email protected]e0bd43a2012-10-12 16:54:21939 return m_rootLayerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:14940}
941
[email protected]96baf3e2012-10-22 23:09:55942void LayerTreeHostImpl::setVisible(bool visible)
[email protected]94f206c12012-08-25 00:09:14943{
[email protected]61de5812012-11-08 07:03:44944 DCHECK(m_proxy->isImplThread());
[email protected]94f206c12012-08-25 00:09:14945
946 if (m_visible == visible)
947 return;
948 m_visible = visible;
949 didVisibilityChange(this, m_visible);
[email protected]a0a00842012-10-22 22:50:28950 enforceManagedMemoryPolicy(m_managedMemoryPolicy);
[email protected]94f206c12012-08-25 00:09:14951
952 if (!m_renderer)
953 return;
954
955 m_renderer->setVisible(visible);
956
957 setBackgroundTickingEnabled(!m_visible && m_needsAnimateLayers);
958}
959
[email protected]96baf3e2012-10-22 23:09:55960bool LayerTreeHostImpl::initializeRenderer(scoped_ptr<GraphicsContext> context)
[email protected]94f206c12012-08-25 00:09:14961{
[email protected]be3181652012-09-25 13:02:13962 // Since we will create a new resource provider, we cannot continue to use
963 // the old resources (i.e. renderSurfaces and texture IDs). Clear them
964 // before we destroy the old resource provider.
[email protected]94f206c12012-08-25 00:09:14965 if (m_rootLayerImpl) {
966 clearRenderSurfaces();
967 sendDidLoseContextRecursive(m_rootLayerImpl.get());
968 }
[email protected]be3181652012-09-25 13:02:13969 // Note: order is important here.
[email protected]0704caf2012-10-16 03:39:47970 m_renderer.reset();
[email protected]a7aa5562012-10-17 14:12:44971 m_resourceProvider.reset();
[email protected]e28efacd2012-10-06 17:07:49972 m_context.reset();
[email protected]94f206c12012-08-25 00:09:14973
[email protected]be3181652012-09-25 13:02:13974 if (!context->bindToClient(this))
975 return false;
976
[email protected]96baf3e2012-10-22 23:09:55977 scoped_ptr<ResourceProvider> resourceProvider = ResourceProvider::create(context.get());
[email protected]be3181652012-09-25 13:02:13978 if (!resourceProvider)
979 return false;
980
981 if (context->context3D())
[email protected]96baf3e2012-10-22 23:09:55982 m_renderer = GLRenderer::create(this, resourceProvider.get());
[email protected]be3181652012-09-25 13:02:13983 else if (context->softwareDevice())
[email protected]96baf3e2012-10-22 23:09:55984 m_renderer = SoftwareRenderer::create(this, resourceProvider.get(), context->softwareDevice());
[email protected]be3181652012-09-25 13:02:13985 if (!m_renderer)
986 return false;
987
[email protected]a7aa5562012-10-17 14:12:44988 m_resourceProvider = resourceProvider.Pass();
[email protected]e28efacd2012-10-06 17:07:49989 m_context = context.Pass();
[email protected]94f206c12012-08-25 00:09:14990
[email protected]be3181652012-09-25 13:02:13991 if (!m_visible)
992 m_renderer->setVisible(m_visible);
[email protected]94f206c12012-08-25 00:09:14993
[email protected]8db2213c2012-09-05 22:08:21994 m_client->onCanDrawStateChanged(canDraw());
995
[email protected]be3181652012-09-25 13:02:13996 return true;
[email protected]94f206c12012-08-25 00:09:14997}
998
[email protected]96baf3e2012-10-22 23:09:55999void LayerTreeHostImpl::setContentsTexturesPurged()
[email protected]e1fc8b32012-09-18 20:29:091000{
1001 m_contentsTexturesPurged = true;
1002 m_client->onCanDrawStateChanged(canDraw());
1003}
1004
[email protected]96baf3e2012-10-22 23:09:551005void LayerTreeHostImpl::resetContentsTexturesPurged()
[email protected]8db2213c2012-09-05 22:08:211006{
1007 m_contentsTexturesPurged = false;
1008 m_client->onCanDrawStateChanged(canDraw());
1009}
1010
[email protected]aad0a0072012-11-01 18:15:581011void LayerTreeHostImpl::setViewportSize(const gfx::Size& layoutViewportSize, const gfx::Size& deviceViewportSize)
[email protected]94f206c12012-08-25 00:09:141012{
1013 if (layoutViewportSize == m_layoutViewportSize && deviceViewportSize == m_deviceViewportSize)
1014 return;
1015
1016 m_layoutViewportSize = layoutViewportSize;
1017 m_deviceViewportSize = deviceViewportSize;
1018
[email protected]c9c1ebe2012-11-05 20:46:131019 m_pinchZoomViewport.setLayoutViewportSize(layoutViewportSize);
[email protected]1c0c9bc2012-10-08 22:41:481020
[email protected]c9c1ebe2012-11-05 20:46:131021 updateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141022
1023 if (m_renderer)
1024 m_renderer->viewportChanged();
[email protected]8db2213c2012-09-05 22:08:211025
1026 m_client->onCanDrawStateChanged(canDraw());
[email protected]94f206c12012-08-25 00:09:141027}
1028
[email protected]96baf3e2012-10-22 23:09:551029static void adjustScrollsForPageScaleChange(LayerImpl* layerImpl, float pageScaleChange)
[email protected]94f206c12012-08-25 00:09:141030{
1031 if (!layerImpl)
1032 return;
1033
1034 if (layerImpl->scrollable()) {
1035 // We need to convert impl-side scroll deltas to pageScale space.
[email protected]c9c1ebe2012-11-05 20:46:131036 gfx::Vector2dF scrollDelta = layerImpl->scrollDelta();
1037 scrollDelta.Scale(pageScaleChange);
[email protected]94f206c12012-08-25 00:09:141038 layerImpl->setScrollDelta(scrollDelta);
1039 }
1040
1041 for (size_t i = 0; i < layerImpl->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031042 adjustScrollsForPageScaleChange(layerImpl->children()[i], pageScaleChange);
[email protected]94f206c12012-08-25 00:09:141043}
1044
[email protected]96baf3e2012-10-22 23:09:551045void LayerTreeHostImpl::setDeviceScaleFactor(float deviceScaleFactor)
[email protected]94f206c12012-08-25 00:09:141046{
1047 if (deviceScaleFactor == m_deviceScaleFactor)
1048 return;
1049 m_deviceScaleFactor = deviceScaleFactor;
[email protected]e39bf212012-11-22 21:04:031050 m_pinchZoomViewport.setDeviceScaleFactor(m_deviceScaleFactor);
[email protected]c0dd24c2012-08-30 23:25:271051
[email protected]c9c1ebe2012-11-05 20:46:131052 updateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141053}
1054
[email protected]96baf3e2012-10-22 23:09:551055float LayerTreeHostImpl::pageScaleFactor() const
[email protected]94f206c12012-08-25 00:09:141056{
[email protected]1c0c9bc2012-10-08 22:41:481057 return m_pinchZoomViewport.pageScaleFactor();
1058}
[email protected]94f206c12012-08-25 00:09:141059
[email protected]96baf3e2012-10-22 23:09:551060void LayerTreeHostImpl::setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor)
[email protected]1c0c9bc2012-10-08 22:41:481061{
1062 if (!pageScaleFactor)
1063 return;
[email protected]94f206c12012-08-25 00:09:141064
[email protected]1c0c9bc2012-10-08 22:41:481065 float pageScaleChange = pageScaleFactor / m_pinchZoomViewport.pageScaleFactor();
1066 m_pinchZoomViewport.setPageScaleFactorAndLimits(pageScaleFactor, minPageScaleFactor, maxPageScaleFactor);
[email protected]94f206c12012-08-25 00:09:141067
[email protected]9bdcfd642012-11-14 21:24:261068 if (!m_settings.pageScalePinchZoomEnabled) {
[email protected]1c0c9bc2012-10-08 22:41:481069 if (pageScaleChange != 1)
1070 adjustScrollsForPageScaleChange(m_rootScrollLayerImpl, pageScaleChange);
1071 }
[email protected]94f206c12012-08-25 00:09:141072
1073 // Clamp delta to limits and refresh display matrix.
[email protected]1c0c9bc2012-10-08 22:41:481074 setPageScaleDelta(m_pinchZoomViewport.pageScaleDelta() / m_pinchZoomViewport.sentPageScaleDelta());
1075 m_pinchZoomViewport.setSentPageScaleDelta(1);
[email protected]94f206c12012-08-25 00:09:141076}
1077
[email protected]96baf3e2012-10-22 23:09:551078void LayerTreeHostImpl::setPageScaleDelta(float delta)
[email protected]94f206c12012-08-25 00:09:141079{
[email protected]1c0c9bc2012-10-08 22:41:481080 m_pinchZoomViewport.setPageScaleDelta(delta);
[email protected]94f206c12012-08-25 00:09:141081
[email protected]c9c1ebe2012-11-05 20:46:131082 updateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141083}
1084
[email protected]c9c1ebe2012-11-05 20:46:131085void LayerTreeHostImpl::updateMaxScrollOffset()
[email protected]94f206c12012-08-25 00:09:141086{
1087 if (!m_rootScrollLayerImpl || !m_rootScrollLayerImpl->children().size())
1088 return;
1089
[email protected]aad0a0072012-11-01 18:15:581090 gfx::SizeF viewBounds = m_deviceViewportSize;
[email protected]96baf3e2012-10-22 23:09:551091 if (LayerImpl* clipLayer = m_rootScrollLayerImpl->parent()) {
[email protected]94f206c12012-08-25 00:09:141092 // Compensate for non-overlay scrollbars.
[email protected]01a15a72012-11-10 09:34:281093 if (clipLayer->masksToBounds())
1094 viewBounds = gfx::ScaleSize(clipLayer->bounds(), m_deviceScaleFactor);
[email protected]94f206c12012-08-25 00:09:141095 }
[email protected]94f206c12012-08-25 00:09:141096
[email protected]aad0a0072012-11-01 18:15:581097 gfx::Size contentBounds = contentSize();
[email protected]9bdcfd642012-11-14 21:24:261098 if (m_settings.pageScalePinchZoomEnabled) {
[email protected]1c0c9bc2012-10-08 22:41:481099 // Pinch with pageScale scrolls entirely in layout space. contentSize
1100 // returns the bounds including the page scale factor, so calculate the
1101 // pre page-scale layout size here.
1102 float pageScaleFactor = m_pinchZoomViewport.pageScaleFactor();
[email protected]aad0a0072012-11-01 18:15:581103 contentBounds.set_width(contentBounds.width() / pageScaleFactor);
1104 contentBounds.set_height(contentBounds.height() / pageScaleFactor);
[email protected]1c0c9bc2012-10-08 22:41:481105 } else {
[email protected]01a15a72012-11-10 09:34:281106 viewBounds.Scale(1 / m_pinchZoomViewport.pageScaleDelta());
[email protected]1c0c9bc2012-10-08 22:41:481107 }
1108
[email protected]0eef4b882012-11-20 20:28:071109 gfx::Vector2dF maxScroll = gfx::Rect(contentBounds).bottom_right() - gfx::RectF(viewBounds).bottom_right();
[email protected]c9c1ebe2012-11-05 20:46:131110 maxScroll.Scale(1 / m_deviceScaleFactor);
[email protected]1c0c9bc2012-10-08 22:41:481111
[email protected]94f206c12012-08-25 00:09:141112 // The viewport may be larger than the contents in some cases, such as
1113 // having a vertical scrollbar but no horizontal overflow.
[email protected]fe07b642012-11-10 00:07:591114 maxScroll.ClampToMin(gfx::Vector2dF());
[email protected]94f206c12012-08-25 00:09:141115
[email protected]c9c1ebe2012-11-05 20:46:131116 m_rootScrollLayerImpl->setMaxScrollOffset(gfx::ToFlooredVector2d(maxScroll));
[email protected]94f206c12012-08-25 00:09:141117}
1118
[email protected]96baf3e2012-10-22 23:09:551119void LayerTreeHostImpl::setNeedsRedraw()
[email protected]94f206c12012-08-25 00:09:141120{
1121 m_client->setNeedsRedrawOnImplThread();
1122}
1123
[email protected]96baf3e2012-10-22 23:09:551124bool LayerTreeHostImpl::ensureRenderSurfaceLayerList()
[email protected]94f206c12012-08-25 00:09:141125{
1126 if (!m_rootLayerImpl)
1127 return false;
1128 if (!m_renderer)
1129 return false;
1130
1131 // We need both a non-empty render surface layer list and a root render
1132 // surface to be able to iterate over the visible layers.
1133 if (m_renderSurfaceLayerList.size() && m_rootLayerImpl->renderSurface())
1134 return true;
1135
1136 // If we are called after setRootLayer() but before prepareToDraw(), we need
1137 // to recalculate the visible layers. This prevents being unable to scroll
1138 // during part of a commit.
1139 m_renderSurfaceLayerList.clear();
1140 calculateRenderSurfaceLayerList(m_renderSurfaceLayerList);
1141
1142 return m_renderSurfaceLayerList.size();
1143}
1144
[email protected]c9c1ebe2012-11-05 20:46:131145InputHandlerClient::ScrollStatus LayerTreeHostImpl::scrollBegin(gfx::Point viewportPoint, InputHandlerClient::ScrollInputType type)
[email protected]94f206c12012-08-25 00:09:141146{
[email protected]96baf3e2012-10-22 23:09:551147 TRACE_EVENT0("cc", "LayerTreeHostImpl::scrollBegin");
[email protected]94f206c12012-08-25 00:09:141148
[email protected]1d993172012-10-18 18:15:041149 DCHECK(!m_currentlyScrollingLayerImpl);
[email protected]94f206c12012-08-25 00:09:141150 clearCurrentlyScrollingLayer();
1151
1152 if (!ensureRenderSurfaceLayerList())
1153 return ScrollIgnored;
1154
[email protected]faf56352012-11-09 21:44:131155 gfx::PointF deviceViewportPoint = gfx::ScalePoint(viewportPoint, m_deviceScaleFactor);
[email protected]94f206c12012-08-25 00:09:141156
1157 // First find out which layer was hit from the saved list of visible layers
1158 // in the most recent frame.
[email protected]96baf3e2012-10-22 23:09:551159 LayerImpl* layerImpl = LayerTreeHostCommon::findLayerThatIsHitByPoint(deviceViewportPoint, m_renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:141160
1161 // Walk up the hierarchy and look for a scrollable layer.
[email protected]96baf3e2012-10-22 23:09:551162 LayerImpl* potentiallyScrollingLayerImpl = 0;
[email protected]94f206c12012-08-25 00:09:141163 for (; layerImpl; layerImpl = layerImpl->parent()) {
1164 // The content layer can also block attempts to scroll outside the main thread.
[email protected]5c6fe1f82012-10-03 18:00:271165 if (layerImpl->tryScroll(deviceViewportPoint, type) == ScrollOnMainThread) {
1166 m_numMainThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141167 return ScrollOnMainThread;
[email protected]5c6fe1f82012-10-03 18:00:271168 }
[email protected]94f206c12012-08-25 00:09:141169
[email protected]96baf3e2012-10-22 23:09:551170 LayerImpl* scrollLayerImpl = findScrollLayerForContentLayer(layerImpl);
[email protected]94f206c12012-08-25 00:09:141171 if (!scrollLayerImpl)
1172 continue;
1173
[email protected]31bfe272012-10-19 18:49:521174 ScrollStatus status = scrollLayerImpl->tryScroll(deviceViewportPoint, type);
[email protected]94f206c12012-08-25 00:09:141175
1176 // If any layer wants to divert the scroll event to the main thread, abort.
[email protected]5c6fe1f82012-10-03 18:00:271177 if (status == ScrollOnMainThread) {
1178 m_numMainThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141179 return ScrollOnMainThread;
[email protected]5c6fe1f82012-10-03 18:00:271180 }
[email protected]94f206c12012-08-25 00:09:141181
1182 if (status == ScrollStarted && !potentiallyScrollingLayerImpl)
1183 potentiallyScrollingLayerImpl = scrollLayerImpl;
1184 }
1185
1186 if (potentiallyScrollingLayerImpl) {
1187 m_currentlyScrollingLayerImpl = potentiallyScrollingLayerImpl;
[email protected]31bfe272012-10-19 18:49:521188 // Gesture events need to be transformed from viewport coordinates to local layer coordinates
[email protected]94f206c12012-08-25 00:09:141189 // so that the scrolling contents exactly follow the user's finger. In contrast, wheel
1190 // events are already in local layer coordinates so we can just apply them directly.
[email protected]31bfe272012-10-19 18:49:521191 m_scrollDeltaIsInViewportSpace = (type == Gesture);
[email protected]5c6fe1f82012-10-03 18:00:271192 m_numImplThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141193 return ScrollStarted;
1194 }
1195 return ScrollIgnored;
1196}
1197
[email protected]c9c1ebe2012-11-05 20:46:131198static gfx::Vector2dF scrollLayerWithViewportSpaceDelta(PinchZoomViewport* viewport, LayerImpl& layerImpl, float scaleFromViewportToScreenSpace, gfx::PointF viewportPoint, gfx::Vector2dF viewportDelta)
[email protected]94f206c12012-08-25 00:09:141199{
1200 // Layers with non-invertible screen space transforms should not have passed the scroll hit
1201 // test in the first place.
[email protected]c8686a02012-11-27 08:29:001202 DCHECK(layerImpl.screenSpaceTransform().IsInvertible());
1203 gfx::Transform inverseScreenSpaceTransform = MathUtil::inverse(layerImpl.screenSpaceTransform());
[email protected]94f206c12012-08-25 00:09:141204
[email protected]faf56352012-11-09 21:44:131205 gfx::PointF screenSpacePoint = gfx::ScalePoint(viewportPoint, scaleFromViewportToScreenSpace);
[email protected]31bfe272012-10-19 18:49:521206
[email protected]c9c1ebe2012-11-05 20:46:131207 gfx::Vector2dF screenSpaceDelta = viewportDelta;
1208 screenSpaceDelta.Scale(scaleFromViewportToScreenSpace);
[email protected]31bfe272012-10-19 18:49:521209
[email protected]94f206c12012-08-25 00:09:141210 // First project the scroll start and end points to local layer space to find the scroll delta
1211 // in layer coordinates.
1212 bool startClipped, endClipped;
[email protected]d455d552012-11-02 00:19:061213 gfx::PointF screenSpaceEndPoint = screenSpacePoint + screenSpaceDelta;
1214 gfx::PointF localStartPoint = MathUtil::projectPoint(inverseScreenSpaceTransform, screenSpacePoint, startClipped);
1215 gfx::PointF localEndPoint = MathUtil::projectPoint(inverseScreenSpaceTransform, screenSpaceEndPoint, endClipped);
[email protected]94f206c12012-08-25 00:09:141216
1217 // In general scroll point coordinates should not get clipped.
[email protected]1d993172012-10-18 18:15:041218 DCHECK(!startClipped);
1219 DCHECK(!endClipped);
[email protected]94f206c12012-08-25 00:09:141220 if (startClipped || endClipped)
[email protected]c9c1ebe2012-11-05 20:46:131221 return gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:141222
[email protected]31bfe272012-10-19 18:49:521223 // localStartPoint and localEndPoint are in content space but we want to move them to layer space for scrolling.
[email protected]aad0a0072012-11-01 18:15:581224 float widthScale = 1 / layerImpl.contentsScaleX();
1225 float heightScale = 1 / layerImpl.contentsScaleY();
[email protected]faf56352012-11-09 21:44:131226 localStartPoint.Scale(widthScale, heightScale);
1227 localEndPoint.Scale(widthScale, heightScale);
[email protected]31bfe272012-10-19 18:49:521228
[email protected]94f206c12012-08-25 00:09:141229 // Apply the scroll delta.
[email protected]c9c1ebe2012-11-05 20:46:131230 gfx::Vector2dF previousDelta = layerImpl.scrollDelta();
1231 gfx::Vector2dF unscrolled = layerImpl.scrollBy(localEndPoint - localStartPoint);
[email protected]1c0c9bc2012-10-08 22:41:481232
[email protected]aeaa50a2012-11-21 20:12:371233 gfx::Vector2dF viewportAppliedPan;
[email protected]1c0c9bc2012-10-08 22:41:481234 if (viewport)
[email protected]aeaa50a2012-11-21 20:12:371235 viewportAppliedPan = unscrolled - viewport->applyScroll(unscrolled);
[email protected]94f206c12012-08-25 00:09:141236
[email protected]31bfe272012-10-19 18:49:521237 // Get the end point in the layer's content space so we can apply its screenSpaceTransform.
[email protected]aeaa50a2012-11-21 20:12:371238 gfx::PointF actualLocalEndPoint = localStartPoint + layerImpl.scrollDelta() + viewportAppliedPan - previousDelta;
[email protected]faf56352012-11-09 21:44:131239 gfx::PointF actualLocalContentEndPoint = gfx::ScalePoint(actualLocalEndPoint, 1 / widthScale, 1 / heightScale);
[email protected]31bfe272012-10-19 18:49:521240
1241 // Calculate the applied scroll delta in viewport space coordinates.
[email protected]d455d552012-11-02 00:19:061242 gfx::PointF actualScreenSpaceEndPoint = MathUtil::mapPoint(layerImpl.screenSpaceTransform(), actualLocalContentEndPoint, endClipped);
[email protected]1d993172012-10-18 18:15:041243 DCHECK(!endClipped);
[email protected]94f206c12012-08-25 00:09:141244 if (endClipped)
[email protected]c9c1ebe2012-11-05 20:46:131245 return gfx::Vector2dF();
[email protected]faf56352012-11-09 21:44:131246 gfx::PointF actualViewportEndPoint = gfx::ScalePoint(actualScreenSpaceEndPoint, 1 / scaleFromViewportToScreenSpace);
[email protected]c9c1ebe2012-11-05 20:46:131247 return actualViewportEndPoint - viewportPoint;
[email protected]94f206c12012-08-25 00:09:141248}
1249
[email protected]c9c1ebe2012-11-05 20:46:131250static gfx::Vector2dF scrollLayerWithLocalDelta(LayerImpl& layerImpl, gfx::Vector2dF localDelta)
[email protected]94f206c12012-08-25 00:09:141251{
[email protected]c9c1ebe2012-11-05 20:46:131252 gfx::Vector2dF previousDelta(layerImpl.scrollDelta());
[email protected]94f206c12012-08-25 00:09:141253 layerImpl.scrollBy(localDelta);
1254 return layerImpl.scrollDelta() - previousDelta;
1255}
1256
[email protected]a9710962012-11-14 20:11:021257bool LayerTreeHostImpl::scrollBy(const gfx::Point& viewportPoint,
1258 const gfx::Vector2d& scrollDelta)
[email protected]94f206c12012-08-25 00:09:141259{
[email protected]96baf3e2012-10-22 23:09:551260 TRACE_EVENT0("cc", "LayerTreeHostImpl::scrollBy");
[email protected]94f206c12012-08-25 00:09:141261 if (!m_currentlyScrollingLayerImpl)
[email protected]a9710962012-11-14 20:11:021262 return false;
[email protected]94f206c12012-08-25 00:09:141263
[email protected]c9c1ebe2012-11-05 20:46:131264 gfx::Vector2dF pendingDelta = scrollDelta;
[email protected]94f206c12012-08-25 00:09:141265
[email protected]96baf3e2012-10-22 23:09:551266 for (LayerImpl* layerImpl = m_currentlyScrollingLayerImpl; layerImpl; layerImpl = layerImpl->parent()) {
[email protected]94f206c12012-08-25 00:09:141267 if (!layerImpl->scrollable())
1268 continue;
1269
[email protected]96baf3e2012-10-22 23:09:551270 PinchZoomViewport* viewport = layerImpl == m_rootScrollLayerImpl ? &m_pinchZoomViewport : 0;
[email protected]c9c1ebe2012-11-05 20:46:131271 gfx::Vector2dF appliedDelta;
[email protected]31bfe272012-10-19 18:49:521272 if (m_scrollDeltaIsInViewportSpace) {
1273 float scaleFromViewportToScreenSpace = m_deviceScaleFactor;
1274 appliedDelta = scrollLayerWithViewportSpaceDelta(viewport, *layerImpl, scaleFromViewportToScreenSpace, viewportPoint, pendingDelta);
1275 } else
[email protected]94f206c12012-08-25 00:09:141276 appliedDelta = scrollLayerWithLocalDelta(*layerImpl, pendingDelta);
1277
1278 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]23bbb412012-08-30 20:03:381279 float moveThresholdSquared = 0.1f * 0.1f;
[email protected]c9c1ebe2012-11-05 20:46:131280 if (appliedDelta.LengthSquared() < moveThresholdSquared)
[email protected]94f206c12012-08-25 00:09:141281 continue;
1282
1283 // If the applied delta is within 45 degrees of the input delta, bail out to make it easier
1284 // to scroll just one layer in one direction without affecting any of its parents.
1285 float angleThreshold = 45;
[email protected]96baf3e2012-10-22 23:09:551286 if (MathUtil::smallestAngleBetweenVectors(appliedDelta, pendingDelta) < angleThreshold) {
[email protected]c9c1ebe2012-11-05 20:46:131287 pendingDelta = gfx::Vector2d();
[email protected]94f206c12012-08-25 00:09:141288 break;
1289 }
1290
1291 // Allow further movement only on an axis perpendicular to the direction in which the layer
1292 // moved.
[email protected]c9c1ebe2012-11-05 20:46:131293 gfx::Vector2dF perpendicularAxis(-appliedDelta.y(), appliedDelta.x());
[email protected]96baf3e2012-10-22 23:09:551294 pendingDelta = MathUtil::projectVector(pendingDelta, perpendicularAxis);
[email protected]94f206c12012-08-25 00:09:141295
[email protected]c9c1ebe2012-11-05 20:46:131296 if (gfx::ToFlooredVector2d(pendingDelta).IsZero())
[email protected]94f206c12012-08-25 00:09:141297 break;
1298 }
1299
[email protected]c9c1ebe2012-11-05 20:46:131300 if (!scrollDelta.IsZero() && gfx::ToFlooredVector2d(pendingDelta).IsZero()) {
[email protected]94f206c12012-08-25 00:09:141301 m_client->setNeedsCommitOnImplThread();
1302 m_client->setNeedsRedrawOnImplThread();
[email protected]a9710962012-11-14 20:11:021303 return true;
[email protected]94f206c12012-08-25 00:09:141304 }
[email protected]a9710962012-11-14 20:11:021305 return false;
[email protected]94f206c12012-08-25 00:09:141306}
1307
[email protected]96baf3e2012-10-22 23:09:551308void LayerTreeHostImpl::clearCurrentlyScrollingLayer()
[email protected]94f206c12012-08-25 00:09:141309{
1310 m_currentlyScrollingLayerImpl = 0;
1311 m_scrollingLayerIdFromPreviousTree = -1;
1312}
1313
[email protected]96baf3e2012-10-22 23:09:551314void LayerTreeHostImpl::scrollEnd()
[email protected]94f206c12012-08-25 00:09:141315{
1316 clearCurrentlyScrollingLayer();
1317}
1318
[email protected]96baf3e2012-10-22 23:09:551319void LayerTreeHostImpl::pinchGestureBegin()
[email protected]94f206c12012-08-25 00:09:141320{
1321 m_pinchGestureActive = true;
[email protected]c9c1ebe2012-11-05 20:46:131322 m_previousPinchAnchor = gfx::Point();
[email protected]94f206c12012-08-25 00:09:141323
1324 if (m_rootScrollLayerImpl && m_rootScrollLayerImpl->scrollbarAnimationController())
1325 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureBegin();
1326}
1327
[email protected]c9c1ebe2012-11-05 20:46:131328void LayerTreeHostImpl::pinchGestureUpdate(float magnifyDelta, gfx::Point anchor)
[email protected]94f206c12012-08-25 00:09:141329{
[email protected]96baf3e2012-10-22 23:09:551330 TRACE_EVENT0("cc", "LayerTreeHostImpl::pinchGestureUpdate");
[email protected]94f206c12012-08-25 00:09:141331
1332 if (!m_rootScrollLayerImpl)
1333 return;
1334
[email protected]94f206c12012-08-25 00:09:141335 // Keep the center-of-pinch anchor specified by (x, y) in a stable
1336 // position over the course of the magnify.
[email protected]1c0c9bc2012-10-08 22:41:481337 float pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
[email protected]c77745d2012-11-20 04:11:571338 gfx::PointF previousScaleAnchor = gfx::ScalePoint(anchor, 1 / pageScaleDelta);
[email protected]1c0c9bc2012-10-08 22:41:481339 setPageScaleDelta(pageScaleDelta * magnifyDelta);
1340 pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
[email protected]faf56352012-11-09 21:44:131341 gfx::PointF newScaleAnchor = gfx::ScalePoint(anchor, 1 / pageScaleDelta);
[email protected]c9c1ebe2012-11-05 20:46:131342 gfx::Vector2dF move = previousScaleAnchor - newScaleAnchor;
[email protected]94f206c12012-08-25 00:09:141343
1344 m_previousPinchAnchor = anchor;
1345
[email protected]9bdcfd642012-11-14 21:24:261346 if (m_settings.pageScalePinchZoomEnabled) {
[email protected]1c0c9bc2012-10-08 22:41:481347 // Compute the application of the delta with respect to the current page zoom of the page.
[email protected]e39bf212012-11-22 21:04:031348 move.Scale(1 / m_pinchZoomViewport.pageScaleFactor());
[email protected]1c0c9bc2012-10-08 22:41:481349 }
1350
[email protected]9bdcfd642012-11-14 21:24:261351 gfx::Vector2dF scrollOverflow = m_settings.pageScalePinchZoomEnabled ? m_pinchZoomViewport.applyScroll(move) : move;
[email protected]c9c1ebe2012-11-05 20:46:131352 m_rootScrollLayerImpl->scrollBy(scrollOverflow);
[email protected]94f206c12012-08-25 00:09:141353
1354 if (m_rootScrollLayerImpl->scrollbarAnimationController())
1355 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureUpdate();
1356
1357 m_client->setNeedsCommitOnImplThread();
1358 m_client->setNeedsRedrawOnImplThread();
1359}
1360
[email protected]96baf3e2012-10-22 23:09:551361void LayerTreeHostImpl::pinchGestureEnd()
[email protected]94f206c12012-08-25 00:09:141362{
1363 m_pinchGestureActive = false;
1364
1365 if (m_rootScrollLayerImpl && m_rootScrollLayerImpl->scrollbarAnimationController())
1366 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureEnd();
1367
1368 m_client->setNeedsCommitOnImplThread();
1369}
1370
[email protected]96baf3e2012-10-22 23:09:551371void LayerTreeHostImpl::computeDoubleTapZoomDeltas(ScrollAndScaleSet* scrollInfo)
[email protected]94f206c12012-08-25 00:09:141372{
[email protected]f6250742012-11-09 04:46:561373 gfx::Vector2dF scaledScrollOffset = m_pageScaleAnimation->targetScrollOffset();
[email protected]9bdcfd642012-11-14 21:24:261374 if (!m_settings.pageScalePinchZoomEnabled)
[email protected]f6250742012-11-09 04:46:561375 scaledScrollOffset.Scale(m_pinchZoomViewport.pageScaleFactor());
1376 makeScrollAndScaleSet(scrollInfo, ToFlooredVector2d(scaledScrollOffset), m_pageScaleAnimation->targetPageScaleFactor());
[email protected]94f206c12012-08-25 00:09:141377}
1378
[email protected]96baf3e2012-10-22 23:09:551379void LayerTreeHostImpl::computePinchZoomDeltas(ScrollAndScaleSet* scrollInfo)
[email protected]94f206c12012-08-25 00:09:141380{
1381 if (!m_rootScrollLayerImpl)
1382 return;
1383
1384 // Only send fake scroll/zoom deltas if we're pinch zooming out by a
1385 // significant amount. This also ensures only one fake delta set will be
1386 // sent.
[email protected]23bbb412012-08-30 20:03:381387 const float pinchZoomOutSensitivity = 0.95f;
[email protected]1c0c9bc2012-10-08 22:41:481388 if (m_pinchZoomViewport.pageScaleDelta() > pinchZoomOutSensitivity)
[email protected]94f206c12012-08-25 00:09:141389 return;
1390
1391 // Compute where the scroll offset/page scale would be if fully pinch-zoomed
1392 // out from the anchor point.
[email protected]c9c1ebe2012-11-05 20:46:131393 gfx::Vector2dF scrollBegin = m_rootScrollLayerImpl->scrollOffset() + m_rootScrollLayerImpl->scrollDelta();
1394 scrollBegin.Scale(m_pinchZoomViewport.pageScaleDelta());
[email protected]1c0c9bc2012-10-08 22:41:481395 float scaleBegin = m_pinchZoomViewport.totalPageScaleFactor();
1396 float pageScaleDeltaToSend = m_pinchZoomViewport.minPageScaleFactor() / m_pinchZoomViewport.pageScaleFactor();
[email protected]01a15a72012-11-10 09:34:281397 gfx::SizeF scaledContentsSize = gfx::ScaleSize(contentSize(), pageScaleDeltaToSend);
[email protected]94f206c12012-08-25 00:09:141398
[email protected]c9c1ebe2012-11-05 20:46:131399 gfx::Vector2d anchorOffset = m_previousPinchAnchor.OffsetFromOrigin();
1400 gfx::Vector2dF scrollEnd = scrollBegin + anchorOffset;
1401 scrollEnd.Scale(m_pinchZoomViewport.minPageScaleFactor() / scaleBegin);
1402 scrollEnd -= anchorOffset;
[email protected]0eef4b882012-11-20 20:28:071403 scrollEnd.ClampToMax(gfx::RectF(scaledContentsSize).bottom_right() - gfx::Rect(m_deviceViewportSize).bottom_right());
[email protected]fe07b642012-11-10 00:07:591404 scrollEnd.ClampToMin(gfx::Vector2d());
[email protected]c9c1ebe2012-11-05 20:46:131405 scrollEnd.Scale(1 / pageScaleDeltaToSend);
1406 scrollEnd.Scale(m_deviceScaleFactor);
[email protected]94f206c12012-08-25 00:09:141407
[email protected]c9c1ebe2012-11-05 20:46:131408 makeScrollAndScaleSet(scrollInfo, gfx::ToRoundedVector2d(scrollEnd), m_pinchZoomViewport.minPageScaleFactor());
[email protected]94f206c12012-08-25 00:09:141409}
1410
[email protected]c9c1ebe2012-11-05 20:46:131411void LayerTreeHostImpl::makeScrollAndScaleSet(ScrollAndScaleSet* scrollInfo, gfx::Vector2d scrollOffset, float pageScale)
[email protected]94f206c12012-08-25 00:09:141412{
1413 if (!m_rootScrollLayerImpl)
1414 return;
1415
[email protected]96baf3e2012-10-22 23:09:551416 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]94f206c12012-08-25 00:09:141417 scroll.layerId = m_rootScrollLayerImpl->id();
[email protected]c9c1ebe2012-11-05 20:46:131418 scroll.scrollDelta = scrollOffset - m_rootScrollLayerImpl->scrollOffset();
[email protected]787465c2012-10-29 01:12:271419 scrollInfo->scrolls.push_back(scroll);
[email protected]94f206c12012-08-25 00:09:141420 m_rootScrollLayerImpl->setSentScrollDelta(scroll.scrollDelta);
[email protected]1c0c9bc2012-10-08 22:41:481421 scrollInfo->pageScaleDelta = pageScale / m_pinchZoomViewport.pageScaleFactor();
1422 m_pinchZoomViewport.setSentPageScaleDelta(scrollInfo->pageScaleDelta);
[email protected]94f206c12012-08-25 00:09:141423}
1424
[email protected]96baf3e2012-10-22 23:09:551425static void collectScrollDeltas(ScrollAndScaleSet* scrollInfo, LayerImpl* layerImpl)
[email protected]94f206c12012-08-25 00:09:141426{
1427 if (!layerImpl)
1428 return;
1429
[email protected]c9c1ebe2012-11-05 20:46:131430 if (!layerImpl->scrollDelta().IsZero()) {
1431 gfx::Vector2d scrollDelta = gfx::ToFlooredVector2d(layerImpl->scrollDelta());
[email protected]96baf3e2012-10-22 23:09:551432 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]94f206c12012-08-25 00:09:141433 scroll.layerId = layerImpl->id();
1434 scroll.scrollDelta = scrollDelta;
[email protected]787465c2012-10-29 01:12:271435 scrollInfo->scrolls.push_back(scroll);
[email protected]94f206c12012-08-25 00:09:141436 layerImpl->setSentScrollDelta(scrollDelta);
1437 }
1438
1439 for (size_t i = 0; i < layerImpl->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031440 collectScrollDeltas(scrollInfo, layerImpl->children()[i]);
[email protected]94f206c12012-08-25 00:09:141441}
1442
[email protected]96baf3e2012-10-22 23:09:551443scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::processScrollDeltas()
[email protected]94f206c12012-08-25 00:09:141444{
[email protected]96baf3e2012-10-22 23:09:551445 scoped_ptr<ScrollAndScaleSet> scrollInfo(new ScrollAndScaleSet());
[email protected]94f206c12012-08-25 00:09:141446
1447 if (m_pinchGestureActive || m_pageScaleAnimation) {
[email protected]1c0c9bc2012-10-08 22:41:481448 scrollInfo->pageScaleDelta = 1;
1449 m_pinchZoomViewport.setSentPageScaleDelta(1);
[email protected]f6250742012-11-09 04:46:561450 // FIXME(aelias): Make pinch-zoom painting optimization compatible with
[email protected]1c0c9bc2012-10-08 22:41:481451 // compositor-side scaling.
[email protected]9bdcfd642012-11-14 21:24:261452 if (!m_settings.pageScalePinchZoomEnabled && m_pinchGestureActive)
[email protected]f6250742012-11-09 04:46:561453 computePinchZoomDeltas(scrollInfo.get());
1454 else if (m_pageScaleAnimation.get())
1455 computeDoubleTapZoomDeltas(scrollInfo.get());
[email protected]a9f4bf22012-10-11 23:39:211456 return scrollInfo.Pass();
[email protected]94f206c12012-08-25 00:09:141457 }
1458
1459 collectScrollDeltas(scrollInfo.get(), m_rootLayerImpl.get());
[email protected]1c0c9bc2012-10-08 22:41:481460 scrollInfo->pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
1461 m_pinchZoomViewport.setSentPageScaleDelta(scrollInfo->pageScaleDelta);
[email protected]94f206c12012-08-25 00:09:141462
[email protected]a9f4bf22012-10-11 23:39:211463 return scrollInfo.Pass();
[email protected]94f206c12012-08-25 00:09:141464}
1465
[email protected]c8686a02012-11-27 08:29:001466gfx::Transform LayerTreeHostImpl::implTransform() const
[email protected]1c0c9bc2012-10-08 22:41:481467{
[email protected]9bdcfd642012-11-14 21:24:261468 return m_pinchZoomViewport.implTransform(m_settings.pageScalePinchZoomEnabled);
[email protected]1c0c9bc2012-10-08 22:41:481469}
1470
[email protected]96baf3e2012-10-22 23:09:551471void LayerTreeHostImpl::setFullRootLayerDamage()
[email protected]94f206c12012-08-25 00:09:141472{
1473 if (m_rootLayerImpl) {
[email protected]96baf3e2012-10-22 23:09:551474 RenderSurfaceImpl* renderSurface = m_rootLayerImpl->renderSurface();
[email protected]94f206c12012-08-25 00:09:141475 if (renderSurface)
1476 renderSurface->damageTracker()->forceFullDamageNextUpdate();
1477 }
1478}
1479
[email protected]30faac92012-10-29 00:06:291480void LayerTreeHostImpl::animatePageScale(base::TimeTicks time)
[email protected]94f206c12012-08-25 00:09:141481{
1482 if (!m_pageScaleAnimation || !m_rootScrollLayerImpl)
1483 return;
1484
[email protected]30faac92012-10-29 00:06:291485 double monotonicTime = (time - base::TimeTicks()).InSecondsF();
[email protected]c9c1ebe2012-11-05 20:46:131486 gfx::Vector2dF scrollTotal = m_rootScrollLayerImpl->scrollOffset() + m_rootScrollLayerImpl->scrollDelta();
[email protected]94f206c12012-08-25 00:09:141487
[email protected]f6250742012-11-09 04:46:561488 setPageScaleDelta(m_pageScaleAnimation->pageScaleFactorAtTime(monotonicTime) / m_pinchZoomViewport.pageScaleFactor());
[email protected]c9c1ebe2012-11-05 20:46:131489 gfx::Vector2dF nextScroll = m_pageScaleAnimation->scrollOffsetAtTime(monotonicTime);
[email protected]f6250742012-11-09 04:46:561490
[email protected]9bdcfd642012-11-14 21:24:261491 if (!m_settings.pageScalePinchZoomEnabled)
[email protected]f6250742012-11-09 04:46:561492 nextScroll.Scale(m_pinchZoomViewport.pageScaleFactor());
[email protected]94f206c12012-08-25 00:09:141493 m_rootScrollLayerImpl->scrollBy(nextScroll - scrollTotal);
1494 m_client->setNeedsRedrawOnImplThread();
1495
1496 if (m_pageScaleAnimation->isAnimationCompleteAtTime(monotonicTime)) {
[email protected]0023e8b2012-10-15 12:52:451497 m_pageScaleAnimation.reset();
[email protected]94f206c12012-08-25 00:09:141498 m_client->setNeedsCommitOnImplThread();
1499 }
1500}
1501
[email protected]30faac92012-10-29 00:06:291502void LayerTreeHostImpl::animateLayers(base::TimeTicks monotonicTime, base::Time wallClockTime)
[email protected]94f206c12012-08-25 00:09:141503{
[email protected]9bdcfd642012-11-14 21:24:261504 if (!m_settings.acceleratedAnimationEnabled || !m_needsAnimateLayers || !m_rootLayerImpl)
[email protected]94f206c12012-08-25 00:09:141505 return;
1506
[email protected]96baf3e2012-10-22 23:09:551507 TRACE_EVENT0("cc", "LayerTreeHostImpl::animateLayers");
[email protected]94f206c12012-08-25 00:09:141508
[email protected]96baf3e2012-10-22 23:09:551509 scoped_ptr<AnimationEventsVector> events(make_scoped_ptr(new AnimationEventsVector));
[email protected]94f206c12012-08-25 00:09:141510
1511 bool didAnimate = false;
1512 animateLayersRecursive(m_rootLayerImpl.get(), monotonicTime, wallClockTime, events.get(), didAnimate, m_needsAnimateLayers);
1513
[email protected]d3143c732012-10-05 19:17:591514 if (!events->empty())
[email protected]ec1d6d52012-10-10 01:28:571515 m_client->postAnimationEventsToMainThreadOnImplThread(events.Pass(), wallClockTime);
[email protected]94f206c12012-08-25 00:09:141516
1517 if (didAnimate)
1518 m_client->setNeedsRedrawOnImplThread();
1519
1520 setBackgroundTickingEnabled(!m_visible && m_needsAnimateLayers);
1521}
1522
[email protected]96baf3e2012-10-22 23:09:551523base::TimeDelta LayerTreeHostImpl::lowFrequencyAnimationInterval() const
[email protected]94f206c12012-08-25 00:09:141524{
[email protected]4481ddb622012-09-20 16:33:471525 return base::TimeDelta::FromSeconds(1);
[email protected]94f206c12012-08-25 00:09:141526}
1527
[email protected]96baf3e2012-10-22 23:09:551528void LayerTreeHostImpl::sendDidLoseContextRecursive(LayerImpl* current)
[email protected]94f206c12012-08-25 00:09:141529{
[email protected]1d993172012-10-18 18:15:041530 DCHECK(current);
[email protected]94f206c12012-08-25 00:09:141531 current->didLoseContext();
1532 if (current->maskLayer())
1533 sendDidLoseContextRecursive(current->maskLayer());
1534 if (current->replicaLayer())
1535 sendDidLoseContextRecursive(current->replicaLayer());
1536 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031537 sendDidLoseContextRecursive(current->children()[i]);
[email protected]94f206c12012-08-25 00:09:141538}
1539
[email protected]96baf3e2012-10-22 23:09:551540static void clearRenderSurfacesOnLayerImplRecursive(LayerImpl* current)
[email protected]94f206c12012-08-25 00:09:141541{
[email protected]1d993172012-10-18 18:15:041542 DCHECK(current);
[email protected]94f206c12012-08-25 00:09:141543 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]96baf3e2012-10-22 23:09:551544 clearRenderSurfacesOnLayerImplRecursive(current->children()[i]);
[email protected]94f206c12012-08-25 00:09:141545 current->clearRenderSurface();
1546}
1547
[email protected]96baf3e2012-10-22 23:09:551548void LayerTreeHostImpl::clearRenderSurfaces()
[email protected]94f206c12012-08-25 00:09:141549{
[email protected]96baf3e2012-10-22 23:09:551550 clearRenderSurfacesOnLayerImplRecursive(m_rootLayerImpl.get());
[email protected]94f206c12012-08-25 00:09:141551 m_renderSurfaceLayerList.clear();
1552}
1553
[email protected]96baf3e2012-10-22 23:09:551554std::string LayerTreeHostImpl::layerTreeAsText() const
[email protected]94f206c12012-08-25 00:09:141555{
[email protected]515e8d232012-09-10 19:15:271556 std::string str;
[email protected]94f206c12012-08-25 00:09:141557 if (m_rootLayerImpl) {
[email protected]515e8d232012-09-10 19:15:271558 str = m_rootLayerImpl->layerTreeAsText();
1559 str += "RenderSurfaces:\n";
1560 dumpRenderSurfaces(&str, 1, m_rootLayerImpl.get());
[email protected]94f206c12012-08-25 00:09:141561 }
[email protected]515e8d232012-09-10 19:15:271562 return str;
[email protected]94f206c12012-08-25 00:09:141563}
1564
[email protected]96baf3e2012-10-22 23:09:551565void LayerTreeHostImpl::dumpRenderSurfaces(std::string* str, int indent, const LayerImpl* layer) const
[email protected]94f206c12012-08-25 00:09:141566{
1567 if (layer->renderSurface())
[email protected]515e8d232012-09-10 19:15:271568 layer->renderSurface()->dumpSurface(str, indent);
[email protected]94f206c12012-08-25 00:09:141569
1570 for (size_t i = 0; i < layer->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031571 dumpRenderSurfaces(str, indent, layer->children()[i]);
[email protected]94f206c12012-08-25 00:09:141572}
1573
[email protected]96baf3e2012-10-22 23:09:551574int LayerTreeHostImpl::sourceAnimationFrameNumber() const
[email protected]94f206c12012-08-25 00:09:141575{
1576 return fpsCounter()->currentFrameNumber();
1577}
1578
[email protected]96baf3e2012-10-22 23:09:551579void LayerTreeHostImpl::renderingStats(RenderingStats* stats) const
[email protected]94f206c12012-08-25 00:09:141580{
[email protected]8b9af6b2012-09-27 00:36:361581 stats->numFramesSentToScreen = fpsCounter()->currentFrameNumber();
1582 stats->droppedFrameCount = fpsCounter()->droppedFrameCount();
[email protected]5c6fe1f82012-10-03 18:00:271583 stats->numImplThreadScrolls = m_numImplThreadScrolls;
1584 stats->numMainThreadScrolls = m_numMainThreadScrolls;
[email protected]9c2be6a2012-11-27 19:16:101585 stats->numLayersInLayerTree = m_cumulativeNumLayersInLayerTree;
[email protected]94f206c12012-08-25 00:09:141586}
1587
[email protected]30faac92012-10-29 00:06:291588void LayerTreeHostImpl::animateScrollbars(base::TimeTicks time)
[email protected]94f206c12012-08-25 00:09:141589{
[email protected]30faac92012-10-29 00:06:291590 animateScrollbarsRecursive(m_rootLayerImpl.get(), time);
[email protected]94f206c12012-08-25 00:09:141591}
1592
[email protected]30faac92012-10-29 00:06:291593void LayerTreeHostImpl::animateScrollbarsRecursive(LayerImpl* layer, base::TimeTicks time)
[email protected]94f206c12012-08-25 00:09:141594{
1595 if (!layer)
1596 return;
1597
[email protected]96baf3e2012-10-22 23:09:551598 ScrollbarAnimationController* scrollbarController = layer->scrollbarAnimationController();
[email protected]30faac92012-10-29 00:06:291599 double monotonicTime = (time - base::TimeTicks()).InSecondsF();
[email protected]94f206c12012-08-25 00:09:141600 if (scrollbarController && scrollbarController->animate(monotonicTime))
1601 m_client->setNeedsRedrawOnImplThread();
1602
1603 for (size_t i = 0; i < layer->children().size(); ++i)
[email protected]30faac92012-10-29 00:06:291604 animateScrollbarsRecursive(layer->children()[i], time);
[email protected]94f206c12012-08-25 00:09:141605}
1606
[email protected]d3143c732012-10-05 19:17:591607} // namespace cc