[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 5 | #include "cc/trees/layer_tree_host_impl.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 6 | |
[email protected] | ac7c7f5 | 2012-11-08 06:26:50 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | ad5d142 | 2012-10-19 13:40:29 | [diff] [blame] | 9 | #include "base/basictypes.h" |
[email protected] | 4456eee2 | 2012-10-19 18:16:38 | [diff] [blame] | 10 | #include "base/debug/trace_event.h" |
[email protected] | 4a23c374c | 2012-12-08 08:38:55 | [diff] [blame] | 11 | #include "base/json/json_writer.h" |
[email protected] | f586491 | 2013-02-01 03:18:14 | [diff] [blame] | 12 | #include "base/metrics/histogram.h" |
[email protected] | de4afb5e | 2012-12-20 00:11:34 | [diff] [blame] | 13 | #include "base/stl_util.h" |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 14 | #include "base/stringprintf.h" |
[email protected] | 95e4e1a0 | 2013-03-18 07:09:09 | [diff] [blame] | 15 | #include "cc/animation/scrollbar_animation_controller.h" |
[email protected] | 681ccff | 2013-03-18 06:13:52 | [diff] [blame] | 16 | #include "cc/base/math_util.h" |
| 17 | #include "cc/base/util.h" |
[email protected] | 6e84de2 | 2013-03-18 06:54:27 | [diff] [blame] | 18 | #include "cc/debug/debug_rect_history.h" |
| 19 | #include "cc/debug/frame_rate_counter.h" |
| 20 | #include "cc/debug/overdraw_metrics.h" |
| 21 | #include "cc/debug/paint_time_counter.h" |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 22 | #include "cc/debug/rendering_stats_instrumentation.h" |
[email protected] | 3052b10f | 2013-03-18 07:41:21 | [diff] [blame] | 23 | #include "cc/input/page_scale_animation.h" |
| 24 | #include "cc/input/top_controls_manager.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 25 | #include "cc/layers/append_quads_data.h" |
| 26 | #include "cc/layers/heads_up_display_layer_impl.h" |
| 27 | #include "cc/layers/layer_iterator.h" |
| 28 | #include "cc/layers/scrollbar_layer_impl.h" |
[email protected] | 7f0d825f | 2013-03-18 07:24:30 | [diff] [blame] | 29 | #include "cc/output/compositor_frame_metadata.h" |
| 30 | #include "cc/output/delegating_renderer.h" |
| 31 | #include "cc/output/gl_renderer.h" |
| 32 | #include "cc/output/software_renderer.h" |
[email protected] | 89e8267 | 2013-03-18 07:50:56 | [diff] [blame] | 33 | #include "cc/quads/render_pass_draw_quad.h" |
| 34 | #include "cc/quads/shared_quad_state.h" |
| 35 | #include "cc/quads/solid_color_draw_quad.h" |
[email protected] | e12dd0e | 2013-03-18 08:24:40 | [diff] [blame] | 36 | #include "cc/resources/memory_history.h" |
| 37 | #include "cc/resources/picture_layer_tiling.h" |
| 38 | #include "cc/resources/prioritized_resource_manager.h" |
[email protected] | be4655a | 2013-03-18 08:36:31 | [diff] [blame] | 39 | #include "cc/scheduler/delay_based_time_source.h" |
| 40 | #include "cc/scheduler/texture_uploader.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 41 | #include "cc/trees/damage_tracker.h" |
| 42 | #include "cc/trees/layer_tree_host.h" |
| 43 | #include "cc/trees/layer_tree_host_common.h" |
| 44 | #include "cc/trees/layer_tree_impl.h" |
| 45 | #include "cc/trees/quad_culler.h" |
| 46 | #include "cc/trees/single_thread_proxy.h" |
| 47 | #include "cc/trees/tree_synchronizer.h" |
[email protected] | d455d55 | 2012-11-02 00:19:06 | [diff] [blame] | 48 | #include "ui/gfx/size_conversions.h" |
[email protected] | c9c1ebe | 2012-11-05 20:46:13 | [diff] [blame] | 49 | #include "ui/gfx/vector2d_conversions.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 50 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 51 | namespace { |
| 52 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 53 | void DidVisibilityChange(cc::LayerTreeHostImpl* id, bool visible) { |
| 54 | if (visible) { |
| 55 | TRACE_EVENT_ASYNC_BEGIN1("webkit", |
| 56 | "LayerTreeHostImpl::SetVisible", |
| 57 | id, |
| 58 | "LayerTreeHostImpl", |
| 59 | id); |
| 60 | return; |
| 61 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 62 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 63 | TRACE_EVENT_ASYNC_END0("webkit", "LayerTreeHostImpl::SetVisible", id); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 66 | std::string ValueToString(scoped_ptr<base::Value> value) { |
| 67 | std::string str; |
| 68 | base::JSONWriter::Write(value.get(), &str); |
| 69 | return str; |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 72 | } // namespace |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 73 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 74 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 75 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 76 | class LayerTreeHostImplTimeSourceAdapter : public TimeSourceClient { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 77 | public: |
| 78 | static scoped_ptr<LayerTreeHostImplTimeSourceAdapter> Create( |
| 79 | LayerTreeHostImpl* layer_tree_host_impl, |
| 80 | scoped_refptr<DelayBasedTimeSource> time_source) { |
| 81 | return make_scoped_ptr( |
| 82 | new LayerTreeHostImplTimeSourceAdapter(layer_tree_host_impl, |
| 83 | time_source)); |
| 84 | } |
| 85 | virtual ~LayerTreeHostImplTimeSourceAdapter() { |
[email protected] | 6d0e69d | 2013-03-20 14:53:26 | [diff] [blame] | 86 | time_source_->SetClient(NULL); |
| 87 | time_source_->SetActive(false); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | 6d0e69d | 2013-03-20 14:53:26 | [diff] [blame] | 90 | virtual void OnTimerTick() OVERRIDE { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 91 | // In single threaded mode we attempt to simulate changing the current |
| 92 | // thread by maintaining a fake thread id. When we switch from one |
| 93 | // thread to another, we construct DebugScopedSetXXXThread objects that |
| 94 | // update the thread id. This lets DCHECKS that ensure we're on the |
| 95 | // right thread to work correctly in single threaded mode. The problem |
| 96 | // here is that the timer tasks are run via the message loop, and when |
| 97 | // they run, we've had no chance to construct a DebugScopedSetXXXThread |
| 98 | // object. The result is that we report that we're running on the main |
| 99 | // thread. In multi-threaded mode, this timer is run on the compositor |
| 100 | // thread, so to keep this consistent in single-threaded mode, we'll |
| 101 | // construct a DebugScopedSetImplThread object. There is no need to do |
| 102 | // this in multi-threaded mode since the real thread id's will be |
| 103 | // correct. In fact, setting fake thread id's interferes with the real |
| 104 | // thread id's and causes breakage. |
| 105 | scoped_ptr<DebugScopedSetImplThread> set_impl_thread; |
| 106 | if (!layer_tree_host_impl_->proxy()->HasImplThread()) { |
| 107 | set_impl_thread.reset( |
| 108 | new DebugScopedSetImplThread(layer_tree_host_impl_->proxy())); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 111 | layer_tree_host_impl_->ActivatePendingTreeIfNeeded(); |
| 112 | layer_tree_host_impl_->Animate(base::TimeTicks::Now(), base::Time::Now()); |
| 113 | layer_tree_host_impl_->BeginNextFrame(); |
| 114 | } |
[email protected] | 37397423 | 2013-01-10 22:20:50 | [diff] [blame] | 115 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 116 | void SetActive(bool active) { |
[email protected] | 6d0e69d | 2013-03-20 14:53:26 | [diff] [blame] | 117 | if (active != time_source_->Active()) |
| 118 | time_source_->SetActive(active); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 119 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 120 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 121 | private: |
| 122 | LayerTreeHostImplTimeSourceAdapter( |
| 123 | LayerTreeHostImpl* layer_tree_host_impl, |
| 124 | scoped_refptr<DelayBasedTimeSource> time_source) |
| 125 | : layer_tree_host_impl_(layer_tree_host_impl), |
| 126 | time_source_(time_source) { |
[email protected] | 6d0e69d | 2013-03-20 14:53:26 | [diff] [blame] | 127 | time_source_->SetClient(this); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 128 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 129 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 130 | LayerTreeHostImpl* layer_tree_host_impl_; |
| 131 | scoped_refptr<DelayBasedTimeSource> time_source_; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 132 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImplTimeSourceAdapter); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 134 | }; |
| 135 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 136 | LayerTreeHostImpl::FrameData::FrameData() |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 137 | : contains_incomplete_tile(false) {} |
| 138 | |
| 139 | LayerTreeHostImpl::FrameData::~FrameData() {} |
| 140 | |
| 141 | scoped_ptr<LayerTreeHostImpl> LayerTreeHostImpl::Create( |
| 142 | const LayerTreeSettings& settings, |
| 143 | LayerTreeHostImplClient* client, |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 144 | Proxy* proxy, |
| 145 | RenderingStatsInstrumentation* rendering_stats_instrumentation) { |
| 146 | return make_scoped_ptr( |
| 147 | new LayerTreeHostImpl(settings, |
| 148 | client, |
| 149 | proxy, |
| 150 | rendering_stats_instrumentation)); |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 153 | LayerTreeHostImpl::LayerTreeHostImpl( |
| 154 | const LayerTreeSettings& settings, |
| 155 | LayerTreeHostImplClient* client, |
| 156 | Proxy* proxy, |
| 157 | RenderingStatsInstrumentation* rendering_stats_instrumentation) |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 158 | : client_(client), |
| 159 | proxy_(proxy), |
| 160 | did_lock_scrolling_layer_(false), |
| 161 | should_bubble_scrolls_(false), |
| 162 | wheel_scrolling_(false), |
| 163 | settings_(settings), |
[email protected] | d908376 | 2013-03-24 01:36:40 | [diff] [blame] | 164 | overdraw_bottom_height_(0.f), |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 165 | device_scale_factor_(1.f), |
| 166 | visible_(true), |
| 167 | managed_memory_policy_( |
[email protected] | b56c130 | 2013-03-20 21:17:34 | [diff] [blame] | 168 | PrioritizedResourceManager::DefaultMemoryAllocationLimit(), |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 169 | ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING, |
| 170 | 0, |
| 171 | ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING), |
| 172 | pinch_gesture_active_(false), |
[email protected] | 9e359452 | 2013-03-18 00:57:36 | [diff] [blame] | 173 | fps_counter_(FrameRateCounter::Create(proxy_->HasImplThread())), |
[email protected] | 7497316a | 2013-03-15 12:42:29 | [diff] [blame] | 174 | paint_time_counter_(PaintTimeCounter::Create()), |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 175 | memory_history_(MemoryHistory::Create()), |
[email protected] | d3599278 | 2013-03-14 14:54:02 | [diff] [blame] | 176 | debug_rect_history_(DebugRectHistory::Create()), |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 177 | last_sent_memory_visible_bytes_(0), |
| 178 | last_sent_memory_visible_and_nearby_bytes_(0), |
| 179 | last_sent_memory_use_bytes_(0), |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 180 | animation_registrar_(AnimationRegistrar::Create()), |
| 181 | rendering_stats_instrumentation_(rendering_stats_instrumentation) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 182 | DCHECK(proxy_->IsImplThread()); |
| 183 | DidVisibilityChange(this, visible_); |
| 184 | |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 185 | SetDebugState(settings.initial_debug_state); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 186 | |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 187 | if (settings.calculate_top_controls_position) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 188 | top_controls_manager_ = |
| 189 | TopControlsManager::Create(this, |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 190 | settings.top_controls_height, |
| 191 | settings.top_controls_show_threshold, |
| 192 | settings.top_controls_hide_threshold); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 195 | SetDebugState(settings.initial_debug_state); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 196 | |
| 197 | // LTHI always has an active tree. |
| 198 | active_tree_ = LayerTreeImpl::create(this); |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 201 | LayerTreeHostImpl::~LayerTreeHostImpl() { |
| 202 | DCHECK(proxy_->IsImplThread()); |
| 203 | TRACE_EVENT0("cc", "LayerTreeHostImpl::~LayerTreeHostImpl()"); |
| 204 | |
| 205 | if (active_tree_->root_layer()) { |
| 206 | ClearRenderSurfaces(); |
| 207 | // The layer trees must be destroyed before the layer tree host. We've |
| 208 | // made a contract with our animation controllers that the registrar |
| 209 | // will outlive them, and we must make good. |
| 210 | recycle_tree_.reset(); |
| 211 | pending_tree_.reset(); |
| 212 | active_tree_.reset(); |
| 213 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 216 | void LayerTreeHostImpl::BeginCommit() {} |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 217 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 218 | void LayerTreeHostImpl::CommitComplete() { |
| 219 | TRACE_EVENT0("cc", "LayerTreeHostImpl::CommitComplete"); |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 220 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 221 | // Impl-side painting needs an update immediately post-commit to have the |
| 222 | // opportunity to create tilings. Other paths can call UpdateDrawProperties |
| 223 | // more lazily when needed prior to drawing. |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 224 | if (settings_.impl_side_painting) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 225 | pending_tree_->set_needs_update_draw_properties(); |
| 226 | pending_tree_->UpdateDrawProperties(LayerTreeImpl::UPDATE_PENDING_TREE); |
| 227 | } else { |
| 228 | active_tree_->set_needs_update_draw_properties(); |
| 229 | } |
[email protected] | 3ba4cae | 2013-01-16 03:58:38 | [diff] [blame] | 230 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 231 | client_->SendManagedMemoryStats(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 234 | bool LayerTreeHostImpl::CanDraw() { |
| 235 | // Note: If you are changing this function or any other function that might |
| 236 | // affect the result of CanDraw, make sure to call |
| 237 | // client_->OnCanDrawStateChanged in the proper places and update the |
| 238 | // NotifyIfCanDrawChanged test. |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 239 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 240 | if (!active_tree_->root_layer()) { |
[email protected] | c76faea | 2013-03-26 07:42:42 | [diff] [blame] | 241 | TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no root layer", |
| 242 | TRACE_EVENT_SCOPE_THREAD); |
[email protected] | 2f1acc26 | 2012-11-16 21:42:22 | [diff] [blame] | 243 | return false; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 244 | } |
| 245 | if (device_viewport_size_.IsEmpty()) { |
[email protected] | c76faea | 2013-03-26 07:42:42 | [diff] [blame] | 246 | TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw empty viewport", |
| 247 | TRACE_EVENT_SCOPE_THREAD); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | if (active_tree_->ViewportSizeInvalid()) { |
| 251 | TRACE_EVENT_INSTANT0( |
[email protected] | c76faea | 2013-03-26 07:42:42 | [diff] [blame] | 252 | "cc", "LayerTreeHostImpl::CanDraw viewport size recently changed", |
| 253 | TRACE_EVENT_SCOPE_THREAD); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | if (!renderer_) { |
[email protected] | c76faea | 2013-03-26 07:42:42 | [diff] [blame] | 257 | TRACE_EVENT_INSTANT0("cc", "LayerTreeHostImpl::CanDraw no renderer", |
| 258 | TRACE_EVENT_SCOPE_THREAD); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | if (active_tree_->ContentsTexturesPurged()) { |
| 262 | TRACE_EVENT_INSTANT0( |
[email protected] | c76faea | 2013-03-26 07:42:42 | [diff] [blame] | 263 | "cc", "LayerTreeHostImpl::CanDraw contents textures purged", |
| 264 | TRACE_EVENT_SCOPE_THREAD); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | return true; |
[email protected] | 2f1acc26 | 2012-11-16 21:42:22 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 270 | void LayerTreeHostImpl::Animate(base::TimeTicks monotonic_time, |
| 271 | base::Time wall_clock_time) { |
| 272 | AnimatePageScale(monotonic_time); |
| 273 | AnimateLayers(monotonic_time, wall_clock_time); |
| 274 | AnimateScrollbars(monotonic_time); |
[email protected] | ffb2720f | 2013-03-15 19:18:37 | [diff] [blame] | 275 | AnimateTopControls(monotonic_time); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 276 | } |
| 277 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 278 | void LayerTreeHostImpl::ManageTiles() { |
| 279 | DCHECK(tile_manager_); |
| 280 | tile_manager_->ManageTiles(); |
| 281 | |
| 282 | size_t memory_required_bytes; |
| 283 | size_t memory_nice_to_have_bytes; |
| 284 | size_t memory_used_bytes; |
| 285 | tile_manager_->GetMemoryStats(&memory_required_bytes, |
| 286 | &memory_nice_to_have_bytes, |
| 287 | &memory_used_bytes); |
| 288 | SendManagedMemoryStats(memory_required_bytes, |
| 289 | memory_nice_to_have_bytes, |
| 290 | memory_used_bytes); |
[email protected] | f57bbc0 | 2012-11-21 07:02:15 | [diff] [blame] | 291 | } |
| 292 | |
[email protected] | 80334ba | 2013-03-20 17:33:50 | [diff] [blame] | 293 | void LayerTreeHostImpl::SetAnticipatedDrawTime(base::TimeTicks time) { |
[email protected] | f845661 | 2013-03-19 20:56:09 | [diff] [blame] | 294 | if (tile_manager_) |
| 295 | tile_manager_->SetAnticipatedDrawTime(time); |
| 296 | } |
| 297 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 298 | void LayerTreeHostImpl::StartPageScaleAnimation(gfx::Vector2d target_offset, |
| 299 | bool anchor_point, |
| 300 | float page_scale, |
| 301 | base::TimeTicks start_time, |
| 302 | base::TimeDelta duration) { |
| 303 | if (!RootScrollLayer()) |
| 304 | return; |
| 305 | |
| 306 | gfx::Vector2dF scroll_total = |
| 307 | RootScrollLayer()->scroll_offset() + RootScrollLayer()->scroll_delta(); |
| 308 | gfx::SizeF scaled_scrollable_size = active_tree_->ScrollableSize(); |
[email protected] | 6fc4ee00 | 2013-03-26 23:39:51 | [diff] [blame] | 309 | gfx::SizeF viewport_size = VisibleViewportSize(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 310 | |
| 311 | double start_time_seconds = (start_time - base::TimeTicks()).InSecondsF(); |
| 312 | page_scale_animation_ = |
| 313 | PageScaleAnimation::Create(scroll_total, |
| 314 | active_tree_->total_page_scale_factor(), |
| 315 | viewport_size, |
| 316 | scaled_scrollable_size, |
| 317 | start_time_seconds); |
| 318 | |
| 319 | if (anchor_point) { |
| 320 | gfx::Vector2dF anchor(target_offset); |
| 321 | page_scale_animation_->ZoomWithAnchor(anchor, |
| 322 | page_scale, |
| 323 | duration.InSecondsF()); |
| 324 | } else { |
| 325 | gfx::Vector2dF scaled_target_offset = target_offset; |
| 326 | page_scale_animation_->ZoomTo(scaled_target_offset, |
| 327 | page_scale, |
| 328 | duration.InSecondsF()); |
| 329 | } |
| 330 | |
| 331 | client_->SetNeedsRedrawOnImplThread(); |
| 332 | client_->SetNeedsCommitOnImplThread(); |
| 333 | client_->RenewTreePriority(); |
[email protected] | f57bbc0 | 2012-11-21 07:02:15 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 336 | void LayerTreeHostImpl::ScheduleAnimation() { |
| 337 | client_->SetNeedsRedrawOnImplThread(); |
[email protected] | f57bbc0 | 2012-11-21 07:02:15 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 340 | bool LayerTreeHostImpl::HaveTouchEventHandlersAt(gfx::Point viewport_point) { |
| 341 | if (!EnsureRenderSurfaceLayerList()) |
| 342 | return false; |
[email protected] | f57bbc0 | 2012-11-21 07:02:15 | [diff] [blame] | 343 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 344 | gfx::PointF device_viewport_point = |
| 345 | gfx::ScalePoint(viewport_point, device_scale_factor_); |
[email protected] | f57bbc0 | 2012-11-21 07:02:15 | [diff] [blame] | 346 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 347 | // First find out which layer was hit from the saved list of visible layers |
| 348 | // in the most recent frame. |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 349 | LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint( |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 350 | device_viewport_point, |
| 351 | active_tree_->RenderSurfaceLayerList()); |
[email protected] | f57bbc0 | 2012-11-21 07:02:15 | [diff] [blame] | 352 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 353 | // Walk up the hierarchy and look for a layer with a touch event handler |
| 354 | // region that the given point hits. |
| 355 | for (; layer_impl; layer_impl = layer_impl->parent()) { |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 356 | if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(device_viewport_point, |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 357 | layer_impl)) |
| 358 | return true; |
| 359 | } |
[email protected] | f57bbc0 | 2012-11-21 07:02:15 | [diff] [blame] | 360 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 361 | return false; |
| 362 | } |
| 363 | |
| 364 | void LayerTreeHostImpl::TrackDamageForAllSurfaces( |
| 365 | LayerImpl* root_draw_layer, |
| 366 | const LayerList& render_surface_layer_list) { |
| 367 | // For now, we use damage tracking to compute a global scissor. To do this, we |
| 368 | // must compute all damage tracking before drawing anything, so that we know |
| 369 | // the root damage rect. The root damage rect is then used to scissor each |
| 370 | // surface. |
| 371 | |
| 372 | for (int surface_index = render_surface_layer_list.size() - 1; |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 373 | surface_index >= 0; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 374 | --surface_index) { |
| 375 | LayerImpl* render_surface_layer = render_surface_layer_list[surface_index]; |
| 376 | RenderSurfaceImpl* render_surface = render_surface_layer->render_surface(); |
| 377 | DCHECK(render_surface); |
| 378 | render_surface->damage_tracker()->UpdateDamageTrackingState( |
| 379 | render_surface->layer_list(), |
| 380 | render_surface_layer->id(), |
| 381 | render_surface->SurfacePropertyChangedOnlyFromDescendant(), |
| 382 | render_surface->content_rect(), |
| 383 | render_surface_layer->mask_layer(), |
| 384 | render_surface_layer->filters(), |
| 385 | render_surface_layer->filter().get()); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void LayerTreeHostImpl::FrameData::AppendRenderPass( |
| 390 | scoped_ptr<RenderPass> render_pass) { |
| 391 | render_passes_by_id[render_pass->id] = render_pass.get(); |
| 392 | render_passes.push_back(render_pass.Pass()); |
| 393 | } |
| 394 | |
| 395 | static void AppendQuadsForLayer(RenderPass* target_render_pass, |
| 396 | LayerImpl* layer, |
| 397 | const OcclusionTrackerImpl& occlusion_tracker, |
| 398 | AppendQuadsData* append_quads_data) { |
| 399 | bool for_surface = false; |
[email protected] | c7e95b4 | 2013-03-18 01:13:49 | [diff] [blame] | 400 | QuadCuller quad_culler(&target_render_pass->quad_list, |
| 401 | &target_render_pass->shared_quad_state_list, |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 402 | layer, |
| 403 | occlusion_tracker, |
| 404 | layer->ShowDebugBorders(), |
| 405 | for_surface); |
| 406 | layer->AppendQuads(&quad_culler, append_quads_data); |
| 407 | } |
| 408 | |
| 409 | static void AppendQuadsForRenderSurfaceLayer( |
| 410 | RenderPass* target_render_pass, |
| 411 | LayerImpl* layer, |
| 412 | const RenderPass* contributing_render_pass, |
| 413 | const OcclusionTrackerImpl& occlusion_tracker, |
| 414 | AppendQuadsData* append_quads_data) { |
| 415 | bool for_surface = true; |
[email protected] | c7e95b4 | 2013-03-18 01:13:49 | [diff] [blame] | 416 | QuadCuller quad_culler(&target_render_pass->quad_list, |
| 417 | &target_render_pass->shared_quad_state_list, |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 418 | layer, |
| 419 | occlusion_tracker, |
| 420 | layer->ShowDebugBorders(), |
| 421 | for_surface); |
| 422 | |
| 423 | bool is_replica = false; |
| 424 | layer->render_surface()->AppendQuads(&quad_culler, |
| 425 | append_quads_data, |
| 426 | is_replica, |
| 427 | contributing_render_pass->id); |
| 428 | |
| 429 | // Add replica after the surface so that it appears below the surface. |
| 430 | if (layer->has_replica()) { |
| 431 | is_replica = true; |
| 432 | layer->render_surface()->AppendQuads(&quad_culler, |
| 433 | append_quads_data, |
| 434 | is_replica, |
| 435 | contributing_render_pass->id); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | static void AppendQuadsToFillScreen( |
| 440 | RenderPass* target_render_pass, |
| 441 | LayerImpl* root_layer, |
| 442 | SkColor screen_background_color, |
| 443 | const OcclusionTrackerImpl& occlusion_tracker) { |
| 444 | if (!root_layer || !SkColorGetA(screen_background_color)) |
| 445 | return; |
| 446 | |
| 447 | Region fill_region = occlusion_tracker.ComputeVisibleRegionInScreen(); |
| 448 | if (fill_region.IsEmpty()) |
| 449 | return; |
| 450 | |
| 451 | bool for_surface = false; |
[email protected] | c7e95b4 | 2013-03-18 01:13:49 | [diff] [blame] | 452 | QuadCuller quad_culler(&target_render_pass->quad_list, |
| 453 | &target_render_pass->shared_quad_state_list, |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 454 | root_layer, |
| 455 | occlusion_tracker, |
| 456 | root_layer->ShowDebugBorders(), |
| 457 | for_surface); |
| 458 | |
| 459 | // Manually create the quad state for the gutter quads, as the root layer |
| 460 | // doesn't have any bounds and so can't generate this itself. |
| 461 | // TODO(danakj): Make the gutter quads generated by the solid color layer |
| 462 | // (make it smarter about generating quads to fill unoccluded areas). |
| 463 | |
| 464 | gfx::Rect root_target_rect = root_layer->render_surface()->content_rect(); |
| 465 | float opacity = 1.f; |
| 466 | SharedQuadState* shared_quad_state = |
[email protected] | c7e95b4 | 2013-03-18 01:13:49 | [diff] [blame] | 467 | quad_culler.UseSharedQuadState(SharedQuadState::Create()); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 468 | shared_quad_state->SetAll(root_layer->draw_transform(), |
| 469 | root_target_rect.size(), |
| 470 | root_target_rect, |
| 471 | root_target_rect, |
[email protected] | dc462d78 | 2012-11-21 21:43:01 | [diff] [blame] | 472 | false, |
[email protected] | f57bbc0 | 2012-11-21 07:02:15 | [diff] [blame] | 473 | opacity); |
| 474 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 475 | AppendQuadsData append_quads_data; |
[email protected] | bda4196 | 2013-01-07 18:46:17 | [diff] [blame] | 476 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 477 | gfx::Transform transform_to_layer_space(gfx::Transform::kSkipInitialization); |
| 478 | bool did_invert = root_layer->screen_space_transform().GetInverse( |
| 479 | &transform_to_layer_space); |
| 480 | DCHECK(did_invert); |
| 481 | for (Region::Iterator fill_rects(fill_region); |
| 482 | fill_rects.has_rect(); |
| 483 | fill_rects.next()) { |
| 484 | // The root layer transform is composed of translations and scales only, |
| 485 | // no perspective, so mapping is sufficient (as opposed to projecting). |
| 486 | gfx::Rect layer_rect = |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 487 | MathUtil::MapClippedRect(transform_to_layer_space, fill_rects.rect()); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 488 | // Skip the quad culler and just append the quads directly to avoid |
| 489 | // occlusion checks. |
| 490 | scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create(); |
| 491 | quad->SetNew(shared_quad_state, layer_rect, screen_background_color); |
[email protected] | c7e95b4 | 2013-03-18 01:13:49 | [diff] [blame] | 492 | quad_culler.Append(quad.PassAs<DrawQuad>(), &append_quads_data); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 493 | } |
[email protected] | 467b361 | 2012-08-28 07:41:16 | [diff] [blame] | 494 | } |
| 495 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 496 | bool LayerTreeHostImpl::CalculateRenderPasses(FrameData* frame) { |
| 497 | DCHECK(frame->render_passes.empty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 498 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 499 | if (!CanDraw() || !active_tree_->root_layer()) |
| 500 | return false; |
[email protected] | 2d69299 | 2012-12-19 01:19:32 | [diff] [blame] | 501 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 502 | TrackDamageForAllSurfaces(active_tree_->root_layer(), |
| 503 | *frame->render_surface_layer_list); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 504 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 505 | TRACE_EVENT1("cc", |
| 506 | "LayerTreeHostImpl::CalculateRenderPasses", |
| 507 | "render_surface_layer_list.size()", |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 508 | static_cast<uint64>(frame->render_surface_layer_list->size())); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 509 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 510 | // Create the render passes in dependency order. |
| 511 | for (int surface_index = frame->render_surface_layer_list->size() - 1; |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 512 | surface_index >= 0; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 513 | --surface_index) { |
| 514 | LayerImpl* render_surface_layer = |
| 515 | (*frame->render_surface_layer_list)[surface_index]; |
| 516 | render_surface_layer->render_surface()->AppendRenderPasses(frame); |
| 517 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 518 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 519 | bool record_metrics_for_frame = |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 520 | settings_.show_overdraw_in_tracing && |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 521 | base::debug::TraceLog::GetInstance() && |
| 522 | base::debug::TraceLog::GetInstance()->IsEnabled(); |
| 523 | OcclusionTrackerImpl occlusion_tracker( |
| 524 | active_tree_->root_layer()->render_surface()->content_rect(), |
| 525 | record_metrics_for_frame); |
| 526 | occlusion_tracker.set_minimum_tracking_size( |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 527 | settings_.minimum_occlusion_tracking_size); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 528 | |
[email protected] | 846f455b | 2013-03-18 19:07:41 | [diff] [blame] | 529 | if (debug_state_.show_occluding_rects) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 530 | occlusion_tracker.set_occluding_screen_space_rects_container( |
| 531 | &frame->occluding_screen_space_rects); |
| 532 | } |
[email protected] | 846f455b | 2013-03-18 19:07:41 | [diff] [blame] | 533 | if (debug_state_.show_non_occluding_rects) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 534 | occlusion_tracker.set_non_occluding_screen_space_rects_container( |
| 535 | &frame->non_occluding_screen_space_rects); |
| 536 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 537 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 538 | // Add quads to the Render passes in FrontToBack order to allow for testing |
| 539 | // occlusion and performing culling during the tree walk. |
| 540 | typedef LayerIterator<LayerImpl, |
| 541 | std::vector<LayerImpl*>, |
| 542 | RenderSurfaceImpl, |
| 543 | LayerIteratorActions::FrontToBack> LayerIteratorType; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 544 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 545 | // Typically when we are missing a texture and use a checkerboard quad, we |
| 546 | // still draw the frame. However when the layer being checkerboarded is moving |
| 547 | // due to an impl-animation, we drop the frame to avoid flashing due to the |
| 548 | // texture suddenly appearing in the future. |
| 549 | bool draw_frame = true; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 550 | |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 551 | int layers_drawn = 0; |
| 552 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 553 | LayerIteratorType end = |
[email protected] | 71dfcc7 | 2013-03-20 21:30:09 | [diff] [blame] | 554 | LayerIteratorType::End(frame->render_surface_layer_list); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 555 | for (LayerIteratorType it = |
[email protected] | 71dfcc7 | 2013-03-20 21:30:09 | [diff] [blame] | 556 | LayerIteratorType::Begin(frame->render_surface_layer_list); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 557 | it != end; |
| 558 | ++it) { |
| 559 | RenderPass::Id target_render_pass_id = |
[email protected] | 71dfcc7 | 2013-03-20 21:30:09 | [diff] [blame] | 560 | it.target_render_surface_layer()->render_surface()->RenderPassId(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 561 | RenderPass* target_render_pass = |
| 562 | frame->render_passes_by_id[target_render_pass_id]; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 563 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 564 | occlusion_tracker.EnterLayer(it); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 565 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 566 | AppendQuadsData append_quads_data(target_render_pass->id); |
[email protected] | 8922820 | 2012-08-29 03:20:30 | [diff] [blame] | 567 | |
[email protected] | 71dfcc7 | 2013-03-20 21:30:09 | [diff] [blame] | 568 | if (it.represents_contributing_render_surface()) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 569 | RenderPass::Id contributing_render_pass_id = |
| 570 | it->render_surface()->RenderPassId(); |
| 571 | RenderPass* contributing_render_pass = |
| 572 | frame->render_passes_by_id[contributing_render_pass_id]; |
| 573 | AppendQuadsForRenderSurfaceLayer(target_render_pass, |
| 574 | *it, |
| 575 | contributing_render_pass, |
| 576 | occlusion_tracker, |
| 577 | &append_quads_data); |
[email protected] | e1e768f | 2013-03-26 08:48:09 | [diff] [blame] | 578 | } else if (it.represents_itself() && |
| 579 | !it->visible_content_rect().IsEmpty()) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 580 | bool has_occlusion_from_outside_target_surface; |
| 581 | bool impl_draw_transform_is_unknown = false; |
| 582 | if (occlusion_tracker.Occluded( |
| 583 | it->render_target(), |
| 584 | it->visible_content_rect(), |
| 585 | it->draw_transform(), |
| 586 | impl_draw_transform_is_unknown, |
| 587 | it->is_clipped(), |
| 588 | it->clip_rect(), |
| 589 | &has_occlusion_from_outside_target_surface)) { |
[email protected] | e1e768f | 2013-03-26 08:48:09 | [diff] [blame] | 590 | append_quads_data.had_occlusion_from_outside_target_surface |= |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 591 | has_occlusion_from_outside_target_surface; |
| 592 | } else { |
| 593 | DCHECK_EQ(active_tree_, it->layer_tree_impl()); |
| 594 | it->WillDraw(resource_provider_.get()); |
| 595 | frame->will_draw_layers.push_back(*it); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 596 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 597 | if (it->HasContributingDelegatedRenderPasses()) { |
| 598 | RenderPass::Id contributing_render_pass_id = |
| 599 | it->FirstContributingRenderPassId(); |
| 600 | while (frame->render_passes_by_id.find(contributing_render_pass_id) != |
| 601 | frame->render_passes_by_id.end()) { |
| 602 | RenderPass* render_pass = |
| 603 | frame->render_passes_by_id[contributing_render_pass_id]; |
[email protected] | f586491 | 2013-02-01 03:18:14 | [diff] [blame] | 604 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 605 | AppendQuadsData append_quads_data(render_pass->id); |
| 606 | AppendQuadsForLayer(render_pass, |
| 607 | *it, |
| 608 | occlusion_tracker, |
| 609 | &append_quads_data); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 610 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 611 | contributing_render_pass_id = |
| 612 | it->NextContributingRenderPassId(contributing_render_pass_id); |
| 613 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 616 | AppendQuadsForLayer(target_render_pass, |
| 617 | *it, |
| 618 | occlusion_tracker, |
| 619 | &append_quads_data); |
| 620 | } |
[email protected] | 8922820 | 2012-08-29 03:20:30 | [diff] [blame] | 621 | |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 622 | ++layers_drawn; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 623 | } |
| 624 | |
[email protected] | e1e768f | 2013-03-26 08:48:09 | [diff] [blame] | 625 | if (append_quads_data.had_occlusion_from_outside_target_surface) |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 626 | target_render_pass->has_occlusion_from_outside_target_surface = true; |
| 627 | |
[email protected] | e1e768f | 2013-03-26 08:48:09 | [diff] [blame] | 628 | if (append_quads_data.num_missing_tiles) { |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 629 | rendering_stats_instrumentation_->AddMissingTiles( |
[email protected] | e1e768f | 2013-03-26 08:48:09 | [diff] [blame] | 630 | append_quads_data.num_missing_tiles); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 631 | bool layer_has_animating_transform = |
| 632 | it->screen_space_transform_is_animating() || |
| 633 | it->draw_transform_is_animating(); |
| 634 | if (layer_has_animating_transform) |
| 635 | draw_frame = false; |
| 636 | } |
| 637 | |
[email protected] | e1e768f | 2013-03-26 08:48:09 | [diff] [blame] | 638 | if (append_quads_data.had_incomplete_tile) |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 639 | frame->contains_incomplete_tile = true; |
| 640 | |
| 641 | occlusion_tracker.LeaveLayer(it); |
| 642 | } |
| 643 | |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 644 | rendering_stats_instrumentation_->AddLayersDrawn(layers_drawn); |
| 645 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 646 | #ifndef NDEBUG |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 647 | for (size_t i = 0; i < frame->render_passes.size(); ++i) { |
| 648 | for (size_t j = 0; j < frame->render_passes[i]->quad_list.size(); ++j) |
| 649 | DCHECK(frame->render_passes[i]->quad_list[j]->shared_quad_state); |
| 650 | DCHECK(frame->render_passes_by_id.find(frame->render_passes[i]->id) |
| 651 | != frame->render_passes_by_id.end()); |
| 652 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 653 | #endif |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 654 | DCHECK(frame->render_passes.back()->output_rect.origin().IsOrigin()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 655 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 656 | if (!active_tree_->has_transparent_background()) { |
| 657 | frame->render_passes.back()->has_transparent_background = false; |
| 658 | AppendQuadsToFillScreen(frame->render_passes.back(), |
| 659 | active_tree_->root_layer(), |
| 660 | active_tree_->background_color(), |
| 661 | occlusion_tracker); |
| 662 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 663 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 664 | if (draw_frame) |
| 665 | occlusion_tracker.overdraw_metrics()->RecordMetrics(this); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 666 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 667 | RemoveRenderPasses(CullRenderPassesWithNoQuads(), frame); |
| 668 | renderer_->DecideRenderPassAllocationsForFrame(frame->render_passes); |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 669 | RemoveRenderPasses(CullRenderPassesWithCachedTextures(renderer_.get()), |
| 670 | frame); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 671 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 672 | return draw_frame; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 673 | } |
| 674 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 675 | void LayerTreeHostImpl::SetBackgroundTickingEnabled(bool enabled) { |
| 676 | // Lazily create the time_source adapter so that we can vary the interval for |
| 677 | // testing. |
| 678 | if (!time_source_client_adapter_) { |
| 679 | time_source_client_adapter_ = LayerTreeHostImplTimeSourceAdapter::Create( |
| 680 | this, |
[email protected] | 6d0e69d | 2013-03-20 14:53:26 | [diff] [blame] | 681 | DelayBasedTimeSource::Create(LowFrequencyAnimationInterval(), |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 682 | proxy_->CurrentThread())); |
| 683 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 684 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 685 | time_source_client_adapter_->SetActive(enabled); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 686 | } |
| 687 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 688 | static inline RenderPass* FindRenderPassById( |
| 689 | RenderPass::Id render_pass_id, |
| 690 | const LayerTreeHostImpl::FrameData& frame) { |
| 691 | RenderPassIdHashMap::const_iterator it = |
| 692 | frame.render_passes_by_id.find(render_pass_id); |
| 693 | return it != frame.render_passes_by_id.end() ? it->second : NULL; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 694 | } |
| 695 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 696 | static void RemoveRenderPassesRecursive(RenderPass::Id remove_render_pass_id, |
| 697 | LayerTreeHostImpl::FrameData* frame) { |
| 698 | RenderPass* remove_render_pass = |
| 699 | FindRenderPassById(remove_render_pass_id, *frame); |
| 700 | // The pass was already removed by another quad - probably the original, and |
| 701 | // we are the replica. |
| 702 | if (!remove_render_pass) |
| 703 | return; |
| 704 | RenderPassList& render_passes = frame->render_passes; |
| 705 | RenderPassList::iterator to_remove = std::find(render_passes.begin(), |
| 706 | render_passes.end(), |
| 707 | remove_render_pass); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 708 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 709 | DCHECK(to_remove != render_passes.end()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 710 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 711 | scoped_ptr<RenderPass> removed_pass = render_passes.take(to_remove); |
| 712 | frame->render_passes.erase(to_remove); |
| 713 | frame->render_passes_by_id.erase(remove_render_pass_id); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 714 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 715 | // Now follow up for all RenderPass quads and remove their RenderPasses |
| 716 | // recursively. |
| 717 | const QuadList& quad_list = removed_pass->quad_list; |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 718 | QuadList::ConstBackToFrontIterator quad_list_iterator = |
| 719 | quad_list.BackToFrontBegin(); |
| 720 | for (; quad_list_iterator != quad_list.BackToFrontEnd(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 721 | ++quad_list_iterator) { |
| 722 | DrawQuad* current_quad = (*quad_list_iterator); |
| 723 | if (current_quad->material != DrawQuad::RENDER_PASS) |
| 724 | continue; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 725 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 726 | RenderPass::Id next_remove_render_pass_id = |
| 727 | RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id; |
| 728 | RemoveRenderPassesRecursive(next_remove_render_pass_id, frame); |
| 729 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 730 | } |
| 731 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 732 | bool LayerTreeHostImpl::CullRenderPassesWithCachedTextures:: |
| 733 | ShouldRemoveRenderPass(const RenderPassDrawQuad& quad, |
| 734 | const FrameData& frame) const { |
| 735 | bool quad_has_damage = !quad.contents_changed_since_last_frame.IsEmpty(); |
| 736 | bool quad_has_cached_resource = |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 737 | renderer_->HaveCachedResourcesForRenderPassId(quad.render_pass_id); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 738 | if (quad_has_damage) { |
| 739 | TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have damage"); |
| 740 | return false; |
| 741 | } else if (!quad_has_cached_resource) { |
| 742 | TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures have no texture"); |
| 743 | return false; |
| 744 | } |
| 745 | TRACE_EVENT0("cc", "CullRenderPassesWithCachedTextures dropped!"); |
| 746 | return true; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 747 | } |
| 748 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 749 | bool LayerTreeHostImpl::CullRenderPassesWithNoQuads::ShouldRemoveRenderPass( |
| 750 | const RenderPassDrawQuad& quad, const FrameData& frame) const { |
| 751 | const RenderPass* render_pass = |
| 752 | FindRenderPassById(quad.render_pass_id, frame); |
| 753 | if (!render_pass) |
| 754 | return false; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 755 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 756 | // If any quad or RenderPass draws into this RenderPass, then keep it. |
| 757 | const QuadList& quad_list = render_pass->quad_list; |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 758 | for (QuadList::ConstBackToFrontIterator quad_list_iterator = |
| 759 | quad_list.BackToFrontBegin(); |
| 760 | quad_list_iterator != quad_list.BackToFrontEnd(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 761 | ++quad_list_iterator) { |
| 762 | DrawQuad* current_quad = *quad_list_iterator; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 763 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 764 | if (current_quad->material != DrawQuad::RENDER_PASS) |
| 765 | return false; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 766 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 767 | const RenderPass* contributing_pass = FindRenderPassById( |
| 768 | RenderPassDrawQuad::MaterialCast(current_quad)->render_pass_id, frame); |
| 769 | if (contributing_pass) |
| 770 | return false; |
| 771 | } |
| 772 | return true; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // Defined for linking tests. |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 776 | template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses< |
| 777 | LayerTreeHostImpl::CullRenderPassesWithCachedTextures>( |
| 778 | CullRenderPassesWithCachedTextures culler, FrameData* frame); |
| 779 | template CC_EXPORT void LayerTreeHostImpl::RemoveRenderPasses< |
| 780 | LayerTreeHostImpl::CullRenderPassesWithNoQuads>( |
| 781 | CullRenderPassesWithNoQuads culler, FrameData*); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 782 | |
| 783 | // static |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 784 | template <typename RenderPassCuller> |
| 785 | void LayerTreeHostImpl::RemoveRenderPasses(RenderPassCuller culler, |
| 786 | FrameData* frame) { |
| 787 | for (size_t it = culler.RenderPassListBegin(frame->render_passes); |
| 788 | it != culler.RenderPassListEnd(frame->render_passes); |
| 789 | it = culler.RenderPassListNext(it)) { |
| 790 | const RenderPass* current_pass = frame->render_passes[it]; |
| 791 | const QuadList& quad_list = current_pass->quad_list; |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 792 | QuadList::ConstBackToFrontIterator quad_list_iterator = |
| 793 | quad_list.BackToFrontBegin(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 794 | |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 795 | for (; quad_list_iterator != quad_list.BackToFrontEnd(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 796 | ++quad_list_iterator) { |
| 797 | DrawQuad* current_quad = *quad_list_iterator; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 798 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 799 | if (current_quad->material != DrawQuad::RENDER_PASS) |
| 800 | continue; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 801 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 802 | const RenderPassDrawQuad* render_pass_quad = |
| 803 | RenderPassDrawQuad::MaterialCast(current_quad); |
| 804 | if (!culler.ShouldRemoveRenderPass(*render_pass_quad, *frame)) |
| 805 | continue; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 806 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 807 | // We are changing the vector in the middle of iteration. Because we |
| 808 | // delete render passes that draw into the current pass, we are |
| 809 | // guaranteed that any data from the iterator to the end will not |
| 810 | // change. So, capture the iterator position from the end of the |
| 811 | // list, and restore it after the change. |
| 812 | size_t position_from_end = frame->render_passes.size() - it; |
| 813 | RemoveRenderPassesRecursive(render_pass_quad->render_pass_id, frame); |
| 814 | it = frame->render_passes.size() - position_from_end; |
| 815 | DCHECK_GE(frame->render_passes.size(), position_from_end); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 816 | } |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 817 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 818 | } |
| 819 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 820 | bool LayerTreeHostImpl::PrepareToDraw(FrameData* frame) { |
| 821 | TRACE_EVENT0("cc", "LayerTreeHostImpl::PrepareToDraw"); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 822 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 823 | active_tree_->UpdateDrawProperties( |
| 824 | LayerTreeImpl::UPDATE_ACTIVE_TREE_FOR_DRAW); |
[email protected] | 2e7ca42 | 2012-12-20 02:57:27 | [diff] [blame] | 825 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 826 | frame->render_surface_layer_list = &active_tree_->RenderSurfaceLayerList(); |
| 827 | frame->render_passes.clear(); |
| 828 | frame->render_passes_by_id.clear(); |
| 829 | frame->will_draw_layers.clear(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 830 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 831 | if (!CalculateRenderPasses(frame)) |
| 832 | return false; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 833 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 834 | // If we return true, then we expect DrawLayers() to be called before this |
| 835 | // function is called again. |
| 836 | return true; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 837 | } |
| 838 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 839 | void LayerTreeHostImpl::EnforceManagedMemoryPolicy( |
| 840 | const ManagedMemoryPolicy& policy) { |
| 841 | bool evicted_resources = client_->ReduceContentsTextureMemoryOnImplThread( |
[email protected] | 46b8acc | 2013-03-19 22:38:35 | [diff] [blame] | 842 | visible_ ? policy.bytes_limit_when_visible |
| 843 | : policy.bytes_limit_when_not_visible, |
| 844 | ManagedMemoryPolicy::PriorityCutoffToValue( |
| 845 | visible_ ? policy.priority_cutoff_when_visible |
| 846 | : policy.priority_cutoff_when_not_visible)); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 847 | if (evicted_resources) { |
| 848 | active_tree_->SetContentsTexturesPurged(); |
| 849 | if (pending_tree_) |
| 850 | pending_tree_->SetContentsTexturesPurged(); |
| 851 | client_->SetNeedsCommitOnImplThread(); |
| 852 | client_->OnCanDrawStateChanged(CanDraw()); |
| 853 | client_->RenewTreePriority(); |
| 854 | } |
| 855 | client_->SendManagedMemoryStats(); |
[email protected] | 8947cbe | 2012-11-28 05:27:43 | [diff] [blame] | 856 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 857 | if (tile_manager_) { |
| 858 | GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState()); |
| 859 | new_state.memory_limit_in_bytes = visible_ ? |
[email protected] | 46b8acc | 2013-03-19 22:38:35 | [diff] [blame] | 860 | policy.bytes_limit_when_visible : |
| 861 | policy.bytes_limit_when_not_visible; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 862 | new_state.memory_limit_policy = |
[email protected] | 46b8acc | 2013-03-19 22:38:35 | [diff] [blame] | 863 | ManagedMemoryPolicy::PriorityCutoffToTileMemoryLimitPolicy( |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 864 | visible_ ? |
[email protected] | 46b8acc | 2013-03-19 22:38:35 | [diff] [blame] | 865 | policy.priority_cutoff_when_visible : |
| 866 | policy.priority_cutoff_when_not_visible); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 867 | tile_manager_->SetGlobalState(new_state); |
| 868 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 869 | } |
| 870 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 871 | bool LayerTreeHostImpl::HasImplThread() const { |
| 872 | return proxy_->HasImplThread(); |
[email protected] | 61de581 | 2012-11-08 07:03:44 | [diff] [blame] | 873 | } |
| 874 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 875 | void LayerTreeHostImpl::ScheduleManageTiles() { |
| 876 | if (client_) |
| 877 | client_->SetNeedsManageTilesOnImplThread(); |
[email protected] | 8947cbe | 2012-11-28 05:27:43 | [diff] [blame] | 878 | } |
| 879 | |
[email protected] | 8612679 | 2013-03-16 20:07:54 | [diff] [blame] | 880 | void LayerTreeHostImpl::DidInitializeVisibleTile() { |
| 881 | // TODO(reveman): Determine tiles that changed and only damage |
| 882 | // what's necessary. |
| 883 | SetFullRootLayerDamage(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 884 | if (client_) |
[email protected] | 8612679 | 2013-03-16 20:07:54 | [diff] [blame] | 885 | client_->DidInitializeVisibleTileOnImplThread(); |
[email protected] | 74d9063c | 2013-01-18 03:14:47 | [diff] [blame] | 886 | } |
| 887 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 888 | bool LayerTreeHostImpl::ShouldClearRootRenderPass() const { |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 889 | return settings_.should_clear_root_render_pass; |
[email protected] | f35e232 | 2012-12-15 21:45:52 | [diff] [blame] | 890 | } |
| 891 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 892 | void LayerTreeHostImpl::SetManagedMemoryPolicy( |
| 893 | const ManagedMemoryPolicy& policy) { |
| 894 | if (managed_memory_policy_ == policy) |
| 895 | return; |
[email protected] | 61de581 | 2012-11-08 07:03:44 | [diff] [blame] | 896 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 897 | managed_memory_policy_ = policy; |
| 898 | if (!proxy_->HasImplThread()) { |
| 899 | // TODO(ccameron): In single-thread mode, this can be called on the main |
| 900 | // thread by GLRenderer::OnMemoryAllocationChanged. |
| 901 | DebugScopedSetImplThread impl_thread(proxy_); |
| 902 | EnforceManagedMemoryPolicy(managed_memory_policy_); |
| 903 | } else { |
| 904 | DCHECK(proxy_->IsImplThread()); |
| 905 | EnforceManagedMemoryPolicy(managed_memory_policy_); |
| 906 | } |
| 907 | // We always need to commit after changing the memory policy because the new |
| 908 | // limit can result in more or less content having texture allocated for it. |
| 909 | client_->SetNeedsCommitOnImplThread(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 910 | } |
| 911 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 912 | void LayerTreeHostImpl::OnVSyncParametersChanged(base::TimeTicks timebase, |
| 913 | base::TimeDelta interval) { |
| 914 | client_->OnVSyncParametersChanged(timebase, interval); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 915 | } |
| 916 | |
[email protected] | 7ed4751 | 2013-03-26 22:28:45 | [diff] [blame] | 917 | void LayerTreeHostImpl::DidVSync(base::TimeTicks frame_time) { |
| 918 | client_->DidVSync(frame_time); |
| 919 | } |
| 920 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 921 | void LayerTreeHostImpl::OnSendFrameToParentCompositorAck( |
| 922 | const CompositorFrameAck& ack) { |
| 923 | if (!renderer_) |
| 924 | return; |
[email protected] | b6f3d7e | 2012-12-08 00:11:21 | [diff] [blame] | 925 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 926 | // TODO(piman): We may need to do some validation on this ack before |
| 927 | // processing it. |
| 928 | renderer_->ReceiveCompositorFrameAck(ack); |
[email protected] | a46f3293 | 2012-12-07 21:43:16 | [diff] [blame] | 929 | } |
| 930 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 931 | void LayerTreeHostImpl::OnCanDrawStateChangedForTree() { |
| 932 | client_->OnCanDrawStateChanged(CanDraw()); |
[email protected] | 3b31c6ac | 2012-12-06 21:27:29 | [diff] [blame] | 933 | } |
| 934 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 935 | CompositorFrameMetadata LayerTreeHostImpl::MakeCompositorFrameMetadata() const { |
| 936 | CompositorFrameMetadata metadata; |
| 937 | metadata.device_scale_factor = device_scale_factor_; |
| 938 | metadata.page_scale_factor = active_tree_->total_page_scale_factor(); |
| 939 | metadata.viewport_size = active_tree_->ScrollableViewportSize(); |
| 940 | metadata.root_layer_size = active_tree_->ScrollableSize(); |
| 941 | metadata.min_page_scale_factor = active_tree_->min_page_scale_factor(); |
| 942 | metadata.max_page_scale_factor = active_tree_->max_page_scale_factor(); |
| 943 | if (top_controls_manager_) { |
| 944 | metadata.location_bar_offset = |
| 945 | gfx::Vector2dF(0.f, top_controls_manager_->controls_top_offset()); |
| 946 | metadata.location_bar_content_translation = |
| 947 | gfx::Vector2dF(0.f, top_controls_manager_->content_top_offset()); |
[email protected] | 5a54b282 | 2013-03-26 10:00:01 | [diff] [blame] | 948 | metadata.overdraw_bottom_height = overdraw_bottom_height_; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 949 | } |
[email protected] | bf189f6 | 2012-12-18 03:42:11 | [diff] [blame] | 950 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 951 | if (!RootScrollLayer()) |
[email protected] | bf189f6 | 2012-12-18 03:42:11 | [diff] [blame] | 952 | return metadata; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 953 | |
[email protected] | ffb2720f | 2013-03-15 19:18:37 | [diff] [blame] | 954 | metadata.root_scroll_offset = RootScrollLayer()->TotalScrollOffset(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 955 | |
| 956 | return metadata; |
[email protected] | bf189f6 | 2012-12-18 03:42:11 | [diff] [blame] | 957 | } |
| 958 | |
[email protected] | f0c2a24 | 2013-03-15 19:34:52 | [diff] [blame] | 959 | void LayerTreeHostImpl::DrawLayers(FrameData* frame, |
| 960 | base::TimeTicks frame_begin_time) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 961 | TRACE_EVENT0("cc", "LayerTreeHostImpl::DrawLayers"); |
| 962 | DCHECK(CanDraw()); |
| 963 | DCHECK(!frame->render_passes.empty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 964 | |
[email protected] | 9e359452 | 2013-03-18 00:57:36 | [diff] [blame] | 965 | fps_counter_->SaveTimeStamp(frame_begin_time); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 966 | |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 967 | rendering_stats_instrumentation_->SetScreenFrameCount( |
| 968 | fps_counter_->current_frame_number()); |
| 969 | rendering_stats_instrumentation_->SetDroppedFrameCount( |
| 970 | fps_counter_->dropped_frame_count()); |
| 971 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 972 | if (tile_manager_) { |
| 973 | memory_history_->SaveEntry( |
| 974 | tile_manager_->memory_stats_from_last_assign()); |
| 975 | } |
[email protected] | 1191d9d | 2013-02-02 06:00:33 | [diff] [blame] | 976 | |
[email protected] | 846f455b | 2013-03-18 19:07:41 | [diff] [blame] | 977 | if (debug_state_.ShowHudRects()) { |
[email protected] | d3599278 | 2013-03-14 14:54:02 | [diff] [blame] | 978 | debug_rect_history_->SaveDebugRectsForCurrentFrame( |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 979 | active_tree_->root_layer(), |
| 980 | *frame->render_surface_layer_list, |
| 981 | frame->occluding_screen_space_rects, |
| 982 | frame->non_occluding_screen_space_rects, |
| 983 | debug_state_); |
| 984 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 985 | |
[email protected] | a848c10 | 2013-03-26 08:59:09 | [diff] [blame] | 986 | if (!settings_.impl_side_painting && debug_state_.continuous_painting) { |
| 987 | const RenderingStats& stats = |
| 988 | rendering_stats_instrumentation_->GetRenderingStats(); |
| 989 | paint_time_counter_->SavePaintTime(stats.total_paint_time); |
| 990 | } |
| 991 | |
[email protected] | 846f455b | 2013-03-18 19:07:41 | [diff] [blame] | 992 | if (debug_state_.trace_all_rendered_frames) { |
[email protected] | c76faea | 2013-03-26 07:42:42 | [diff] [blame] | 993 | TRACE_EVENT_INSTANT1("cc.debug", "Frame", TRACE_EVENT_SCOPE_THREAD, |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 994 | "frame", ValueToString(FrameStateAsValue())); |
| 995 | } |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 996 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 997 | // Because the contents of the HUD depend on everything else in the frame, the |
| 998 | // contents of its texture are updated as the last thing before the frame is |
| 999 | // drawn. |
| 1000 | if (active_tree_->hud_layer()) |
[email protected] | 264dc033 | 2013-03-17 21:00:54 | [diff] [blame] | 1001 | active_tree_->hud_layer()->UpdateHudTexture(resource_provider_.get()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1002 | |
[email protected] | d002dd0 | 2013-03-27 07:40:40 | [diff] [blame] | 1003 | renderer_->DrawFrame(&frame->render_passes); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1004 | // The render passes should be consumed by the renderer. |
| 1005 | DCHECK(frame->render_passes.empty()); |
| 1006 | frame->render_passes_by_id.clear(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1007 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1008 | // The next frame should start by assuming nothing has changed, and changes |
| 1009 | // are noted as they occur. |
[email protected] | 264dc033 | 2013-03-17 21:00:54 | [diff] [blame] | 1010 | for (size_t i = 0; i < frame->render_surface_layer_list->size(); i++) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1011 | (*frame->render_surface_layer_list)[i]->render_surface()->damage_tracker()-> |
| 1012 | DidDrawDamagedArea(); |
| 1013 | } |
| 1014 | active_tree_->root_layer()->ResetAllChangeTrackingForSubtree(); |
| 1015 | UpdateAnimationState(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1016 | } |
| 1017 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1018 | void LayerTreeHostImpl::DidDrawAllLayers(const FrameData& frame) { |
| 1019 | for (size_t i = 0; i < frame.will_draw_layers.size(); ++i) |
| 1020 | frame.will_draw_layers[i]->DidDraw(resource_provider_.get()); |
[email protected] | b914e10 | 2012-10-02 08:11:52 | [diff] [blame] | 1021 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1022 | // Once all layers have been drawn, pending texture uploads should no |
| 1023 | // longer block future uploads. |
| 1024 | resource_provider_->MarkPendingUploadsAsNonBlocking(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1025 | } |
| 1026 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1027 | void LayerTreeHostImpl::FinishAllRendering() { |
| 1028 | if (renderer_) |
| 1029 | renderer_->Finish(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1030 | } |
| 1031 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1032 | bool LayerTreeHostImpl::IsContextLost() { |
| 1033 | DCHECK(proxy_->IsImplThread()); |
| 1034 | return renderer_ && renderer_->IsContextLost(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1035 | } |
| 1036 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1037 | const RendererCapabilities& LayerTreeHostImpl::GetRendererCapabilities() const { |
| 1038 | return renderer_->Capabilities(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1039 | } |
| 1040 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1041 | bool LayerTreeHostImpl::SwapBuffers() { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1042 | return renderer_->SwapBuffers(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1043 | } |
| 1044 | |
[email protected] | 7ed4751 | 2013-03-26 22:28:45 | [diff] [blame] | 1045 | void LayerTreeHostImpl::EnableVSyncNotification(bool enable) { |
| 1046 | if (output_surface_) |
| 1047 | output_surface_->EnableVSyncNotification(enable); |
| 1048 | } |
| 1049 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1050 | gfx::Size LayerTreeHostImpl::DeviceViewportSize() const { |
| 1051 | return device_viewport_size(); |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 1052 | } |
| 1053 | |
[email protected] | ffb2720f | 2013-03-15 19:18:37 | [diff] [blame] | 1054 | gfx::SizeF LayerTreeHostImpl::VisibleViewportSize() const { |
| 1055 | gfx::SizeF dip_size = |
| 1056 | gfx::ScaleSize(DeviceViewportSize(), 1.f / device_scale_factor()); |
| 1057 | |
| 1058 | // The clip layer should be used if non-overlay scrollbars may exist since |
| 1059 | // it adjusts for them. |
| 1060 | LayerImpl* clip_layer = active_tree_->RootClipLayer(); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 1061 | if (!Settings().solid_color_scrollbars && clip_layer && |
[email protected] | ffb2720f | 2013-03-15 19:18:37 | [diff] [blame] | 1062 | clip_layer->masks_to_bounds()) |
| 1063 | dip_size = clip_layer->bounds(); |
| 1064 | |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1065 | float top_offset = |
[email protected] | ffb2720f | 2013-03-15 19:18:37 | [diff] [blame] | 1066 | top_controls_manager_ ? top_controls_manager_->content_top_offset() : 0.f; |
[email protected] | d908376 | 2013-03-24 01:36:40 | [diff] [blame] | 1067 | return gfx::SizeF(dip_size.width(), |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1068 | dip_size.height() - top_offset - overdraw_bottom_height_); |
[email protected] | ffb2720f | 2013-03-15 19:18:37 | [diff] [blame] | 1069 | } |
| 1070 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1071 | const LayerTreeSettings& LayerTreeHostImpl::Settings() const { |
| 1072 | return settings(); |
[email protected] | 49306751 | 2012-09-19 23:34:10 | [diff] [blame] | 1073 | } |
| 1074 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1075 | void LayerTreeHostImpl::DidLoseOutputSurface() { |
| 1076 | client_->DidLoseOutputSurfaceOnImplThread(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1077 | } |
| 1078 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1079 | void LayerTreeHostImpl::OnSwapBuffersComplete() { |
| 1080 | client_->OnSwapBuffersCompleteOnImplThread(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1081 | } |
| 1082 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1083 | void LayerTreeHostImpl::Readback(void* pixels, |
| 1084 | gfx::Rect rect_in_device_viewport) { |
| 1085 | DCHECK(renderer_); |
| 1086 | renderer_->GetFramebufferPixels(pixels, rect_in_device_viewport); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1087 | } |
| 1088 | |
[email protected] | 69b50ec | 2013-01-19 04:58:01 | [diff] [blame] | 1089 | bool LayerTreeHostImpl::haveRootScrollLayer() const { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1090 | return RootScrollLayer(); |
[email protected] | 69b50ec | 2013-01-19 04:58:01 | [diff] [blame] | 1091 | } |
| 1092 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1093 | LayerImpl* LayerTreeHostImpl::RootLayer() const { |
| 1094 | return active_tree_->root_layer(); |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 1095 | } |
| 1096 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1097 | LayerImpl* LayerTreeHostImpl::RootScrollLayer() const { |
| 1098 | return active_tree_->RootScrollLayer(); |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 1099 | } |
| 1100 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1101 | LayerImpl* LayerTreeHostImpl::CurrentlyScrollingLayer() const { |
| 1102 | return active_tree_->CurrentlyScrollingLayer(); |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 1103 | } |
| 1104 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1105 | // Content layers can be either directly scrollable or contained in an outer |
| 1106 | // scrolling layer which applies the scroll transform. Given a content layer, |
| 1107 | // this function returns the associated scroll layer if any. |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1108 | static LayerImpl* FindScrollLayerForContentLayer(LayerImpl* layer_impl) { |
| 1109 | if (!layer_impl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1110 | return 0; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1111 | |
| 1112 | if (layer_impl->scrollable()) |
| 1113 | return layer_impl; |
| 1114 | |
| 1115 | if (layer_impl->DrawsContent() && |
| 1116 | layer_impl->parent() && |
| 1117 | layer_impl->parent()->scrollable()) |
| 1118 | return layer_impl->parent(); |
| 1119 | |
| 1120 | return 0; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1121 | } |
| 1122 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1123 | void LayerTreeHostImpl::CreatePendingTree() { |
| 1124 | CHECK(!pending_tree_); |
| 1125 | if (recycle_tree_) |
| 1126 | recycle_tree_.swap(pending_tree_); |
| 1127 | else |
| 1128 | pending_tree_ = LayerTreeImpl::create(this); |
| 1129 | client_->OnCanDrawStateChanged(CanDraw()); |
| 1130 | client_->OnHasPendingTreeStateChanged(pending_tree_); |
| 1131 | TRACE_EVENT_ASYNC_BEGIN0("cc", "PendingTree", pending_tree_.get()); |
| 1132 | TRACE_EVENT_ASYNC_STEP0("cc", |
| 1133 | "PendingTree", pending_tree_.get(), "waiting"); |
[email protected] | 2e7ca42 | 2012-12-20 02:57:27 | [diff] [blame] | 1134 | } |
| 1135 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1136 | void LayerTreeHostImpl::CheckForCompletedTileUploads() { |
| 1137 | DCHECK(!client_->IsInsideDraw()) << |
| 1138 | "Checking for completed uploads within a draw may trigger " |
| 1139 | "spurious redraws."; |
| 1140 | if (tile_manager_) |
| 1141 | tile_manager_->CheckForCompletedTileUploads(); |
[email protected] | eabe500 | 2013-01-12 22:07:48 | [diff] [blame] | 1142 | } |
| 1143 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1144 | bool LayerTreeHostImpl::ActivatePendingTreeIfNeeded() { |
| 1145 | if (!pending_tree_) |
| 1146 | return false; |
[email protected] | 2e7ca42 | 2012-12-20 02:57:27 | [diff] [blame] | 1147 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1148 | CHECK(tile_manager_); |
[email protected] | 2ae038b | 2013-01-28 12:52:09 | [diff] [blame] | 1149 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1150 | pending_tree_->UpdateDrawProperties(LayerTreeImpl::UPDATE_PENDING_TREE); |
[email protected] | 615c78a | 2013-01-24 23:44:16 | [diff] [blame] | 1151 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1152 | TRACE_EVENT_ASYNC_STEP1("cc", |
| 1153 | "PendingTree", pending_tree_.get(), "activate", |
| 1154 | "state", ValueToString(ActivationStateAsValue())); |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 1155 | |
[email protected] | ed25b44 | 2013-03-26 21:52:40 | [diff] [blame] | 1156 | // Activate once all visible resources in pending tree are ready |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1157 | // or tile manager has no work scheduled for pending tree. |
[email protected] | ed25b44 | 2013-03-26 21:52:40 | [diff] [blame] | 1158 | if (!pending_tree_->AreVisibleResourcesReady()) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1159 | // In smoothness takes priority mode, the pending tree's priorities are |
| 1160 | // ignored, so the tile manager may not have work for it even though it |
| 1161 | // is simultaneously not ready to be activated. |
| 1162 | if (tile_manager_->GlobalState().tree_priority == |
| 1163 | SMOOTHNESS_TAKES_PRIORITY || |
| 1164 | tile_manager_->HasPendingWorkScheduled(PENDING_TREE)) { |
| 1165 | TRACE_EVENT_ASYNC_STEP0("cc", |
| 1166 | "PendingTree", |
| 1167 | pending_tree_.get(), |
| 1168 | "waiting"); |
| 1169 | return false; |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 1170 | } |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1171 | } |
[email protected] | 2e7ca42 | 2012-12-20 02:57:27 | [diff] [blame] | 1172 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1173 | ActivatePendingTree(); |
| 1174 | return true; |
[email protected] | 2e7ca42 | 2012-12-20 02:57:27 | [diff] [blame] | 1175 | } |
| 1176 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1177 | void LayerTreeHostImpl::ActivatePendingTree() { |
| 1178 | CHECK(pending_tree_); |
| 1179 | TRACE_EVENT_ASYNC_END0("cc", "PendingTree", pending_tree_.get()); |
[email protected] | 1e0f8d6 | 2013-01-09 07:41:35 | [diff] [blame] | 1180 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1181 | active_tree_->PushPersistedState(pending_tree_.get()); |
| 1182 | if (pending_tree_->needs_full_tree_sync()) { |
| 1183 | active_tree_->SetRootLayer( |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 1184 | TreeSynchronizer::SynchronizeTrees(pending_tree_->root_layer(), |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1185 | active_tree_->DetachLayerTree(), |
| 1186 | active_tree_.get())); |
| 1187 | } |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 1188 | TreeSynchronizer::PushProperties(pending_tree_->root_layer(), |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1189 | active_tree_->root_layer()); |
| 1190 | DCHECK(!recycle_tree_); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 1191 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1192 | pending_tree_->PushPropertiesTo(active_tree_.get()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 1193 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1194 | // Now that we've synced everything from the pending tree to the active |
| 1195 | // tree, rename the pending tree the recycle tree so we can reuse it on the |
| 1196 | // next sync. |
| 1197 | pending_tree_.swap(recycle_tree_); |
| 1198 | recycle_tree_->ClearRenderSurfaces(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 1199 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1200 | active_tree_->DidBecomeActive(); |
[email protected] | 37386f05 | 2013-01-13 00:42:22 | [diff] [blame] | 1201 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1202 | // Reduce wasted memory now that unlinked resources are guaranteed not |
| 1203 | // to be used. |
| 1204 | client_->ReduceWastedContentsTextureMemoryOnImplThread(); |
[email protected] | a0b8417 | 2013-02-04 08:13:41 | [diff] [blame] | 1205 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1206 | client_->OnCanDrawStateChanged(CanDraw()); |
| 1207 | client_->OnHasPendingTreeStateChanged(pending_tree_); |
| 1208 | client_->SetNeedsRedrawOnImplThread(); |
| 1209 | client_->RenewTreePriority(); |
[email protected] | 652cf13 | 2013-02-15 21:53:24 | [diff] [blame] | 1210 | |
[email protected] | a848c10 | 2013-03-26 08:59:09 | [diff] [blame] | 1211 | if (debug_state_.continuous_painting) { |
| 1212 | const RenderingStats& stats = |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 1213 | rendering_stats_instrumentation_->GetRenderingStats(); |
[email protected] | a848c10 | 2013-03-26 08:59:09 | [diff] [blame] | 1214 | paint_time_counter_->SavePaintTime( |
| 1215 | stats.total_paint_time + |
| 1216 | stats.total_rasterize_time_for_now_bins_on_pending_tree); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1217 | } |
[email protected] | 2e7ca42 | 2012-12-20 02:57:27 | [diff] [blame] | 1218 | } |
| 1219 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1220 | void LayerTreeHostImpl::SetVisible(bool visible) { |
| 1221 | DCHECK(proxy_->IsImplThread()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1222 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1223 | if (visible_ == visible) |
| 1224 | return; |
| 1225 | visible_ = visible; |
| 1226 | DidVisibilityChange(this, visible_); |
| 1227 | EnforceManagedMemoryPolicy(managed_memory_policy_); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1228 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1229 | if (!renderer_) |
| 1230 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1231 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1232 | renderer_->SetVisible(visible); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1233 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1234 | SetBackgroundTickingEnabled( |
| 1235 | !visible_ && |
| 1236 | !animation_registrar_->active_animation_controllers().empty()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1237 | } |
| 1238 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1239 | bool LayerTreeHostImpl::InitializeRenderer( |
| 1240 | scoped_ptr<OutputSurface> output_surface) { |
| 1241 | // Since we will create a new resource provider, we cannot continue to use |
| 1242 | // the old resources (i.e. render_surfaces and texture IDs). Clear them |
| 1243 | // before we destroy the old resource provider. |
| 1244 | if (active_tree_->root_layer()) |
| 1245 | ClearRenderSurfaces(); |
| 1246 | if (active_tree_->root_layer()) |
| 1247 | SendDidLoseOutputSurfaceRecursive(active_tree_->root_layer()); |
| 1248 | if (pending_tree_ && pending_tree_->root_layer()) |
| 1249 | SendDidLoseOutputSurfaceRecursive(pending_tree_->root_layer()); |
| 1250 | if (recycle_tree_ && recycle_tree_->root_layer()) |
| 1251 | SendDidLoseOutputSurfaceRecursive(recycle_tree_->root_layer()); |
[email protected] | 45c4b1e | 2013-01-16 02:19:40 | [diff] [blame] | 1252 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1253 | // Note: order is important here. |
| 1254 | renderer_.reset(); |
| 1255 | tile_manager_.reset(); |
| 1256 | resource_provider_.reset(); |
| 1257 | output_surface_.reset(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1258 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1259 | if (!output_surface->BindToClient(this)) |
| 1260 | return false; |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 1261 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1262 | scoped_ptr<ResourceProvider> resource_provider = |
| 1263 | ResourceProvider::Create(output_surface.get()); |
| 1264 | if (!resource_provider) |
| 1265 | return false; |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 1266 | |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 1267 | if (settings_.impl_side_painting) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1268 | tile_manager_.reset(new TileManager(this, |
| 1269 | resource_provider.get(), |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 1270 | settings_.num_raster_threads, |
| 1271 | settings_.use_cheapness_estimator, |
| 1272 | settings_.use_color_estimator, |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 1273 | settings_.prediction_benchmarking, |
| 1274 | rendering_stats_instrumentation_)); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1275 | } |
[email protected] | 8947cbe | 2012-11-28 05:27:43 | [diff] [blame] | 1276 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1277 | if (output_surface->capabilities().has_parent_compositor) { |
| 1278 | renderer_ = DelegatingRenderer::Create(this, output_surface.get(), |
| 1279 | resource_provider.get()); |
| 1280 | } else if (output_surface->context3d()) { |
| 1281 | renderer_ = GLRenderer::Create(this, |
| 1282 | output_surface.get(), |
| 1283 | resource_provider.get()); |
| 1284 | } else if (output_surface->software_device()) { |
| 1285 | renderer_ = SoftwareRenderer::Create(this, |
| 1286 | output_surface.get(), |
| 1287 | resource_provider.get()); |
| 1288 | } |
| 1289 | if (!renderer_) |
| 1290 | return false; |
[email protected] | be318165 | 2012-09-25 13:02:13 | [diff] [blame] | 1291 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1292 | resource_provider_ = resource_provider.Pass(); |
| 1293 | output_surface_ = output_surface.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1294 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1295 | if (!visible_) |
| 1296 | renderer_->SetVisible(visible_); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1297 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1298 | client_->OnCanDrawStateChanged(CanDraw()); |
[email protected] | 8db2213c | 2012-09-05 22:08:21 | [diff] [blame] | 1299 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1300 | // See note in LayerTreeImpl::UpdateDrawProperties. Renderer needs |
| 1301 | // to be initialized to get max texture size. |
| 1302 | active_tree_->set_needs_update_draw_properties(); |
| 1303 | if (pending_tree_) |
| 1304 | pending_tree_->set_needs_update_draw_properties(); |
[email protected] | 615c78a | 2013-01-24 23:44:16 | [diff] [blame] | 1305 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1306 | return true; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1307 | } |
| 1308 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1309 | void LayerTreeHostImpl::SetViewportSize(gfx::Size layout_viewport_size, |
| 1310 | gfx::Size device_viewport_size) { |
| 1311 | if (layout_viewport_size == layout_viewport_size_ && |
| 1312 | device_viewport_size == device_viewport_size_) |
| 1313 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1314 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1315 | if (pending_tree_ && device_viewport_size_ != device_viewport_size) |
| 1316 | active_tree_->SetViewportSizeInvalid(); |
[email protected] | 31882285 | 2013-02-14 00:54:27 | [diff] [blame] | 1317 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1318 | layout_viewport_size_ = layout_viewport_size; |
| 1319 | device_viewport_size_ = device_viewport_size; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1320 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1321 | UpdateMaxScrollOffset(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1322 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1323 | if (renderer_) |
| 1324 | renderer_->ViewportChanged(); |
[email protected] | 8db2213c | 2012-09-05 22:08:21 | [diff] [blame] | 1325 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1326 | client_->OnCanDrawStateChanged(CanDraw()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1327 | } |
| 1328 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1329 | static void AdjustScrollsForPageScaleChange(LayerImpl* layer_impl, |
| 1330 | float page_scale_change) { |
| 1331 | if (!layer_impl) |
| 1332 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1333 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1334 | if (layer_impl->scrollable()) { |
| 1335 | // We need to convert impl-side scroll deltas to page_scale space. |
| 1336 | gfx::Vector2dF scroll_delta = layer_impl->scroll_delta(); |
| 1337 | scroll_delta.Scale(page_scale_change); |
| 1338 | layer_impl->SetScrollDelta(scroll_delta); |
| 1339 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1340 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1341 | for (size_t i = 0; i < layer_impl->children().size(); ++i) |
| 1342 | AdjustScrollsForPageScaleChange(layer_impl->children()[i], |
| 1343 | page_scale_change); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1344 | } |
| 1345 | |
[email protected] | d908376 | 2013-03-24 01:36:40 | [diff] [blame] | 1346 | void LayerTreeHostImpl::SetOverdrawBottomHeight(float overdraw_bottom_height) { |
| 1347 | if (overdraw_bottom_height == overdraw_bottom_height_) |
| 1348 | return; |
| 1349 | overdraw_bottom_height_ = overdraw_bottom_height; |
| 1350 | |
| 1351 | UpdateMaxScrollOffset(); |
| 1352 | } |
| 1353 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1354 | void LayerTreeHostImpl::SetDeviceScaleFactor(float device_scale_factor) { |
| 1355 | if (device_scale_factor == device_scale_factor_) |
| 1356 | return; |
| 1357 | device_scale_factor_ = device_scale_factor; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 1358 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1359 | UpdateMaxScrollOffset(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1360 | } |
| 1361 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1362 | void LayerTreeHostImpl::UpdateMaxScrollOffset() { |
| 1363 | active_tree_->UpdateMaxScrollOffset(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1364 | } |
| 1365 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1366 | void LayerTreeHostImpl::setActiveTreeNeedsUpdateDrawProperties() { |
| 1367 | active_tree_->set_needs_update_draw_properties(); |
[email protected] | 3ba4cae | 2013-01-16 03:58:38 | [diff] [blame] | 1368 | } |
| 1369 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1370 | void LayerTreeHostImpl::setNeedsRedraw() { |
| 1371 | client_->SetNeedsRedrawOnImplThread(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1372 | } |
| 1373 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1374 | bool LayerTreeHostImpl::EnsureRenderSurfaceLayerList() { |
| 1375 | active_tree_->UpdateDrawProperties(LayerTreeImpl::UPDATE_ACTIVE_TREE); |
| 1376 | return active_tree_->RenderSurfaceLayerList().size(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1377 | } |
| 1378 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1379 | InputHandlerClient::ScrollStatus LayerTreeHostImpl::ScrollBegin( |
| 1380 | gfx::Point viewport_point, InputHandlerClient::ScrollInputType type) { |
[email protected] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 1381 | TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBegin"); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1382 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1383 | if (top_controls_manager_) |
| 1384 | top_controls_manager_->ScrollBegin(); |
[email protected] | 3ba4cae | 2013-01-16 03:58:38 | [diff] [blame] | 1385 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1386 | DCHECK(!CurrentlyScrollingLayer()); |
| 1387 | ClearCurrentlyScrollingLayer(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1388 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1389 | if (!EnsureRenderSurfaceLayerList()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1390 | return ScrollIgnored; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1391 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1392 | gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point, |
| 1393 | device_scale_factor_); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1394 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1395 | // First find out which layer was hit from the saved list of visible layers |
| 1396 | // in the most recent frame. |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 1397 | LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint( |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1398 | device_viewport_point, active_tree_->RenderSurfaceLayerList()); |
[email protected] | 31bfe27 | 2012-10-19 18:49:52 | [diff] [blame] | 1399 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1400 | // Walk up the hierarchy and look for a scrollable layer. |
| 1401 | LayerImpl* potentially_scrolling_layer_impl = 0; |
| 1402 | for (; layer_impl; layer_impl = layer_impl->parent()) { |
| 1403 | // The content layer can also block attempts to scroll outside the main |
| 1404 | // thread. |
| 1405 | ScrollStatus status = layer_impl->TryScroll(device_viewport_point, type); |
| 1406 | if (status == ScrollOnMainThread) { |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 1407 | rendering_stats_instrumentation_->IncrementMainThreadScrolls(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1408 | UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true); |
[email protected] | b7c4783f | 2013-03-15 23:11:42 | [diff] [blame] | 1409 | active_tree()->DidBeginScroll(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1410 | return ScrollOnMainThread; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1411 | } |
| 1412 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1413 | LayerImpl* scroll_layer_impl = FindScrollLayerForContentLayer(layer_impl); |
| 1414 | if (!scroll_layer_impl) |
| 1415 | continue; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1416 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1417 | status = scroll_layer_impl->TryScroll(device_viewport_point, type); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1418 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1419 | // If any layer wants to divert the scroll event to the main thread, abort. |
| 1420 | if (status == ScrollOnMainThread) { |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 1421 | rendering_stats_instrumentation_->IncrementMainThreadScrolls(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1422 | UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", true); |
[email protected] | b7c4783f | 2013-03-15 23:11:42 | [diff] [blame] | 1423 | active_tree()->DidBeginScroll(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1424 | return ScrollOnMainThread; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1425 | } |
| 1426 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1427 | if (status == ScrollStarted && !potentially_scrolling_layer_impl) |
| 1428 | potentially_scrolling_layer_impl = scroll_layer_impl; |
| 1429 | } |
| 1430 | |
| 1431 | // When hiding top controls is enabled and the controls are hidden or |
| 1432 | // overlaying the content, force scrolls to be enabled on the root layer to |
| 1433 | // allow bringing the top controls back into view. |
| 1434 | if (!potentially_scrolling_layer_impl && top_controls_manager_ && |
| 1435 | top_controls_manager_->content_top_offset() != |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 1436 | settings_.top_controls_height) { |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1437 | potentially_scrolling_layer_impl = RootScrollLayer(); |
| 1438 | } |
| 1439 | |
| 1440 | if (potentially_scrolling_layer_impl) { |
[email protected] | 0fc818e | 2013-03-18 06:45:20 | [diff] [blame] | 1441 | active_tree_->SetCurrentlyScrollingLayer( |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1442 | potentially_scrolling_layer_impl); |
| 1443 | should_bubble_scrolls_ = (type != NonBubblingGesture); |
| 1444 | wheel_scrolling_ = (type == Wheel); |
[email protected] | 372bad5f | 2013-03-21 16:38:43 | [diff] [blame] | 1445 | rendering_stats_instrumentation_->IncrementImplThreadScrolls(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1446 | client_->RenewTreePriority(); |
| 1447 | UMA_HISTOGRAM_BOOLEAN("TryScroll.SlowScroll", false); |
[email protected] | b7c4783f | 2013-03-15 23:11:42 | [diff] [blame] | 1448 | active_tree()->DidBeginScroll(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1449 | return ScrollStarted; |
| 1450 | } |
| 1451 | return ScrollIgnored; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1452 | } |
| 1453 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1454 | gfx::Vector2dF LayerTreeHostImpl::ScrollLayerWithViewportSpaceDelta( |
| 1455 | LayerImpl* layer_impl, |
| 1456 | float scale_from_viewport_to_screen_space, |
| 1457 | gfx::PointF viewport_point, |
| 1458 | gfx::Vector2dF viewport_delta) { |
| 1459 | // Layers with non-invertible screen space transforms should not have passed |
| 1460 | // the scroll hit test in the first place. |
| 1461 | DCHECK(layer_impl->screen_space_transform().IsInvertible()); |
| 1462 | gfx::Transform inverse_screen_space_transform( |
| 1463 | gfx::Transform::kSkipInitialization); |
| 1464 | bool did_invert = layer_impl->screen_space_transform().GetInverse( |
| 1465 | &inverse_screen_space_transform); |
[email protected] | ca2902e9 | 2013-03-28 01:45:35 | [diff] [blame^] | 1466 | // TODO(shawnsingh): With the advent of impl-side crolling for non-root |
| 1467 | // layers, we may need to explicitly handle uninvertible transforms here. |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1468 | DCHECK(did_invert); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1469 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1470 | gfx::PointF screen_space_point = |
| 1471 | gfx::ScalePoint(viewport_point, scale_from_viewport_to_screen_space); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1472 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1473 | gfx::Vector2dF screen_space_delta = viewport_delta; |
| 1474 | screen_space_delta.Scale(scale_from_viewport_to_screen_space); |
| 1475 | |
| 1476 | // First project the scroll start and end points to local layer space to find |
| 1477 | // the scroll delta in layer coordinates. |
| 1478 | bool start_clipped, end_clipped; |
| 1479 | gfx::PointF screen_space_end_point = screen_space_point + screen_space_delta; |
| 1480 | gfx::PointF local_start_point = |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 1481 | MathUtil::ProjectPoint(inverse_screen_space_transform, |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1482 | screen_space_point, |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 1483 | &start_clipped); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1484 | gfx::PointF local_end_point = |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 1485 | MathUtil::ProjectPoint(inverse_screen_space_transform, |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1486 | screen_space_end_point, |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 1487 | &end_clipped); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1488 | |
| 1489 | // In general scroll point coordinates should not get clipped. |
| 1490 | DCHECK(!start_clipped); |
| 1491 | DCHECK(!end_clipped); |
| 1492 | if (start_clipped || end_clipped) |
| 1493 | return gfx::Vector2dF(); |
| 1494 | |
| 1495 | // local_start_point and local_end_point are in content space but we want to |
| 1496 | // move them to layer space for scrolling. |
| 1497 | float width_scale = 1.f / layer_impl->contents_scale_x(); |
| 1498 | float height_scale = 1.f / layer_impl->contents_scale_y(); |
| 1499 | local_start_point.Scale(width_scale, height_scale); |
| 1500 | local_end_point.Scale(width_scale, height_scale); |
| 1501 | |
| 1502 | // Apply the scroll delta. |
| 1503 | gfx::Vector2dF previous_delta = layer_impl->scroll_delta(); |
| 1504 | layer_impl->ScrollBy(local_end_point - local_start_point); |
| 1505 | |
| 1506 | // Get the end point in the layer's content space so we can apply its |
| 1507 | // ScreenSpaceTransform. |
| 1508 | gfx::PointF actual_local_end_point = local_start_point + |
| 1509 | layer_impl->scroll_delta() - |
| 1510 | previous_delta; |
| 1511 | gfx::PointF actual_local_content_end_point = |
| 1512 | gfx::ScalePoint(actual_local_end_point, |
| 1513 | 1.f / width_scale, |
| 1514 | 1.f / height_scale); |
| 1515 | |
| 1516 | // Calculate the applied scroll delta in viewport space coordinates. |
| 1517 | gfx::PointF actual_screen_space_end_point = |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 1518 | MathUtil::MapPoint(layer_impl->screen_space_transform(), |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1519 | actual_local_content_end_point, |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 1520 | &end_clipped); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1521 | DCHECK(!end_clipped); |
| 1522 | if (end_clipped) |
| 1523 | return gfx::Vector2dF(); |
| 1524 | gfx::PointF actual_viewport_end_point = |
| 1525 | gfx::ScalePoint(actual_screen_space_end_point, |
| 1526 | 1.f / scale_from_viewport_to_screen_space); |
| 1527 | return actual_viewport_end_point - viewport_point; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1528 | } |
| 1529 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1530 | static gfx::Vector2dF ScrollLayerWithLocalDelta(LayerImpl* layer_impl, |
| 1531 | gfx::Vector2dF local_delta) { |
| 1532 | gfx::Vector2dF previous_delta(layer_impl->scroll_delta()); |
| 1533 | layer_impl->ScrollBy(local_delta); |
| 1534 | return layer_impl->scroll_delta() - previous_delta; |
| 1535 | } |
| 1536 | |
| 1537 | bool LayerTreeHostImpl::ScrollBy(gfx::Point viewport_point, |
| 1538 | gfx::Vector2dF scroll_delta) { |
| 1539 | TRACE_EVENT0("cc", "LayerTreeHostImpl::ScrollBy"); |
| 1540 | if (!CurrentlyScrollingLayer()) |
| 1541 | return false; |
| 1542 | |
| 1543 | gfx::Vector2dF pending_delta = scroll_delta; |
| 1544 | bool did_scroll = false; |
[email protected] | 60b4d25 | 2013-03-23 18:49:42 | [diff] [blame] | 1545 | bool consume_by_top_controls = top_controls_manager_ && |
| 1546 | (CurrentlyScrollingLayer() == RootScrollLayer() || scroll_delta.y() < 0); |
[email protected] | a91e4f8 | 2013-03-15 06:58:06 | [diff] [blame] | 1547 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1548 | for (LayerImpl* layer_impl = CurrentlyScrollingLayer(); |
| 1549 | layer_impl; |
| 1550 | layer_impl = layer_impl->parent()) { |
| 1551 | if (!layer_impl->scrollable()) |
| 1552 | continue; |
| 1553 | |
[email protected] | 60b4d25 | 2013-03-23 18:49:42 | [diff] [blame] | 1554 | // Only allow bubble scrolling when the scroll is in the direction to make |
| 1555 | // the top controls visible. |
| 1556 | if (consume_by_top_controls && layer_impl == RootScrollLayer()) { |
| 1557 | pending_delta = top_controls_manager_->ScrollBy(pending_delta); |
| 1558 | UpdateMaxScrollOffset(); |
| 1559 | } |
| 1560 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1561 | gfx::Vector2dF applied_delta; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1562 | // Gesture events need to be transformed from viewport coordinates to local |
| 1563 | // layer coordinates so that the scrolling contents exactly follow the |
| 1564 | // user's finger. In contrast, wheel events represent a fixed amount of |
| 1565 | // scrolling so we can just apply them directly. |
| 1566 | if (!wheel_scrolling_) { |
| 1567 | float scale_from_viewport_to_screen_space = device_scale_factor_; |
| 1568 | applied_delta = |
| 1569 | ScrollLayerWithViewportSpaceDelta(layer_impl, |
| 1570 | scale_from_viewport_to_screen_space, |
| 1571 | viewport_point, pending_delta); |
| 1572 | } else { |
| 1573 | applied_delta = ScrollLayerWithLocalDelta(layer_impl, pending_delta); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1574 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1575 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1576 | // If the layer wasn't able to move, try the next one in the hierarchy. |
| 1577 | float move_threshold_squared = 0.1f * 0.1f; |
| 1578 | if (applied_delta.LengthSquared() < move_threshold_squared) { |
| 1579 | if (should_bubble_scrolls_ || !did_lock_scrolling_layer_) |
| 1580 | continue; |
| 1581 | else |
| 1582 | break; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1583 | } |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1584 | did_scroll = true; |
| 1585 | did_lock_scrolling_layer_ = true; |
| 1586 | if (!should_bubble_scrolls_) { |
[email protected] | 0fc818e | 2013-03-18 06:45:20 | [diff] [blame] | 1587 | active_tree_->SetCurrentlyScrollingLayer(layer_impl); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1588 | break; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1589 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1590 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1591 | // If the applied delta is within 45 degrees of the input delta, bail out to |
| 1592 | // make it easier to scroll just one layer in one direction without |
| 1593 | // affecting any of its parents. |
| 1594 | float angle_threshold = 45; |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 1595 | if (MathUtil::SmallestAngleBetweenVectors( |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1596 | applied_delta, pending_delta) < angle_threshold) { |
| 1597 | pending_delta = gfx::Vector2d(); |
| 1598 | break; |
[email protected] | 4a23c374c | 2012-12-08 08:38:55 | [diff] [blame] | 1599 | } |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1600 | |
| 1601 | // Allow further movement only on an axis perpendicular to the direction in |
| 1602 | // which the layer moved. |
| 1603 | gfx::Vector2dF perpendicular_axis(-applied_delta.y(), applied_delta.x()); |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 1604 | pending_delta = MathUtil::ProjectVector(pending_delta, perpendicular_axis); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1605 | |
| 1606 | if (gfx::ToFlooredVector2d(pending_delta).IsZero()) |
| 1607 | break; |
| 1608 | } |
| 1609 | |
[email protected] | b7c4783f | 2013-03-15 23:11:42 | [diff] [blame] | 1610 | active_tree()->DidUpdateScroll(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1611 | if (did_scroll) { |
| 1612 | client_->SetNeedsCommitOnImplThread(); |
| 1613 | client_->SetNeedsRedrawOnImplThread(); |
| 1614 | client_->RenewTreePriority(); |
| 1615 | } |
| 1616 | return did_scroll; |
[email protected] | 4a23c374c | 2012-12-08 08:38:55 | [diff] [blame] | 1617 | } |
| 1618 | |
[email protected] | be782f5 | 2013-03-23 21:36:14 | [diff] [blame] | 1619 | // This implements scrolling by page as described here: |
| 1620 | // http://msdn.microsoft.com/en-us/library/windows/desktop/ms645601(v=vs.85).aspx#_win32_The_Mouse_Wheel |
| 1621 | // for events with WHEEL_PAGESCROLL set. |
| 1622 | bool LayerTreeHostImpl::ScrollVerticallyByPage( |
| 1623 | gfx::Point viewport_point, |
| 1624 | WebKit::WebScrollbar::ScrollDirection direction) { |
| 1625 | DCHECK(wheel_scrolling_); |
| 1626 | |
| 1627 | for (LayerImpl* layer_impl = CurrentlyScrollingLayer(); |
| 1628 | layer_impl; |
| 1629 | layer_impl = layer_impl->parent()) { |
| 1630 | if (!layer_impl->scrollable()) |
| 1631 | continue; |
| 1632 | |
| 1633 | if (!layer_impl->vertical_scrollbar_layer()) |
| 1634 | continue; |
| 1635 | |
| 1636 | float height = layer_impl->vertical_scrollbar_layer()->bounds().height(); |
| 1637 | |
| 1638 | // These magical values match WebKit and are designed to scroll nearly the |
| 1639 | // entire visible content height but leave a bit of overlap. |
| 1640 | float page = std::max(height * 0.875f, 1.f); |
| 1641 | if (direction == WebKit::WebScrollbar::ScrollBackward) |
| 1642 | page = -page; |
| 1643 | |
| 1644 | gfx::Vector2dF delta = gfx::Vector2dF(0.f, page); |
| 1645 | |
| 1646 | gfx::Vector2dF applied_delta = ScrollLayerWithLocalDelta(layer_impl, delta); |
| 1647 | |
| 1648 | if (!applied_delta.IsZero()) { |
| 1649 | active_tree()->DidUpdateScroll(); |
| 1650 | client_->SetNeedsCommitOnImplThread(); |
| 1651 | client_->SetNeedsRedrawOnImplThread(); |
| 1652 | client_->RenewTreePriority(); |
| 1653 | return true; |
| 1654 | } |
| 1655 | |
| 1656 | active_tree_->SetCurrentlyScrollingLayer(layer_impl); |
| 1657 | } |
| 1658 | |
| 1659 | return false; |
| 1660 | } |
| 1661 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1662 | void LayerTreeHostImpl::ClearCurrentlyScrollingLayer() { |
| 1663 | active_tree_->ClearCurrentlyScrollingLayer(); |
| 1664 | did_lock_scrolling_layer_ = false; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1665 | } |
| 1666 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1667 | void LayerTreeHostImpl::ScrollEnd() { |
| 1668 | if (top_controls_manager_) |
| 1669 | top_controls_manager_->ScrollEnd(); |
| 1670 | ClearCurrentlyScrollingLayer(); |
[email protected] | b7c4783f | 2013-03-15 23:11:42 | [diff] [blame] | 1671 | active_tree()->DidEndScroll(); |
[email protected] | 0fc818e | 2013-03-18 06:45:20 | [diff] [blame] | 1672 | StartScrollbarAnimation(base::TimeTicks::Now()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1673 | } |
| 1674 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1675 | void LayerTreeHostImpl::PinchGestureBegin() { |
| 1676 | pinch_gesture_active_ = true; |
| 1677 | previous_pinch_anchor_ = gfx::Point(); |
| 1678 | client_->RenewTreePriority(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1679 | } |
| 1680 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1681 | void LayerTreeHostImpl::PinchGestureUpdate(float magnify_delta, |
| 1682 | gfx::Point anchor) { |
| 1683 | TRACE_EVENT0("cc", "LayerTreeHostImpl::PinchGestureUpdate"); |
[email protected] | d3afa11 | 2012-12-08 06:24:28 | [diff] [blame] | 1684 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1685 | if (!RootScrollLayer()) |
| 1686 | return; |
[email protected] | d3afa11 | 2012-12-08 06:24:28 | [diff] [blame] | 1687 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1688 | // Keep the center-of-pinch anchor specified by (x, y) in a stable |
| 1689 | // position over the course of the magnify. |
| 1690 | float page_scale_delta = active_tree_->page_scale_delta(); |
| 1691 | gfx::PointF previous_scale_anchor = |
| 1692 | gfx::ScalePoint(anchor, 1.f / page_scale_delta); |
| 1693 | active_tree_->SetPageScaleDelta(page_scale_delta * magnify_delta); |
| 1694 | page_scale_delta = active_tree_->page_scale_delta(); |
| 1695 | gfx::PointF new_scale_anchor = |
| 1696 | gfx::ScalePoint(anchor, 1.f / page_scale_delta); |
| 1697 | gfx::Vector2dF move = previous_scale_anchor - new_scale_anchor; |
| 1698 | |
| 1699 | previous_pinch_anchor_ = anchor; |
| 1700 | |
| 1701 | move.Scale(1 / active_tree_->page_scale_factor()); |
| 1702 | |
| 1703 | RootScrollLayer()->ScrollBy(move); |
| 1704 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1705 | client_->SetNeedsCommitOnImplThread(); |
| 1706 | client_->SetNeedsRedrawOnImplThread(); |
| 1707 | client_->RenewTreePriority(); |
[email protected] | d3afa11 | 2012-12-08 06:24:28 | [diff] [blame] | 1708 | } |
| 1709 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1710 | void LayerTreeHostImpl::PinchGestureEnd() { |
| 1711 | pinch_gesture_active_ = false; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1712 | client_->SetNeedsCommitOnImplThread(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1713 | } |
| 1714 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1715 | static void CollectScrollDeltas(ScrollAndScaleSet* scroll_info, |
| 1716 | LayerImpl* layer_impl) { |
| 1717 | if (!layer_impl) |
| 1718 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1719 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1720 | gfx::Vector2d scroll_delta = |
| 1721 | gfx::ToFlooredVector2d(layer_impl->scroll_delta()); |
| 1722 | if (!scroll_delta.IsZero()) { |
| 1723 | LayerTreeHostCommon::ScrollUpdateInfo scroll; |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 1724 | scroll.layer_id = layer_impl->id(); |
| 1725 | scroll.scroll_delta = scroll_delta; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1726 | scroll_info->scrolls.push_back(scroll); |
| 1727 | layer_impl->SetSentScrollDelta(scroll_delta); |
| 1728 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1729 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1730 | for (size_t i = 0; i < layer_impl->children().size(); ++i) |
| 1731 | CollectScrollDeltas(scroll_info, layer_impl->children()[i]); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1732 | } |
| 1733 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1734 | scoped_ptr<ScrollAndScaleSet> LayerTreeHostImpl::ProcessScrollDeltas() { |
| 1735 | scoped_ptr<ScrollAndScaleSet> scroll_info(new ScrollAndScaleSet()); |
[email protected] | 362f1e8b | 2013-01-21 16:54:30 | [diff] [blame] | 1736 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1737 | CollectScrollDeltas(scroll_info.get(), active_tree_->root_layer()); |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 1738 | scroll_info->page_scale_delta = active_tree_->page_scale_delta(); |
| 1739 | active_tree_->set_sent_page_scale_delta(scroll_info->page_scale_delta); |
[email protected] | 362f1e8b | 2013-01-21 16:54:30 | [diff] [blame] | 1740 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1741 | return scroll_info.Pass(); |
[email protected] | 362f1e8b | 2013-01-21 16:54:30 | [diff] [blame] | 1742 | } |
| 1743 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1744 | void LayerTreeHostImpl::SetFullRootLayerDamage() { |
| 1745 | if (active_tree_->root_layer()) { |
| 1746 | RenderSurfaceImpl* render_surface = |
| 1747 | active_tree_->root_layer()->render_surface(); |
| 1748 | if (render_surface) |
| 1749 | render_surface->damage_tracker()->ForceFullDamageNextUpdate(); |
| 1750 | } |
[email protected] | 829ad97 | 2013-01-28 23:36:10 | [diff] [blame] | 1751 | } |
| 1752 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1753 | void LayerTreeHostImpl::AnimatePageScale(base::TimeTicks time) { |
| 1754 | if (!page_scale_animation_ || !RootScrollLayer()) |
| 1755 | return; |
| 1756 | |
| 1757 | double monotonic_time = (time - base::TimeTicks()).InSecondsF(); |
| 1758 | gfx::Vector2dF scroll_total = RootScrollLayer()->scroll_offset() + |
| 1759 | RootScrollLayer()->scroll_delta(); |
| 1760 | |
| 1761 | active_tree_->SetPageScaleDelta( |
| 1762 | page_scale_animation_->PageScaleFactorAtTime(monotonic_time) / |
| 1763 | active_tree_->page_scale_factor()); |
| 1764 | gfx::Vector2dF next_scroll = |
| 1765 | page_scale_animation_->ScrollOffsetAtTime(monotonic_time); |
| 1766 | |
| 1767 | RootScrollLayer()->ScrollBy(next_scroll - scroll_total); |
| 1768 | client_->SetNeedsRedrawOnImplThread(); |
| 1769 | |
| 1770 | if (page_scale_animation_->IsAnimationCompleteAtTime(monotonic_time)) { |
| 1771 | page_scale_animation_.reset(); |
| 1772 | client_->SetNeedsCommitOnImplThread(); |
| 1773 | client_->RenewTreePriority(); |
| 1774 | } |
[email protected] | 829ad97 | 2013-01-28 23:36:10 | [diff] [blame] | 1775 | } |
| 1776 | |
[email protected] | ffb2720f | 2013-03-15 19:18:37 | [diff] [blame] | 1777 | void LayerTreeHostImpl::AnimateTopControls(base::TimeTicks time) { |
| 1778 | if (!top_controls_manager_ || !RootScrollLayer()) |
| 1779 | return; |
| 1780 | gfx::Vector2dF scroll = top_controls_manager_->Animate(time); |
| 1781 | UpdateMaxScrollOffset(); |
| 1782 | RootScrollLayer()->ScrollBy(gfx::ScaleVector2d( |
| 1783 | scroll, 1.f / active_tree_->total_page_scale_factor())); |
| 1784 | } |
| 1785 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1786 | void LayerTreeHostImpl::AnimateLayers(base::TimeTicks monotonic_time, |
| 1787 | base::Time wall_clock_time) { |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 1788 | if (!settings_.accelerated_animation_enabled || |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1789 | animation_registrar_->active_animation_controllers().empty() || |
| 1790 | !active_tree_->root_layer()) |
| 1791 | return; |
| 1792 | |
| 1793 | TRACE_EVENT0("cc", "LayerTreeHostImpl::AnimateLayers"); |
| 1794 | |
| 1795 | last_animation_time_ = wall_clock_time; |
| 1796 | double monotonic_seconds = (monotonic_time - base::TimeTicks()).InSecondsF(); |
| 1797 | |
| 1798 | AnimationRegistrar::AnimationControllerMap copy = |
| 1799 | animation_registrar_->active_animation_controllers(); |
| 1800 | for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin(); |
| 1801 | iter != copy.end(); |
| 1802 | ++iter) |
| 1803 | (*iter).second->Animate(monotonic_seconds); |
| 1804 | |
| 1805 | client_->SetNeedsRedrawOnImplThread(); |
| 1806 | SetBackgroundTickingEnabled( |
| 1807 | !visible_ && |
| 1808 | !animation_registrar_->active_animation_controllers().empty()); |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 1809 | } |
| 1810 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1811 | void LayerTreeHostImpl::UpdateAnimationState() { |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 1812 | if (!settings_.accelerated_animation_enabled || |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1813 | animation_registrar_->active_animation_controllers().empty() || |
| 1814 | !active_tree_->root_layer()) |
| 1815 | return; |
| 1816 | |
| 1817 | TRACE_EVENT0("cc", "LayerTreeHostImpl::UpdateAnimationState"); |
| 1818 | scoped_ptr<AnimationEventsVector> events = |
| 1819 | make_scoped_ptr(new AnimationEventsVector); |
| 1820 | AnimationRegistrar::AnimationControllerMap copy = |
| 1821 | animation_registrar_->active_animation_controllers(); |
| 1822 | for (AnimationRegistrar::AnimationControllerMap::iterator iter = copy.begin(); |
| 1823 | iter != copy.end(); |
| 1824 | ++iter) |
| 1825 | (*iter).second->UpdateState(events.get()); |
| 1826 | |
| 1827 | if (!events->empty()) { |
| 1828 | client_->PostAnimationEventsToMainThreadOnImplThread(events.Pass(), |
| 1829 | last_animation_time_); |
| 1830 | } |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 1831 | } |
| 1832 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1833 | base::TimeDelta LayerTreeHostImpl::LowFrequencyAnimationInterval() const { |
| 1834 | return base::TimeDelta::FromSeconds(1); |
| 1835 | } |
| 1836 | |
| 1837 | void LayerTreeHostImpl::SendDidLoseOutputSurfaceRecursive(LayerImpl* current) { |
| 1838 | DCHECK(current); |
| 1839 | current->DidLoseOutputSurface(); |
| 1840 | if (current->mask_layer()) |
| 1841 | SendDidLoseOutputSurfaceRecursive(current->mask_layer()); |
| 1842 | if (current->replica_layer()) |
| 1843 | SendDidLoseOutputSurfaceRecursive(current->replica_layer()); |
| 1844 | for (size_t i = 0; i < current->children().size(); ++i) |
| 1845 | SendDidLoseOutputSurfaceRecursive(current->children()[i]); |
| 1846 | } |
| 1847 | |
| 1848 | void LayerTreeHostImpl::ClearRenderSurfaces() { |
| 1849 | active_tree_->ClearRenderSurfaces(); |
| 1850 | if (pending_tree_) |
| 1851 | pending_tree_->ClearRenderSurfaces(); |
| 1852 | } |
| 1853 | |
| 1854 | std::string LayerTreeHostImpl::LayerTreeAsText() const { |
| 1855 | std::string str; |
| 1856 | if (active_tree_->root_layer()) { |
| 1857 | str = active_tree_->root_layer()->LayerTreeAsText(); |
| 1858 | str += "RenderSurfaces:\n"; |
| 1859 | DumpRenderSurfaces(&str, 1, active_tree_->root_layer()); |
| 1860 | } |
| 1861 | return str; |
| 1862 | } |
| 1863 | |
| 1864 | std::string LayerTreeHostImpl::LayerTreeAsJson() const { |
| 1865 | std::string str; |
| 1866 | if (active_tree_->root_layer()) { |
| 1867 | scoped_ptr<base::Value> json(active_tree_->root_layer()->LayerTreeAsJson()); |
| 1868 | base::JSONWriter::WriteWithOptions( |
| 1869 | json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &str); |
| 1870 | } |
| 1871 | return str; |
| 1872 | } |
| 1873 | |
| 1874 | void LayerTreeHostImpl::DumpRenderSurfaces(std::string* str, |
| 1875 | int indent, |
| 1876 | const LayerImpl* layer) const { |
| 1877 | if (layer->render_surface()) |
| 1878 | layer->render_surface()->DumpSurface(str, indent); |
| 1879 | |
| 1880 | for (size_t i = 0; i < layer->children().size(); ++i) |
| 1881 | DumpRenderSurfaces(str, indent, layer->children()[i]); |
| 1882 | } |
| 1883 | |
| 1884 | int LayerTreeHostImpl::SourceAnimationFrameNumber() const { |
[email protected] | 9e359452 | 2013-03-18 00:57:36 | [diff] [blame] | 1885 | return fps_counter_->current_frame_number(); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1886 | } |
| 1887 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1888 | void LayerTreeHostImpl::SendManagedMemoryStats( |
| 1889 | size_t memory_visible_bytes, |
| 1890 | size_t memory_visible_and_nearby_bytes, |
| 1891 | size_t memory_use_bytes) { |
| 1892 | if (!renderer_) |
| 1893 | return; |
| 1894 | |
| 1895 | // Round the numbers being sent up to the next 8MB, to throttle the rate |
| 1896 | // at which we spam the GPU process. |
| 1897 | static const size_t rounding_step = 8 * 1024 * 1024; |
| 1898 | memory_visible_bytes = RoundUp(memory_visible_bytes, rounding_step); |
| 1899 | memory_visible_and_nearby_bytes = RoundUp(memory_visible_and_nearby_bytes, |
| 1900 | rounding_step); |
| 1901 | memory_use_bytes = RoundUp(memory_use_bytes, rounding_step); |
| 1902 | if (last_sent_memory_visible_bytes_ == memory_visible_bytes && |
| 1903 | last_sent_memory_visible_and_nearby_bytes_ == |
| 1904 | memory_visible_and_nearby_bytes && |
| 1905 | last_sent_memory_use_bytes_ == memory_use_bytes) { |
| 1906 | return; |
| 1907 | } |
| 1908 | last_sent_memory_visible_bytes_ = memory_visible_bytes; |
| 1909 | last_sent_memory_visible_and_nearby_bytes_ = memory_visible_and_nearby_bytes; |
| 1910 | last_sent_memory_use_bytes_ = memory_use_bytes; |
| 1911 | |
| 1912 | renderer_->SendManagedMemoryStats(last_sent_memory_visible_bytes_, |
| 1913 | last_sent_memory_visible_and_nearby_bytes_, |
| 1914 | last_sent_memory_use_bytes_); |
| 1915 | } |
| 1916 | |
| 1917 | void LayerTreeHostImpl::AnimateScrollbars(base::TimeTicks time) { |
| 1918 | AnimateScrollbarsRecursive(active_tree_->root_layer(), time); |
| 1919 | } |
| 1920 | |
| 1921 | void LayerTreeHostImpl::AnimateScrollbarsRecursive(LayerImpl* layer, |
| 1922 | base::TimeTicks time) { |
| 1923 | if (!layer) |
| 1924 | return; |
| 1925 | |
| 1926 | ScrollbarAnimationController* scrollbar_controller = |
| 1927 | layer->scrollbar_animation_controller(); |
[email protected] | 6bc09e8 | 2013-03-19 03:48:35 | [diff] [blame] | 1928 | if (scrollbar_controller && scrollbar_controller->Animate(time)) { |
[email protected] | 0fc818e | 2013-03-18 06:45:20 | [diff] [blame] | 1929 | TRACE_EVENT_INSTANT0( |
[email protected] | c76faea | 2013-03-26 07:42:42 | [diff] [blame] | 1930 | "cc", "LayerTreeHostImpl::SetNeedsRedraw due to AnimateScrollbars", |
| 1931 | TRACE_EVENT_SCOPE_THREAD); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1932 | client_->SetNeedsRedrawOnImplThread(); |
[email protected] | 0fc818e | 2013-03-18 06:45:20 | [diff] [blame] | 1933 | } |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1934 | |
| 1935 | for (size_t i = 0; i < layer->children().size(); ++i) |
| 1936 | AnimateScrollbarsRecursive(layer->children()[i], time); |
| 1937 | } |
| 1938 | |
[email protected] | 0fc818e | 2013-03-18 06:45:20 | [diff] [blame] | 1939 | void LayerTreeHostImpl::StartScrollbarAnimation(base::TimeTicks time) { |
| 1940 | TRACE_EVENT0("cc", "LayerTreeHostImpl::StartScrollbarAnimation"); |
| 1941 | StartScrollbarAnimationRecursive(RootLayer(), time); |
| 1942 | } |
| 1943 | |
| 1944 | void LayerTreeHostImpl::StartScrollbarAnimationRecursive(LayerImpl* layer, |
| 1945 | base::TimeTicks time) { |
| 1946 | if (!layer) |
| 1947 | return; |
| 1948 | |
| 1949 | ScrollbarAnimationController* scrollbar_controller = |
| 1950 | layer->scrollbar_animation_controller(); |
[email protected] | 6bc09e8 | 2013-03-19 03:48:35 | [diff] [blame] | 1951 | if (scrollbar_controller && scrollbar_controller->IsAnimating()) { |
| 1952 | base::TimeDelta delay = scrollbar_controller->DelayBeforeStart(time); |
[email protected] | 0fc818e | 2013-03-18 06:45:20 | [diff] [blame] | 1953 | if (delay > base::TimeDelta()) |
| 1954 | client_->RequestScrollbarAnimationOnImplThread(delay); |
[email protected] | 6bc09e8 | 2013-03-19 03:48:35 | [diff] [blame] | 1955 | else if (scrollbar_controller->Animate(time)) |
[email protected] | 0fc818e | 2013-03-18 06:45:20 | [diff] [blame] | 1956 | client_->SetNeedsRedrawOnImplThread(); |
| 1957 | } |
| 1958 | |
| 1959 | for (size_t i = 0; i < layer->children().size(); ++i) |
| 1960 | StartScrollbarAnimationRecursive(layer->children()[i], time); |
| 1961 | } |
| 1962 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1963 | void LayerTreeHostImpl::SetTreePriority(TreePriority priority) { |
| 1964 | if (!tile_manager_) |
| 1965 | return; |
| 1966 | |
| 1967 | GlobalStateThatImpactsTilePriority new_state(tile_manager_->GlobalState()); |
| 1968 | if (new_state.tree_priority == priority) |
| 1969 | return; |
| 1970 | |
| 1971 | new_state.tree_priority = priority; |
| 1972 | tile_manager_->SetGlobalState(new_state); |
| 1973 | } |
| 1974 | |
| 1975 | void LayerTreeHostImpl::BeginNextFrame() { |
| 1976 | current_frame_time_ = base::TimeTicks(); |
| 1977 | } |
| 1978 | |
| 1979 | base::TimeTicks LayerTreeHostImpl::CurrentFrameTime() { |
| 1980 | if (current_frame_time_.is_null()) |
| 1981 | current_frame_time_ = base::TimeTicks::Now(); |
| 1982 | return current_frame_time_; |
| 1983 | } |
| 1984 | |
| 1985 | scoped_ptr<base::Value> LayerTreeHostImpl::AsValue() const { |
| 1986 | scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue()); |
| 1987 | state->Set("activation_state", ActivationStateAsValue().release()); |
| 1988 | state->Set("frame_state", FrameStateAsValue().release()); |
| 1989 | return state.PassAs<base::Value>(); |
| 1990 | } |
| 1991 | |
| 1992 | scoped_ptr<base::Value> LayerTreeHostImpl::ActivationStateAsValue() const { |
| 1993 | scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue()); |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 1994 | state->SetString("lthi_id", base::StringPrintf("%p", this)); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 1995 | state->SetBoolean("visible_resources_ready", |
| 1996 | pending_tree_->AreVisibleResourcesReady()); |
| 1997 | state->Set("tile_manager", tile_manager_->BasicStateAsValue().release()); |
| 1998 | return state.PassAs<base::Value>(); |
| 1999 | } |
| 2000 | |
| 2001 | scoped_ptr<base::Value> LayerTreeHostImpl::FrameStateAsValue() const { |
| 2002 | scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue()); |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 2003 | state->SetString("lthi_id", base::StringPrintf("%p", this)); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 2004 | state->Set("device_viewport_size", |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 2005 | MathUtil::AsValue(device_viewport_size_).release()); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 2006 | if (tile_manager_) |
| 2007 | state->Set("tiles", tile_manager_->AllTilesAsValue().release()); |
| 2008 | state->Set("active_tree", active_tree_->AsValue().release()); |
| 2009 | return state.PassAs<base::Value>(); |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 2010 | } |
| 2011 | |
[email protected] | b9dcf43a | 2013-01-09 00:15:29 | [diff] [blame] | 2012 | // static |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 2013 | LayerImpl* LayerTreeHostImpl::GetNonCompositedContentLayerRecursive( |
| 2014 | LayerImpl* layer) { |
| 2015 | if (!layer) |
[email protected] | b9dcf43a | 2013-01-09 00:15:29 | [diff] [blame] | 2016 | return NULL; |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 2017 | |
| 2018 | if (layer->DrawsContent()) |
| 2019 | return layer; |
| 2020 | |
| 2021 | for (LayerImpl::LayerList::const_iterator it = layer->children().begin(); |
| 2022 | it != layer->children().end(); ++it) { |
| 2023 | LayerImpl* nccr = GetNonCompositedContentLayerRecursive(*it); |
| 2024 | if (nccr) |
| 2025 | return nccr; |
| 2026 | } |
| 2027 | |
| 2028 | return NULL; |
[email protected] | b9dcf43a | 2013-01-09 00:15:29 | [diff] [blame] | 2029 | } |
| 2030 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 2031 | skia::RefPtr<SkPicture> LayerTreeHostImpl::CapturePicture() { |
| 2032 | LayerTreeImpl* tree = |
| 2033 | pending_tree_ ? pending_tree_.get() : active_tree_.get(); |
| 2034 | LayerImpl* layer = GetNonCompositedContentLayerRecursive(tree->root_layer()); |
| 2035 | return layer ? layer->GetPicture() : skia::RefPtr<SkPicture>(); |
[email protected] | b9dcf43a | 2013-01-09 00:15:29 | [diff] [blame] | 2036 | } |
| 2037 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 2038 | void LayerTreeHostImpl::SetDebugState(const LayerTreeDebugState& debug_state) { |
[email protected] | 846f455b | 2013-03-18 19:07:41 | [diff] [blame] | 2039 | if (debug_state_.continuous_painting != debug_state.continuous_painting) |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 2040 | paint_time_counter_->ClearHistory(); |
[email protected] | 652cf13 | 2013-02-15 21:53:24 | [diff] [blame] | 2041 | |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 2042 | debug_state_ = debug_state; |
[email protected] | d0d1219 | 2013-02-08 19:02:02 | [diff] [blame] | 2043 | } |
| 2044 | |
[email protected] | d3143c73 | 2012-10-05 19:17:59 | [diff] [blame] | 2045 | } // namespace cc |