blob: 96b4a0275ce26f5b393042042ed43bd5498e8c9b [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]94f206c12012-08-25 00:09:14229{
[email protected]61de5812012-11-08 07:03:44230 DCHECK(m_proxy->isImplThread());
[email protected]94f206c12012-08-25 00:09:14231 didVisibilityChange(this, m_visible);
232}
233
[email protected]96baf3e2012-10-22 23:09:55234LayerTreeHostImpl::~LayerTreeHostImpl()
[email protected]94f206c12012-08-25 00:09:14235{
[email protected]61de5812012-11-08 07:03:44236 DCHECK(m_proxy->isImplThread());
[email protected]96baf3e2012-10-22 23:09:55237 TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()");
[email protected]94f206c12012-08-25 00:09:14238
239 if (m_rootLayerImpl)
240 clearRenderSurfaces();
241}
242
[email protected]96baf3e2012-10-22 23:09:55243void LayerTreeHostImpl::beginCommit()
[email protected]94f206c12012-08-25 00:09:14244{
245}
246
[email protected]96baf3e2012-10-22 23:09:55247void LayerTreeHostImpl::commitComplete()
[email protected]94f206c12012-08-25 00:09:14248{
[email protected]96baf3e2012-10-22 23:09:55249 TRACE_EVENT0("cc", "LayerTreeHostImpl::commitComplete");
[email protected]94f206c12012-08-25 00:09:14250 // Recompute max scroll position; must be after layer content bounds are
251 // updated.
[email protected]c9c1ebe2012-11-05 20:46:13252 updateMaxScrollOffset();
[email protected]3d21e022012-10-25 20:03:08253 m_client->sendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14254}
255
[email protected]96baf3e2012-10-22 23:09:55256bool LayerTreeHostImpl::canDraw()
[email protected]94f206c12012-08-25 00:09:14257{
[email protected]8db2213c2012-09-05 22:08:21258 // Note: If you are changing this function or any other function that might
259 // affect the result of canDraw, make sure to call m_client->onCanDrawStateChanged
260 // in the proper places and update the notifyIfCanDrawChanged test.
261
[email protected]94f206c12012-08-25 00:09:14262 if (!m_rootLayerImpl) {
[email protected]96baf3e2012-10-22 23:09:55263 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw no root layer");
[email protected]94f206c12012-08-25 00:09:14264 return false;
265 }
[email protected]aad0a0072012-11-01 18:15:58266 if (deviceViewportSize().IsEmpty()) {
[email protected]96baf3e2012-10-22 23:09:55267 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw empty viewport");
[email protected]94f206c12012-08-25 00:09:14268 return false;
269 }
270 if (!m_renderer) {
[email protected]96baf3e2012-10-22 23:09:55271 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw no renderer");
[email protected]94f206c12012-08-25 00:09:14272 return false;
273 }
274 if (m_contentsTexturesPurged) {
[email protected]96baf3e2012-10-22 23:09:55275 TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::canDraw contents textures purged");
[email protected]94f206c12012-08-25 00:09:14276 return false;
277 }
278 return true;
279}
280
[email protected]96baf3e2012-10-22 23:09:55281GraphicsContext* LayerTreeHostImpl::context() const
[email protected]94f206c12012-08-25 00:09:14282{
283 return m_context.get();
284}
285
[email protected]30faac92012-10-29 00:06:29286void LayerTreeHostImpl::animate(base::TimeTicks monotonicTime, base::Time wallClockTime)
[email protected]94f206c12012-08-25 00:09:14287{
288 animatePageScale(monotonicTime);
289 animateLayers(monotonicTime, wallClockTime);
[email protected]94f206c12012-08-25 00:09:14290 animateScrollbars(monotonicTime);
291}
292
[email protected]69a2a5be2012-11-14 06:51:44293void LayerTreeHostImpl::startPageScaleAnimation(gfx::Vector2d targetOffset, bool anchorPoint, float pageScale, base::TimeTicks startTime, base::TimeDelta duration)
[email protected]94f206c12012-08-25 00:09:14294{
295 if (!m_rootScrollLayerImpl)
296 return;
297
[email protected]c9c1ebe2012-11-05 20:46:13298 gfx::Vector2dF scrollTotal = m_rootScrollLayerImpl->scrollOffset() + m_rootScrollLayerImpl->scrollDelta();
[email protected]f6250742012-11-09 04:46:56299 gfx::SizeF scaledContentSize = contentSize();
[email protected]9bdcfd642012-11-14 21:24:26300 if (!m_settings.pageScalePinchZoomEnabled) {
[email protected]f6250742012-11-09 04:46:56301 scrollTotal.Scale(1 / m_pinchZoomViewport.pageScaleFactor());
[email protected]01a15a72012-11-10 09:34:28302 scaledContentSize.Scale(1 / m_pinchZoomViewport.pageScaleFactor());
[email protected]f6250742012-11-09 04:46:56303 }
[email protected]01a15a72012-11-10 09:34:28304 gfx::SizeF viewportSize = gfx::ScaleSize(m_deviceViewportSize, 1 / m_deviceScaleFactor);
[email protected]94f206c12012-08-25 00:09:14305
[email protected]30faac92012-10-29 00:06:29306 double startTimeSeconds = (startTime - base::TimeTicks()).InSecondsF();
[email protected]f6250742012-11-09 04:46:56307 m_pageScaleAnimation = PageScaleAnimation::create(scrollTotal, m_pinchZoomViewport.totalPageScaleFactor(), viewportSize, scaledContentSize, startTimeSeconds);
[email protected]94f206c12012-08-25 00:09:14308
309 if (anchorPoint) {
[email protected]69a2a5be2012-11-14 06:51:44310 gfx::Vector2dF anchor(targetOffset);
[email protected]9bdcfd642012-11-14 21:24:26311 if (!m_settings.pageScalePinchZoomEnabled)
[email protected]f6250742012-11-09 04:46:56312 anchor.Scale(1 / pageScale);
313 m_pageScaleAnimation->zoomWithAnchor(anchor, pageScale, duration.InSecondsF());
314 } else {
[email protected]69a2a5be2012-11-14 06:51:44315 gfx::Vector2dF scaledTargetOffset = targetOffset;
[email protected]9bdcfd642012-11-14 21:24:26316 if (!m_settings.pageScalePinchZoomEnabled)
[email protected]69a2a5be2012-11-14 06:51:44317 scaledTargetOffset.Scale(1 / pageScale);
318 m_pageScaleAnimation->zoomTo(scaledTargetOffset, pageScale, duration.InSecondsF());
[email protected]f6250742012-11-09 04:46:56319 }
[email protected]94f206c12012-08-25 00:09:14320
321 m_client->setNeedsRedrawOnImplThread();
322 m_client->setNeedsCommitOnImplThread();
323}
324
[email protected]96baf3e2012-10-22 23:09:55325void LayerTreeHostImpl::scheduleAnimation()
[email protected]94f206c12012-08-25 00:09:14326{
327 m_client->setNeedsRedrawOnImplThread();
328}
329
[email protected]2f1acc262012-11-16 21:42:22330bool LayerTreeHostImpl::haveTouchEventHandlersAt(const gfx::Point& viewportPoint)
331{
332
333 gfx::PointF deviceViewportPoint = gfx::ScalePoint(viewportPoint, m_deviceScaleFactor);
334
335 // First find out which layer was hit from the saved list of visible layers
336 // in the most recent frame.
337 LayerImpl* layerImplHitByPointInTouchHandlerRegion = LayerTreeHostCommon::findLayerThatIsHitByPointInTouchHandlerRegion(deviceViewportPoint, m_renderSurfaceLayerList);
338
339 if (layerImplHitByPointInTouchHandlerRegion)
340 return true;
341
342 return false;
343}
344
[email protected]96baf3e2012-10-22 23:09:55345void LayerTreeHostImpl::trackDamageForAllSurfaces(LayerImpl* rootDrawLayer, const LayerList& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14346{
347 // For now, we use damage tracking to compute a global scissor. To do this, we must
348 // compute all damage tracking before drawing anything, so that we know the root
349 // damage rect. The root damage rect is then used to scissor each surface.
350
351 for (int surfaceIndex = renderSurfaceLayerList.size() - 1; surfaceIndex >= 0 ; --surfaceIndex) {
[email protected]96baf3e2012-10-22 23:09:55352 LayerImpl* renderSurfaceLayer = renderSurfaceLayerList[surfaceIndex];
353 RenderSurfaceImpl* renderSurface = renderSurfaceLayer->renderSurface();
[email protected]1d993172012-10-18 18:15:04354 DCHECK(renderSurface);
[email protected]4000abf2012-10-23 04:45:45355 renderSurface->damageTracker()->updateDamageTrackingState(renderSurface->layerList(), renderSurfaceLayer->id(), renderSurface->surfacePropertyChangedOnlyFromDescendant(), renderSurface->contentRect(), renderSurfaceLayer->maskLayer(), renderSurfaceLayer->filters(), renderSurfaceLayer->filter());
[email protected]94f206c12012-08-25 00:09:14356 }
357}
358
[email protected]96baf3e2012-10-22 23:09:55359void LayerTreeHostImpl::updateRootScrollLayerImplTransform()
[email protected]1c0c9bc2012-10-08 22:41:48360{
361 if (m_rootScrollLayerImpl) {
362 m_rootScrollLayerImpl->setImplTransform(implTransform());
363 }
364}
365
[email protected]96baf3e2012-10-22 23:09:55366void LayerTreeHostImpl::calculateRenderSurfaceLayerList(LayerList& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:14367{
[email protected]1d993172012-10-18 18:15:04368 DCHECK(renderSurfaceLayerList.empty());
369 DCHECK(m_rootLayerImpl);
370 DCHECK(m_renderer); // For maxTextureSize.
[email protected]94f206c12012-08-25 00:09:14371
372 {
[email protected]1c0c9bc2012-10-08 22:41:48373 updateRootScrollLayerImplTransform();
374
[email protected]96baf3e2012-10-22 23:09:55375 TRACE_EVENT0("cc", "LayerTreeHostImpl::calcDrawEtc");
[email protected]518ee582012-10-24 18:29:44376 float pageScaleFactor = m_pinchZoomViewport.pageScaleFactor();
377 LayerTreeHostCommon::calculateDrawTransforms(m_rootLayerImpl.get(), deviceViewportSize(), m_deviceScaleFactor, pageScaleFactor, &m_layerSorter, rendererCapabilities().maxTextureSize, renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14378
379 trackDamageForAllSurfaces(m_rootLayerImpl.get(), renderSurfaceLayerList);
380 }
381}
382
[email protected]96baf3e2012-10-22 23:09:55383void LayerTreeHostImpl::FrameData::appendRenderPass(scoped_ptr<RenderPass> renderPass)
[email protected]467b3612012-08-28 07:41:16384{
[email protected]96baf3e2012-10-22 23:09:55385 RenderPass* pass = renderPass.get();
[email protected]f8ad8342012-09-27 20:07:02386 renderPasses.push_back(pass);
[email protected]f57bbc02012-11-21 07:02:15387 renderPassesById.set(pass->id, renderPass.Pass());
388}
389
390static void appendQuadsForLayer(RenderPass* targetRenderPass, LayerImpl* layer, OcclusionTrackerImpl& occlusionTracker, AppendQuadsData& appendQuadsData)
391{
392 bool forSurface = false;
393 QuadCuller quadCuller(targetRenderPass->quad_list,
394 targetRenderPass->shared_quad_state_list,
395 layer,
396 occlusionTracker,
397 layer->showDebugBorders(),
398 forSurface);
399 layer->appendQuads(quadCuller, appendQuadsData);
400}
401
402static void appendQuadsForRenderSurfaceLayer(RenderPass* targetRenderPass, LayerImpl* layer, const RenderPass* contributingRenderPass, OcclusionTrackerImpl& occlusionTracker, AppendQuadsData& appendQuadsData)
403{
404 bool forSurface = true;
405 QuadCuller quadCuller(targetRenderPass->quad_list,
406 targetRenderPass->shared_quad_state_list,
407 layer,
408 occlusionTracker,
409 layer->showDebugBorders(),
410 forSurface);
411
412 bool isReplica = false;
413 layer->renderSurface()->appendQuads(quadCuller,
414 appendQuadsData,
415 isReplica,
416 contributingRenderPass->id);
417
418 // Add replica after the surface so that it appears below the surface.
419 if (layer->hasReplica()) {
420 isReplica = true;
421 layer->renderSurface()->appendQuads(quadCuller,
422 appendQuadsData,
423 isReplica,
424 contributingRenderPass->id);
425 }
426}
427
428static void appendQuadsToFillScreen(RenderPass* targetRenderPass, LayerImpl* rootLayer, SkColor screenBackgroundColor, const OcclusionTrackerImpl& occlusionTracker)
429{
430 if (!rootLayer || !SkColorGetA(screenBackgroundColor))
431 return;
432
433 Region fillRegion = occlusionTracker.computeVisibleRegionInScreen();
434 if (fillRegion.IsEmpty())
435 return;
436
437 bool forSurface = false;
438 QuadCuller quadCuller(targetRenderPass->quad_list,
439 targetRenderPass->shared_quad_state_list,
440 rootLayer,
441 occlusionTracker,
442 rootLayer->showDebugBorders(),
443 forSurface);
444
445 // Manually create the quad state for the gutter quads, as the root layer
446 // doesn't have any bounds and so can't generate this itself.
447 // FIXME: Make the gutter quads generated by the solid color layer (make it smarter about generating quads to fill unoccluded areas).
448
[email protected]c8686a02012-11-27 08:29:00449 DCHECK(rootLayer->screenSpaceTransform().IsInvertible());
[email protected]f57bbc02012-11-21 07:02:15450
451 gfx::Rect rootTargetRect = rootLayer->renderSurface()->contentRect();
452 float opacity = 1;
453 SharedQuadState* sharedQuadState = quadCuller.useSharedQuadState(SharedQuadState::Create());
454 sharedQuadState->SetAll(rootLayer->drawTransform(),
455 rootTargetRect,
456 rootTargetRect,
[email protected]dc462d782012-11-21 21:43:01457 rootTargetRect,
458 false,
[email protected]f57bbc02012-11-21 07:02:15459 opacity);
460
461 AppendQuadsData appendQuadsData;
[email protected]c8686a02012-11-27 08:29:00462 gfx::Transform transformToLayerSpace = MathUtil::inverse(rootLayer->screenSpaceTransform());
[email protected]f57bbc02012-11-21 07:02:15463 for (Region::Iterator fillRects(fillRegion); fillRects.has_rect(); fillRects.next()) {
464 // The root layer transform is composed of translations and scales only,
465 // no perspective, so mapping is sufficient.
466 gfx::Rect layerRect = MathUtil::mapClippedRect(transformToLayerSpace, fillRects.rect());
467 // Skip the quad culler and just append the quads directly to avoid
468 // occlusion checks.
469 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
470 quad->SetNew(sharedQuadState, layerRect, screenBackgroundColor);
471 quadCuller.append(quad.PassAs<DrawQuad>(), appendQuadsData);
472 }
[email protected]467b3612012-08-28 07:41:16473}
474
[email protected]96baf3e2012-10-22 23:09:55475bool LayerTreeHostImpl::calculateRenderPasses(FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14476{
[email protected]1d993172012-10-18 18:15:04477 DCHECK(frame.renderPasses.empty());
[email protected]94f206c12012-08-25 00:09:14478
479 calculateRenderSurfaceLayerList(*frame.renderSurfaceLayerList);
480
[email protected]96baf3e2012-10-22 23:09:55481 TRACE_EVENT1("cc", "LayerTreeHostImpl::calculateRenderPasses", "renderSurfaceLayerList.size()", static_cast<long long unsigned>(frame.renderSurfaceLayerList->size()));
[email protected]94f206c12012-08-25 00:09:14482
483 // Create the render passes in dependency order.
[email protected]94f206c12012-08-25 00:09:14484 for (int surfaceIndex = frame.renderSurfaceLayerList->size() - 1; surfaceIndex >= 0 ; --surfaceIndex) {
[email protected]96baf3e2012-10-22 23:09:55485 LayerImpl* renderSurfaceLayer = (*frame.renderSurfaceLayerList)[surfaceIndex];
[email protected]467b3612012-08-28 07:41:16486 renderSurfaceLayer->renderSurface()->appendRenderPasses(frame);
[email protected]94f206c12012-08-25 00:09:14487 }
488
[email protected]9bdcfd642012-11-14 21:24:26489 bool recordMetricsForFrame = m_settings.showOverdrawInTracing && base::debug::TraceLog::GetInstance() && base::debug::TraceLog::GetInstance()->IsEnabled();
[email protected]96baf3e2012-10-22 23:09:55490 OcclusionTrackerImpl occlusionTracker(m_rootLayerImpl->renderSurface()->contentRect(), recordMetricsForFrame);
[email protected]94f206c12012-08-25 00:09:14491 occlusionTracker.setMinimumTrackingSize(m_settings.minimumOcclusionTrackingSize);
492
493 if (settings().showOccludingRects)
494 occlusionTracker.setOccludingScreenSpaceRectsContainer(&frame.occludingScreenSpaceRects);
[email protected]4d8804e2012-11-15 01:51:10495 if (settings().showNonOccludingRects)
496 occlusionTracker.setNonOccludingScreenSpaceRectsContainer(&frame.nonOccludingScreenSpaceRects);
[email protected]94f206c12012-08-25 00:09:14497
498 // 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:55499 typedef LayerIterator<LayerImpl, std::vector<LayerImpl*>, RenderSurfaceImpl, LayerIteratorActions::FrontToBack> LayerIteratorType;
[email protected]94f206c12012-08-25 00:09:14500
501 // Typically when we are missing a texture and use a checkerboard quad, we still draw the frame. However when the layer being
502 // checkerboarded is moving due to an impl-animation, we drop the frame to avoid flashing due to the texture suddenly appearing
503 // in the future.
504 bool drawFrame = true;
505
[email protected]96baf3e2012-10-22 23:09:55506 LayerIteratorType end = LayerIteratorType::end(frame.renderSurfaceLayerList);
507 for (LayerIteratorType it = LayerIteratorType::begin(frame.renderSurfaceLayerList); it != end; ++it) {
508 RenderPass::Id targetRenderPassId = it.targetRenderSurfaceLayer()->renderSurface()->renderPassId();
509 RenderPass* targetRenderPass = frame.renderPassesById.get(targetRenderPassId);
[email protected]94f206c12012-08-25 00:09:14510
511 occlusionTracker.enterLayer(it);
512
[email protected]f57bbc02012-11-21 07:02:15513 AppendQuadsData appendQuadsData(targetRenderPass->id);
[email protected]89228202012-08-29 03:20:30514
[email protected]94f206c12012-08-25 00:09:14515 if (it.representsContributingRenderSurface()) {
[email protected]96baf3e2012-10-22 23:09:55516 RenderPass::Id contributingRenderPassId = it->renderSurface()->renderPassId();
517 RenderPass* contributingRenderPass = frame.renderPassesById.get(contributingRenderPassId);
[email protected]f57bbc02012-11-21 07:02:15518 appendQuadsForRenderSurfaceLayer(targetRenderPass, *it, contributingRenderPass, occlusionTracker, appendQuadsData);
[email protected]aad0a0072012-11-01 18:15:58519 } else if (it.representsItself() && !it->visibleContentRect().IsEmpty()) {
[email protected]94f206c12012-08-25 00:09:14520 bool hasOcclusionFromOutsideTargetSurface;
[email protected]710ffc02012-10-30 21:42:02521 bool implDrawTransformIsUnknown = false;
522 if (occlusionTracker.occluded(it->renderTarget(), it->visibleContentRect(), it->drawTransform(), implDrawTransformIsUnknown, it->drawableContentRect(), &hasOcclusionFromOutsideTargetSurface))
[email protected]89228202012-08-29 03:20:30523 appendQuadsData.hadOcclusionFromOutsideTargetSurface |= hasOcclusionFromOutsideTargetSurface;
524 else {
[email protected]94f206c12012-08-25 00:09:14525 it->willDraw(m_resourceProvider.get());
[email protected]d58499a2012-10-09 22:27:47526 frame.willDrawLayers.push_back(*it);
[email protected]7d929c02012-09-20 17:26:57527
528 if (it->hasContributingDelegatedRenderPasses()) {
[email protected]96baf3e2012-10-22 23:09:55529 RenderPass::Id contributingRenderPassId = it->firstContributingRenderPassId();
[email protected]7d929c02012-09-20 17:26:57530 while (frame.renderPassesById.contains(contributingRenderPassId)) {
[email protected]96baf3e2012-10-22 23:09:55531 RenderPass* renderPass = frame.renderPassesById.get(contributingRenderPassId);
[email protected]7d929c02012-09-20 17:26:57532
[email protected]f57bbc02012-11-21 07:02:15533 AppendQuadsData appendQuadsData(renderPass->id);
534 appendQuadsForLayer(renderPass, *it, occlusionTracker, appendQuadsData);
[email protected]7d929c02012-09-20 17:26:57535
536 contributingRenderPassId = it->nextContributingRenderPassId(contributingRenderPassId);
537 }
538 }
539
[email protected]f57bbc02012-11-21 07:02:15540 appendQuadsForLayer(targetRenderPass, *it, occlusionTracker, appendQuadsData);
[email protected]94f206c12012-08-25 00:09:14541 }
542 }
543
[email protected]89228202012-08-29 03:20:30544 if (appendQuadsData.hadOcclusionFromOutsideTargetSurface)
[email protected]f57bbc02012-11-21 07:02:15545 targetRenderPass->has_occlusion_from_outside_target_surface = true;
[email protected]89228202012-08-29 03:20:30546
547 if (appendQuadsData.hadMissingTiles) {
[email protected]94f206c12012-08-25 00:09:14548 bool layerHasAnimatingTransform = it->screenSpaceTransformIsAnimating() || it->drawTransformIsAnimating();
[email protected]22619922012-11-14 17:58:10549 if (layerHasAnimatingTransform)
[email protected]94f206c12012-08-25 00:09:14550 drawFrame = false;
551 }
552
553 occlusionTracker.leaveLayer(it);
554 }
555
[email protected]1d993172012-10-18 18:15:04556#ifndef NDEBUG
[email protected]94f206c12012-08-25 00:09:14557 for (size_t i = 0; i < frame.renderPasses.size(); ++i) {
[email protected]f57bbc02012-11-21 07:02:15558 for (size_t j = 0; j < frame.renderPasses[i]->quad_list.size(); ++j)
559 DCHECK(frame.renderPasses[i]->quad_list[j]->shared_quad_state);
560 DCHECK(frame.renderPassesById.contains(frame.renderPasses[i]->id));
[email protected]94f206c12012-08-25 00:09:14561 }
562#endif
563
564 if (!m_hasTransparentBackground) {
[email protected]f57bbc02012-11-21 07:02:15565 frame.renderPasses.back()->has_transparent_background = false;
566 appendQuadsToFillScreen(frame.renderPasses.back(), m_rootLayerImpl.get(), m_backgroundColor, occlusionTracker);
[email protected]94f206c12012-08-25 00:09:14567 }
568
569 if (drawFrame)
570 occlusionTracker.overdrawMetrics().recordMetrics(this);
571
572 removeRenderPasses(CullRenderPassesWithNoQuads(), frame);
573 m_renderer->decideRenderPassAllocationsForFrame(frame.renderPasses);
574 removeRenderPasses(CullRenderPassesWithCachedTextures(*m_renderer), frame);
575
576 return drawFrame;
577}
578
[email protected]30faac92012-10-29 00:06:29579void LayerTreeHostImpl::animateLayersRecursive(LayerImpl* current, base::TimeTicks monotonicTime, base::Time wallClockTime, AnimationEventsVector* events, bool& didAnimate, bool& needsAnimateLayers)
[email protected]94f206c12012-08-25 00:09:14580{
581 bool subtreeNeedsAnimateLayers = false;
582
[email protected]96baf3e2012-10-22 23:09:55583 LayerAnimationController* currentController = current->layerAnimationController();
[email protected]94f206c12012-08-25 00:09:14584
585 bool hadActiveAnimation = currentController->hasActiveAnimation();
[email protected]30faac92012-10-29 00:06:29586 double monotonicTimeSeconds = (monotonicTime - base::TimeTicks()).InSecondsF();
587 currentController->animate(monotonicTimeSeconds, events);
[email protected]94f206c12012-08-25 00:09:14588 bool startedAnimation = events->size() > 0;
589
590 // We animated if we either ticked a running animation, or started a new animation.
591 if (hadActiveAnimation || startedAnimation)
592 didAnimate = true;
593
594 // If the current controller still has an active animation, we must continue animating layers.
595 if (currentController->hasActiveAnimation())
596 subtreeNeedsAnimateLayers = true;
597
598 for (size_t i = 0; i < current->children().size(); ++i) {
599 bool childNeedsAnimateLayers = false;
[email protected]0920e24f2012-09-20 03:34:03600 animateLayersRecursive(current->children()[i], monotonicTime, wallClockTime, events, didAnimate, childNeedsAnimateLayers);
[email protected]94f206c12012-08-25 00:09:14601 if (childNeedsAnimateLayers)
602 subtreeNeedsAnimateLayers = true;
603 }
604
605 needsAnimateLayers = subtreeNeedsAnimateLayers;
606}
607
[email protected]96baf3e2012-10-22 23:09:55608void LayerTreeHostImpl::setBackgroundTickingEnabled(bool enabled)
[email protected]94f206c12012-08-25 00:09:14609{
610 // Lazily create the timeSource adapter so that we can vary the interval for testing.
611 if (!m_timeSourceClientAdapter)
[email protected]61de5812012-11-08 07:03:44612 m_timeSourceClientAdapter = LayerTreeHostImplTimeSourceAdapter::create(this, DelayBasedTimeSource::create(lowFrequencyAnimationInterval(), m_proxy->currentThread()));
[email protected]94f206c12012-08-25 00:09:14613
614 m_timeSourceClientAdapter->setActive(enabled);
615}
616
[email protected]aad0a0072012-11-01 18:15:58617gfx::Size LayerTreeHostImpl::contentSize() const
[email protected]94f206c12012-08-25 00:09:14618{
619 // TODO(aelias): Hardcoding the first child here is weird. Think of
620 // a cleaner way to get the contentBounds on the Impl side.
621 if (!m_rootScrollLayerImpl || m_rootScrollLayerImpl->children().isEmpty())
[email protected]aad0a0072012-11-01 18:15:58622 return gfx::Size();
[email protected]94f206c12012-08-25 00:09:14623 return m_rootScrollLayerImpl->children()[0]->contentBounds();
624}
625
[email protected]96baf3e2012-10-22 23:09:55626static inline RenderPass* findRenderPassById(RenderPass::Id renderPassId, const LayerTreeHostImpl::FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14627{
[email protected]96baf3e2012-10-22 23:09:55628 RenderPassIdHashMap::const_iterator it = frame.renderPassesById.find(renderPassId);
[email protected]1d993172012-10-18 18:15:04629 DCHECK(it != frame.renderPassesById.end());
[email protected]87cea5372012-09-26 18:59:56630 return it->second;
[email protected]94f206c12012-08-25 00:09:14631}
632
[email protected]96baf3e2012-10-22 23:09:55633static void removeRenderPassesRecursive(RenderPass::Id removeRenderPassId, LayerTreeHostImpl::FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14634{
[email protected]96baf3e2012-10-22 23:09:55635 RenderPass* removeRenderPass = findRenderPassById(removeRenderPassId, frame);
636 RenderPassList& renderPasses = frame.renderPasses;
637 RenderPassList::iterator toRemove = std::find(renderPasses.begin(), renderPasses.end(), removeRenderPass);
[email protected]94f206c12012-08-25 00:09:14638
639 // The pass was already removed by another quad - probably the original, and we are the replica.
[email protected]f8ad8342012-09-27 20:07:02640 if (toRemove == renderPasses.end())
[email protected]94f206c12012-08-25 00:09:14641 return;
642
[email protected]96baf3e2012-10-22 23:09:55643 const RenderPass* removedPass = *toRemove;
[email protected]f8ad8342012-09-27 20:07:02644 frame.renderPasses.erase(toRemove);
[email protected]94f206c12012-08-25 00:09:14645
646 // Now follow up for all RenderPass quads and remove their RenderPasses recursively.
[email protected]f57bbc02012-11-21 07:02:15647 const QuadList& quadList = removedPass->quad_list;
[email protected]96baf3e2012-10-22 23:09:55648 QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14649 for (; quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
[email protected]96baf3e2012-10-22 23:09:55650 DrawQuad* currentQuad = (*quadListIterator);
[email protected]1bc93f62012-11-17 19:29:50651 if (currentQuad->material != DrawQuad::RENDER_PASS)
[email protected]94f206c12012-08-25 00:09:14652 continue;
653
[email protected]c22418b2012-11-20 23:06:26654 RenderPass::Id nextRemoveRenderPassId = RenderPassDrawQuad::MaterialCast(currentQuad)->render_pass_id;
[email protected]94f206c12012-08-25 00:09:14655 removeRenderPassesRecursive(nextRemoveRenderPassId, frame);
656 }
657}
658
[email protected]96baf3e2012-10-22 23:09:55659bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures::shouldRemoveRenderPass(const RenderPassDrawQuad& quad, const FrameData&) const
[email protected]94f206c12012-08-25 00:09:14660{
[email protected]c22418b2012-11-20 23:06:26661 return quad.contents_changed_since_last_frame.IsEmpty() && m_renderer.haveCachedResourcesForRenderPassId(quad.render_pass_id);
[email protected]94f206c12012-08-25 00:09:14662}
663
[email protected]96baf3e2012-10-22 23:09:55664bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::shouldRemoveRenderPass(const RenderPassDrawQuad& quad, const FrameData& frame) const
[email protected]94f206c12012-08-25 00:09:14665{
[email protected]c22418b2012-11-20 23:06:26666 const RenderPass* renderPass = findRenderPassById(quad.render_pass_id, frame);
[email protected]96baf3e2012-10-22 23:09:55667 const RenderPassList& renderPasses = frame.renderPasses;
668 RenderPassList::const_iterator foundPass = std::find(renderPasses.begin(), renderPasses.end(), renderPass);
[email protected]94f206c12012-08-25 00:09:14669
[email protected]f8ad8342012-09-27 20:07:02670 bool renderPassAlreadyRemoved = foundPass == renderPasses.end();
[email protected]94f206c12012-08-25 00:09:14671 if (renderPassAlreadyRemoved)
672 return false;
673
674 // If any quad or RenderPass draws into this RenderPass, then keep it.
[email protected]f57bbc02012-11-21 07:02:15675 const QuadList& quadList = (*foundPass)->quad_list;
[email protected]96baf3e2012-10-22 23:09:55676 for (QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin(); quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
677 DrawQuad* currentQuad = *quadListIterator;
[email protected]94f206c12012-08-25 00:09:14678
[email protected]1bc93f62012-11-17 19:29:50679 if (currentQuad->material != DrawQuad::RENDER_PASS)
[email protected]94f206c12012-08-25 00:09:14680 return false;
681
[email protected]c22418b2012-11-20 23:06:26682 const RenderPass* contributingPass = findRenderPassById(RenderPassDrawQuad::MaterialCast(currentQuad)->render_pass_id, frame);
[email protected]96baf3e2012-10-22 23:09:55683 RenderPassList::const_iterator foundContributingPass = std::find(renderPasses.begin(), renderPasses.end(), contributingPass);
[email protected]f8ad8342012-09-27 20:07:02684 if (foundContributingPass != renderPasses.end())
[email protected]94f206c12012-08-25 00:09:14685 return false;
686 }
687 return true;
688}
689
690// Defined for linking tests.
[email protected]52347c842012-11-02 21:06:20691template CC_EXPORT void LayerTreeHostImpl::removeRenderPasses<LayerTreeHostImpl::CullRenderPassesWithCachedTextures>(CullRenderPassesWithCachedTextures, FrameData&);
692template CC_EXPORT void LayerTreeHostImpl::removeRenderPasses<LayerTreeHostImpl::CullRenderPassesWithNoQuads>(CullRenderPassesWithNoQuads, FrameData&);
[email protected]94f206c12012-08-25 00:09:14693
694// static
695template<typename RenderPassCuller>
[email protected]96baf3e2012-10-22 23:09:55696void LayerTreeHostImpl::removeRenderPasses(RenderPassCuller culler, FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14697{
698 for (size_t it = culler.renderPassListBegin(frame.renderPasses); it != culler.renderPassListEnd(frame.renderPasses); it = culler.renderPassListNext(it)) {
[email protected]96baf3e2012-10-22 23:09:55699 const RenderPass* currentPass = frame.renderPasses[it];
[email protected]f57bbc02012-11-21 07:02:15700 const QuadList& quadList = currentPass->quad_list;
[email protected]96baf3e2012-10-22 23:09:55701 QuadList::constBackToFrontIterator quadListIterator = quadList.backToFrontBegin();
[email protected]94f206c12012-08-25 00:09:14702
703 for (; quadListIterator != quadList.backToFrontEnd(); ++quadListIterator) {
[email protected]96baf3e2012-10-22 23:09:55704 DrawQuad* currentQuad = *quadListIterator;
[email protected]94f206c12012-08-25 00:09:14705
[email protected]1bc93f62012-11-17 19:29:50706 if (currentQuad->material != DrawQuad::RENDER_PASS)
[email protected]94f206c12012-08-25 00:09:14707 continue;
708
[email protected]96baf3e2012-10-22 23:09:55709 RenderPassDrawQuad* renderPassQuad = static_cast<RenderPassDrawQuad*>(currentQuad);
[email protected]94f206c12012-08-25 00:09:14710 if (!culler.shouldRemoveRenderPass(*renderPassQuad, frame))
711 continue;
712
713 // We are changing the vector in the middle of iteration. Because we
714 // delete render passes that draw into the current pass, we are
715 // guaranteed that any data from the iterator to the end will not
716 // change. So, capture the iterator position from the end of the
717 // list, and restore it after the change.
718 int positionFromEnd = frame.renderPasses.size() - it;
[email protected]c22418b2012-11-20 23:06:26719 removeRenderPassesRecursive(renderPassQuad->render_pass_id, frame);
[email protected]94f206c12012-08-25 00:09:14720 it = frame.renderPasses.size() - positionFromEnd;
[email protected]1d993172012-10-18 18:15:04721 DCHECK(it >= 0);
[email protected]94f206c12012-08-25 00:09:14722 }
723 }
724}
725
[email protected]96baf3e2012-10-22 23:09:55726bool LayerTreeHostImpl::prepareToDraw(FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14727{
[email protected]96baf3e2012-10-22 23:09:55728 TRACE_EVENT0("cc", "LayerTreeHostImpl::prepareToDraw");
[email protected]1d993172012-10-18 18:15:04729 DCHECK(canDraw());
[email protected]94f206c12012-08-25 00:09:14730
731 frame.renderSurfaceLayerList = &m_renderSurfaceLayerList;
732 frame.renderPasses.clear();
733 frame.renderPassesById.clear();
734 frame.renderSurfaceLayerList->clear();
735 frame.willDrawLayers.clear();
736
737 if (!calculateRenderPasses(frame))
738 return false;
739
740 // If we return true, then we expect drawLayers() to be called before this function is called again.
741 return true;
742}
743
[email protected]96baf3e2012-10-22 23:09:55744void LayerTreeHostImpl::enforceManagedMemoryPolicy(const ManagedMemoryPolicy& policy)
[email protected]94f206c12012-08-25 00:09:14745{
[email protected]a0a00842012-10-22 22:50:28746 bool evictedResources = m_client->reduceContentsTextureMemoryOnImplThread(
747 m_visible ? policy.bytesLimitWhenVisible : policy.bytesLimitWhenNotVisible,
748 m_visible ? policy.priorityCutoffWhenVisible : policy.priorityCutoffWhenNotVisible);
[email protected]b1969fa2012-10-17 20:16:29749 if (evictedResources) {
750 setContentsTexturesPurged();
751 m_client->setNeedsCommitOnImplThread();
752 m_client->onCanDrawStateChanged(canDraw());
753 }
[email protected]3d21e022012-10-25 20:03:08754 m_client->sendManagedMemoryStats();
[email protected]94f206c12012-08-25 00:09:14755}
756
[email protected]61de5812012-11-08 07:03:44757bool LayerTreeHostImpl::hasImplThread() const
758{
759 return m_proxy->hasImplThread();
760}
761
[email protected]96baf3e2012-10-22 23:09:55762void LayerTreeHostImpl::setManagedMemoryPolicy(const ManagedMemoryPolicy& policy)
[email protected]94f206c12012-08-25 00:09:14763{
[email protected]a0a00842012-10-22 22:50:28764 if (m_managedMemoryPolicy == policy)
[email protected]94f206c12012-08-25 00:09:14765 return;
[email protected]61de5812012-11-08 07:03:44766
[email protected]a0a00842012-10-22 22:50:28767 m_managedMemoryPolicy = policy;
[email protected]61de5812012-11-08 07:03:44768 if (!m_proxy->hasImplThread()) {
769 // FIXME: In single-thread mode, this can be called on the main thread
770 // by GLRenderer::onMemoryAllocationChanged.
771 DebugScopedSetImplThread implThread(m_proxy);
772 enforceManagedMemoryPolicy(m_managedMemoryPolicy);
773 } else {
774 DCHECK(m_proxy->isImplThread());
775 enforceManagedMemoryPolicy(m_managedMemoryPolicy);
776 }
[email protected]a0a00842012-10-22 22:50:28777 // We always need to commit after changing the memory policy because the new
778 // limit can result in more or less content having texture allocated for it.
[email protected]94f206c12012-08-25 00:09:14779 m_client->setNeedsCommitOnImplThread();
780}
781
[email protected]96baf3e2012-10-22 23:09:55782void LayerTreeHostImpl::onVSyncParametersChanged(double monotonicTimebase, double intervalInSeconds)
[email protected]94f206c12012-08-25 00:09:14783{
[email protected]30faac92012-10-29 00:06:29784 base::TimeTicks timebase = base::TimeTicks::FromInternalValue(monotonicTimebase * base::Time::kMicrosecondsPerSecond);
785 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(intervalInSeconds * base::Time::kMicrosecondsPerSecond);
786 m_client->onVSyncParametersChanged(timebase, interval);
[email protected]94f206c12012-08-25 00:09:14787}
788
[email protected]96baf3e2012-10-22 23:09:55789void LayerTreeHostImpl::drawLayers(const FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14790{
[email protected]96baf3e2012-10-22 23:09:55791 TRACE_EVENT0("cc", "LayerTreeHostImpl::drawLayers");
[email protected]1d993172012-10-18 18:15:04792 DCHECK(canDraw());
793 DCHECK(!frame.renderPasses.empty());
[email protected]94f206c12012-08-25 00:09:14794
795 // FIXME: use the frame begin time from the overall compositor scheduler.
796 // This value is currently inaccessible because it is up in Chromium's
797 // RenderWidget.
[email protected]6bea87c2012-10-13 00:15:21798 m_fpsCounter->markBeginningOfFrame(base::TimeTicks::Now());
[email protected]94f206c12012-08-25 00:09:14799
800 if (m_settings.showDebugRects())
[email protected]4d8804e2012-11-15 01:51:10801 m_debugRectHistory->saveDebugRectsForCurrentFrame(m_rootLayerImpl.get(), *frame.renderSurfaceLayerList, frame.occludingScreenSpaceRects, frame.nonOccludingScreenSpaceRects, settings());
[email protected]94f206c12012-08-25 00:09:14802
803 // Because the contents of the HUD depend on everything else in the frame, the contents
804 // of its texture are updated as the last thing before the frame is drawn.
805 if (m_hudLayerImpl)
806 m_hudLayerImpl->updateHudTexture(m_resourceProvider.get());
807
808 m_renderer->drawFrame(frame.renderPasses, frame.renderPassesById);
809
810 // Once a RenderPass has been drawn, its damage should be cleared in
811 // case the RenderPass will be reused next frame.
812 for (unsigned int i = 0; i < frame.renderPasses.size(); i++)
[email protected]f57bbc02012-11-21 07:02:15813 frame.renderPasses[i]->damage_rect = gfx::RectF();
[email protected]94f206c12012-08-25 00:09:14814
815 // The next frame should start by assuming nothing has changed, and changes are noted as they occur.
816 for (unsigned int i = 0; i < frame.renderSurfaceLayerList->size(); i++)
817 (*frame.renderSurfaceLayerList)[i]->renderSurface()->damageTracker()->didDrawDamagedArea();
818 m_rootLayerImpl->resetAllChangeTrackingForSubtree();
819}
820
[email protected]96baf3e2012-10-22 23:09:55821void LayerTreeHostImpl::didDrawAllLayers(const FrameData& frame)
[email protected]94f206c12012-08-25 00:09:14822{
823 for (size_t i = 0; i < frame.willDrawLayers.size(); ++i)
824 frame.willDrawLayers[i]->didDraw(m_resourceProvider.get());
[email protected]b914e102012-10-02 08:11:52825
826 // Once all layers have been drawn, pending texture uploads should no
827 // longer block future uploads.
[email protected]e2249592012-10-19 06:59:09828 m_resourceProvider->markPendingUploadsAsNonBlocking();
[email protected]94f206c12012-08-25 00:09:14829}
830
[email protected]96baf3e2012-10-22 23:09:55831void LayerTreeHostImpl::finishAllRendering()
[email protected]94f206c12012-08-25 00:09:14832{
833 if (m_renderer)
834 m_renderer->finish();
835}
836
[email protected]96baf3e2012-10-22 23:09:55837bool LayerTreeHostImpl::isContextLost()
[email protected]94f206c12012-08-25 00:09:14838{
839 return m_renderer && m_renderer->isContextLost();
840}
841
[email protected]96baf3e2012-10-22 23:09:55842const RendererCapabilities& LayerTreeHostImpl::rendererCapabilities() const
[email protected]94f206c12012-08-25 00:09:14843{
844 return m_renderer->capabilities();
845}
846
[email protected]96baf3e2012-10-22 23:09:55847bool LayerTreeHostImpl::swapBuffers()
[email protected]94f206c12012-08-25 00:09:14848{
[email protected]1d993172012-10-18 18:15:04849 DCHECK(m_renderer);
[email protected]94f206c12012-08-25 00:09:14850
851 m_fpsCounter->markEndOfFrame();
852 return m_renderer->swapBuffers();
853}
854
[email protected]aad0a0072012-11-01 18:15:58855const gfx::Size& LayerTreeHostImpl::deviceViewportSize() const
[email protected]493067512012-09-19 23:34:10856{
857 return m_deviceViewportSize;
858}
859
[email protected]96baf3e2012-10-22 23:09:55860const LayerTreeSettings& LayerTreeHostImpl::settings() const
[email protected]493067512012-09-19 23:34:10861{
862 return m_settings;
863}
864
[email protected]96baf3e2012-10-22 23:09:55865void LayerTreeHostImpl::didLoseContext()
[email protected]94f206c12012-08-25 00:09:14866{
867 m_client->didLoseContextOnImplThread();
868}
869
[email protected]96baf3e2012-10-22 23:09:55870void LayerTreeHostImpl::onSwapBuffersComplete()
[email protected]94f206c12012-08-25 00:09:14871{
872 m_client->onSwapBuffersCompleteOnImplThread();
873}
874
[email protected]aad0a0072012-11-01 18:15:58875void LayerTreeHostImpl::readback(void* pixels, const gfx::Rect& rect)
[email protected]94f206c12012-08-25 00:09:14876{
[email protected]1d993172012-10-18 18:15:04877 DCHECK(m_renderer);
[email protected]94f206c12012-08-25 00:09:14878 m_renderer->getFramebufferPixels(pixels, rect);
879}
880
[email protected]96baf3e2012-10-22 23:09:55881static LayerImpl* findRootScrollLayer(LayerImpl* layer)
[email protected]94f206c12012-08-25 00:09:14882{
883 if (!layer)
884 return 0;
885
886 if (layer->scrollable())
887 return layer;
888
889 for (size_t i = 0; i < layer->children().size(); ++i) {
[email protected]96baf3e2012-10-22 23:09:55890 LayerImpl* found = findRootScrollLayer(layer->children()[i]);
[email protected]94f206c12012-08-25 00:09:14891 if (found)
892 return found;
893 }
894
895 return 0;
896}
897
898// Content layers can be either directly scrollable or contained in an outer
899// scrolling layer which applies the scroll transform. Given a content layer,
900// this function returns the associated scroll layer if any.
[email protected]96baf3e2012-10-22 23:09:55901static LayerImpl* findScrollLayerForContentLayer(LayerImpl* layerImpl)
[email protected]94f206c12012-08-25 00:09:14902{
903 if (!layerImpl)
904 return 0;
905
906 if (layerImpl->scrollable())
907 return layerImpl;
908
909 if (layerImpl->drawsContent() && layerImpl->parent() && layerImpl->parent()->scrollable())
910 return layerImpl->parent();
911
912 return 0;
913}
914
[email protected]96baf3e2012-10-22 23:09:55915void LayerTreeHostImpl::setRootLayer(scoped_ptr<LayerImpl> layer)
[email protected]94f206c12012-08-25 00:09:14916{
[email protected]e0bd43a2012-10-12 16:54:21917 m_rootLayerImpl = layer.Pass();
[email protected]94f206c12012-08-25 00:09:14918 m_rootScrollLayerImpl = findRootScrollLayer(m_rootLayerImpl.get());
919 m_currentlyScrollingLayerImpl = 0;
920
921 if (m_rootLayerImpl && m_scrollingLayerIdFromPreviousTree != -1)
[email protected]96baf3e2012-10-22 23:09:55922 m_currentlyScrollingLayerImpl = LayerTreeHostCommon::findLayerInSubtree(m_rootLayerImpl.get(), m_scrollingLayerIdFromPreviousTree);
[email protected]94f206c12012-08-25 00:09:14923
924 m_scrollingLayerIdFromPreviousTree = -1;
[email protected]8db2213c2012-09-05 22:08:21925
926 m_client->onCanDrawStateChanged(canDraw());
[email protected]94f206c12012-08-25 00:09:14927}
928
[email protected]96baf3e2012-10-22 23:09:55929scoped_ptr<LayerImpl> LayerTreeHostImpl::detachLayerTree()
[email protected]94f206c12012-08-25 00:09:14930{
931 // Clear all data structures that have direct references to the layer tree.
932 m_scrollingLayerIdFromPreviousTree = m_currentlyScrollingLayerImpl ? m_currentlyScrollingLayerImpl->id() : -1;
933 m_currentlyScrollingLayerImpl = 0;
934 m_renderSurfaceLayerList.clear();
935
[email protected]e0bd43a2012-10-12 16:54:21936 return m_rootLayerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:14937}
938
[email protected]96baf3e2012-10-22 23:09:55939void LayerTreeHostImpl::setVisible(bool visible)
[email protected]94f206c12012-08-25 00:09:14940{
[email protected]61de5812012-11-08 07:03:44941 DCHECK(m_proxy->isImplThread());
[email protected]94f206c12012-08-25 00:09:14942
943 if (m_visible == visible)
944 return;
945 m_visible = visible;
946 didVisibilityChange(this, m_visible);
[email protected]a0a00842012-10-22 22:50:28947 enforceManagedMemoryPolicy(m_managedMemoryPolicy);
[email protected]94f206c12012-08-25 00:09:14948
949 if (!m_renderer)
950 return;
951
952 m_renderer->setVisible(visible);
953
954 setBackgroundTickingEnabled(!m_visible && m_needsAnimateLayers);
955}
956
[email protected]96baf3e2012-10-22 23:09:55957bool LayerTreeHostImpl::initializeRenderer(scoped_ptr<GraphicsContext> context)
[email protected]94f206c12012-08-25 00:09:14958{
[email protected]be3181652012-09-25 13:02:13959 // Since we will create a new resource provider, we cannot continue to use
960 // the old resources (i.e. renderSurfaces and texture IDs). Clear them
961 // before we destroy the old resource provider.
[email protected]94f206c12012-08-25 00:09:14962 if (m_rootLayerImpl) {
963 clearRenderSurfaces();
964 sendDidLoseContextRecursive(m_rootLayerImpl.get());
965 }
[email protected]be3181652012-09-25 13:02:13966 // Note: order is important here.
[email protected]0704caf2012-10-16 03:39:47967 m_renderer.reset();
[email protected]a7aa5562012-10-17 14:12:44968 m_resourceProvider.reset();
[email protected]e28efacd2012-10-06 17:07:49969 m_context.reset();
[email protected]94f206c12012-08-25 00:09:14970
[email protected]be3181652012-09-25 13:02:13971 if (!context->bindToClient(this))
972 return false;
973
[email protected]96baf3e2012-10-22 23:09:55974 scoped_ptr<ResourceProvider> resourceProvider = ResourceProvider::create(context.get());
[email protected]be3181652012-09-25 13:02:13975 if (!resourceProvider)
976 return false;
977
978 if (context->context3D())
[email protected]96baf3e2012-10-22 23:09:55979 m_renderer = GLRenderer::create(this, resourceProvider.get());
[email protected]be3181652012-09-25 13:02:13980 else if (context->softwareDevice())
[email protected]96baf3e2012-10-22 23:09:55981 m_renderer = SoftwareRenderer::create(this, resourceProvider.get(), context->softwareDevice());
[email protected]be3181652012-09-25 13:02:13982 if (!m_renderer)
983 return false;
984
[email protected]a7aa5562012-10-17 14:12:44985 m_resourceProvider = resourceProvider.Pass();
[email protected]e28efacd2012-10-06 17:07:49986 m_context = context.Pass();
[email protected]94f206c12012-08-25 00:09:14987
[email protected]be3181652012-09-25 13:02:13988 if (!m_visible)
989 m_renderer->setVisible(m_visible);
[email protected]94f206c12012-08-25 00:09:14990
[email protected]8db2213c2012-09-05 22:08:21991 m_client->onCanDrawStateChanged(canDraw());
992
[email protected]be3181652012-09-25 13:02:13993 return true;
[email protected]94f206c12012-08-25 00:09:14994}
995
[email protected]96baf3e2012-10-22 23:09:55996void LayerTreeHostImpl::setContentsTexturesPurged()
[email protected]e1fc8b32012-09-18 20:29:09997{
998 m_contentsTexturesPurged = true;
999 m_client->onCanDrawStateChanged(canDraw());
1000}
1001
[email protected]96baf3e2012-10-22 23:09:551002void LayerTreeHostImpl::resetContentsTexturesPurged()
[email protected]8db2213c2012-09-05 22:08:211003{
1004 m_contentsTexturesPurged = false;
1005 m_client->onCanDrawStateChanged(canDraw());
1006}
1007
[email protected]aad0a0072012-11-01 18:15:581008void LayerTreeHostImpl::setViewportSize(const gfx::Size& layoutViewportSize, const gfx::Size& deviceViewportSize)
[email protected]94f206c12012-08-25 00:09:141009{
1010 if (layoutViewportSize == m_layoutViewportSize && deviceViewportSize == m_deviceViewportSize)
1011 return;
1012
1013 m_layoutViewportSize = layoutViewportSize;
1014 m_deviceViewportSize = deviceViewportSize;
1015
[email protected]c9c1ebe2012-11-05 20:46:131016 m_pinchZoomViewport.setLayoutViewportSize(layoutViewportSize);
[email protected]1c0c9bc2012-10-08 22:41:481017
[email protected]c9c1ebe2012-11-05 20:46:131018 updateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141019
1020 if (m_renderer)
1021 m_renderer->viewportChanged();
[email protected]8db2213c2012-09-05 22:08:211022
1023 m_client->onCanDrawStateChanged(canDraw());
[email protected]94f206c12012-08-25 00:09:141024}
1025
[email protected]96baf3e2012-10-22 23:09:551026static void adjustScrollsForPageScaleChange(LayerImpl* layerImpl, float pageScaleChange)
[email protected]94f206c12012-08-25 00:09:141027{
1028 if (!layerImpl)
1029 return;
1030
1031 if (layerImpl->scrollable()) {
1032 // We need to convert impl-side scroll deltas to pageScale space.
[email protected]c9c1ebe2012-11-05 20:46:131033 gfx::Vector2dF scrollDelta = layerImpl->scrollDelta();
1034 scrollDelta.Scale(pageScaleChange);
[email protected]94f206c12012-08-25 00:09:141035 layerImpl->setScrollDelta(scrollDelta);
1036 }
1037
1038 for (size_t i = 0; i < layerImpl->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031039 adjustScrollsForPageScaleChange(layerImpl->children()[i], pageScaleChange);
[email protected]94f206c12012-08-25 00:09:141040}
1041
[email protected]96baf3e2012-10-22 23:09:551042void LayerTreeHostImpl::setDeviceScaleFactor(float deviceScaleFactor)
[email protected]94f206c12012-08-25 00:09:141043{
1044 if (deviceScaleFactor == m_deviceScaleFactor)
1045 return;
1046 m_deviceScaleFactor = deviceScaleFactor;
[email protected]e39bf212012-11-22 21:04:031047 m_pinchZoomViewport.setDeviceScaleFactor(m_deviceScaleFactor);
[email protected]c0dd24c2012-08-30 23:25:271048
[email protected]c9c1ebe2012-11-05 20:46:131049 updateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141050}
1051
[email protected]96baf3e2012-10-22 23:09:551052float LayerTreeHostImpl::pageScaleFactor() const
[email protected]94f206c12012-08-25 00:09:141053{
[email protected]1c0c9bc2012-10-08 22:41:481054 return m_pinchZoomViewport.pageScaleFactor();
1055}
[email protected]94f206c12012-08-25 00:09:141056
[email protected]96baf3e2012-10-22 23:09:551057void LayerTreeHostImpl::setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor)
[email protected]1c0c9bc2012-10-08 22:41:481058{
1059 if (!pageScaleFactor)
1060 return;
[email protected]94f206c12012-08-25 00:09:141061
[email protected]1c0c9bc2012-10-08 22:41:481062 float pageScaleChange = pageScaleFactor / m_pinchZoomViewport.pageScaleFactor();
1063 m_pinchZoomViewport.setPageScaleFactorAndLimits(pageScaleFactor, minPageScaleFactor, maxPageScaleFactor);
[email protected]94f206c12012-08-25 00:09:141064
[email protected]9bdcfd642012-11-14 21:24:261065 if (!m_settings.pageScalePinchZoomEnabled) {
[email protected]1c0c9bc2012-10-08 22:41:481066 if (pageScaleChange != 1)
1067 adjustScrollsForPageScaleChange(m_rootScrollLayerImpl, pageScaleChange);
1068 }
[email protected]94f206c12012-08-25 00:09:141069
1070 // Clamp delta to limits and refresh display matrix.
[email protected]1c0c9bc2012-10-08 22:41:481071 setPageScaleDelta(m_pinchZoomViewport.pageScaleDelta() / m_pinchZoomViewport.sentPageScaleDelta());
1072 m_pinchZoomViewport.setSentPageScaleDelta(1);
[email protected]94f206c12012-08-25 00:09:141073}
1074
[email protected]96baf3e2012-10-22 23:09:551075void LayerTreeHostImpl::setPageScaleDelta(float delta)
[email protected]94f206c12012-08-25 00:09:141076{
[email protected]1c0c9bc2012-10-08 22:41:481077 m_pinchZoomViewport.setPageScaleDelta(delta);
[email protected]94f206c12012-08-25 00:09:141078
[email protected]c9c1ebe2012-11-05 20:46:131079 updateMaxScrollOffset();
[email protected]94f206c12012-08-25 00:09:141080}
1081
[email protected]c9c1ebe2012-11-05 20:46:131082void LayerTreeHostImpl::updateMaxScrollOffset()
[email protected]94f206c12012-08-25 00:09:141083{
1084 if (!m_rootScrollLayerImpl || !m_rootScrollLayerImpl->children().size())
1085 return;
1086
[email protected]aad0a0072012-11-01 18:15:581087 gfx::SizeF viewBounds = m_deviceViewportSize;
[email protected]96baf3e2012-10-22 23:09:551088 if (LayerImpl* clipLayer = m_rootScrollLayerImpl->parent()) {
[email protected]94f206c12012-08-25 00:09:141089 // Compensate for non-overlay scrollbars.
[email protected]01a15a72012-11-10 09:34:281090 if (clipLayer->masksToBounds())
1091 viewBounds = gfx::ScaleSize(clipLayer->bounds(), m_deviceScaleFactor);
[email protected]94f206c12012-08-25 00:09:141092 }
[email protected]94f206c12012-08-25 00:09:141093
[email protected]aad0a0072012-11-01 18:15:581094 gfx::Size contentBounds = contentSize();
[email protected]9bdcfd642012-11-14 21:24:261095 if (m_settings.pageScalePinchZoomEnabled) {
[email protected]1c0c9bc2012-10-08 22:41:481096 // Pinch with pageScale scrolls entirely in layout space. contentSize
1097 // returns the bounds including the page scale factor, so calculate the
1098 // pre page-scale layout size here.
1099 float pageScaleFactor = m_pinchZoomViewport.pageScaleFactor();
[email protected]aad0a0072012-11-01 18:15:581100 contentBounds.set_width(contentBounds.width() / pageScaleFactor);
1101 contentBounds.set_height(contentBounds.height() / pageScaleFactor);
[email protected]1c0c9bc2012-10-08 22:41:481102 } else {
[email protected]01a15a72012-11-10 09:34:281103 viewBounds.Scale(1 / m_pinchZoomViewport.pageScaleDelta());
[email protected]1c0c9bc2012-10-08 22:41:481104 }
1105
[email protected]0eef4b882012-11-20 20:28:071106 gfx::Vector2dF maxScroll = gfx::Rect(contentBounds).bottom_right() - gfx::RectF(viewBounds).bottom_right();
[email protected]c9c1ebe2012-11-05 20:46:131107 maxScroll.Scale(1 / m_deviceScaleFactor);
[email protected]1c0c9bc2012-10-08 22:41:481108
[email protected]94f206c12012-08-25 00:09:141109 // The viewport may be larger than the contents in some cases, such as
1110 // having a vertical scrollbar but no horizontal overflow.
[email protected]fe07b642012-11-10 00:07:591111 maxScroll.ClampToMin(gfx::Vector2dF());
[email protected]94f206c12012-08-25 00:09:141112
[email protected]c9c1ebe2012-11-05 20:46:131113 m_rootScrollLayerImpl->setMaxScrollOffset(gfx::ToFlooredVector2d(maxScroll));
[email protected]94f206c12012-08-25 00:09:141114}
1115
[email protected]96baf3e2012-10-22 23:09:551116void LayerTreeHostImpl::setNeedsRedraw()
[email protected]94f206c12012-08-25 00:09:141117{
1118 m_client->setNeedsRedrawOnImplThread();
1119}
1120
[email protected]96baf3e2012-10-22 23:09:551121bool LayerTreeHostImpl::ensureRenderSurfaceLayerList()
[email protected]94f206c12012-08-25 00:09:141122{
1123 if (!m_rootLayerImpl)
1124 return false;
1125 if (!m_renderer)
1126 return false;
1127
1128 // We need both a non-empty render surface layer list and a root render
1129 // surface to be able to iterate over the visible layers.
1130 if (m_renderSurfaceLayerList.size() && m_rootLayerImpl->renderSurface())
1131 return true;
1132
1133 // If we are called after setRootLayer() but before prepareToDraw(), we need
1134 // to recalculate the visible layers. This prevents being unable to scroll
1135 // during part of a commit.
1136 m_renderSurfaceLayerList.clear();
1137 calculateRenderSurfaceLayerList(m_renderSurfaceLayerList);
1138
1139 return m_renderSurfaceLayerList.size();
1140}
1141
[email protected]c9c1ebe2012-11-05 20:46:131142InputHandlerClient::ScrollStatus LayerTreeHostImpl::scrollBegin(gfx::Point viewportPoint, InputHandlerClient::ScrollInputType type)
[email protected]94f206c12012-08-25 00:09:141143{
[email protected]96baf3e2012-10-22 23:09:551144 TRACE_EVENT0("cc", "LayerTreeHostImpl::scrollBegin");
[email protected]94f206c12012-08-25 00:09:141145
[email protected]1d993172012-10-18 18:15:041146 DCHECK(!m_currentlyScrollingLayerImpl);
[email protected]94f206c12012-08-25 00:09:141147 clearCurrentlyScrollingLayer();
1148
1149 if (!ensureRenderSurfaceLayerList())
1150 return ScrollIgnored;
1151
[email protected]faf56352012-11-09 21:44:131152 gfx::PointF deviceViewportPoint = gfx::ScalePoint(viewportPoint, m_deviceScaleFactor);
[email protected]94f206c12012-08-25 00:09:141153
1154 // First find out which layer was hit from the saved list of visible layers
1155 // in the most recent frame.
[email protected]96baf3e2012-10-22 23:09:551156 LayerImpl* layerImpl = LayerTreeHostCommon::findLayerThatIsHitByPoint(deviceViewportPoint, m_renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:141157
1158 // Walk up the hierarchy and look for a scrollable layer.
[email protected]96baf3e2012-10-22 23:09:551159 LayerImpl* potentiallyScrollingLayerImpl = 0;
[email protected]94f206c12012-08-25 00:09:141160 for (; layerImpl; layerImpl = layerImpl->parent()) {
1161 // The content layer can also block attempts to scroll outside the main thread.
[email protected]5c6fe1f82012-10-03 18:00:271162 if (layerImpl->tryScroll(deviceViewportPoint, type) == ScrollOnMainThread) {
1163 m_numMainThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141164 return ScrollOnMainThread;
[email protected]5c6fe1f82012-10-03 18:00:271165 }
[email protected]94f206c12012-08-25 00:09:141166
[email protected]96baf3e2012-10-22 23:09:551167 LayerImpl* scrollLayerImpl = findScrollLayerForContentLayer(layerImpl);
[email protected]94f206c12012-08-25 00:09:141168 if (!scrollLayerImpl)
1169 continue;
1170
[email protected]31bfe272012-10-19 18:49:521171 ScrollStatus status = scrollLayerImpl->tryScroll(deviceViewportPoint, type);
[email protected]94f206c12012-08-25 00:09:141172
1173 // If any layer wants to divert the scroll event to the main thread, abort.
[email protected]5c6fe1f82012-10-03 18:00:271174 if (status == ScrollOnMainThread) {
1175 m_numMainThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141176 return ScrollOnMainThread;
[email protected]5c6fe1f82012-10-03 18:00:271177 }
[email protected]94f206c12012-08-25 00:09:141178
1179 if (status == ScrollStarted && !potentiallyScrollingLayerImpl)
1180 potentiallyScrollingLayerImpl = scrollLayerImpl;
1181 }
1182
1183 if (potentiallyScrollingLayerImpl) {
1184 m_currentlyScrollingLayerImpl = potentiallyScrollingLayerImpl;
[email protected]31bfe272012-10-19 18:49:521185 // Gesture events need to be transformed from viewport coordinates to local layer coordinates
[email protected]94f206c12012-08-25 00:09:141186 // so that the scrolling contents exactly follow the user's finger. In contrast, wheel
1187 // events are already in local layer coordinates so we can just apply them directly.
[email protected]31bfe272012-10-19 18:49:521188 m_scrollDeltaIsInViewportSpace = (type == Gesture);
[email protected]5c6fe1f82012-10-03 18:00:271189 m_numImplThreadScrolls++;
[email protected]94f206c12012-08-25 00:09:141190 return ScrollStarted;
1191 }
1192 return ScrollIgnored;
1193}
1194
[email protected]c9c1ebe2012-11-05 20:46:131195static gfx::Vector2dF scrollLayerWithViewportSpaceDelta(PinchZoomViewport* viewport, LayerImpl& layerImpl, float scaleFromViewportToScreenSpace, gfx::PointF viewportPoint, gfx::Vector2dF viewportDelta)
[email protected]94f206c12012-08-25 00:09:141196{
1197 // Layers with non-invertible screen space transforms should not have passed the scroll hit
1198 // test in the first place.
[email protected]c8686a02012-11-27 08:29:001199 DCHECK(layerImpl.screenSpaceTransform().IsInvertible());
1200 gfx::Transform inverseScreenSpaceTransform = MathUtil::inverse(layerImpl.screenSpaceTransform());
[email protected]94f206c12012-08-25 00:09:141201
[email protected]faf56352012-11-09 21:44:131202 gfx::PointF screenSpacePoint = gfx::ScalePoint(viewportPoint, scaleFromViewportToScreenSpace);
[email protected]31bfe272012-10-19 18:49:521203
[email protected]c9c1ebe2012-11-05 20:46:131204 gfx::Vector2dF screenSpaceDelta = viewportDelta;
1205 screenSpaceDelta.Scale(scaleFromViewportToScreenSpace);
[email protected]31bfe272012-10-19 18:49:521206
[email protected]94f206c12012-08-25 00:09:141207 // First project the scroll start and end points to local layer space to find the scroll delta
1208 // in layer coordinates.
1209 bool startClipped, endClipped;
[email protected]d455d552012-11-02 00:19:061210 gfx::PointF screenSpaceEndPoint = screenSpacePoint + screenSpaceDelta;
1211 gfx::PointF localStartPoint = MathUtil::projectPoint(inverseScreenSpaceTransform, screenSpacePoint, startClipped);
1212 gfx::PointF localEndPoint = MathUtil::projectPoint(inverseScreenSpaceTransform, screenSpaceEndPoint, endClipped);
[email protected]94f206c12012-08-25 00:09:141213
1214 // In general scroll point coordinates should not get clipped.
[email protected]1d993172012-10-18 18:15:041215 DCHECK(!startClipped);
1216 DCHECK(!endClipped);
[email protected]94f206c12012-08-25 00:09:141217 if (startClipped || endClipped)
[email protected]c9c1ebe2012-11-05 20:46:131218 return gfx::Vector2dF();
[email protected]94f206c12012-08-25 00:09:141219
[email protected]31bfe272012-10-19 18:49:521220 // 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:581221 float widthScale = 1 / layerImpl.contentsScaleX();
1222 float heightScale = 1 / layerImpl.contentsScaleY();
[email protected]faf56352012-11-09 21:44:131223 localStartPoint.Scale(widthScale, heightScale);
1224 localEndPoint.Scale(widthScale, heightScale);
[email protected]31bfe272012-10-19 18:49:521225
[email protected]94f206c12012-08-25 00:09:141226 // Apply the scroll delta.
[email protected]c9c1ebe2012-11-05 20:46:131227 gfx::Vector2dF previousDelta = layerImpl.scrollDelta();
1228 gfx::Vector2dF unscrolled = layerImpl.scrollBy(localEndPoint - localStartPoint);
[email protected]1c0c9bc2012-10-08 22:41:481229
[email protected]aeaa50a2012-11-21 20:12:371230 gfx::Vector2dF viewportAppliedPan;
[email protected]1c0c9bc2012-10-08 22:41:481231 if (viewport)
[email protected]aeaa50a2012-11-21 20:12:371232 viewportAppliedPan = unscrolled - viewport->applyScroll(unscrolled);
[email protected]94f206c12012-08-25 00:09:141233
[email protected]31bfe272012-10-19 18:49:521234 // Get the end point in the layer's content space so we can apply its screenSpaceTransform.
[email protected]aeaa50a2012-11-21 20:12:371235 gfx::PointF actualLocalEndPoint = localStartPoint + layerImpl.scrollDelta() + viewportAppliedPan - previousDelta;
[email protected]faf56352012-11-09 21:44:131236 gfx::PointF actualLocalContentEndPoint = gfx::ScalePoint(actualLocalEndPoint, 1 / widthScale, 1 / heightScale);
[email protected]31bfe272012-10-19 18:49:521237
1238 // Calculate the applied scroll delta in viewport space coordinates.
[email protected]d455d552012-11-02 00:19:061239 gfx::PointF actualScreenSpaceEndPoint = MathUtil::mapPoint(layerImpl.screenSpaceTransform(), actualLocalContentEndPoint, endClipped);
[email protected]1d993172012-10-18 18:15:041240 DCHECK(!endClipped);
[email protected]94f206c12012-08-25 00:09:141241 if (endClipped)
[email protected]c9c1ebe2012-11-05 20:46:131242 return gfx::Vector2dF();
[email protected]faf56352012-11-09 21:44:131243 gfx::PointF actualViewportEndPoint = gfx::ScalePoint(actualScreenSpaceEndPoint, 1 / scaleFromViewportToScreenSpace);
[email protected]c9c1ebe2012-11-05 20:46:131244 return actualViewportEndPoint - viewportPoint;
[email protected]94f206c12012-08-25 00:09:141245}
1246
[email protected]c9c1ebe2012-11-05 20:46:131247static gfx::Vector2dF scrollLayerWithLocalDelta(LayerImpl& layerImpl, gfx::Vector2dF localDelta)
[email protected]94f206c12012-08-25 00:09:141248{
[email protected]c9c1ebe2012-11-05 20:46:131249 gfx::Vector2dF previousDelta(layerImpl.scrollDelta());
[email protected]94f206c12012-08-25 00:09:141250 layerImpl.scrollBy(localDelta);
1251 return layerImpl.scrollDelta() - previousDelta;
1252}
1253
[email protected]a9710962012-11-14 20:11:021254bool LayerTreeHostImpl::scrollBy(const gfx::Point& viewportPoint,
1255 const gfx::Vector2d& scrollDelta)
[email protected]94f206c12012-08-25 00:09:141256{
[email protected]96baf3e2012-10-22 23:09:551257 TRACE_EVENT0("cc", "LayerTreeHostImpl::scrollBy");
[email protected]94f206c12012-08-25 00:09:141258 if (!m_currentlyScrollingLayerImpl)
[email protected]a9710962012-11-14 20:11:021259 return false;
[email protected]94f206c12012-08-25 00:09:141260
[email protected]c9c1ebe2012-11-05 20:46:131261 gfx::Vector2dF pendingDelta = scrollDelta;
[email protected]94f206c12012-08-25 00:09:141262
[email protected]96baf3e2012-10-22 23:09:551263 for (LayerImpl* layerImpl = m_currentlyScrollingLayerImpl; layerImpl; layerImpl = layerImpl->parent()) {
[email protected]94f206c12012-08-25 00:09:141264 if (!layerImpl->scrollable())
1265 continue;
1266
[email protected]96baf3e2012-10-22 23:09:551267 PinchZoomViewport* viewport = layerImpl == m_rootScrollLayerImpl ? &m_pinchZoomViewport : 0;
[email protected]c9c1ebe2012-11-05 20:46:131268 gfx::Vector2dF appliedDelta;
[email protected]31bfe272012-10-19 18:49:521269 if (m_scrollDeltaIsInViewportSpace) {
1270 float scaleFromViewportToScreenSpace = m_deviceScaleFactor;
1271 appliedDelta = scrollLayerWithViewportSpaceDelta(viewport, *layerImpl, scaleFromViewportToScreenSpace, viewportPoint, pendingDelta);
1272 } else
[email protected]94f206c12012-08-25 00:09:141273 appliedDelta = scrollLayerWithLocalDelta(*layerImpl, pendingDelta);
1274
1275 // If the layer wasn't able to move, try the next one in the hierarchy.
[email protected]23bbb412012-08-30 20:03:381276 float moveThresholdSquared = 0.1f * 0.1f;
[email protected]c9c1ebe2012-11-05 20:46:131277 if (appliedDelta.LengthSquared() < moveThresholdSquared)
[email protected]94f206c12012-08-25 00:09:141278 continue;
1279
1280 // If the applied delta is within 45 degrees of the input delta, bail out to make it easier
1281 // to scroll just one layer in one direction without affecting any of its parents.
1282 float angleThreshold = 45;
[email protected]96baf3e2012-10-22 23:09:551283 if (MathUtil::smallestAngleBetweenVectors(appliedDelta, pendingDelta) < angleThreshold) {
[email protected]c9c1ebe2012-11-05 20:46:131284 pendingDelta = gfx::Vector2d();
[email protected]94f206c12012-08-25 00:09:141285 break;
1286 }
1287
1288 // Allow further movement only on an axis perpendicular to the direction in which the layer
1289 // moved.
[email protected]c9c1ebe2012-11-05 20:46:131290 gfx::Vector2dF perpendicularAxis(-appliedDelta.y(), appliedDelta.x());
[email protected]96baf3e2012-10-22 23:09:551291 pendingDelta = MathUtil::projectVector(pendingDelta, perpendicularAxis);
[email protected]94f206c12012-08-25 00:09:141292
[email protected]c9c1ebe2012-11-05 20:46:131293 if (gfx::ToFlooredVector2d(pendingDelta).IsZero())
[email protected]94f206c12012-08-25 00:09:141294 break;
1295 }
1296
[email protected]c9c1ebe2012-11-05 20:46:131297 if (!scrollDelta.IsZero() && gfx::ToFlooredVector2d(pendingDelta).IsZero()) {
[email protected]94f206c12012-08-25 00:09:141298 m_client->setNeedsCommitOnImplThread();
1299 m_client->setNeedsRedrawOnImplThread();
[email protected]a9710962012-11-14 20:11:021300 return true;
[email protected]94f206c12012-08-25 00:09:141301 }
[email protected]a9710962012-11-14 20:11:021302 return false;
[email protected]94f206c12012-08-25 00:09:141303}
1304
[email protected]96baf3e2012-10-22 23:09:551305void LayerTreeHostImpl::clearCurrentlyScrollingLayer()
[email protected]94f206c12012-08-25 00:09:141306{
1307 m_currentlyScrollingLayerImpl = 0;
1308 m_scrollingLayerIdFromPreviousTree = -1;
1309}
1310
[email protected]96baf3e2012-10-22 23:09:551311void LayerTreeHostImpl::scrollEnd()
[email protected]94f206c12012-08-25 00:09:141312{
1313 clearCurrentlyScrollingLayer();
1314}
1315
[email protected]96baf3e2012-10-22 23:09:551316void LayerTreeHostImpl::pinchGestureBegin()
[email protected]94f206c12012-08-25 00:09:141317{
1318 m_pinchGestureActive = true;
[email protected]c9c1ebe2012-11-05 20:46:131319 m_previousPinchAnchor = gfx::Point();
[email protected]94f206c12012-08-25 00:09:141320
1321 if (m_rootScrollLayerImpl && m_rootScrollLayerImpl->scrollbarAnimationController())
1322 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureBegin();
1323}
1324
[email protected]c9c1ebe2012-11-05 20:46:131325void LayerTreeHostImpl::pinchGestureUpdate(float magnifyDelta, gfx::Point anchor)
[email protected]94f206c12012-08-25 00:09:141326{
[email protected]96baf3e2012-10-22 23:09:551327 TRACE_EVENT0("cc", "LayerTreeHostImpl::pinchGestureUpdate");
[email protected]94f206c12012-08-25 00:09:141328
1329 if (!m_rootScrollLayerImpl)
1330 return;
1331
[email protected]94f206c12012-08-25 00:09:141332 // Keep the center-of-pinch anchor specified by (x, y) in a stable
1333 // position over the course of the magnify.
[email protected]1c0c9bc2012-10-08 22:41:481334 float pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
[email protected]c77745d2012-11-20 04:11:571335 gfx::PointF previousScaleAnchor = gfx::ScalePoint(anchor, 1 / pageScaleDelta);
[email protected]1c0c9bc2012-10-08 22:41:481336 setPageScaleDelta(pageScaleDelta * magnifyDelta);
1337 pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
[email protected]faf56352012-11-09 21:44:131338 gfx::PointF newScaleAnchor = gfx::ScalePoint(anchor, 1 / pageScaleDelta);
[email protected]c9c1ebe2012-11-05 20:46:131339 gfx::Vector2dF move = previousScaleAnchor - newScaleAnchor;
[email protected]94f206c12012-08-25 00:09:141340
1341 m_previousPinchAnchor = anchor;
1342
[email protected]9bdcfd642012-11-14 21:24:261343 if (m_settings.pageScalePinchZoomEnabled) {
[email protected]1c0c9bc2012-10-08 22:41:481344 // Compute the application of the delta with respect to the current page zoom of the page.
[email protected]e39bf212012-11-22 21:04:031345 move.Scale(1 / m_pinchZoomViewport.pageScaleFactor());
[email protected]1c0c9bc2012-10-08 22:41:481346 }
1347
[email protected]9bdcfd642012-11-14 21:24:261348 gfx::Vector2dF scrollOverflow = m_settings.pageScalePinchZoomEnabled ? m_pinchZoomViewport.applyScroll(move) : move;
[email protected]c9c1ebe2012-11-05 20:46:131349 m_rootScrollLayerImpl->scrollBy(scrollOverflow);
[email protected]94f206c12012-08-25 00:09:141350
1351 if (m_rootScrollLayerImpl->scrollbarAnimationController())
1352 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureUpdate();
1353
1354 m_client->setNeedsCommitOnImplThread();
1355 m_client->setNeedsRedrawOnImplThread();
1356}
1357
[email protected]96baf3e2012-10-22 23:09:551358void LayerTreeHostImpl::pinchGestureEnd()
[email protected]94f206c12012-08-25 00:09:141359{
1360 m_pinchGestureActive = false;
1361
1362 if (m_rootScrollLayerImpl && m_rootScrollLayerImpl->scrollbarAnimationController())
1363 m_rootScrollLayerImpl->scrollbarAnimationController()->didPinchGestureEnd();
1364
1365 m_client->setNeedsCommitOnImplThread();
1366}
1367
[email protected]96baf3e2012-10-22 23:09:551368void LayerTreeHostImpl::computeDoubleTapZoomDeltas(ScrollAndScaleSet* scrollInfo)
[email protected]94f206c12012-08-25 00:09:141369{
[email protected]f6250742012-11-09 04:46:561370 gfx::Vector2dF scaledScrollOffset = m_pageScaleAnimation->targetScrollOffset();
[email protected]9bdcfd642012-11-14 21:24:261371 if (!m_settings.pageScalePinchZoomEnabled)
[email protected]f6250742012-11-09 04:46:561372 scaledScrollOffset.Scale(m_pinchZoomViewport.pageScaleFactor());
1373 makeScrollAndScaleSet(scrollInfo, ToFlooredVector2d(scaledScrollOffset), m_pageScaleAnimation->targetPageScaleFactor());
[email protected]94f206c12012-08-25 00:09:141374}
1375
[email protected]96baf3e2012-10-22 23:09:551376void LayerTreeHostImpl::computePinchZoomDeltas(ScrollAndScaleSet* scrollInfo)
[email protected]94f206c12012-08-25 00:09:141377{
1378 if (!m_rootScrollLayerImpl)
1379 return;
1380
1381 // Only send fake scroll/zoom deltas if we're pinch zooming out by a
1382 // significant amount. This also ensures only one fake delta set will be
1383 // sent.
[email protected]23bbb412012-08-30 20:03:381384 const float pinchZoomOutSensitivity = 0.95f;
[email protected]1c0c9bc2012-10-08 22:41:481385 if (m_pinchZoomViewport.pageScaleDelta() > pinchZoomOutSensitivity)
[email protected]94f206c12012-08-25 00:09:141386 return;
1387
1388 // Compute where the scroll offset/page scale would be if fully pinch-zoomed
1389 // out from the anchor point.
[email protected]c9c1ebe2012-11-05 20:46:131390 gfx::Vector2dF scrollBegin = m_rootScrollLayerImpl->scrollOffset() + m_rootScrollLayerImpl->scrollDelta();
1391 scrollBegin.Scale(m_pinchZoomViewport.pageScaleDelta());
[email protected]1c0c9bc2012-10-08 22:41:481392 float scaleBegin = m_pinchZoomViewport.totalPageScaleFactor();
1393 float pageScaleDeltaToSend = m_pinchZoomViewport.minPageScaleFactor() / m_pinchZoomViewport.pageScaleFactor();
[email protected]01a15a72012-11-10 09:34:281394 gfx::SizeF scaledContentsSize = gfx::ScaleSize(contentSize(), pageScaleDeltaToSend);
[email protected]94f206c12012-08-25 00:09:141395
[email protected]c9c1ebe2012-11-05 20:46:131396 gfx::Vector2d anchorOffset = m_previousPinchAnchor.OffsetFromOrigin();
1397 gfx::Vector2dF scrollEnd = scrollBegin + anchorOffset;
1398 scrollEnd.Scale(m_pinchZoomViewport.minPageScaleFactor() / scaleBegin);
1399 scrollEnd -= anchorOffset;
[email protected]0eef4b882012-11-20 20:28:071400 scrollEnd.ClampToMax(gfx::RectF(scaledContentsSize).bottom_right() - gfx::Rect(m_deviceViewportSize).bottom_right());
[email protected]fe07b642012-11-10 00:07:591401 scrollEnd.ClampToMin(gfx::Vector2d());
[email protected]c9c1ebe2012-11-05 20:46:131402 scrollEnd.Scale(1 / pageScaleDeltaToSend);
1403 scrollEnd.Scale(m_deviceScaleFactor);
[email protected]94f206c12012-08-25 00:09:141404
[email protected]c9c1ebe2012-11-05 20:46:131405 makeScrollAndScaleSet(scrollInfo, gfx::ToRoundedVector2d(scrollEnd), m_pinchZoomViewport.minPageScaleFactor());
[email protected]94f206c12012-08-25 00:09:141406}
1407
[email protected]c9c1ebe2012-11-05 20:46:131408void LayerTreeHostImpl::makeScrollAndScaleSet(ScrollAndScaleSet* scrollInfo, gfx::Vector2d scrollOffset, float pageScale)
[email protected]94f206c12012-08-25 00:09:141409{
1410 if (!m_rootScrollLayerImpl)
1411 return;
1412
[email protected]96baf3e2012-10-22 23:09:551413 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]94f206c12012-08-25 00:09:141414 scroll.layerId = m_rootScrollLayerImpl->id();
[email protected]c9c1ebe2012-11-05 20:46:131415 scroll.scrollDelta = scrollOffset - m_rootScrollLayerImpl->scrollOffset();
[email protected]787465c2012-10-29 01:12:271416 scrollInfo->scrolls.push_back(scroll);
[email protected]94f206c12012-08-25 00:09:141417 m_rootScrollLayerImpl->setSentScrollDelta(scroll.scrollDelta);
[email protected]1c0c9bc2012-10-08 22:41:481418 scrollInfo->pageScaleDelta = pageScale / m_pinchZoomViewport.pageScaleFactor();
1419 m_pinchZoomViewport.setSentPageScaleDelta(scrollInfo->pageScaleDelta);
[email protected]94f206c12012-08-25 00:09:141420}
1421
[email protected]96baf3e2012-10-22 23:09:551422static void collectScrollDeltas(ScrollAndScaleSet* scrollInfo, LayerImpl* layerImpl)
[email protected]94f206c12012-08-25 00:09:141423{
1424 if (!layerImpl)
1425 return;
1426
[email protected]c9c1ebe2012-11-05 20:46:131427 if (!layerImpl->scrollDelta().IsZero()) {
1428 gfx::Vector2d scrollDelta = gfx::ToFlooredVector2d(layerImpl->scrollDelta());
[email protected]96baf3e2012-10-22 23:09:551429 LayerTreeHostCommon::ScrollUpdateInfo scroll;
[email protected]94f206c12012-08-25 00:09:141430 scroll.layerId = layerImpl->id();
1431 scroll.scrollDelta = scrollDelta;
[email protected]787465c2012-10-29 01:12:271432 scrollInfo->scrolls.push_back(scroll);
[email protected]94f206c12012-08-25 00:09:141433 layerImpl->setSentScrollDelta(scrollDelta);
1434 }
1435
1436 for (size_t i = 0; i < layerImpl->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031437 collectScrollDeltas(scrollInfo, layerImpl->children()[i]);
[email protected]94f206c12012-08-25 00:09:141438}
1439
[email protected]96baf3e2012-10-22 23:09:551440scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::processScrollDeltas()
[email protected]94f206c12012-08-25 00:09:141441{
[email protected]96baf3e2012-10-22 23:09:551442 scoped_ptr<ScrollAndScaleSet> scrollInfo(new ScrollAndScaleSet());
[email protected]94f206c12012-08-25 00:09:141443
1444 if (m_pinchGestureActive || m_pageScaleAnimation) {
[email protected]1c0c9bc2012-10-08 22:41:481445 scrollInfo->pageScaleDelta = 1;
1446 m_pinchZoomViewport.setSentPageScaleDelta(1);
[email protected]f6250742012-11-09 04:46:561447 // FIXME(aelias): Make pinch-zoom painting optimization compatible with
[email protected]1c0c9bc2012-10-08 22:41:481448 // compositor-side scaling.
[email protected]9bdcfd642012-11-14 21:24:261449 if (!m_settings.pageScalePinchZoomEnabled && m_pinchGestureActive)
[email protected]f6250742012-11-09 04:46:561450 computePinchZoomDeltas(scrollInfo.get());
1451 else if (m_pageScaleAnimation.get())
1452 computeDoubleTapZoomDeltas(scrollInfo.get());
[email protected]a9f4bf22012-10-11 23:39:211453 return scrollInfo.Pass();
[email protected]94f206c12012-08-25 00:09:141454 }
1455
1456 collectScrollDeltas(scrollInfo.get(), m_rootLayerImpl.get());
[email protected]1c0c9bc2012-10-08 22:41:481457 scrollInfo->pageScaleDelta = m_pinchZoomViewport.pageScaleDelta();
1458 m_pinchZoomViewport.setSentPageScaleDelta(scrollInfo->pageScaleDelta);
[email protected]94f206c12012-08-25 00:09:141459
[email protected]a9f4bf22012-10-11 23:39:211460 return scrollInfo.Pass();
[email protected]94f206c12012-08-25 00:09:141461}
1462
[email protected]c8686a02012-11-27 08:29:001463gfx::Transform LayerTreeHostImpl::implTransform() const
[email protected]1c0c9bc2012-10-08 22:41:481464{
[email protected]9bdcfd642012-11-14 21:24:261465 return m_pinchZoomViewport.implTransform(m_settings.pageScalePinchZoomEnabled);
[email protected]1c0c9bc2012-10-08 22:41:481466}
1467
[email protected]96baf3e2012-10-22 23:09:551468void LayerTreeHostImpl::setFullRootLayerDamage()
[email protected]94f206c12012-08-25 00:09:141469{
1470 if (m_rootLayerImpl) {
[email protected]96baf3e2012-10-22 23:09:551471 RenderSurfaceImpl* renderSurface = m_rootLayerImpl->renderSurface();
[email protected]94f206c12012-08-25 00:09:141472 if (renderSurface)
1473 renderSurface->damageTracker()->forceFullDamageNextUpdate();
1474 }
1475}
1476
[email protected]30faac92012-10-29 00:06:291477void LayerTreeHostImpl::animatePageScale(base::TimeTicks time)
[email protected]94f206c12012-08-25 00:09:141478{
1479 if (!m_pageScaleAnimation || !m_rootScrollLayerImpl)
1480 return;
1481
[email protected]30faac92012-10-29 00:06:291482 double monotonicTime = (time - base::TimeTicks()).InSecondsF();
[email protected]c9c1ebe2012-11-05 20:46:131483 gfx::Vector2dF scrollTotal = m_rootScrollLayerImpl->scrollOffset() + m_rootScrollLayerImpl->scrollDelta();
[email protected]94f206c12012-08-25 00:09:141484
[email protected]f6250742012-11-09 04:46:561485 setPageScaleDelta(m_pageScaleAnimation->pageScaleFactorAtTime(monotonicTime) / m_pinchZoomViewport.pageScaleFactor());
[email protected]c9c1ebe2012-11-05 20:46:131486 gfx::Vector2dF nextScroll = m_pageScaleAnimation->scrollOffsetAtTime(monotonicTime);
[email protected]f6250742012-11-09 04:46:561487
[email protected]9bdcfd642012-11-14 21:24:261488 if (!m_settings.pageScalePinchZoomEnabled)
[email protected]f6250742012-11-09 04:46:561489 nextScroll.Scale(m_pinchZoomViewport.pageScaleFactor());
[email protected]94f206c12012-08-25 00:09:141490 m_rootScrollLayerImpl->scrollBy(nextScroll - scrollTotal);
1491 m_client->setNeedsRedrawOnImplThread();
1492
1493 if (m_pageScaleAnimation->isAnimationCompleteAtTime(monotonicTime)) {
[email protected]0023e8b2012-10-15 12:52:451494 m_pageScaleAnimation.reset();
[email protected]94f206c12012-08-25 00:09:141495 m_client->setNeedsCommitOnImplThread();
1496 }
1497}
1498
[email protected]30faac92012-10-29 00:06:291499void LayerTreeHostImpl::animateLayers(base::TimeTicks monotonicTime, base::Time wallClockTime)
[email protected]94f206c12012-08-25 00:09:141500{
[email protected]9bdcfd642012-11-14 21:24:261501 if (!m_settings.acceleratedAnimationEnabled || !m_needsAnimateLayers || !m_rootLayerImpl)
[email protected]94f206c12012-08-25 00:09:141502 return;
1503
[email protected]96baf3e2012-10-22 23:09:551504 TRACE_EVENT0("cc", "LayerTreeHostImpl::animateLayers");
[email protected]94f206c12012-08-25 00:09:141505
[email protected]96baf3e2012-10-22 23:09:551506 scoped_ptr<AnimationEventsVector> events(make_scoped_ptr(new AnimationEventsVector));
[email protected]94f206c12012-08-25 00:09:141507
1508 bool didAnimate = false;
1509 animateLayersRecursive(m_rootLayerImpl.get(), monotonicTime, wallClockTime, events.get(), didAnimate, m_needsAnimateLayers);
1510
[email protected]d3143c732012-10-05 19:17:591511 if (!events->empty())
[email protected]ec1d6d52012-10-10 01:28:571512 m_client->postAnimationEventsToMainThreadOnImplThread(events.Pass(), wallClockTime);
[email protected]94f206c12012-08-25 00:09:141513
1514 if (didAnimate)
1515 m_client->setNeedsRedrawOnImplThread();
1516
1517 setBackgroundTickingEnabled(!m_visible && m_needsAnimateLayers);
1518}
1519
[email protected]96baf3e2012-10-22 23:09:551520base::TimeDelta LayerTreeHostImpl::lowFrequencyAnimationInterval() const
[email protected]94f206c12012-08-25 00:09:141521{
[email protected]4481ddb622012-09-20 16:33:471522 return base::TimeDelta::FromSeconds(1);
[email protected]94f206c12012-08-25 00:09:141523}
1524
[email protected]96baf3e2012-10-22 23:09:551525void LayerTreeHostImpl::sendDidLoseContextRecursive(LayerImpl* current)
[email protected]94f206c12012-08-25 00:09:141526{
[email protected]1d993172012-10-18 18:15:041527 DCHECK(current);
[email protected]94f206c12012-08-25 00:09:141528 current->didLoseContext();
1529 if (current->maskLayer())
1530 sendDidLoseContextRecursive(current->maskLayer());
1531 if (current->replicaLayer())
1532 sendDidLoseContextRecursive(current->replicaLayer());
1533 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031534 sendDidLoseContextRecursive(current->children()[i]);
[email protected]94f206c12012-08-25 00:09:141535}
1536
[email protected]96baf3e2012-10-22 23:09:551537static void clearRenderSurfacesOnLayerImplRecursive(LayerImpl* current)
[email protected]94f206c12012-08-25 00:09:141538{
[email protected]1d993172012-10-18 18:15:041539 DCHECK(current);
[email protected]94f206c12012-08-25 00:09:141540 for (size_t i = 0; i < current->children().size(); ++i)
[email protected]96baf3e2012-10-22 23:09:551541 clearRenderSurfacesOnLayerImplRecursive(current->children()[i]);
[email protected]94f206c12012-08-25 00:09:141542 current->clearRenderSurface();
1543}
1544
[email protected]96baf3e2012-10-22 23:09:551545void LayerTreeHostImpl::clearRenderSurfaces()
[email protected]94f206c12012-08-25 00:09:141546{
[email protected]96baf3e2012-10-22 23:09:551547 clearRenderSurfacesOnLayerImplRecursive(m_rootLayerImpl.get());
[email protected]94f206c12012-08-25 00:09:141548 m_renderSurfaceLayerList.clear();
1549}
1550
[email protected]96baf3e2012-10-22 23:09:551551std::string LayerTreeHostImpl::layerTreeAsText() const
[email protected]94f206c12012-08-25 00:09:141552{
[email protected]515e8d232012-09-10 19:15:271553 std::string str;
[email protected]94f206c12012-08-25 00:09:141554 if (m_rootLayerImpl) {
[email protected]515e8d232012-09-10 19:15:271555 str = m_rootLayerImpl->layerTreeAsText();
1556 str += "RenderSurfaces:\n";
1557 dumpRenderSurfaces(&str, 1, m_rootLayerImpl.get());
[email protected]94f206c12012-08-25 00:09:141558 }
[email protected]515e8d232012-09-10 19:15:271559 return str;
[email protected]94f206c12012-08-25 00:09:141560}
1561
[email protected]96baf3e2012-10-22 23:09:551562void LayerTreeHostImpl::dumpRenderSurfaces(std::string* str, int indent, const LayerImpl* layer) const
[email protected]94f206c12012-08-25 00:09:141563{
1564 if (layer->renderSurface())
[email protected]515e8d232012-09-10 19:15:271565 layer->renderSurface()->dumpSurface(str, indent);
[email protected]94f206c12012-08-25 00:09:141566
1567 for (size_t i = 0; i < layer->children().size(); ++i)
[email protected]0920e24f2012-09-20 03:34:031568 dumpRenderSurfaces(str, indent, layer->children()[i]);
[email protected]94f206c12012-08-25 00:09:141569}
1570
[email protected]96baf3e2012-10-22 23:09:551571int LayerTreeHostImpl::sourceAnimationFrameNumber() const
[email protected]94f206c12012-08-25 00:09:141572{
1573 return fpsCounter()->currentFrameNumber();
1574}
1575
[email protected]96baf3e2012-10-22 23:09:551576void LayerTreeHostImpl::renderingStats(RenderingStats* stats) const
[email protected]94f206c12012-08-25 00:09:141577{
[email protected]8b9af6b2012-09-27 00:36:361578 stats->numFramesSentToScreen = fpsCounter()->currentFrameNumber();
1579 stats->droppedFrameCount = fpsCounter()->droppedFrameCount();
[email protected]5c6fe1f82012-10-03 18:00:271580 stats->numImplThreadScrolls = m_numImplThreadScrolls;
1581 stats->numMainThreadScrolls = m_numMainThreadScrolls;
[email protected]94f206c12012-08-25 00:09:141582}
1583
[email protected]30faac92012-10-29 00:06:291584void LayerTreeHostImpl::animateScrollbars(base::TimeTicks time)
[email protected]94f206c12012-08-25 00:09:141585{
[email protected]30faac92012-10-29 00:06:291586 animateScrollbarsRecursive(m_rootLayerImpl.get(), time);
[email protected]94f206c12012-08-25 00:09:141587}
1588
[email protected]30faac92012-10-29 00:06:291589void LayerTreeHostImpl::animateScrollbarsRecursive(LayerImpl* layer, base::TimeTicks time)
[email protected]94f206c12012-08-25 00:09:141590{
1591 if (!layer)
1592 return;
1593
[email protected]96baf3e2012-10-22 23:09:551594 ScrollbarAnimationController* scrollbarController = layer->scrollbarAnimationController();
[email protected]30faac92012-10-29 00:06:291595 double monotonicTime = (time - base::TimeTicks()).InSecondsF();
[email protected]94f206c12012-08-25 00:09:141596 if (scrollbarController && scrollbarController->animate(monotonicTime))
1597 m_client->setNeedsRedrawOnImplThread();
1598
1599 for (size_t i = 0; i < layer->children().size(); ++i)
[email protected]30faac92012-10-29 00:06:291600 animateScrollbarsRecursive(layer->children()[i], time);
[email protected]94f206c12012-08-25 00:09:141601}
1602
[email protected]d3143c732012-10-05 19:17:591603} // namespace cc